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.
4
5 #include "ash/first_run/first_run_helper.h"
6
7 #include "ash/first_run/desktop_cleaner.h"
8 #include "ash/shell.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ui/aura/test/event_generator.h"
12 #include "ui/events/event_handler.h"
13 #include "ui/views/window/dialog_delegate.h"
14
15 namespace ash {
16 namespace test {
17
18 namespace {
19
20 class TestModalDialogDelegate : public views::DialogDelegateView {
21 public:
TestModalDialogDelegate()22 TestModalDialogDelegate() {}
~TestModalDialogDelegate()23 virtual ~TestModalDialogDelegate() {}
24
25 // Overridden from views::WidgetDelegate:
GetModalType() const26 virtual ui::ModalType GetModalType() const OVERRIDE {
27 return ui::MODAL_TYPE_SYSTEM;
28 }
29
30 private:
31 DISALLOW_COPY_AND_ASSIGN(TestModalDialogDelegate);
32 };
33
34 class CountingEventHandler : public ui::EventHandler {
35 public:
36 // Handler resets |*mouse_events_registered_| during construction and updates
37 // it after each registered event.
CountingEventHandler(int * mouse_events_registered)38 explicit CountingEventHandler(int* mouse_events_registered)
39 : mouse_events_registered_(mouse_events_registered) {
40 *mouse_events_registered = 0;
41 }
42
~CountingEventHandler()43 virtual ~CountingEventHandler() {}
44
45 private:
46 // ui::EventHandler overrides.
OnMouseEvent(ui::MouseEvent * event)47 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
48 ++*mouse_events_registered_;
49 }
50
51 int* mouse_events_registered_;
52
53 DISALLOW_COPY_AND_ASSIGN(CountingEventHandler);
54 };
55
56 } // namespace
57
58 class FirstRunHelperTest : public AshTestBase,
59 public FirstRunHelper::Observer {
60 public:
FirstRunHelperTest()61 FirstRunHelperTest() : cancelled_times_(0) {}
62
~FirstRunHelperTest()63 virtual ~FirstRunHelperTest() {}
64
SetUp()65 virtual void SetUp() OVERRIDE {
66 AshTestBase::SetUp();
67 CheckContainersAreVisible();
68 helper_.reset(ash::Shell::GetInstance()->CreateFirstRunHelper());
69 helper_->AddObserver(this);
70 helper_->GetOverlayWidget()->Show();
71 }
72
TearDown()73 virtual void TearDown() OVERRIDE {
74 EXPECT_TRUE(helper_.get());
75 helper_.reset();
76 CheckContainersAreVisible();
77 AshTestBase::TearDown();
78 }
79
CheckContainersAreVisible() const80 void CheckContainersAreVisible() const {
81 aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
82 std::vector<int> containers_to_check =
83 internal::DesktopCleaner::GetContainersToHideForTest();
84 for (size_t i = 0; i < containers_to_check.size(); ++i) {
85 aura::Window* container =
86 Shell::GetContainer(root_window, containers_to_check[i]);
87 EXPECT_TRUE(container->IsVisible());
88 }
89 }
90
CheckContainersAreHidden() const91 void CheckContainersAreHidden() const {
92 aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
93 std::vector<int> containers_to_check =
94 internal::DesktopCleaner::GetContainersToHideForTest();
95 for (size_t i = 0; i < containers_to_check.size(); ++i) {
96 aura::Window* container =
97 Shell::GetContainer(root_window, containers_to_check[i]);
98 EXPECT_TRUE(!container->IsVisible());
99 }
100 }
101
helper()102 FirstRunHelper* helper() { return helper_.get(); }
103
cancelled_times() const104 int cancelled_times() const { return cancelled_times_; }
105
106 private:
107 // FirstRunHelper::Observer overrides.
OnCancelled()108 virtual void OnCancelled() OVERRIDE {
109 ++cancelled_times_;
110 }
111
112 scoped_ptr<FirstRunHelper> helper_;
113 int cancelled_times_;
114
115 DISALLOW_COPY_AND_ASSIGN(FirstRunHelperTest);
116 };
117
118 // This test creates helper, checks that containers are hidden and then
119 // destructs helper.
TEST_F(FirstRunHelperTest,ContainersAreHidden)120 TEST_F(FirstRunHelperTest, ContainersAreHidden) {
121 CheckContainersAreHidden();
122 }
123
124 // Tests that helper correctly handles Escape key press.
TEST_F(FirstRunHelperTest,Cancel)125 TEST_F(FirstRunHelperTest, Cancel) {
126 GetEventGenerator().PressKey(ui::VKEY_ESCAPE, 0);
127 EXPECT_EQ(cancelled_times(), 1);
128 }
129
130 // Tests that modal window doesn't block events for overlay window.
TEST_F(FirstRunHelperTest,ModalWindowDoesNotBlock)131 TEST_F(FirstRunHelperTest, ModalWindowDoesNotBlock) {
132 views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
133 new TestModalDialogDelegate(), CurrentContext(), NULL);
134 modal_dialog->Show();
135 // TODO(dzhioev): modal window should not steal focus from overlay window.
136 aura::Window* overlay_window = helper()->GetOverlayWidget()->GetNativeView();
137 overlay_window->Focus();
138 EXPECT_TRUE(overlay_window->HasFocus());
139 int mouse_events;
140 overlay_window->SetEventFilter(new CountingEventHandler(&mouse_events));
141 GetEventGenerator().PressLeftButton();
142 GetEventGenerator().ReleaseLeftButton();
143 EXPECT_EQ(mouse_events, 2);
144 }
145
146 } // namespace test
147 } // namespace ash
148