1 // Copyright 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.test.suitebuilder.annotation.SmallTest;
8 import android.webkit.ValueCallback;
10 import org.chromium.android_webview.AwContents;
11 import org.chromium.android_webview.test.TestAwContentsClient.DoUpdateVisitedHistoryHelper;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.content.browser.test.util.CallbackHelper;
14 import org.chromium.net.test.util.TestWebServer;
17 * Tests for AwContentsClient.getVisitedHistory and AwContents.doUpdateVisitedHistory callbacks.
19 public class AwContentsClientVisitedHistoryTest extends AwTestBase {
20 private static class GetVisitedHistoryHelper extends CallbackHelper {
21 private ValueCallback<String[]> mCallback;
22 private boolean mSaveCallback = false;
24 public ValueCallback<String[]> getCallback() {
25 assert getCallCount() > 0;
29 public void setSaveCallback(boolean value) {
30 mSaveCallback = value;
33 public void notifyCalled(ValueCallback<String[]> callback) {
41 private static class VisitedHistoryTestAwContentsClient extends TestAwContentsClient {
43 private GetVisitedHistoryHelper mGetVisitedHistoryHelper;
45 public VisitedHistoryTestAwContentsClient() {
46 mGetVisitedHistoryHelper = new GetVisitedHistoryHelper();
49 public GetVisitedHistoryHelper getGetVisitedHistoryHelper() {
50 return mGetVisitedHistoryHelper;
54 public void getVisitedHistory(ValueCallback<String[]> callback) {
55 getGetVisitedHistoryHelper().notifyCalled(callback);
60 private VisitedHistoryTestAwContentsClient mContentsClient =
61 new VisitedHistoryTestAwContentsClient();
63 @Feature({"AndroidWebView"})
65 public void testUpdateVisitedHistoryCallback() throws Throwable {
66 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
67 AwContents awContents = testView.getAwContents();
69 final String path = "/testUpdateVisitedHistoryCallback.html";
70 final String html = "testUpdateVisitedHistoryCallback";
72 TestWebServer webServer = null;
74 webServer = new TestWebServer(false);
75 final String pageUrl = webServer.setResponse(path, html, null);
76 final DoUpdateVisitedHistoryHelper doUpdateVisitedHistoryHelper =
77 mContentsClient.getDoUpdateVisitedHistoryHelper();
78 int callCount = doUpdateVisitedHistoryHelper.getCallCount();
79 loadUrlAsync(awContents, pageUrl);
80 doUpdateVisitedHistoryHelper.waitForCallback(callCount);
81 assertEquals(pageUrl, doUpdateVisitedHistoryHelper.getUrl());
82 assertEquals(false, doUpdateVisitedHistoryHelper.getIsReload());
85 callCount = doUpdateVisitedHistoryHelper.getCallCount();
86 loadUrlAsync(awContents, pageUrl);
87 doUpdateVisitedHistoryHelper.waitForCallback(callCount);
88 assertEquals(pageUrl, doUpdateVisitedHistoryHelper.getUrl());
89 assertEquals(true, doUpdateVisitedHistoryHelper.getIsReload());
91 if (webServer != null) webServer.shutdown();
95 @Feature({"AndroidWebView"})
97 public void testGetVisitedHistoryExerciseCodePath() throws Throwable {
98 // Due to security/privacy restrictions around the :visited css property, it is not
99 // possible test this end to end without using the flaky and brittle capturing picture of
100 // the web page. So we are doing the next best thing, exercising all the code paths.
101 final GetVisitedHistoryHelper visitedHistoryHelper =
102 mContentsClient.getGetVisitedHistoryHelper();
103 final int callCount = visitedHistoryHelper.getCallCount();
104 visitedHistoryHelper.setSaveCallback(true);
106 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
107 AwContents awContents = testView.getAwContents();
109 final String path = "/testGetVisitedHistoryExerciseCodePath.html";
110 final String visitedLinks[] = {"http://foo.com", "http://bar.com", null};
111 final String html = "<a src=\"http://foo.com\">foo</a><a src=\"http://bar.com\">bar</a>";
113 TestWebServer webServer = null;
115 webServer = new TestWebServer(false);
116 final String pageUrl = webServer.setResponse(path, html, null);
117 loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
118 visitedHistoryHelper.waitForCallback(callCount);
119 assertNotNull(visitedHistoryHelper.getCallback());
121 visitedHistoryHelper.getCallback().onReceiveValue(visitedLinks);
122 visitedHistoryHelper.getCallback().onReceiveValue(null);
124 loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
126 if (webServer != null) webServer.shutdown();
130 @Feature({"AndroidWebView"})
132 public void testGetVisitedHistoryCallbackAfterDestroy() throws Throwable {
133 GetVisitedHistoryHelper visitedHistoryHelper =
134 mContentsClient.getGetVisitedHistoryHelper();
135 visitedHistoryHelper.setSaveCallback(true);
136 final int callCount = visitedHistoryHelper.getCallCount();
137 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
138 AwContents awContents = testView.getAwContents();
139 loadUrlAsync(awContents, "about:blank");
140 visitedHistoryHelper.waitForCallback(callCount);
141 assertNotNull(visitedHistoryHelper.getCallback());
143 destroyAwContentsOnMainSync(awContents);
144 visitedHistoryHelper.getCallback().onReceiveValue(new String[] {"abc.def"});
145 visitedHistoryHelper.getCallback().onReceiveValue(null);