diff options
author | mkosiba@chromium.org <mkosiba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-01 21:52:34 +0000 |
---|---|---|
committer | mkosiba@chromium.org <mkosiba@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-01 21:52:34 +0000 |
commit | 1dca97bbf3d4c109ae93be900cf6823dc87c154f (patch) | |
tree | 86382ed1398ba0a3d9ee3fd506fb56cbbfc8b03a /android_webview | |
parent | ebd6da45df7f7008b421b636e8f1c51b9a51f22c (diff) | |
download | chromium_src-1dca97bbf3d4c109ae93be900cf6823dc87c154f.zip chromium_src-1dca97bbf3d4c109ae93be900cf6823dc87c154f.tar.gz chromium_src-1dca97bbf3d4c109ae93be900cf6823dc87c154f.tar.bz2 |
[android_webview] Move test case out of AwTestBase.
AwTestBase is not the place for test case implementations.
BUG=None
TEST=AndroidWebViewTest
Review URL: https://codereview.chromium.org/263803002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267638 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'android_webview')
5 files changed, 115 insertions, 86 deletions
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java index e21e2e8..757fd89 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java @@ -16,6 +16,8 @@ import android.webkit.JavascriptInterface; import android.webkit.ValueCallback; import android.webkit.WebSettings; +import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; + import org.apache.http.Header; import org.apache.http.HttpRequest; import org.chromium.android_webview.AwContents; @@ -24,6 +26,7 @@ import org.chromium.android_webview.AwSettings.LayoutAlgorithm; import org.chromium.android_webview.InterceptedRequestData; import org.chromium.android_webview.test.util.CommonResources; import org.chromium.android_webview.test.util.ImagePageGenerator; +import org.chromium.android_webview.test.util.VideoTestUtil; import org.chromium.android_webview.test.util.VideoTestWebServer; import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.Feature; @@ -2543,14 +2546,14 @@ public class AwSettingsTest extends AwTestBase { */ @DisabledTest public void testMediaPlaybackWithoutUserGesture() throws Throwable { - assertTrue(runVideoTest(false, -1)); + assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS)); } @SmallTest @Feature({"AndroidWebView", "Preferences"}) public void testMediaPlaybackWithUserGesture() throws Throwable { // Wait for 5 second to see if video played. - assertFalse(runVideoTest(true, 5000)); + assertFalse(VideoTestUtil.runVideoTest(this, true, scaleTimeout(5000))); } @SmallTest diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwTestBase.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwTestBase.java index c476910..fd90331 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwTestBase.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwTestBase.java @@ -17,8 +17,6 @@ import org.chromium.android_webview.AwContents; import org.chromium.android_webview.AwContentsClient; import org.chromium.android_webview.AwSettings; import org.chromium.android_webview.test.util.JSUtils; -import org.chromium.android_webview.test.util.JavascriptEventObserver; -import org.chromium.android_webview.test.util.VideoTestWebServer; import org.chromium.base.test.util.InMemorySharedPreferences; import org.chromium.content.browser.ContentSettings; import org.chromium.content.browser.LoadUrlParams; @@ -37,8 +35,8 @@ import java.util.concurrent.atomic.AtomicReference; */ public class AwTestBase extends ActivityInstrumentationTestCase2<AwTestRunnerActivity> { - protected static final long WAIT_TIMEOUT_MS = scaleTimeout(15000); - protected static final int CHECK_INTERVAL = 100; + public static final long WAIT_TIMEOUT_MS = scaleTimeout(15000); + public static final int CHECK_INTERVAL = 100; private static final String TAG = "AwTestBase"; public AwTestBase() { @@ -82,7 +80,7 @@ public class AwTestBase return task.get(); } - protected void enableJavaScriptOnUiThread(final AwContents awContents) { + public void enableJavaScriptOnUiThread(final AwContents awContents) { getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { @@ -91,7 +89,7 @@ public class AwTestBase }); } - protected void setNetworkAvailableOnUiThread(final AwContents awContents, + public void setNetworkAvailableOnUiThread(final AwContents awContents, final boolean networkUp) { getInstrumentation().runOnMainSync(new Runnable() { @Override @@ -104,13 +102,13 @@ public class AwTestBase /** * Loads url on the UI thread and blocks until onPageFinished is called. */ - protected void loadUrlSync(final AwContents awContents, + public void loadUrlSync(final AwContents awContents, CallbackHelper onPageFinishedHelper, final String url) throws Exception { loadUrlSync(awContents, onPageFinishedHelper, url, null); } - protected void loadUrlSync(final AwContents awContents, + public void loadUrlSync(final AwContents awContents, CallbackHelper onPageFinishedHelper, final String url, final Map<String, String> extraHeaders) throws Exception { @@ -120,7 +118,7 @@ public class AwTestBase TimeUnit.MILLISECONDS); } - protected void loadUrlSyncAndExpectError(final AwContents awContents, + public void loadUrlSyncAndExpectError(final AwContents awContents, CallbackHelper onPageFinishedHelper, CallbackHelper onReceivedErrorHelper, final String url) throws Exception { @@ -136,12 +134,12 @@ public class AwTestBase /** * Loads url on the UI thread but does not block. */ - protected void loadUrlAsync(final AwContents awContents, + public void loadUrlAsync(final AwContents awContents, final String url) throws Exception { loadUrlAsync(awContents, url, null); } - protected void loadUrlAsync(final AwContents awContents, + public void loadUrlAsync(final AwContents awContents, final String url, final Map<String, String> extraHeaders) { getInstrumentation().runOnMainSync(new Runnable() { @@ -157,7 +155,7 @@ public class AwTestBase /** * Posts url on the UI thread and blocks until onPageFinished is called. */ - protected void postUrlSync(final AwContents awContents, + public void postUrlSync(final AwContents awContents, CallbackHelper onPageFinishedHelper, final String url, byte[] postData) throws Exception { int currentCallCount = onPageFinishedHelper.getCallCount(); @@ -169,7 +167,7 @@ public class AwTestBase /** * Loads url on the UI thread but does not block. */ - protected void postUrlAsync(final AwContents awContents, + public void postUrlAsync(final AwContents awContents, final String url, byte[] postData) throws Exception { class PostUrl implements Runnable { byte[] mPostData; @@ -188,7 +186,7 @@ public class AwTestBase /** * Loads data on the UI thread and blocks until onPageFinished is called. */ - protected void loadDataSync(final AwContents awContents, + public void loadDataSync(final AwContents awContents, CallbackHelper onPageFinishedHelper, final String data, final String mimeType, final boolean isBase64Encoded) throws Exception { @@ -198,7 +196,7 @@ public class AwTestBase TimeUnit.MILLISECONDS); } - protected void loadDataSyncWithCharset(final AwContents awContents, + public void loadDataSyncWithCharset(final AwContents awContents, CallbackHelper onPageFinishedHelper, final String data, final String mimeType, final boolean isBase64Encoded, final String charset) @@ -218,7 +216,7 @@ public class AwTestBase /** * Loads data on the UI thread but does not block. */ - protected void loadDataAsync(final AwContents awContents, final String data, + public void loadDataAsync(final AwContents awContents, final String data, final String mimeType, final boolean isBase64Encoded) throws Exception { getInstrumentation().runOnMainSync(new Runnable() { @@ -230,7 +228,7 @@ public class AwTestBase }); } - protected void loadDataWithBaseUrlSync(final AwContents awContents, + public void loadDataWithBaseUrlSync(final AwContents awContents, CallbackHelper onPageFinishedHelper, final String data, final String mimeType, final boolean isBase64Encoded, final String baseUrl, final String historyUrl) throws Throwable { @@ -240,7 +238,7 @@ public class AwTestBase TimeUnit.MILLISECONDS); } - protected void loadDataWithBaseUrlAsync(final AwContents awContents, + public void loadDataWithBaseUrlAsync(final AwContents awContents, final String data, final String mimeType, final boolean isBase64Encoded, final String baseUrl, final String historyUrl) throws Throwable { runTestOnUiThread(new Runnable() { @@ -255,7 +253,7 @@ public class AwTestBase /** * Reloads the current page synchronously. */ - protected void reloadSync(final AwContents awContents, + public void reloadSync(final AwContents awContents, CallbackHelper onPageFinishedHelper) throws Exception { int currentCallCount = onPageFinishedHelper.getCallCount(); getInstrumentation().runOnMainSync(new Runnable() { @@ -287,12 +285,12 @@ public class AwTestBase return new TestDependencyFactory(); } - protected AwTestContainerView createAwTestContainerView( + public AwTestContainerView createAwTestContainerView( final AwContentsClient awContentsClient) { return createAwTestContainerView(awContentsClient, false); } - protected AwTestContainerView createAwTestContainerView( + public AwTestContainerView createAwTestContainerView( final AwContentsClient awContentsClient, boolean supportsLegacyQuirks) { AwTestContainerView testContainerView = createDetachedAwTestContainerView(awContentsClient, supportsLegacyQuirks); @@ -305,12 +303,12 @@ public class AwTestBase private AwBrowserContext mBrowserContext = new AwBrowserContext(new InMemorySharedPreferences()); - protected AwTestContainerView createDetachedAwTestContainerView( + public AwTestContainerView createDetachedAwTestContainerView( final AwContentsClient awContentsClient) { return createDetachedAwTestContainerView(awContentsClient, false); } - protected AwTestContainerView createDetachedAwTestContainerView( + public AwTestContainerView createDetachedAwTestContainerView( final AwContentsClient awContentsClient, boolean supportsLegacyQuirks) { final TestDependencyFactory testDependencyFactory = createTestDependencyFactory(); final AwTestContainerView testContainerView = @@ -323,12 +321,12 @@ public class AwTestBase return testContainerView; } - protected AwTestContainerView createAwTestContainerViewOnMainSync( + public AwTestContainerView createAwTestContainerViewOnMainSync( final AwContentsClient client) throws Exception { return createAwTestContainerViewOnMainSync(client, false); } - protected AwTestContainerView createAwTestContainerViewOnMainSync( + public AwTestContainerView createAwTestContainerViewOnMainSync( final AwContentsClient client, final boolean supportsLegacyQuirks) throws Exception { final AtomicReference<AwTestContainerView> testContainerView = new AtomicReference<AwTestContainerView>(); @@ -341,7 +339,7 @@ public class AwTestBase return testContainerView.get(); } - protected void destroyAwContentsOnMainSync(final AwContents awContents) { + public void destroyAwContentsOnMainSync(final AwContents awContents) { if (awContents == null) return; getInstrumentation().runOnMainSync(new Runnable() { @Override @@ -351,7 +349,7 @@ public class AwTestBase }); } - protected String getTitleOnUiThread(final AwContents awContents) throws Exception { + public String getTitleOnUiThread(final AwContents awContents) throws Exception { return runTestOnUiThreadAndGetResult(new Callable<String>() { @Override public String call() throws Exception { @@ -360,7 +358,7 @@ public class AwTestBase }); } - protected ContentSettings getContentSettingsOnUiThread( + public ContentSettings getContentSettingsOnUiThread( final AwContents awContents) throws Exception { return runTestOnUiThreadAndGetResult(new Callable<ContentSettings>() { @Override @@ -370,7 +368,7 @@ public class AwTestBase }); } - protected AwSettings getAwSettingsOnUiThread( + public AwSettings getAwSettingsOnUiThread( final AwContents awContents) throws Exception { return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() { @Override @@ -384,7 +382,7 @@ public class AwTestBase * Executes the given snippet of JavaScript code within the given ContentView. Returns the * result of its execution in JSON format. */ - protected String executeJavaScriptAndWaitForResult(final AwContents awContents, + public String executeJavaScriptAndWaitForResult(final AwContents awContents, TestAwContentsClient viewClient, final String code) throws Exception { return JSUtils.executeJavaScriptAndWaitForResult(this, awContents, viewClient.getOnEvaluateJavaScriptResultHelper(), @@ -395,7 +393,7 @@ public class AwTestBase * Wrapper around CriteriaHelper.pollForCriteria. This uses AwTestBase-specifc timeouts and * treats timeouts and exceptions as test failures automatically. */ - protected static void poll(final Callable<Boolean> callable) throws Exception { + public static void poll(final Callable<Boolean> callable) throws Exception { assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { @Override public boolean isSatisfied() { @@ -412,7 +410,7 @@ public class AwTestBase /** * Wrapper around {@link AwTestBase#poll()} but runs the callable on the UI thread. */ - protected void pollOnUiThread(final Callable<Boolean> callable) throws Exception { + public void pollOnUiThread(final Callable<Boolean> callable) throws Exception { poll(new Callable<Boolean>() { @Override public Boolean call() throws Exception { @@ -425,7 +423,7 @@ public class AwTestBase * Clears the resource cache. Note that the cache is per-application, so this will clear the * cache for all WebViews used. */ - protected void clearCacheOnUiThread( + public void clearCacheOnUiThread( final AwContents awContents, final boolean includeDiskFiles) throws Exception { getInstrumentation().runOnMainSync(new Runnable() { @@ -439,7 +437,7 @@ public class AwTestBase /** * Returns pure page scale. */ - protected float getScaleOnUiThread(final AwContents awContents) throws Exception { + public float getScaleOnUiThread(final AwContents awContents) throws Exception { return runTestOnUiThreadAndGetResult(new Callable<Float>() { @Override public Float call() throws Exception { @@ -451,7 +449,7 @@ public class AwTestBase /** * Returns page scale multiplied by the screen density. */ - protected float getPixelScaleOnUiThread(final AwContents awContents) throws Exception { + public float getPixelScaleOnUiThread(final AwContents awContents) throws Exception { return runTestOnUiThreadAndGetResult(new Callable<Float>() { @Override public Float call() throws Exception { @@ -463,7 +461,7 @@ public class AwTestBase /** * Returns whether a user can zoom the page in. */ - protected boolean canZoomInOnUiThread(final AwContents awContents) throws Exception { + public boolean canZoomInOnUiThread(final AwContents awContents) throws Exception { return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { @Override public Boolean call() throws Exception { @@ -475,7 +473,7 @@ public class AwTestBase /** * Returns whether a user can zoom the page out. */ - protected boolean canZoomOutOnUiThread(final AwContents awContents) throws Exception { + public boolean canZoomOutOnUiThread(final AwContents awContents) throws Exception { return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { @Override public Boolean call() throws Exception { @@ -484,48 +482,4 @@ public class AwTestBase }); } - /** - * Run video test. - * @param requiredUserGesture the settings of MediaPlaybackRequiresUserGesture. - * @param waitTime time for waiting event happen, -1 means forever. - * @return true if the event happened, - * @throws Throwable throw exception if timeout. - */ - protected boolean runVideoTest(final boolean requiredUserGesture, long waitTime) - throws Throwable { - final JavascriptEventObserver observer = new JavascriptEventObserver(); - TestAwContentsClient client = new TestAwContentsClient(); - final AwContents awContents = createAwTestContainerViewOnMainSync(client).getAwContents(); - getInstrumentation().runOnMainSync(new Runnable() { - @Override - public void run() { - AwSettings awSettings = awContents.getSettings(); - awSettings.setJavaScriptEnabled(true); - awSettings.setMediaPlaybackRequiresUserGesture(requiredUserGesture); - observer.register(awContents.getContentViewCore(), "javaObserver"); - } - }); - VideoTestWebServer webServer = new VideoTestWebServer(getActivity()); - try { - String data = "<html><head><script>" + - "addEventListener('DOMContentLoaded', function() { " + - " document.getElementById('video').addEventListener('play', function() { " + - " javaObserver.notifyJava(); " + - " }, false); " + - "}, false); " + - "</script></head><body>" + - "<video id='video' autoplay control src='" + - webServer.getOnePixelOneFrameWebmURL() + "' /> </body></html>"; - loadDataAsync(awContents, data, "text/html", false); - if (waitTime == -1) { - observer.waitForEvent(); - return true; - } else { - return observer.waitForEvent(waitTime); - } - } finally { - if (webServer != null && webServer.getTestWebServer() != null) - webServer.getTestWebServer().shutdown(); - } - } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/ExternalVideoSurfaceContainerTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/ExternalVideoSurfaceContainerTest.java index 85fae9a..e4afe7c 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/ExternalVideoSurfaceContainerTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/ExternalVideoSurfaceContainerTest.java @@ -8,6 +8,7 @@ import android.graphics.RectF; import android.test.suitebuilder.annotation.SmallTest; import org.chromium.android_webview.ExternalVideoSurfaceContainer; +import org.chromium.android_webview.test.util.VideoTestUtil; import org.chromium.base.CommandLine; import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.Feature; @@ -111,7 +112,7 @@ public class ExternalVideoSurfaceContainerTest extends AwTestBase { int onRequestCallCount = mOnRequestExternalVideoSurface.getCallCount(); int onPositionChangedCallCount = mOnExternalVideoSurfacePositionChanged.getCallCount(); - assertTrue(runVideoTest(false, -1)); + assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS)); mOnRequestExternalVideoSurface.waitForCallback(onRequestCallCount); waitForVideoSizeChangeTo(mOnExternalVideoSurfacePositionChanged, @@ -123,7 +124,7 @@ public class ExternalVideoSurfaceContainerTest extends AwTestBase { public void testDisableVideoOverlayForEmbeddedVideo() throws Throwable { setUpMockExternalVideoSurfaceContainer(); - assertTrue(runVideoTest(false, -1)); + assertTrue(VideoTestUtil.runVideoTest(this, false, WAIT_TIMEOUT_MS)); assertEquals(0, mOnRequestExternalVideoSurface.getCallCount()); assertEquals(0, mOnExternalVideoSurfacePositionChanged.getCallCount()); diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/TestAwContentsClient.java b/android_webview/javatests/src/org/chromium/android_webview/test/TestAwContentsClient.java index ccd0da3..d96b5e7 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/TestAwContentsClient.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/TestAwContentsClient.java @@ -14,7 +14,10 @@ import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPage import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageStartedHelper; import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnReceivedErrorHelper; -class TestAwContentsClient extends NullContentsClient { +/** + * AwContentsClient subclass used for testing. + */ +public class TestAwContentsClient extends NullContentsClient { private String mUpdatedTitle; private final OnPageStartedHelper mOnPageStartedHelper; private final OnPageFinishedHelper mOnPageFinishedHelper; @@ -73,6 +76,9 @@ class TestAwContentsClient extends NullContentsClient { return mAddMessageToConsoleHelper; } + /** + * Callback helper for onScaleChangedScaled. + */ public static class OnScaleChangedHelper extends CallbackHelper { private float mPreviousScale; private float mCurrentScale; @@ -227,6 +233,9 @@ class TestAwContentsClient extends NullContentsClient { return false; } + /** + * Callback helper for onScaleChangedScaled. + */ public static class AddMessageToConsoleHelper extends CallbackHelper { private int mLevel; private String mMessage; @@ -267,6 +276,9 @@ class TestAwContentsClient extends NullContentsClient { mOnScaleChangedHelper.notifyCalled(oldScale, newScale); } + /** + * Callback helper for onScaleChangedScaled. + */ public static class PictureListenerHelper extends CallbackHelper { // Generally null, depending on |invalidationOnly| in enableOnNewPicture() private Picture mPicture; @@ -287,6 +299,9 @@ class TestAwContentsClient extends NullContentsClient { mPictureListenerHelper.notifyCalled(picture); } + /** + * Callback helper for onScaleChangedScaled. + */ public static class ShouldOverrideUrlLoadingHelper extends CallbackHelper { private String mShouldOverrideUrlLoadingUrl; private String mPreviousShouldOverrideUrlLoadingUrl; diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/util/VideoTestUtil.java b/android_webview/javatests/src/org/chromium/android_webview/test/util/VideoTestUtil.java new file mode 100644 index 0000000..5df2a03 --- /dev/null +++ b/android_webview/javatests/src/org/chromium/android_webview/test/util/VideoTestUtil.java @@ -0,0 +1,56 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.android_webview.test.util; + +import org.chromium.android_webview.AwContents; +import org.chromium.android_webview.AwSettings; +import org.chromium.android_webview.test.AwTestBase; +import org.chromium.android_webview.test.TestAwContentsClient; + +/** + * Code shared between the various video tests. + */ +public class VideoTestUtil { + /** + * Run video test. + * @param testCase the test case instance we're going to run the test in. + * @param requiredUserGesture the settings of MediaPlaybackRequiresUserGesture. + * @return true if the event happened, + * @throws Throwable throw exception if timeout. + */ + public static boolean runVideoTest(final AwTestBase testCase, final boolean requiredUserGesture, + long waitTime) throws Throwable { + final JavascriptEventObserver observer = new JavascriptEventObserver(); + TestAwContentsClient client = new TestAwContentsClient(); + final AwContents awContents = + testCase.createAwTestContainerViewOnMainSync(client).getAwContents(); + testCase.getInstrumentation().runOnMainSync(new Runnable() { + @Override + public void run() { + AwSettings awSettings = awContents.getSettings(); + awSettings.setJavaScriptEnabled(true); + awSettings.setMediaPlaybackRequiresUserGesture(requiredUserGesture); + observer.register(awContents.getContentViewCore(), "javaObserver"); + } + }); + VideoTestWebServer webServer = new VideoTestWebServer(testCase.getActivity()); + try { + String data = "<html><head><script>" + + "addEventListener('DOMContentLoaded', function() { " + + " document.getElementById('video').addEventListener('play', function() { " + + " javaObserver.notifyJava(); " + + " }, false); " + + "}, false); " + + "</script></head><body>" + + "<video id='video' autoplay control src='" + + webServer.getOnePixelOneFrameWebmURL() + "' /> </body></html>"; + testCase.loadDataAsync(awContents, data, "text/html", false); + return observer.waitForEvent(waitTime); + } finally { + if (webServer != null && webServer.getTestWebServer() != null) + webServer.getTestWebServer().shutdown(); + } + } +} |