Chromium Code Reviews| Index: android_webview/tools/PageCycler/src/org/chromium/webview_shell/test/PageCyclerTest.java |
| diff --git a/android_webview/tools/PageCycler/src/org/chromium/webview_shell/test/PageCyclerTest.java b/android_webview/tools/PageCycler/src/org/chromium/webview_shell/test/PageCyclerTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..68f74957db2cd825c6d2798ec8352b461246929c |
| --- /dev/null |
| +++ b/android_webview/tools/PageCycler/src/org/chromium/webview_shell/test/PageCyclerTest.java |
| @@ -0,0 +1,174 @@ |
| +// Copyright 2015 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.webview_shell.test; |
| + |
| +import android.test.ActivityInstrumentationTestCase2; |
| +import android.test.suitebuilder.annotation.LargeTest; |
| +import android.webkit.WebSettings; |
| +import android.webkit.WebView; |
| +import android.webkit.WebViewClient; |
| + |
| +import org.chromium.base.test.util.Restriction; |
| +import org.chromium.content.browser.test.util.CallbackHelper; |
| +import org.chromium.webview_shell.PageCyclerTestActivity; |
| + |
| +import java.util.concurrent.TimeUnit; |
| +import java.util.concurrent.TimeoutException; |
| + |
| +/** |
| + * Tests running on bots with internet connection to load popular urls, |
| + * making sure webview doesn't crash |
| + */ |
| +public class PageCyclerTest |
| + extends ActivityInstrumentationTestCase2<PageCyclerTestActivity> { |
| + |
| + private static final long TIMEOUT_IN_SECS = 20; |
| + |
| + private PageCyclerTestActivity mTestActivity; |
| + |
| + public PageCyclerTest() { |
| + super(PageCyclerTestActivity.class); |
| + } |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + mTestActivity = (PageCyclerTestActivity) getActivity(); |
|
boliu
2015/09/24 22:35:31
this cast should not be needed
Yoland Yan(Google)
2015/09/24 23:30:06
Done
|
| + } |
| + |
| + @Override |
| + protected void tearDown() throws Exception { |
| + mTestActivity.finish(); |
|
boliu
2015/09/24 22:35:31
Wah? Why? And wrong thread
Yoland Yan(Google)
2015/09/24 23:30:07
Done
|
| + super.tearDown(); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitGoogleCom() throws Throwable { |
| + //TODO(yolandyan@): verify the page |
| + visitUrlSync("http://google.com"); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitFacebookCom() throws Throwable { |
| + visitUrlSync("http://facebook.com"); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitWikipediaOrg() throws Throwable { |
| + visitUrlSync("http://wikipedia.org"); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitAmazonCom() throws Throwable { |
| + visitUrlSync("http://amazon.com"); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitYoutubeCom() throws Throwable { |
| + visitUrlSync("http://youtube.com"); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitYahooCom() throws Throwable { |
| + visitUrlSync("http://yahoo.com"); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitEbayCom() throws Throwable { |
| + visitUrlSync("http://ebay.com"); |
| + } |
| + |
| + @LargeTest |
| + @Restriction(Restriction.RESTRICTION_TYPE_INTERNET) |
| + public void testVisitRedditCom() throws Throwable { |
| + visitUrlSync("http://reddit.com"); |
| + } |
| + |
| + private class PageCyclerWebViewClient extends WebViewClient { |
|
boliu
2015/09/24 22:35:31
static
Yoland Yan(Google)
2015/09/24 23:30:06
Done
|
| + private final CallbackHelper mPageFinishedCallback; |
| + private final CallbackHelper mErrorCallback; |
| + |
| + public PageCyclerWebViewClient() { |
| + super(); |
| + mPageFinishedCallback = new CallbackHelper(); |
| + errorCallback = new CallbackHelper(); |
|
boliu
2015/09/24 22:35:31
What's errorCallback? Does this actually compile?
Yoland Yan(Google)
2015/09/24 23:30:06
Damn...my bad
Done
|
| + } |
| + |
| + public CallbackHelper getPageFinishedCallback() { |
| + return mPageFinishedCallback; |
| + } |
| + |
| + public CallbackHelper getErrorCallback() { |
| + return errorCallback; |
| + } |
| + |
| + @Override |
| + public void onPageFinished(WebView view, String url) { |
| + mPageFinishedCallback.notifyCalled(); |
| + } |
| + |
| + // TODO(yolandyan@): create helper class to manage network error |
| + @Override |
| + public void onReceivedError(WebView webview, int code, String description, |
| + String failingUrl) { |
| + errorCallback.notifyCalled(); |
| + } |
| + } |
| + |
| + private void visitUrlSync(final String url) throws Throwable { |
| + final WebView view = mTestActivity.getWebView(); |
|
boliu
2015/09/24 22:35:31
Wrong thread. It's debatable whether it's thread s
Yoland Yan(Google)
2015/09/24 23:30:06
Done
Yoland Yan(Google)
2015/09/24 23:30:07
Done
|
| + final PageCyclerWebViewClient pageCyclerWebViewClient = new PageCyclerWebViewClient(); |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + WebSettings settings = view.getSettings(); |
| + settings.setJavaScriptEnabled(true); |
| + view.setWebViewClient(pageCyclerWebViewClient); |
| + } |
| + }); |
| + CallbackHelper pageFinishedCallback = pageCyclerWebViewClient.getPageFinishedCallback(); |
| + CallbackHelper errorCallback = pageCyclerWebViewClient.getErrorCallback(); |
| + loadUrlSync(view, url, pageFinishedCallback, errorCallback); |
| + } |
| + |
| + private void initializeSettings(WebSettings settings) { |
|
boliu
2015/09/24 22:35:31
nothing calls this
Yoland Yan(Google)
2015/09/24 23:30:06
Done
|
| + settings.setJavaScriptEnabled(true); |
| + settings.setGeolocationEnabled(true); |
| + } |
| + |
| + private void loadUrlSync(final WebView view, final String url, |
| + final CallbackHelper pageFinishedCallback, final CallbackHelper errorCallback) |
| + throws InterruptedException { |
| + boolean timeout = false; |
| + int pageFinishedCount = pageFinishedCallback.getCallCount(); |
| + int errorCount = errorCallback.getCallCount(); |
| + loadUrlAsync(view, url); |
| + try { |
| + pageFinishedCallback.waitForCallback(pageFinishedCount, pageFinishedCount + 1, |
| + TIMEOUT_IN_SECS, TimeUnit.SECONDS); |
| + } catch (TimeoutException ex) { |
| + timeout = true; |
| + } |
| + assertEquals(String.format("Network error while accessing %s", url), errorCount, |
| + errorCallback.getCallCount()); |
| + assertFalse(String.format("Timeout error while accessing %s", url), timeout); |
| + } |
| + |
| + private void loadUrlAsync(final WebView view, final String url) { |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + view.loadUrl(url); |
| + } |
| + }); |
| + } |
| +} |