[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" |
| 8 | #include "ash/shell.h" |
| 9 | #include "ash/shell_window_ids.h" |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 10 | #include "ash/system/status_area_widget.h" |
| 11 | #include "ash/system/status_area_widget_delegate.h" |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 12 | #include "ash/system/tray/system_tray.h" |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 13 | #include "ash/wm/window_util.h" |
| 14 | #include "ash/test/ash_test_base.h" |
| 15 | #include "ash/shell_factory.h" |
| 16 | #include "ui/aura/test/test_windows.h" |
| 17 | #include "ui/aura/window.h" |
| 18 | #include "ui/views/controls/button/menu_button.h" |
| 19 | #include "ui/views/widget/widget.h" |
| 20 | |
| 21 | namespace ash { |
| 22 | namespace test { |
| 23 | |
| 24 | using aura::test::CreateTestWindowWithId; |
| 25 | using aura::Window; |
| 26 | using internal::FocusCycler; |
| 27 | |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 28 | namespace { |
| 29 | |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 30 | internal::StatusAreaWidgetDelegate* GetStatusAreaWidgetDelegate( |
| 31 | views::Widget* widget) { |
| 32 | return static_cast<internal::StatusAreaWidgetDelegate*>( |
[email protected] | 82157fb8 | 2012-04-11 22:07:49 | [diff] [blame] | 33 | widget->GetContentsView()); |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 36 | SystemTray* CreateSystemTray() { |
| 37 | SystemTray* tray = new SystemTray; |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 38 | internal::StatusAreaWidget* widget = new internal::StatusAreaWidget; |
| 39 | widget->AddTray(tray); |
| 40 | widget->Show(); |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 41 | return tray; |
| 42 | } |
| 43 | |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 44 | } // namespace |
| 45 | |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 46 | typedef AshTestBase FocusCyclerTest; |
| 47 | |
| 48 | TEST_F(FocusCyclerTest, CycleFocusBrowserOnly) { |
| 49 | scoped_ptr<FocusCycler> focus_cycler(new FocusCycler()); |
| 50 | |
| 51 | // Create a single test window. |
| 52 | Window* default_container = |
| 53 | ash::Shell::GetInstance()->GetContainer( |
| 54 | internal::kShellWindowId_DefaultContainer); |
| 55 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container)); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 56 | wm::ActivateWindow(window0.get()); |
| 57 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 58 | |
| 59 | // Cycle the window |
| 60 | focus_cycler->RotateFocus(FocusCycler::FORWARD); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 61 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST_F(FocusCyclerTest, CycleFocusForward) { |
| 65 | scoped_ptr<FocusCycler> focus_cycler(new FocusCycler()); |
| 66 | |
| 67 | // Add the Status area |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 68 | scoped_ptr<SystemTray> tray(CreateSystemTray()); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 69 | ASSERT_TRUE(tray->GetWidget()); |
| 70 | focus_cycler->AddWidget(tray->GetWidget()); |
| 71 | GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting( |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 72 | focus_cycler.get()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 73 | |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 74 | // Add the launcher |
| 75 | Launcher* launcher = Shell::GetInstance()->launcher(); |
| 76 | ASSERT_TRUE(launcher); |
| 77 | views::Widget* launcher_widget = launcher->widget(); |
| 78 | ASSERT_TRUE(launcher_widget); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 79 | launcher->SetFocusCycler(focus_cycler.get()); |
| 80 | |
| 81 | // Create a single test window. |
| 82 | Window* default_container = |
| 83 | ash::Shell::GetInstance()->GetContainer( |
| 84 | internal::kShellWindowId_DefaultContainer); |
| 85 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container)); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 86 | wm::ActivateWindow(window0.get()); |
| 87 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 88 | |
| 89 | // Cycle focus to the status area |
| 90 | focus_cycler->RotateFocus(FocusCycler::FORWARD); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 91 | EXPECT_TRUE(tray->GetWidget()->IsActive()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 92 | |
| 93 | // Cycle focus to the launcher |
| 94 | focus_cycler->RotateFocus(FocusCycler::FORWARD); |
| 95 | EXPECT_TRUE(launcher_widget->IsActive()); |
| 96 | |
| 97 | // Cycle focus to the browser |
| 98 | focus_cycler->RotateFocus(FocusCycler::FORWARD); |
[email protected] | f3c7b25 | 2012-02-27 12:17:54 | [diff] [blame] | 99 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(FocusCyclerTest, CycleFocusBackward) { |
| 103 | scoped_ptr<FocusCycler> focus_cycler(new FocusCycler()); |
| 104 | |
| 105 | // Add the Status area |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 106 | scoped_ptr<SystemTray> tray(CreateSystemTray()); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 107 | ASSERT_TRUE(tray->GetWidget()); |
| 108 | focus_cycler->AddWidget(tray->GetWidget()); |
| 109 | GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting( |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 110 | focus_cycler.get()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 111 | |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 112 | // Add the launcher |
| 113 | Launcher* launcher = Shell::GetInstance()->launcher(); |
| 114 | ASSERT_TRUE(launcher); |
| 115 | views::Widget* launcher_widget = launcher->widget(); |
| 116 | ASSERT_TRUE(launcher_widget); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 117 | launcher->SetFocusCycler(focus_cycler.get()); |
| 118 | |
| 119 | // Create a single test window. |
| 120 | Window* default_container = |
| 121 | ash::Shell::GetInstance()->GetContainer( |
| 122 | internal::kShellWindowId_DefaultContainer); |
| 123 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container)); |
[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 launcher |
| 128 | focus_cycler->RotateFocus(FocusCycler::BACKWARD); |
| 129 | EXPECT_TRUE(launcher_widget->IsActive()); |
| 130 | |
| 131 | // Cycle focus to the status area |
| 132 | focus_cycler->RotateFocus(FocusCycler::BACKWARD); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 133 | EXPECT_TRUE(tray->GetWidget()->IsActive()); |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 134 | |
| 135 | // Cycle focus to the browser |
| 136 | focus_cycler->RotateFocus(FocusCycler::BACKWARD); |
[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] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 140 | class FocusCyclerLauncherTest : public AshTestBase { |
| 141 | public: |
| 142 | FocusCyclerLauncherTest() : AshTestBase() {} |
| 143 | virtual ~FocusCyclerLauncherTest() {} |
| 144 | |
| 145 | virtual void SetUp() OVERRIDE { |
| 146 | AshTestBase::SetUp(); |
| 147 | |
| 148 | // Hide the launcher |
| 149 | Launcher* launcher = Shell::GetInstance()->launcher(); |
| 150 | ASSERT_TRUE(launcher); |
| 151 | views::Widget* launcher_widget = launcher->widget(); |
| 152 | ASSERT_TRUE(launcher_widget); |
| 153 | launcher_widget->Hide(); |
| 154 | } |
| 155 | |
| 156 | virtual void TearDown() OVERRIDE { |
| 157 | // Show the launcher |
| 158 | Launcher* launcher = Shell::GetInstance()->launcher(); |
| 159 | ASSERT_TRUE(launcher); |
| 160 | views::Widget* launcher_widget = launcher->widget(); |
| 161 | ASSERT_TRUE(launcher_widget); |
| 162 | launcher_widget->Show(); |
| 163 | |
| 164 | AshTestBase::TearDown(); |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | DISALLOW_COPY_AND_ASSIGN(FocusCyclerLauncherTest); |
| 169 | }; |
| 170 | |
| 171 | TEST_F(FocusCyclerLauncherTest, CycleFocusForwardInvisible) { |
| 172 | scoped_ptr<FocusCycler> focus_cycler(new FocusCycler()); |
| 173 | |
| 174 | // Add the Status area |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 175 | scoped_ptr<SystemTray> tray(CreateSystemTray()); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 176 | ASSERT_TRUE(tray->GetWidget()); |
| 177 | focus_cycler->AddWidget(tray->GetWidget()); |
| 178 | GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting( |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 179 | focus_cycler.get()); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 180 | |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 181 | // Add the launcher |
| 182 | Launcher* launcher = Shell::GetInstance()->launcher(); |
| 183 | ASSERT_TRUE(launcher); |
| 184 | views::Widget* launcher_widget = launcher->widget(); |
| 185 | ASSERT_TRUE(launcher_widget); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 186 | launcher->SetFocusCycler(focus_cycler.get()); |
| 187 | |
| 188 | // Create a single test window. |
| 189 | Window* default_container = |
| 190 | ash::Shell::GetInstance()->GetContainer( |
| 191 | internal::kShellWindowId_DefaultContainer); |
| 192 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container)); |
| 193 | wm::ActivateWindow(window0.get()); |
| 194 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 195 | |
| 196 | // Cycle focus to the status area |
| 197 | focus_cycler->RotateFocus(FocusCycler::FORWARD); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 198 | EXPECT_TRUE(tray->GetWidget()->IsActive()); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 199 | |
| 200 | // Cycle focus to the browser |
| 201 | focus_cycler->RotateFocus(FocusCycler::FORWARD); |
| 202 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 203 | } |
| 204 | |
| 205 | TEST_F(FocusCyclerLauncherTest, CycleFocusBackwardInvisible) { |
| 206 | scoped_ptr<FocusCycler> focus_cycler(new FocusCycler()); |
| 207 | |
| 208 | // Add the Status area |
[email protected] | 860f594 | 2012-04-24 16:11:39 | [diff] [blame] | 209 | scoped_ptr<SystemTray> tray(CreateSystemTray()); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 210 | ASSERT_TRUE(tray->GetWidget()); |
| 211 | focus_cycler->AddWidget(tray->GetWidget()); |
| 212 | GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting( |
[email protected] | 8676f047 | 2012-03-29 20:30:12 | [diff] [blame] | 213 | focus_cycler.get()); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 214 | |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 215 | // Add the launcher |
| 216 | Launcher* launcher = Shell::GetInstance()->launcher(); |
| 217 | ASSERT_TRUE(launcher); |
| 218 | views::Widget* launcher_widget = launcher->widget(); |
| 219 | ASSERT_TRUE(launcher_widget); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 220 | launcher->SetFocusCycler(focus_cycler.get()); |
| 221 | |
| 222 | // Create a single test window. |
| 223 | Window* default_container = |
| 224 | ash::Shell::GetInstance()->GetContainer( |
| 225 | internal::kShellWindowId_DefaultContainer); |
| 226 | scoped_ptr<Window> window0(CreateTestWindowWithId(0, default_container)); |
| 227 | wm::ActivateWindow(window0.get()); |
| 228 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 229 | |
| 230 | // Cycle focus to the status area |
| 231 | focus_cycler->RotateFocus(FocusCycler::BACKWARD); |
[email protected] | 6d6546e | 2012-05-30 23:12:02 | [diff] [blame^] | 232 | EXPECT_TRUE(tray->GetWidget()->IsActive()); |
[email protected] | 057dc575 | 2012-03-06 23:59:31 | [diff] [blame] | 233 | |
| 234 | // Cycle focus to the browser |
| 235 | focus_cycler->RotateFocus(FocusCycler::BACKWARD); |
| 236 | EXPECT_TRUE(wm::IsActiveWindow(window0.get())); |
| 237 | } |
| 238 | |
[email protected] | df1c986 | 2012-02-27 10:37:31 | [diff] [blame] | 239 | } // namespace test |
| 240 | } // namespace ash |