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 static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 |
| 9 import android.os.Environment; |
| 10 import android.text.TextUtils; |
| 11 import android.util.Log; |
| 12 |
| 13 import org.chromium.base.annotations.SuppressFBWarnings; |
| 14 import org.chromium.base.test.util.Manual; |
| 15 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 16 import org.chromium.content.browser.test.util.CallbackHelper; |
| 17 import org.chromium.content_public.browser.LoadUrlParams; |
| 18 import org.chromium.ui.base.PageTransition; |
| 19 |
| 20 import java.io.BufferedReader; |
| 21 import java.io.File; |
| 22 import java.io.FileNotFoundException; |
| 23 import java.io.FileReader; |
| 24 import java.io.FileWriter; |
| 25 import java.io.IOException; |
| 26 import java.io.OutputStreamWriter; |
| 27 import java.util.Iterator; |
| 28 import java.util.LinkedList; |
| 29 import java.util.List; |
| 30 import java.util.concurrent.TimeUnit; |
| 31 import java.util.concurrent.TimeoutException; |
| 32 |
| 33 /** |
| 34 * Popular URL tests (ported from {@link com.android.browser.PopularUrlsTest}). |
| 35 * <p> |
| 36 * These tests read popular URLs from /sdcard/popular_urls.txt, open them one by
one and verify |
| 37 * page load. When aborted, they save the last opened URL in /sdcard/test_status
.txt, so that they |
| 38 * can continue opening the next URL when they are restarted. |
| 39 */ |
| 40 public class PopularUrlsTest extends ChromeActivityTestCaseBase<ChromeActivity>
{ |
| 41 |
| 42 private static final String TAG = "PopularUrlsTest"; |
| 43 private static final String NEW_LINE = System.getProperty("line.separator"); |
| 44 private static final File INPUT_FILE = |
| 45 new File(Environment.getExternalStorageDirectory(), "popular_urls.tx
t"); |
| 46 private static final File OUTPUT_FILE = |
| 47 new File(Environment.getExternalStorageDirectory(), "test_output.txt
"); |
| 48 private static final File STATUS_FILE = |
| 49 new File(Environment.getExternalStorageDirectory(), "test_status.txt
"); |
| 50 private static final File FAILURE_FILE = |
| 51 new File(Environment.getExternalStorageDirectory(), "failures.txt"); |
| 52 private static final File WAIT_FLAG_FILE = |
| 53 new File(Environment.getExternalStorageDirectory(), "url-test-short-
wait"); |
| 54 private static final int PERF_LOOPCOUNT = 10; |
| 55 private static final int STABILITY_LOOPCOUNT = 1; |
| 56 private static final int SHORT_WAIT_TIMEOUT = 1000; |
| 57 |
| 58 private RunStatus mStatus; |
| 59 private boolean mFailed; |
| 60 private boolean mDoShortWait; |
| 61 |
| 62 public PopularUrlsTest() { |
| 63 super(ChromeActivity.class); |
| 64 } |
| 65 |
| 66 @Override |
| 67 protected void setUp() throws Exception { |
| 68 mSkipCheckHttpServer = true; |
| 69 mStatus = new RunStatus(STATUS_FILE); |
| 70 mFailed = false; |
| 71 mDoShortWait = checkDoShortWait(); |
| 72 super.setUp(); |
| 73 } |
| 74 |
| 75 @Override |
| 76 protected void tearDown() throws Exception { |
| 77 if (mStatus != null) { |
| 78 mStatus.cleanUp(); |
| 79 } |
| 80 super.tearDown(); |
| 81 } |
| 82 |
| 83 @Override |
| 84 public void startMainActivity() throws InterruptedException { |
| 85 startMainActivityFromLauncher(); |
| 86 } |
| 87 |
| 88 private BufferedReader getInputStream(File inputFile) throws FileNotFoundExc
eption { |
| 89 FileReader fileReader = new FileReader(inputFile); |
| 90 BufferedReader bufferedReader = new BufferedReader(fileReader); |
| 91 |
| 92 return bufferedReader; |
| 93 } |
| 94 |
| 95 private OutputStreamWriter getOutputStream(File outputFile) throws IOExcepti
on { |
| 96 return new FileWriter(outputFile, mStatus.getIsRecovery()); |
| 97 } |
| 98 |
| 99 private void logToStream(String str, OutputStreamWriter writer) throws IOExc
eption { |
| 100 if (writer != null) { |
| 101 writer.write(str); |
| 102 writer.flush(); |
| 103 } |
| 104 } |
| 105 |
| 106 private boolean checkDoShortWait() { |
| 107 return WAIT_FLAG_FILE.isFile() && WAIT_FLAG_FILE.exists(); |
| 108 } |
| 109 |
| 110 private static class RunStatus { |
| 111 private File mFile; |
| 112 private int mIteration; |
| 113 private int mPage; |
| 114 private String mUrl; |
| 115 private boolean mIsRecovery; |
| 116 private boolean mAllClear; |
| 117 |
| 118 public RunStatus(File file) throws IOException { |
| 119 mFile = file; |
| 120 FileReader input = null; |
| 121 BufferedReader reader = null; |
| 122 mIsRecovery = false; |
| 123 mAllClear = false; |
| 124 mIteration = 0; |
| 125 mPage = 0; |
| 126 try { |
| 127 input = new FileReader(mFile); |
| 128 mIsRecovery = true; |
| 129 reader = new BufferedReader(input); |
| 130 String line = reader.readLine(); |
| 131 if (line == null) { |
| 132 return; |
| 133 } |
| 134 mIteration = Integer.parseInt(line); |
| 135 line = reader.readLine(); |
| 136 if (line == null) { |
| 137 return; |
| 138 } |
| 139 mPage = Integer.parseInt(line); |
| 140 } catch (FileNotFoundException ex) { |
| 141 return; |
| 142 } catch (NumberFormatException nfe) { |
| 143 Log.wtf(TAG, "Unexpected data in status file. Will run for all U
RLs."); |
| 144 return; |
| 145 } finally { |
| 146 try { |
| 147 if (reader != null) { |
| 148 reader.close(); |
| 149 } |
| 150 } finally { |
| 151 if (input != null) { |
| 152 input.close(); |
| 153 } |
| 154 } |
| 155 } |
| 156 } |
| 157 |
| 158 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") |
| 159 public void write() throws IOException { |
| 160 FileWriter output = null; |
| 161 if (mFile.exists()) { |
| 162 mFile.delete(); |
| 163 } |
| 164 try { |
| 165 output = new FileWriter(mFile); |
| 166 output.write(mIteration + NEW_LINE); |
| 167 output.write(mPage + NEW_LINE); |
| 168 output.write(mUrl + NEW_LINE); |
| 169 } finally { |
| 170 if (output != null) { |
| 171 output.close(); |
| 172 } |
| 173 } |
| 174 } |
| 175 |
| 176 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") |
| 177 public void cleanUp() { |
| 178 // Only perform cleanup when mAllClear flag is set, i.e. |
| 179 // when the test was not interrupted by a Java crash. |
| 180 if (mFile.exists() && mAllClear) { |
| 181 mFile.delete(); |
| 182 } |
| 183 } |
| 184 |
| 185 public void resetPage() { |
| 186 mPage = 0; |
| 187 } |
| 188 |
| 189 public void incrementPage() { |
| 190 ++mPage; |
| 191 mAllClear = true; |
| 192 } |
| 193 |
| 194 public void incrementIteration() { |
| 195 ++mIteration; |
| 196 } |
| 197 |
| 198 public int getPage() { |
| 199 return mPage; |
| 200 } |
| 201 |
| 202 public int getIteration() { |
| 203 return mIteration; |
| 204 } |
| 205 |
| 206 public boolean getIsRecovery() { |
| 207 return mIsRecovery; |
| 208 } |
| 209 |
| 210 public void setUrl(String url) { |
| 211 mUrl = url; |
| 212 mAllClear = false; |
| 213 } |
| 214 } |
| 215 |
| 216 /** |
| 217 * Navigates to a URL directly without going through the UrlBar. This bypass
es the page |
| 218 * preloading mechanism of the UrlBar. |
| 219 * @param url the page URL |
| 220 * @param failureWriter the writer where failures/crashes/timeouts are logge
d. |
| 221 * @throws IOException unable to read from input or write to writer. |
| 222 * @throws InterruptedException the thread was interrupted waiting for the p
age to load. |
| 223 */ |
| 224 public void loadUrl(final String url, OutputStreamWriter failureWriter) |
| 225 throws InterruptedException, IOException { |
| 226 Tab tab = getActivity().getActivityTab(); |
| 227 final CallbackHelper loadedCallback = new CallbackHelper(); |
| 228 final CallbackHelper failedCallback = new CallbackHelper(); |
| 229 final CallbackHelper crashedCallback = new CallbackHelper(); |
| 230 |
| 231 tab.addObserver(new EmptyTabObserver() { |
| 232 @Override |
| 233 public void onPageLoadFinished(Tab tab) { |
| 234 loadedCallback.notifyCalled(); |
| 235 } |
| 236 |
| 237 @Override |
| 238 public void onPageLoadFailed(Tab tab, int errorCode) { |
| 239 failedCallback.notifyCalled(); |
| 240 } |
| 241 |
| 242 @Override |
| 243 public void onCrash(Tab tab, boolean sadTabShown) { |
| 244 crashedCallback.notifyCalled(); |
| 245 } |
| 246 }); |
| 247 |
| 248 getInstrumentation().runOnMainSync(new Runnable() { |
| 249 @Override |
| 250 public void run() { |
| 251 Tab tab = getActivity().getActivityTab(); |
| 252 int pageTransition = PageTransition.TYPED | PageTransition.FROM_
ADDRESS_BAR; |
| 253 tab.loadUrl(new LoadUrlParams(url, pageTransition)); |
| 254 } |
| 255 }); |
| 256 // There are a combination of events ordering in a failure case. |
| 257 // There might be TAB_CRASHED with or without PAGE_LOAD_FINISHED precedi
ng it. |
| 258 // It is possible to get PAGE_LOAD_FINISHED followed by PAGE_LOAD_FAILED
due to redirects. |
| 259 boolean timedout = false; |
| 260 try { |
| 261 loadedCallback.waitForCallback(0, 1, 2, TimeUnit.MINUTES); |
| 262 } catch (TimeoutException ex) { |
| 263 timedout = true; |
| 264 } |
| 265 |
| 266 boolean failed = true; |
| 267 boolean crashed = true; |
| 268 if (mDoShortWait) { |
| 269 try { |
| 270 failedCallback.waitForCallback(0, 1, SHORT_WAIT_TIMEOUT, TimeUni
t.MILLISECONDS); |
| 271 } catch (TimeoutException ex) { |
| 272 failed = false; |
| 273 } |
| 274 try { |
| 275 crashedCallback.waitForCallback(0, 1, SHORT_WAIT_TIMEOUT, TimeUn
it.MILLISECONDS); |
| 276 } catch (TimeoutException ex) { |
| 277 crashed = false; |
| 278 } |
| 279 } else { |
| 280 try { |
| 281 failedCallback.waitForCallback( |
| 282 0, 1, scaleTimeout(100 * 1000), TimeUnit.MILLISECONDS); |
| 283 } catch (TimeoutException ex) { |
| 284 failed = false; |
| 285 } |
| 286 try { |
| 287 crashedCallback.waitForCallback( |
| 288 0, 1, scaleTimeout(100 * 1000), TimeUnit.MILLISECONDS); |
| 289 } catch (TimeoutException ex) { |
| 290 crashed = false; |
| 291 } |
| 292 } |
| 293 if (crashed) { |
| 294 logToStream(url + " crashed!" + NEW_LINE, failureWriter); |
| 295 mFailed = true; |
| 296 } |
| 297 if (failed) { |
| 298 logToStream(url + " failed to load!" + NEW_LINE, failureWriter); |
| 299 mFailed = true; |
| 300 } |
| 301 if (timedout) { |
| 302 logToStream(url + " timed out!" + NEW_LINE, failureWriter); |
| 303 mFailed = true; |
| 304 } |
| 305 // Try to stop page load. |
| 306 getInstrumentation().runOnMainSync(new Runnable() { |
| 307 @Override |
| 308 public void run() { |
| 309 getActivity().getActivityTab().stopLoading(); |
| 310 } |
| 311 }); |
| 312 getInstrumentation().waitForIdleSync(); |
| 313 } |
| 314 |
| 315 /** |
| 316 * Loops over a list of URLs, points the browser to each one, and records th
e time elapsed. |
| 317 * |
| 318 * @param input the reader from which to get the URLs. |
| 319 * @param outputWriter the writer to which to output the results. |
| 320 * @param failureWriter the writer where failures/crashes/timeouts are logge
d. |
| 321 * @param clearCache determines whether the cache is cleared before loading
each page. |
| 322 * @param loopCount the number of times to loop through the list of pages. |
| 323 * @throws IOException unable to read from input or write to writer. |
| 324 * @throws InterruptedException the thread was interrupted waiting for the p
age to load. |
| 325 */ |
| 326 private void loopUrls(BufferedReader input, OutputStreamWriter outputWriter, |
| 327 OutputStreamWriter failureWriter, boolean clearCache, int loopCount) |
| 328 throws IOException, InterruptedException { |
| 329 List<String> pages = new LinkedList<String>(); |
| 330 |
| 331 String page; |
| 332 while (null != (page = input.readLine())) { |
| 333 if (!TextUtils.isEmpty(page)) { |
| 334 pages.add(page); |
| 335 } |
| 336 } |
| 337 |
| 338 Iterator<String> iterator = pages.iterator(); |
| 339 for (int i = 0; i < mStatus.getPage(); ++i) { |
| 340 iterator.next(); |
| 341 } |
| 342 |
| 343 if (mStatus.getIsRecovery()) { |
| 344 Log.e(TAG, "Recovering after crash: " + iterator.next()); |
| 345 mStatus.incrementPage(); |
| 346 } |
| 347 |
| 348 while (mStatus.getIteration() < loopCount) { |
| 349 if (clearCache) { |
| 350 // TODO(jingzhao): Clear cache before loading the URL. |
| 351 } |
| 352 while (iterator.hasNext()) { |
| 353 page = iterator.next(); |
| 354 mStatus.setUrl(page); |
| 355 mStatus.write(); |
| 356 Log.i(TAG, "Start: " + page); |
| 357 |
| 358 long startTime = System.currentTimeMillis(); |
| 359 loadUrl(page, failureWriter); |
| 360 long stopTime = System.currentTimeMillis(); |
| 361 |
| 362 String currentUrl = getActivity().getActivityTab().getUrl(); |
| 363 Log.i(TAG, "Finish: " + currentUrl); |
| 364 logToStream(page + "|" + (stopTime - startTime) + NEW_LINE, outp
utWriter); |
| 365 mStatus.incrementPage(); |
| 366 } |
| 367 mStatus.incrementIteration(); |
| 368 mStatus.resetPage(); |
| 369 iterator = pages.iterator(); |
| 370 } |
| 371 } |
| 372 |
| 373 /** |
| 374 * Navigate to all the pages listed in the input. |
| 375 * @param perf Whether this is a performance test or stability test. |
| 376 * @throws IOException |
| 377 * @throws InterruptedException |
| 378 */ |
| 379 public void loadPages(boolean perf) throws IOException, InterruptedException
{ |
| 380 OutputStreamWriter outputWriter = null; |
| 381 if (perf) { |
| 382 outputWriter = getOutputStream(OUTPUT_FILE); |
| 383 } |
| 384 OutputStreamWriter failureWriter = getOutputStream(FAILURE_FILE); |
| 385 try { |
| 386 BufferedReader bufferedReader = getInputStream(INPUT_FILE); |
| 387 int loopCount = perf ? PERF_LOOPCOUNT : STABILITY_LOOPCOUNT; |
| 388 try { |
| 389 loopUrls(bufferedReader, outputWriter, failureWriter, true, loop
Count); |
| 390 assertFalse( |
| 391 String.format("Failed to load all pages. Take a look at
%s", FAILURE_FILE), |
| 392 mFailed); |
| 393 } finally { |
| 394 if (bufferedReader != null) { |
| 395 bufferedReader.close(); |
| 396 } |
| 397 } |
| 398 } catch (FileNotFoundException fnfe) { |
| 399 Log.e(TAG, fnfe.getMessage(), fnfe); |
| 400 fail(String.format("URL file %s is not found.", INPUT_FILE)); |
| 401 } finally { |
| 402 if (outputWriter != null) { |
| 403 outputWriter.close(); |
| 404 } |
| 405 if (failureWriter != null) { |
| 406 failureWriter.close(); |
| 407 } |
| 408 } |
| 409 } |
| 410 |
| 411 /** |
| 412 * Repeats loading all URLs by PERF_LOOPCOUNT times, and records the time ea
ch load takes. |
| 413 */ |
| 414 @Manual |
| 415 public void testLoadPerformance() throws IOException, InterruptedException { |
| 416 loadPages(true); |
| 417 } |
| 418 |
| 419 /** |
| 420 * Loads all URLs. |
| 421 */ |
| 422 @Manual |
| 423 public void testStability() throws IOException, InterruptedException { |
| 424 loadPages(false); |
| 425 } |
| 426 } |
OLD | NEW |