blob: a6669285bad7a724ee5b8f8dfb3a8a2b6c67ff21 [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]6d6546e2012-05-30 23:12:0210#include "ash/system/status_area_widget.h"
11#include "ash/system/status_area_widget_delegate.h"
[email protected]860f5942012-04-24 16:11:3912#include "ash/system/tray/system_tray.h"
[email protected]df1c9862012-02-27 10:37:3113#include "ash/wm/window_util.h"
14#include "ash/test/ash_test_base.h"
15#include "ash/shell_factory.h"
16#include "ui/aura/test/test_windows.h"
17#include "ui/aura/window.h"
18#include "ui/views/controls/button/menu_button.h"
19#include "ui/views/widget/widget.h"
20
21namespace ash {
22namespace test {
23
24using aura::test::CreateTestWindowWithId;
25using aura::Window;
26using internal::FocusCycler;
27
[email protected]8676f0472012-03-29 20:30:1228namespace {
29
[email protected]6d6546e2012-05-30 23:12:0230internal::StatusAreaWidgetDelegate* GetStatusAreaWidgetDelegate(
31 views::Widget* widget) {
32 return static_cast<internal::StatusAreaWidgetDelegate*>(
[email protected]82157fb82012-04-11 22:07:4933 widget->GetContentsView());
[email protected]8676f0472012-03-29 20:30:1234}
35
[email protected]860f5942012-04-24 16:11:3936SystemTray* CreateSystemTray() {
37 SystemTray* tray = new SystemTray;
[email protected]6d6546e2012-05-30 23:12:0238 internal::StatusAreaWidget* widget = new internal::StatusAreaWidget;
39 widget->AddTray(tray);
40 widget->Show();
[email protected]860f5942012-04-24 16:11:3941 return tray;
42}
43
[email protected]8676f0472012-03-29 20:30:1244} // namespace
45
[email protected]df1c9862012-02-27 10:37:3146typedef AshTestBase FocusCyclerTest;
47
48TEST_F(FocusCyclerTest, CycleFocusBrowserOnly) {
49 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
50
51 // Create a single test window.
52 Window* default_container =
53 ash::Shell::GetInstance()->GetContainer(
54 internal::kShellWindowId_DefaultContainer);
55 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:5456 wm::ActivateWindow(window0.get());
57 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3158
59 // Cycle the window
60 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]f3c7b252012-02-27 12:17:5461 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3162}
63
64TEST_F(FocusCyclerTest, CycleFocusForward) {
65 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
66
67 // Add the Status area
[email protected]860f5942012-04-24 16:11:3968 scoped_ptr<SystemTray> tray(CreateSystemTray());
[email protected]6d6546e2012-05-30 23:12:0269 ASSERT_TRUE(tray->GetWidget());
70 focus_cycler->AddWidget(tray->GetWidget());
71 GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:1272 focus_cycler.get());
[email protected]df1c9862012-02-27 10:37:3173
[email protected]df1c9862012-02-27 10:37:3174 // Add the launcher
75 Launcher* launcher = Shell::GetInstance()->launcher();
76 ASSERT_TRUE(launcher);
77 views::Widget* launcher_widget = launcher->widget();
78 ASSERT_TRUE(launcher_widget);
[email protected]df1c9862012-02-27 10:37:3179 launcher->SetFocusCycler(focus_cycler.get());
80
81 // Create a single test window.
82 Window* default_container =
83 ash::Shell::GetInstance()->GetContainer(
84 internal::kShellWindowId_DefaultContainer);
85 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:5486 wm::ActivateWindow(window0.get());
87 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:3188
89 // Cycle focus to the status area
90 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]6d6546e2012-05-30 23:12:0291 EXPECT_TRUE(tray->GetWidget()->IsActive());
[email protected]df1c9862012-02-27 10:37:3192
93 // Cycle focus to the launcher
94 focus_cycler->RotateFocus(FocusCycler::FORWARD);
95 EXPECT_TRUE(launcher_widget->IsActive());
96
97 // Cycle focus to the browser
98 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]f3c7b252012-02-27 12:17:5499 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31100}
101
102TEST_F(FocusCyclerTest, CycleFocusBackward) {
103 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
104
105 // Add the Status area
[email protected]860f5942012-04-24 16:11:39106 scoped_ptr<SystemTray> tray(CreateSystemTray());
[email protected]6d6546e2012-05-30 23:12:02107 ASSERT_TRUE(tray->GetWidget());
108 focus_cycler->AddWidget(tray->GetWidget());
109 GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:12110 focus_cycler.get());
[email protected]df1c9862012-02-27 10:37:31111
[email protected]df1c9862012-02-27 10:37:31112 // Add the launcher
113 Launcher* launcher = Shell::GetInstance()->launcher();
114 ASSERT_TRUE(launcher);
115 views::Widget* launcher_widget = launcher->widget();
116 ASSERT_TRUE(launcher_widget);
[email protected]df1c9862012-02-27 10:37:31117 launcher->SetFocusCycler(focus_cycler.get());
118
119 // Create a single test window.
120 Window* default_container =
121 ash::Shell::GetInstance()->GetContainer(
122 internal::kShellWindowId_DefaultContainer);
123 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
[email protected]f3c7b252012-02-27 12:17:54124 wm::ActivateWindow(window0.get());
125 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31126
127 // Cycle focus to the launcher
128 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
129 EXPECT_TRUE(launcher_widget->IsActive());
130
131 // Cycle focus to the status area
132 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
[email protected]6d6546e2012-05-30 23:12:02133 EXPECT_TRUE(tray->GetWidget()->IsActive());
[email protected]df1c9862012-02-27 10:37:31134
135 // Cycle focus to the browser
136 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
[email protected]f3c7b252012-02-27 12:17:54137 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31138}
139
[email protected]057dc5752012-03-06 23:59:31140class FocusCyclerLauncherTest : public AshTestBase {
141 public:
142 FocusCyclerLauncherTest() : AshTestBase() {}
143 virtual ~FocusCyclerLauncherTest() {}
144
145 virtual void SetUp() OVERRIDE {
146 AshTestBase::SetUp();
147
148 // Hide the launcher
149 Launcher* launcher = Shell::GetInstance()->launcher();
150 ASSERT_TRUE(launcher);
151 views::Widget* launcher_widget = launcher->widget();
152 ASSERT_TRUE(launcher_widget);
153 launcher_widget->Hide();
154 }
155
156 virtual void TearDown() OVERRIDE {
157 // Show the launcher
158 Launcher* launcher = Shell::GetInstance()->launcher();
159 ASSERT_TRUE(launcher);
160 views::Widget* launcher_widget = launcher->widget();
161 ASSERT_TRUE(launcher_widget);
162 launcher_widget->Show();
163
164 AshTestBase::TearDown();
165 }
166
167 private:
168 DISALLOW_COPY_AND_ASSIGN(FocusCyclerLauncherTest);
169};
170
171TEST_F(FocusCyclerLauncherTest, CycleFocusForwardInvisible) {
172 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
173
174 // Add the Status area
[email protected]860f5942012-04-24 16:11:39175 scoped_ptr<SystemTray> tray(CreateSystemTray());
[email protected]6d6546e2012-05-30 23:12:02176 ASSERT_TRUE(tray->GetWidget());
177 focus_cycler->AddWidget(tray->GetWidget());
178 GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:12179 focus_cycler.get());
[email protected]057dc5752012-03-06 23:59:31180
[email protected]057dc5752012-03-06 23:59:31181 // Add the launcher
182 Launcher* launcher = Shell::GetInstance()->launcher();
183 ASSERT_TRUE(launcher);
184 views::Widget* launcher_widget = launcher->widget();
185 ASSERT_TRUE(launcher_widget);
[email protected]057dc5752012-03-06 23:59:31186 launcher->SetFocusCycler(focus_cycler.get());
187
188 // Create a single test window.
189 Window* default_container =
190 ash::Shell::GetInstance()->GetContainer(
191 internal::kShellWindowId_DefaultContainer);
192 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
193 wm::ActivateWindow(window0.get());
194 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
195
196 // Cycle focus to the status area
197 focus_cycler->RotateFocus(FocusCycler::FORWARD);
[email protected]6d6546e2012-05-30 23:12:02198 EXPECT_TRUE(tray->GetWidget()->IsActive());
[email protected]057dc5752012-03-06 23:59:31199
200 // Cycle focus to the browser
201 focus_cycler->RotateFocus(FocusCycler::FORWARD);
202 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
203}
204
205TEST_F(FocusCyclerLauncherTest, CycleFocusBackwardInvisible) {
206 scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
207
208 // Add the Status area
[email protected]860f5942012-04-24 16:11:39209 scoped_ptr<SystemTray> tray(CreateSystemTray());
[email protected]6d6546e2012-05-30 23:12:02210 ASSERT_TRUE(tray->GetWidget());
211 focus_cycler->AddWidget(tray->GetWidget());
212 GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
[email protected]8676f0472012-03-29 20:30:12213 focus_cycler.get());
[email protected]057dc5752012-03-06 23:59:31214
[email protected]057dc5752012-03-06 23:59:31215 // Add the launcher
216 Launcher* launcher = Shell::GetInstance()->launcher();
217 ASSERT_TRUE(launcher);
218 views::Widget* launcher_widget = launcher->widget();
219 ASSERT_TRUE(launcher_widget);
[email protected]057dc5752012-03-06 23:59:31220 launcher->SetFocusCycler(focus_cycler.get());
221
222 // Create a single test window.
223 Window* default_container =
224 ash::Shell::GetInstance()->GetContainer(
225 internal::kShellWindowId_DefaultContainer);
226 scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container));
227 wm::ActivateWindow(window0.get());
228 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
229
230 // Cycle focus to the status area
231 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
[email protected]6d6546e2012-05-30 23:12:02232 EXPECT_TRUE(tray->GetWidget()->IsActive());
[email protected]057dc5752012-03-06 23:59:31233
234 // Cycle focus to the browser
235 focus_cycler->RotateFocus(FocusCycler::BACKWARD);
236 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
237}
238
[email protected]df1c9862012-02-27 10:37:31239} // namespace test
240} // namespace ash