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

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

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

Powered by Google App Engine
This is Rietveld 408576698