forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_unittest.cc
3428 lines (2879 loc) · 120 KB
/
window_unittest.cc
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/aura/window.h"
#include <limits.h>
#include <string>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind_test_util.h"
#include "build/build_config.h"
#include "cc/trees/layer_tree_frame_sink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/client/focus_change_observer.h"
#include "ui/aura/client/visibility_client.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/scoped_window_event_targeting_blocker.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/aura_test_utils.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/test/window_test_api.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tracker.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/class_property.h"
#include "ui/base/hit_test.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/compositor/test/layer_animator_test_controller.h"
#include "ui/compositor/test/test_layers.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/overlay_transform_utils.h"
#include "ui/gfx/skia_util.h"
DEFINE_UI_CLASS_PROPERTY_TYPE(const char*)
namespace {
enum class DeletionOrder {
LAYOUT_MANAGER_FIRST,
PROPERTY_FIRST,
UNKNOWN,
};
class DeletionTracker {
public:
DeletionTracker() {}
~DeletionTracker() {}
DeletionOrder order() const { return order_; }
bool property_deleted() const { return property_deleted_; }
bool layout_manager_deleted() const { return layout_manager_deleted_; }
void PropertyDeleted() {
property_deleted_ = true;
if (order_ == DeletionOrder::UNKNOWN)
order_ = DeletionOrder::PROPERTY_FIRST;
}
void LayoutManagerDeleted() {
layout_manager_deleted_ = true;
if (order_ == DeletionOrder::UNKNOWN)
order_ = DeletionOrder::LAYOUT_MANAGER_FIRST;
}
private:
bool property_deleted_ = false;
bool layout_manager_deleted_ = false;
DeletionOrder order_ = DeletionOrder::UNKNOWN;
DISALLOW_COPY_AND_ASSIGN(DeletionTracker);
};
class DeletionTestProperty {
public:
explicit DeletionTestProperty(DeletionTracker* tracker) : tracker_(tracker) {}
~DeletionTestProperty() { tracker_->PropertyDeleted(); }
private:
DeletionTracker* tracker_;
DISALLOW_COPY_AND_ASSIGN(DeletionTestProperty);
};
DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(DeletionTestProperty,
kDeletionTestPropertyKey,
nullptr)
} // namespace
DEFINE_UI_CLASS_PROPERTY_TYPE(DeletionTestProperty*)
namespace aura {
namespace test {
namespace {
class WindowTest : public AuraTestBase {
public:
WindowTest() : max_separation_(0) {
}
void SetUp() override {
AuraTestBase::SetUp();
// TODO: there needs to be an easier way to do this.
max_separation_ = ui::GestureConfiguration::GetInstance()
->max_separation_for_gesture_touches_in_pixels();
ui::GestureConfiguration::GetInstance()
->set_max_separation_for_gesture_touches_in_pixels(0);
}
void TearDown() override {
AuraTestBase::TearDown();
ui::GestureConfiguration::GetInstance()
->set_max_separation_for_gesture_touches_in_pixels(max_separation_);
}
private:
float max_separation_;
DISALLOW_COPY_AND_ASSIGN(WindowTest);
};
// Used for verifying destruction methods are invoked.
class DestroyTrackingDelegateImpl : public TestWindowDelegate {
public:
DestroyTrackingDelegateImpl()
: destroying_count_(0),
destroyed_count_(0),
in_destroying_(false) {}
void clear_destroying_count() { destroying_count_ = 0; }
int destroying_count() const { return destroying_count_; }
void clear_destroyed_count() { destroyed_count_ = 0; }
int destroyed_count() const { return destroyed_count_; }
bool in_destroying() const { return in_destroying_; }
void OnWindowDestroying(Window* window) override {
EXPECT_FALSE(in_destroying_);
in_destroying_ = true;
destroying_count_++;
}
void OnWindowDestroyed(Window* window) override {
EXPECT_TRUE(in_destroying_);
in_destroying_ = false;
destroyed_count_++;
}
private:
int destroying_count_;
int destroyed_count_;
bool in_destroying_;
DISALLOW_COPY_AND_ASSIGN(DestroyTrackingDelegateImpl);
};
// Used to verify that when OnWindowDestroying is invoked the parent is also
// is in the process of being destroyed.
class ChildWindowDelegateImpl : public DestroyTrackingDelegateImpl {
public:
explicit ChildWindowDelegateImpl(
DestroyTrackingDelegateImpl* parent_delegate)
: parent_delegate_(parent_delegate) {
}
void OnWindowDestroying(Window* window) override {
EXPECT_TRUE(parent_delegate_->in_destroying());
DestroyTrackingDelegateImpl::OnWindowDestroying(window);
}
private:
DestroyTrackingDelegateImpl* parent_delegate_;
DISALLOW_COPY_AND_ASSIGN(ChildWindowDelegateImpl);
};
// Used to verify that a Window is removed from its parent when
// OnWindowDestroyed is called.
class DestroyOrphanDelegate : public TestWindowDelegate {
public:
DestroyOrphanDelegate() : window_(nullptr) {}
void set_window(Window* window) { window_ = window; }
void OnWindowDestroyed(Window* window) override {
EXPECT_FALSE(window_->parent());
}
private:
Window* window_;
DISALLOW_COPY_AND_ASSIGN(DestroyOrphanDelegate);
};
// Used in verifying mouse capture.
class CaptureWindowDelegateImpl : public TestWindowDelegate {
public:
CaptureWindowDelegateImpl() {
ResetCounts();
}
void ResetCounts() {
capture_changed_event_count_ = 0;
capture_lost_count_ = 0;
mouse_event_count_ = 0;
touch_event_count_ = 0;
gesture_event_count_ = 0;
}
int capture_changed_event_count() const {
return capture_changed_event_count_;
}
int capture_lost_count() const { return capture_lost_count_; }
int mouse_event_count() const { return mouse_event_count_; }
int touch_event_count() const { return touch_event_count_; }
int gesture_event_count() const { return gesture_event_count_; }
void OnMouseEvent(ui::MouseEvent* event) override {
if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED)
capture_changed_event_count_++;
mouse_event_count_++;
}
void OnTouchEvent(ui::TouchEvent* event) override { touch_event_count_++; }
void OnGestureEvent(ui::GestureEvent* event) override {
gesture_event_count_++;
}
void OnCaptureLost() override { capture_lost_count_++; }
private:
int capture_changed_event_count_;
int capture_lost_count_;
int mouse_event_count_;
int touch_event_count_;
int gesture_event_count_;
DISALLOW_COPY_AND_ASSIGN(CaptureWindowDelegateImpl);
};
// Keeps track of the location of the gesture.
class GestureTrackPositionDelegate : public TestWindowDelegate {
public:
GestureTrackPositionDelegate() {}
void OnGestureEvent(ui::GestureEvent* event) override {
position_ = event->location();
event->StopPropagation();
}
const gfx::Point& position() const { return position_; }
private:
gfx::Point position_;
DISALLOW_COPY_AND_ASSIGN(GestureTrackPositionDelegate);
};
base::TimeTicks getTime() {
return ui::EventTimeForNow();
}
class SelfEventHandlingWindowDelegate : public TestWindowDelegate {
public:
SelfEventHandlingWindowDelegate() {}
bool ShouldDescendIntoChildForEventHandling(
Window* child,
const gfx::Point& location) override {
return false;
}
private:
DISALLOW_COPY_AND_ASSIGN(SelfEventHandlingWindowDelegate);
};
// The delegate deletes itself when the window is being destroyed.
class DestroyWindowDelegate : public TestWindowDelegate {
public:
DestroyWindowDelegate() {}
private:
~DestroyWindowDelegate() override {}
// Overridden from WindowDelegate.
void OnWindowDestroyed(Window* window) override { delete this; }
DISALLOW_COPY_AND_ASSIGN(DestroyWindowDelegate);
};
void OffsetBounds(Window* window, int horizontal, int vertical) {
gfx::Rect bounds = window->bounds();
bounds.Offset(horizontal, vertical);
window->SetBounds(bounds);
}
TEST_F(WindowTest, GetChildById) {
std::unique_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
std::unique_ptr<Window> w11(CreateTestWindowWithId(11, w1.get()));
std::unique_ptr<Window> w111(CreateTestWindowWithId(111, w11.get()));
std::unique_ptr<Window> w12(CreateTestWindowWithId(12, w1.get()));
EXPECT_FALSE(w1->GetChildById(57));
EXPECT_EQ(w12.get(), w1->GetChildById(12));
EXPECT_EQ(w111.get(), w1->GetChildById(111));
}
// Make sure that Window::Contains correctly handles children, grandchildren,
// and not containing NULL or parents.
TEST_F(WindowTest, Contains) {
Window parent(nullptr);
parent.Init(ui::LAYER_NOT_DRAWN);
Window child1(nullptr);
child1.Init(ui::LAYER_NOT_DRAWN);
Window child2(nullptr);
child2.Init(ui::LAYER_NOT_DRAWN);
parent.AddChild(&child1);
child1.AddChild(&child2);
EXPECT_TRUE(parent.Contains(&parent));
EXPECT_TRUE(parent.Contains(&child1));
EXPECT_TRUE(parent.Contains(&child2));
EXPECT_FALSE(parent.Contains(nullptr));
EXPECT_FALSE(child1.Contains(&parent));
EXPECT_FALSE(child2.Contains(&child1));
}
TEST_F(WindowTest, ContainsPointInRoot) {
std::unique_ptr<Window> w(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 5, 5), root_window()));
EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(9, 9)));
EXPECT_TRUE(w->ContainsPointInRoot(gfx::Point(10, 10)));
EXPECT_TRUE(w->ContainsPointInRoot(gfx::Point(14, 14)));
EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(15, 15)));
EXPECT_FALSE(w->ContainsPointInRoot(gfx::Point(20, 20)));
}
TEST_F(WindowTest, ContainsPoint) {
std::unique_ptr<Window> w(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 5, 5), root_window()));
EXPECT_TRUE(w->ContainsPoint(gfx::Point(0, 0)));
EXPECT_TRUE(w->ContainsPoint(gfx::Point(4, 4)));
EXPECT_FALSE(w->ContainsPoint(gfx::Point(5, 5)));
EXPECT_FALSE(w->ContainsPoint(gfx::Point(10, 10)));
}
TEST_F(WindowTest, ConvertPointToWindow) {
// Window::ConvertPointToWindow is mostly identical to
// Layer::ConvertPointToLayer, except NULL values for |source| are permitted,
// in which case the function just returns.
std::unique_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
gfx::Point reference_point(100, 100);
gfx::Point test_point = reference_point;
Window::ConvertPointToTarget(nullptr, w1.get(), &test_point);
EXPECT_EQ(reference_point, test_point);
}
TEST_F(WindowTest, MoveCursorTo) {
std::unique_ptr<Window> w1(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500), root_window()));
std::unique_ptr<Window> w11(
CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
std::unique_ptr<Window> w111(
CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
std::unique_ptr<Window> w1111(
CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
Window* root = root_window();
root->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("10,10",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
w1->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("20,20",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
w11->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("25,25",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
w111->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("30,30",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
w1111->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("35,35",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
}
TEST_F(WindowTest, ContainsMouse) {
std::unique_ptr<Window> w(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500), root_window()));
w->Show();
WindowTestApi w_test_api(w.get());
Window* root = root_window();
root->MoveCursorTo(gfx::Point(10, 10));
EXPECT_TRUE(w_test_api.ContainsMouse());
root->MoveCursorTo(gfx::Point(9, 10));
EXPECT_FALSE(w_test_api.ContainsMouse());
}
// Tests that the root window gets a valid LocalSurfaceId.
TEST_F(WindowTest, RootWindowHasValidLocalSurfaceId) {
EXPECT_TRUE(root_window()->GetLocalSurfaceId().is_valid());
}
TEST_F(WindowTest, WindowEmbeddingClientHasValidLocalSurfaceId) {
std::unique_ptr<Window> window(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 300, 200), root_window()));
test::WindowTestApi(window.get()).DisableFrameSinkRegistration();
window->SetEmbedFrameSinkId(viz::FrameSinkId(0, 1));
EXPECT_TRUE(window->GetLocalSurfaceId().is_valid());
}
// Test Window::ConvertPointToWindow() with transform to root_window.
TEST_F(WindowTest, MoveCursorToWithTransformRootWindow) {
gfx::Transform transform;
transform.Translate(100.0, 100.0);
transform = transform * OverlayTransformToTransform(
gfx::OVERLAY_TRANSFORM_ROTATE_90, gfx::SizeF());
transform.Scale(2.0, 5.0);
host()->SetRootTransform(transform);
host()->MoveCursorToLocationInDIP(gfx::Point(10, 10));
#if !defined(OS_WIN)
// TODO(yoshiki): fix this to build on Windows. See crbug.com/133413.OD
EXPECT_EQ("50,120", QueryLatestMousePositionRequestInHost(host()).ToString());
#endif
EXPECT_EQ("10,10",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
}
// Tests Window::ConvertPointToWindow() with transform to non-root windows.
TEST_F(WindowTest, MoveCursorToWithTransformWindow) {
std::unique_ptr<Window> w1(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500), root_window()));
gfx::Transform transform1;
transform1.Scale(2, 2);
w1->SetTransform(transform1);
w1->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("30,30",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
gfx::Transform transform2;
transform2.Translate(-10, 20);
w1->SetTransform(transform2);
w1->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("10,40",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
gfx::Transform transform3;
transform3 = transform3 * OverlayTransformToTransform(
gfx::OVERLAY_TRANSFORM_ROTATE_90, gfx::SizeF());
w1->SetTransform(transform3);
w1->MoveCursorTo(gfx::Point(5, 5));
EXPECT_EQ("5,15",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
gfx::Transform transform4;
transform4.Translate(100.0, 100.0);
transform4 = transform4 * OverlayTransformToTransform(
gfx::OVERLAY_TRANSFORM_ROTATE_90, gfx::SizeF());
transform4.Scale(2.0, 5.0);
w1->SetTransform(transform4);
w1->MoveCursorTo(gfx::Point(10, 10));
EXPECT_EQ("60,130",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
}
// Test Window::ConvertPointToWindow() with complex transforms to both root and
// non-root windows.
// Test Window::ConvertPointToWindow() with transform to root_window.
TEST_F(WindowTest, MoveCursorToWithComplexTransform) {
std::unique_ptr<Window> w1(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500), root_window()));
std::unique_ptr<Window> w11(
CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
std::unique_ptr<Window> w111(
CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
std::unique_ptr<Window> w1111(
CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
// The root window expects transforms that produce integer rects.
gfx::Transform root_transform;
root_transform.Translate(60.0, 70.0);
root_transform =
root_transform * OverlayTransformToTransform(
gfx::OVERLAY_TRANSFORM_ROTATE_270, gfx::SizeF());
root_transform.Translate(-50.0, -50.0);
root_transform.Scale(2.0, 3.0);
gfx::Transform transform;
transform.Translate(10.0, 20.0);
transform.Rotate(10.0);
transform.Scale(0.3f, 0.5f);
host()->SetRootTransform(root_transform);
w1->SetTransform(transform);
w11->SetTransform(transform);
w111->SetTransform(transform);
w1111->SetTransform(transform);
w1111->MoveCursorTo(gfx::Point(10, 10));
#if !defined(OS_WIN)
// TODO(yoshiki): fix this to build on Windows. See crbug.com/133413.
EXPECT_EQ("169,80", QueryLatestMousePositionRequestInHost(host()).ToString());
#endif
EXPECT_EQ("20,53",
display::Screen::GetScreen()->GetCursorScreenPoint().ToString());
}
// Tests that we do not crash when a Window is destroyed by going out of
// scope (as opposed to being explicitly deleted by its WindowDelegate).
TEST_F(WindowTest, NoCrashOnWindowDelete) {
CaptureWindowDelegateImpl delegate;
std::unique_ptr<Window> window(CreateTestWindowWithDelegate(
&delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
}
TEST_F(WindowTest, GetEventHandlerForPoint) {
std::unique_ptr<Window> w1(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500), root_window()));
std::unique_ptr<Window> w11(
CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get()));
std::unique_ptr<Window> w111(
CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get()));
std::unique_ptr<Window> w1111(
CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get()));
std::unique_ptr<Window> w12(CreateTestWindow(
SK_ColorMAGENTA, 12, gfx::Rect(10, 420, 25, 25), w1.get()));
std::unique_ptr<Window> w121(
CreateTestWindow(SK_ColorYELLOW, 121, gfx::Rect(5, 5, 5, 5), w12.get()));
std::unique_ptr<Window> w13(
CreateTestWindow(SK_ColorGRAY, 13, gfx::Rect(5, 470, 50, 50), w1.get()));
Window* root = root_window();
w1->parent()->SetBounds(gfx::Rect(500, 500));
EXPECT_EQ(nullptr, root->GetEventHandlerForPoint(gfx::Point(5, 5)));
EXPECT_EQ(w1.get(), root->GetEventHandlerForPoint(gfx::Point(11, 11)));
EXPECT_EQ(w11.get(), root->GetEventHandlerForPoint(gfx::Point(16, 16)));
EXPECT_EQ(w111.get(), root->GetEventHandlerForPoint(gfx::Point(21, 21)));
EXPECT_EQ(w1111.get(), root->GetEventHandlerForPoint(gfx::Point(26, 26)));
EXPECT_EQ(w12.get(), root->GetEventHandlerForPoint(gfx::Point(21, 431)));
EXPECT_EQ(w121.get(), root->GetEventHandlerForPoint(gfx::Point(26, 436)));
EXPECT_EQ(w13.get(), root->GetEventHandlerForPoint(gfx::Point(26, 481)));
}
TEST_F(WindowTest, GetEventHandlerForPointInCornerOfChildBounds) {
// If our child is flush to our top-left corner it gets events just inside the
// window edges.
std::unique_ptr<Window> parent(CreateTestWindow(
SK_ColorWHITE, 1, gfx::Rect(10, 20, 400, 500), root_window()));
std::unique_ptr<Window> child(
CreateTestWindow(SK_ColorRED, 2, gfx::Rect(0, 0, 60, 70), parent.get()));
EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
EXPECT_EQ(child.get(), parent->GetEventHandlerForPoint(gfx::Point(1, 1)));
}
TEST_F(WindowTest, GetEventHandlerForPointWithOverrideDescendingOrder) {
std::unique_ptr<SelfEventHandlingWindowDelegate> parent_delegate(
new SelfEventHandlingWindowDelegate);
std::unique_ptr<Window> parent(CreateTestWindowWithDelegate(
parent_delegate.get(), 1, gfx::Rect(10, 20, 400, 500), root_window()));
std::unique_ptr<Window> child(CreateTestWindow(
SK_ColorRED, 2, gfx::Rect(0, 0, 390, 480), parent.get()));
// We can override ShouldDescendIntoChildForEventHandling to make the parent
// grab all events.
EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(0, 0)));
EXPECT_EQ(parent.get(), parent->GetEventHandlerForPoint(gfx::Point(50, 50)));
}
TEST_F(WindowTest, GetToplevelWindow) {
const gfx::Rect kBounds(0, 0, 10, 10);
TestWindowDelegate delegate;
std::unique_ptr<Window> w1(CreateTestWindowWithId(1, root_window()));
std::unique_ptr<Window> w11(
CreateTestWindowWithDelegate(&delegate, 11, kBounds, w1.get()));
std::unique_ptr<Window> w111(CreateTestWindowWithId(111, w11.get()));
std::unique_ptr<Window> w1111(
CreateTestWindowWithDelegate(&delegate, 1111, kBounds, w111.get()));
EXPECT_TRUE(root_window()->GetToplevelWindow() == nullptr);
EXPECT_TRUE(w1->GetToplevelWindow() == nullptr);
EXPECT_EQ(w11.get(), w11->GetToplevelWindow());
EXPECT_EQ(w11.get(), w111->GetToplevelWindow());
EXPECT_EQ(w11.get(), w1111->GetToplevelWindow());
}
class AddedToRootWindowObserver : public WindowObserver {
public:
AddedToRootWindowObserver() : called_(false) {}
void OnWindowAddedToRootWindow(Window* window) override { called_ = true; }
bool called() const { return called_; }
private:
bool called_;
DISALLOW_COPY_AND_ASSIGN(AddedToRootWindowObserver);
};
TEST_F(WindowTest, WindowAddedToRootWindowShouldNotifyChildAndNotParent) {
AddedToRootWindowObserver parent_observer;
AddedToRootWindowObserver child_observer;
std::unique_ptr<Window> parent_window(
CreateTestWindowWithId(1, root_window()));
std::unique_ptr<Window> child_window(new Window(nullptr));
child_window->Init(ui::LAYER_TEXTURED);
child_window->Show();
parent_window->AddObserver(&parent_observer);
child_window->AddObserver(&child_observer);
parent_window->AddChild(child_window.get());
EXPECT_FALSE(parent_observer.called());
EXPECT_TRUE(child_observer.called());
parent_window->RemoveObserver(&parent_observer);
child_window->RemoveObserver(&child_observer);
}
// Various destruction assertions.
TEST_F(WindowTest, DestroyTest) {
DestroyTrackingDelegateImpl parent_delegate;
ChildWindowDelegateImpl child_delegate(&parent_delegate);
{
std::unique_ptr<Window> parent(CreateTestWindowWithDelegate(
&parent_delegate, 0, gfx::Rect(), root_window()));
CreateTestWindowWithDelegate(&child_delegate, 0, gfx::Rect(), parent.get());
}
// Both the parent and child should have been destroyed.
EXPECT_EQ(1, parent_delegate.destroying_count());
EXPECT_EQ(1, parent_delegate.destroyed_count());
EXPECT_EQ(1, child_delegate.destroying_count());
EXPECT_EQ(1, child_delegate.destroyed_count());
}
// Tests that a window is orphaned before OnWindowDestroyed is called.
TEST_F(WindowTest, OrphanedBeforeOnDestroyed) {
TestWindowDelegate parent_delegate;
DestroyOrphanDelegate child_delegate;
{
std::unique_ptr<Window> parent(CreateTestWindowWithDelegate(
&parent_delegate, 0, gfx::Rect(), root_window()));
std::unique_ptr<Window> child(CreateTestWindowWithDelegate(
&child_delegate, 0, gfx::Rect(), parent.get()));
child_delegate.set_window(child.get());
}
}
// Make sure StackChildAtTop moves both the window and layer to the front.
TEST_F(WindowTest, StackChildAtTop) {
Window parent(nullptr);
parent.Init(ui::LAYER_NOT_DRAWN);
Window child1(nullptr);
child1.Init(ui::LAYER_NOT_DRAWN);
Window child2(nullptr);
child2.Init(ui::LAYER_NOT_DRAWN);
parent.AddChild(&child1);
parent.AddChild(&child2);
ASSERT_EQ(2u, parent.children().size());
EXPECT_EQ(&child1, parent.children()[0]);
EXPECT_EQ(&child2, parent.children()[1]);
ASSERT_EQ(2u, parent.layer()->children().size());
EXPECT_EQ(child1.layer(), parent.layer()->children()[0]);
EXPECT_EQ(child2.layer(), parent.layer()->children()[1]);
parent.StackChildAtTop(&child1);
ASSERT_EQ(2u, parent.children().size());
EXPECT_EQ(&child1, parent.children()[1]);
EXPECT_EQ(&child2, parent.children()[0]);
ASSERT_EQ(2u, parent.layer()->children().size());
EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
}
// Make sure StackChildBelow works.
TEST_F(WindowTest, StackChildBelow) {
Window parent(nullptr);
parent.Init(ui::LAYER_NOT_DRAWN);
Window child1(nullptr);
child1.Init(ui::LAYER_NOT_DRAWN);
child1.set_id(1);
Window child2(nullptr);
child2.Init(ui::LAYER_NOT_DRAWN);
child2.set_id(2);
Window child3(nullptr);
child3.Init(ui::LAYER_NOT_DRAWN);
child3.set_id(3);
parent.AddChild(&child1);
parent.AddChild(&child2);
parent.AddChild(&child3);
EXPECT_EQ("1 2 3", ChildWindowIDsAsString(&parent));
parent.StackChildBelow(&child1, &child2);
EXPECT_EQ("1 2 3", ChildWindowIDsAsString(&parent));
parent.StackChildBelow(&child2, &child1);
EXPECT_EQ("2 1 3", ChildWindowIDsAsString(&parent));
parent.StackChildBelow(&child3, &child2);
EXPECT_EQ("3 2 1", ChildWindowIDsAsString(&parent));
parent.StackChildBelow(&child3, &child1);
EXPECT_EQ("2 3 1", ChildWindowIDsAsString(&parent));
}
// Various assertions for StackChildAbove.
TEST_F(WindowTest, StackChildAbove) {
Window parent(nullptr);
parent.Init(ui::LAYER_NOT_DRAWN);
Window child1(nullptr);
child1.Init(ui::LAYER_NOT_DRAWN);
Window child2(nullptr);
child2.Init(ui::LAYER_NOT_DRAWN);
Window child3(nullptr);
child3.Init(ui::LAYER_NOT_DRAWN);
parent.AddChild(&child1);
parent.AddChild(&child2);
// Move 1 in front of 2.
parent.StackChildAbove(&child1, &child2);
ASSERT_EQ(2u, parent.children().size());
EXPECT_EQ(&child2, parent.children()[0]);
EXPECT_EQ(&child1, parent.children()[1]);
ASSERT_EQ(2u, parent.layer()->children().size());
EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
// Add 3, resulting in order [2, 1, 3], then move 2 in front of 1, resulting
// in [1, 2, 3].
parent.AddChild(&child3);
parent.StackChildAbove(&child2, &child1);
ASSERT_EQ(3u, parent.children().size());
EXPECT_EQ(&child1, parent.children()[0]);
EXPECT_EQ(&child2, parent.children()[1]);
EXPECT_EQ(&child3, parent.children()[2]);
ASSERT_EQ(3u, parent.layer()->children().size());
EXPECT_EQ(child1.layer(), parent.layer()->children()[0]);
EXPECT_EQ(child2.layer(), parent.layer()->children()[1]);
EXPECT_EQ(child3.layer(), parent.layer()->children()[2]);
// Move 1 in front of 3, resulting in [2, 3, 1].
parent.StackChildAbove(&child1, &child3);
ASSERT_EQ(3u, parent.children().size());
EXPECT_EQ(&child2, parent.children()[0]);
EXPECT_EQ(&child3, parent.children()[1]);
EXPECT_EQ(&child1, parent.children()[2]);
ASSERT_EQ(3u, parent.layer()->children().size());
EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
EXPECT_EQ(child3.layer(), parent.layer()->children()[1]);
EXPECT_EQ(child1.layer(), parent.layer()->children()[2]);
// Moving 1 in front of 2 should lower it, resulting in [2, 1, 3].
parent.StackChildAbove(&child1, &child2);
ASSERT_EQ(3u, parent.children().size());
EXPECT_EQ(&child2, parent.children()[0]);
EXPECT_EQ(&child1, parent.children()[1]);
EXPECT_EQ(&child3, parent.children()[2]);
ASSERT_EQ(3u, parent.layer()->children().size());
EXPECT_EQ(child2.layer(), parent.layer()->children()[0]);
EXPECT_EQ(child1.layer(), parent.layer()->children()[1]);
EXPECT_EQ(child3.layer(), parent.layer()->children()[2]);
}
// Various capture assertions.
TEST_F(WindowTest, CaptureTests) {
CaptureWindowDelegateImpl delegate;
std::unique_ptr<Window> window(CreateTestWindowWithDelegate(
&delegate, 0, gfx::Rect(0, 0, 20, 20), root_window()));
EXPECT_FALSE(window->HasCapture());
delegate.ResetCounts();
// Do a capture.
window->SetCapture();
EXPECT_TRUE(window->HasCapture());
EXPECT_EQ(0, delegate.capture_lost_count());
EXPECT_EQ(0, delegate.capture_changed_event_count());
ui::test::EventGenerator generator(root_window(), gfx::Point(50, 50));
generator.PressLeftButton();
EXPECT_EQ(1, delegate.mouse_event_count());
generator.ReleaseLeftButton();
EXPECT_EQ(2, delegate.mouse_event_count());
delegate.ResetCounts();
ui::TouchEvent touchev(ui::ET_TOUCH_PRESSED, gfx::Point(50, 50), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&touchev);
EXPECT_EQ(1, delegate.touch_event_count());
delegate.ResetCounts();
window->ReleaseCapture();
EXPECT_FALSE(window->HasCapture());
EXPECT_EQ(1, delegate.capture_lost_count());
EXPECT_EQ(1, delegate.capture_changed_event_count());
EXPECT_EQ(1, delegate.mouse_event_count());
EXPECT_EQ(0, delegate.touch_event_count());
generator.PressLeftButton();
EXPECT_EQ(1, delegate.mouse_event_count());
ui::TouchEvent touchev2(ui::ET_TOUCH_PRESSED, gfx::Point(250, 250), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 1));
DispatchEventUsingWindowDispatcher(&touchev2);
EXPECT_EQ(0, delegate.touch_event_count());
// Removing the capture window from parent should reset the capture window
// in the root window.
window->SetCapture();
EXPECT_EQ(window.get(), aura::client::GetCaptureWindow(root_window()));
window->parent()->RemoveChild(window.get());
EXPECT_FALSE(window->HasCapture());
EXPECT_EQ(nullptr, aura::client::GetCaptureWindow(root_window()));
}
TEST_F(WindowTest, TouchCaptureCancelsOtherTouches) {
CaptureWindowDelegateImpl delegate1;
std::unique_ptr<Window> w1(CreateTestWindowWithDelegate(
&delegate1, 0, gfx::Rect(0, 0, 50, 50), root_window()));
CaptureWindowDelegateImpl delegate2;
std::unique_ptr<Window> w2(CreateTestWindowWithDelegate(
&delegate2, 0, gfx::Rect(50, 50, 50, 50), root_window()));
// Press on w1.
ui::TouchEvent press1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&press1);
// We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
EXPECT_EQ(2, delegate1.gesture_event_count());
delegate1.ResetCounts();
// Capturing to w2 should cause the touch to be canceled.
w2->SetCapture();
EXPECT_EQ(1, delegate1.touch_event_count());
EXPECT_EQ(0, delegate2.touch_event_count());
delegate1.ResetCounts();
delegate2.ResetCounts();
// Events are now untargeted.
ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 20), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&move);
EXPECT_EQ(0, delegate1.gesture_event_count());
EXPECT_EQ(0, delegate1.touch_event_count());
EXPECT_EQ(0, delegate2.gesture_event_count());
EXPECT_EQ(0, delegate2.touch_event_count());
ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&release);
EXPECT_EQ(0, delegate1.gesture_event_count());
EXPECT_EQ(0, delegate2.gesture_event_count());
// A new press is captured by w2.
ui::TouchEvent press2(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&press2);
EXPECT_EQ(0, delegate1.gesture_event_count());
// We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
EXPECT_EQ(2, delegate2.gesture_event_count());
delegate1.ResetCounts();
delegate2.ResetCounts();
// And releasing capture changes nothing.
w2->ReleaseCapture();
EXPECT_EQ(0, delegate1.gesture_event_count());
EXPECT_EQ(0, delegate1.touch_event_count());
EXPECT_EQ(0, delegate2.gesture_event_count());
EXPECT_EQ(0, delegate2.touch_event_count());
}
TEST_F(WindowTest, TouchCaptureDoesntCancelCapturedTouches) {
CaptureWindowDelegateImpl delegate;
std::unique_ptr<Window> window(CreateTestWindowWithDelegate(
&delegate, 0, gfx::Rect(0, 0, 50, 50), root_window()));
base::TimeTicks time = getTime();
const int kTimeDelta = 100;
ui::TouchEvent press(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), time,
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&press);
// We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
EXPECT_EQ(2, delegate.gesture_event_count());
EXPECT_EQ(1, delegate.touch_event_count());
delegate.ResetCounts();
window->SetCapture();
EXPECT_EQ(0, delegate.gesture_event_count());
EXPECT_EQ(0, delegate.touch_event_count());
delegate.ResetCounts();
// On move We will get TOUCH_MOVED, GESTURE_TAP_CANCEL,
// GESTURE_SCROLL_START and GESTURE_SCROLL_UPDATE.
time += base::TimeDelta::FromMilliseconds(kTimeDelta);
ui::TouchEvent move(ui::ET_TOUCH_MOVED, gfx::Point(10, 20), time,
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&move);
EXPECT_EQ(1, delegate.touch_event_count());
EXPECT_EQ(3, delegate.gesture_event_count());
delegate.ResetCounts();
// Release capture shouldn't change anything.
window->ReleaseCapture();
EXPECT_EQ(0, delegate.touch_event_count());
EXPECT_EQ(0, delegate.gesture_event_count());
delegate.ResetCounts();
// On move we still get TOUCH_MOVED and GESTURE_SCROLL_UPDATE.
time += base::TimeDelta::FromMilliseconds(kTimeDelta);
ui::TouchEvent move2(ui::ET_TOUCH_MOVED, gfx::Point(10, 30), time,
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&move2);
EXPECT_EQ(1, delegate.touch_event_count());
EXPECT_EQ(1, delegate.gesture_event_count());
delegate.ResetCounts();
// And on release we get TOUCH_RELEASED, GESTURE_SCROLL_END, GESTURE_END
time += base::TimeDelta::FromMilliseconds(kTimeDelta);
ui::TouchEvent release(ui::ET_TOUCH_RELEASED, gfx::Point(10, 20), time,
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&release);
EXPECT_EQ(1, delegate.touch_event_count());
EXPECT_EQ(2, delegate.gesture_event_count());
}
// Assertions around SetCapture() and touch/gestures.
TEST_F(WindowTest, TransferCaptureTouchEvents) {
// Touch on |w1|.
CaptureWindowDelegateImpl d1;
std::unique_ptr<Window> w1(CreateTestWindowWithDelegate(
&d1, 0, gfx::Rect(0, 0, 20, 20), root_window()));
ui::TouchEvent p1(ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
DispatchEventUsingWindowDispatcher(&p1);
// We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN.
EXPECT_EQ(1, d1.touch_event_count());
EXPECT_EQ(2, d1.gesture_event_count());
d1.ResetCounts();
// Touch on |w2| with a different id.
CaptureWindowDelegateImpl d2;
std::unique_ptr<Window> w2(CreateTestWindowWithDelegate(
&d2, 0, gfx::Rect(40, 0, 40, 20), root_window()));
ui::TouchEvent p2(ui::ET_TOUCH_PRESSED, gfx::Point(41, 10), getTime(),
ui::PointerDetails(ui::EventPointerType::kTouch, 1));
DispatchEventUsingWindowDispatcher(&p2);
EXPECT_EQ(0, d1.touch_event_count());
EXPECT_EQ(0, d1.gesture_event_count());
// We will get both GESTURE_BEGIN and GESTURE_TAP_DOWN for new target window.
EXPECT_EQ(1, d2.touch_event_count());
EXPECT_EQ(2, d2.gesture_event_count());
d1.ResetCounts();
d2.ResetCounts();
// Set capture on |w2|, this should send a cancel (TAP_CANCEL, END) to |w1|
// but not |w2|.
w2->SetCapture();
EXPECT_EQ(1, d1.touch_event_count());
EXPECT_EQ(2, d1.gesture_event_count());
EXPECT_EQ(0, d2.touch_event_count());
EXPECT_EQ(0, d2.gesture_event_count());
d1.ResetCounts();
d2.ResetCounts();
CaptureWindowDelegateImpl d3;
std::unique_ptr<Window> w3(CreateTestWindowWithDelegate(
&d3, 0, gfx::Rect(0, 0, 100, 101), root_window()));
// Set capture on |w3|. All touches have already been cancelled.
w3->SetCapture();
EXPECT_EQ(0, d1.touch_event_count());