1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.android_webview.test;
7 import android.webkit.ValueCallback;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import org.chromium.content.browser.test.util.CallbackHelper;
11 import org.chromium.base.test.util.Feature;
12 import org.chromium.android_webview.AwContents;
13 import org.chromium.net.test.util.TestWebServer;
16 * Tests for AwContentsClient.getVisitedHistory and AwContents.doUpdateVisitedHistory callbacks.
18 public class AwContentsClientVisitedHistoryTest extends AwTestBase {
19 public static class GetVisitedHistoryHelper extends CallbackHelper {
20 private ValueCallback<String[]> mCallback;
21 private boolean mSaveCallback = false;
23 public ValueCallback<String[]> getCallback() {
24 assert getCallCount() > 0;
28 public void setSaveCallback(boolean value) {
29 mSaveCallback = value;
32 public void notifyCalled(ValueCallback<String[]> callback) {
40 public static class DoUpdateVisitedHistoryHelper extends CallbackHelper {
44 public String getUrl() {
45 assert getCallCount() > 0;
49 public boolean getIsReload() {
50 assert getCallCount() > 0;
54 public void notifyCalled(String url, boolean isReload) {
61 private static class TestAwContentsClient
62 extends org.chromium.android_webview.test.TestAwContentsClient {
64 private GetVisitedHistoryHelper mGetVisitedHistoryHelper;
65 private DoUpdateVisitedHistoryHelper mDoUpdateVisitedHistoryHelper;
67 public TestAwContentsClient() {
68 mGetVisitedHistoryHelper = new GetVisitedHistoryHelper();
69 mDoUpdateVisitedHistoryHelper = new DoUpdateVisitedHistoryHelper();
72 public GetVisitedHistoryHelper getGetVisitedHistoryHelper() {
73 return mGetVisitedHistoryHelper;
76 public DoUpdateVisitedHistoryHelper getDoUpdateVisitedHistoryHelper() {
77 return mDoUpdateVisitedHistoryHelper;
81 public void getVisitedHistory(ValueCallback<String[]> callback) {
82 getGetVisitedHistoryHelper().notifyCalled(callback);
86 public void doUpdateVisitedHistory(String url, boolean isReload) {
87 getDoUpdateVisitedHistoryHelper().notifyCalled(url, isReload);
91 private TestAwContentsClient mContentsClient = new TestAwContentsClient();
93 @Feature({"AndroidWebView"})
95 public void testUpdateVisitedHistoryCallback() throws Throwable {
96 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
97 AwContents awContents = testView.getAwContents();
99 final String path = "/testUpdateVisitedHistoryCallback.html";
100 final String html = "testUpdateVisitedHistoryCallback";
102 TestWebServer webServer = null;
104 webServer = new TestWebServer(false);
105 final String pageUrl = webServer.setResponse(path, html, null);
106 final DoUpdateVisitedHistoryHelper doUpdateVisitedHistoryHelper =
107 mContentsClient.getDoUpdateVisitedHistoryHelper();
108 int callCount = doUpdateVisitedHistoryHelper.getCallCount();
109 loadUrlAsync(awContents, pageUrl);
110 doUpdateVisitedHistoryHelper.waitForCallback(callCount);
111 assertEquals(pageUrl, doUpdateVisitedHistoryHelper.getUrl());
112 assertEquals(false, doUpdateVisitedHistoryHelper.getIsReload());
115 callCount = doUpdateVisitedHistoryHelper.getCallCount();
116 loadUrlAsync(awContents, pageUrl);
117 doUpdateVisitedHistoryHelper.waitForCallback(callCount);
118 assertEquals(pageUrl, doUpdateVisitedHistoryHelper.getUrl());
119 assertEquals(true, doUpdateVisitedHistoryHelper.getIsReload());
121 if (webServer != null) webServer.shutdown();
125 @Feature({"AndroidWebView"})
127 public void testGetVisitedHistoryExerciseCodePath() throws Throwable {
128 // Due to security/privacy restrictions around the :visited css property, it is not
129 // possible test this end to end without using the flaky and brittle capturing picture of
130 // the web page. So we are doing the next best thing, exercising all the code paths.
131 final GetVisitedHistoryHelper visitedHistoryHelper =
132 mContentsClient.getGetVisitedHistoryHelper();
133 final int callCount = visitedHistoryHelper.getCallCount();
134 visitedHistoryHelper.setSaveCallback(true);
136 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
137 AwContents awContents = testView.getAwContents();
139 final String path = "/testGetVisitedHistoryExerciseCodePath.html";
140 final String visitedLinks[] = {"http://foo.com", "http://bar.com", null};
141 final String html = "<a src=\"http://foo.com\">foo</a><a src=\"http://bar.com\">bar</a>";
143 TestWebServer webServer = null;
145 webServer = new TestWebServer(false);
146 final String pageUrl = webServer.setResponse(path, html, null);
147 loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
148 visitedHistoryHelper.waitForCallback(callCount);
149 assertNotNull(visitedHistoryHelper.getCallback());
151 visitedHistoryHelper.getCallback().onReceiveValue(visitedLinks);
152 visitedHistoryHelper.getCallback().onReceiveValue(null);
154 loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
156 if (webServer != null) webServer.shutdown();
160 @Feature({"AndroidWebView"})
162 public void testGetVisitedHistoryCallbackAfterDestroy() throws Throwable {
163 GetVisitedHistoryHelper visitedHistoryHelper =
164 mContentsClient.getGetVisitedHistoryHelper();
165 visitedHistoryHelper.setSaveCallback(true);
166 final int callCount = visitedHistoryHelper.getCallCount();
167 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
168 AwContents awContents = testView.getAwContents();
169 loadUrlAsync(awContents, "about:blank");
170 visitedHistoryHelper.waitForCallback(callCount);
171 assertNotNull(visitedHistoryHelper.getCallback());
173 destroyAwContentsOnMainSync(awContents);
174 visitedHistoryHelper.getCallback().onReceiveValue(new String[] {"abc.def"});
175 visitedHistoryHelper.getCallback().onReceiveValue(null);