| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.pm.ActivityInfo; | 7 import android.content.pm.ActivityInfo; |
| 8 import android.graphics.Rect; | 8 import android.graphics.Rect; |
| 9 import android.support.test.InstrumentationRegistry; |
| 9 import android.support.test.filters.MediumTest; | 10 import android.support.test.filters.MediumTest; |
| 10 | 11 |
| 12 import org.junit.Assert; |
| 13 import org.junit.Before; |
| 14 import org.junit.Rule; |
| 15 import org.junit.Test; |
| 16 import org.junit.runner.RunWith; |
| 17 |
| 11 import org.chromium.base.test.util.CommandLineFlags; | 18 import org.chromium.base.test.util.CommandLineFlags; |
| 12 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
| 20 import org.chromium.content.browser.test.ContentJUnit4ClassRunner; |
| 13 import org.chromium.content.browser.test.util.Criteria; | 21 import org.chromium.content.browser.test.util.Criteria; |
| 14 import org.chromium.content.browser.test.util.CriteriaHelper; | 22 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 15 import org.chromium.content.browser.test.util.DOMUtils; | 23 import org.chromium.content.browser.test.util.DOMUtils; |
| 16 import org.chromium.content.browser.test.util.JavaScriptUtils; | 24 import org.chromium.content.browser.test.util.JavaScriptUtils; |
| 17 import org.chromium.content.browser.test.util.UiUtils; | 25 import org.chromium.content.browser.test.util.UiUtils; |
| 18 import org.chromium.content.common.ContentSwitches; | 26 import org.chromium.content.common.ContentSwitches; |
| 19 import org.chromium.content_shell_apk.ContentShellTestBase; | 27 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
| 20 import org.chromium.ui.base.DeviceFormFactor; | 28 import org.chromium.ui.base.DeviceFormFactor; |
| 21 | 29 |
| 22 import java.util.concurrent.Callable; | 30 import java.util.concurrent.Callable; |
| 23 import java.util.concurrent.TimeoutException; | 31 import java.util.concurrent.TimeoutException; |
| 24 | 32 |
| 25 /** | 33 /** |
| 26 * Integration tests for the feature that auto locks the orientation when a vide
o goes fullscreen. | 34 * Integration tests for the feature that auto locks the orientation when a vide
o goes fullscreen. |
| 27 * See also chrome layer org.chromium.chrome.browser.VideoFullscreenOrientationL
ockChromeTest | 35 * See also chrome layer org.chromium.chrome.browser.VideoFullscreenOrientationL
ockChromeTest |
| 28 */ | 36 ContentSwitches.ENABLE_TEST_INTENTS */ |
| 29 @CommandLineFlags.Add({ "enable-features=VideoFullscreenOrientationLock", | 37 @RunWith(ContentJUnit4ClassRunner.class) |
| 30 ContentSwitches.DISABLE_GESTURE_REQUIREMENT_FOR_MEDIA_PL
AYBACK }) | 38 @CommandLineFlags.Add({"enable-features=VideoFullscreenOrientationLock", |
| 31 public class VideoFullscreenOrientationLockTest extends ContentShellTestBase { | 39 ContentSwitches.DISABLE_GESTURE_REQUIREMENT_FOR_MEDIA_PLAYBACK, |
| 40 ContentSwitches.ENABLE_TEST_INTENTS}) |
| 41 public class VideoFullscreenOrientationLockTest { |
| 42 @Rule |
| 43 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
| 44 |
| 32 private static final String TEST_URL = "content/test/data/media/video-player
.html"; | 45 private static final String TEST_URL = "content/test/data/media/video-player
.html"; |
| 33 private static final String VIDEO_ID = "video"; | 46 private static final String VIDEO_ID = "video"; |
| 34 | 47 |
| 35 private void waitForContentsFullscreenState(boolean fullscreenValue) | 48 private void waitForContentsFullscreenState(boolean fullscreenValue) { |
| 36 throws InterruptedException { | |
| 37 CriteriaHelper.pollInstrumentationThread( | 49 CriteriaHelper.pollInstrumentationThread( |
| 38 Criteria.equals(fullscreenValue, new Callable<Boolean>() { | 50 Criteria.equals(fullscreenValue, new Callable<Boolean>() { |
| 39 @Override | 51 @Override |
| 40 public Boolean call() throws InterruptedException, TimeoutEx
ception { | 52 public Boolean call() throws InterruptedException, TimeoutEx
ception { |
| 41 return DOMUtils.isFullscreen(getWebContents()); | 53 return DOMUtils.isFullscreen(mActivityTestRule.getWebCon
tents()); |
| 42 } | 54 } |
| 43 }) | 55 }) |
| 44 ); | 56 ); |
| 45 } | 57 } |
| 46 | 58 |
| 47 private boolean isScreenOrientationLocked() { | 59 private boolean isScreenOrientationLocked() { |
| 48 return getActivity().getRequestedOrientation() | 60 return mActivityTestRule.getActivity().getRequestedOrientation() |
| 49 != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; | 61 != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 50 } | 62 } |
| 51 | 63 |
| 52 private boolean isScreenOrientationLandscape() throws InterruptedException,
TimeoutException { | 64 private boolean isScreenOrientationLandscape() throws InterruptedException,
TimeoutException { |
| 53 StringBuilder sb = new StringBuilder(); | 65 StringBuilder sb = new StringBuilder(); |
| 54 sb.append("(function() {"); | 66 sb.append("(function() {"); |
| 55 sb.append(" return screen.orientation.type.startsWith('landscape');"); | 67 sb.append(" return screen.orientation.type.startsWith('landscape');"); |
| 56 sb.append("})();"); | 68 sb.append("})();"); |
| 57 | 69 |
| 58 return JavaScriptUtils.executeJavaScriptAndWaitForResult(getWebContents(
), sb.toString()) | 70 return JavaScriptUtils |
| 71 .executeJavaScriptAndWaitForResult( |
| 72 mActivityTestRule.getWebContents(), sb.toString()) |
| 59 .equals("true"); | 73 .equals("true"); |
| 60 } | 74 } |
| 61 | 75 |
| 62 private void waitUntilLockedToLandscape() throws InterruptedException { | 76 private void waitUntilLockedToLandscape() throws InterruptedException { |
| 63 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 77 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
| 64 @Override | 78 @Override |
| 65 public boolean isSatisfied() { | 79 public boolean isSatisfied() { |
| 66 try { | 80 try { |
| 67 return isScreenOrientationLocked() && isScreenOrientationLan
dscape(); | 81 return isScreenOrientationLocked() && isScreenOrientationLan
dscape(); |
| 68 } catch (InterruptedException e) { | 82 } catch (InterruptedException e) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 114 } |
| 101 | 115 |
| 102 private Rect fullscreenButtonBounds(Rect videoRect) { | 116 private Rect fullscreenButtonBounds(Rect videoRect) { |
| 103 Rect bar = controlBarBounds(videoRect); | 117 Rect bar = controlBarBounds(videoRect); |
| 104 int right = bar.right - BUTTON_RIGHT_MARGIN; | 118 int right = bar.right - BUTTON_RIGHT_MARGIN; |
| 105 int left = right - BUTTON_WIDTH; | 119 int left = right - BUTTON_WIDTH; |
| 106 return new Rect(left, bar.top, right, bar.bottom); | 120 return new Rect(left, bar.top, right, bar.bottom); |
| 107 } | 121 } |
| 108 | 122 |
| 109 private boolean clickFullscreenButton() throws InterruptedException, Timeout
Exception { | 123 private boolean clickFullscreenButton() throws InterruptedException, Timeout
Exception { |
| 110 return DOMUtils.clickRect(getContentViewCore(), | 124 return DOMUtils.clickRect(mActivityTestRule.getContentViewCore(), |
| 111 fullscreenButtonBounds(DOMUtils.getNodeBounds(getWebContents(),
VIDEO_ID))); | 125 fullscreenButtonBounds( |
| 126 DOMUtils.getNodeBounds(mActivityTestRule.getWebContents(
), VIDEO_ID))); |
| 112 } | 127 } |
| 113 | 128 |
| 114 @Override | 129 @Before |
| 115 public void setUp() throws Exception { | 130 public void setUp() throws Exception { |
| 116 super.setUp(); | 131 mActivityTestRule.launchContentShellWithUrlSync(TEST_URL); |
| 117 | |
| 118 startActivityWithTestUrl(TEST_URL); | |
| 119 } | 132 } |
| 120 | 133 |
| 134 @Test |
| 121 @MediumTest | 135 @MediumTest |
| 122 @Feature({"VideoFullscreenOrientationLock"}) | 136 @Feature({"VideoFullscreenOrientationLock"}) |
| 123 public void testEnterExitFullscreenWithControlsButton() throws Exception { | 137 public void testEnterExitFullscreenWithControlsButton() throws Exception { |
| 124 if (DeviceFormFactor.isTablet(getInstrumentation().getContext())) return
; | 138 if (DeviceFormFactor.isTablet(InstrumentationRegistry.getInstrumentation
().getContext())) { |
| 139 return; |
| 140 } |
| 125 | 141 |
| 126 // Start playback to guarantee it's properly loaded. | 142 // Start playback to guarantee it's properly loaded. |
| 127 assertTrue(DOMUtils.isMediaPaused(getWebContents(), VIDEO_ID)); | 143 Assert.assertTrue(DOMUtils.isMediaPaused(mActivityTestRule.getWebContent
s(), VIDEO_ID)); |
| 128 DOMUtils.playMedia(getWebContents(), VIDEO_ID); | 144 DOMUtils.playMedia(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 129 DOMUtils.waitForMediaPlay(getWebContents(), VIDEO_ID); | 145 DOMUtils.waitForMediaPlay(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 130 | 146 |
| 131 // Simulate click on fullscreen button. | 147 // Simulate click on fullscreen button. |
| 132 assertTrue(clickFullscreenButton()); | 148 Assert.assertTrue(clickFullscreenButton()); |
| 133 waitForContentsFullscreenState(true); | 149 waitForContentsFullscreenState(true); |
| 134 | 150 |
| 135 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. | 151 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. |
| 136 waitUntilLockedToLandscape(); | 152 waitUntilLockedToLandscape(); |
| 137 | 153 |
| 138 // Because of the fullscreen animation, the click on the exit fullscreen
button will fail | 154 // Because of the fullscreen animation, the click on the exit fullscreen
button will fail |
| 139 // roughly 10% of the time. Settling down the UI reduces the flake to 0%
. | 155 // roughly 10% of the time. Settling down the UI reduces the flake to 0%
. |
| 140 UiUtils.settleDownUI(getInstrumentation()); | 156 UiUtils.settleDownUI(InstrumentationRegistry.getInstrumentation()); |
| 141 | 157 |
| 142 // Leave fullscreen by clicking back on the button. | 158 // Leave fullscreen by clicking back on the button. |
| 143 assertTrue(clickFullscreenButton()); | 159 Assert.assertTrue(clickFullscreenButton()); |
| 144 waitForContentsFullscreenState(false); | 160 waitForContentsFullscreenState(false); |
| 145 waitUntilUnlocked(); | 161 waitUntilUnlocked(); |
| 146 } | 162 } |
| 147 | 163 |
| 164 @Test |
| 148 @MediumTest | 165 @MediumTest |
| 149 @Feature({"VideoFullscreenOrientationLock"}) | 166 @Feature({"VideoFullscreenOrientationLock"}) |
| 150 public void testEnterExitFullscreenWithAPI() throws Exception { | 167 public void testEnterExitFullscreenWithAPI() throws Exception { |
| 151 if (DeviceFormFactor.isTablet(getInstrumentation().getContext())) return
; | 168 if (DeviceFormFactor.isTablet(InstrumentationRegistry.getInstrumentation
().getContext())) { |
| 169 return; |
| 170 } |
| 152 | 171 |
| 153 // Start playback to guarantee it's properly loaded. | 172 // Start playback to guarantee it's properly loaded. |
| 154 assertTrue(DOMUtils.isMediaPaused(getWebContents(), VIDEO_ID)); | 173 Assert.assertTrue(DOMUtils.isMediaPaused(mActivityTestRule.getWebContent
s(), VIDEO_ID)); |
| 155 DOMUtils.playMedia(getWebContents(), VIDEO_ID); | 174 DOMUtils.playMedia(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 156 DOMUtils.waitForMediaPlay(getWebContents(), VIDEO_ID); | 175 DOMUtils.waitForMediaPlay(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 157 | 176 |
| 158 // Trigger requestFullscreen() via a click on a button. | 177 // Trigger requestFullscreen() via a click on a button. |
| 159 assertTrue(DOMUtils.clickNode(getContentViewCore(), "fullscreen")); | 178 Assert.assertTrue(DOMUtils.clickNode(mActivityTestRule.getContentViewCor
e(), "fullscreen")); |
| 160 waitForContentsFullscreenState(true); | 179 waitForContentsFullscreenState(true); |
| 161 | 180 |
| 162 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. | 181 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. |
| 163 waitUntilLockedToLandscape(); | 182 waitUntilLockedToLandscape(); |
| 164 | 183 |
| 165 // Leave fullscreen from API. | 184 // Leave fullscreen from API. |
| 166 DOMUtils.exitFullscreen(getWebContents()); | 185 DOMUtils.exitFullscreen(mActivityTestRule.getWebContents()); |
| 167 waitForContentsFullscreenState(false); | 186 waitForContentsFullscreenState(false); |
| 168 waitUntilUnlocked(); | 187 waitUntilUnlocked(); |
| 169 } | 188 } |
| 170 | 189 |
| 190 @Test |
| 171 @MediumTest | 191 @MediumTest |
| 172 @Feature({"VideoFullscreenOrientationLock"}) | 192 @Feature({"VideoFullscreenOrientationLock"}) |
| 173 public void testExitFullscreenByRemovingVideo() throws Exception { | 193 public void testExitFullscreenByRemovingVideo() throws Exception { |
| 174 if (DeviceFormFactor.isTablet(getInstrumentation().getContext())) return
; | 194 if (DeviceFormFactor.isTablet(InstrumentationRegistry.getInstrumentation
().getContext())) { |
| 195 return; |
| 196 } |
| 175 | 197 |
| 176 // Start playback to guarantee it's properly loaded. | 198 // Start playback to guarantee it's properly loaded. |
| 177 assertTrue(DOMUtils.isMediaPaused(getWebContents(), VIDEO_ID)); | 199 Assert.assertTrue(DOMUtils.isMediaPaused(mActivityTestRule.getWebContent
s(), VIDEO_ID)); |
| 178 DOMUtils.playMedia(getWebContents(), VIDEO_ID); | 200 DOMUtils.playMedia(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 179 DOMUtils.waitForMediaPlay(getWebContents(), VIDEO_ID); | 201 DOMUtils.waitForMediaPlay(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 180 | 202 |
| 181 // Trigger requestFullscreen() via a click on a button. | 203 // Trigger requestFullscreen() via a click on a button. |
| 182 assertTrue(DOMUtils.clickNode(getContentViewCore(), "fullscreen")); | 204 Assert.assertTrue(DOMUtils.clickNode(mActivityTestRule.getContentViewCor
e(), "fullscreen")); |
| 183 waitForContentsFullscreenState(true); | 205 waitForContentsFullscreenState(true); |
| 184 | 206 |
| 185 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. | 207 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. |
| 186 waitUntilLockedToLandscape(); | 208 waitUntilLockedToLandscape(); |
| 187 | 209 |
| 188 // Leave fullscreen by removing video element from page. | 210 // Leave fullscreen by removing video element from page. |
| 189 JavaScriptUtils.executeJavaScriptAndWaitForResult( | 211 JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 190 getWebContents(), "document.body.innerHTML = '';"); | 212 mActivityTestRule.getWebContents(), "document.body.innerHTML = '
';"); |
| 191 waitForContentsFullscreenState(false); | 213 waitForContentsFullscreenState(false); |
| 192 waitUntilUnlocked(); | 214 waitUntilUnlocked(); |
| 193 } | 215 } |
| 194 | 216 |
| 217 @Test |
| 195 @MediumTest | 218 @MediumTest |
| 196 @Feature({"VideoFullscreenOrientationLock"}) | 219 @Feature({"VideoFullscreenOrientationLock"}) |
| 197 public void testExitFullscreenWithNavigation() throws Exception { | 220 public void testExitFullscreenWithNavigation() throws Exception { |
| 198 if (DeviceFormFactor.isTablet(getInstrumentation().getContext())) return
; | 221 if (DeviceFormFactor.isTablet(InstrumentationRegistry.getInstrumentation
().getContext())) { |
| 222 return; |
| 223 } |
| 199 | 224 |
| 200 // Start playback to guarantee it's properly loaded. | 225 // Start playback to guarantee it's properly loaded. |
| 201 assertTrue(DOMUtils.isMediaPaused(getWebContents(), VIDEO_ID)); | 226 Assert.assertTrue(DOMUtils.isMediaPaused(mActivityTestRule.getWebContent
s(), VIDEO_ID)); |
| 202 DOMUtils.playMedia(getWebContents(), VIDEO_ID); | 227 DOMUtils.playMedia(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 203 DOMUtils.waitForMediaPlay(getWebContents(), VIDEO_ID); | 228 DOMUtils.waitForMediaPlay(mActivityTestRule.getWebContents(), VIDEO_ID); |
| 204 | 229 |
| 205 // Trigger requestFullscreen() via a click on a button. | 230 // Trigger requestFullscreen() via a click on a button. |
| 206 assertTrue(DOMUtils.clickNode(getContentViewCore(), "fullscreen")); | 231 Assert.assertTrue(DOMUtils.clickNode(mActivityTestRule.getContentViewCor
e(), "fullscreen")); |
| 207 waitForContentsFullscreenState(true); | 232 waitForContentsFullscreenState(true); |
| 208 | 233 |
| 209 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. | 234 // Should be locked to landscape now, `waitUntilLockedToLandscape` will
throw otherwise. |
| 210 waitUntilLockedToLandscape(); | 235 waitUntilLockedToLandscape(); |
| 211 | 236 |
| 212 // Leave fullscreen by navigating page. | 237 // Leave fullscreen by navigating page. |
| 213 JavaScriptUtils.executeJavaScriptAndWaitForResult(getWebContents(), "loc
ation.reload();"); | 238 JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 239 mActivityTestRule.getWebContents(), "location.reload();"); |
| 214 waitForContentsFullscreenState(false); | 240 waitForContentsFullscreenState(false); |
| 215 waitUntilUnlocked(); | 241 waitUntilUnlocked(); |
| 216 } | 242 } |
| 217 } | 243 } |
| OLD | NEW |