blob: 9f3e429784dbe9c514817c3e6ff762d04cd095ee [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"
[email protected]860f5942012-04-24 16:11:3910#include "ash/system/tray/system_tray.h"
11#include "ash/system/tray/system_tray_widget_delegate.h"
[email protected]df1c9862012-02-27 10:37:3112#include "ash/wm/window_util.h"
13#include "ash/test/ash_test_base.h"
14#include "ash/shell_factory.h"
15#include "ui/aura/test/test_windows.h"
16#include "ui/aura/window.h"
17#include "ui/views/controls/button/menu_button.h"
18#include "ui/views/widget/widget.h"
19
20namespace ash {
21namespace test {
22
23using aura::test::CreateTestWindowWithId;
24using aura::Window;
25using internal::FocusCycler;
26
[email protected]8676f0472012-03-29 20:30:1227namespace {
28
29internal::StatusAreaView* GetStatusAreaView(views::Widget* widget) {
30 return static_cast<internal::StatusAreaView*>(
[email protected]82157fb82012-04-11 22:07:4931 widget->GetContentsView());
[email protected]8676f0472012-03-29 20:30:1232}
33
[email protected]860f5942012-04-24 16:11:3934SystemTray* CreateSystemTray() {
35 SystemTray* tray = new SystemTray;
36 tray->CreateWidget();
37 return tray;
38}
39
[email protected]8676f0472012-03-29 20:30:1240} // namespace
41
[email protected]df1c9862012-02-27 10:37:3142typedef AshTestBase FocusCyclerTest;
43
44TEST_F(FocusCyclerTest, CycleFocusBrowserOnly) {
45 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
46
47 // Create a single test window.
48 Window* default_container =
49 ash::Shell::GetInstance()->GetContainer(
50 internal::kShellWindowId_DefaultContainer);
51 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:5452 wm::ActivateWindow(window0.get());
53 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3154
55 // Cycle the window
56 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]f3c7b252012-02-27 12:17:5457 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3158}
59
60TEST_F(FocusCyclerTest, CycleFocusForward) {
61 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
62
63 // Add the Status area
[email protected]860f5942012-04-24 16:11:3964 scoped_ptr<SystemTray> tray(CreateSystemTray());
65 ASSERT_TRUE(tray->widget());
66 focus_cycler->AddWidget(tray->widget());
67 GetStatusAreaView(tray->widget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:1268 focus_cycler.get());
[email protected]df1c9862012-02-27 10:37:3169
[email protected]df1c9862012-02-27 10:37:3170 // Add the launcher
71 Launcher* launcher = Shell::GetInstance()->launcher();
72 ASSERT_TRUE(launcher);
73 views::Widget* launcher_widget = launcher->widget();
74 ASSERT_TRUE(launcher_widget);
[email protected]df1c9862012-02-27 10:37:3175 launcher->SetFocusCycler(focus_cycler.get());
76
77 // Create a single test window.
78 Window* default_container =
79 ash::Shell::GetInstance()->GetContainer(
80 internal::kShellWindowId_DefaultContainer);
81 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:5482 wm::ActivateWindow(window0.get());
83 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3184
85 // Cycle focus to the status area
86 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]860f5942012-04-24 16:11:3987 EXPECT_TRUE(tray->widget()->IsActive());
[email protected]df1c9862012-02-27 10:37:3188
89 // Cycle focus to the launcher
90 focus_cycler->RotateFocus(FocusCycler::FORWARD);
91 EXPECT_TRUE(launcher_widget->IsActive());
92
93 // Cycle focus to the browser
94 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]f3c7b252012-02-27 12:17:5495 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3196}
97
98TEST_F(FocusCyclerTest, CycleFocusBackward) {
99 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
100
101 // Add the Status area
[email protected]860f5942012-04-24 16:11:39102 scoped_ptr<SystemTray> tray(CreateSystemTray());
103 ASSERT_TRUE(tray->widget());
104 focus_cycler->AddWidget(tray->widget());
105 GetStatusAreaView(tray->widget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:12106 focus_cycler.get());
[email protected]df1c9862012-02-27 10:37:31107
[email protected]df1c9862012-02-27 10:37:31108 // Add the launcher
109 Launcher* launcher = Shell::GetInstance()->launcher();
110 ASSERT_TRUE(launcher);
111 views::Widget* launcher_widget = launcher->widget();
112 ASSERT_TRUE(launcher_widget);
[email protected]df1c9862012-02-27 10:37:31113 launcher->SetFocusCycler(focus_cycler.get());
114
115 // Create a single test window.
116 Window* default_container =
117 ash::Shell::GetInstance()->GetContainer(
118 internal::kShellWindowId_DefaultContainer);
119 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:54120 wm::ActivateWindow(window0.get());
121 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31122
123 // Cycle focus to the launcher
124 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
125 EXPECT_TRUE(launcher_widget->IsActive());
126
127 // Cycle focus to the status area
128 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
[email protected]860f5942012-04-24 16:11:39129 EXPECT_TRUE(tray->widget()->IsActive());
[email protected]df1c9862012-02-27 10:37:31130
131 // Cycle focus to the browser
132 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
[email protected]f3c7b252012-02-27 12:17:54133 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31134}
135
[email protected]057dc5752012-03-06 23:59:31136class FocusCyclerLauncherTest : public AshTestBase {
137 public:
138 FocusCyclerLauncherTest() : AshTestBase() {}
139 virtual ~FocusCyclerLauncherTest() {}
140
141 virtual void SetUp() OVERRIDE {
142 AshTestBase::SetUp();
143
144 // Hide the launcher
145 Launcher* launcher = Shell::GetInstance()->launcher();
146 ASSERT_TRUE(launcher);
147 views::Widget* launcher_widget = launcher->widget();
148 ASSERT_TRUE(launcher_widget);
149 launcher_widget->Hide();
150 }
151
152 virtual void TearDown() OVERRIDE {
153 // Show the launcher
154 Launcher* launcher = Shell::GetInstance()->launcher();
155 ASSERT_TRUE(launcher);
156 views::Widget* launcher_widget = launcher->widget();
157 ASSERT_TRUE(launcher_widget);
158 launcher_widget->Show();
159
160 AshTestBase::TearDown();
161 }
162
163 private:
164 DISALLOW_COPY_AND_ASSIGN(FocusCyclerLauncherTest);
165};
166
167TEST_F(FocusCyclerLauncherTest, CycleFocusForwardInvisible) {
168 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
169
170 // Add the Status area
[email protected]860f5942012-04-24 16:11:39171 scoped_ptr<SystemTray> tray(CreateSystemTray());
172 ASSERT_TRUE(tray->widget());
173 focus_cycler->AddWidget(tray->widget());
174 GetStatusAreaView(tray->widget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:12175 focus_cycler.get());
[email protected]057dc5752012-03-06 23:59:31176
[email protected]057dc5752012-03-06 23:59:31177 // Add the launcher
178 Launcher* launcher = Shell::GetInstance()->launcher();
179 ASSERT_TRUE(launcher);
180 views::Widget* launcher_widget = launcher->widget();
181 ASSERT_TRUE(launcher_widget);
[email protected]057dc5752012-03-06 23:59:31182 launcher->SetFocusCycler(focus_cycler.get());
183
184 // Create a single test window.
185 Window* default_container =
186 ash::Shell::GetInstance()->GetContainer(
187 internal::kShellWindowId_DefaultContainer);
188 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
189 wm::ActivateWindow(window0.get());
190 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
191
192 // Cycle focus to the status area
193 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]860f5942012-04-24 16:11:39194 EXPECT_TRUE(tray->widget()->IsActive());
[email protected]057dc5752012-03-06 23:59:31195
196 // Cycle focus to the browser
197 focus_cycler->RotateFocus(FocusCycler::FORWARD);
198 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
199}
200
201TEST_F(FocusCyclerLauncherTest, CycleFocusBackwardInvisible) {
202 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
203
204 // Add the Status area
[email protected]860f5942012-04-24 16:11:39205 scoped_ptr<SystemTray> tray(CreateSystemTray());
206 ASSERT_TRUE(tray->widget());
207 focus_cycler->AddWidget(tray->widget());
208 GetStatusAreaView(tray->widget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:12209 focus_cycler.get());
[email protected]057dc5752012-03-06 23:59:31210
[email protected]057dc5752012-03-06 23:59:31211 // Add the launcher
212 Launcher* launcher = Shell::GetInstance()->launcher();
213 ASSERT_TRUE(launcher);
214 views::Widget* launcher_widget = launcher->widget();
215 ASSERT_TRUE(launcher_widget);
[email protected]057dc5752012-03-06 23:59:31216 launcher->SetFocusCycler(focus_cycler.get());
217
218 // Create a single test window.
219 Window* default_container =
220 ash::Shell::GetInstance()->GetContainer(
221 internal::kShellWindowId_DefaultContainer);
222 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
223 wm::ActivateWindow(window0.get());
224 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
225
226 // Cycle focus to the status area
227 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
[email protected]860f5942012-04-24 16:11:39228 EXPECT_TRUE(tray->widget()->IsActive());
[email protected]057dc5752012-03-06 23:59:31229
230 // Cycle focus to the browser
231 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
232 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
233}
234
[email protected]df1c9862012-02-27 10:37:31235} // namespace test
236} // namespace ash