blob: 39c53a18c02004fc1d95304dd36909cc9769d99a [file] [log] [blame]
[email protected]df1c9862012-02-27 10:37:311// Copyright (c) 2012 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/focus_cycler.h"
6
7#include "ash/launcher/launcher.h"
8#include "ash/shell.h"
9#include "ash/shell_window_ids.h"
10#include "ash/wm/window_util.h"
11#include "ash/test/ash_test_base.h"
12#include "ash/shell_factory.h"
13#include "ui/aura/test/test_windows.h"
14#include "ui/aura/window.h"
15#include "ui/views/controls/button/menu_button.h"
16#include "ui/views/widget/widget.h"
17
18namespace ash {
19namespace test {
20
21using aura::test::CreateTestWindowWithId;
22using aura::Window;
23using internal::FocusCycler;
24
25typedef AshTestBase FocusCyclerTest;
26
27TEST_F(FocusCyclerTest, CycleFocusBrowserOnly) {
28 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
29
30 // Create a single test window.
31 Window* default_container =
32 ash::Shell::GetInstance()->GetContainer(
33 internal::kShellWindowId_DefaultContainer);
34 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:5435 wm::ActivateWindow(window0.get());
36 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3137
38 // Cycle the window
39 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]f3c7b252012-02-27 12:17:5440 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3141}
42
43TEST_F(FocusCyclerTest, CycleFocusForward) {
44 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
45
46 // Add the Status area
[email protected]1dcddae2012-03-04 19:00:4047 views::Widget* status_widget = internal::CreateStatusArea(NULL);
[email protected]df1c9862012-02-27 10:37:3148 ASSERT_TRUE(status_widget);
49 focus_cycler->AddWidget(status_widget);
50
51 // Add a mock button to the status area.
52 status_widget->GetContentsView()->AddChildView(
53 new views::MenuButton(NULL, string16(), NULL, false));
54
55 // Add the launcher
56 Launcher* launcher = Shell::GetInstance()->launcher();
57 ASSERT_TRUE(launcher);
58 views::Widget* launcher_widget = launcher->widget();
59 ASSERT_TRUE(launcher_widget);
60 focus_cycler->AddWidget(launcher_widget);
61 launcher->SetFocusCycler(focus_cycler.get());
62
63 // Create a single test window.
64 Window* default_container =
65 ash::Shell::GetInstance()->GetContainer(
66 internal::kShellWindowId_DefaultContainer);
67 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:5468 wm::ActivateWindow(window0.get());
69 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3170
71 // Cycle focus to the status area
72 focus_cycler->RotateFocus(FocusCycler::FORWARD);
73 EXPECT_TRUE(status_widget->IsActive());
74
75 // Cycle focus to the launcher
76 focus_cycler->RotateFocus(FocusCycler::FORWARD);
77 EXPECT_TRUE(launcher_widget->IsActive());
78
79 // Cycle focus to the browser
80 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]f3c7b252012-02-27 12:17:5481 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3182}
83
84TEST_F(FocusCyclerTest, CycleFocusBackward) {
85 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
86
87 // Add the Status area
[email protected]1dcddae2012-03-04 19:00:4088 views::Widget* status_widget = internal::CreateStatusArea(NULL);
[email protected]df1c9862012-02-27 10:37:3189 ASSERT_TRUE(status_widget);
90 focus_cycler->AddWidget(status_widget);
91
92 // Add a mock button to the status area.
93 status_widget->GetContentsView()->AddChildView(
94 new views::MenuButton(NULL, string16(), NULL, false));
95
96 // Add the launcher
97 Launcher* launcher = Shell::GetInstance()->launcher();
98 ASSERT_TRUE(launcher);
99 views::Widget* launcher_widget = launcher->widget();
100 ASSERT_TRUE(launcher_widget);
101 focus_cycler->AddWidget(launcher_widget);
102 launcher->SetFocusCycler(focus_cycler.get());
103
104 // Create a single test window.
105 Window* default_container =
106 ash::Shell::GetInstance()->GetContainer(
107 internal::kShellWindowId_DefaultContainer);
108 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:54109 wm::ActivateWindow(window0.get());
110 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31111
112 // Cycle focus to the launcher
113 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
114 EXPECT_TRUE(launcher_widget->IsActive());
115
116 // Cycle focus to the status area
117 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
118 EXPECT_TRUE(status_widget->IsActive());
119
120 // Cycle focus to the browser
121 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
[email protected]f3c7b252012-02-27 12:17:54122 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31123}
124
[email protected]057dc5752012-03-06 23:59:31125class FocusCyclerLauncherTest : public AshTestBase {
126 public:
127 FocusCyclerLauncherTest() : AshTestBase() {}
128 virtual ~FocusCyclerLauncherTest() {}
129
130 virtual void SetUp() OVERRIDE {
131 AshTestBase::SetUp();
132
133 // Hide the launcher
134 Launcher* launcher = Shell::GetInstance()->launcher();
135 ASSERT_TRUE(launcher);
136 views::Widget* launcher_widget = launcher->widget();
137 ASSERT_TRUE(launcher_widget);
138 launcher_widget->Hide();
139 }
140
141 virtual void TearDown() OVERRIDE {
142 // Show the launcher
143 Launcher* launcher = Shell::GetInstance()->launcher();
144 ASSERT_TRUE(launcher);
145 views::Widget* launcher_widget = launcher->widget();
146 ASSERT_TRUE(launcher_widget);
147 launcher_widget->Show();
148
149 AshTestBase::TearDown();
150 }
151
152 private:
153 DISALLOW_COPY_AND_ASSIGN(FocusCyclerLauncherTest);
154};
155
156TEST_F(FocusCyclerLauncherTest, CycleFocusForwardInvisible) {
157 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
158
159 // Add the Status area
160 views::Widget* status_widget = internal::CreateStatusArea(NULL);
161 ASSERT_TRUE(status_widget);
162 focus_cycler->AddWidget(status_widget);
163
164 // Add a mock button to the status area.
165 status_widget->GetContentsView()->AddChildView(
166 new views::MenuButton(NULL, string16(), NULL, false));
167
168 // Add the launcher
169 Launcher* launcher = Shell::GetInstance()->launcher();
170 ASSERT_TRUE(launcher);
171 views::Widget* launcher_widget = launcher->widget();
172 ASSERT_TRUE(launcher_widget);
173 focus_cycler->AddWidget(launcher_widget);
174 launcher->SetFocusCycler(focus_cycler.get());
175
176 // Create a single test window.
177 Window* default_container =
178 ash::Shell::GetInstance()->GetContainer(
179 internal::kShellWindowId_DefaultContainer);
180 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
181 wm::ActivateWindow(window0.get());
182 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
183
184 // Cycle focus to the status area
185 focus_cycler->RotateFocus(FocusCycler::FORWARD);
186 EXPECT_TRUE(status_widget->IsActive());
187
188 // Cycle focus to the browser
189 focus_cycler->RotateFocus(FocusCycler::FORWARD);
190 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
191}
192
193TEST_F(FocusCyclerLauncherTest, CycleFocusBackwardInvisible) {
194 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
195
196 // Add the Status area
197 views::Widget* status_widget = internal::CreateStatusArea(NULL);
198 ASSERT_TRUE(status_widget);
199 focus_cycler->AddWidget(status_widget);
200
201 // Add a mock button to the status area.
202 status_widget->GetContentsView()->AddChildView(
203 new views::MenuButton(NULL, string16(), NULL, false));
204
205 // Add the launcher
206 Launcher* launcher = Shell::GetInstance()->launcher();
207 ASSERT_TRUE(launcher);
208 views::Widget* launcher_widget = launcher->widget();
209 ASSERT_TRUE(launcher_widget);
210 focus_cycler->AddWidget(launcher_widget);
211 launcher->SetFocusCycler(focus_cycler.get());
212
213 // Create a single test window.
214 Window* default_container =
215 ash::Shell::GetInstance()->GetContainer(
216 internal::kShellWindowId_DefaultContainer);
217 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
218 wm::ActivateWindow(window0.get());
219 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
220
221 // Cycle focus to the status area
222 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
223 EXPECT_TRUE(status_widget->IsActive());
224
225 // Cycle focus to the browser
226 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
227 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
228}
229
[email protected]df1c9862012-02-27 10:37:31230} // namespace test
231} // namespace ash