Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/HistoryUITest.java

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

Powered by Google App Engine
This is Rietveld 408576698