Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / AwContentsClientVisitedHistoryTest.java
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.
4
5 package org.chromium.android_webview.test;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8 import android.webkit.ValueCallback;
9
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;
15
16 /**
17  * Tests for AwContentsClient.getVisitedHistory and AwContents.doUpdateVisitedHistory callbacks.
18  */
19 public class AwContentsClientVisitedHistoryTest extends AwTestBase {
20     private static class GetVisitedHistoryHelper extends CallbackHelper {
21         private ValueCallback<String[]> mCallback;
22         private boolean mSaveCallback = false;
23
24         public ValueCallback<String[]> getCallback() {
25             assert getCallCount() > 0;
26             return mCallback;
27         }
28
29         public void setSaveCallback(boolean value) {
30             mSaveCallback = value;
31         }
32
33         public void notifyCalled(ValueCallback<String[]> callback) {
34             if (mSaveCallback) {
35                 mCallback = callback;
36             }
37             notifyCalled();
38         }
39     }
40
41     private static class VisitedHistoryTestAwContentsClient extends TestAwContentsClient {
42
43         private GetVisitedHistoryHelper mGetVisitedHistoryHelper;
44
45         public VisitedHistoryTestAwContentsClient() {
46             mGetVisitedHistoryHelper = new GetVisitedHistoryHelper();
47         }
48
49         public GetVisitedHistoryHelper getGetVisitedHistoryHelper() {
50             return mGetVisitedHistoryHelper;
51         }
52
53         @Override
54         public void getVisitedHistory(ValueCallback<String[]> callback) {
55             getGetVisitedHistoryHelper().notifyCalled(callback);
56         }
57
58     }
59
60     private VisitedHistoryTestAwContentsClient mContentsClient =
61         new VisitedHistoryTestAwContentsClient();
62
63     @Feature({"AndroidWebView"})
64     @SmallTest
65     public void testUpdateVisitedHistoryCallback() throws Throwable {
66         AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
67         AwContents awContents = testView.getAwContents();
68
69         final String path = "/testUpdateVisitedHistoryCallback.html";
70         final String html = "testUpdateVisitedHistoryCallback";
71
72         TestWebServer webServer = TestWebServer.start();
73         try {
74             final String pageUrl = webServer.setResponse(path, html, null);
75             final DoUpdateVisitedHistoryHelper doUpdateVisitedHistoryHelper =
76                 mContentsClient.getDoUpdateVisitedHistoryHelper();
77             int callCount = doUpdateVisitedHistoryHelper.getCallCount();
78             loadUrlAsync(awContents, pageUrl);
79             doUpdateVisitedHistoryHelper.waitForCallback(callCount);
80             assertEquals(pageUrl, doUpdateVisitedHistoryHelper.getUrl());
81             assertEquals(false, doUpdateVisitedHistoryHelper.getIsReload());
82
83             // Reload
84             callCount = doUpdateVisitedHistoryHelper.getCallCount();
85             loadUrlAsync(awContents, pageUrl);
86             doUpdateVisitedHistoryHelper.waitForCallback(callCount);
87             assertEquals(pageUrl, doUpdateVisitedHistoryHelper.getUrl());
88             assertEquals(true, doUpdateVisitedHistoryHelper.getIsReload());
89         } finally {
90             webServer.shutdown();
91         }
92     }
93
94     @Feature({"AndroidWebView"})
95     @SmallTest
96     public void testGetVisitedHistoryExerciseCodePath() throws Throwable {
97         // Due to security/privacy restrictions around the :visited css property, it is not
98         // possible test this end to end without using the flaky and brittle capturing picture of
99         // the web page. So we are doing the next best thing, exercising all the code paths.
100         final GetVisitedHistoryHelper visitedHistoryHelper =
101             mContentsClient.getGetVisitedHistoryHelper();
102         final int callCount = visitedHistoryHelper.getCallCount();
103         visitedHistoryHelper.setSaveCallback(true);
104
105         AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
106         AwContents awContents = testView.getAwContents();
107
108         final String path = "/testGetVisitedHistoryExerciseCodePath.html";
109         final String visitedLinks[] = {"http://foo.com", "http://bar.com", null};
110         final String html = "<a src=\"http://foo.com\">foo</a><a src=\"http://bar.com\">bar</a>";
111
112         TestWebServer webServer = TestWebServer.start();
113         try {
114             final String pageUrl = webServer.setResponse(path, html, null);
115             loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
116             visitedHistoryHelper.waitForCallback(callCount);
117             assertNotNull(visitedHistoryHelper.getCallback());
118
119             visitedHistoryHelper.getCallback().onReceiveValue(visitedLinks);
120             visitedHistoryHelper.getCallback().onReceiveValue(null);
121
122             loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), pageUrl);
123         } finally {
124             webServer.shutdown();
125         }
126     }
127
128     @Feature({"AndroidWebView"})
129     @SmallTest
130     public void testGetVisitedHistoryCallbackAfterDestroy() throws Throwable {
131         GetVisitedHistoryHelper visitedHistoryHelper =
132             mContentsClient.getGetVisitedHistoryHelper();
133         visitedHistoryHelper.setSaveCallback(true);
134         final int callCount = visitedHistoryHelper.getCallCount();
135         AwTestContainerView testView = createAwTestContainerViewOnMainSync(mContentsClient);
136         AwContents awContents = testView.getAwContents();
137         loadUrlAsync(awContents, "about:blank");
138         visitedHistoryHelper.waitForCallback(callCount);
139         assertNotNull(visitedHistoryHelper.getCallback());
140
141         destroyAwContentsOnMainSync(awContents);
142         visitedHistoryHelper.getCallback().onReceiveValue(new String[] {"abc.def"});
143         visitedHistoryHelper.getCallback().onReceiveValue(null);
144     }
145 }