OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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.chrome.browser; | |
6 | |
7 import android.preference.PreferenceScreen; | |
8 import android.support.test.filters.LargeTest; | |
9 import android.support.test.filters.MediumTest; | |
10 import android.util.JsonReader; | |
11 | |
12 import org.chromium.base.ThreadUtils; | |
13 import org.chromium.base.test.util.CallbackHelper; | |
14 import org.chromium.base.test.util.CommandLineFlags; | |
15 import org.chromium.base.test.util.DisabledTest; | |
16 import org.chromium.base.test.util.Feature; | |
17 import org.chromium.base.test.util.RetryOnFailure; | |
18 import org.chromium.chrome.browser.preferences.ButtonPreference; | |
19 import org.chromium.chrome.browser.preferences.Preferences; | |
20 import org.chromium.chrome.browser.preferences.privacy.ClearBrowsingDataPreferen ces; | |
21 import org.chromium.chrome.browser.tab.EmptyTabObserver; | |
22 import org.chromium.chrome.browser.tab.Tab; | |
23 import org.chromium.chrome.browser.tab.TabObserver; | |
24 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | |
25 import org.chromium.chrome.test.util.ActivityUtils; | |
26 import org.chromium.content.browser.ContentViewCore; | |
27 import org.chromium.content.browser.test.util.Criteria; | |
28 import org.chromium.content.browser.test.util.CriteriaHelper; | |
29 import org.chromium.content.browser.test.util.JavaScriptUtils; | |
30 import org.chromium.net.test.EmbeddedTestServer; | |
31 | |
32 import java.io.IOException; | |
33 import java.io.StringReader; | |
34 import java.util.Vector; | |
35 import java.util.concurrent.TimeoutException; | |
36 | |
37 /** | |
38 * UI Tests for the history page. | |
39 */ | |
40 @CommandLineFlags.Add("disable-features=AndroidHistoryManager") | |
41 public class HistoryUITest extends ChromeActivityTestCaseBase<ChromeActivity> { | |
42 private static final String HISTORY_URL = "chrome://history-frame/"; | |
43 | |
44 private EmbeddedTestServer mTestServer; | |
45 | |
46 public HistoryUITest() { | |
47 super(ChromeActivity.class); | |
48 } | |
49 | |
50 @Override | |
51 protected void setUp() throws Exception { | |
52 super.setUp(); | |
53 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation ().getContext()); | |
54 } | |
55 | |
56 @Override | |
57 protected void tearDown() throws Exception { | |
58 mTestServer.stopAndDestroyServer(); | |
59 super.tearDown(); | |
60 } | |
61 | |
62 @Override | |
63 public void startMainActivity() throws InterruptedException { | |
64 startMainActivityOnBlankPage(); | |
65 } | |
66 | |
67 private static class HistoryItem { | |
68 public final String url; | |
69 public final String title; | |
70 | |
71 HistoryItem(String url, String title) { | |
72 this.url = url; | |
73 this.title = title; | |
74 } | |
75 } | |
76 | |
77 private HistoryItem[] getHistoryContents() throws InterruptedException, Time outException { | |
78 getInstrumentation().waitForIdleSync(); | |
79 String jsResults = runJavaScriptCodeInCurrentTab(new StringBuilder() | |
80 .append("var rawResults = document.querySelectorAll('.title');\n ") | |
81 .append("var results = [];\n") | |
82 .append("for (i = 0; i < rawResults.length; ++i) {\n") | |
83 .append(" results.push([rawResults[i].childNodes[0].href,\n") | |
84 .append(" rawResults[i].childNodes[0].childNodes[0].textConte nt]);\n") | |
85 .append("}\n") | |
86 .append("results").toString()); | |
87 | |
88 JsonReader jsonReader = new JsonReader(new StringReader(jsResults)); | |
89 Vector<HistoryItem> results = new Vector<HistoryItem>(); | |
90 try { | |
91 jsonReader.beginArray(); | |
92 while (jsonReader.hasNext()) { | |
93 jsonReader.beginArray(); | |
94 assertTrue(jsonReader.hasNext()); | |
95 String url = jsonReader.nextString(); | |
96 assertTrue(jsonReader.hasNext()); | |
97 String title = jsonReader.nextString(); | |
98 assertFalse(jsonReader.hasNext()); | |
99 jsonReader.endArray(); | |
100 results.add(new HistoryItem(url, title)); | |
101 } | |
102 jsonReader.endArray(); | |
103 jsonReader.close(); | |
104 } catch (IOException ioe) { | |
105 fail("Failed to evaluate JavaScript: " + jsResults + "\n" + ioe); | |
106 } | |
107 | |
108 HistoryItem[] history = new HistoryItem[results.size()]; | |
109 results.toArray(history); | |
110 return history; | |
111 } | |
112 | |
113 private void removeSelectedHistoryEntryAtIndex(int index) | |
114 throws InterruptedException, TimeoutException { | |
115 runJavaScriptCodeInCurrentTab( | |
116 "document.getElementsByClassName('remove-entry')[" + index + "]. click();"); | |
117 } | |
118 | |
119 private int getHistoryLength(ContentViewCore cvc) | |
120 throws InterruptedException, TimeoutException { | |
121 String numResultsString = JavaScriptUtils.executeJavaScriptAndWaitForRes ult( | |
122 cvc.getWebContents(), "document.querySelectorAll('.entry').lengt h"); | |
123 int numResults = Integer.parseInt(numResultsString); | |
124 return numResults; | |
125 } | |
126 | |
127 /** | |
128 * Wait for the UI to show the expected number of results. | |
129 * @param expected The number of results that should be loaded. | |
130 */ | |
131 private void waitForResultCount(final ContentViewCore cvc, final int expecte d) { | |
132 CriteriaHelper.pollInstrumentationThread( | |
133 new Criteria() { | |
134 @Override | |
135 public boolean isSatisfied() { | |
136 try { | |
137 return expected == getHistoryLength(cvc); | |
138 } catch (InterruptedException e) { | |
139 e.printStackTrace(); | |
140 return false; | |
141 } catch (TimeoutException e) { | |
142 e.printStackTrace(); | |
143 return false; | |
144 } | |
145 } | |
146 }); | |
147 } | |
148 | |
149 @MediumTest | |
150 @Feature({"History"}) | |
151 @RetryOnFailure | |
152 public void testSearchHistory() throws InterruptedException, TimeoutExceptio n { | |
153 // Introduce some entries in the history page. | |
154 loadUrl(mTestServer.getURL("/chrome/test/data/android/about.html")); | |
155 loadUrl(mTestServer.getURL("/chrome/test/data/android/get_title_test.htm l")); | |
Dan Beam
2017/04/27 00:38:06
i'm fairly sure these files are re-used elsewhere
Theresa
2017/04/27 17:38:23
They most certainly are.
| |
156 loadUrl(HISTORY_URL); | |
157 waitForResultCount(getActivity().getCurrentContentViewCore(), 2); | |
158 | |
159 // Search for one of them. | |
160 Tab tab = getActivity().getActivityTab(); | |
161 final CallbackHelper loadCallback = new CallbackHelper(); | |
162 TabObserver observer = new EmptyTabObserver() { | |
163 @Override | |
164 public void onLoadStopped(Tab tab, boolean toDifferentDocument) { | |
165 if (tab.getUrl().startsWith(HISTORY_URL)) { | |
166 loadCallback.notifyCalled(); | |
167 } | |
168 } | |
169 }; | |
170 tab.addObserver(observer); | |
171 runJavaScriptCodeInCurrentTab("historyView.setSearch('about')"); | |
172 loadCallback.waitForCallback(0); | |
173 waitForResultCount(getActivity().getCurrentContentViewCore(), 1); | |
174 | |
175 // Delete the search term. | |
176 runJavaScriptCodeInCurrentTab("historyView.setSearch('')"); | |
177 loadCallback.waitForCallback(1); | |
178 waitForResultCount(getActivity().getCurrentContentViewCore(), 2); | |
179 tab.removeObserver(observer); | |
180 } | |
181 | |
182 // @LargeTest | |
183 // @Feature({"History"}) | |
184 @DisabledTest | |
185 public void testRemovingEntries() throws InterruptedException, TimeoutExcept ion { | |
186 // Urls will be visited in reverse order to preserve the array ordering | |
187 // in the history results. | |
188 String[] testUrls = new String[] { | |
189 mTestServer.getURL("/chrome/test/data/android/google.html"), | |
190 mTestServer.getURL("/chrome/test/data/android/about.html"), | |
191 }; | |
192 | |
193 String[] testTitles = new String[testUrls.length]; | |
194 for (int i = testUrls.length - 1; i >= 0; --i) { | |
195 loadUrl(testUrls[i]); | |
196 testTitles[i] = getActivity().getActivityTab().getTitle(); | |
197 } | |
198 | |
199 // Check that the history page contains the visited pages. | |
200 loadUrl(HISTORY_URL); | |
201 waitForResultCount(getActivity().getCurrentContentViewCore(), 2); | |
202 | |
203 HistoryItem[] history = getHistoryContents(); | |
204 for (int i = 0; i < testUrls.length; ++i) { | |
205 assertEquals(testUrls[i], history[i].url); | |
206 assertEquals(testTitles[i], history[i].title); | |
207 } | |
208 | |
209 // Remove the first entry from history. | |
210 assertTrue(history.length >= 1); | |
211 removeSelectedHistoryEntryAtIndex(0); | |
212 waitForResultCount(getActivity().getCurrentContentViewCore(), 1); | |
213 | |
214 // Check that now the first result is the second visited page. | |
215 history = getHistoryContents(); | |
216 assertEquals(testUrls[1], history[0].url); | |
217 assertEquals(testTitles[1], history[0].title); | |
218 } | |
219 | |
220 @LargeTest | |
221 @Feature({"History"}) | |
222 @RetryOnFailure | |
223 public void testClearBrowsingData() throws InterruptedException, TimeoutExce ption { | |
224 // Introduce some entries in the history page. | |
225 loadUrl(mTestServer.getURL("/chrome/test/data/android/google.html")); | |
226 loadUrl(mTestServer.getURL("/chrome/test/data/android/about.html")); | |
227 loadUrl(HISTORY_URL); | |
228 waitForResultCount(getActivity().getCurrentContentViewCore(), 2); | |
229 | |
230 // Trigger cleaning up all the browsing data. JS finishing events will m ake it synchronous | |
231 // to us. | |
232 final Preferences prefActivity = ActivityUtils.waitForActivity( | |
233 getInstrumentation(), Preferences.class, new Runnable() { | |
234 @Override | |
235 public void run() { | |
236 try { | |
237 runJavaScriptCodeInCurrentTab("openClearBrowsingData ()"); | |
238 } catch (InterruptedException e) { | |
239 fail("Exception occurred while attempting to open cl ear browing data"); | |
240 } catch (TimeoutException e) { | |
241 fail("Exception occurred while attempting to open cl ear browing data"); | |
242 } | |
243 } | |
244 }); | |
245 assertNotNull("Could not find the preferences activity", prefActivity); | |
246 | |
247 final ClearBrowsingDataPreferences clearBrowsingFragment = | |
248 (ClearBrowsingDataPreferences) prefActivity.getFragmentForTest() ; | |
249 assertNotNull("Could not find clear browsing data fragment", clearBrowsi ngFragment); | |
250 | |
251 final ChromeActivity mainActivity = ActivityUtils.waitForActivity( | |
252 getInstrumentation(), getActivity().getClass(), new Runnable() { | |
253 @Override | |
254 public void run() { | |
255 ThreadUtils.runOnUiThread(new Runnable() { | |
256 @Override | |
257 public void run() { | |
258 PreferenceScreen screen = | |
259 clearBrowsingFragment.getPreferenceScree n(); | |
260 ButtonPreference clearButton = | |
261 (ButtonPreference) screen.findPreference ( | |
262 ClearBrowsingDataPreferences.PREF_ CLEAR_BUTTON); | |
263 clearButton.getOnPreferenceClickListener().onPre ferenceClick( | |
264 clearButton); | |
265 } | |
266 }); | |
267 } | |
268 }); | |
269 assertNotNull("Main never resumed", mainActivity); | |
270 CriteriaHelper.pollUiThread(new Criteria("Main tab never restored") { | |
271 @Override | |
272 public boolean isSatisfied() { | |
273 return !clearBrowsingFragment.isVisible() | |
274 && mainActivity.getActivityTab() != null | |
275 && !mainActivity.getActivityTab().isFrozen(); | |
276 } | |
277 }); | |
278 JavaScriptUtils.executeJavaScriptAndWaitForResult( | |
279 mainActivity.getCurrentContentViewCore().getWebContents(), "relo adHistory()"); | |
280 waitForResultCount(getActivity().getCurrentContentViewCore(), 0); | |
281 } | |
282 } | |
OLD | NEW |