comparison examples/image/main.cpp @ 74:78de82cc6bde

Tests: move graphical examples into examples, closes #606
author David Demelier <markand@malikania.fr>
date Thu, 22 Dec 2016 15:17:43 +0100
parents
children a162f380f02e
comparison
equal deleted inserted replaced
73:efe1a7fb43f8 74:78de82cc6bde
1 /*
2 * main.cpp -- test Image
3 *
4 * Copyright (c) 2013-2016 Malikania Authors
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <chrono>
20 #include <thread>
21
22 #define BOOST_TEST_MODULE "Image"
23 #include <boost/test/unit_test.hpp>
24
25 #include <malikania/client_resources_loader.hpp>
26 #include <malikania/image.hpp>
27 #include <malikania/resources_locator.hpp>
28 #include <malikania/window.hpp>
29
30 using namespace std::chrono_literals;
31
32 namespace {
33
34 mlk::window window(400, 400);
35
36 } // !namespace
37
38 class test_image {
39 protected:
40 mlk::directory_resources_locator m_locator;
41 mlk::client_resources_loader m_loader;
42
43 public:
44 test_image()
45 : m_locator(SOURCE_DIRECTORY "/resources")
46 , m_loader(m_locator)
47 {
48 }
49 };
50
51 BOOST_FIXTURE_TEST_SUITE(test_image_suite, test_image)
52
53 BOOST_AUTO_TEST_CASE(image100x10)
54 {
55 try {
56 auto image = m_loader.load_image("images/100x10.png");
57
58 BOOST_REQUIRE_EQUAL(100U, image.size().width());
59 BOOST_REQUIRE_EQUAL(10U, image.size().height());
60 } catch (const std::exception &ex) {
61 BOOST_FAIL(ex.what());
62 }
63 }
64
65 BOOST_AUTO_TEST_CASE(image16x16)
66 {
67 try {
68 auto image = m_loader.load_image("images/16x16.png");
69
70 BOOST_REQUIRE_EQUAL(16U, image.size().width());
71 BOOST_REQUIRE_EQUAL(16U, image.size().height());
72 } catch (const std::exception &ex) {
73 BOOST_FAIL(ex.what());
74 }
75 }
76
77 BOOST_AUTO_TEST_CASE(image10x100)
78 {
79 try {
80 auto image = m_loader.load_image("images/10x100.png");
81
82 BOOST_REQUIRE_EQUAL(10U, image.size().width());
83 BOOST_REQUIRE_EQUAL(100U, image.size().height());
84 } catch (const std::exception &ex) {
85 BOOST_FAIL(ex.what());
86 }
87 }
88
89 BOOST_AUTO_TEST_CASE(notfound)
90 {
91 BOOST_REQUIRE_THROW(m_loader.load_image("image-not-found"), std::exception);
92 }
93
94 BOOST_AUTO_TEST_CASE(draw)
95 {
96 try {
97 auto image = m_loader.load_image("images/smiley.png");
98 auto x = (400 / 2) - (image.size().width() / 2);
99 auto y = (400 / 2) - (image.size().height() / 2);
100
101 window.clear();
102 image.draw(window, mlk::point(x, y));
103 window.present();
104
105 std::this_thread::sleep_for(3s);
106 } catch (const std::exception &ex) {
107 BOOST_FAIL(ex.what());
108 }
109 }
110
111 BOOST_AUTO_TEST_SUITE_END()