comparison tests/libclient/sprite/main.cpp @ 0:8991989c4708

Initial import
author David Demelier <markand@malikania.fr>
date Tue, 22 Mar 2016 18:26:05 +0100
parents
children 45b3c770803c
comparison
equal deleted inserted replaced
-1:000000000000 0:8991989c4708
1 /*
2 * main.cpp -- test Sprite
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 #include <exception>
22
23 #include <gtest/gtest.h>
24
25 #include <malikania/ClientResourcesLoader.h>
26 #include <malikania/ResourcesLocator.h>
27 #include <malikania/Sprite.h>
28 #include <malikania/Window.h>
29
30 using namespace malikania;
31
32 using namespace std::chrono_literals;
33
34 namespace {
35
36 Window window(400, 400);
37
38 } // !namespace
39
40 class TestSprite : public testing::Test {
41 protected:
42 ResourcesLocatorDirectory m_locator;
43 ClientResourcesLoader m_loader;
44
45 public:
46 TestSprite()
47 : m_locator(SOURCE_DIRECTORY "/resources")
48 , m_loader(window, m_locator)
49 {
50 }
51 };
52
53 /*
54 * Missing properties
55 * ------------------------------------------------------------------
56 */
57
58 TEST_F(TestSprite, missingPropertyImage)
59 {
60 try {
61 m_loader.loadSprite("sprites/no-property-image.json");
62
63 FAIL() << "exception expected";
64 } catch (const std::exception &) {
65 }
66 }
67
68 TEST_F(TestSprite, missingPropertyCell)
69 {
70 try {
71 m_loader.loadSprite("sprites/no-property-cell.json");
72
73 FAIL() << "exception expected";
74 } catch (const std::exception &) {
75 }
76 }
77
78 /*
79 * Invalid properties
80 * ------------------------------------------------------------------
81 */
82
83 TEST_F(TestSprite, imageNotString)
84 {
85 try {
86 m_loader.loadSprite("sprites/property-image-not-string.json");
87
88 FAIL() << "exception expected";
89 } catch (const std::exception &) {
90 }
91 }
92
93 TEST_F(TestSprite, cellNotArray)
94 {
95 try {
96 m_loader.loadSprite("sprites/property-cell-not-array.json");
97
98 FAIL() << "exception expected";
99 } catch (const std::exception &) {
100 }
101 }
102
103 TEST_F(TestSprite, cellNotArray2)
104 {
105 try {
106 m_loader.loadSprite("sprites/property-cell-not-array2.json");
107
108 FAIL() << "exception expected";
109 } catch (const std::exception &) {
110 }
111 }
112
113 /*
114 * Other errors
115 * ------------------------------------------------------------------
116 */
117
118 TEST_F(TestSprite, imageNotFound)
119 {
120 try {
121 m_loader.loadSprite("sprites/image-not-found.json");
122
123 FAIL() << "exception expected";
124 } catch (const std::exception &) {
125 }
126 }
127
128 TEST_F(TestSprite, notObject)
129 {
130 try {
131 m_loader.loadSprite("sprites/not-object.json");
132
133 FAIL() << "exception expected";
134 } catch (const std::exception &) {
135 }
136 }
137
138 /*
139 * Valid sprites
140 * ------------------------------------------------------------------
141 */
142
143 TEST_F(TestSprite, standard)
144 {
145 try {
146 Sprite sprite = m_loader.loadSprite("sprites/simple.json");
147
148 ASSERT_EQ(300U, sprite.cell().width());
149 ASSERT_EQ(300U, sprite.cell().height());
150 } catch (const std::exception &ex) {
151 FAIL() << ex.what();
152 }
153 }
154
155 TEST_F(TestSprite, margins)
156 {
157 try {
158 Sprite sprite = m_loader.loadSprite("sprites/margins.json");
159
160 ASSERT_EQ(3U, sprite.rows());
161 ASSERT_EQ(4U, sprite.columns());
162 } catch (const std::exception &ex) {
163 FAIL() << ex.what();
164 }
165 }
166
167 TEST_F(TestSprite, draw)
168 {
169 try {
170 Sprite sprite = m_loader.loadSprite("sprites/margins.json");
171
172 unsigned total = sprite.rows() * sprite.columns();
173
174 auto x = (400 / 2) - (sprite.cell().width() / 2);
175 auto y = (400 / 2) - (sprite.cell().height() / 2);
176
177 for (unsigned c = 0; c < total; ++c) {
178 window.clear();
179 sprite.draw(window, c, Point(x, y));
180 window.present();
181
182 std::this_thread::sleep_for(1s);
183 }
184 } catch (const std::exception &ex) {
185 FAIL() << ex.what();
186 }
187 }
188
189 int main(int argc, char **argv)
190 {
191 testing::InitGoogleTest(&argc, argv);
192
193 return RUN_ALL_TESTS();
194 }