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 ~TestModalDialogDelegate() override
{}
26 // Overridden from views::WidgetDelegate:
27 ui::ModalType
GetModalType() const override
{ return ui::MODAL_TYPE_SYSTEM
; }
30 DISALLOW_COPY_AND_ASSIGN(TestModalDialogDelegate
);
33 class CountingEventHandler
: public ui::EventHandler
{
35 // Handler resets |*mouse_events_registered_| during construction and updates
36 // it after each registered event.
37 explicit CountingEventHandler(int* mouse_events_registered
)
38 : mouse_events_registered_(mouse_events_registered
) {
39 *mouse_events_registered
= 0;
42 ~CountingEventHandler() override
{}
45 // ui::EventHandler overrides.
46 void OnMouseEvent(ui::MouseEvent
* event
) override
{
47 ++*mouse_events_registered_
;
50 int* mouse_events_registered_
;
52 DISALLOW_COPY_AND_ASSIGN(CountingEventHandler
);
57 class FirstRunHelperTest
: public AshTestBase
,
58 public FirstRunHelper::Observer
{
60 FirstRunHelperTest() : cancelled_times_(0) {}
62 ~FirstRunHelperTest() override
{}
64 void SetUp() override
{
66 CheckContainersAreVisible();
67 helper_
.reset(ash::Shell::GetInstance()->CreateFirstRunHelper());
68 helper_
->AddObserver(this);
69 helper_
->GetOverlayWidget()->Show();
72 void TearDown() override
{
73 EXPECT_TRUE(helper_
.get());
75 CheckContainersAreVisible();
76 AshTestBase::TearDown();
79 void CheckContainersAreVisible() const {
80 aura::Window
* root_window
= Shell::GetInstance()->GetPrimaryRootWindow();
81 std::vector
<int> containers_to_check
=
82 DesktopCleaner::GetContainersToHideForTest();
83 for (size_t i
= 0; i
< containers_to_check
.size(); ++i
) {
84 aura::Window
* container
=
85 Shell::GetContainer(root_window
, containers_to_check
[i
]);
86 EXPECT_TRUE(container
->IsVisible());
90 void CheckContainersAreHidden() const {
91 aura::Window
* root_window
= Shell::GetInstance()->GetPrimaryRootWindow();
92 std::vector
<int> containers_to_check
=
93 DesktopCleaner::GetContainersToHideForTest();
94 for (size_t i
= 0; i
< containers_to_check
.size(); ++i
) {
95 aura::Window
* container
=
96 Shell::GetContainer(root_window
, containers_to_check
[i
]);
97 EXPECT_TRUE(!container
->IsVisible());
101 FirstRunHelper
* helper() { return helper_
.get(); }
103 int cancelled_times() const { return cancelled_times_
; }
106 // FirstRunHelper::Observer overrides.
107 void OnCancelled() override
{ ++cancelled_times_
; }
109 scoped_ptr
<FirstRunHelper
> helper_
;
110 int cancelled_times_
;
112 DISALLOW_COPY_AND_ASSIGN(FirstRunHelperTest
);
115 // This test creates helper, checks that containers are hidden and then
117 TEST_F(FirstRunHelperTest
, ContainersAreHidden
) {
118 CheckContainersAreHidden();
121 // Tests that helper correctly handles Escape key press.
122 TEST_F(FirstRunHelperTest
, Cancel
) {
123 GetEventGenerator().PressKey(ui::VKEY_ESCAPE
, 0);
124 EXPECT_EQ(cancelled_times(), 1);
127 // Tests that modal window doesn't block events for overlay window.
128 TEST_F(FirstRunHelperTest
, ModalWindowDoesNotBlock
) {
129 views::Widget
* modal_dialog
= views::DialogDelegate::CreateDialogWidget(
130 new TestModalDialogDelegate(), CurrentContext(), NULL
);
131 modal_dialog
->Show();
132 // TODO(dzhioev): modal window should not steal focus from overlay window.
133 aura::Window
* overlay_window
= helper()->GetOverlayWidget()->GetNativeView();
134 overlay_window
->Focus();
135 EXPECT_TRUE(overlay_window
->HasFocus());
137 CountingEventHandler
handler(&mouse_events
);
138 overlay_window
->AddPreTargetHandler(&handler
);
139 GetEventGenerator().PressLeftButton();
140 GetEventGenerator().ReleaseLeftButton();
141 EXPECT_EQ(mouse_events
, 2);
142 overlay_window
->RemovePreTargetHandler(&handler
);