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