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()->EnableMaximizeModeWindowManager(true);
108 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
109 EXPECT_FALSE(tray_view()->visible());
110 Shell::GetInstance()->EnableMaximizeModeWindowManager(false);
113 // Tests that when the tray view is created, while MaximizeMode is active, and
114 // rotation is locked, that it is visible.
115 TEST_F(TrayRotationLockTest, CreateTrayViewDuringMaximizeModeAndRotationLock) {
117 Shell::GetInstance()->EnableMaximizeModeWindowManager(true);
118 Shell::GetInstance()-> maximize_mode_controller()->set_rotation_locked(true);
119 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
120 EXPECT_TRUE(tray_view()->visible());
121 Shell::GetInstance()->EnableMaximizeModeWindowManager(false);
124 // Tests that the enabling of MaximizeMode affects a previously created tray
125 // view, changing the visibility.
126 TEST_F(TrayRotationLockTest, TrayViewVisibilityChangesDuringMaximizeMode) {
128 Shell::GetInstance()->EnableMaximizeModeWindowManager(true);
129 Shell::GetInstance()-> maximize_mode_controller()->set_rotation_locked(true);
130 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
131 EXPECT_TRUE(tray_view()->visible());
132 Shell::GetInstance()->EnableMaximizeModeWindowManager(false);
133 EXPECT_FALSE(tray_view()->visible());
136 // Tests that the when the tray view is created for a secondary display, that
137 // it is not visible, and that MaximizeMode does not affect visibility.
138 TEST_F(TrayRotationLockTest, CreateSecondaryTrayView) {
139 if (!SupportsMultipleDisplays())
141 UpdateDisplay("400x400,200x200");
143 SetUpForStatusAreaWidget(
144 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
145 EXPECT_FALSE(tray_view()->visible());
146 Shell::GetInstance()->EnableMaximizeModeWindowManager(true);
147 EXPECT_FALSE(tray_view()->visible());
148 Shell::GetInstance()->EnableMaximizeModeWindowManager(false);
149 EXPECT_FALSE(tray_view()->visible());
152 // Tests that when the default view is initially created, that it is created
154 TEST_F(TrayRotationLockTest, CreateDefaultView) {
155 EXPECT_FALSE(default_view()->visible());
158 // Tests that when the default view is created, while MaximizeMode is active,
159 // that it is visible.
160 TEST_F(TrayRotationLockTest, CreateDefaultViewDuringMaximizeMode) {
162 Shell::GetInstance()->EnableMaximizeModeWindowManager(true);
163 SetUpForStatusAreaWidget(StatusAreaWidgetTestHelper::GetStatusAreaWidget());
164 EXPECT_TRUE(default_view()->visible());
165 Shell::GetInstance()->EnableMaximizeModeWindowManager(false);
168 // Tests that the enabling of MaximizeMode affects a previously created default
169 // view, changing the visibility.
170 TEST_F(TrayRotationLockTest, DefaultViewVisibilityChangesDuringMaximizeMode) {
171 Shell::GetInstance()->EnableMaximizeModeWindowManager(true);
172 EXPECT_TRUE(default_view()->visible());
173 Shell::GetInstance()->EnableMaximizeModeWindowManager(false);
174 EXPECT_FALSE(default_view()->visible());
177 // Tests that no default view is created when the target is a secondary
179 TEST_F(TrayRotationLockTest, CreateSecondaryDefaultView) {
180 if (!SupportsMultipleDisplays())
182 UpdateDisplay("400x400,200x200");
185 SetUpForStatusAreaWidget(
186 StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
187 EXPECT_EQ(NULL, default_view());
190 // Tests that activating the default view causes the display to have its
191 // rotation locked, and that the tray view becomes visible.
192 TEST_F(TrayRotationLockTest, PerformActionOnDefaultView) {
193 MaximizeModeController* maximize_mode_controller = Shell::GetInstance()->
194 maximize_mode_controller();
195 ASSERT_FALSE(maximize_mode_controller->rotation_locked());
196 Shell::GetInstance()->EnableMaximizeModeWindowManager(true);
197 ASSERT_FALSE(tray_view()->visible());
199 ui::GestureEvent tap(ui::ET_GESTURE_TAP, 0, 0, 0, base::TimeDelta(),
200 ui::GestureEventDetails(ui::ET_GESTURE_TAP, 0.0f, 0.0f), 0);
201 default_view()->OnGestureEvent(&tap);
202 EXPECT_TRUE(maximize_mode_controller->rotation_locked());
203 EXPECT_TRUE(tray_view()->visible());
205 Shell::GetInstance()->EnableMaximizeModeWindowManager(false);