Index: android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenTest.java |
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenTest.java |
index 3d3f6af0fa303830a7d3f1bc22ecb2ccc06cbffc..840fbaa99eef92ba47d8e9086718aa6e188aa869 100644 |
--- a/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenTest.java |
+++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwContentsClientFullScreenTest.java |
@@ -5,14 +5,19 @@ |
package org.chromium.android_webview.test; |
import android.test.suitebuilder.annotation.MediumTest; |
+import android.view.View; |
+import android.view.ViewGroup; |
import org.chromium.android_webview.test.util.JavascriptEventObserver; |
-import org.chromium.android_webview.test.util.VideoTestWebServer; |
import org.chromium.base.test.util.Feature; |
import org.chromium.content.browser.ContentViewCore; |
+import org.chromium.content.browser.test.util.Criteria; |
+import org.chromium.content.browser.test.util.CriteriaHelper; |
import org.chromium.content.browser.test.util.DOMUtils; |
import org.chromium.content.browser.test.util.TouchCommon; |
+import java.util.concurrent.TimeoutException; |
+ |
/** |
* Test the fullscreen API (WebChromeClient::onShow/HideCustomView). |
* |
@@ -22,9 +27,21 @@ import org.chromium.content.browser.test.util.TouchCommon; |
* very common use case. |
*/ |
public class AwContentsClientFullScreenTest extends AwTestBase { |
+ private static final String VIDEO_TEST_URL = |
+ "file:///android_asset/full_screen_video_test.html"; |
+ private static final String VIDEO_INSIDE_DIV_TEST_URL = |
+ "file:///android_asset/full_screen_video_inside_div_test.html"; |
+ |
+ // These values must be kept in sync with the strings in |
+ // full_screen_video_test.html, full_screen_video_inside_div_test.html and |
+ // full_screen_video.js. |
+ private static final String VIDEO_ID = "video"; |
+ private static final String CUSTOM_PLAY_CONTROL_ID = "playControl"; |
+ private static final String CUSTOM_FULLSCREEN_CONTROL_ID = "fullscreenControl"; |
+ private static final String FULLSCREEN_ERROR_OBSERVER = "javaFullScreenErrorObserver"; |
+ |
private FullScreenVideoTestAwContentsClient mContentsClient; |
private ContentViewCore mContentViewCore; |
- private VideoTestWebServer mWebServer; |
private AwTestContainerView mTestContainerView; |
@Override |
@@ -36,26 +53,18 @@ public class AwContentsClientFullScreenTest extends AwTestBase { |
mContentViewCore = mTestContainerView.getContentViewCore(); |
enableJavaScriptOnUiThread(mTestContainerView.getAwContents()); |
mTestContainerView.getAwContents().getSettings().setFullscreenSupported(true); |
- mWebServer = new VideoTestWebServer( |
- getInstrumentation().getTargetContext()); |
- } |
- |
- @Override |
- protected void tearDown() throws Exception { |
- super.tearDown(); |
- if (mWebServer != null) mWebServer.getTestWebServer().shutdown(); |
} |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testOnShowAndHideCustomViewWithCallback_video() throws Throwable { |
- doTestOnShowAndHideCustomViewWithCallback(getFullScreenVideoTestUrl()); |
+ doTestOnShowAndHideCustomViewWithCallback(VIDEO_TEST_URL); |
} |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testOnShowAndHideCustomViewWithCallback_videoInsideDiv() throws Throwable { |
- doTestOnShowAndHideCustomViewWithCallback(getFullScreenVideoInsideDivTestUrl()); |
+ doTestOnShowAndHideCustomViewWithCallback(VIDEO_INSIDE_DIV_TEST_URL); |
} |
public void doTestOnShowAndHideCustomViewWithCallback(String videoTestUrl) throws Throwable { |
@@ -70,14 +79,14 @@ public class AwContentsClientFullScreenTest extends AwTestBase { |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testOnShowAndHideCustomViewWithJavascript_video() throws Throwable { |
- doTestOnShowAndHideCustomViewWithJavascript(getFullScreenVideoTestUrl()); |
+ doTestOnShowAndHideCustomViewWithJavascript(VIDEO_TEST_URL); |
} |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testOnShowAndHideCustomViewWithJavascript_videoInsideDiv() |
throws Throwable { |
- doTestOnShowAndHideCustomViewWithJavascript(getFullScreenVideoInsideDivTestUrl()); |
+ doTestOnShowAndHideCustomViewWithJavascript(VIDEO_INSIDE_DIV_TEST_URL); |
} |
public void doTestOnShowAndHideCustomViewWithJavascript(String videoTestUrl) throws Throwable { |
@@ -92,55 +101,200 @@ public class AwContentsClientFullScreenTest extends AwTestBase { |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testOnShowCustomViewAndPlayWithHtmlControl_video() throws Throwable { |
- doTestOnShowCustomViewAndPlayWithHtmlControl(getFullScreenVideoTestUrl()); |
+ doTestOnShowCustomViewAndPlayWithHtmlControl(VIDEO_TEST_URL); |
} |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testOnShowCustomViewAndPlayWithHtmlControl_videoInsideDiv() throws Throwable { |
- doTestOnShowCustomViewAndPlayWithHtmlControl(getFullScreenVideoInsideDivTestUrl()); |
+ doTestOnShowCustomViewAndPlayWithHtmlControl(VIDEO_INSIDE_DIV_TEST_URL); |
} |
public void doTestOnShowCustomViewAndPlayWithHtmlControl(String videoTestUrl) throws Throwable { |
- final JavascriptEventObserver onPlayObserver = registerObserver("javaOnPlayObserver"); |
- |
doOnShowCustomViewTest(videoTestUrl); |
+ assertTrue(DOMUtils.isVideoPaused(mContentViewCore.getWebContents(), VIDEO_ID)); |
- // Click the html play button that is rendered above the video right in the middle |
- // of the custom view. Note that we're not able to get the precise location of the |
- // control since it is a shadow element, so this test might break if the location |
- // ever moves. |
- TouchCommon touchCommon = new TouchCommon( |
- AwContentsClientFullScreenTest.this); |
- touchCommon.singleClickView(mContentsClient.getCustomView()); |
- |
- assertTrue(onPlayObserver.waitForEvent(WAIT_TIMEOUT_MS)); |
+ tapPlayButton(); |
+ assertTrue(DOMUtils.waitForVideoPlay(mContentViewCore.getWebContents(), VIDEO_ID)); |
} |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testFullscreenNotSupported_video() throws Throwable { |
- doTestFullscreenNotSupported(getFullScreenVideoTestUrl()); |
+ doTestFullscreenNotSupported(VIDEO_TEST_URL); |
} |
@MediumTest |
@Feature({"AndroidWebView"}) |
public void testFullscreenNotSupported_videoInsideDiv() throws Throwable { |
- doTestFullscreenNotSupported(getFullScreenVideoInsideDivTestUrl()); |
+ doTestFullscreenNotSupported(VIDEO_INSIDE_DIV_TEST_URL); |
} |
public void doTestFullscreenNotSupported(String videoTestUrl) throws Throwable { |
mTestContainerView.getAwContents().getSettings().setFullscreenSupported(false); |
- |
- final JavascriptEventObserver fullScreenErrorObserver = registerObserver( |
- "javaFullScreenErrorObserver"); |
+ final JavascriptEventObserver fullscreenErrorObserver = registerObserver( |
+ FULLSCREEN_ERROR_OBSERVER); |
loadTestPageAndClickFullscreen(videoTestUrl); |
- assertTrue(fullScreenErrorObserver.waitForEvent(WAIT_TIMEOUT_MS)); |
+ assertTrue(fullscreenErrorObserver.waitForEvent(WAIT_TIMEOUT_MS)); |
assertFalse(mContentsClient.wasCustomViewShownCalled()); |
} |
+ @MediumTest |
+ @Feature({"AndroidWebView"}) |
+ public void testPowerSaveBlockerIsEnabledDuringFullscreenPlayback_video() |
+ throws Throwable { |
+ doTestPowerSaveBlockerIsEnabledDuringFullscreenPlayback(VIDEO_TEST_URL); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView"}) |
+ public void testPowerSaveBlockerIsEnabledDuringFullscreenPlayback_videoInsideDiv() |
+ throws Throwable { |
+ doTestPowerSaveBlockerIsEnabledDuringFullscreenPlayback(VIDEO_INSIDE_DIV_TEST_URL); |
+ } |
+ |
+ public void doTestPowerSaveBlockerIsEnabledDuringFullscreenPlayback(String videoTestUrl) |
+ throws Throwable { |
+ // Enter fullscreen. |
+ doOnShowCustomViewTest(videoTestUrl); |
+ View customView = mContentsClient.getCustomView(); |
+ |
+ // No power save blocker is active before playback starts. |
+ assertKeepScreenOnActive(customView, false); |
+ |
+ // Play and verify that there is an active power save blocker. |
+ tapPlayButton(); |
+ assertKeepScreenOnActive(customView, true); |
+ |
+ // Stop the video and verify that the power save blocker is gone. |
+ DOMUtils.pauseVideo(mContentViewCore.getWebContents(), VIDEO_ID); |
+ assertKeepScreenOnActive(customView, false); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView"}) |
+ public void testPowerSaveBlockerIsEnabledDuringEmbeddedPlayback() |
+ throws Throwable { |
+ assertFalse(DOMUtils.isFullscreen(mContentViewCore.getWebContents())); |
+ loadTestPage(VIDEO_INSIDE_DIV_TEST_URL); |
+ |
+ // No power save blocker is active before playback starts. |
+ assertKeepScreenOnActive(mTestContainerView, false); |
+ |
+ // Play and verify that there is an active power save blocker. |
+ tapPlayButton(); |
+ assertKeepScreenOnActive(mTestContainerView, true); |
+ |
+ // Stop the video and verify that the power save blocker is gone. |
+ DOMUtils.pauseVideo(mContentViewCore.getWebContents(), VIDEO_ID); |
+ assertKeepScreenOnActive(mTestContainerView, false); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView"}) |
+ public void testPowerSaveBlockerIsTransferredToFullscreen() |
+ throws Throwable { |
+ assertFalse(DOMUtils.isFullscreen(mContentViewCore.getWebContents())); |
+ loadTestPage(VIDEO_INSIDE_DIV_TEST_URL); |
+ |
+ // Play and verify that there is an active power save blocker. |
+ tapPlayButton(); |
+ assertKeepScreenOnActive(mTestContainerView, true); |
+ |
+ // Enter fullscreen and verify that the power save blocker is |
+ // still there. |
+ DOMUtils.clickNode(this, mContentViewCore, CUSTOM_FULLSCREEN_CONTROL_ID); |
+ mContentsClient.waitForCustomViewShown(); |
+ View customView = mContentsClient.getCustomView(); |
+ assertKeepScreenOnActive(customView, true); |
+ |
+ // Pause the video and the power save blocker is gone. |
+ DOMUtils.pauseVideo(mContentViewCore.getWebContents(), VIDEO_ID); |
+ assertKeepScreenOnActive(customView, false); |
+ |
+ // Exit fullscreen and the power save blocker is still gone. |
+ DOMUtils.exitFullscreen(mContentViewCore.getWebContents()); |
+ mContentsClient.waitForCustomViewHidden(); |
+ assertKeepScreenOnActive(mTestContainerView, false); |
+ } |
+ |
+ @MediumTest |
+ @Feature({"AndroidWebView"}) |
+ public void testPowerSaveBlockerIsTransferredToEmbedded() |
+ throws Throwable { |
+ // Enter fullscreen. |
+ doOnShowCustomViewTest(VIDEO_INSIDE_DIV_TEST_URL); |
+ View customView = mContentsClient.getCustomView(); |
+ |
+ // Play and verify that there is an active power save blocker |
+ // in fullscreen. |
+ tapPlayButton(); |
+ assertKeepScreenOnActive(customView, true); |
+ |
+ // Exit fullscreen and verify that the power save blocker is |
+ // still there. |
+ DOMUtils.exitFullscreen(mContentViewCore.getWebContents()); |
+ mContentsClient.waitForCustomViewHidden(); |
+ assertKeepScreenOnActive(mTestContainerView, true); |
+ } |
+ |
+ private void tapPlayButton() throws Exception { |
+ String testUrl = mTestContainerView.getAwContents().getUrl(); |
+ if (VIDEO_INSIDE_DIV_TEST_URL.equals(testUrl)) { |
+ // VIDEO_INSIDE_DIV_TEST_URL uses a custom play control with known id. |
+ DOMUtils.clickNode(this, mContentViewCore, CUSTOM_PLAY_CONTROL_ID); |
+ } else if (VIDEO_TEST_URL.equals(testUrl) |
+ && DOMUtils.isFullscreen(mContentViewCore.getWebContents())) { |
+ // VIDEO_TEST_URL uses the standard html5 video controls. The standard |
+ // html5 controls are shadow html elements without any ids. In fullscreen we can still |
+ // tap the play button because this is rendered in the center of the custom view. |
+ // In embedded mode we don't have an easy way of retrieving its location. |
+ TouchCommon touchCommon = new TouchCommon(AwContentsClientFullScreenTest.this); |
+ touchCommon.singleClickView(mContentsClient.getCustomView()); |
+ } else { |
+ fail("Unable to tap standard html5 play control in embedded mode"); |
+ } |
+ } |
+ |
+ /** |
+ * Asserts that the keep screen on property in the given {@code view} is active as |
+ * {@code expected}. It also verifies that it is only active when the video is playing. |
+ */ |
+ private void assertKeepScreenOnActive(final View view, final boolean expected) |
+ throws InterruptedException { |
+ // We need to poll because it takes time to synchronize the state between the android |
+ // views and Javascript. |
+ assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
+ @Override |
+ public boolean isSatisfied() { |
+ try { |
+ return getKeepScreenOn(view) == expected |
+ && DOMUtils.isVideoPaused(mContentViewCore.getWebContents(), VIDEO_ID) |
+ != expected; |
+ } catch (InterruptedException | TimeoutException e) { |
+ fail(e.getMessage()); |
+ return false; |
+ } |
+ } |
+ })); |
+ } |
+ |
+ private boolean getKeepScreenOn(View view) { |
+ // The power save blocker is added to a child anchor view, |
+ // so we need to traverse the hierarchy. |
+ if (view instanceof ViewGroup) { |
+ ViewGroup viewGroup = (ViewGroup) view; |
+ for (int i = 0; i < viewGroup.getChildCount(); i++) { |
+ if (getKeepScreenOn(viewGroup.getChildAt(i))) { |
+ return true; |
+ } |
+ } |
+ } |
+ return view.getKeepScreenOn(); |
+ } |
+ |
private JavascriptEventObserver registerObserver(final String observerName) { |
final JavascriptEventObserver observer = new JavascriptEventObserver(); |
getInstrumentation().runOnMainSync(new Runnable() { |
@@ -160,27 +314,18 @@ public class AwContentsClientFullScreenTest extends AwTestBase { |
} |
private void doOnShowCustomViewTest(String videoTestUrl) throws Exception { |
- final JavascriptEventObserver onEnterFullscreenObserver = |
- registerObserver("javaOnEnterFullscreen"); |
loadTestPageAndClickFullscreen(videoTestUrl); |
mContentsClient.waitForCustomViewShown(); |
- assertTrue(onEnterFullscreenObserver.waitForEvent(WAIT_TIMEOUT_MS)); |
+ assertTrue(DOMUtils.isFullscreen(mContentViewCore.getWebContents())); |
} |
private void loadTestPageAndClickFullscreen(String videoTestUrl) throws Exception { |
- loadUrlSync(mTestContainerView.getAwContents(), |
- mContentsClient.getOnPageFinishedHelper(), videoTestUrl); |
- |
- // Click the button in full_screen_video_test.html to enter fullscreen. |
- TouchCommon touchCommon = new TouchCommon(this); |
- touchCommon.singleClickView(mTestContainerView); |
+ loadTestPage(videoTestUrl); |
+ DOMUtils.clickNode(this, mContentViewCore, CUSTOM_FULLSCREEN_CONTROL_ID); |
} |
- private String getFullScreenVideoTestUrl() { |
- return mWebServer.getFullScreenVideoTestURL(); |
- } |
- |
- private String getFullScreenVideoInsideDivTestUrl() { |
- return mWebServer.getFullScreenVideoInsideDivTestURL(); |
+ private void loadTestPage(String videoTestUrl) throws Exception { |
+ loadUrlSync(mTestContainerView.getAwContents(), |
+ mContentsClient.getOnPageFinishedHelper(), videoTestUrl); |
} |
} |