Mercurial > malikania
comparison examples/js-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 (JavaScript binding) | |
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 "Javacript Image" | |
23 #include <boost/test/unit_test.hpp> | |
24 | |
25 #include <malikania/js_client_resources_loader.hpp> | |
26 #include <malikania/js_image.hpp> | |
27 #include <malikania/js_window.hpp> | |
28 #include <malikania/resources_locator.hpp> | |
29 | |
30 using namespace std::chrono_literals; | |
31 | |
32 class test_image { | |
33 protected: | |
34 mlk::directory_resources_locator m_locator; | |
35 mlk::client_resources_loader m_loader; | |
36 dukx_context m_ctx; | |
37 | |
38 public: | |
39 test_image() | |
40 : m_locator(SOURCE_DIRECTORY "/resources") | |
41 , m_loader(m_locator) | |
42 { | |
43 duk_push_object(m_ctx); | |
44 duk_put_global_string(m_ctx, "Malikania"); | |
45 mlk::dukx_load_image(m_ctx); | |
46 mlk::dukx_load_window(m_ctx); | |
47 dukx_put_client_loader(m_ctx, m_loader); | |
48 } | |
49 }; | |
50 | |
51 BOOST_FIXTURE_TEST_SUITE(test_image_suite, test_image) | |
52 | |
53 BOOST_AUTO_TEST_CASE(size) | |
54 { | |
55 try { | |
56 auto ret = duk_peval_string(m_ctx, | |
57 "i = new Malikania.Image('images/smiley.png');" | |
58 "w = i.size.width;" | |
59 "h = i.size.height;" | |
60 ); | |
61 | |
62 if (ret != 0) { | |
63 throw dukx_get_exception(m_ctx, -1); | |
64 } | |
65 | |
66 duk_get_global_string(m_ctx, "w"); | |
67 BOOST_REQUIRE_EQUAL(32U, duk_to_uint(m_ctx, -1)); | |
68 duk_pop(m_ctx); | |
69 duk_get_global_string(m_ctx, "h"); | |
70 BOOST_REQUIRE_EQUAL(32U, duk_to_uint(m_ctx, -1)); | |
71 duk_pop(m_ctx); | |
72 } catch (const std::exception &ex) { | |
73 BOOST_FAIL(ex.what()); | |
74 } | |
75 } | |
76 | |
77 BOOST_AUTO_TEST_CASE(basic) | |
78 { | |
79 try { | |
80 auto ret = duk_peval_string(m_ctx, | |
81 "w = new Malikania.Window();" | |
82 "i = new Malikania.Image('images/smiley.png');" | |
83 "w.setDrawingColor('lightskyblue');" | |
84 "w.clear();" | |
85 "i.draw(w, { x: 320 - 16, y: 240 - 16 });" | |
86 "w.present();" | |
87 ); | |
88 | |
89 if (ret != 0) { | |
90 throw dukx_get_exception(m_ctx, -1); | |
91 } | |
92 | |
93 std::this_thread::sleep_for(3s); | |
94 } catch (const std::exception &ex) { | |
95 BOOST_FAIL(ex.what()); | |
96 } | |
97 } | |
98 | |
99 BOOST_AUTO_TEST_CASE(stretch) | |
100 { | |
101 try { | |
102 auto ret = duk_peval_string(m_ctx, | |
103 "w = new Malikania.Window();" | |
104 "i = new Malikania.Image('images/smiley.png');" | |
105 "w.setDrawingColor('lightskyblue');" | |
106 "w.clear();" | |
107 "i.draw(w, null, { x: 10, y: 10, width: 620, height: 460 });" | |
108 "w.present();" | |
109 ); | |
110 | |
111 if (ret != 0) { | |
112 throw dukx_get_exception(m_ctx, -1); | |
113 } | |
114 | |
115 std::this_thread::sleep_for(3s); | |
116 } catch (const std::exception &ex) { | |
117 BOOST_FAIL(ex.what()); | |
118 } | |
119 } | |
120 | |
121 BOOST_AUTO_TEST_SUITE_END() |