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
|
// Copyright (c) 2013 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/bind.h"
#include "base/run_loop.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"
#if defined(USE_AURA)
#include "ui/aura/client/activation_client.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#endif
#if defined(USE_AURA) && !defined(OS_CHROMEOS)
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#endif
#if defined(OS_WIN)
#include "ui/views/win/hwnd_util.h"
#endif
namespace views {
namespace test {
namespace {
// A View that closes the Widget and exits the current message-loop when it
// receives a mouse-release event.
class ExitLoopOnRelease : public View {
public:
ExitLoopOnRelease() {}
virtual ~ExitLoopOnRelease() {}
private:
// Overridden from View:
virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE {
GetWidget()->Close();
base::MessageLoop::current()->QuitNow();
}
DISALLOW_COPY_AND_ASSIGN(ExitLoopOnRelease);
};
// A view that does a capture on gesture-begin events.
class GestureCaptureView : public View {
public:
GestureCaptureView() {}
virtual ~GestureCaptureView() {}
private:
// Overridden from View:
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
if (event->type() == ui::ET_GESTURE_BEGIN) {
GetWidget()->SetCapture(this);
event->StopPropagation();
}
}
DISALLOW_COPY_AND_ASSIGN(GestureCaptureView);
};
// A view that always processes all mouse events.
class MouseView : public View {
public:
MouseView()
: View(),
entered_(0),
exited_(0),
pressed_(0) {
}
virtual ~MouseView() {}
virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE {
pressed_++;
return true;
}
virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE {
entered_++;
}
virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE {
exited_++;
}
// Return the number of OnMouseEntered calls and reset the counter.
int EnteredCalls() {
int i = entered_;
entered_ = 0;
return i;
}
// Return the number of OnMouseExited calls and reset the counter.
int ExitedCalls() {
int i = exited_;
exited_ = 0;
return i;
}
int pressed() const { return pressed_; }
private:
int entered_;
int exited_;
int pressed_;
DISALLOW_COPY_AND_ASSIGN(MouseView);
};
// A View that shows a different widget, sets capture on that widget, and
// initiates a nested message-loop when it receives a mouse-press event.
class NestedLoopCaptureView : public View {
public:
explicit NestedLoopCaptureView(Widget* widget) : widget_(widget) {}
virtual ~NestedLoopCaptureView() {}
private:
// Overridden from View:
virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE {
// Start a nested loop.
widget_->Show();
widget_->SetCapture(widget_->GetContentsView());
EXPECT_TRUE(widget_->HasCapture());
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow(loop);
base::RunLoop run_loop;
#if defined(USE_AURA)
run_loop.set_dispatcher(aura::Env::GetInstance()->GetDispatcher());
#endif
run_loop.Run();
return true;
}
Widget* widget_;
DISALLOW_COPY_AND_ASSIGN(NestedLoopCaptureView);
};
} // namespace
#if defined(OS_WIN) && defined(USE_AURA)
// Tests whether activation and focus change works correctly in Windows AURA.
// We test the following:-
// 1. If the active aura window is correctly set when a top level widget is
// created.
// 2. If the active aura window in widget 1 created above, is set to NULL when
// another top level widget is created and focused.
// 3. On focusing the native platform window for widget 1, the active aura
// window for widget 1 should be set and that for widget 2 should reset.
// TODO(ananta)
// Discuss with erg on how to write this test for linux x11 aura.
TEST_F(WidgetTest, DesktopNativeWidgetAuraActivationAndFocusTest) {
// Create widget 1 and expect the active window to be its window.
View* contents_view1 = new View;
contents_view1->set_focusable(true);
Widget widget1;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
init_params.bounds = gfx::Rect(0, 0, 200, 200);
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.native_widget = new DesktopNativeWidgetAura(&widget1);
widget1.Init(init_params);
widget1.SetContentsView(contents_view1);
widget1.Show();
aura::Window* root_window1= widget1.GetNativeView()->GetRootWindow();
contents_view1->RequestFocus();
EXPECT_TRUE(root_window1 != NULL);
aura::client::ActivationClient* activation_client1 =
aura::client::GetActivationClient(root_window1);
EXPECT_TRUE(activation_client1 != NULL);
EXPECT_EQ(activation_client1->GetActiveWindow(), widget1.GetNativeView());
// Create widget 2 and expect the active window to be its window.
View* contents_view2 = new View;
Widget widget2;
Widget::InitParams init_params2 =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
init_params2.bounds = gfx::Rect(0, 0, 200, 200);
init_params2.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params2.native_widget = new DesktopNativeWidgetAura(&widget2);
widget2.Init(init_params2);
widget2.SetContentsView(contents_view2);
widget2.Show();
aura::Window* root_window2 = widget2.GetNativeView()->GetRootWindow();
contents_view2->RequestFocus();
::SetActiveWindow(root_window2->GetDispatcher()->GetAcceleratedWidget());
aura::client::ActivationClient* activation_client2 =
aura::client::GetActivationClient(root_window2);
EXPECT_TRUE(activation_client2 != NULL);
EXPECT_EQ(activation_client2->GetActiveWindow(), widget2.GetNativeView());
EXPECT_EQ(activation_client1->GetActiveWindow(),
reinterpret_cast<aura::Window*>(NULL));
// Now set focus back to widget 1 and expect the active window to be its
// window.
contents_view1->RequestFocus();
::SetActiveWindow(root_window1->GetDispatcher()->GetAcceleratedWidget());
EXPECT_EQ(activation_client2->GetActiveWindow(),
reinterpret_cast<aura::Window*>(NULL));
EXPECT_EQ(activation_client1->GetActiveWindow(), widget1.GetNativeView());
}
#endif
TEST_F(WidgetTest, CaptureAutoReset) {
Widget* toplevel = CreateTopLevelFramelessPlatformWidget();
View* container = new View;
toplevel->SetContentsView(container);
EXPECT_FALSE(toplevel->HasCapture());
toplevel->SetCapture(NULL);
EXPECT_TRUE(toplevel->HasCapture());
// By default, mouse release removes capture.
gfx::Point click_location(45, 15);
ui::MouseEvent release(ui::ET_MOUSE_RELEASED, click_location, click_location,
ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(&release);
EXPECT_FALSE(toplevel->HasCapture());
// Now a mouse release shouldn't remove capture.
toplevel->set_auto_release_capture(false);
toplevel->SetCapture(NULL);
EXPECT_TRUE(toplevel->HasCapture());
toplevel->OnMouseEvent(&release);
EXPECT_TRUE(toplevel->HasCapture());
toplevel->ReleaseCapture();
EXPECT_FALSE(toplevel->HasCapture());
toplevel->Close();
RunPendingMessages();
}
TEST_F(WidgetTest, ResetCaptureOnGestureEnd) {
Widget* toplevel = CreateTopLevelFramelessPlatformWidget();
View* container = new View;
toplevel->SetContentsView(container);
View* gesture = new GestureCaptureView;
gesture->SetBounds(0, 0, 30, 30);
container->AddChildView(gesture);
MouseView* mouse = new MouseView;
mouse->SetBounds(30, 0, 30, 30);
container->AddChildView(mouse);
toplevel->SetSize(gfx::Size(100, 100));
toplevel->Show();
// Start a gesture on |gesture|.
ui::GestureEvent begin(ui::ET_GESTURE_BEGIN,
15, 15, 0, base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_BEGIN, 0, 0), 1);
ui::GestureEvent end(ui::ET_GESTURE_END,
15, 15, 0, base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_END, 0, 0), 1);
toplevel->OnGestureEvent(&begin);
// Now try to click on |mouse|. Since |gesture| will have capture, |mouse|
// will not receive the event.
gfx::Point click_location(45, 15);
ui::MouseEvent press(ui::ET_MOUSE_PRESSED, click_location, click_location,
ui::EF_LEFT_MOUSE_BUTTON);
ui::MouseEvent release(ui::ET_MOUSE_RELEASED, click_location, click_location,
ui::EF_LEFT_MOUSE_BUTTON);
EXPECT_TRUE(toplevel->HasCapture());
toplevel->OnMouseEvent(&press);
toplevel->OnMouseEvent(&release);
EXPECT_EQ(0, mouse->pressed());
EXPECT_FALSE(toplevel->HasCapture());
// The end of the gesture should release the capture, and pressing on |mouse|
// should now reach |mouse|.
toplevel->OnGestureEvent(&end);
toplevel->OnMouseEvent(&press);
toplevel->OnMouseEvent(&release);
EXPECT_EQ(1, mouse->pressed());
toplevel->Close();
RunPendingMessages();
}
// Checks that if a mouse-press triggers a capture on a different widget (which
// consumes the mouse-release event), then the target of the press does not have
// capture.
TEST_F(WidgetTest, DisableCaptureWidgetFromMousePress) {
// The test creates two widgets: |first| and |second|.
// The View in |first| makes |second| visible, sets capture on it, and starts
// a nested loop (like a menu does). The View in |second| terminates the
// nested loop and closes the widget.
// The test sends a mouse-press event to |first|, and posts a task to send a
// release event to |second|, to make sure that the release event is
// dispatched after the nested loop starts.
Widget* first = CreateTopLevelFramelessPlatformWidget();
Widget* second = CreateTopLevelFramelessPlatformWidget();
View* container = new NestedLoopCaptureView(second);
first->SetContentsView(container);
second->SetContentsView(new ExitLoopOnRelease());
first->SetSize(gfx::Size(100, 100));
first->Show();
gfx::Point location(20, 20);
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(&Widget::OnMouseEvent,
base::Unretained(second),
base::Owned(new ui::MouseEvent(ui::ET_MOUSE_RELEASED,
location,
location,
ui::EF_LEFT_MOUSE_BUTTON))));
ui::MouseEvent press(ui::ET_MOUSE_PRESSED, location, location,
ui::EF_LEFT_MOUSE_BUTTON);
first->OnMouseEvent(&press);
EXPECT_FALSE(first->HasCapture());
first->Close();
RunPendingMessages();
}
// Tests some grab/ungrab events.
// TODO(estade): can this be enabled now that this is an interactive ui test?
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
gfx::Point p1(45, 45);
ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, p1, p1,
ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(&pressed);
EXPECT_TRUE(toplevel->HasCapture());
EXPECT_TRUE(child1->HasCapture());
EXPECT_FALSE(child2->HasCapture());
ui::MouseEvent released(ui::ET_MOUSE_RELEASED, p1, p1,
ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(&released);
EXPECT_FALSE(toplevel->HasCapture());
EXPECT_FALSE(child1->HasCapture());
EXPECT_FALSE(child2->HasCapture());
RunPendingMessages();
// Click on child2
gfx::Point p2(315, 45);
ui::MouseEvent pressed2(ui::ET_MOUSE_PRESSED, p2, p2,
ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(&pressed2);
EXPECT_TRUE(pressed2.handled());
EXPECT_TRUE(toplevel->HasCapture());
EXPECT_TRUE(child2->HasCapture());
EXPECT_FALSE(child1->HasCapture());
ui::MouseEvent released2(ui::ET_MOUSE_RELEASED, p2, p2,
ui::EF_LEFT_MOUSE_BUTTON);
toplevel->OnMouseEvent(&released2);
EXPECT_FALSE(toplevel->HasCapture());
EXPECT_FALSE(child1->HasCapture());
EXPECT_FALSE(child2->HasCapture());
toplevel->CloseNow();
}
// Tests mouse move outside of the window into the "resize controller" and back
// will still generate an OnMouseEntered and OnMouseExited event..
TEST_F(WidgetTest, CheckResizeControllerEvents) {
Widget* toplevel = CreateTopLevelPlatformWidget();
toplevel->SetBounds(gfx::Rect(0, 0, 100, 100));
MouseView* view = new MouseView();
view->SetBounds(90, 90, 10, 10);
toplevel->GetRootView()->AddChildView(view);
toplevel->Show();
RunPendingMessages();
// Move to an outside position.
gfx::Point p1(200, 200);
ui::MouseEvent moved_out(ui::ET_MOUSE_MOVED, p1, p1, ui::EF_NONE);
toplevel->OnMouseEvent(&moved_out);
EXPECT_EQ(0, view->EnteredCalls());
EXPECT_EQ(0, view->ExitedCalls());
// Move onto the active view.
gfx::Point p2(95, 95);
ui::MouseEvent moved_over(ui::ET_MOUSE_MOVED, p2, p2, ui::EF_NONE);
toplevel->OnMouseEvent(&moved_over);
EXPECT_EQ(1, view->EnteredCalls());
EXPECT_EQ(0, view->ExitedCalls());
// Move onto the outer resizing border.
gfx::Point p3(102, 95);
ui::MouseEvent moved_resizer(ui::ET_MOUSE_MOVED, p3, p3, ui::EF_NONE);
toplevel->OnMouseEvent(&moved_resizer);
EXPECT_EQ(0, view->EnteredCalls());
EXPECT_EQ(1, view->ExitedCalls());
// Move onto the view again.
toplevel->OnMouseEvent(&moved_over);
EXPECT_EQ(1, view->EnteredCalls());
EXPECT_EQ(0, view->ExitedCalls());
RunPendingMessages();
toplevel->CloseNow();
}
#if defined(OS_WIN)
// This class subclasses the Widget class to listen for activation change
// notifications and provides accessors to return information as to whether
// the widget is active. We need this to ensure that users of the widget
// class activate the widget only when the underlying window becomes really
// active. Previously we would activate the widget in the WM_NCACTIVATE
// message which is incorrect because APIs like FlashWindowEx flash the
// window caption by sending fake WM_NCACTIVATE messages.
class WidgetActivationTest : public Widget {
public:
WidgetActivationTest()
: active_(false) {}
virtual ~WidgetActivationTest() {}
virtual void OnNativeWidgetActivationChanged(bool active) OVERRIDE {
active_ = active;
}
bool active() const { return active_; }
private:
bool active_;
DISALLOW_COPY_AND_ASSIGN(WidgetActivationTest);
};
// Tests whether the widget only becomes active when the underlying window
// is really active.
TEST_F(WidgetTest, WidgetNotActivatedOnFakeActivationMessages) {
WidgetActivationTest widget1;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
#if defined(USE_AURA)
init_params.native_widget = new DesktopNativeWidgetAura(&widget1);
#endif
init_params.bounds = gfx::Rect(0, 0, 200, 200);
widget1.Init(init_params);
widget1.Show();
EXPECT_EQ(true, widget1.active());
WidgetActivationTest widget2;
#if defined(USE_AURA)
init_params.native_widget = new DesktopNativeWidgetAura(&widget2);
#endif
widget2.Init(init_params);
widget2.Show();
EXPECT_EQ(true, widget2.active());
EXPECT_EQ(false, widget1.active());
HWND win32_native_window1 = HWNDForWidget(&widget1);
EXPECT_TRUE(::IsWindow(win32_native_window1));
::SendMessage(win32_native_window1, WM_NCACTIVATE, 1, 0);
EXPECT_EQ(false, widget1.active());
EXPECT_EQ(true, widget2.active());
::SetActiveWindow(win32_native_window1);
EXPECT_EQ(true, widget1.active());
EXPECT_EQ(false, widget2.active());
}
#endif
#if defined(USE_AURA) && !defined(OS_CHROMEOS)
// Provides functionality to create a window modal dialog.
class ModalDialogDelegate : public DialogDelegateView {
public:
ModalDialogDelegate() {}
virtual ~ModalDialogDelegate() {}
// WidgetDelegate overrides.
virtual ui::ModalType GetModalType() const OVERRIDE {
return ui::MODAL_TYPE_WINDOW;
}
private:
DISALLOW_COPY_AND_ASSIGN(ModalDialogDelegate);
};
// Tests whether the focused window is set correctly when a modal window is
// created and destroyed. When it is destroyed it should focus the owner
// window.
TEST_F(WidgetTest, WindowModalWindowDestroyedActivationTest) {
// Create a top level widget.
Widget top_level_widget;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
init_params.show_state = ui::SHOW_STATE_NORMAL;
gfx::Rect initial_bounds(0, 0, 500, 500);
init_params.bounds = initial_bounds;
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.native_widget = new DesktopNativeWidgetAura(&top_level_widget);
top_level_widget.Init(init_params);
top_level_widget.Show();
aura::Window* top_level_window = top_level_widget.GetNativeWindow();
EXPECT_EQ(top_level_window, aura::client::GetFocusClient(
top_level_window)->GetFocusedWindow());
// Create a modal dialog.
// This instance will be destroyed when the dialog is destroyed.
ModalDialogDelegate* dialog_delegate = new ModalDialogDelegate;
Widget* modal_dialog_widget = views::DialogDelegate::CreateDialogWidget(
dialog_delegate, NULL, top_level_widget.GetNativeWindow());
modal_dialog_widget->SetBounds(gfx::Rect(100, 100, 200, 200));
modal_dialog_widget->Show();
aura::Window* dialog_window = modal_dialog_widget->GetNativeWindow();
EXPECT_EQ(dialog_window, aura::client::GetFocusClient(
top_level_window)->GetFocusedWindow());
modal_dialog_widget->CloseNow();
EXPECT_EQ(top_level_window, aura::client::GetFocusClient(
top_level_window)->GetFocusedWindow());
top_level_widget.CloseNow();
}
#endif
namespace {
// Used to veirfy OnMouseCaptureLost() has been invoked.
class CaptureLostTrackingWidget : public Widget {
public:
CaptureLostTrackingWidget() : got_capture_lost_(false) {}
virtual ~CaptureLostTrackingWidget() {}
bool GetAndClearGotCaptureLost() {
bool value = got_capture_lost_;
got_capture_lost_ = false;
return value;
}
// Widget:
virtual void OnMouseCaptureLost() OVERRIDE {
got_capture_lost_ = true;
Widget::OnMouseCaptureLost();
}
private:
bool got_capture_lost_;
DISALLOW_COPY_AND_ASSIGN(CaptureLostTrackingWidget);
};
} // namespace
class WidgetCaptureTest : public ViewsTestBase {
public:
WidgetCaptureTest() {
}
virtual ~WidgetCaptureTest() {
}
// Verifies Widget::SetCapture() results in updating native capture along with
// invoking the right Widget function.
void TestCapture(bool use_desktop_native_widget) {
CaptureLostTrackingWidget widget1;
Widget::InitParams params1 =
CreateParams(views::Widget::InitParams::TYPE_WINDOW);
params1.native_widget = CreateNativeWidget(use_desktop_native_widget,
&widget1);
params1.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget1.Init(params1);
widget1.Show();
CaptureLostTrackingWidget widget2;
Widget::InitParams params2 =
CreateParams(views::Widget::InitParams::TYPE_WINDOW);
params2.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params2.native_widget = CreateNativeWidget(use_desktop_native_widget,
&widget2);
widget2.Init(params2);
widget2.Show();
// Set capture to widget2 and verity it gets it.
widget2.SetCapture(widget2.GetRootView());
EXPECT_FALSE(widget1.HasCapture());
EXPECT_TRUE(widget2.HasCapture());
EXPECT_FALSE(widget1.GetAndClearGotCaptureLost());
EXPECT_FALSE(widget2.GetAndClearGotCaptureLost());
// Set capture to widget1 and verify it gets it.
widget1.SetCapture(widget1.GetRootView());
EXPECT_TRUE(widget1.HasCapture());
EXPECT_FALSE(widget2.HasCapture());
EXPECT_FALSE(widget1.GetAndClearGotCaptureLost());
EXPECT_TRUE(widget2.GetAndClearGotCaptureLost());
// Release and verify no one has it.
widget1.ReleaseCapture();
EXPECT_FALSE(widget1.HasCapture());
EXPECT_FALSE(widget2.HasCapture());
EXPECT_TRUE(widget1.GetAndClearGotCaptureLost());
EXPECT_FALSE(widget2.GetAndClearGotCaptureLost());
}
private:
NativeWidget* CreateNativeWidget(bool create_desktop_native_widget,
Widget* widget) {
#if defined(USE_AURA) && !defined(OS_CHROMEOS)
if (create_desktop_native_widget)
return new DesktopNativeWidgetAura(widget);
#endif
return NULL;
}
DISALLOW_COPY_AND_ASSIGN(WidgetCaptureTest);
};
// See description in TestCapture().
TEST_F(WidgetCaptureTest, Capture) {
TestCapture(false);
}
#if defined(USE_AURA) && !defined(OS_LINUX)
// See description in TestCapture(). Creates DesktopNativeWidget.
TEST_F(WidgetCaptureTest, CaptureDesktopNativeWidget) {
TestCapture(true);
}
#endif
#if defined(USE_AURA) && !defined(OS_CHROMEOS)
namespace {
// Used to veirfy OnMouseEvent() has been invoked.
class MouseEventTrackingWidget : public Widget {
public:
MouseEventTrackingWidget() : got_mouse_event_(false) {}
virtual ~MouseEventTrackingWidget() {}
bool GetAndClearGotMouseEvent() {
bool value = got_mouse_event_;
got_mouse_event_ = false;
return value;
}
// Widget:
virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
got_mouse_event_ = true;
Widget::OnMouseEvent(event);
}
private:
bool got_mouse_event_;
DISALLOW_COPY_AND_ASSIGN(MouseEventTrackingWidget);
};
} // namespace
#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
// TODO(erg): linux_aura bringup: http://crbug.com/163931
#define MAYBE_MouseEventDispatchedToRightWindow \
DISABLED_MouseEventDispatchedToRightWindow
#else
#define MAYBE_MouseEventDispatchedToRightWindow \
MouseEventDispatchedToRightWindow
#endif
// Verifies if a mouse event is received on a widget that doesn't have capture
// it is correctly processed by the widget that doesn't have capture.
TEST_F(WidgetCaptureTest, MAYBE_MouseEventDispatchedToRightWindow) {
MouseEventTrackingWidget widget1;
Widget::InitParams params1 =
CreateParams(views::Widget::InitParams::TYPE_WINDOW);
params1.native_widget = new DesktopNativeWidgetAura(&widget1);
params1.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget1.Init(params1);
widget1.Show();
MouseEventTrackingWidget widget2;
Widget::InitParams params2 =
CreateParams(views::Widget::InitParams::TYPE_WINDOW);
params2.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params2.native_widget = new DesktopNativeWidgetAura(&widget2);
widget2.Init(params2);
widget2.Show();
// Set capture to widget2 and verity it gets it.
widget2.SetCapture(widget2.GetRootView());
EXPECT_FALSE(widget1.HasCapture());
EXPECT_TRUE(widget2.HasCapture());
widget1.GetAndClearGotMouseEvent();
widget2.GetAndClearGotMouseEvent();
// Send a mouse event to the RootWindow associated with |widget1|. Even though
// |widget2| has capture, |widget1| should still get the event.
ui::MouseEvent mouse_event(ui::ET_MOUSE_EXITED, gfx::Point(), gfx::Point(),
ui::EF_NONE);
widget1.GetNativeWindow()->GetDispatcher()->AsRootWindowHostDelegate()->
OnHostMouseEvent(&mouse_event);
EXPECT_TRUE(widget1.GetAndClearGotMouseEvent());
EXPECT_FALSE(widget2.GetAndClearGotMouseEvent());
}
#endif
} // namespace test
} // namespace views
|