1 // 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.
5 #include "ash/focus_cycler.h"
7 #include "ash/launcher/launcher.h"
8 #include "ash/root_window_controller.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/system/status_area_widget.h"
12 #include "ash/system/status_area_widget_delegate.h"
13 #include "ash/system/tray/system_tray.h"
14 #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"
25 using aura::test::CreateTestWindowWithId
;
27 using internal::FocusCycler
;
31 internal::StatusAreaWidgetDelegate
* GetStatusAreaWidgetDelegate(
32 views::Widget
* widget
) {
33 return static_cast<internal::StatusAreaWidgetDelegate
*>(
34 widget
->GetContentsView());
39 class FocusCyclerTest
: public AshTestBase
{
43 virtual void SetUp() OVERRIDE
{
46 focus_cycler_
.reset(new FocusCycler());
48 ASSERT_TRUE(Launcher::ForPrimaryDisplay());
51 virtual void TearDown() OVERRIDE
{
53 GetStatusAreaWidgetDelegate(tray_
->GetWidget())->
54 SetFocusCyclerForTesting(NULL
);
58 Launcher::ForPrimaryDisplay()->SetFocusCycler(NULL
);
60 focus_cycler_
.reset();
62 AshTestBase::TearDown();
66 // Creates the system tray, returning true on success.
70 aura::Window
* parent
= Shell::GetPrimaryRootWindowController()->
71 GetContainer(ash::internal::kShellWindowId_StatusContainer
);
73 internal::StatusAreaWidget
* widget
= new internal::StatusAreaWidget(parent
);
74 widget
->CreateTrayViews(NULL
);
76 tray_
.reset(widget
->system_tray());
77 if (!tray_
->GetWidget())
79 focus_cycler_
->AddWidget(tray()->GetWidget());
80 GetStatusAreaWidgetDelegate(tray_
->GetWidget())->SetFocusCyclerForTesting(
85 FocusCycler
* focus_cycler() { return focus_cycler_
.get(); }
87 SystemTray
* tray() { return tray_
.get(); }
89 views::Widget
* launcher_widget() {
90 return Launcher::ForPrimaryDisplay()->widget();
93 void InstallFocusCycleOnLauncher() {
95 Launcher
* launcher
= Launcher::ForPrimaryDisplay();
96 launcher
->SetFocusCycler(focus_cycler());
100 scoped_ptr
<FocusCycler
> focus_cycler_
;
101 scoped_ptr
<SystemTray
> tray_
;
103 DISALLOW_COPY_AND_ASSIGN(FocusCyclerTest
);
106 TEST_F(FocusCyclerTest
, CycleFocusBrowserOnly
) {
107 // Create a single test window.
108 scoped_ptr
<Window
> window0(CreateTestWindowWithId(0, NULL
));
109 wm::ActivateWindow(window0
.get());
110 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
113 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
114 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
117 TEST_F(FocusCyclerTest
, CycleFocusForward
) {
118 ASSERT_TRUE(CreateTray());
120 InstallFocusCycleOnLauncher();
122 // Create a single test window.
123 scoped_ptr
<Window
> window0(CreateTestWindowWithId(0, NULL
));
124 wm::ActivateWindow(window0
.get());
125 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
127 // Cycle focus to the status area
128 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
129 EXPECT_TRUE(tray()->GetWidget()->IsActive());
131 // Cycle focus to the launcher
132 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
133 EXPECT_TRUE(launcher_widget()->IsActive());
135 // Cycle focus to the browser
136 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
137 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
140 TEST_F(FocusCyclerTest
, CycleFocusBackward
) {
141 ASSERT_TRUE(CreateTray());
143 InstallFocusCycleOnLauncher();
145 // Create a single test window.
146 scoped_ptr
<Window
> window0(CreateTestWindowWithId(0, NULL
));
147 wm::ActivateWindow(window0
.get());
148 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
150 // Cycle focus to the launcher
151 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
152 EXPECT_TRUE(launcher_widget()->IsActive());
154 // Cycle focus to the status area
155 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
156 EXPECT_TRUE(tray()->GetWidget()->IsActive());
158 // Cycle focus to the browser
159 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
160 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
163 TEST_F(FocusCyclerTest
, CycleFocusForwardBackward
) {
164 ASSERT_TRUE(CreateTray());
166 InstallFocusCycleOnLauncher();
168 // Create a single test window.
169 scoped_ptr
<Window
> window0(CreateTestWindowWithId(0, NULL
));
170 wm::ActivateWindow(window0
.get());
171 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
173 // Cycle focus to the launcher
174 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
175 EXPECT_TRUE(launcher_widget()->IsActive());
177 // Cycle focus to the status area
178 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
179 EXPECT_TRUE(tray()->GetWidget()->IsActive());
181 // Cycle focus to the browser
182 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
183 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
185 // Cycle focus to the status area
186 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
187 EXPECT_TRUE(tray()->GetWidget()->IsActive());
189 // Cycle focus to the launcher
190 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
191 EXPECT_TRUE(launcher_widget()->IsActive());
193 // Cycle focus to the browser
194 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
195 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
198 TEST_F(FocusCyclerTest
, CycleFocusNoBrowser
) {
199 ASSERT_TRUE(CreateTray());
201 InstallFocusCycleOnLauncher();
203 // Add the launcher and focus it
204 focus_cycler()->FocusWidget(launcher_widget());
206 // Cycle focus to the status area
207 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
208 EXPECT_TRUE(tray()->GetWidget()->IsActive());
210 // Cycle focus to the launcher
211 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
212 EXPECT_TRUE(launcher_widget()->IsActive());
214 // Cycle focus to the status area
215 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
216 EXPECT_TRUE(tray()->GetWidget()->IsActive());
218 // Cycle focus to the launcher
219 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
220 EXPECT_TRUE(launcher_widget()->IsActive());
222 // Cycle focus to the status area
223 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
224 EXPECT_TRUE(tray()->GetWidget()->IsActive());
227 TEST_F(FocusCyclerTest
, Launcher_CycleFocusForward
) {
228 ASSERT_TRUE(CreateTray());
229 InstallFocusCycleOnLauncher();
230 launcher_widget()->Hide();
232 // Create a single test window.
233 scoped_ptr
<Window
> window0(CreateTestWindowWithId(0, NULL
));
234 wm::ActivateWindow(window0
.get());
235 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
237 // Cycle focus to the status area
238 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
239 EXPECT_TRUE(tray()->GetWidget()->IsActive());
241 // Cycle focus to the browser
242 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
243 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
246 TEST_F(FocusCyclerTest
, Launcher_CycleFocusBackwardInvisible
) {
247 ASSERT_TRUE(CreateTray());
248 InstallFocusCycleOnLauncher();
249 launcher_widget()->Hide();
251 // Create a single test window.
252 scoped_ptr
<Window
> window0(CreateTestWindowWithId(0, NULL
));
253 wm::ActivateWindow(window0
.get());
254 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
256 // Cycle focus to the status area
257 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
258 EXPECT_TRUE(tray()->GetWidget()->IsActive());
260 // Cycle focus to the browser
261 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
262 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));