comparison tests/libclient/js-sprite/main.cpp @ 21:a087bc11ba04

Client: add JavaScript bindings for Sprite, #461
author David Demelier <markand@malikania.fr>
date Mon, 04 Apr 2016 21:35:13 +0200
parents
children dc47ce56ce36
comparison
equal deleted inserted replaced
20:787c2adb366c 21:a087bc11ba04
1 /*
2 * main.cpp -- test Sprite (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-sprite.h>
27 #include <malikania/js-window.h>
28 #include <malikania/resources-locator.h>
29
30 using namespace malikania;
31
32 using namespace std::chrono_literals;
33
34 class TestSprite : public testing::Test {
35 protected:
36 ResourcesLocatorDirectory m_locator;
37 ClientResourcesLoader m_loader;
38
39 duk::Context m_ctx;
40
41 public:
42 TestSprite()
43 : m_locator(SOURCE_DIRECTORY "/resources")
44 , m_loader(m_locator)
45 {
46 duk::putGlobal(m_ctx, "Malikania", duk::Object());
47
48 loadMalikaniaImage(m_ctx);
49 loadMalikaniaSprite(m_ctx);
50 loadMalikaniaWindow(m_ctx);
51
52 /* Store the loader */
53 duk::putGlobal(m_ctx, "\xff""\xff""loader", duk::RawPointer<ClientResourcesLoader>{&m_loader});
54 }
55 };
56
57 TEST_F(TestSprite, basic)
58 {
59 try {
60 auto ret = duk::pevalString(m_ctx,
61 "w = new Malikania.Window();"
62 "s = new Malikania.Sprite('sprites/margins.json');"
63 "c = 0;"
64 );
65
66 if (ret != 0) {
67 throw duk::error(m_ctx, -1);
68 }
69
70 for (unsigned c = 0; c < 12; ++c) {
71 auto ret = duk::pevalString(m_ctx,
72 "w.setDrawingColor('lightskyblue');"
73 "w.clear();"
74 "s.draw(w, c++, { x: 320 - 16, y: 240 - 16 });"
75 "w.present();"
76 );
77
78 if (ret != 0) {
79 throw duk::error(m_ctx, -1);
80 }
81
82 std::this_thread::sleep_for(1s);
83 }
84 } catch (const std::exception &ex) {
85 FAIL() << ex.what();
86 }
87 }
88
89 int main(int argc, char **argv)
90 {
91 testing::InitGoogleTest(&argc, argv);
92
93 return RUN_ALL_TESTS();
94 }