[go: up one dir, main page]

Menu

[r135]: / lpi_gui_unittest.cpp  Maximize  Restore  History

Download this file

738 lines (623 with data), 25.7 kB

  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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
Copyright (c) 2005-2009 Lode Vandevenne
All rights reserved.
This file is part of Lode's Programming Interface.
Lode's Programming Interface is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lode's Programming Interface is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lode's Programming Interface. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lpi_gui_unittest.h"
#include "lpi_unittest.h"
#include "lpi_event.h"
#include "lpi_input_sdl.h"
#include "lpi_gui.h"
#include "lpi_gui_ex.h"
#include "lpi_gui_text.h"
#include "lpi_gui_drawer_buffer.h"
#include <iostream>
namespace lpi
{
namespace gui
{
class GUIInputDebug : public InputSDL
{
private:
int x;
int y;
bool lmb;
bool rmb;
public:
void debugSetMousePos(int x, int y)
{
this->x = x;
this->y = y;
}
void debugSetLMB(bool set)
{
this->lmb = set;
}
void debugSetRMB(bool set)
{
this->rmb = set;
}
public:
//check position of mouse
virtual int mouseX() const
{
return x;
}
virtual int mouseY() const
{
return y;
}
//check the state of the 3 buttons of mouse
virtual bool mouseButtonDown(MouseButton button) const
{
if(button == LMB)
return lmb;
else return rmb;
}
};
#define LUT_MY_RESET \
{\
testinput.debugSetLMB(0);\
testinput.debugSetRMB(0);\
testinput.debugSetMousePos(0, 0);\
}
GUIInputDebug testinput;
void unitTest()
{
lpi::gui::GUIDrawerBuffer dummydrawer;
lpi::Drawer2DBuffer& drawer = dynamic_cast<lpi::Drawer2DBuffer&>(dummydrawer.getDrawer());
std::vector<unsigned char> buffer(1024 * 768 * 4);
drawer.setBuffer(&buffer[0], 1024, 768);
LUT_START_UNIT_TEST
//I had a bug where when making a new BulletList and using "set" to give it a value, "check" wasn't returning the correct value until mouseOver
LUT_CASE("set bulletlist")
BulletList test1;
test1.make(0, 0, 5, 0, 0);
test1.set(3);
LUT_SUB_ASSERT_FALSE(test1.check() != 3)
test1.set(1);
LUT_SUB_ASSERT_FALSE(test1.check() != 1)
LUT_CASE_END
LUT_CASE("mouseOver and mouseDown on Dummy")
Dummy dummy;
dummy.resize(0, 0, 50, 50);
testinput.debugSetMousePos(100, 100);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseOver(testinput))
testinput.debugSetMousePos(1, 1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseOver(testinput))
LUT_SUB_ASSERT_TRUE(!dummy.mouseDown(testinput))
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseDown(testinput))
testinput.debugSetMousePos(100, 100);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseDown(testinput))
LUT_CASE_END
LUT_CASE("mouseJustDown")
Dummy dummy;
dummy.resize(0, 0, 50, 50);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDown(testinput))
testinput.debugSetMousePos(100, 100);
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDown(testinput))
testinput.debugSetMousePos(20, 20);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseJustDown(testinput)) //the mouse moved over the element, the mouse button was already down since being outside the element, so it's now down over the element for the first time
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDown(testinput)) //now it may not return true anymore!
testinput.debugSetLMB(0);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDown(testinput))
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseJustDown(testinput))
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDown(testinput)) //now it may not return true anymore!
LUT_CASE_END
LUT_CASE("mouseJustDownHere")
Dummy dummy;
dummy.resize(0, 0, 50, 50);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDownHere(testinput))
testinput.debugSetMousePos(100, 100);
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDownHere(testinput))
testinput.debugSetMousePos(20, 20);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDownHere(testinput))
testinput.debugSetLMB(0);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDownHere(testinput))
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseJustDownHere(testinput))
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDownHere(testinput)) //now it may not return true anymore!
LUT_CASE_END
LUT_CASE("mouseJustDownHere on top of window")
Container c(dummydrawer);
Window w;
w.make(0, 0, 100, 100);
c.pushTopAt(&w, 0, 0);
w.addTop(dummydrawer);
c.handle(testinput);c.handle(testinput);
int mx = w.top.getCenterX();
int my = w.top.getCenterY();
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!w.top.mouseJustDownHere(testinput))
testinput.debugSetMousePos(100, 100);
testinput.debugSetLMB(1);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!w.top.mouseJustDownHere(testinput))
testinput.debugSetMousePos(mx, my);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!w.top.mouseJustDownHere(testinput))
testinput.debugSetLMB(0);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!w.top.mouseJustDownHere(testinput))
testinput.debugSetLMB(1);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(w.top.mouseJustDownHere(testinput))
LUT_SUB_ASSERT_TRUE(!w.top.mouseJustDownHere(testinput)) //now it may not return true anymore!
LUT_CASE_END
LUT_CASE("mouseGrabbed")
Dummy dummy;
dummy.resize(0, 0, 50, 50);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput)) //mouse not down yet
testinput.debugSetMousePos(10, 10);
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseGrabbed(testinput)) //mouse down on element so it's grabbed
testinput.debugSetMousePos(100, 100);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseGrabbed(testinput)) //mouse moved away but still down ==> still grabbed
LUT_SUB_ASSERT_TRUE(dummy.mouseGetGrabX() == 10 && dummy.mouseGetGrabY() == 10)
testinput.debugSetLMB(0);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput)) //mouse button up ==> not grabbed anymore
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput)) //mouse down somewhere else not on element ==> not grabbed
testinput.debugSetMousePos(15, 15);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput)) //mouse still down and moving over element, but it wasn't down HERE, so not grabbed
/*//now repeat the test to see if states can properly be reused
testinput.debugSetLMB(0);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput))
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseGrabbed(testinput)) //mouse down on element so it's grabbed
testinput.debugSetMousePos(100, 100);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseGrabbed(testinput)) //mouse moved away but still down ==> still grabbed
LUT_SUB_ASSERT_TRUE(dummy.mouseGetGrabX() == 15 && dummy.mouseGetGrabY() == 15)
testinput.debugSetLMB(0);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput)) //mouse button up ==> not grabbed anymore
testinput.debugSetLMB(1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput)) //mouse down somewhere else not on element ==> not grabbed
testinput.debugSetMousePos(11, 11);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseGrabbed(testinput)) //mouse still down and moving over element, but it wasn't down HERE, so not grabbed*/
LUT_CASE_END
//the fact that the container and the window handles the elements, influences the result
LUT_CASE("mouseGrabbed on top of window")
Container c(dummydrawer);
Window w;
w.make(0, 0, 100, 100);
c.pushTopAt(&w, 0, 0);
w.addTop(dummydrawer);
c.handle(testinput);c.handle(testinput);
int mx = w.top.getCenterX();
int my = w.top.getCenterY();
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!w.top.mouseGrabbed(testinput)) //mouse not down yet
testinput.debugSetMousePos(mx, my);
testinput.debugSetLMB(1);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(w.top.mouseGrabbed(testinput)) //mouse down on element so it's grabbed
testinput.debugSetMousePos(200, 200);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(w.top.mouseGrabbed(testinput)) //mouse moved away but still down ==> still grabbed
LUT_SUB_ASSERT_TRUE(w.top.mouseGetGrabX() == mx && w.top.mouseGetGrabY() == my)
testinput.debugSetLMB(0);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!w.top.mouseGrabbed(testinput)) //mouse button up ==> not grabbed anymore
testinput.debugSetLMB(1);
c.handle(testinput);c.handle(testinput);
LUT_SUB_ASSERT_TRUE(w.top.mouseGrabbed(testinput)) //mouse down somewhere else not on element ==> normally not grabbed, BUT, the window moves!! ==> under mouse and grabbed again
LUT_CASE_END
LUT_CASE("dragging a window")
Container c(dummydrawer);
Window w;
w.make(0, 0, 100, 100);
c.pushTopAt(&w, 0, 0);
w.addTop(dummydrawer);
c.handle(testinput);c.handle(testinput);
int mx = w.top.getCenterX();
int my = w.top.getCenterY();
int wx1 = w.getX0();
int wy1 = w.getY0();
testinput.debugSetMousePos(mx, my); //mouse above top bar of the window
testinput.debugSetLMB(1);
c.handle(testinput);c.handle(testinput);
int wx2 = w.getX0();
int wy2 = w.getY0();
LUT_SUB_ASSERT_TRUE(wx1 == wx2 && wy1 == wy2)
testinput.debugSetMousePos(mx + 100, my + 50);
c.handle(testinput);c.handle(testinput);
int wx3 = w.getX0();
int wy3 = w.getY0();
LUT_SUB_ASSERT_FALSE(wx3 == wx1 && wy3 == wy1)
LUT_SUB_ASSERT_TRUE(wx3 == wx1 + 100 && wy3 == wy1 + 50)
LUT_APPEND_MSG << "wx1: " << wx1 << "wy1: " << wy1 << "wx2: " << wx2 << "wy2: " << wy2 << "wx3: " << wx3 << "wy3: " << wy3;
LUT_CASE_END
//functions like mouseJustDown and mouseJustDownHere may not influence each other's result, so both can return true once
LUT_CASE("mouse independence")
Dummy dummy;
dummy.resize(0, 0, 50, 50);
testinput.debugSetMousePos(10, 10); //mouse above
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDown(testinput))
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDownHere(testinput))
testinput.debugSetLMB(1); //press button
dummy.handle(testinput);
//now both must return true
LUT_SUB_ASSERT_TRUE(dummy.mouseJustDown(testinput))
LUT_SUB_ASSERT_TRUE(dummy.mouseJustDownHere(testinput))
//but of course now both must return false
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDown(testinput))
LUT_SUB_ASSERT_TRUE(!dummy.mouseJustDownHere(testinput))
LUT_CASE_END
//when boolean mouseActive is true, mouseOver should work, when it's false, mouseOver shout NOT work
LUT_CASE("mouseOver when active and not active")
Dummy dummy;
dummy.resize(0, 0, 50, 50);
testinput.debugSetMousePos(1, 1);
dummy.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy.mouseOver(testinput))
dummy.setElementOver(true);
LUT_SUB_ASSERT_TRUE(!dummy.mouseOver(testinput))
//note: containers like a gui::Window have to take control of the mouseActive boolean of the elements in it
LUT_CASE_END
//the selfActivate system (used by text input boxes) didn't work anymore, so I added this test to make sure it won't break anymore
LUT_CASE("selfActivate of InputLine")
InputLine line;
line.make(0, 0, 10);
//initially it's not active
line.handle(testinput); //handle line all the time to let the selfActivate system work
LUT_SUB_ASSERT_TRUE(!line.isControlActive())
//move mouse above it and click
testinput.debugSetMousePos(1, 1);
testinput.debugSetLMB(1);
line.handle(testinput);
//it must be active now!
LUT_SUB_ASSERT_TRUE(line.isControlActive())
//now release the mouse button
testinput.debugSetLMB(0);
line.handle(testinput);
//it should still be active!
LUT_SUB_ASSERT_TRUE(line.isControlActive())
//move mouse away
testinput.debugSetMousePos(100, 100);
line.handle(testinput);
//it should still be active!
LUT_SUB_ASSERT_TRUE(line.isControlActive())
//move mouse away from it and click
testinput.debugSetMousePos(100, 100);
testinput.debugSetLMB(1);
line.handle(testinput);
//it may not be active anymore after clicking on a location outside (???????)
LUT_SUB_ASSERT_TRUE(!line.isControlActive())
LUT_CASE_END
LUT_CASE("selfActivate of InputLine in Window")
Container c(dummydrawer);
Window w;
w.make(0, 0, 100, 100);
c.pushTopAt(&w, 0, 0);
InputLine line;
line.make(0, 0, 10);
w.pushTopAt(&line, 0, 0);
//initially it's not active
c.handle(testinput); //handle line all the time to let the selfActivate system work
LUT_SUB_ASSERT_TRUE(!line.isControlActive())
//move mouse above it and click
testinput.debugSetMousePos(line.getCenterX(), line.getCenterY());
testinput.debugSetLMB(1);
c.handle(testinput);
//it must be active now!
LUT_SUB_ASSERT_TRUE(line.isControlActive())
//now release the mouse button
testinput.debugSetLMB(0);
c.handle(testinput);
//it should still be active!
LUT_SUB_ASSERT_TRUE(line.isControlActive())
//move mouse away
testinput.debugSetMousePos(100, 100);
c.handle(testinput);
//it should still be active!
LUT_SUB_ASSERT_TRUE(line.isControlActive())
//move mouse away from it and click
testinput.debugSetMousePos(100, 100);
testinput.debugSetLMB(1);
c.handle(testinput);
//it may not be active anymore after clicking on a location outside (???????)
LUT_SUB_ASSERT_TRUE(!line.isControlActive())
LUT_CASE_END
//two things on a container, both at the same location, first the one is totallyEnabled and the other totallyDisabled, then vica versa, then each time only the enabled one may return true to mouseOver
LUT_CASE("enabled and disabled on each other")
Container c(dummydrawer);
Window w;
w.make(0, 0, 100, 100);
c.pushTopAt(&w, 0, 0);
Dummy dummy1;
dummy1.resize(0, 0, 50, 50);
w.pushTopAt(&dummy1, 0, 0);
Dummy dummy2;
dummy2.resize(0, 0, 50, 50);
w.pushTopAt(&dummy2, 0, 0);
testinput.debugSetMousePos(dummy1.getCenterX(), dummy1.getCenterY()); //doesn't matter if you take center of dummy1 or dummy2, both are exactly at same position
dummy1.setEnabled(true);
dummy2.setEnabled(false);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(dummy1.mouseOver(testinput))
LUT_SUB_ASSERT_TRUE(!dummy2.mouseOver(testinput))
dummy1.setEnabled(false);
dummy2.setEnabled(true);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!dummy1.mouseOver(testinput))
LUT_SUB_ASSERT_TRUE(dummy2.mouseOver(testinput))
LUT_CASE_END
//dragging a window that has a top
LUT_CASE("dragging of a window")
Container c(dummydrawer);
Window w;
w.make(0, 0, 100, 100);
w.addTop(dummydrawer);
c.pushTopAt(&w, 0, 0);
int mx = w.top.getCenterX();
int my = w.top.getCenterY();
//grab the window
testinput.debugSetMousePos(mx, my);
testinput.debugSetLMB(1);
c.handle(testinput);c.handle(testinput);
//move
testinput.debugSetMousePos(200, 200);
c.handle(testinput);c.handle(testinput);
//now the window must still be below the mouse
LUT_SUB_ASSERT_TRUE(w.mouseOver(testinput))
LUT_CASE_END
//when there are two windows, A and B, and you're dragging one over the other, but you drag the mouse fast and the mousepointer goes to a location not over the dragged window (the dragged window will be under the mouse again soon), then the other window may not swap to top
LUT_CASE("fast dragging of one window over another")
Container c(dummydrawer);
Window A;
A.make(0, 0, 100, 100);
A.addTop(dummydrawer);
c.pushTopAt(&A, 0, 0);
Window B;
B.make(0, 0, 100, 100);
B.addTop(dummydrawer);
c.pushTopAt(&B, 200, 0);
int ax = A.top.getCenterX();
int ay = A.top.getCenterY();
int bx = B.top.getCenterX();
int by = B.top.getCenterY();
//grab the window A
testinput.debugSetMousePos(ax, ay);
testinput.debugSetLMB(1);
c.handle(testinput);c.handle(testinput);
//move it over window B
testinput.debugSetMousePos(bx, by);
c.handle(testinput);c.handle(testinput);
//now window A must be over window B, so window A must have mouseOver, window B not
LUT_SUB_ASSERT_TRUE(A.mouseOver(testinput))
LUT_SUB_ASSERT_TRUE(!B.mouseOver(testinput))
LUT_CASE_END
//dragging a window that has a top that is shifted: when starting to drag, the window may not "jump" (but I once had)
LUT_CASE("dragging of a window with shifted top")
Container c(dummydrawer);
Window w;
w.make(0, 0, 100, 100);
w.addTop(dummydrawer); //could cause jump of two pixels when the bug was there
c.pushTopAt(&w, 0, 0);
int mx = w.top.getCenterX();
int my = w.top.getCenterY();
//grab the window
testinput.debugSetMousePos(mx, my);
testinput.debugSetLMB(1);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(w.getX0() == 0 && w.getY0() == 0); //NOT shifted over two pixels
LUT_CASE_END
//two things on a container, both at the same location, first the one is totallyEnabled and the other totallyDisabled, then vica versa, then each time only the enabled one may return true to mouseOver
LUT_CASE("mouseOver works in nested containers")
Container c(dummydrawer);
Window w; //window is nested container in c
w.make(0, 0, 100, 100);
c.pushTopAt(&w, 0, 0);
Dummy dummy;
dummy.resize(0, 0, 50, 50);
w.pushTopAt(&dummy, 0, 0);
testinput.debugSetMousePos(dummy.getCenterX(), dummy.getCenterY());
c.handle(testinput); //handle only ONCE, no multiple times to make things "settle"
LUT_SUB_ASSERT_TRUE(dummy.mouseOver(testinput)) //the mouse really is over the dummy, and we called handle on the top container so that all input is up to date, so mouseOver of the nested element in it should return true
LUT_CASE_END
LUT_CASE("mouseOver on button on window in Tabs, behind other window")
Container c(dummydrawer);
Window w2;
w2.make(50, 50, 300, 300);
w2.addTop(dummydrawer);
w2.addTitle("Window 2");
w2.addCloseButton(dummydrawer);
w2.setColor(lpi::RGBA_White(192));
w2.addResizer(dummydrawer);
c.pushTop(&w2);
Tabs tabs;
tabs.resize(0, 0, w2.getSizeX(), w2.getSizeY() - 32);
tabs.addTab("tab 1");
tabs.addTab("tab 2");
tabs.addTab("tab 3");
tabs.addTab("tab 4");
w2.pushTopAt(&tabs, 0, 16, lpi::gui::Sticky(0.1, 0, 0.1, 0, 0.9, 0, 0.5, 0));
Window w1;
w1.make(0, 0, 500, 500);
w1.addTop(dummydrawer);
w1.addTitle("Window 1");
w1.addCloseButton(dummydrawer);
w1.addResizer(dummydrawer);
w1.setColor(lpi::RGBA_Red(192));
c.pushTop(&w1);
Button wb;
wb.makeTextPanel(0, 0, "window button");
wb.autoTextSize(&dummydrawer, 4);
tabs.getTabContent(0).pushTopAt(&wb, 20, 50);
testinput.debugSetMousePos(0, 0);
c.handle(testinput);
testinput.debugSetMousePos(wb.getCenterX(), wb.getCenterY());
c.handle(testinput);
LUT_SUB_ASSERT_FALSE(wb.mouseOver(testinput)); //it should NOT be over, because the big "window 1" is over this - but some bug once made this be "true" here anyway (elements in tabs were active when another window was over it, due to tabs calling "totallyEnable" on each tab constantly, and totallyEnable had a setElementOver(false) call in it...)
w1.move(wb.getX1(), wb.getY1());
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(wb.mouseOver(testinput)); //window moved away, now mouse should be over the button
LUT_CASE_END
LUT_CASE("mouseOver on different tabs")
Container c(dummydrawer);
Window w2;
w2.make(50, 50, 300, 300);
w2.addTop(dummydrawer);
w2.addTitle("Window 2");
w2.addCloseButton(dummydrawer);
w2.setColor(lpi::RGBA_White(192));
w2.addResizer(dummydrawer);
c.pushTop(&w2);
Tabs tabs;
tabs.resize(0, 0, w2.getSizeX(), w2.getSizeY() - 32);
tabs.addTab("tab 1");
tabs.addTab("tab 2");
tabs.addTab("tab 3");
tabs.addTab("tab 4");
w2.pushTopAt(&tabs, 0, 16, lpi::gui::Sticky(0.1, 0, 0.1, 0, 0.9, 0, 0.5, 0));
Button wb;
wb.makeTextPanel(0, 0, "window button");
wb.autoTextSize(&dummydrawer, 4);
tabs.getTabContent(0).pushTopAt(&wb, 20, 50);
testinput.debugSetMousePos(0, 0);
c.handle(testinput);
testinput.debugSetMousePos(wb.getCenterX(), wb.getCenterY());
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(wb.mouseOver(testinput));
tabs.selectTab(1);
c.handle(testinput);
LUT_SUB_ASSERT_FALSE(wb.mouseOver(testinput));
tabs.selectTab(0);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(wb.mouseOver(testinput));
LUT_CASE_END
LUT_CASE("handling element outside of scrollzone")
Container c(dummydrawer);
Container c2;
c2.make(200, 200, 400, 400);
Checkbox cb;
cb.make(200, 200);
c2.pushTopAt(&cb, 100, 100);
ScrollElement s;
s.make(200, 200, 200, 200, &c2, dummydrawer);
c.pushTopAt(&s, 200, 200);
s.bars.vbar.setRelativePosition(0.0);
LUT_SUB_ASSERT_TRUE(!cb.isChecked());
testinput.debugSetMousePos(cb.getCenterX(), cb.getCenterY());
c.handle(testinput);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(s.mouseOver(testinput));
LUT_SUB_ASSERT_TRUE(cb.mouseOver(testinput));
testinput.debugSetLMB(true);
c.handle(testinput);
testinput.debugSetLMB(false);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(cb.isChecked());
int yold = cb.getCenterY();
s.bars.vbar.setRelativePosition(1.0);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(yold != cb.getCenterY());
testinput.debugSetMousePos(cb.getCenterX(), cb.getCenterY());
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!s.mouseOver(testinput));
LUT_SUB_ASSERT_TRUE(!cb.mouseOver(testinput));
testinput.debugSetLMB(true);
c.handle(testinput);
testinput.debugSetLMB(false);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(cb.isChecked());
s.bars.vbar.setRelativePosition(0.0);
c.handle(testinput);
testinput.debugSetMousePos(cb.getCenterX(), cb.getCenterY());
c.handle(testinput);
testinput.debugSetLMB(true);
c.handle(testinput);
testinput.debugSetLMB(false);
c.handle(testinput);
LUT_SUB_ASSERT_TRUE(!cb.isChecked());
LUT_CASE_END
LUT_CASE("handling element outside of scrollzone II")
Container c2;
c2.make(200, 200, 400, 400);
Checkbox cb;
cb.make(200, 200);
c2.pushTopAt(&cb, 100, 100);
ScrollElement s;
s.make(200, 200, 200, 200, &c2, dummydrawer);
s.bars.vbar.setRelativePosition(0.0);
LUT_SUB_ASSERT_TRUE(!cb.isChecked());
testinput.debugSetMousePos(cb.getCenterX(), cb.getCenterY());
s.handle(testinput);
s.handle(testinput);
LUT_SUB_ASSERT_TRUE(s.mouseOver(testinput));
LUT_SUB_ASSERT_TRUE(cb.mouseOver(testinput));
testinput.debugSetLMB(true);
s.handle(testinput);
testinput.debugSetLMB(false);
s.handle(testinput);
LUT_SUB_ASSERT_TRUE(cb.isChecked());
int yold = cb.getCenterY();
s.bars.vbar.setRelativePosition(1.0);
s.handle(testinput);
LUT_SUB_ASSERT_TRUE(yold != cb.getCenterY());
testinput.debugSetMousePos(cb.getCenterX(), cb.getCenterY());
s.handle(testinput);
LUT_SUB_ASSERT_TRUE(!s.mouseOver(testinput));
LUT_SUB_ASSERT_TRUE(!cb.mouseOver(testinput));
testinput.debugSetLMB(true);
s.handle(testinput);
testinput.debugSetLMB(false);
s.handle(testinput);
LUT_SUB_ASSERT_TRUE(cb.isChecked());
s.bars.vbar.setRelativePosition(0.0);
s.handle(testinput);
testinput.debugSetMousePos(cb.getCenterX(), cb.getCenterY());
s.handle(testinput);
testinput.debugSetLMB(true);
s.handle(testinput);
testinput.debugSetLMB(false);
s.handle(testinput);
LUT_SUB_ASSERT_TRUE(!cb.isChecked());
LUT_CASE_END
//TODO: add tests for all mouse functions (mouseOver, mouseJustDownHere, etc...) as well as combinations of them (e.g. test that mouseDownHere and mouseDownElsewhere don't interfere with each other...)
LUT_END_UNIT_TEST
}
////////////////////////////////////////////////////////////////////////////////
} //namespace gui
} //namespace lpi