blob: 2ba405aff05a5022e1e53ebca2dc3c06ddcf8e01 [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"
[email protected]16059276d2012-10-22 18:59:508#include "ash/root_window_controller.h"
[email protected]df1c9862012-02-27 10:37:319#include "ash/shell.h"
10#include "ash/shell_window_ids.h"
[email protected]6d6546e2012-05-30 23:12:0211#include "ash/system/status_area_widget.h"
12#include "ash/system/status_area_widget_delegate.h"
[email protected]860f5942012-04-24 16:11:3913#include "ash/system/tray/system_tray.h"
[email protected]df1c9862012-02-27 10:37:3114#include "ash/wm/window_util.h"
15#include "ash/test/ash_test_base.h"
16#include "ash/shell_factory.h"
17#include "ui/aura/test/test_windows.h"
18#include "ui/aura/window.h"
19#include "ui/views/controls/button/menu_button.h"
20#include "ui/views/widget/widget.h"
21
22namespace ash {
23namespace test {
24
25using aura::test::CreateTestWindowWithId;
26using aura::Window;
27using internal::FocusCycler;
28
[email protected]8676f0472012-03-29 20:30:1229namespace {
30
[email protected]6d6546e2012-05-30 23:12:0231internal::StatusAreaWidgetDelegate* GetStatusAreaWidgetDelegate(
32 views::Widget* widget) {
33 return static_cast<internal::StatusAreaWidgetDelegate*>(
[email protected]82157fb82012-04-11 22:07:4934 widget->GetContentsView());
[email protected]8676f0472012-03-29 20:30:1235}
36
37} // namespace
38
[email protected]eb5a0ab2012-10-20 00:50:2539class FocusCyclerTest : public AshTestBase {
40 public:
41 FocusCyclerTest() {}
[email protected]df1c9862012-02-27 10:37:3142
[email protected]eb5a0ab2012-10-20 00:50:2543 virtual void SetUp() OVERRIDE {
44 AshTestBase::SetUp();
[email protected]df1c9862012-02-27 10:37:3145
[email protected]eb5a0ab2012-10-20 00:50:2546 focus_cycler_.reset(new FocusCycler());
47
48 ASSERT_TRUE(Launcher::ForPrimaryDisplay());
49 }
50
51 virtual void TearDown() OVERRIDE {
52 if (tray_.get()) {
53 GetStatusAreaWidgetDelegate(tray_->GetWidget())->
54 SetFocusCyclerForTesting(NULL);
55 tray_.reset();
56 }
57
58 Launcher::ForPrimaryDisplay()->SetFocusCycler(NULL);
59
60 focus_cycler_.reset();
61
62 AshTestBase::TearDown();
63 }
64
65 protected:
66 // Creates the system tray, returning true on success.
67 bool CreateTray() {
68 if (tray_.get())
69 return false;
[email protected]16059276d2012-10-22 18:59:5070 aura::Window* parent = Shell::GetPrimaryRootWindowController()->
71 GetContainer(ash::internal::kShellWindowId_StatusContainer);
72
73 internal::StatusAreaWidget* widget = new internal::StatusAreaWidget(parent);
[email protected]eb5a0ab2012-10-20 00:50:2574 widget->CreateTrayViews(NULL);
75 widget->Show();
76 tray_.reset(widget->system_tray());
77 if (!tray_->GetWidget())
78 return false;
79 focus_cycler_->AddWidget(tray()->GetWidget());
80 GetStatusAreaWidgetDelegate(tray_->GetWidget())->SetFocusCyclerForTesting(
81 focus_cycler());
82 return true;
83 }
84
85 FocusCycler* focus_cycler() { return focus_cycler_.get(); }
86
87 SystemTray* tray() { return tray_.get(); }
88
89 views::Widget* launcher_widget() {
90 return Launcher::ForPrimaryDisplay()->widget();
91 }
92
93 void InstallFocusCycleOnLauncher() {
94 // Add the launcher
95 Launcher* launcher = Launcher::ForPrimaryDisplay();
96 launcher->SetFocusCycler(focus_cycler());
97 }
98
99 private:
100 scoped_ptr<FocusCycler> focus_cycler_;
101 scoped_ptr<SystemTray> tray_;
102
103 DISALLOW_COPY_AND_ASSIGN(FocusCyclerTest);
104};
105
106TEST_F(FocusCyclerTest, CycleFocusBrowserOnly) {
[email protected]df1c9862012-02-27 10:37:31107 // Create a single test window.
[email protected]932cf112012-08-16 04:28:37108 scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
[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 the window
[email protected]eb5a0ab2012-10-20 00:50:25113 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
[email protected]f3c7b252012-02-27 12:17:54114 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31115}
116
[email protected]eb5a0ab2012-10-20 00:50:25117TEST_F(FocusCyclerTest, CycleFocusForward) {
118 ASSERT_TRUE(CreateTray());
[email protected]df1c9862012-02-27 10:37:31119
[email protected]eb5a0ab2012-10-20 00:50:25120 InstallFocusCycleOnLauncher();
[email protected]df1c9862012-02-27 10:37:31121
122 // Create a single test window.
[email protected]932cf112012-08-16 04:28:37123 scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
[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 status area
[email protected]eb5a0ab2012-10-20 00:50:25128 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
129 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]df1c9862012-02-27 10:37:31130
131 // Cycle focus to the launcher
[email protected]eb5a0ab2012-10-20 00:50:25132 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
133 EXPECT_TRUE(launcher_widget()->IsActive());
[email protected]df1c9862012-02-27 10:37:31134
135 // Cycle focus to the browser
[email protected]eb5a0ab2012-10-20 00:50:25136 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
[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]eb5a0ab2012-10-20 00:50:25140TEST_F(FocusCyclerTest, CycleFocusBackward) {
141 ASSERT_TRUE(CreateTray());
[email protected]df1c9862012-02-27 10:37:31142
[email protected]eb5a0ab2012-10-20 00:50:25143 InstallFocusCycleOnLauncher();
[email protected]df1c9862012-02-27 10:37:31144
145 // Create a single test window.
[email protected]932cf112012-08-16 04:28:37146 scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
[email protected]f3c7b252012-02-27 12:17:54147 wm::ActivateWindow(window0.get());
148 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31149
150 // Cycle focus to the launcher
[email protected]eb5a0ab2012-10-20 00:50:25151 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
152 EXPECT_TRUE(launcher_widget()->IsActive());
[email protected]df1c9862012-02-27 10:37:31153
154 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25155 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
156 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]df1c9862012-02-27 10:37:31157
158 // Cycle focus to the browser
[email protected]eb5a0ab2012-10-20 00:50:25159 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
[email protected]f3c7b252012-02-27 12:17:54160 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
[email protected]df1c9862012-02-27 10:37:31161}
162
[email protected]eb5a0ab2012-10-20 00:50:25163TEST_F(FocusCyclerTest, CycleFocusForwardBackward) {
164 ASSERT_TRUE(CreateTray());
[email protected]ce711ac2012-06-14 07:05:41165
[email protected]eb5a0ab2012-10-20 00:50:25166 InstallFocusCycleOnLauncher();
[email protected]ce711ac2012-06-14 07:05:41167
168 // Create a single test window.
[email protected]932cf112012-08-16 04:28:37169 scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
[email protected]ce711ac2012-06-14 07:05:41170 wm::ActivateWindow(window0.get());
171 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
172
173 // Cycle focus to the launcher
[email protected]eb5a0ab2012-10-20 00:50:25174 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
175 EXPECT_TRUE(launcher_widget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41176
177 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25178 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
179 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41180
181 // Cycle focus to the browser
[email protected]eb5a0ab2012-10-20 00:50:25182 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
[email protected]ce711ac2012-06-14 07:05:41183 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
184
185 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25186 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
187 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41188
189 // Cycle focus to the launcher
[email protected]eb5a0ab2012-10-20 00:50:25190 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
191 EXPECT_TRUE(launcher_widget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41192
193 // Cycle focus to the browser
[email protected]eb5a0ab2012-10-20 00:50:25194 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
[email protected]ce711ac2012-06-14 07:05:41195 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
196}
197
[email protected]eb5a0ab2012-10-20 00:50:25198TEST_F(FocusCyclerTest, CycleFocusNoBrowser) {
199 ASSERT_TRUE(CreateTray());
[email protected]ce711ac2012-06-14 07:05:41200
[email protected]eb5a0ab2012-10-20 00:50:25201 InstallFocusCycleOnLauncher();
[email protected]ce711ac2012-06-14 07:05:41202
203 // Add the launcher and focus it
[email protected]eb5a0ab2012-10-20 00:50:25204 focus_cycler()->FocusWidget(launcher_widget());
[email protected]ce711ac2012-06-14 07:05:41205
206 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25207 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
208 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41209
210 // Cycle focus to the launcher
[email protected]eb5a0ab2012-10-20 00:50:25211 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
212 EXPECT_TRUE(launcher_widget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41213
214 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25215 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
216 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41217
218 // Cycle focus to the launcher
[email protected]eb5a0ab2012-10-20 00:50:25219 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
220 EXPECT_TRUE(launcher_widget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41221
222 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25223 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
224 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]ce711ac2012-06-14 07:05:41225}
226
[email protected]eb5a0ab2012-10-20 00:50:25227TEST_F(FocusCyclerTest, Launcher_CycleFocusForward) {
228 ASSERT_TRUE(CreateTray());
229 InstallFocusCycleOnLauncher();
230 launcher_widget()->Hide();
[email protected]057dc5752012-03-06 23:59:31231
232 // Create a single test window.
[email protected]932cf112012-08-16 04:28:37233 scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
[email protected]057dc5752012-03-06 23:59:31234 wm::ActivateWindow(window0.get());
235 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
236
237 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25238 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
239 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]057dc5752012-03-06 23:59:31240
241 // Cycle focus to the browser
[email protected]eb5a0ab2012-10-20 00:50:25242 focus_cycler()->RotateFocus(FocusCycler::FORWARD);
[email protected]057dc5752012-03-06 23:59:31243 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
244}
245
[email protected]eb5a0ab2012-10-20 00:50:25246TEST_F(FocusCyclerTest, Launcher_CycleFocusBackwardInvisible) {
247 ASSERT_TRUE(CreateTray());
248 InstallFocusCycleOnLauncher();
249 launcher_widget()->Hide();
[email protected]057dc5752012-03-06 23:59:31250
251 // Create a single test window.
[email protected]932cf112012-08-16 04:28:37252 scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
[email protected]057dc5752012-03-06 23:59:31253 wm::ActivateWindow(window0.get());
254 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
255
256 // Cycle focus to the status area
[email protected]eb5a0ab2012-10-20 00:50:25257 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
258 EXPECT_TRUE(tray()->GetWidget()->IsActive());
[email protected]057dc5752012-03-06 23:59:31259
260 // Cycle focus to the browser
[email protected]eb5a0ab2012-10-20 00:50:25261 focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
[email protected]057dc5752012-03-06 23:59:31262 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
263}
264
[email protected]df1c9862012-02-27 10:37:31265} // namespace test
266} // namespace ash