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/chromeos/rotation/tray_rotation_lock.h"
7 #include "ash/ash_switches.h"
8 #include "ash/content/display/screen_orientation_controller_chromeos.h"
9 #include "ash/display/display_manager.h"
10 #include "ash/root_window_controller.h"
11 #include "ash/shelf/shelf_widget.h"
12 #include "ash/shell.h"
13 #include "ash/system/status_area_widget.h"
14 #include "ash/system/tray/system_tray.h"
15 #include "ash/system/tray/system_tray_delegate.h"
16 #include "ash/test/ash_test_base.h"
17 #include "ash/test/status_area_widget_test_helper.h"
18 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
19 #include "base/command_line.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/time/time.h"
22 #include "ui/events/event.h"
23 #include "ui/events/event_constants.h"
24 #include "ui/views/view.h"
28 class TrayRotationLockTest
: public test::AshTestBase
{
30 TrayRotationLockTest() {}
31 ~TrayRotationLockTest() override
{}
33 TrayRotationLock
* tray() {
37 views::View
* tray_view() {
38 return tray_view_
.get();
41 views::View
* default_view() {
42 return default_view_
.get();
45 // Sets up a TrayRotationLock, its tray view, and its default view, for the
46 // given SystemTray and its display. On a primary display all will be
47 // created. On a secondary display both the tray view and default view will
49 void SetUpForStatusAreaWidget(StatusAreaWidget
* status_area_widget
);
51 // Resets |tray_| |tray_view_| and |default_view_| so that all components of
52 // TrayRotationLock have been cleared. Tests may then call
53 // SetUpForStatusAreaWidget in order to initial the components.
57 void SetUp() override
;
58 void TearDown() override
;
61 scoped_ptr
<TrayRotationLock
> tray_
;
62 scoped_ptr
<views::View
> tray_view_
;
63 scoped_ptr
<views::View
> default_view_
;
65 DISALLOW_COPY_AND_ASSIGN(TrayRotationLockTest
);
68 void TrayRotationLockTest::SetUpForStatusAreaWidget(
69 StatusAreaWidget
* status_area_widget
) {
70 tray_
.reset(new TrayRotationLock(status_area_widget
->system_tray()));
71 tray_view_
.reset(tray_
->CreateTrayView(
72 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
73 default_view_
.reset(tray_
->CreateDefaultView(
74 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
77 void TrayRotationLockTest::TearDownViews() {
79 default_view_
.reset();
83 void TrayRotationLockTest::SetUp() {
84 // The Display used for testing is not an internal display. This flag
85 // allows for DisplayManager to treat it as one. TrayRotationLock is only
86 // visible on internal primary displays.
87 base::CommandLine::ForCurrentProcess()->AppendSwitch(
88 switches::kAshUseFirstDisplayAsInternal
);
89 test::AshTestBase::SetUp();
90 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
93 void TrayRotationLockTest::TearDown() {
95 test::AshTestBase::TearDown();
98 // Tests that when the tray view is initially created, that it is created
100 TEST_F(TrayRotationLockTest
, CreateTrayView
) {
101 EXPECT_FALSE(tray_view()->visible());
104 // Tests that when the tray view is created, while MaximizeMode is active, that
105 // it is not visible.
106 TEST_F(TrayRotationLockTest
, CreateTrayViewDuringMaximizeMode
) {
108 Shell::GetInstance()->maximize_mode_controller()->
109 EnableMaximizeModeWindowManager(true);
110 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
111 EXPECT_FALSE(tray_view()->visible());
112 Shell::GetInstance()->maximize_mode_controller()->
113 EnableMaximizeModeWindowManager(false);
116 // Tests that when the tray view is created, while MaximizeMode is active, and
117 // rotation is locked, that it is visible.
118 TEST_F(TrayRotationLockTest
, CreateTrayViewDuringMaximizeModeAndRotationLock
) {
120 Shell::GetInstance()->maximize_mode_controller()->
121 EnableMaximizeModeWindowManager(true);
122 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked(
124 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
125 EXPECT_TRUE(tray_view()->visible());
126 Shell::GetInstance()->maximize_mode_controller()->
127 EnableMaximizeModeWindowManager(false);
128 EXPECT_FALSE(tray_view()->visible());
131 // Tests that the enabling of MaximizeMode affects a previously created tray
132 // view, changing the visibility.
133 TEST_F(TrayRotationLockTest
, TrayViewVisibilityChangesDuringMaximizeMode
) {
134 ASSERT_FALSE(tray_view()->visible());
135 Shell::GetInstance()->maximize_mode_controller()->
136 EnableMaximizeModeWindowManager(true);
137 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked(
139 EXPECT_TRUE(tray_view()->visible());
140 Shell::GetInstance()->maximize_mode_controller()->
141 EnableMaximizeModeWindowManager(false);
142 EXPECT_FALSE(tray_view()->visible());
145 // Tests that the when the tray view is created for a secondary display, that
146 // it is not visible, and that MaximizeMode does not affect visibility.
147 TEST_F(TrayRotationLockTest
, CreateSecondaryTrayView
) {
148 if (!SupportsMultipleDisplays())
150 UpdateDisplay("400x400,200x200");
152 SetUpForStatusAreaWidget(
153 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
154 EXPECT_FALSE(tray_view()->visible());
155 Shell::GetInstance()->maximize_mode_controller()->
156 EnableMaximizeModeWindowManager(true);
157 EXPECT_FALSE(tray_view()->visible());
158 Shell::GetInstance()->maximize_mode_controller()->
159 EnableMaximizeModeWindowManager(false);
160 EXPECT_FALSE(tray_view()->visible());
163 // Tests that when the default view is initially created, that it is created
165 TEST_F(TrayRotationLockTest
, CreateDefaultView
) {
166 EXPECT_FALSE(default_view()->visible());
169 // Tests that when the default view is created, while MaximizeMode is active,
170 // that it is visible.
171 TEST_F(TrayRotationLockTest
, CreateDefaultViewDuringMaximizeMode
) {
173 Shell::GetInstance()->maximize_mode_controller()->
174 EnableMaximizeModeWindowManager(true);
175 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
176 EXPECT_TRUE(default_view()->visible());
177 Shell::GetInstance()->maximize_mode_controller()->
178 EnableMaximizeModeWindowManager(false);
181 // Tests that the enabling of MaximizeMode affects a previously created default
182 // view, changing the visibility.
183 TEST_F(TrayRotationLockTest
, DefaultViewVisibilityChangesDuringMaximizeMode
) {
184 Shell::GetInstance()->maximize_mode_controller()->
185 EnableMaximizeModeWindowManager(true);
186 EXPECT_TRUE(default_view()->visible());
187 Shell::GetInstance()->maximize_mode_controller()->
188 EnableMaximizeModeWindowManager(false);
189 EXPECT_FALSE(default_view()->visible());
192 // Tests that no default view is created when the target is a secondary
194 TEST_F(TrayRotationLockTest
, CreateSecondaryDefaultView
) {
195 if (!SupportsMultipleDisplays())
197 UpdateDisplay("400x400,200x200");
200 SetUpForStatusAreaWidget(
201 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
202 EXPECT_EQ(NULL
, default_view());
205 // Tests that activating the default view causes the display to have its
206 // rotation locked, and that the tray view becomes visible.
207 TEST_F(TrayRotationLockTest
, PerformActionOnDefaultView
) {
208 MaximizeModeController
* maximize_mode_controller
= Shell::GetInstance()->
209 maximize_mode_controller();
210 ScreenOrientationController
* screen_orientation_controller
=
211 Shell::GetInstance()->screen_orientation_controller();
212 ASSERT_FALSE(screen_orientation_controller
->rotation_locked());
213 maximize_mode_controller
->EnableMaximizeModeWindowManager(true);
214 ASSERT_FALSE(tray_view()->visible());
216 ui::GestureEvent
tap(
217 0, 0, 0, base::TimeDelta(), ui::GestureEventDetails(ui::ET_GESTURE_TAP
));
218 default_view()->OnGestureEvent(&tap
);
219 EXPECT_TRUE(screen_orientation_controller
->rotation_locked());
220 EXPECT_TRUE(tray_view()->visible());
222 maximize_mode_controller
->EnableMaximizeModeWindowManager(false);