1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/first_run/first_run_helper.h"
7 #include "ash/first_run/desktop_cleaner.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ui/events/event_handler.h"
12 #include "ui/events/test/event_generator.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/dialog_delegate.h"
21 class TestModalDialogDelegate
: public views::DialogDelegateView
{
23 TestModalDialogDelegate() {}
24 virtual ~TestModalDialogDelegate() {}
26 // Overridden from views::WidgetDelegate:
27 virtual ui::ModalType
GetModalType() const OVERRIDE
{
28 return ui::MODAL_TYPE_SYSTEM
;
32 DISALLOW_COPY_AND_ASSIGN(TestModalDialogDelegate
);
35 class CountingEventHandler
: public ui::EventHandler
{
37 // Handler resets |*mouse_events_registered_| during construction and updates
38 // it after each registered event.
39 explicit CountingEventHandler(int* mouse_events_registered
)
40 : mouse_events_registered_(mouse_events_registered
) {
41 *mouse_events_registered
= 0;
44 virtual ~CountingEventHandler() {}
47 // ui::EventHandler overrides.
48 virtual void OnMouseEvent(ui::MouseEvent
* event
) OVERRIDE
{
49 ++*mouse_events_registered_
;
52 int* mouse_events_registered_
;
54 DISALLOW_COPY_AND_ASSIGN(CountingEventHandler
);
59 class FirstRunHelperTest
: public AshTestBase
,
60 public FirstRunHelper::Observer
{
62 FirstRunHelperTest() : cancelled_times_(0) {}
64 virtual ~FirstRunHelperTest() {}
66 virtual void SetUp() OVERRIDE
{
68 CheckContainersAreVisible();
69 helper_
.reset(ash::Shell::GetInstance()->CreateFirstRunHelper());
70 helper_
->AddObserver(this);
71 helper_
->GetOverlayWidget()->Show();
74 virtual void TearDown() OVERRIDE
{
75 EXPECT_TRUE(helper_
.get());
77 CheckContainersAreVisible();
78 AshTestBase::TearDown();
81 void CheckContainersAreVisible() const {
82 aura::Window
* root_window
= Shell::GetInstance()->GetPrimaryRootWindow();
83 std::vector
<int> containers_to_check
=
84 DesktopCleaner::GetContainersToHideForTest();
85 for (size_t i
= 0; i
< containers_to_check
.size(); ++i
) {
86 aura::Window
* container
=
87 Shell::GetContainer(root_window
, containers_to_check
[i
]);
88 EXPECT_TRUE(container
->IsVisible());
92 void CheckContainersAreHidden() const {
93 aura::Window
* root_window
= Shell::GetInstance()->GetPrimaryRootWindow();
94 std::vector
<int> containers_to_check
=
95 DesktopCleaner::GetContainersToHideForTest();
96 for (size_t i
= 0; i
< containers_to_check
.size(); ++i
) {
97 aura::Window
* container
=
98 Shell::GetContainer(root_window
, containers_to_check
[i
]);
99 EXPECT_TRUE(!container
->IsVisible());
103 FirstRunHelper
* helper() { return helper_
.get(); }
105 int cancelled_times() const { return cancelled_times_
; }
108 // FirstRunHelper::Observer overrides.
109 virtual void OnCancelled() OVERRIDE
{
113 scoped_ptr
<FirstRunHelper
> helper_
;
114 int cancelled_times_
;
116 DISALLOW_COPY_AND_ASSIGN(FirstRunHelperTest
);
119 // This test creates helper, checks that containers are hidden and then
121 TEST_F(FirstRunHelperTest
, ContainersAreHidden
) {
122 CheckContainersAreHidden();
125 // Tests that helper correctly handles Escape key press.
126 TEST_F(FirstRunHelperTest
, Cancel
) {
127 GetEventGenerator().PressKey(ui::VKEY_ESCAPE
, 0);
128 EXPECT_EQ(cancelled_times(), 1);
131 // Tests that modal window doesn't block events for overlay window.
132 TEST_F(FirstRunHelperTest
, ModalWindowDoesNotBlock
) {
133 views::Widget
* modal_dialog
= views::DialogDelegate::CreateDialogWidget(
134 new TestModalDialogDelegate(), CurrentContext(), NULL
);
135 modal_dialog
->Show();
136 // TODO(dzhioev): modal window should not steal focus from overlay window.
137 aura::Window
* overlay_window
= helper()->GetOverlayWidget()->GetNativeView();
138 overlay_window
->Focus();
139 EXPECT_TRUE(overlay_window
->HasFocus());
141 CountingEventHandler
handler(&mouse_events
);
142 overlay_window
->AddPreTargetHandler(&handler
);
143 GetEventGenerator().PressLeftButton();
144 GetEventGenerator().ReleaseLeftButton();
145 EXPECT_EQ(mouse_events
, 2);
146 overlay_window
->RemovePreTargetHandler(&handler
);