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/display/display_manager.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/shelf/shelf_widget.h"
11 #include "ash/shell.h"
12 #include "ash/system/status_area_widget.h"
13 #include "ash/system/tray/system_tray.h"
14 #include "ash/system/tray/system_tray_delegate.h"
15 #include "ash/test/ash_test_base.h"
16 #include "ash/test/status_area_widget_test_helper.h"
17 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
18 #include "base/command_line.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/time/time.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_constants.h"
23 #include "ui/views/view.h"
27 class TrayRotationLockTest
: public test::AshTestBase
{
29 TrayRotationLockTest() {}
30 virtual ~TrayRotationLockTest() {}
32 TrayRotationLock
* tray() {
36 views::View
* tray_view() {
37 return tray_view_
.get();
40 views::View
* default_view() {
41 return default_view_
.get();
44 // Sets up a TrayRotationLock, its tray view, and its default view, for the
45 // given SystemTray and its display. On a primary display all will be
46 // created. On a secondary display both the tray view and default view will
48 void SetUpForStatusAreaWidget(StatusAreaWidget
* status_area_widget
);
50 // Resets |tray_| |tray_view_| and |default_view_| so that all components of
51 // TrayRotationLock have been cleared. Tests may then call
52 // SetUpForStatusAreaWidget in order to initial the components.
56 virtual void SetUp() OVERRIDE
;
57 virtual void TearDown() OVERRIDE
;
60 scoped_ptr
<TrayRotationLock
> tray_
;
61 scoped_ptr
<views::View
> tray_view_
;
62 scoped_ptr
<views::View
> default_view_
;
64 DISALLOW_COPY_AND_ASSIGN(TrayRotationLockTest
);
67 void TrayRotationLockTest::SetUpForStatusAreaWidget(
68 StatusAreaWidget
* status_area_widget
) {
69 tray_
.reset(new TrayRotationLock(status_area_widget
->system_tray()));
70 tray_view_
.reset(tray_
->CreateTrayView(
71 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
72 default_view_
.reset(tray_
->CreateDefaultView(
73 StatusAreaWidgetTestHelper::GetUserLoginStatus()));
76 void TrayRotationLockTest::TearDownViews() {
78 default_view_
.reset();
82 void TrayRotationLockTest::SetUp() {
83 // The Display used for testing is not an internal display. This flag
84 // allows for DisplayManager to treat it as one. TrayRotationLock is only
85 // visible on internal primary displays.
86 CommandLine::ForCurrentProcess()->AppendSwitch(
87 switches::kAshUseFirstDisplayAsInternal
);
88 test::AshTestBase::SetUp();
89 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
92 void TrayRotationLockTest::TearDown() {
94 test::AshTestBase::TearDown();
97 // Tests that when the tray view is initially created, that it is created
99 TEST_F(TrayRotationLockTest
, CreateTrayView
) {
100 EXPECT_FALSE(tray_view()->visible());
103 // Tests that when the tray view is created, while MaximizeMode is active, that
104 // it is not visible.
105 TEST_F(TrayRotationLockTest
, CreateTrayViewDuringMaximizeMode
) {
107 Shell::GetInstance()->maximize_mode_controller()->
108 EnableMaximizeModeWindowManager(true);
109 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
110 EXPECT_FALSE(tray_view()->visible());
111 Shell::GetInstance()->maximize_mode_controller()->
112 EnableMaximizeModeWindowManager(false);
115 // Tests that when the tray view is created, while MaximizeMode is active, and
116 // rotation is locked, that it is visible.
117 TEST_F(TrayRotationLockTest
, CreateTrayViewDuringMaximizeModeAndRotationLock
) {
119 Shell::GetInstance()->maximize_mode_controller()->
120 EnableMaximizeModeWindowManager(true);
121 Shell::GetInstance()-> maximize_mode_controller()->SetRotationLocked(true);
122 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
123 EXPECT_TRUE(tray_view()->visible());
124 Shell::GetInstance()->maximize_mode_controller()->
125 EnableMaximizeModeWindowManager(false);
126 EXPECT_FALSE(tray_view()->visible());
129 // Tests that the enabling of MaximizeMode affects a previously created tray
130 // view, changing the visibility.
131 TEST_F(TrayRotationLockTest
, TrayViewVisibilityChangesDuringMaximizeMode
) {
132 ASSERT_FALSE(tray_view()->visible());
133 Shell::GetInstance()->maximize_mode_controller()->
134 EnableMaximizeModeWindowManager(true);
135 Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(true);
136 EXPECT_TRUE(tray_view()->visible());
137 Shell::GetInstance()->maximize_mode_controller()->
138 EnableMaximizeModeWindowManager(false);
139 EXPECT_FALSE(tray_view()->visible());
142 // Tests that the when the tray view is created for a secondary display, that
143 // it is not visible, and that MaximizeMode does not affect visibility.
144 TEST_F(TrayRotationLockTest
, CreateSecondaryTrayView
) {
145 if (!SupportsMultipleDisplays())
147 UpdateDisplay("400x400,200x200");
149 SetUpForStatusAreaWidget(
150 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
151 EXPECT_FALSE(tray_view()->visible());
152 Shell::GetInstance()->maximize_mode_controller()->
153 EnableMaximizeModeWindowManager(true);
154 EXPECT_FALSE(tray_view()->visible());
155 Shell::GetInstance()->maximize_mode_controller()->
156 EnableMaximizeModeWindowManager(false);
157 EXPECT_FALSE(tray_view()->visible());
160 // Tests that when the default view is initially created, that it is created
162 TEST_F(TrayRotationLockTest
, CreateDefaultView
) {
163 EXPECT_FALSE(default_view()->visible());
166 // Tests that when the default view is created, while MaximizeMode is active,
167 // that it is visible.
168 TEST_F(TrayRotationLockTest
, CreateDefaultViewDuringMaximizeMode
) {
170 Shell::GetInstance()->maximize_mode_controller()->
171 EnableMaximizeModeWindowManager(true);
172 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
173 EXPECT_TRUE(default_view()->visible());
174 Shell::GetInstance()->maximize_mode_controller()->
175 EnableMaximizeModeWindowManager(false);
178 // Tests that the enabling of MaximizeMode affects a previously created default
179 // view, changing the visibility.
180 TEST_F(TrayRotationLockTest
, DefaultViewVisibilityChangesDuringMaximizeMode
) {
181 Shell::GetInstance()->maximize_mode_controller()->
182 EnableMaximizeModeWindowManager(true);
183 EXPECT_TRUE(default_view()->visible());
184 Shell::GetInstance()->maximize_mode_controller()->
185 EnableMaximizeModeWindowManager(false);
186 EXPECT_FALSE(default_view()->visible());
189 // Tests that no default view is created when the target is a secondary
191 TEST_F(TrayRotationLockTest
, CreateSecondaryDefaultView
) {
192 if (!SupportsMultipleDisplays())
194 UpdateDisplay("400x400,200x200");
197 SetUpForStatusAreaWidget(
198 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
199 EXPECT_EQ(NULL
, default_view());
202 // Tests that activating the default view causes the display to have its
203 // rotation locked, and that the tray view becomes visible.
204 TEST_F(TrayRotationLockTest
, PerformActionOnDefaultView
) {
205 MaximizeModeController
* maximize_mode_controller
= Shell::GetInstance()->
206 maximize_mode_controller();
207 ASSERT_FALSE(maximize_mode_controller
->rotation_locked());
208 Shell::GetInstance()->maximize_mode_controller()->
209 EnableMaximizeModeWindowManager(true);
210 ASSERT_FALSE(tray_view()->visible());
212 ui::GestureEvent
tap(0, 0, 0, base::TimeDelta(),
213 ui::GestureEventDetails(ui::ET_GESTURE_TAP
, 0.0f
, 0.0f
));
214 default_view()->OnGestureEvent(&tap
);
215 EXPECT_TRUE(maximize_mode_controller
->rotation_locked());
216 EXPECT_TRUE(tray_view()->visible());
218 Shell::GetInstance()->maximize_mode_controller()->
219 EnableMaximizeModeWindowManager(false);