focus_cycler_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "ash/focus_cycler.h"
6
7#include "ash/launcher/launcher.h"
8#include "ash/root_window_controller.h"
9#include "ash/shelf/shelf_widget.h"
10#include "ash/shell.h"
11#include "ash/shell_window_ids.h"
12#include "ash/system/status_area_widget.h"
13#include "ash/system/status_area_widget_delegate.h"
14#include "ash/system/tray/system_tray.h"
15#include "ash/wm/window_util.h"
16#include "ash/test/ash_test_base.h"
17#include "ash/shell_factory.h"
18#include "ui/aura/test/test_windows.h"
19#include "ui/aura/window.h"
20#include "ui/views/controls/button/menu_button.h"
21#include "ui/views/widget/widget.h"
22
23namespace ash {
24namespace test {
25
26using aura::Window;
27using internal::FocusCycler;
28
29namespace {
30
31internal::StatusAreaWidgetDelegate* GetStatusAreaWidgetDelegate(
32    views::Widget* widget) {
33  return static_cast<internal::StatusAreaWidgetDelegate*>(
34      widget->GetContentsView());
35}
36
37}  // namespace
38
39class FocusCyclerTest : public AshTestBase {
40 public:
41  FocusCyclerTest() {}
42
43  virtual void SetUp() OVERRIDE {
44    AshTestBase::SetUp();
45
46    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    shelf_widget()->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;
70    aura::Window* parent = Shell::GetPrimaryRootWindowController()->
71        GetContainer(ash::internal::kShellWindowId_StatusContainer);
72
73    internal::StatusAreaWidget* widget = new internal::StatusAreaWidget(parent);
74    widget->CreateTrayViews();
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  ShelfWidget* shelf_widget() {
90    return Launcher::ForPrimaryDisplay()->shelf_widget();
91  }
92
93  void InstallFocusCycleOnShelf() {
94    // Add the shelf.
95    shelf_widget()->SetFocusCycler(focus_cycler());
96  }
97
98 private:
99  scoped_ptr<FocusCycler> focus_cycler_;
100  scoped_ptr<SystemTray> tray_;
101
102  DISALLOW_COPY_AND_ASSIGN(FocusCyclerTest);
103};
104
105TEST_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()));
110
111  // Cycle the window
112  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
113  EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
114}
115
116TEST_F(FocusCyclerTest, CycleFocusForward) {
117  ASSERT_TRUE(CreateTray());
118
119  InstallFocusCycleOnShelf();
120
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()));
125
126  // Cycle focus to the status area.
127  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
128  EXPECT_TRUE(tray()->GetWidget()->IsActive());
129
130  // Cycle focus to the shelf.
131  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
132  EXPECT_TRUE(shelf_widget()->IsActive());
133
134  // Cycle focus to the browser.
135  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
136  EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
137}
138
139TEST_F(FocusCyclerTest, CycleFocusBackward) {
140  ASSERT_TRUE(CreateTray());
141
142  InstallFocusCycleOnShelf();
143
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()));
148
149  // Cycle focus to the shelf.
150  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
151  EXPECT_TRUE(shelf_widget()->IsActive());
152
153  // Cycle focus to the status area.
154  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
155  EXPECT_TRUE(tray()->GetWidget()->IsActive());
156
157  // Cycle focus to the browser.
158  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
159  EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
160}
161
162TEST_F(FocusCyclerTest, CycleFocusForwardBackward) {
163  ASSERT_TRUE(CreateTray());
164
165  InstallFocusCycleOnShelf();
166
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()));
171
172  // Cycle focus to the shelf.
173  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
174  EXPECT_TRUE(shelf_widget()->IsActive());
175
176  // Cycle focus to the status area.
177  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
178  EXPECT_TRUE(tray()->GetWidget()->IsActive());
179
180  // Cycle focus to the browser.
181  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
182  EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
183
184  // Cycle focus to the status area.
185  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
186  EXPECT_TRUE(tray()->GetWidget()->IsActive());
187
188  // Cycle focus to the shelf.
189  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
190  EXPECT_TRUE(shelf_widget()->IsActive());
191
192  // Cycle focus to the browser.
193  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
194  EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
195}
196
197TEST_F(FocusCyclerTest, CycleFocusNoBrowser) {
198  ASSERT_TRUE(CreateTray());
199
200  InstallFocusCycleOnShelf();
201
202  // Add the shelf and focus it.
203  focus_cycler()->FocusWidget(shelf_widget());
204
205  // Cycle focus to the status area.
206  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
207  EXPECT_TRUE(tray()->GetWidget()->IsActive());
208
209  // Cycle focus to the shelf.
210  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
211  EXPECT_TRUE(shelf_widget()->IsActive());
212
213  // Cycle focus to the status area.
214  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
215  EXPECT_TRUE(tray()->GetWidget()->IsActive());
216
217  // Cycle focus to the shelf.
218  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
219  EXPECT_TRUE(shelf_widget()->IsActive());
220
221  // Cycle focus to the status area.
222  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
223  EXPECT_TRUE(tray()->GetWidget()->IsActive());
224}
225
226TEST_F(FocusCyclerTest, Shelf_CycleFocusForward) {
227  ASSERT_TRUE(CreateTray());
228  InstallFocusCycleOnShelf();
229  shelf_widget()->Hide();
230
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()));
235
236  // Cycle focus to the status area.
237  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
238  EXPECT_TRUE(tray()->GetWidget()->IsActive());
239
240  // Cycle focus to the browser.
241  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
242  EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
243}
244
245TEST_F(FocusCyclerTest, Shelf_CycleFocusBackwardInvisible) {
246  ASSERT_TRUE(CreateTray());
247  InstallFocusCycleOnShelf();
248  shelf_widget()->Hide();
249
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()));
254
255  // Cycle focus to the status area.
256  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
257  EXPECT_TRUE(tray()->GetWidget()->IsActive());
258
259  // Cycle focus to the browser.
260  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
261  EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
262}
263
264}  // namespace test
265}  // namespace ash
266