comparison tests/libclient/js-image/main.cpp @ 19:6400830bb36b

Client: add some JavaScript bindings for Image, #457
author David Demelier <markand@malikania.fr>
date Mon, 04 Apr 2016 13:20:06 +0200
parents
children 7f7c2607ace3
comparison
equal deleted inserted replaced
18:cc13926bed59 19:6400830bb36b
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 #include <gtest/gtest.h>
23
24 #include <malikania/client-resources-loader.h>
25 #include <malikania/js-image.h>
26 #include <malikania/js-window.h>
27 #include <malikania/resources-locator.h>
28
29 using namespace malikania;
30
31 using namespace std::chrono_literals;
32
33 class TestImage : public testing::Test {
34 protected:
35 ResourcesLocatorDirectory m_locator;
36 ClientResourcesLoader m_loader;
37
38 duk::Context m_ctx;
39
40 public:
41 TestImage()
42 : m_locator(SOURCE_DIRECTORY "/resources")
43 , m_loader(m_locator)
44 {
45 duk::putGlobal(m_ctx, "Malikania", duk::Object());
46
47 loadMalikaniaImage(m_ctx);
48 loadMalikaniaWindow(m_ctx);
49
50 /* Store the loader */
51 duk::putGlobal(m_ctx, "\xff""\xff""loader", duk::RawPointer<ClientResourcesLoader>{&m_loader});
52 }
53 };
54
55 TEST_F(TestImage, basic)
56 {
57 try {
58 auto ret = duk::pevalString(m_ctx,
59 "w = new Malikania.Window();"
60 "i = new Malikania.Image('images/smiley.png');"
61 "w.setDrawingColor('lightskyblue');"
62 "w.clear();"
63 "i.draw(w, { x: 320 - 16, y: 240 - 16 });"
64 "w.present();"
65 );
66
67 if (ret != 0) {
68 throw duk::error(m_ctx, -1);
69 }
70
71 std::this_thread::sleep_for(3s);
72 } catch (const std::exception &ex) {
73 FAIL() << ex.what();
74 }
75 }
76
77 TEST_F(TestImage, stretch)
78 {
79 try {
80 auto ret = duk::pevalString(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, null, { x: 10, y: 10, width: 620, height: 460 });"
86 "w.present();"
87 );
88
89 if (ret != 0) {
90 throw duk::error(m_ctx, -1);
91 }
92
93 std::this_thread::sleep_for(3s);
94 } catch (const std::exception &ex) {
95 FAIL() << ex.what();
96 }
97 }
98
99 int main(int argc, char **argv)
100 {
101 testing::InitGoogleTest(&argc, argv);
102
103 return RUN_ALL_TESTS();
104 }