forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_ui_tests.cpp
329 lines (272 loc) · 8.36 KB
/
grid_ui_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Aseprite UI Library
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#define TEST_GUI
#include "tests/app_test.h"
#include "gfx/rect_io.h"
#include "gfx/size.h"
using namespace gfx;
using namespace ui;
// Tests two widgets in a row, each one of 10x10 pixels.
TEST(Grid, Simple2x1Grid)
{
Grid* grid = new Grid(2, false);
Widget* w1 = new Widget;
Widget* w2 = new Widget;
w1->setMinSize(gfx::Size(10, 10));
w2->setMinSize(gfx::Size(10, 10));
grid->addChildInCell(w1, 1, 1, 0);
grid->addChildInCell(w2, 1, 1, 0);
// Test request-size
Size reqSize = grid->sizeHint();
EXPECT_EQ(20, reqSize.w);
EXPECT_EQ(10, reqSize.h);
// Test child-spacing
grid->setChildSpacing(2);
reqSize = grid->sizeHint();
EXPECT_EQ(22, reqSize.w);
EXPECT_EQ(10, reqSize.h);
// Test borders
grid->setBorder(gfx::Border(3, 0, 0, 3));
reqSize = grid->sizeHint();
EXPECT_EQ(25, reqSize.w);
EXPECT_EQ(13, reqSize.h);
grid->setBorder(gfx::Border(3, 2, 5, 3));
reqSize = grid->sizeHint();
EXPECT_EQ(30, reqSize.w);
EXPECT_EQ(15, reqSize.h);
delete grid;
}
TEST(Grid, Expand2ndWidget)
{
Grid* grid = new Grid(2, false);
Widget* w1 = new Widget;
Widget* w2 = new Widget;
w1->setMinSize(gfx::Size(20, 20));
w2->setMinSize(gfx::Size(10, 10));
grid->addChildInCell(w1, 1, 1, 0);
grid->addChildInCell(w2, 1, 1, HORIZONTAL | TOP);
// Test request size
Size reqSize = grid->sizeHint();
EXPECT_EQ(30, reqSize.w);
EXPECT_EQ(20, reqSize.h);
// Test layout
grid->setBounds(gfx::Rect(0, 0, 40, 20));
EXPECT_EQ(0, w1->bounds().x);
EXPECT_EQ(0, w1->bounds().y);
EXPECT_EQ(20, w1->bounds().w);
EXPECT_EQ(20, w1->bounds().h);
EXPECT_EQ(20, w2->bounds().x);
EXPECT_EQ(0, w2->bounds().y);
EXPECT_EQ(20, w2->bounds().w);
EXPECT_EQ(10, w2->bounds().h);
delete grid;
}
TEST(Grid, SameWidth2x1Grid)
{
Grid* grid = new Grid(2, true);
Widget* w1 = new Widget;
Widget* w2 = new Widget;
w1->setMinSize(gfx::Size(20, 20));
w2->setMinSize(gfx::Size(10, 10));
grid->addChildInCell(w1, 1, 1, 0);
grid->addChildInCell(w2, 1, 1, 0);
// Test request size
Size reqSize = grid->sizeHint();
EXPECT_EQ(40, reqSize.w);
EXPECT_EQ(20, reqSize.h);
// Test layout
grid->setBounds(gfx::Rect(0, 0, 60, 20));
EXPECT_EQ(0, w1->bounds().x);
EXPECT_EQ(30, w2->bounds().x);
EXPECT_EQ(30, w1->bounds().w);
EXPECT_EQ(30, w2->bounds().w);
delete grid;
}
// Tests the next layout (a grid of 3x3 cells):
//
// 2 (separator)
// _|_
// | |
// 10 2 0 2 10
// +---+-------+
// | 1 | 2 | 10
// +---+---+---+ --- 2 (separator)
// | 3 | 4 | 4
// | | | --- 2 (separator)
// | | | 4
// +-------+---+
//
// 1.align = 0
// 2.align = HORIZONTAL
// 3.align = HORIZONTAL | VERTICAL
// 4.align = VERTICAL
//
//
// When we expand the grid we get the following layout:
//
// +---+----------------------------+
// | 1 | 2 |
// +---+------------------------+---+
// | 3 | 4 |
// | | |
// | | |
// | | |
// | | |
// | | |
// | | |
// +----------------------------+---+
//
TEST(Grid, Intrincate2x2Grid)
{
Grid* grid = new Grid(3, false);
Widget* w1 = new Widget;
Widget* w2 = new Widget;
Widget* w3 = new Widget;
Widget* w4 = new Widget;
w1->setMinSize(gfx::Size(10, 10));
w2->setMinSize(gfx::Size(10, 10));
w3->setMinSize(gfx::Size(10, 10));
w4->setMinSize(gfx::Size(10, 10));
grid->addChildInCell(w1, 1, 1, 0);
grid->addChildInCell(w2, 2, 1, HORIZONTAL);
grid->addChildInCell(w3, 2, 2, HORIZONTAL | VERTICAL);
grid->addChildInCell(w4, 1, 2, VERTICAL);
// Test request size
grid->setChildSpacing(2);
Size reqSize = grid->sizeHint();
EXPECT_EQ(22, reqSize.w);
EXPECT_EQ(22, reqSize.h);
// Test layout
grid->setBounds(gfx::Rect(0, 0, 100, 100));
EXPECT_EQ(0, w1->bounds().x);
EXPECT_EQ(0, w1->bounds().y);
EXPECT_EQ(12, w2->bounds().x);
EXPECT_EQ(0, w2->bounds().y);
EXPECT_EQ(0, w3->bounds().x);
EXPECT_EQ(12, w3->bounds().y);
EXPECT_EQ(90, w4->bounds().x);
EXPECT_EQ(12, w4->bounds().y);
EXPECT_EQ(10, w1->bounds().w);
EXPECT_EQ(10, w1->bounds().h);
EXPECT_EQ(88, w2->bounds().w);
EXPECT_EQ(10, w2->bounds().h);
EXPECT_EQ(88, w3->bounds().w);
EXPECT_EQ(88, w3->bounds().h);
EXPECT_EQ(10, w4->bounds().w);
EXPECT_EQ(88, w4->bounds().h);
delete grid;
}
TEST(Grid, FourColumns)
{
Grid grid(4, false);
grid.noBorderNoChildSpacing();
Widget a;
Widget b;
Widget c;
Widget d;
Widget e;
a.setMinSize(gfx::Size(10, 10));
b.setMinSize(gfx::Size(10, 10));
c.setMinSize(gfx::Size(10, 10));
d.setMinSize(gfx::Size(10, 10));
e.setMinSize(gfx::Size(10, 10));
grid.addChildInCell(&a, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&b, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&c, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&d, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&e, 4, 1, HORIZONTAL | VERTICAL);
// Test request size
grid.setChildSpacing(0);
EXPECT_EQ(gfx::Size(40, 20), grid.sizeHint());
// Test layout
grid.setBounds(gfx::Rect(0, 0, 40, 20));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(10, 0, 10, 10), b.bounds());
EXPECT_EQ(gfx::Rect(20, 0, 10, 10), c.bounds());
EXPECT_EQ(gfx::Rect(30, 0, 10, 10), d.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 40, 10), e.bounds());
}
TEST(Grid, OneFixedReduceThree)
{
Grid grid(1, false);
grid.noBorderNoChildSpacing();
Widget a;
Widget b;
Widget c;
Widget d;
a.setMinSize(gfx::Size(10, 10));
b.setMinSize(gfx::Size(10, 10));
c.setMinSize(gfx::Size(10, 10));
d.setMinSize(gfx::Size(10, 10));
grid.addChildInCell(&a, 1, 1, HORIZONTAL);
grid.addChildInCell(&b, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&c, 1, 1, HORIZONTAL | VERTICAL);
grid.addChildInCell(&d, 1, 1, HORIZONTAL | VERTICAL);
// Test request size
grid.setChildSpacing(0);
EXPECT_EQ(gfx::Size(10, 40), grid.sizeHint());
// Test bigger layout
grid.setBounds(gfx::Rect(0, 0, 10, 100));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 30), b.bounds());
EXPECT_EQ(gfx::Rect(0, 40, 10, 30), c.bounds());
EXPECT_EQ(gfx::Rect(0, 70, 10, 30), d.bounds());
// Test perfect layout
grid.setBounds(gfx::Rect(0, 0, 10, 40));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 10), b.bounds());
EXPECT_EQ(gfx::Rect(0, 20, 10, 10), c.bounds());
EXPECT_EQ(gfx::Rect(0, 30, 10, 10), d.bounds());
// Test reduced layout
grid.setBounds(gfx::Rect(0, 0, 10, 16));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 2), b.bounds());
EXPECT_EQ(gfx::Rect(0, 12, 10, 2), c.bounds());
EXPECT_EQ(gfx::Rect(0, 14, 10, 2), d.bounds());
}
TEST(Grid, ReduceThree)
{
Grid grid(1, false);
grid.noBorderNoChildSpacing();
Widget a;
Widget b;
Widget c;
a.setMinSize(gfx::Size(10, 10));
b.setMinSize(gfx::Size(10, 10));
c.setMinSize(gfx::Size(10, 10));
grid.addChildInCell(&a, 1, 1, HORIZONTAL);
grid.addChildInCell(&b, 1, 1, HORIZONTAL);
grid.addChildInCell(&c, 1, 1, HORIZONTAL);
// Test request size
grid.setChildSpacing(0);
EXPECT_EQ(gfx::Size(10, 30), grid.sizeHint());
// Test bigger layout (as these widgets aren't expandible, they
// should have height=10)
grid.setBounds(gfx::Rect(0, 0, 10, 90));
EXPECT_EQ(gfx::Rect(0, 0, 10, 10), a.bounds());
EXPECT_EQ(gfx::Rect(0, 10, 10, 10), b.bounds());
EXPECT_EQ(gfx::Rect(0, 20, 10, 10), c.bounds());
// Test reduced layout
grid.setBounds(gfx::Rect(0, 0, 10, 12));
EXPECT_EQ(gfx::Rect(0, 0, 10, 4), a.bounds());
EXPECT_EQ(gfx::Rect(0, 4, 10, 4), b.bounds());
EXPECT_EQ(gfx::Rect(0, 8, 10, 4), c.bounds());
}
TEST(Grid, Alignment)
{
Grid grid(1, false);
grid.noBorderNoChildSpacing();
Widget a;
a.setMinSize(gfx::Size(10, 10));
grid.addChildInCell(&a, 1, 1, CENTER | BOTTOM | HORIZONTAL | VERTICAL);
Size reqSize = grid.sizeHint();
EXPECT_EQ(10, reqSize.w);
EXPECT_EQ(10, reqSize.h);
grid.setBounds(gfx::Rect(10, 10, 40, 10));
EXPECT_EQ(gfx::Rect(25, 10, 10, 10), a.bounds());
grid.setBounds(gfx::Rect(10, 10, 60, 20));
EXPECT_EQ(gfx::Rect(35, 20, 10, 10), a.bounds());
}