comparison tests/libclient/js-animation/main.cpp @ 23:ed63752a8720

Client: add JavaScript bindings for Animation, Animator, #454, #467
author David Demelier <markand@malikania.fr>
date Tue, 05 Apr 2016 21:06:27 +0200
parents
children 0a1adf7dcca0
comparison
equal deleted inserted replaced
22:1533fe3e3d64 23:ed63752a8720
1 /*
2 * main.cpp -- test Animation (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/elapsed-timer.h>
26 #include <malikania/js-animation.h>
27 #include <malikania/js-animator.h>
28 #include <malikania/js-window.h>
29 #include <malikania/resources-locator.h>
30
31 using namespace malikania;
32
33 using namespace std::chrono_literals;
34
35 class TestAnimation : public testing::Test {
36 protected:
37 ResourcesLocatorDirectory m_locator;
38 ClientResourcesLoader m_loader;
39
40 duk::Context m_ctx;
41
42 public:
43 TestAnimation()
44 : m_locator(SOURCE_DIRECTORY "/resources")
45 , m_loader(m_locator)
46 {
47 duk::putGlobal(m_ctx, "Malikania", duk::Object());
48
49 loadMalikaniaAnimation(m_ctx);
50 loadMalikaniaAnimator(m_ctx);
51 loadMalikaniaWindow(m_ctx);
52
53 /* Store the loader */
54 duk::putGlobal(m_ctx, "\xff""\xff""loader", duk::RawPointer<ClientResourcesLoader>{&m_loader});
55 }
56 };
57
58 TEST_F(TestAnimation, basic)
59 {
60 try {
61 auto ret = duk::pevalString(m_ctx,
62 "w = new Malikania.Window();"
63 "a = new Malikania.Animation('animations/margins.json');"
64 "d = new Malikania.Animator(a);"
65 );
66
67 if (ret != 0) {
68 throw duk::error(m_ctx, -1);
69 }
70
71 ElapsedTimer timer;
72
73 while (timer.elapsed() < 8000) {
74 auto ret = duk::pevalString(m_ctx,
75 "w.setDrawingColor('lightskyblue');"
76 "w.clear();"
77 "d.draw(w, { x: 320 - 16, y: 240 - 16 });"
78 "d.update();"
79 "w.present();"
80 );
81
82 if (ret != 0) {
83 throw duk::error(m_ctx, -1);
84 }
85 }
86
87 std::this_thread::sleep_for(3s);
88 } catch (const std::exception &ex) {
89 FAIL() << ex.what();
90 }
91 }
92
93 int main(int argc, char **argv)
94 {
95 testing::InitGoogleTest(&argc, argv);
96
97 return RUN_ALL_TESTS();
98 }