[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 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" |
[email protected] | 16059276d | 2012-10-22 18:59:50 | [diff] [blame^] | 8 | #include "ash/root_window_controller.h" |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 9 | #include "ash/shell.h" |
| 10 | #include "ash/shell_window_ids.h" |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame] | 11 | #include "ash/system/status_area_widget.h" |
| 12 | #include "ash/system/status_area_widget_delegate.h" |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 13 | #include "ash/system/tray/system_tray.h" |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 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" |
| 21 | |
| 22 | namespace ash { |
| 23 | namespace test { |
| 24 | |
| 25 | using aura::test::CreateTestWindowWithId; |
| 26 | using aura::Window; |
| 27 | using internal::FocusCycler; |
| 28 | |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 29 | namespace { |
| 30 | |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame] | 31 | internal::StatusAreaWidgetDelegate* GetStatusAreaWidgetDelegate( |
| 32 | views::Widget* widget) { |
| 33 | return static_cast<internal::StatusAreaWidgetDelegate*>( |
[email protected] | 82157fb8 | 2012-04-11 22:07:49 | [diff] [blame] | 34 | widget->GetContentsView()); |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 39 | class FocusCyclerTest : public AshTestBase { |
| 40 | public: |
| 41 | FocusCyclerTest() {} |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 42 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 43 | virtual void SetUp() OVERRIDE { |
| 44 | AshTestBase::SetUp(); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 45 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 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 | 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] | 16059276d | 2012-10-22 18:59:50 | [diff] [blame^] | 70 | aura::Window* parent = Shell::GetPrimaryRootWindowController()-> |
| 71 | GetContainer(ash::internal::kShellWindowId_StatusContainer); |
| 72 | |
| 73 | internal::StatusAreaWidget* widget = new internal::StatusAreaWidget(parent); |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 74 | 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 | |
| 106 | TEST_F(FocusCyclerTest, CycleFocusBrowserOnly) { |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 107 | // Create a single test window. |
[email protected] | 932cf11 | 2012-08-16 04:28:37 | [diff] [blame] | 108 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL)); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 109 | wm::ActivateWindow(window0.get()); |
| 110 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 111 | |
| 112 | // Cycle the window |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 113 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 114 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 117 | TEST_F(FocusCyclerTest, CycleFocusForward) { |
| 118 | ASSERT_TRUE(CreateTray()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 119 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 120 | InstallFocusCycleOnLauncher(); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 121 | |
| 122 | // Create a single test window. |
[email protected] | 932cf11 | 2012-08-16 04:28:37 | [diff] [blame] | 123 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL)); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 124 | wm::ActivateWindow(window0.get()); |
| 125 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 126 | |
| 127 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 128 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 129 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 130 | |
| 131 | // Cycle focus to the launcher |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 132 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 133 | EXPECT_TRUE(launcher_widget()->IsActive()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 134 | |
| 135 | // Cycle focus to the browser |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 136 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 137 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 140 | TEST_F(FocusCyclerTest, CycleFocusBackward) { |
| 141 | ASSERT_TRUE(CreateTray()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 142 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 143 | InstallFocusCycleOnLauncher(); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 144 | |
| 145 | // Create a single test window. |
[email protected] | 932cf11 | 2012-08-16 04:28:37 | [diff] [blame] | 146 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL)); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 147 | wm::ActivateWindow(window0.get()); |
| 148 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 149 | |
| 150 | // Cycle focus to the launcher |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 151 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
| 152 | EXPECT_TRUE(launcher_widget()->IsActive()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 153 | |
| 154 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 155 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
| 156 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 157 | |
| 158 | // Cycle focus to the browser |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 159 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 160 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 163 | TEST_F(FocusCyclerTest, CycleFocusForwardBackward) { |
| 164 | ASSERT_TRUE(CreateTray()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 165 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 166 | InstallFocusCycleOnLauncher(); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 167 | |
| 168 | // Create a single test window. |
[email protected] | 932cf11 | 2012-08-16 04:28:37 | [diff] [blame] | 169 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL)); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 170 | wm::ActivateWindow(window0.get()); |
| 171 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 172 | |
| 173 | // Cycle focus to the launcher |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 174 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
| 175 | EXPECT_TRUE(launcher_widget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 176 | |
| 177 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 178 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
| 179 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 180 | |
| 181 | // Cycle focus to the browser |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 182 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 183 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 184 | |
| 185 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 186 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 187 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 188 | |
| 189 | // Cycle focus to the launcher |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 190 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 191 | EXPECT_TRUE(launcher_widget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 192 | |
| 193 | // Cycle focus to the browser |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 194 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 195 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 196 | } |
| 197 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 198 | TEST_F(FocusCyclerTest, CycleFocusNoBrowser) { |
| 199 | ASSERT_TRUE(CreateTray()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 200 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 201 | InstallFocusCycleOnLauncher(); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 202 | |
| 203 | // Add the launcher and focus it |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 204 | focus_cycler()->FocusWidget(launcher_widget()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 205 | |
| 206 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 207 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 208 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 209 | |
| 210 | // Cycle focus to the launcher |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 211 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 212 | EXPECT_TRUE(launcher_widget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 213 | |
| 214 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 215 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 216 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 217 | |
| 218 | // Cycle focus to the launcher |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 219 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
| 220 | EXPECT_TRUE(launcher_widget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 221 | |
| 222 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 223 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
| 224 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | ce711ac | 2012-06-14 07:05:41 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 227 | TEST_F(FocusCyclerTest, Launcher_CycleFocusForward) { |
| 228 | ASSERT_TRUE(CreateTray()); |
| 229 | InstallFocusCycleOnLauncher(); |
| 230 | launcher_widget()->Hide(); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 231 | |
| 232 | // Create a single test window. |
[email protected] | 932cf11 | 2012-08-16 04:28:37 | [diff] [blame] | 233 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL)); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 234 | wm::ActivateWindow(window0.get()); |
| 235 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 236 | |
| 237 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 238 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
| 239 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 240 | |
| 241 | // Cycle focus to the browser |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 242 | focus_cycler()->RotateFocus(FocusCycler::FORWARD); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 243 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 244 | } |
| 245 | |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 246 | TEST_F(FocusCyclerTest, Launcher_CycleFocusBackwardInvisible) { |
| 247 | ASSERT_TRUE(CreateTray()); |
| 248 | InstallFocusCycleOnLauncher(); |
| 249 | launcher_widget()->Hide(); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 250 | |
| 251 | // Create a single test window. |
[email protected] | 932cf11 | 2012-08-16 04:28:37 | [diff] [blame] | 252 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL)); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 253 | wm::ActivateWindow(window0.get()); |
| 254 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 255 | |
| 256 | // Cycle focus to the status area |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 257 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
| 258 | EXPECT_TRUE(tray()->GetWidget()->IsActive()); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 259 | |
| 260 | // Cycle focus to the browser |
[email protected] | eb5a0ab | 2012-10-20 00:50:25 | [diff] [blame] | 261 | focus_cycler()->RotateFocus(FocusCycler::BACKWARD); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 262 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 263 | } |
| 264 | |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 265 | } // namespace test |
| 266 | } // namespace ash |