Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "content/public/browser/screen_orientation_provider.h" | |
| 6 | |
| 7 #include "base/optional.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "content/common/frame_messages.h" | |
| 10 #include "content/public/browser/screen_orientation_delegate.h" | |
| 11 #include "content/public/browser/web_contents_delegate.h" | |
| 12 #include "content/test/test_render_view_host.h" | |
| 13 #include "content/test/test_web_contents.h" | |
| 14 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScree nOrientationLockType.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 using device::mojom::ScreenOrientationLockResult; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 class FakeScreenOrientationDelegate : public ScreenOrientationDelegate { | |
| 23 public: | |
| 24 FakeScreenOrientationDelegate(bool supported, bool full_screen_required) | |
| 25 : supported_(supported), full_screen_required_(full_screen_required) { | |
| 26 ScreenOrientationProvider::SetDelegate(this); | |
| 27 } | |
| 28 | |
| 29 ~FakeScreenOrientationDelegate() override {} | |
| 30 | |
| 31 bool FullScreenRequired(WebContents* web_contents) override { | |
| 32 return full_screen_required_; | |
| 33 } | |
| 34 | |
| 35 bool ScreenOrientationProviderSupported() override { return supported_; } | |
| 36 | |
| 37 void Lock(WebContents* web_contents, | |
| 38 blink::WebScreenOrientationLockType lock_orientation) override { | |
| 39 lock_count_++; | |
|
leonhsl(Using Gerrit)
2017/01/25 10:52:21
I can't find a way to enable the fake screen orien
| |
| 40 } | |
| 41 | |
| 42 void Unlock(WebContents* web_contents) override { unlock_count_++; } | |
| 43 | |
| 44 int lock_count() { return lock_count_; } | |
| 45 int unlock_count() { return unlock_count_; } | |
| 46 | |
| 47 private: | |
| 48 bool supported_ = true; | |
| 49 bool full_screen_required_ = false; | |
| 50 | |
| 51 int lock_count_ = 0; | |
| 52 int unlock_count_ = 0; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(FakeScreenOrientationDelegate); | |
| 55 }; | |
| 56 | |
| 57 class FakeWebContentsDelegate : public WebContentsDelegate { | |
| 58 public: | |
| 59 FakeWebContentsDelegate() : fullscreened_contents_(nullptr) {} | |
| 60 ~FakeWebContentsDelegate() override {} | |
| 61 | |
| 62 void EnterFullscreenModeForTab(WebContents* web_contents, | |
| 63 const GURL& origin) override { | |
| 64 fullscreened_contents_ = web_contents; | |
| 65 } | |
| 66 | |
| 67 void ExitFullscreenModeForTab(WebContents* web_contents) override { | |
| 68 fullscreened_contents_ = nullptr; | |
| 69 } | |
| 70 | |
| 71 bool IsFullscreenForTabOrPending( | |
| 72 const WebContents* web_contents) const override { | |
| 73 return fullscreened_contents_ && web_contents == fullscreened_contents_; | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 WebContents* fullscreened_contents_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(FakeWebContentsDelegate); | |
| 80 }; | |
| 81 | |
| 82 void LockResultCallback(base::Optional<ScreenOrientationLockResult>* out_result, | |
| 83 ScreenOrientationLockResult result) { | |
| 84 *out_result = result; | |
| 85 } | |
| 86 | |
| 87 } // namespace | |
| 88 | |
| 89 class ScreenOrientationProviderTest : public RenderViewHostImplTestHarness { | |
| 90 public: | |
| 91 ScreenOrientationProviderTest() {} | |
| 92 | |
| 93 void SetUp() override { | |
| 94 content::RenderViewHostImplTestHarness::SetUp(); | |
| 95 | |
| 96 contents()->SetDelegate(&wc_delegate_); | |
| 97 } | |
| 98 | |
| 99 // Helpers for testing ScreenOrientationProvider methods. | |
| 100 // |expected| may be null to indicate expecting no any result back. | |
| 101 void CallLockAndExpect( | |
| 102 blink::WebScreenOrientationLockType orientation, | |
| 103 const base::Optional<ScreenOrientationLockResult>& expected) { | |
| 104 base::Optional<ScreenOrientationLockResult> lock_result; | |
| 105 contents()->GetScreenOrientationProvider()->LockOrientation( | |
| 106 orientation, base::Bind(&LockResultCallback, &lock_result)); | |
| 107 | |
| 108 base::RunLoop().RunUntilIdle(); | |
| 109 EXPECT_EQ(expected, lock_result); | |
| 110 } | |
| 111 | |
| 112 void CallLockAndGetResult( | |
| 113 blink::WebScreenOrientationLockType orientation, | |
| 114 base::Optional<ScreenOrientationLockResult>* out_result) { | |
| 115 contents()->GetScreenOrientationProvider()->LockOrientation( | |
| 116 orientation, base::Bind(&LockResultCallback, out_result)); | |
| 117 | |
| 118 base::RunLoop().RunUntilIdle(); | |
| 119 } | |
| 120 | |
| 121 void CallUnlock() { | |
| 122 contents()->GetScreenOrientationProvider()->UnlockOrientation(); | |
| 123 } | |
| 124 | |
| 125 private: | |
| 126 FakeWebContentsDelegate wc_delegate_; | |
| 127 }; | |
| 128 | |
| 129 // Lock operation is not available. | |
| 130 TEST_F(ScreenOrientationProviderTest, DelegateNotAvailableLockOnce) { | |
| 131 // No ScreenOrientationDelegate. | |
| 132 CallLockAndExpect(blink::WebScreenOrientationLockType:: | |
| 133 WebScreenOrientationLockPortraitPrimary, | |
| 134 ScreenOrientationLockResult:: | |
| 135 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); | |
| 136 | |
| 137 // ScreenOrientationDelegate not supported. | |
| 138 FakeScreenOrientationDelegate delegate(false, false); | |
| 139 CallLockAndExpect(blink::WebScreenOrientationLockType:: | |
| 140 WebScreenOrientationLockPortraitPrimary, | |
| 141 ScreenOrientationLockResult:: | |
| 142 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); | |
| 143 } | |
| 144 | |
| 145 // Full screen is not required by delegate, normally lock once. | |
| 146 TEST_F(ScreenOrientationProviderTest, DelegateLockOnce) { | |
| 147 // ScreenOrientationDelegate does not require full screen. | |
| 148 FakeScreenOrientationDelegate delegate(true, false); | |
| 149 | |
| 150 // Navigate to a site. | |
| 151 const GURL url("http://www.google.com"); | |
| 152 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 153 std::string()); | |
| 154 | |
| 155 CallLockAndExpect(blink::WebScreenOrientationLockType:: | |
| 156 WebScreenOrientationLockPortraitPrimary, | |
| 157 base::Optional<ScreenOrientationLockResult>()); | |
| 158 // Delegate did apply lock once. | |
| 159 EXPECT_EQ(1, delegate.lock_count()); | |
| 160 } | |
| 161 | |
| 162 // Full screen is required by delegate. | |
| 163 TEST_F(ScreenOrientationProviderTest, DelegateRequireFullScreenLockOnce) { | |
| 164 // ScreenOrientationDelegate requires full screen. | |
| 165 FakeScreenOrientationDelegate delegate(true, true); | |
| 166 | |
| 167 // Navigate to a site. | |
| 168 const GURL url("http://www.google.com"); | |
| 169 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 170 std::string()); | |
| 171 | |
| 172 // Current web contents is not in full screen. | |
| 173 ASSERT_FALSE(contents()->IsFullscreenForCurrentTab()); | |
| 174 CallLockAndExpect( | |
| 175 blink::WebScreenOrientationLockType:: | |
| 176 WebScreenOrientationLockPortraitPrimary, | |
| 177 ScreenOrientationLockResult:: | |
| 178 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); | |
| 179 // Delegate did not apply any lock. | |
| 180 EXPECT_EQ(0, delegate.lock_count()); | |
| 181 | |
| 182 // Simulates entering full screen. | |
| 183 main_test_rfh()->OnMessageReceived( | |
| 184 FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), true)); | |
| 185 ASSERT_TRUE(contents()->IsFullscreenForCurrentTab()); | |
| 186 CallLockAndExpect(blink::WebScreenOrientationLockType:: | |
| 187 WebScreenOrientationLockPortraitPrimary, | |
| 188 base::Optional<ScreenOrientationLockResult>()); | |
| 189 // Delegate did apply lock once. | |
| 190 EXPECT_EQ(1, delegate.lock_count()); | |
| 191 } | |
| 192 | |
| 193 // Lock once, then unlock once, the pending lock request will be cancelled. | |
| 194 TEST_F(ScreenOrientationProviderTest, DelegateLockThenUnlock) { | |
| 195 FakeScreenOrientationDelegate delegate(true, false); | |
| 196 | |
| 197 // Navigate to a site. | |
| 198 const GURL url("http://www.google.com"); | |
| 199 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 200 std::string()); | |
| 201 | |
| 202 base::Optional<ScreenOrientationLockResult> result_1; | |
| 203 CallLockAndGetResult(blink::WebScreenOrientationLockType:: | |
| 204 WebScreenOrientationLockPortraitPrimary, | |
| 205 &result_1); | |
| 206 // The lock request will be pending. | |
| 207 EXPECT_FALSE(result_1); | |
| 208 // Delegate did apply lock once. | |
| 209 EXPECT_EQ(1, delegate.lock_count()); | |
| 210 EXPECT_EQ(0, delegate.unlock_count()); | |
| 211 | |
| 212 CallUnlock(); | |
| 213 // The pending lock request is cancelled. | |
| 214 EXPECT_EQ(ScreenOrientationLockResult:: | |
| 215 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED, | |
| 216 result_1); | |
| 217 // Delegate did apply unlock once. | |
| 218 EXPECT_EQ(1, delegate.unlock_count()); | |
| 219 } | |
| 220 | |
| 221 // Lock twice, the first lock request will be cancelled by the second one. | |
| 222 TEST_F(ScreenOrientationProviderTest, DelegateLockThenLock) { | |
| 223 FakeScreenOrientationDelegate delegate(true, false); | |
| 224 | |
| 225 // Navigate to a site. | |
| 226 const GURL url("http://www.google.com"); | |
| 227 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 228 std::string()); | |
| 229 | |
| 230 base::Optional<ScreenOrientationLockResult> result_1; | |
| 231 CallLockAndGetResult(blink::WebScreenOrientationLockType:: | |
| 232 WebScreenOrientationLockPortraitPrimary, | |
| 233 &result_1); | |
| 234 // The lock request will be pending. | |
| 235 EXPECT_FALSE(result_1); | |
| 236 // Delegate did apply lock once. | |
| 237 EXPECT_EQ(1, delegate.lock_count()); | |
| 238 EXPECT_EQ(0, delegate.unlock_count()); | |
| 239 | |
| 240 base::Optional<ScreenOrientationLockResult> result_2; | |
| 241 CallLockAndGetResult(blink::WebScreenOrientationLockType:: | |
| 242 WebScreenOrientationLockPortraitPrimary, | |
| 243 &result_2); | |
| 244 // The pending lock request is cancelled. | |
| 245 EXPECT_EQ(ScreenOrientationLockResult:: | |
| 246 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED, | |
| 247 result_1); | |
| 248 // The second one became pending. | |
| 249 EXPECT_FALSE(result_2); | |
| 250 // Delegate did apply lock once more. | |
| 251 EXPECT_EQ(2, delegate.lock_count()); | |
| 252 EXPECT_EQ(0, delegate.unlock_count()); | |
| 253 } | |
| 254 | |
| 255 // Unlock won't be applied if no lock has been applied previously. | |
| 256 TEST_F(ScreenOrientationProviderTest, NoUnlockWithoutLock) { | |
| 257 FakeScreenOrientationDelegate delegate(true, false); | |
| 258 | |
| 259 // Navigate to a site. | |
| 260 const GURL url("http://www.google.com"); | |
| 261 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 262 std::string()); | |
| 263 | |
| 264 CallUnlock(); | |
| 265 // Delegate did not apply any unlock. | |
| 266 EXPECT_EQ(0, delegate.unlock_count()); | |
| 267 } | |
| 268 | |
| 269 // If lock already applied once in full screen, then unlock should be triggered | |
| 270 // once automatically when exiting full screen. | |
| 271 TEST_F(ScreenOrientationProviderTest, UnlockWhenExitingFullScreen) { | |
| 272 // ScreenOrientationDelegate requires full screen. | |
| 273 FakeScreenOrientationDelegate delegate(true, true); | |
| 274 | |
| 275 // Navigate to a site. | |
| 276 const GURL url("http://www.google.com"); | |
| 277 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 278 std::string()); | |
| 279 | |
| 280 // Simulates entering full screen. | |
| 281 main_test_rfh()->OnMessageReceived( | |
| 282 FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), true)); | |
| 283 ASSERT_TRUE(contents()->IsFullscreenForCurrentTab()); | |
| 284 CallLockAndExpect(blink::WebScreenOrientationLockType:: | |
| 285 WebScreenOrientationLockPortraitPrimary, | |
| 286 base::Optional<ScreenOrientationLockResult>()); | |
| 287 // Delegate did apply lock once. | |
| 288 EXPECT_EQ(1, delegate.lock_count()); | |
| 289 EXPECT_EQ(0, delegate.unlock_count()); | |
| 290 | |
| 291 // Simulates exiting full screen. | |
| 292 main_test_rfh()->OnMessageReceived( | |
| 293 FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), false)); | |
| 294 ASSERT_FALSE(contents()->IsFullscreenForCurrentTab()); | |
| 295 // Delegate did apply unlock once. | |
| 296 EXPECT_EQ(1, delegate.unlock_count()); | |
| 297 } | |
| 298 | |
| 299 // If lock already applied once, then unlock should be triggered once | |
| 300 // automatically when navigating to other web page. | |
| 301 TEST_F(ScreenOrientationProviderTest, UnlockWhenNavigation) { | |
| 302 FakeScreenOrientationDelegate delegate(true, false); | |
| 303 | |
| 304 // Navigate to a site. | |
| 305 const GURL url("http://www.google.com"); | |
| 306 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 307 std::string()); | |
| 308 | |
| 309 CallLockAndExpect(blink::WebScreenOrientationLockType:: | |
| 310 WebScreenOrientationLockPortraitPrimary, | |
| 311 base::Optional<ScreenOrientationLockResult>()); | |
| 312 // Delegate did apply lock once. | |
| 313 EXPECT_EQ(1, delegate.lock_count()); | |
| 314 EXPECT_EQ(0, delegate.unlock_count()); | |
| 315 | |
| 316 // Navigate to another site. | |
| 317 const GURL another_url("http://www.google.com/abc.html"); | |
| 318 contents()->NavigateAndCommit(another_url); | |
| 319 // Delegate did apply unlock once. | |
| 320 EXPECT_EQ(1, delegate.unlock_count()); | |
| 321 } | |
| 322 | |
| 323 } // namespace content | |
| OLD | NEW |