| 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/browser/screen_orientation/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 = default; |
| 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_++; |
| 40 } |
| 41 |
| 42 void Unlock(WebContents* web_contents) override { unlock_count_++; } |
| 43 |
| 44 int lock_count() const { return lock_count_; } |
| 45 int unlock_count() const { 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() = default; |
| 60 ~FakeWebContentsDelegate() override = default; |
| 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_ = nullptr; |
| 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() = default; |
| 92 |
| 93 void SetUp() override { |
| 94 content::RenderViewHostImplTestHarness::SetUp(); |
| 95 |
| 96 contents()->SetDelegate(&wc_delegate_); |
| 97 } |
| 98 |
| 99 // Helpers for testing ScreenOrientationProvider methods. |
| 100 void CallLockAndGetResult( |
| 101 blink::WebScreenOrientationLockType orientation, |
| 102 base::Optional<ScreenOrientationLockResult>* out_result) { |
| 103 contents()->screen_orientation_provider_for_testing()->LockOrientation( |
| 104 orientation, base::Bind(&LockResultCallback, out_result)); |
| 105 |
| 106 base::RunLoop().RunUntilIdle(); |
| 107 } |
| 108 |
| 109 void CallUnlock() { |
| 110 contents()->screen_orientation_provider_for_testing()->UnlockOrientation(); |
| 111 } |
| 112 |
| 113 private: |
| 114 FakeWebContentsDelegate wc_delegate_; |
| 115 }; |
| 116 |
| 117 // Lock operation is not available. |
| 118 TEST_F(ScreenOrientationProviderTest, DelegateNotAvailableLockOnce) { |
| 119 // No ScreenOrientationDelegate. |
| 120 base::Optional<ScreenOrientationLockResult> result_1; |
| 121 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 122 WebScreenOrientationLockLandscapeSecondary, |
| 123 &result_1); |
| 124 EXPECT_EQ(ScreenOrientationLockResult:: |
| 125 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE, |
| 126 *result_1); |
| 127 |
| 128 // ScreenOrientationDelegate not supported. |
| 129 FakeScreenOrientationDelegate delegate(false, false); |
| 130 base::Optional<ScreenOrientationLockResult> result_2; |
| 131 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 132 WebScreenOrientationLockLandscapeSecondary, |
| 133 &result_2); |
| 134 EXPECT_EQ(ScreenOrientationLockResult:: |
| 135 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE, |
| 136 *result_2); |
| 137 } |
| 138 |
| 139 // Full screen is not required by delegate, normally lock once. |
| 140 TEST_F(ScreenOrientationProviderTest, DelegateLockOnce) { |
| 141 // ScreenOrientationDelegate does not require full screen. |
| 142 FakeScreenOrientationDelegate delegate(true, false); |
| 143 |
| 144 // Navigate to a site. |
| 145 const GURL url("http://www.google.com"); |
| 146 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 147 std::string()); |
| 148 |
| 149 base::Optional<ScreenOrientationLockResult> result_1; |
| 150 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 151 WebScreenOrientationLockLandscapeSecondary, |
| 152 &result_1); |
| 153 // Lock request is pending. |
| 154 EXPECT_FALSE(result_1.has_value()); |
| 155 // Delegate did apply lock once. |
| 156 EXPECT_EQ(1, delegate.lock_count()); |
| 157 } |
| 158 |
| 159 // Full screen is required by delegate. |
| 160 TEST_F(ScreenOrientationProviderTest, DelegateRequireFullScreenLockOnce) { |
| 161 // ScreenOrientationDelegate requires full screen. |
| 162 FakeScreenOrientationDelegate delegate(true, true); |
| 163 |
| 164 // Navigate to a site. |
| 165 const GURL url("http://www.google.com"); |
| 166 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 167 std::string()); |
| 168 |
| 169 // Current web contents is not in full screen. |
| 170 ASSERT_FALSE(contents()->IsFullscreenForCurrentTab()); |
| 171 base::Optional<ScreenOrientationLockResult> result_1; |
| 172 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 173 WebScreenOrientationLockLandscapeSecondary, |
| 174 &result_1); |
| 175 EXPECT_EQ(ScreenOrientationLockResult:: |
| 176 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED, |
| 177 *result_1); |
| 178 // Delegate did not apply any lock. |
| 179 EXPECT_EQ(0, delegate.lock_count()); |
| 180 |
| 181 // Simulates entering full screen. |
| 182 main_test_rfh()->OnMessageReceived( |
| 183 FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), true)); |
| 184 ASSERT_TRUE(contents()->IsFullscreenForCurrentTab()); |
| 185 |
| 186 base::Optional<ScreenOrientationLockResult> result_2; |
| 187 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 188 WebScreenOrientationLockLandscapeSecondary, |
| 189 &result_2); |
| 190 // Lock request is pending. |
| 191 EXPECT_FALSE(result_2.has_value()); |
| 192 // Delegate did apply lock once. |
| 193 EXPECT_EQ(1, delegate.lock_count()); |
| 194 } |
| 195 |
| 196 // Lock once, then unlock once, the pending lock request will be cancelled. |
| 197 TEST_F(ScreenOrientationProviderTest, DelegateLockThenUnlock) { |
| 198 FakeScreenOrientationDelegate delegate(true, false); |
| 199 |
| 200 // Navigate to a site. |
| 201 const GURL url("http://www.google.com"); |
| 202 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 203 std::string()); |
| 204 |
| 205 base::Optional<ScreenOrientationLockResult> result_1; |
| 206 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 207 WebScreenOrientationLockLandscapeSecondary, |
| 208 &result_1); |
| 209 // The lock request will be pending. |
| 210 EXPECT_FALSE(result_1.has_value()); |
| 211 // Delegate did apply lock once. |
| 212 EXPECT_EQ(1, delegate.lock_count()); |
| 213 EXPECT_EQ(0, delegate.unlock_count()); |
| 214 |
| 215 CallUnlock(); |
| 216 // The pending lock request is cancelled. |
| 217 EXPECT_EQ(ScreenOrientationLockResult:: |
| 218 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED, |
| 219 result_1); |
| 220 // Delegate did apply unlock once. |
| 221 EXPECT_EQ(1, delegate.unlock_count()); |
| 222 } |
| 223 |
| 224 // Lock twice, the first lock request will be cancelled by the second one. |
| 225 TEST_F(ScreenOrientationProviderTest, DelegateLockThenLock) { |
| 226 FakeScreenOrientationDelegate delegate(true, false); |
| 227 |
| 228 // Navigate to a site. |
| 229 const GURL url("http://www.google.com"); |
| 230 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 231 std::string()); |
| 232 |
| 233 base::Optional<ScreenOrientationLockResult> result_1; |
| 234 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 235 WebScreenOrientationLockLandscapeSecondary, |
| 236 &result_1); |
| 237 // The lock request will be pending. |
| 238 EXPECT_FALSE(result_1.has_value()); |
| 239 // Delegate did apply lock once. |
| 240 EXPECT_EQ(1, delegate.lock_count()); |
| 241 EXPECT_EQ(0, delegate.unlock_count()); |
| 242 |
| 243 base::Optional<ScreenOrientationLockResult> result_2; |
| 244 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 245 WebScreenOrientationLockLandscapeSecondary, |
| 246 &result_2); |
| 247 // The pending lock request is cancelled. |
| 248 EXPECT_EQ(ScreenOrientationLockResult:: |
| 249 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED, |
| 250 result_1); |
| 251 // The second one became pending. |
| 252 EXPECT_FALSE(result_2.has_value()); |
| 253 // Delegate did apply lock once more. |
| 254 EXPECT_EQ(2, delegate.lock_count()); |
| 255 EXPECT_EQ(0, delegate.unlock_count()); |
| 256 } |
| 257 |
| 258 // Unlock won't be applied if no lock has been applied previously. |
| 259 TEST_F(ScreenOrientationProviderTest, NoUnlockWithoutLock) { |
| 260 FakeScreenOrientationDelegate delegate(true, false); |
| 261 |
| 262 // Navigate to a site. |
| 263 const GURL url("http://www.google.com"); |
| 264 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 265 std::string()); |
| 266 |
| 267 CallUnlock(); |
| 268 // Delegate did not apply any unlock. |
| 269 EXPECT_EQ(0, delegate.unlock_count()); |
| 270 } |
| 271 |
| 272 // If lock already applied once in full screen, then unlock should be triggered |
| 273 // once automatically when exiting full screen, and previous pending lock |
| 274 // request will be cancelled. |
| 275 TEST_F(ScreenOrientationProviderTest, UnlockWhenExitingFullScreen) { |
| 276 // ScreenOrientationDelegate requires full screen. |
| 277 FakeScreenOrientationDelegate delegate(true, true); |
| 278 |
| 279 // Navigate to a site. |
| 280 const GURL url("http://www.google.com"); |
| 281 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 282 std::string()); |
| 283 |
| 284 // Simulates entering full screen. |
| 285 main_test_rfh()->OnMessageReceived( |
| 286 FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), true)); |
| 287 ASSERT_TRUE(contents()->IsFullscreenForCurrentTab()); |
| 288 |
| 289 base::Optional<ScreenOrientationLockResult> result; |
| 290 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 291 WebScreenOrientationLockLandscapeSecondary, |
| 292 &result); |
| 293 // The lock request will be pending. |
| 294 EXPECT_FALSE(result.has_value()); |
| 295 // Delegate did apply lock once. |
| 296 EXPECT_EQ(1, delegate.lock_count()); |
| 297 EXPECT_EQ(0, delegate.unlock_count()); |
| 298 |
| 299 // Simulates exiting full screen. |
| 300 main_test_rfh()->OnMessageReceived( |
| 301 FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), false)); |
| 302 ASSERT_FALSE(contents()->IsFullscreenForCurrentTab()); |
| 303 // The pending lock request is cancelled. |
| 304 EXPECT_EQ(ScreenOrientationLockResult:: |
| 305 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED, |
| 306 result); |
| 307 // Delegate did apply unlock once. |
| 308 EXPECT_EQ(1, delegate.unlock_count()); |
| 309 } |
| 310 |
| 311 // If lock already applied once, then unlock should be triggered once |
| 312 // automatically when navigating to other web page, and previous pending lock |
| 313 // request will be cancelled. |
| 314 TEST_F(ScreenOrientationProviderTest, UnlockWhenNavigation) { |
| 315 FakeScreenOrientationDelegate delegate(true, false); |
| 316 |
| 317 // Navigate to a site. |
| 318 const GURL url("http://www.google.com"); |
| 319 controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 320 std::string()); |
| 321 |
| 322 base::Optional<ScreenOrientationLockResult> result; |
| 323 CallLockAndGetResult(blink::WebScreenOrientationLockType:: |
| 324 WebScreenOrientationLockLandscapeSecondary, |
| 325 &result); |
| 326 // The lock request will be pending. |
| 327 EXPECT_FALSE(result.has_value()); |
| 328 // Delegate did apply lock once. |
| 329 EXPECT_EQ(1, delegate.lock_count()); |
| 330 EXPECT_EQ(0, delegate.unlock_count()); |
| 331 |
| 332 // Navigate to another site. |
| 333 const GURL another_url("http://www.google.com/abc.html"); |
| 334 contents()->NavigateAndCommit(another_url); |
| 335 // The pending lock request is cancelled. |
| 336 EXPECT_EQ(ScreenOrientationLockResult:: |
| 337 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED, |
| 338 result); |
| 339 // Delegate did apply unlock once. |
| 340 EXPECT_EQ(1, delegate.unlock_count()); |
| 341 } |
| 342 |
| 343 } // namespace content |
| OLD | NEW |