Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / rotation / tray_rotation_lock_unittest.cc
blob6d036e9547163129e62c283876659d7ec9bb6119
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"
26 namespace ash {
28 class TrayRotationLockTest : public test::AshTestBase {
29 public:
30 TrayRotationLockTest() {}
31 ~TrayRotationLockTest() override {}
33 TrayRotationLock* tray() {
34 return tray_.get();
37 views::View* tray_view() {
38 return tray_view_.get();
41 views::View* default_view() {
42 return default_view_.get();
45 // Creates the tray view associated to |tray_rotation_lock|.
46 views::View* CreateTrayView(TrayRotationLock* tray_rotation_lock);
48 // Destroys only the |tray_view_|. Tests may call this to simulate destruction
49 // order during the deletion of the StatusAreaWidget.
50 void DestroyTrayView();
52 // Sets up a TrayRotationLock, its tray view, and its default view, for the
53 // given SystemTray and its display. On a primary display all will be
54 // created. On a secondary display both the tray view and default view will
55 // be null.
56 void SetUpForStatusAreaWidget(StatusAreaWidget* status_area_widget);
58 // Resets |tray_| |tray_view_| and |default_view_| so that all components of
59 // TrayRotationLock have been cleared. Tests may then call
60 // SetUpForStatusAreaWidget in order to initial the components.
61 void TearDownViews();
63 // test::AshTestBase:
64 void SetUp() override;
65 void TearDown() override;
67 private:
68 scoped_ptr<TrayRotationLock> tray_;
69 scoped_ptr<views::View> tray_view_;
70 scoped_ptr<views::View> default_view_;
72 DISALLOW_COPY_AND_ASSIGN(TrayRotationLockTest);
75 views::View* TrayRotationLockTest::CreateTrayView(
76 TrayRotationLock* tray_rotation_lock) {
77 return tray_rotation_lock->CreateTrayView(
78 StatusAreaWidgetTestHelper::GetUserLoginStatus());
81 void TrayRotationLockTest::DestroyTrayView() {
82 tray_view_.reset();
83 tray_->DestroyTrayView();
86 void TrayRotationLockTest::SetUpForStatusAreaWidget(
87 StatusAreaWidget* status_area_widget) {
88 tray_.reset(new TrayRotationLock(status_area_widget->system_tray()));
89 tray_view_.reset(tray_->CreateTrayView(
90 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
91 default_view_.reset(tray_->CreateDefaultView(
92 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
95 void TrayRotationLockTest::TearDownViews() {
96 tray_view_.reset();
97 default_view_.reset();
98 tray_.reset();
101 void TrayRotationLockTest::SetUp() {
102 // The Display used for testing is not an internal display. This flag
103 // allows for DisplayManager to treat it as one. TrayRotationLock is only
104 // visible on internal primary displays.
105 base::CommandLine::ForCurrentProcess()->AppendSwitch(
106 switches::kAshUseFirstDisplayAsInternal);
107 test::AshTestBase::SetUp();
108 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
111 void TrayRotationLockTest::TearDown() {
112 TearDownViews();
113 test::AshTestBase::TearDown();
116 // Tests that when the tray view is initially created, that it is created
117 // not visible.
118 TEST_F(TrayRotationLockTest, CreateTrayView) {
119 EXPECT_FALSE(tray_view()->visible());
122 // Tests that when the tray view is created, while MaximizeMode is active, that
123 // it is not visible.
124 TEST_F(TrayRotationLockTest, CreateTrayViewDuringMaximizeMode) {
125 TearDownViews();
126 Shell::GetInstance()->maximize_mode_controller()->
127 EnableMaximizeModeWindowManager(true);
128 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
129 EXPECT_FALSE(tray_view()->visible());
130 Shell::GetInstance()->maximize_mode_controller()->
131 EnableMaximizeModeWindowManager(false);
134 // Tests that when the tray view is created, while MaximizeMode is active, and
135 // rotation is locked, that it is visible.
136 TEST_F(TrayRotationLockTest, CreateTrayViewDuringMaximizeModeAndRotationLock) {
137 TearDownViews();
138 Shell::GetInstance()->maximize_mode_controller()->
139 EnableMaximizeModeWindowManager(true);
140 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked(
141 true);
142 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
143 EXPECT_TRUE(tray_view()->visible());
144 Shell::GetInstance()->maximize_mode_controller()->
145 EnableMaximizeModeWindowManager(false);
146 EXPECT_FALSE(tray_view()->visible());
149 // Tests that the enabling of MaximizeMode affects a previously created tray
150 // view, changing the visibility.
151 TEST_F(TrayRotationLockTest, TrayViewVisibilityChangesDuringMaximizeMode) {
152 ASSERT_FALSE(tray_view()->visible());
153 Shell::GetInstance()->maximize_mode_controller()->
154 EnableMaximizeModeWindowManager(true);
155 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked(
156 true);
157 EXPECT_TRUE(tray_view()->visible());
158 Shell::GetInstance()->maximize_mode_controller()->
159 EnableMaximizeModeWindowManager(false);
160 EXPECT_FALSE(tray_view()->visible());
163 // Tests that the when the tray view is created for a secondary display, that
164 // it is not visible, and that MaximizeMode does not affect visibility.
165 TEST_F(TrayRotationLockTest, CreateSecondaryTrayView) {
166 if (!SupportsMultipleDisplays())
167 return;
168 UpdateDisplay("400x400,200x200");
170 SetUpForStatusAreaWidget(
171 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
172 EXPECT_FALSE(tray_view()->visible());
173 Shell::GetInstance()->maximize_mode_controller()->
174 EnableMaximizeModeWindowManager(true);
175 EXPECT_FALSE(tray_view()->visible());
176 Shell::GetInstance()->maximize_mode_controller()->
177 EnableMaximizeModeWindowManager(false);
178 EXPECT_FALSE(tray_view()->visible());
181 // Tests that when the default view is initially created, that it is created
182 // not visible.
183 TEST_F(TrayRotationLockTest, CreateDefaultView) {
184 EXPECT_FALSE(default_view()->visible());
187 // Tests that when the default view is created, while MaximizeMode is active,
188 // that it is visible.
189 TEST_F(TrayRotationLockTest, CreateDefaultViewDuringMaximizeMode) {
190 TearDownViews();
191 Shell::GetInstance()->maximize_mode_controller()->
192 EnableMaximizeModeWindowManager(true);
193 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
194 EXPECT_TRUE(default_view()->visible());
195 Shell::GetInstance()->maximize_mode_controller()->
196 EnableMaximizeModeWindowManager(false);
199 // Tests that the enabling of MaximizeMode affects a previously created default
200 // view, changing the visibility.
201 TEST_F(TrayRotationLockTest, DefaultViewVisibilityChangesDuringMaximizeMode) {
202 Shell::GetInstance()->maximize_mode_controller()->
203 EnableMaximizeModeWindowManager(true);
204 EXPECT_TRUE(default_view()->visible());
205 Shell::GetInstance()->maximize_mode_controller()->
206 EnableMaximizeModeWindowManager(false);
207 EXPECT_FALSE(default_view()->visible());
210 // Tests that no default view is created when the target is a secondary
211 // display.
212 TEST_F(TrayRotationLockTest, CreateSecondaryDefaultView) {
213 if (!SupportsMultipleDisplays())
214 return;
215 UpdateDisplay("400x400,200x200");
217 TearDownViews();
218 SetUpForStatusAreaWidget(
219 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
220 EXPECT_EQ(NULL, default_view());
223 // Tests that activating the default view causes the display to have its
224 // rotation locked, and that the tray view becomes visible.
225 TEST_F(TrayRotationLockTest, PerformActionOnDefaultView) {
226 MaximizeModeController* maximize_mode_controller = Shell::GetInstance()->
227 maximize_mode_controller();
228 ScreenOrientationController* screen_orientation_controller =
229 Shell::GetInstance()->screen_orientation_controller();
230 ASSERT_FALSE(screen_orientation_controller->rotation_locked());
231 maximize_mode_controller->EnableMaximizeModeWindowManager(true);
232 ASSERT_FALSE(tray_view()->visible());
234 ui::GestureEvent tap(
235 0, 0, 0, base::TimeDelta(), ui::GestureEventDetails(ui::ET_GESTURE_TAP));
236 default_view()->OnGestureEvent(&tap);
237 EXPECT_TRUE(screen_orientation_controller->rotation_locked());
238 EXPECT_TRUE(tray_view()->visible());
240 maximize_mode_controller->EnableMaximizeModeWindowManager(false);
243 // Tests that when the tray is created without the internal display being known,
244 // that it will still display correctly once the internal display is known.
245 TEST_F(TrayRotationLockTest, InternalDisplayNotAvailableAtCreation) {
246 int64 internal_display_id = gfx::Display::InternalDisplayId();
247 TearDownViews();
248 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
250 scoped_ptr<TrayRotationLock> tray(new TrayRotationLock(
251 StatusAreaWidgetTestHelper::GetStatusAreaWidget()->system_tray()));
253 gfx::Display::SetInternalDisplayId(internal_display_id);
254 scoped_ptr<views::View> tray_view(CreateTrayView(tray.get()));
255 scoped_ptr<views::View> default_view(tray->CreateDefaultView(
256 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
257 EXPECT_TRUE(default_view);
258 Shell::GetInstance()
259 ->maximize_mode_controller()
260 ->EnableMaximizeModeWindowManager(true);
261 EXPECT_TRUE(default_view->visible());
264 // Tests that when the tray view is deleted, while TrayRotationLock has not been
265 // deleted, that updates to the rotation lock state do not crash.
266 TEST_F(TrayRotationLockTest, LockUpdatedDuringDesctruction) {
267 Shell::GetInstance()
268 ->maximize_mode_controller()
269 ->EnableMaximizeModeWindowManager(true);
270 DestroyTrayView();
271 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked(
272 true);
273 Shell::GetInstance()
274 ->maximize_mode_controller()
275 ->EnableMaximizeModeWindowManager(false);
278 } // namespace ash