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"
26 using internal::FocusCycler
;
30 internal::StatusAreaWidgetDelegate
* GetStatusAreaWidgetDelegate(
31 views::Widget
* widget
) {
32 return static_cast<internal::StatusAreaWidgetDelegate
*>(
33 widget
->GetContentsView());
38 class FocusCyclerTest
: public AshTestBase
{
42 virtual void SetUp() OVERRIDE
{
45 focus_cycler_
.reset(new FocusCycler());
47 ASSERT_TRUE(Launcher::ForPrimaryDisplay());
50 virtual void TearDown() OVERRIDE
{
52 GetStatusAreaWidgetDelegate(tray_
->GetWidget())->
53 SetFocusCyclerForTesting(NULL
);
57 Launcher::ForPrimaryDisplay()->SetFocusCycler(NULL
);
59 focus_cycler_
.reset();
61 AshTestBase::TearDown();
65 // Creates the system tray, returning true on success.
69 aura::Window
* parent
= Shell::GetPrimaryRootWindowController()->
70 GetContainer(ash::internal::kShellWindowId_StatusContainer
);
72 internal::StatusAreaWidget
* widget
= new internal::StatusAreaWidget(parent
);
73 widget
->CreateTrayViews();
75 tray_
.reset(widget
->system_tray());
76 if (!tray_
->GetWidget())
78 focus_cycler_
->AddWidget(tray()->GetWidget());
79 GetStatusAreaWidgetDelegate(tray_
->GetWidget())->SetFocusCyclerForTesting(
84 FocusCycler
* focus_cycler() { return focus_cycler_
.get(); }
86 SystemTray
* tray() { return tray_
.get(); }
88 views::Widget
* launcher_widget() {
89 return Launcher::ForPrimaryDisplay()->widget();
92 void InstallFocusCycleOnLauncher() {
94 Launcher
* launcher
= Launcher::ForPrimaryDisplay();
95 launcher
->SetFocusCycler(focus_cycler());
99 scoped_ptr
<FocusCycler
> focus_cycler_
;
100 scoped_ptr
<SystemTray
> tray_
;
102 DISALLOW_COPY_AND_ASSIGN(FocusCyclerTest
);
105 TEST_F(FocusCyclerTest
, CycleFocusBrowserOnly
) {
106 // Create a single test window.
107 scoped_ptr
<Window
> window0(CreateTestWindowInShellWithId(0));
108 wm::ActivateWindow(window0
.get());
109 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
112 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
113 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
116 TEST_F(FocusCyclerTest
, CycleFocusForward
) {
117 ASSERT_TRUE(CreateTray());
119 InstallFocusCycleOnLauncher();
121 // Create a single test window.
122 scoped_ptr
<Window
> window0(CreateTestWindowInShellWithId(0));
123 wm::ActivateWindow(window0
.get());
124 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
126 // Cycle focus to the status area
127 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
128 EXPECT_TRUE(tray()->GetWidget()->IsActive());
130 // Cycle focus to the launcher
131 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
132 EXPECT_TRUE(launcher_widget()->IsActive());
134 // Cycle focus to the browser
135 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
136 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
139 TEST_F(FocusCyclerTest
, CycleFocusBackward
) {
140 ASSERT_TRUE(CreateTray());
142 InstallFocusCycleOnLauncher();
144 // Create a single test window.
145 scoped_ptr
<Window
> window0(CreateTestWindowInShellWithId(0));
146 wm::ActivateWindow(window0
.get());
147 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
149 // Cycle focus to the launcher
150 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
151 EXPECT_TRUE(launcher_widget()->IsActive());
153 // Cycle focus to the status area
154 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
155 EXPECT_TRUE(tray()->GetWidget()->IsActive());
157 // Cycle focus to the browser
158 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
159 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
162 TEST_F(FocusCyclerTest
, CycleFocusForwardBackward
) {
163 ASSERT_TRUE(CreateTray());
165 InstallFocusCycleOnLauncher();
167 // Create a single test window.
168 scoped_ptr
<Window
> window0(CreateTestWindowInShellWithId(0));
169 wm::ActivateWindow(window0
.get());
170 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
172 // Cycle focus to the launcher
173 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
174 EXPECT_TRUE(launcher_widget()->IsActive());
176 // Cycle focus to the status area
177 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
178 EXPECT_TRUE(tray()->GetWidget()->IsActive());
180 // Cycle focus to the browser
181 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
182 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
184 // Cycle focus to the status area
185 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
186 EXPECT_TRUE(tray()->GetWidget()->IsActive());
188 // Cycle focus to the launcher
189 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
190 EXPECT_TRUE(launcher_widget()->IsActive());
192 // Cycle focus to the browser
193 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
194 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
197 TEST_F(FocusCyclerTest
, CycleFocusNoBrowser
) {
198 ASSERT_TRUE(CreateTray());
200 InstallFocusCycleOnLauncher();
202 // Add the launcher and focus it
203 focus_cycler()->FocusWidget(launcher_widget());
205 // Cycle focus to the status area
206 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
207 EXPECT_TRUE(tray()->GetWidget()->IsActive());
209 // Cycle focus to the launcher
210 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
211 EXPECT_TRUE(launcher_widget()->IsActive());
213 // Cycle focus to the status area
214 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
215 EXPECT_TRUE(tray()->GetWidget()->IsActive());
217 // Cycle focus to the launcher
218 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
219 EXPECT_TRUE(launcher_widget()->IsActive());
221 // Cycle focus to the status area
222 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
223 EXPECT_TRUE(tray()->GetWidget()->IsActive());
226 TEST_F(FocusCyclerTest
, Launcher_CycleFocusForward
) {
227 ASSERT_TRUE(CreateTray());
228 InstallFocusCycleOnLauncher();
229 launcher_widget()->Hide();
231 // Create a single test window.
232 scoped_ptr
<Window
> window0(CreateTestWindowInShellWithId(0));
233 wm::ActivateWindow(window0
.get());
234 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
236 // Cycle focus to the status area
237 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
238 EXPECT_TRUE(tray()->GetWidget()->IsActive());
240 // Cycle focus to the browser
241 focus_cycler()->RotateFocus(FocusCycler::FORWARD
);
242 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
245 TEST_F(FocusCyclerTest
, Launcher_CycleFocusBackwardInvisible
) {
246 ASSERT_TRUE(CreateTray());
247 InstallFocusCycleOnLauncher();
248 launcher_widget()->Hide();
250 // Create a single test window.
251 scoped_ptr
<Window
> window0(CreateTestWindowInShellWithId(0));
252 wm::ActivateWindow(window0
.get());
253 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));
255 // Cycle focus to the status area
256 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
257 EXPECT_TRUE(tray()->GetWidget()->IsActive());
259 // Cycle focus to the browser
260 focus_cycler()->RotateFocus(FocusCycler::BACKWARD
);
261 EXPECT_TRUE(wm::IsActiveWindow(window0
.get()));