1 // Copyright 2014 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/system/overview/overview_button_tray.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/rotator/screen_rotation_animator.h"
11 #include "ash/shelf/shelf_types.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "ash/system/status_area_widget.h"
15 #include "ash/system/user/login_status.h"
16 #include "ash/test/ash_test_base.h"
17 #include "ash/test/ash_test_helper.h"
18 #include "ash/test/status_area_widget_test_helper.h"
19 #include "ash/test/test_session_state_delegate.h"
20 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
21 #include "ash/wm/overview/window_selector_controller.h"
22 #include "base/command_line.h"
23 #include "base/test/user_action_tester.h"
24 #include "base/time/time.h"
25 #include "ui/aura/client/aura_constants.h"
26 #include "ui/aura/window.h"
27 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
28 #include "ui/events/event.h"
29 #include "ui/events/event_constants.h"
30 #include "ui/events/gestures/gesture_types.h"
31 #include "ui/views/controls/image_view.h"
37 const char kTrayOverview
[] = "Tray_Overview";
39 OverviewButtonTray
* GetTray() {
40 return StatusAreaWidgetTestHelper::GetStatusAreaWidget()->
41 overview_button_tray();
44 OverviewButtonTray
* GetSecondaryTray() {
45 return StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget()->
46 overview_button_tray();
51 class OverviewButtonTrayTest
: public test::AshTestBase
{
53 OverviewButtonTrayTest() {}
54 ~OverviewButtonTrayTest() override
{}
56 void SetUp() override
;
58 void NotifySessionStateChanged();
61 views::ImageView
* GetImageView(OverviewButtonTray
* tray
) {
66 DISALLOW_COPY_AND_ASSIGN(OverviewButtonTrayTest
);
69 void OverviewButtonTrayTest::SetUp() {
70 base::CommandLine::ForCurrentProcess()->AppendSwitch(
71 switches::kAshUseFirstDisplayAsInternal
);
72 base::CommandLine::ForCurrentProcess()->AppendSwitch(
73 switches::kAshEnableScreenRotationAnimation
);
77 void OverviewButtonTrayTest::NotifySessionStateChanged() {
78 GetTray()->SessionStateChanged(
79 ash_test_helper()->GetTestSessionStateDelegate()->GetSessionState());
82 // Ensures that creation doesn't cause any crashes and adds the image icon.
83 TEST_F(OverviewButtonTrayTest
, BasicConstruction
) {
84 EXPECT_TRUE(GetImageView(GetTray()) != NULL
);
87 // Test that maximize mode toggle changes visibility.
88 // OverviewButtonTray should only be visible when MaximizeMode is enabled.
89 // By default the system should not have MaximizeMode enabled.
90 TEST_F(OverviewButtonTrayTest
, MaximizeModeObserverOnMaximizeModeToggled
) {
91 ASSERT_FALSE(GetTray()->visible());
92 Shell::GetInstance()->maximize_mode_controller()->
93 EnableMaximizeModeWindowManager(true);
94 EXPECT_TRUE(GetTray()->visible());
96 Shell::GetInstance()->maximize_mode_controller()->
97 EnableMaximizeModeWindowManager(false);
98 EXPECT_FALSE(GetTray()->visible());
101 // Tests that activating this control brings up window selection mode.
102 TEST_F(OverviewButtonTrayTest
, PerformAction
) {
103 ASSERT_FALSE(Shell::GetInstance()->window_selector_controller()->
106 // Overview Mode only works when there is a window
107 scoped_ptr
<aura::Window
> window(
108 CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
109 ui::GestureEvent
tap(
110 0, 0, 0, base::TimeDelta(), ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
111 GetTray()->PerformAction(tap
);
112 EXPECT_TRUE(Shell::GetInstance()->window_selector_controller()->
116 // Tests that tapping on the control will record the user action Tray_Overview.
117 TEST_F(OverviewButtonTrayTest
, TrayOverviewUserAction
) {
119 Shell::GetInstance()->window_selector_controller()->IsSelecting());
121 // Tapping on the control when there are no windows (and thus the user cannot
122 // enter overview mode) should still record the action.
123 base::UserActionTester user_action_tester
;
124 ui::GestureEvent
tap(0, 0, 0, base::TimeDelta(),
125 ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
126 GetTray()->PerformAction(tap
);
128 Shell::GetInstance()->window_selector_controller()->IsSelecting());
129 EXPECT_EQ(1, user_action_tester
.GetActionCount(kTrayOverview
));
131 // With one window present, tapping on the control to enter overview mode
132 // should record the user action.
133 scoped_ptr
<aura::Window
> window(
134 CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
135 GetTray()->PerformAction(tap
);
137 Shell::GetInstance()->window_selector_controller()->IsSelecting());
138 EXPECT_EQ(2, user_action_tester
.GetActionCount(kTrayOverview
));
140 // Tapping on the control to exit overview mode should record the
142 GetTray()->PerformAction(tap
);
144 Shell::GetInstance()->window_selector_controller()->IsSelecting());
145 EXPECT_EQ(3, user_action_tester
.GetActionCount(kTrayOverview
));
148 // Tests that a second OverviewButtonTray has been created, and only shows
149 // when MaximizeMode has been enabled, when we are using multiple displays.
150 // By default the DisplayManger is in extended mode.
151 TEST_F(OverviewButtonTrayTest
, DisplaysOnBothDisplays
) {
152 if (!SupportsMultipleDisplays())
155 UpdateDisplay("400x400,200x200");
156 EXPECT_FALSE(GetTray()->visible());
157 EXPECT_FALSE(GetSecondaryTray()->visible());
158 Shell::GetInstance()->maximize_mode_controller()->
159 EnableMaximizeModeWindowManager(true);
160 EXPECT_TRUE(GetTray()->visible());
161 EXPECT_TRUE(GetSecondaryTray()->visible());
162 Shell::GetInstance()->maximize_mode_controller()->
163 EnableMaximizeModeWindowManager(false);
166 // Tests if Maximize Mode is enabled before a secondary display is attached
167 // that the second OverviewButtonTray should be created in a visible state.
168 TEST_F(OverviewButtonTrayTest
, SecondaryTrayCreatedVisible
) {
169 if (!SupportsMultipleDisplays())
172 Shell::GetInstance()->maximize_mode_controller()->
173 EnableMaximizeModeWindowManager(true);
174 UpdateDisplay("400x400,200x200");
175 EXPECT_TRUE(GetSecondaryTray()->visible());
176 Shell::GetInstance()->maximize_mode_controller()->
177 EnableMaximizeModeWindowManager(false);
180 // Tests that the tray loses visibility when a user logs out, and that it
181 // regains visibility when a user logs back in.
182 TEST_F(OverviewButtonTrayTest
, VisibilityChangesForLoginStatus
) {
183 Shell::GetInstance()->maximize_mode_controller()->
184 EnableMaximizeModeWindowManager(true);
185 SetUserLoggedIn(false);
186 Shell::GetInstance()->UpdateAfterLoginStatusChange(user::LOGGED_IN_NONE
);
187 EXPECT_FALSE(GetTray()->visible());
188 SetUserLoggedIn(true);
189 SetSessionStarted(true);
190 Shell::GetInstance()->UpdateAfterLoginStatusChange(user::LOGGED_IN_USER
);
191 EXPECT_TRUE(GetTray()->visible());
192 SetUserAddingScreenRunning(true);
193 NotifySessionStateChanged();
194 EXPECT_FALSE(GetTray()->visible());
195 SetUserAddingScreenRunning(false);
196 NotifySessionStateChanged();
197 EXPECT_TRUE(GetTray()->visible());
198 Shell::GetInstance()->maximize_mode_controller()->
199 EnableMaximizeModeWindowManager(false);
202 // Tests that the tray only renders as active while selection is ongoing. Any
203 // dismissal of overview mode clears the active state.
204 TEST_F(OverviewButtonTrayTest
, ActiveStateOnlyDuringOverviewMode
) {
206 Shell::GetInstance()->window_selector_controller()->IsSelecting());
207 ASSERT_FALSE(GetTray()->draw_background_as_active());
209 // Overview Mode only works when there is a window
210 scoped_ptr
<aura::Window
> window(
211 CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
212 ui::GestureEvent
tap(
213 0, 0, 0, base::TimeDelta(), ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
214 GetTray()->PerformAction(tap
);
216 Shell::GetInstance()->window_selector_controller()->IsSelecting());
217 EXPECT_TRUE(GetTray()->draw_background_as_active());
219 Shell::GetInstance()->window_selector_controller()->OnSelectionEnded();
221 Shell::GetInstance()->window_selector_controller()->IsSelecting());
222 EXPECT_FALSE(GetTray()->draw_background_as_active());
225 // Test that when a hide animation is aborted via deletion, that the
226 // OverviewButton is still hidden.
227 TEST_F(OverviewButtonTrayTest
, HideAnimationAlwaysCompletes
) {
229 ->maximize_mode_controller()
230 ->EnableMaximizeModeWindowManager(true);
232 // Long duration for hide animation, to allow it to be interrupted.
233 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> hide_duration(
234 new ui::ScopedAnimationDurationScaleMode(
235 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION
));
236 GetTray()->SetVisible(false);
238 // ScreenRotationAnimator copies the current layers, and deletes them upon
239 // completion. Allow its animation to complete first.
240 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> rotate_duration(
241 new ui::ScopedAnimationDurationScaleMode(
242 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION
));
243 ash::ScreenRotationAnimator(gfx::Display::InternalDisplayId())
244 .Rotate(gfx::Display::ROTATE_270
, gfx::Display::ROTATION_SOURCE_ACTIVE
);
246 RunAllPendingInMessageLoop();
247 EXPECT_FALSE(GetTray()->visible());
250 // Tests that the overview button becomes visible when the user enters
251 // maximize mode with a system modal window open, and that it hides once
252 // the user exits maximize mode.
253 TEST_F(OverviewButtonTrayTest
, VisibilityChangesForSystemModalWindow
) {
254 // TODO(jonross): When CreateTestWindow*() have been unified, use the
255 // appropriate method to replace this setup. (crbug.com/483503)
256 scoped_ptr
<aura::Window
> window(new aura::Window(nullptr));
257 window
->SetProperty(aura::client::kModalKey
, ui::MODAL_TYPE_SYSTEM
);
258 window
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
259 window
->Init(ui::LAYER_TEXTURED
);
261 ParentWindowInPrimaryRootWindow(window
.get());
263 ASSERT_TRUE(Shell::GetInstance()->IsSystemModalWindowOpen());
265 ->maximize_mode_controller()
266 ->EnableMaximizeModeWindowManager(true);
267 EXPECT_TRUE(GetTray()->visible());
269 ->maximize_mode_controller()
270 ->EnableMaximizeModeWindowManager(false);
271 EXPECT_FALSE(GetTray()->visible());