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
|
// 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 "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/point.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_delegate.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#include "ui/views/widget/native_widget_aura.h"
#elif defined(OS_WIN)
#include "ui/views/widget/native_widget_win.h"
#endif
namespace views {
namespace {
// A generic typedef to pick up relevant NativeWidget implementations.
#if defined(USE_AURA)
typedef NativeWidgetAura NativeWidgetPlatform;
#elif defined(OS_WIN)
typedef NativeWidgetWin NativeWidgetPlatform;
#endif
// A widget that assumes mouse capture always works. It won't on Aura in
// testing, so we mock it.
#if defined(USE_AURA)
class NativeWidgetCapture : public NativeWidgetPlatform {
public:
explicit NativeWidgetCapture(internal::NativeWidgetDelegate* delegate)
: NativeWidgetPlatform(delegate),
mouse_capture_(false) {}
virtual ~NativeWidgetCapture() {}
virtual void SetCapture(unsigned int flags) OVERRIDE {
if (!(flags & ui::CW_LOCK_MOUSE))
return;
mouse_capture_ = true;
}
virtual void ReleaseCapture() OVERRIDE {
if (mouse_capture_)
delegate()->OnMouseCaptureLost();
mouse_capture_ = false;
}
virtual bool HasCapture(unsigned int flags) const OVERRIDE {
if (flags == ui::CW_LOCK_MOUSE)
return mouse_capture_;
return false;
}
private:
bool mouse_capture_;
DISALLOW_COPY_AND_ASSIGN(NativeWidgetCapture);
};
#endif
// A typedef that inserts our mock-capture NativeWidget implementation for
// relevant platforms.
#if defined(USE_AURA)
typedef NativeWidgetCapture NativeWidgetPlatformForTest;
#elif defined(OS_WIN)
typedef NativeWidgetWin NativeWidgetPlatformForTest;
#endif
// A view that always processes all mouse events.
class MouseView : public View {
public:
MouseView() : View() {
}
virtual ~MouseView() {}
virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE {
return true;
}
};
typedef ViewsTestBase WidgetTest;
NativeWidget* CreatePlatformNativeWidget(
internal::NativeWidgetDelegate* delegate) {
return new NativeWidgetPlatformForTest(delegate);
}
Widget* CreateTopLevelPlatformWidget() {
Widget* toplevel = new Widget;
Widget::InitParams toplevel_params(Widget::InitParams::TYPE_WINDOW);
toplevel_params.native_widget = CreatePlatformNativeWidget(toplevel);
toplevel->Init(toplevel_params);
return toplevel;
}
Widget* CreateChildPlatformWidget(gfx::NativeView parent_native_view) {
Widget* child = new Widget;
Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL);
child_params.native_widget = CreatePlatformNativeWidget(child);
child_params.parent = parent_native_view;
child->Init(child_params);
child->SetContentsView(new View);
return child;
}
#if defined(OS_WIN) && !defined(USE_AURA)
// On Windows, it is possible for us to have a child window that is TYPE_POPUP.
Widget* CreateChildPopupPlatformWidget(gfx::NativeView parent_native_view) {
Widget* child = new Widget;
Widget::InitParams child_params(Widget::InitParams::TYPE_POPUP);
child_params.child = true;
child_params.native_widget = CreatePlatformNativeWidget(child);
child_params.parent = parent_native_view;
child->Init(child_params);
child->SetContentsView(new View);
return child;
}
#endif
Widget* CreateTopLevelNativeWidget() {
Widget* toplevel = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
toplevel->Init(params);
toplevel->SetContentsView(new View);
return toplevel;
}
Widget* CreateChildNativeWidgetWithParent(Widget* parent) {
Widget* child = new Widget;
Widget::InitParams params(Widget::InitParams::TYPE_CONTROL);
params.parent_widget = parent;
child->Init(params);
child->SetContentsView(new View);
return child;
}
Widget* CreateChildNativeWidget() {
return CreateChildNativeWidgetWithParent(NULL);
}
bool WidgetHasMouseCapture(const Widget* widget) {
return static_cast<const internal::NativeWidgetPrivate*>(widget->
native_widget())->HasCapture(ui::CW_LOCK_MOUSE);
}
ui::WindowShowState GetWidgetShowState(const Widget* widget) {
// Use IsMaximized/IsMinimized/IsFullScreen instead of GetWindowPlacement
// because the former is implemented on all platforms but the latter is not.
return widget->IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN :
widget->IsMaximized() ? ui::SHOW_STATE_MAXIMIZED :
widget->IsMinimized() ? ui::SHOW_STATE_MINIMIZED :
ui::SHOW_STATE_NORMAL;
}
TEST_F(WidgetTest, WidgetInitParams) {
ASSERT_FALSE(views_delegate().UseTransparentWindows());
// Widgets are not transparent by default.
Widget::InitParams init1;
EXPECT_FALSE(init1.transparent);
// Non-window widgets are not transparent either.
Widget::InitParams init2(Widget::InitParams::TYPE_MENU);
EXPECT_FALSE(init2.transparent);
// A ViewsDelegate can set windows transparent by default.
views_delegate().SetUseTransparentWindows(true);
Widget::InitParams init3;
EXPECT_TRUE(init3.transparent);
// Non-window widgets stay opaque.
Widget::InitParams init4(Widget::InitParams::TYPE_MENU);
EXPECT_FALSE(init4.transparent);
}
////////////////////////////////////////////////////////////////////////////////
// Widget::GetTopLevelWidget tests.
TEST_F(WidgetTest, GetTopLevelWidget_Native) {
// Create a hierarchy of native widgets.
Widget* toplevel = CreateTopLevelPlatformWidget();
gfx::NativeView parent = toplevel->GetNativeView();
Widget* child = CreateChildPlatformWidget(parent);
EXPECT_EQ(toplevel, toplevel->GetTopLevelWidget());
EXPECT_EQ(toplevel, child->GetTopLevelWidget());
toplevel->CloseNow();
// |child| should be automatically destroyed with |toplevel|.
}
// Tests some grab/ungrab events.
TEST_F(WidgetTest, DISABLED_GrabUngrab) {
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* child1 = CreateChildNativeWidgetWithParent(toplevel);
Widget* child2 = CreateChildNativeWidgetWithParent(toplevel);
toplevel->SetBounds(gfx::Rect(0, 0, 500, 500));
child1->SetBounds(gfx::Rect(10, 10, 300, 300));
View* view = new MouseView();
view->SetBounds(0, 0, 300, 300);
child1->GetRootView()->AddChildView(view);
child2->SetBounds(gfx::Rect(200, 10, 200, 200));
view = new MouseView();
view->SetBounds(0, 0, 200, 200);
child2->GetRootView()->AddChildView(view);
toplevel->Show();
RunPendingMessages();
// Click on child1
MouseEvent pressed(ui::ET_MOUSE_PRESSED, 45, 45, ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(pressed);
EXPECT_TRUE(WidgetHasMouseCapture(toplevel));
EXPECT_TRUE(WidgetHasMouseCapture(child1));
EXPECT_FALSE(WidgetHasMouseCapture(child2));
MouseEvent released(ui::ET_MOUSE_RELEASED, 45, 45, ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(released);
EXPECT_FALSE(WidgetHasMouseCapture(toplevel));
EXPECT_FALSE(WidgetHasMouseCapture(child1));
EXPECT_FALSE(WidgetHasMouseCapture(child2));
RunPendingMessages();
// Click on child2
MouseEvent pressed2(ui::ET_MOUSE_PRESSED, 315, 45, ui::EF_LEFT_MOUSE_BUTTON);
EXPECT_TRUE(toplevel->OnMouseEvent(pressed2));
EXPECT_TRUE(WidgetHasMouseCapture(toplevel));
EXPECT_TRUE(WidgetHasMouseCapture(child2));
EXPECT_FALSE(WidgetHasMouseCapture(child1));
MouseEvent released2(ui::ET_MOUSE_RELEASED, 315, 45,
ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(released2);
EXPECT_FALSE(WidgetHasMouseCapture(toplevel));
EXPECT_FALSE(WidgetHasMouseCapture(child1));
EXPECT_FALSE(WidgetHasMouseCapture(child2));
toplevel->CloseNow();
}
// Test if a focus manager and an inputmethod work without CHECK failure
// when window activation changes.
TEST_F(WidgetTest, ChangeActivation) {
Widget* top1 = CreateTopLevelPlatformWidget();
// CreateInputMethod before activated
top1->GetInputMethod();
top1->Show();
RunPendingMessages();
Widget* top2 = CreateTopLevelPlatformWidget();
top2->Show();
RunPendingMessages();
top1->Activate();
RunPendingMessages();
// Create InputMethod after deactivated.
top2->GetInputMethod();
top2->Activate();
RunPendingMessages();
top1->Activate();
RunPendingMessages();
top1->CloseNow();
top2->CloseNow();
}
// Tests visibility of child widgets.
TEST_F(WidgetTest, Visibility) {
Widget* toplevel = CreateTopLevelPlatformWidget();
gfx::NativeView parent = toplevel->GetNativeView();
Widget* child = CreateChildPlatformWidget(parent);
EXPECT_FALSE(toplevel->IsVisible());
EXPECT_FALSE(child->IsVisible());
child->Show();
EXPECT_FALSE(toplevel->IsVisible());
EXPECT_FALSE(child->IsVisible());
toplevel->Show();
EXPECT_TRUE(toplevel->IsVisible());
EXPECT_TRUE(child->IsVisible());
toplevel->CloseNow();
// |child| should be automatically destroyed with |toplevel|.
}
#if defined(OS_WIN) && !defined(USE_AURA)
// On Windows, it is possible to have child window that are TYPE_POPUP. Unlike
// regular child windows, these should be created as hidden and must be shown
// explicitly.
TEST_F(WidgetTest, Visibility_ChildPopup) {
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* child_popup = CreateChildPopupPlatformWidget(
toplevel->GetNativeView());
EXPECT_FALSE(toplevel->IsVisible());
EXPECT_FALSE(child_popup->IsVisible());
toplevel->Show();
EXPECT_TRUE(toplevel->IsVisible());
EXPECT_FALSE(child_popup->IsVisible());
child_popup->Show();
EXPECT_TRUE(child_popup->IsVisible());
toplevel->CloseNow();
// |child_popup| should be automatically destroyed with |toplevel|.
}
#endif
////////////////////////////////////////////////////////////////////////////////
// Widget ownership tests.
//
// Tests various permutations of Widget ownership specified in the
// InitParams::Ownership param.
// A WidgetTest that supplies a toplevel widget for NativeWidget to parent to.
class WidgetOwnershipTest : public WidgetTest {
public:
WidgetOwnershipTest() {}
virtual ~WidgetOwnershipTest() {}
virtual void SetUp() {
WidgetTest::SetUp();
desktop_widget_ = CreateTopLevelPlatformWidget();
}
virtual void TearDown() {
desktop_widget_->CloseNow();
WidgetTest::TearDown();
}
private:
Widget* desktop_widget_;
DISALLOW_COPY_AND_ASSIGN(WidgetOwnershipTest);
};
// A bag of state to monitor destructions.
struct OwnershipTestState {
OwnershipTestState() : widget_deleted(false), native_widget_deleted(false) {}
bool widget_deleted;
bool native_widget_deleted;
};
// A platform NativeWidget subclass that updates a bag of state when it is
// destroyed.
class OwnershipTestNativeWidget : public NativeWidgetPlatform {
public:
OwnershipTestNativeWidget(internal::NativeWidgetDelegate* delegate,
OwnershipTestState* state)
: NativeWidgetPlatform(delegate),
state_(state) {
}
virtual ~OwnershipTestNativeWidget() {
state_->native_widget_deleted = true;
}
private:
OwnershipTestState* state_;
DISALLOW_COPY_AND_ASSIGN(OwnershipTestNativeWidget);
};
// A views NativeWidget subclass that updates a bag of state when it is
// destroyed.
class OwnershipTestNativeWidgetPlatform : public NativeWidgetPlatformForTest {
public:
OwnershipTestNativeWidgetPlatform(internal::NativeWidgetDelegate* delegate,
OwnershipTestState* state)
: NativeWidgetPlatformForTest(delegate),
state_(state) {
}
virtual ~OwnershipTestNativeWidgetPlatform() {
state_->native_widget_deleted = true;
}
private:
OwnershipTestState* state_;
DISALLOW_COPY_AND_ASSIGN(OwnershipTestNativeWidgetPlatform);
};
// A Widget subclass that updates a bag of state when it is destroyed.
class OwnershipTestWidget : public Widget {
public:
explicit OwnershipTestWidget(OwnershipTestState* state) : state_(state) {}
virtual ~OwnershipTestWidget() {
state_->widget_deleted = true;
}
private:
OwnershipTestState* state_;
DISALLOW_COPY_AND_ASSIGN(OwnershipTestWidget);
};
// Widget owns its NativeWidget, part 1: NativeWidget is a platform-native
// widget.
TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsPlatformNativeWidget) {
OwnershipTestState state;
scoped_ptr<Widget> widget(new OwnershipTestWidget(&state));
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget.get(), &state);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
// Now delete the Widget, which should delete the NativeWidget.
widget.reset();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
// TODO(beng): write test for this ownership scenario and the NativeWidget
// being deleted out from under the Widget.
}
// Widget owns its NativeWidget, part 2: NativeWidget is a NativeWidget.
TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsViewsNativeWidget) {
OwnershipTestState state;
scoped_ptr<Widget> widget(new OwnershipTestWidget(&state));
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget.get(), &state);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
// Now delete the Widget, which should delete the NativeWidget.
widget.reset();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
// TODO(beng): write test for this ownership scenario and the NativeWidget
// being deleted out from under the Widget.
}
// Widget owns its NativeWidget, part 3: NativeWidget is a NativeWidget,
// destroy the parent view.
TEST_F(WidgetOwnershipTest,
Ownership_WidgetOwnsViewsNativeWidget_DestroyParentView) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
scoped_ptr<Widget> widget(new OwnershipTestWidget(&state));
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget.get(), &state);
params.parent_widget = toplevel;
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
// Now close the toplevel, which deletes the view hierarchy.
toplevel->CloseNow();
RunPendingMessages();
// This shouldn't delete the widget because it shouldn't be deleted
// from the native side.
EXPECT_FALSE(state.widget_deleted);
EXPECT_FALSE(state.native_widget_deleted);
// Now delete it explicitly.
widget.reset();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 1: NativeWidget is a platform-native
// widget.
TEST_F(WidgetOwnershipTest, Ownership_PlatformNativeWidgetOwnsWidget) {
OwnershipTestState state;
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget, &state);
widget->Init(params);
// Now destroy the native widget.
widget->CloseNow();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 2: NativeWidget is a NativeWidget.
TEST_F(WidgetOwnershipTest, Ownership_ViewsNativeWidgetOwnsWidget) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget, &state);
params.parent_widget = toplevel;
widget->Init(params);
// Now destroy the native widget. This is achieved by closing the toplevel.
toplevel->CloseNow();
// The NativeWidget won't be deleted until after a return to the message loop
// so we have to run pending messages before testing the destruction status.
RunPendingMessages();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 3: NativeWidget is a platform-native
// widget, destroyed out from under it by the OS.
TEST_F(WidgetOwnershipTest,
Ownership_PlatformNativeWidgetOwnsWidget_NativeDestroy) {
OwnershipTestState state;
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget, &state);
widget->Init(params);
// Now simulate a destroy of the platform native widget from the OS:
#if defined(USE_AURA)
delete widget->GetNativeView();
#elif defined(OS_WIN)
DestroyWindow(widget->GetNativeView());
#endif
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 4: NativeWidget is a NativeWidget,
// destroyed by the view hierarchy that contains it.
TEST_F(WidgetOwnershipTest,
Ownership_ViewsNativeWidgetOwnsWidget_NativeDestroy) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget, &state);
params.parent_widget = toplevel;
widget->Init(params);
// Destroy the widget (achieved by closing the toplevel).
toplevel->CloseNow();
// The NativeWidget won't be deleted until after a return to the message loop
// so we have to run pending messages before testing the destruction status.
RunPendingMessages();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 5: NativeWidget is a NativeWidget,
// we close it directly.
TEST_F(WidgetOwnershipTest,
Ownership_ViewsNativeWidgetOwnsWidget_Close) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetPlatform(widget, &state);
params.parent_widget = toplevel;
widget->Init(params);
// Destroy the widget.
widget->Close();
toplevel->CloseNow();
// The NativeWidget won't be deleted until after a return to the message loop
// so we have to run pending messages before testing the destruction status.
RunPendingMessages();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
////////////////////////////////////////////////////////////////////////////////
// Widget observer tests.
//
class WidgetObserverTest : public WidgetTest,
Widget::Observer {
public:
WidgetObserverTest()
: active_(NULL),
widget_closed_(NULL),
widget_activated_(NULL),
widget_shown_(NULL),
widget_hidden_(NULL) {
}
virtual ~WidgetObserverTest() {}
virtual void OnWidgetClosing(Widget* widget) OVERRIDE {
if (active_ == widget)
active_ = NULL;
widget_closed_ = widget;
}
virtual void OnWidgetActivationChanged(Widget* widget,
bool active) OVERRIDE {
if (active) {
if (widget_activated_)
widget_activated_->Deactivate();
widget_activated_ = widget;
active_ = widget;
} else {
if (widget_activated_ == widget)
widget_activated_ = NULL;
widget_deactivated_ = widget;
}
}
virtual void OnWidgetVisibilityChanged(Widget* widget,
bool visible) OVERRIDE {
if (visible)
widget_shown_ = widget;
else
widget_hidden_ = widget;
}
void reset() {
active_ = NULL;
widget_closed_ = NULL;
widget_activated_ = NULL;
widget_deactivated_ = NULL;
widget_shown_ = NULL;
widget_hidden_ = NULL;
}
Widget* NewWidget() {
Widget* widget = CreateTopLevelNativeWidget();
widget->AddObserver(this);
return widget;
}
const Widget* active() const { return active_; }
const Widget* widget_closed() const { return widget_closed_; }
const Widget* widget_activated() const { return widget_activated_; }
const Widget* widget_deactivated() const { return widget_deactivated_; }
const Widget* widget_shown() const { return widget_shown_; }
const Widget* widget_hidden() const { return widget_hidden_; }
private:
Widget* active_;
Widget* widget_closed_;
Widget* widget_activated_;
Widget* widget_deactivated_;
Widget* widget_shown_;
Widget* widget_hidden_;
};
TEST_F(WidgetObserverTest, DISABLED_ActivationChange) {
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* toplevel1 = NewWidget();
Widget* toplevel2 = NewWidget();
toplevel1->Show();
toplevel2->Show();
reset();
toplevel1->Activate();
RunPendingMessages();
EXPECT_EQ(toplevel1, widget_activated());
toplevel2->Activate();
RunPendingMessages();
EXPECT_EQ(toplevel1, widget_deactivated());
EXPECT_EQ(toplevel2, widget_activated());
EXPECT_EQ(toplevel2, active());
toplevel->CloseNow();
}
TEST_F(WidgetObserverTest, DISABLED_VisibilityChange) {
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* child1 = NewWidget();
Widget* child2 = NewWidget();
toplevel->Show();
child1->Show();
child2->Show();
reset();
child1->Hide();
EXPECT_EQ(child1, widget_hidden());
child2->Hide();
EXPECT_EQ(child2, widget_hidden());
child1->Show();
EXPECT_EQ(child1, widget_shown());
child2->Show();
EXPECT_EQ(child2, widget_shown());
toplevel->CloseNow();
}
TEST_F(WidgetObserverTest, DestroyBubble) {
Widget* anchor = CreateTopLevelPlatformWidget();
View* view = new View;
anchor->SetContentsView(view);
anchor->Show();
BubbleDelegateView* bubble_delegate =
new BubbleDelegateView(view, BubbleBorder::NONE);
Widget* bubble_widget(BubbleDelegateView::CreateBubble(bubble_delegate));
bubble_widget->Show();
bubble_widget->CloseNow();
anchor->Hide();
anchor->CloseNow();
}
#if !defined(USE_AURA) && defined(OS_WIN)
// Aura needs shell to maximize/fullscreen window.
// NativeWidgetGtk doesn't implement GetRestoredBounds.
TEST_F(WidgetTest, GetRestoredBounds) {
Widget* toplevel = CreateTopLevelPlatformWidget();
EXPECT_EQ(toplevel->GetWindowScreenBounds().ToString(),
toplevel->GetRestoredBounds().ToString());
toplevel->Show();
toplevel->Maximize();
RunPendingMessages();
EXPECT_NE(toplevel->GetWindowScreenBounds().ToString(),
toplevel->GetRestoredBounds().ToString());
EXPECT_GT(toplevel->GetRestoredBounds().width(), 0);
EXPECT_GT(toplevel->GetRestoredBounds().height(), 0);
toplevel->Restore();
RunPendingMessages();
EXPECT_EQ(toplevel->GetWindowScreenBounds().ToString(),
toplevel->GetRestoredBounds().ToString());
toplevel->SetFullscreen(true);
RunPendingMessages();
EXPECT_NE(toplevel->GetWindowScreenBounds().ToString(),
toplevel->GetRestoredBounds().ToString());
EXPECT_GT(toplevel->GetRestoredBounds().width(), 0);
EXPECT_GT(toplevel->GetRestoredBounds().height(), 0);
}
#endif
// Test that window state is not changed after getting out of full screen.
TEST_F(WidgetTest, ExitFullscreenRestoreState) {
Widget* toplevel = CreateTopLevelPlatformWidget();
toplevel->Show();
RunPendingMessages();
// This should be a normal state window.
EXPECT_EQ(ui::SHOW_STATE_NORMAL, GetWidgetShowState(toplevel));
toplevel->SetFullscreen(true);
while (GetWidgetShowState(toplevel) != ui::SHOW_STATE_FULLSCREEN)
RunPendingMessages();
toplevel->SetFullscreen(false);
while (GetWidgetShowState(toplevel) == ui::SHOW_STATE_FULLSCREEN)
RunPendingMessages();
// And it should still be in normal state after getting out of full screen.
EXPECT_EQ(ui::SHOW_STATE_NORMAL, GetWidgetShowState(toplevel));
// Now, make it maximized.
toplevel->Maximize();
while (GetWidgetShowState(toplevel) != ui::SHOW_STATE_MAXIMIZED)
RunPendingMessages();
toplevel->SetFullscreen(true);
while (GetWidgetShowState(toplevel) != ui::SHOW_STATE_FULLSCREEN)
RunPendingMessages();
toplevel->SetFullscreen(false);
while (GetWidgetShowState(toplevel) == ui::SHOW_STATE_FULLSCREEN)
RunPendingMessages();
// And it stays maximized after getting out of full screen.
EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, GetWidgetShowState(toplevel));
// Clean up.
toplevel->Close();
RunPendingMessages();
}
} // namespace
} // namespace views
|