summaryrefslogtreecommitdiffstats
path: root/ash/content/display/screen_orientation_delegate_chromeos_unittest.cc
blob: 28d9bda1f5074854de37f1165bdbcc132967df4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/ash_switches.h"
#include "ash/content/display/screen_orientation_delegate_chromeos.h"
#include "ash/display/display_info.h"
#include "ash/display/display_manager.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.h"
#include "ash/test/test_shell_delegate.h"
#include "ash/wm/maximize_mode/maximize_mode_controller.h"
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_browser_context.h"
#include "third_party/WebKit/public/platform/WebScreenOrientationLockType.h"
#include "ui/gfx/display.h"
#include "ui/views/test/webview_test_helper.h"
#include "ui/views/view.h"
#include "ui/views/views_delegate.h"

namespace ash {

namespace {

gfx::Display::Rotation Rotation() {
  return Shell::GetInstance()
      ->display_manager()
      ->GetDisplayInfo(gfx::Display::InternalDisplayId())
      .rotation();
}

bool RotationLocked() {
  return Shell::GetInstance()->maximize_mode_controller()->rotation_locked();
}

}  // namespace

class ScreenOrientationDelegateTest : public test::AshTestBase {
 public:
  ScreenOrientationDelegateTest();
  virtual ~ScreenOrientationDelegateTest();

  ScreenOrientationDelegate* delegate() { return screen_orientation_delegate_; }

  // Creates and initializes and empty content::WebContents that is backed by a
  // content::BrowserContext and that has an aura::Window.
  content::WebContents* CreateWebContents();

  // Creates a secondary content::WebContents, with a separate
  // content::BrowserContext.
  content::WebContents* CreateSecondaryWebContents();

  // test::AshTestBase:
  void SetUp() override;

 private:
  ScreenOrientationDelegate* screen_orientation_delegate_;

  // Optional content::BrowserContext used for two window tests.
  scoped_ptr<content::BrowserContext> secondary_browser_context_;

  // Setups underlying content layer so that content::WebContents can be
  // generated.
  scoped_ptr<views::WebViewTestHelper> webview_test_helper_;

  DISALLOW_COPY_AND_ASSIGN(ScreenOrientationDelegateTest);
};

ScreenOrientationDelegateTest::ScreenOrientationDelegateTest() {
  webview_test_helper_.reset(new views::WebViewTestHelper());
}

ScreenOrientationDelegateTest::~ScreenOrientationDelegateTest() {
}

content::WebContents* ScreenOrientationDelegateTest::CreateWebContents() {
  return views::ViewsDelegate::views_delegate->CreateWebContents(
      ash_test_helper()->test_shell_delegate()->GetActiveBrowserContext(),
      nullptr);
}

content::WebContents*
ScreenOrientationDelegateTest::CreateSecondaryWebContents() {
  secondary_browser_context_.reset(new content::TestBrowserContext());
  return views::ViewsDelegate::views_delegate->CreateWebContents(
      secondary_browser_context_.get(), nullptr);
}

void ScreenOrientationDelegateTest::SetUp() {
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kAshUseFirstDisplayAsInternal);
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kAshEnableTouchViewTesting);
  test::AshTestBase::SetUp();
  screen_orientation_delegate_ =
      Shell::GetInstance()->screen_orientation_delegate();
}

// Tests that a content::WebContents can lock rotation.
TEST_F(ScreenOrientationDelegateTest, LockOrientation) {
  scoped_ptr<content::WebContents> content(CreateWebContents());
  ASSERT_NE(nullptr, content->GetNativeView());
  ASSERT_EQ(gfx::Display::ROTATE_0, Rotation());
  ASSERT_FALSE(RotationLocked());

  delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
  EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
  EXPECT_TRUE(RotationLocked());
}

// Tests that a content::WebContents can unlock rotation.
TEST_F(ScreenOrientationDelegateTest, Unlock) {
  scoped_ptr<content::WebContents> content(CreateWebContents());
  ASSERT_NE(nullptr, content->GetNativeView());
  ASSERT_EQ(gfx::Display::ROTATE_0, Rotation());
  ASSERT_FALSE(RotationLocked());

  delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
  EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
  EXPECT_TRUE(RotationLocked());

  delegate()->Unlock(content.get());
  EXPECT_FALSE(RotationLocked());
}

// Tests that a content::WebContents is able to change the orientation of the
// display after having locked rotation.
TEST_F(ScreenOrientationDelegateTest, OrientationChanges) {
  scoped_ptr<content::WebContents> content(CreateWebContents());
  ASSERT_NE(nullptr, content->GetNativeView());
  ASSERT_EQ(gfx::Display::ROTATE_0, Rotation());
  ASSERT_FALSE(RotationLocked());

  delegate()->Lock(content.get(), blink::WebScreenOrientationLockPortrait);
  EXPECT_EQ(gfx::Display::ROTATE_90, Rotation());
  EXPECT_TRUE(RotationLocked());

  delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
  EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
}

// Tests that a user initiated rotation lock cannot be unlocked by a
// content::WebContents.
TEST_F(ScreenOrientationDelegateTest, UserLockRejectsUnlock) {
  Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(true);

  scoped_ptr<content::WebContents> content(CreateWebContents());
  delegate()->Unlock(content.get());
  EXPECT_TRUE(RotationLocked());
}

// Tests that orientation can only be set by the first content::WebContents that
// has set a rotation lock.
TEST_F(ScreenOrientationDelegateTest, SecondContentCannotChangeOrientation) {
  scoped_ptr<content::WebContents> content1(CreateWebContents());
  scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
  ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());

  delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
  delegate()->Lock(content2.get(), blink::WebScreenOrientationLockPortrait);
  EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
}

// Tests that only the content::WebContents that set a rotation lock can perform
// an unlock.
TEST_F(ScreenOrientationDelegateTest, SecondContentCannotUnlock) {
  scoped_ptr<content::WebContents> content1(CreateWebContents());
  scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
  ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());

  delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
  delegate()->Unlock(content2.get());
  EXPECT_TRUE(RotationLocked());
}

// Tests that alternate content::WebContents can set a rotation lock after a
// preexisting lock has been released.
TEST_F(ScreenOrientationDelegateTest, AfterUnlockSecondContentCanLock) {
  scoped_ptr<content::WebContents> content1(CreateWebContents());
  scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
  ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());

  delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
  delegate()->Unlock(content1.get());
  delegate()->Lock(content2.get(), blink::WebScreenOrientationLockPortrait);
  EXPECT_EQ(gfx::Display::ROTATE_90, Rotation());
  EXPECT_TRUE(RotationLocked());
}

}  // namespace ash