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.
4
5#include "ash/system/chromeos/rotation/tray_rotation_lock.h"
6
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"
24
25namespace ash {
26
27class TrayRotationLockTest : public test::AshTestBase {
28 public:
29  TrayRotationLockTest() {}
30  virtual ~TrayRotationLockTest() {}
31
32  TrayRotationLock* tray() {
33    return tray_.get();
34  }
35
36  views::View* tray_view() {
37    return tray_view_.get();
38  }
39
40  views::View* default_view() {
41    return default_view_.get();
42  }
43
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
47  // be null.
48  void SetUpForStatusAreaWidget(StatusAreaWidget* status_area_widget);
49
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.
53  void TearDownViews();
54
55  // test::AshTestBase:
56  virtual void SetUp() OVERRIDE;
57  virtual void TearDown() OVERRIDE;
58
59 private:
60  scoped_ptr<TrayRotationLock> tray_;
61  scoped_ptr<views::View> tray_view_;
62  scoped_ptr<views::View> default_view_;
63
64  DISALLOW_COPY_AND_ASSIGN(TrayRotationLockTest);
65};
66
67void 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()));
74}
75
76void TrayRotationLockTest::TearDownViews() {
77  tray_view_.reset();
78  default_view_.reset();
79  tray_.reset();
80}
81
82void 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());
90}
91
92void TrayRotationLockTest::TearDown() {
93  TearDownViews();
94  test::AshTestBase::TearDown();
95}
96
97// Tests that when the tray view is initially created, that it is created
98// not visible.
99TEST_F(TrayRotationLockTest, CreateTrayView) {
100  EXPECT_FALSE(tray_view()->visible());
101}
102
103// Tests that when the tray view is created, while MaximizeMode is active, that
104// it is not visible.
105TEST_F(TrayRotationLockTest, CreateTrayViewDuringMaximizeMode) {
106  TearDownViews();
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);
113}
114
115// Tests that when the tray view is created, while MaximizeMode is active, and
116// rotation is locked, that it is visible.
117TEST_F(TrayRotationLockTest, CreateTrayViewDuringMaximizeModeAndRotationLock) {
118  TearDownViews();
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());
127}
128
129// Tests that the enabling of MaximizeMode affects a previously created tray
130// view, changing the visibility.
131TEST_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());
140}
141
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.
144TEST_F(TrayRotationLockTest, CreateSecondaryTrayView) {
145  if (!SupportsMultipleDisplays())
146    return;
147  UpdateDisplay("400x400,200x200");
148
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());
158}
159
160// Tests that when the default view is initially created, that it is created
161// not visible.
162TEST_F(TrayRotationLockTest, CreateDefaultView) {
163  EXPECT_FALSE(default_view()->visible());
164}
165
166// Tests that when the default view is created, while MaximizeMode is active,
167// that it is visible.
168TEST_F(TrayRotationLockTest, CreateDefaultViewDuringMaximizeMode) {
169  TearDownViews();
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);
176}
177
178// Tests that the enabling of MaximizeMode affects a previously created default
179// view, changing the visibility.
180TEST_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());
187}
188
189// Tests that no default view is created when the target is a secondary
190// display.
191TEST_F(TrayRotationLockTest, CreateSecondaryDefaultView) {
192  if (!SupportsMultipleDisplays())
193    return;
194  UpdateDisplay("400x400,200x200");
195
196  TearDownViews();
197  SetUpForStatusAreaWidget(
198      StatusAreaWidgetTestHelper::GetSecondaryStatusAreaWidget());
199  EXPECT_EQ(NULL, default_view());
200}
201
202// Tests that activating the default view causes the display to have its
203// rotation locked, and that the tray view becomes visible.
204TEST_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());
211
212  ui::GestureEvent tap(
213      0, 0, 0, base::TimeDelta(), ui::GestureEventDetails(ui::ET_GESTURE_TAP));
214  default_view()->OnGestureEvent(&tap);
215  EXPECT_TRUE(maximize_mode_controller->rotation_locked());
216  EXPECT_TRUE(tray_view()->visible());
217
218  Shell::GetInstance()->maximize_mode_controller()->
219      EnableMaximizeModeWindowManager(false);
220}
221
222}  // namespace ash
223