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