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
);
75 void OverviewButtonTrayTest::NotifySessionStateChanged() {
76 GetTray()->SessionStateChanged(
77 ash_test_helper()->GetTestSessionStateDelegate()->GetSessionState());
80 // Ensures that creation doesn't cause any crashes and adds the image icon.
81 TEST_F(OverviewButtonTrayTest
, BasicConstruction
) {
82 EXPECT_TRUE(GetImageView(GetTray()) != NULL
);
85 // Test that maximize mode toggle changes visibility.
86 // OverviewButtonTray should only be visible when MaximizeMode is enabled.
87 // By default the system should not have MaximizeMode enabled.
88 TEST_F(OverviewButtonTrayTest
, MaximizeModeObserverOnMaximizeModeToggled
) {
89 ASSERT_FALSE(GetTray()->visible());
90 Shell::GetInstance()->maximize_mode_controller()->
91 EnableMaximizeModeWindowManager(true);
92 EXPECT_TRUE(GetTray()->visible());
94 Shell::GetInstance()->maximize_mode_controller()->
95 EnableMaximizeModeWindowManager(false);
96 EXPECT_FALSE(GetTray()->visible());
99 // Tests that activating this control brings up window selection mode.
100 TEST_F(OverviewButtonTrayTest
, PerformAction
) {
101 ASSERT_FALSE(Shell::GetInstance()->window_selector_controller()->
104 // Overview Mode only works when there is a window
105 scoped_ptr
<aura::Window
> window(
106 CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
107 ui::GestureEvent
tap(
108 0, 0, 0, base::TimeDelta(), ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
109 GetTray()->PerformAction(tap
);
110 EXPECT_TRUE(Shell::GetInstance()->window_selector_controller()->
114 // Tests that tapping on the control will record the user action Tray_Overview.
115 TEST_F(OverviewButtonTrayTest
, TrayOverviewUserAction
) {
117 Shell::GetInstance()->window_selector_controller()->IsSelecting());
119 // Tapping on the control when there are no windows (and thus the user cannot
120 // enter overview mode) should still record the action.
121 base::UserActionTester user_action_tester
;
122 ui::GestureEvent
tap(0, 0, 0, base::TimeDelta(),
123 ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
124 GetTray()->PerformAction(tap
);
126 Shell::GetInstance()->window_selector_controller()->IsSelecting());
127 EXPECT_EQ(1, user_action_tester
.GetActionCount(kTrayOverview
));
129 // With one window present, tapping on the control to enter overview mode
130 // should record the user action.
131 scoped_ptr
<aura::Window
> window(
132 CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
133 GetTray()->PerformAction(tap
);
135 Shell::GetInstance()->window_selector_controller()->IsSelecting());
136 EXPECT_EQ(2, user_action_tester
.GetActionCount(kTrayOverview
));
138 // Tapping on the control to exit overview mode should record the
140 GetTray()->PerformAction(tap
);
142 Shell::GetInstance()->window_selector_controller()->IsSelecting());
143 EXPECT_EQ(3, user_action_tester
.GetActionCount(kTrayOverview
));
146 // Tests that a second OverviewButtonTray has been created, and only shows
147 // when MaximizeMode has been enabled, when we are using multiple displays.
148 // By default the DisplayManger is in extended mode.
149 TEST_F(OverviewButtonTrayTest
, DisplaysOnBothDisplays
) {
150 if (!SupportsMultipleDisplays())
153 UpdateDisplay("400x400,200x200");
154 EXPECT_FALSE(GetTray()->visible());
155 EXPECT_FALSE(GetSecondaryTray()->visible());
156 Shell::GetInstance()->maximize_mode_controller()->
157 EnableMaximizeModeWindowManager(true);
158 EXPECT_TRUE(GetTray()->visible());
159 EXPECT_TRUE(GetSecondaryTray()->visible());
160 Shell::GetInstance()->maximize_mode_controller()->
161 EnableMaximizeModeWindowManager(false);
164 // Tests if Maximize Mode is enabled before a secondary display is attached
165 // that the second OverviewButtonTray should be created in a visible state.
166 TEST_F(OverviewButtonTrayTest
, SecondaryTrayCreatedVisible
) {
167 if (!SupportsMultipleDisplays())
170 Shell::GetInstance()->maximize_mode_controller()->
171 EnableMaximizeModeWindowManager(true);
172 UpdateDisplay("400x400,200x200");
173 EXPECT_TRUE(GetSecondaryTray()->visible());
174 Shell::GetInstance()->maximize_mode_controller()->
175 EnableMaximizeModeWindowManager(false);
178 // Tests that the tray loses visibility when a user logs out, and that it
179 // regains visibility when a user logs back in.
180 TEST_F(OverviewButtonTrayTest
, VisibilityChangesForLoginStatus
) {
181 Shell::GetInstance()->maximize_mode_controller()->
182 EnableMaximizeModeWindowManager(true);
183 SetUserLoggedIn(false);
184 Shell::GetInstance()->UpdateAfterLoginStatusChange(user::LOGGED_IN_NONE
);
185 EXPECT_FALSE(GetTray()->visible());
186 SetUserLoggedIn(true);
187 SetSessionStarted(true);
188 Shell::GetInstance()->UpdateAfterLoginStatusChange(user::LOGGED_IN_USER
);
189 EXPECT_TRUE(GetTray()->visible());
190 SetUserAddingScreenRunning(true);
191 NotifySessionStateChanged();
192 EXPECT_FALSE(GetTray()->visible());
193 SetUserAddingScreenRunning(false);
194 NotifySessionStateChanged();
195 EXPECT_TRUE(GetTray()->visible());
196 Shell::GetInstance()->maximize_mode_controller()->
197 EnableMaximizeModeWindowManager(false);
200 // Tests that the tray only renders as active while selection is ongoing. Any
201 // dismissal of overview mode clears the active state.
202 TEST_F(OverviewButtonTrayTest
, ActiveStateOnlyDuringOverviewMode
) {
204 Shell::GetInstance()->window_selector_controller()->IsSelecting());
205 ASSERT_FALSE(GetTray()->draw_background_as_active());
207 // Overview Mode only works when there is a window
208 scoped_ptr
<aura::Window
> window(
209 CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
210 ui::GestureEvent
tap(
211 0, 0, 0, base::TimeDelta(), ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
212 GetTray()->PerformAction(tap
);
214 Shell::GetInstance()->window_selector_controller()->IsSelecting());
215 EXPECT_TRUE(GetTray()->draw_background_as_active());
217 Shell::GetInstance()->window_selector_controller()->OnSelectionEnded();
219 Shell::GetInstance()->window_selector_controller()->IsSelecting());
220 EXPECT_FALSE(GetTray()->draw_background_as_active());
223 // Test that when a hide animation is aborted via deletion, that the
224 // OverviewButton is still hidden.
225 TEST_F(OverviewButtonTrayTest
, HideAnimationAlwaysCompletes
) {
227 ->maximize_mode_controller()
228 ->EnableMaximizeModeWindowManager(true);
230 // Long duration for hide animation, to allow it to be interrupted.
231 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> hide_duration(
232 new ui::ScopedAnimationDurationScaleMode(
233 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION
));
234 GetTray()->SetVisible(false);
236 // ScreenRotationAnimator copies the current layers, and deletes them upon
237 // completion. Allow its animation to complete first.
238 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> rotate_duration(
239 new ui::ScopedAnimationDurationScaleMode(
240 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION
));
241 ash::ScreenRotationAnimator(gfx::Display::InternalDisplayId())
242 .Rotate(gfx::Display::ROTATE_270
, gfx::Display::ROTATION_SOURCE_ACTIVE
);
244 RunAllPendingInMessageLoop();
245 EXPECT_FALSE(GetTray()->visible());
248 // Tests that the overview button becomes visible when the user enters
249 // maximize mode with a system modal window open, and that it hides once
250 // the user exits maximize mode.
251 TEST_F(OverviewButtonTrayTest
, VisibilityChangesForSystemModalWindow
) {
252 // TODO(jonross): When CreateTestWindow*() have been unified, use the
253 // appropriate method to replace this setup. (crbug.com/483503)
254 scoped_ptr
<aura::Window
> window(new aura::Window(nullptr));
255 window
->SetProperty(aura::client::kModalKey
, ui::MODAL_TYPE_SYSTEM
);
256 window
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
257 window
->Init(ui::LAYER_TEXTURED
);
259 ParentWindowInPrimaryRootWindow(window
.get());
261 ASSERT_TRUE(Shell::GetInstance()->IsSystemModalWindowOpen());
263 ->maximize_mode_controller()
264 ->EnableMaximizeModeWindowManager(true);
265 EXPECT_TRUE(GetTray()->visible());
267 ->maximize_mode_controller()
268 ->EnableMaximizeModeWindowManager(false);
269 EXPECT_FALSE(GetTray()->visible());