| OLD | NEW |
| 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 static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 | 8 |
| 9 import android.os.Environment; | 9 import android.os.Environment; |
| 10 import android.support.test.InstrumentationRegistry; |
| 10 import android.text.TextUtils; | 11 import android.text.TextUtils; |
| 11 import android.util.Log; | 12 import android.util.Log; |
| 12 | 13 |
| 14 import org.junit.After; |
| 15 import org.junit.Assert; |
| 16 import org.junit.Before; |
| 17 import org.junit.Rule; |
| 18 import org.junit.Test; |
| 19 import org.junit.runner.RunWith; |
| 20 |
| 13 import org.chromium.base.annotations.SuppressFBWarnings; | 21 import org.chromium.base.annotations.SuppressFBWarnings; |
| 14 import org.chromium.base.test.util.CallbackHelper; | 22 import org.chromium.base.test.util.CallbackHelper; |
| 23 import org.chromium.base.test.util.CommandLineFlags; |
| 15 import org.chromium.base.test.util.Manual; | 24 import org.chromium.base.test.util.Manual; |
| 16 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 25 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
| 17 import org.chromium.chrome.browser.tab.Tab; | 26 import org.chromium.chrome.browser.tab.Tab; |
| 18 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 27 import org.chromium.chrome.test.ChromeActivityTestRule; |
| 28 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 19 import org.chromium.content_public.browser.LoadUrlParams; | 29 import org.chromium.content_public.browser.LoadUrlParams; |
| 20 import org.chromium.ui.base.PageTransition; | 30 import org.chromium.ui.base.PageTransition; |
| 21 | 31 |
| 22 import java.io.BufferedReader; | 32 import java.io.BufferedReader; |
| 23 import java.io.File; | 33 import java.io.File; |
| 24 import java.io.FileNotFoundException; | 34 import java.io.FileNotFoundException; |
| 25 import java.io.FileReader; | 35 import java.io.FileReader; |
| 26 import java.io.FileWriter; | 36 import java.io.FileWriter; |
| 27 import java.io.IOException; | 37 import java.io.IOException; |
| 28 import java.io.OutputStreamWriter; | 38 import java.io.OutputStreamWriter; |
| 29 import java.util.Iterator; | 39 import java.util.Iterator; |
| 30 import java.util.LinkedList; | 40 import java.util.LinkedList; |
| 31 import java.util.List; | 41 import java.util.List; |
| 32 import java.util.concurrent.TimeUnit; | 42 import java.util.concurrent.TimeUnit; |
| 33 import java.util.concurrent.TimeoutException; | 43 import java.util.concurrent.TimeoutException; |
| 34 | 44 |
| 35 /** | 45 /** |
| 36 * Popular URL tests (ported from {@link com.android.browser.PopularUrlsTest}). | 46 * Popular URL tests (ported from {@link com.android.browser.PopularUrlsTest}). |
| 37 * <p> | 47 * <p> |
| 38 * These tests read popular URLs from /sdcard/popular_urls.txt, open them one by
one and verify | 48 * These tests read popular URLs from /sdcard/popular_urls.txt, open them one by
one and verify |
| 39 * page load. When aborted, they save the last opened URL in /sdcard/test_status
.txt, so that they | 49 * page load. When aborted, they save the last opened URL in /sdcard/test_status
.txt, so that they |
| 40 * can continue opening the next URL when they are restarted. | 50 * can continue opening the next URL when they are restarted. |
| 41 */ | 51 */ |
| 42 public class PopularUrlsTest extends ChromeActivityTestCaseBase<ChromeActivity>
{ | 52 @RunWith(ChromeJUnit4ClassRunner.class) |
| 53 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, |
| 54 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG}) |
| 55 public class PopularUrlsTest { |
| 56 @Rule |
| 57 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule = |
| 58 new ChromeActivityTestRule<>(ChromeActivity.class); |
| 43 | 59 |
| 44 private static final String TAG = "PopularUrlsTest"; | 60 private static final String TAG = "PopularUrlsTest"; |
| 45 private static final String NEW_LINE = System.getProperty("line.separator"); | 61 private static final String NEW_LINE = System.getProperty("line.separator"); |
| 46 private static final File INPUT_FILE = | 62 private static final File INPUT_FILE = |
| 47 new File(Environment.getExternalStorageDirectory(), "popular_urls.tx
t"); | 63 new File(Environment.getExternalStorageDirectory(), "popular_urls.tx
t"); |
| 48 private static final File OUTPUT_FILE = | 64 private static final File OUTPUT_FILE = |
| 49 new File(Environment.getExternalStorageDirectory(), "test_output.txt
"); | 65 new File(Environment.getExternalStorageDirectory(), "test_output.txt
"); |
| 50 private static final File STATUS_FILE = | 66 private static final File STATUS_FILE = |
| 51 new File(Environment.getExternalStorageDirectory(), "test_status.txt
"); | 67 new File(Environment.getExternalStorageDirectory(), "test_status.txt
"); |
| 52 private static final File FAILURE_FILE = | 68 private static final File FAILURE_FILE = |
| 53 new File(Environment.getExternalStorageDirectory(), "failures.txt"); | 69 new File(Environment.getExternalStorageDirectory(), "failures.txt"); |
| 54 private static final File WAIT_FLAG_FILE = | 70 private static final File WAIT_FLAG_FILE = |
| 55 new File(Environment.getExternalStorageDirectory(), "url-test-short-
wait"); | 71 new File(Environment.getExternalStorageDirectory(), "url-test-short-
wait"); |
| 56 private static final int PERF_LOOPCOUNT = 10; | 72 private static final int PERF_LOOPCOUNT = 10; |
| 57 private static final int STABILITY_LOOPCOUNT = 1; | 73 private static final int STABILITY_LOOPCOUNT = 1; |
| 58 private static final int SHORT_WAIT_TIMEOUT = 1000; | 74 private static final int SHORT_WAIT_TIMEOUT = 1000; |
| 59 | 75 |
| 60 private RunStatus mStatus; | 76 private RunStatus mStatus; |
| 61 private boolean mFailed; | 77 private boolean mFailed; |
| 62 private boolean mDoShortWait; | 78 private boolean mDoShortWait; |
| 63 | 79 |
| 64 public PopularUrlsTest() { | 80 @Before |
| 65 super(ChromeActivity.class); | 81 public void setUp() throws Exception { |
| 66 } | |
| 67 | |
| 68 @Override | |
| 69 protected void setUp() throws Exception { | |
| 70 mStatus = new RunStatus(STATUS_FILE); | 82 mStatus = new RunStatus(STATUS_FILE); |
| 71 mFailed = false; | 83 mFailed = false; |
| 72 mDoShortWait = checkDoShortWait(); | 84 mDoShortWait = checkDoShortWait(); |
| 73 super.setUp(); | 85 mActivityTestRule.startMainActivityFromLauncher(); |
| 74 } | 86 } |
| 75 | 87 |
| 76 @Override | 88 @After |
| 77 protected void tearDown() throws Exception { | 89 public void tearDown() throws Exception { |
| 78 if (mStatus != null) { | 90 if (mStatus != null) { |
| 79 mStatus.cleanUp(); | 91 mStatus.cleanUp(); |
| 80 } | 92 } |
| 81 super.tearDown(); | |
| 82 } | |
| 83 | |
| 84 @Override | |
| 85 public void startMainActivity() throws InterruptedException { | |
| 86 startMainActivityFromLauncher(); | |
| 87 } | 93 } |
| 88 | 94 |
| 89 private BufferedReader getInputStream(File inputFile) throws FileNotFoundExc
eption { | 95 private BufferedReader getInputStream(File inputFile) throws FileNotFoundExc
eption { |
| 90 FileReader fileReader = new FileReader(inputFile); | 96 FileReader fileReader = new FileReader(inputFile); |
| 91 BufferedReader bufferedReader = new BufferedReader(fileReader); | 97 BufferedReader bufferedReader = new BufferedReader(fileReader); |
| 92 | 98 |
| 93 return bufferedReader; | 99 return bufferedReader; |
| 94 } | 100 } |
| 95 | 101 |
| 96 private OutputStreamWriter getOutputStream(File outputFile) throws IOExcepti
on { | 102 private OutputStreamWriter getOutputStream(File outputFile) throws IOExcepti
on { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 /** | 223 /** |
| 218 * Navigates to a URL directly without going through the UrlBar. This bypass
es the page | 224 * Navigates to a URL directly without going through the UrlBar. This bypass
es the page |
| 219 * preloading mechanism of the UrlBar. | 225 * preloading mechanism of the UrlBar. |
| 220 * @param url the page URL | 226 * @param url the page URL |
| 221 * @param failureWriter the writer where failures/crashes/timeouts are logge
d. | 227 * @param failureWriter the writer where failures/crashes/timeouts are logge
d. |
| 222 * @throws IOException unable to read from input or write to writer. | 228 * @throws IOException unable to read from input or write to writer. |
| 223 * @throws InterruptedException the thread was interrupted waiting for the p
age to load. | 229 * @throws InterruptedException the thread was interrupted waiting for the p
age to load. |
| 224 */ | 230 */ |
| 225 public void loadUrl(final String url, OutputStreamWriter failureWriter) | 231 public void loadUrl(final String url, OutputStreamWriter failureWriter) |
| 226 throws InterruptedException, IOException { | 232 throws InterruptedException, IOException { |
| 227 Tab tab = getActivity().getActivityTab(); | 233 Tab tab = mActivityTestRule.getActivity().getActivityTab(); |
| 228 final CallbackHelper loadedCallback = new CallbackHelper(); | 234 final CallbackHelper loadedCallback = new CallbackHelper(); |
| 229 final CallbackHelper failedCallback = new CallbackHelper(); | 235 final CallbackHelper failedCallback = new CallbackHelper(); |
| 230 final CallbackHelper crashedCallback = new CallbackHelper(); | 236 final CallbackHelper crashedCallback = new CallbackHelper(); |
| 231 | 237 |
| 232 tab.addObserver(new EmptyTabObserver() { | 238 tab.addObserver(new EmptyTabObserver() { |
| 233 @Override | 239 @Override |
| 234 public void onPageLoadFinished(Tab tab) { | 240 public void onPageLoadFinished(Tab tab) { |
| 235 loadedCallback.notifyCalled(); | 241 loadedCallback.notifyCalled(); |
| 236 } | 242 } |
| 237 | 243 |
| 238 @Override | 244 @Override |
| 239 public void onPageLoadFailed(Tab tab, int errorCode) { | 245 public void onPageLoadFailed(Tab tab, int errorCode) { |
| 240 failedCallback.notifyCalled(); | 246 failedCallback.notifyCalled(); |
| 241 } | 247 } |
| 242 | 248 |
| 243 @Override | 249 @Override |
| 244 public void onCrash(Tab tab, boolean sadTabShown) { | 250 public void onCrash(Tab tab, boolean sadTabShown) { |
| 245 crashedCallback.notifyCalled(); | 251 crashedCallback.notifyCalled(); |
| 246 } | 252 } |
| 247 }); | 253 }); |
| 248 | 254 |
| 249 getInstrumentation().runOnMainSync(new Runnable() { | 255 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(
) { |
| 250 @Override | 256 @Override |
| 251 public void run() { | 257 public void run() { |
| 252 Tab tab = getActivity().getActivityTab(); | 258 Tab tab = mActivityTestRule.getActivity().getActivityTab(); |
| 253 int pageTransition = PageTransition.TYPED | PageTransition.FROM_
ADDRESS_BAR; | 259 int pageTransition = PageTransition.TYPED | PageTransition.FROM_
ADDRESS_BAR; |
| 254 tab.loadUrl(new LoadUrlParams(url, pageTransition)); | 260 tab.loadUrl(new LoadUrlParams(url, pageTransition)); |
| 255 } | 261 } |
| 256 }); | 262 }); |
| 257 // There are a combination of events ordering in a failure case. | 263 // There are a combination of events ordering in a failure case. |
| 258 // There might be TAB_CRASHED with or without PAGE_LOAD_FINISHED precedi
ng it. | 264 // There might be TAB_CRASHED with or without PAGE_LOAD_FINISHED precedi
ng it. |
| 259 // It is possible to get PAGE_LOAD_FINISHED followed by PAGE_LOAD_FAILED
due to redirects. | 265 // It is possible to get PAGE_LOAD_FINISHED followed by PAGE_LOAD_FAILED
due to redirects. |
| 260 boolean timedout = false; | 266 boolean timedout = false; |
| 261 try { | 267 try { |
| 262 loadedCallback.waitForCallback(0, 1, 2, TimeUnit.MINUTES); | 268 loadedCallback.waitForCallback(0, 1, 2, TimeUnit.MINUTES); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 303 } |
| 298 if (failed) { | 304 if (failed) { |
| 299 logToStream(url + " failed to load!" + NEW_LINE, failureWriter); | 305 logToStream(url + " failed to load!" + NEW_LINE, failureWriter); |
| 300 mFailed = true; | 306 mFailed = true; |
| 301 } | 307 } |
| 302 if (timedout) { | 308 if (timedout) { |
| 303 logToStream(url + " timed out!" + NEW_LINE, failureWriter); | 309 logToStream(url + " timed out!" + NEW_LINE, failureWriter); |
| 304 mFailed = true; | 310 mFailed = true; |
| 305 } | 311 } |
| 306 // Try to stop page load. | 312 // Try to stop page load. |
| 307 getInstrumentation().runOnMainSync(new Runnable() { | 313 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable(
) { |
| 308 @Override | 314 @Override |
| 309 public void run() { | 315 public void run() { |
| 310 getActivity().getActivityTab().stopLoading(); | 316 mActivityTestRule.getActivity().getActivityTab().stopLoading(); |
| 311 } | 317 } |
| 312 }); | 318 }); |
| 313 getInstrumentation().waitForIdleSync(); | 319 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 314 } | 320 } |
| 315 | 321 |
| 316 /** | 322 /** |
| 317 * Loops over a list of URLs, points the browser to each one, and records th
e time elapsed. | 323 * Loops over a list of URLs, points the browser to each one, and records th
e time elapsed. |
| 318 * | 324 * |
| 319 * @param input the reader from which to get the URLs. | 325 * @param input the reader from which to get the URLs. |
| 320 * @param outputWriter the writer to which to output the results. | 326 * @param outputWriter the writer to which to output the results. |
| 321 * @param failureWriter the writer where failures/crashes/timeouts are logge
d. | 327 * @param failureWriter the writer where failures/crashes/timeouts are logge
d. |
| 322 * @param clearCache determines whether the cache is cleared before loading
each page. | 328 * @param clearCache determines whether the cache is cleared before loading
each page. |
| 323 * @param loopCount the number of times to loop through the list of pages. | 329 * @param loopCount the number of times to loop through the list of pages. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 353 while (iterator.hasNext()) { | 359 while (iterator.hasNext()) { |
| 354 page = iterator.next(); | 360 page = iterator.next(); |
| 355 mStatus.setUrl(page); | 361 mStatus.setUrl(page); |
| 356 mStatus.write(); | 362 mStatus.write(); |
| 357 Log.i(TAG, "Start: " + page); | 363 Log.i(TAG, "Start: " + page); |
| 358 | 364 |
| 359 long startTime = System.currentTimeMillis(); | 365 long startTime = System.currentTimeMillis(); |
| 360 loadUrl(page, failureWriter); | 366 loadUrl(page, failureWriter); |
| 361 long stopTime = System.currentTimeMillis(); | 367 long stopTime = System.currentTimeMillis(); |
| 362 | 368 |
| 363 String currentUrl = getActivity().getActivityTab().getUrl(); | 369 String currentUrl = mActivityTestRule.getActivity().getActivityT
ab().getUrl(); |
| 364 Log.i(TAG, "Finish: " + currentUrl); | 370 Log.i(TAG, "Finish: " + currentUrl); |
| 365 logToStream(page + "|" + (stopTime - startTime) + NEW_LINE, outp
utWriter); | 371 logToStream(page + "|" + (stopTime - startTime) + NEW_LINE, outp
utWriter); |
| 366 mStatus.incrementPage(); | 372 mStatus.incrementPage(); |
| 367 } | 373 } |
| 368 mStatus.incrementIteration(); | 374 mStatus.incrementIteration(); |
| 369 mStatus.resetPage(); | 375 mStatus.resetPage(); |
| 370 iterator = pages.iterator(); | 376 iterator = pages.iterator(); |
| 371 } | 377 } |
| 372 } | 378 } |
| 373 | 379 |
| 374 /** | 380 /** |
| 375 * Navigate to all the pages listed in the input. | 381 * Navigate to all the pages listed in the input. |
| 376 * @param perf Whether this is a performance test or stability test. | 382 * @param perf Whether this is a performance test or stability test. |
| 377 * @throws IOException | 383 * @throws IOException |
| 378 * @throws InterruptedException | 384 * @throws InterruptedException |
| 379 */ | 385 */ |
| 380 public void loadPages(boolean perf) throws IOException, InterruptedException
{ | 386 public void loadPages(boolean perf) throws IOException, InterruptedException
{ |
| 381 OutputStreamWriter outputWriter = null; | 387 OutputStreamWriter outputWriter = null; |
| 382 if (perf) { | 388 if (perf) { |
| 383 outputWriter = getOutputStream(OUTPUT_FILE); | 389 outputWriter = getOutputStream(OUTPUT_FILE); |
| 384 } | 390 } |
| 385 OutputStreamWriter failureWriter = getOutputStream(FAILURE_FILE); | 391 OutputStreamWriter failureWriter = getOutputStream(FAILURE_FILE); |
| 386 try { | 392 try { |
| 387 BufferedReader bufferedReader = getInputStream(INPUT_FILE); | 393 BufferedReader bufferedReader = getInputStream(INPUT_FILE); |
| 388 int loopCount = perf ? PERF_LOOPCOUNT : STABILITY_LOOPCOUNT; | 394 int loopCount = perf ? PERF_LOOPCOUNT : STABILITY_LOOPCOUNT; |
| 389 try { | 395 try { |
| 390 loopUrls(bufferedReader, outputWriter, failureWriter, true, loop
Count); | 396 loopUrls(bufferedReader, outputWriter, failureWriter, true, loop
Count); |
| 391 assertFalse( | 397 Assert.assertFalse( |
| 392 String.format("Failed to load all pages. Take a look at
%s", FAILURE_FILE), | 398 String.format("Failed to load all pages. Take a look at
%s", FAILURE_FILE), |
| 393 mFailed); | 399 mFailed); |
| 394 } finally { | 400 } finally { |
| 395 if (bufferedReader != null) { | 401 if (bufferedReader != null) { |
| 396 bufferedReader.close(); | 402 bufferedReader.close(); |
| 397 } | 403 } |
| 398 } | 404 } |
| 399 } catch (FileNotFoundException fnfe) { | 405 } catch (FileNotFoundException fnfe) { |
| 400 Log.e(TAG, fnfe.getMessage(), fnfe); | 406 Log.e(TAG, fnfe.getMessage(), fnfe); |
| 401 fail(String.format("URL file %s is not found.", INPUT_FILE)); | 407 Assert.fail(String.format("URL file %s is not found.", INPUT_FILE)); |
| 402 } finally { | 408 } finally { |
| 403 if (outputWriter != null) { | 409 if (outputWriter != null) { |
| 404 outputWriter.close(); | 410 outputWriter.close(); |
| 405 } | 411 } |
| 406 if (failureWriter != null) { | 412 if (failureWriter != null) { |
| 407 failureWriter.close(); | 413 failureWriter.close(); |
| 408 } | 414 } |
| 409 } | 415 } |
| 410 } | 416 } |
| 411 | 417 |
| 412 /** | 418 /** |
| 413 * Repeats loading all URLs by PERF_LOOPCOUNT times, and records the time ea
ch load takes. | 419 * Repeats loading all URLs by PERF_LOOPCOUNT times, and records the time ea
ch load takes. |
| 414 */ | 420 */ |
| 421 @Test |
| 415 @Manual | 422 @Manual |
| 416 public void testLoadPerformance() throws IOException, InterruptedException { | 423 public void testLoadPerformance() throws IOException, InterruptedException { |
| 417 loadPages(true); | 424 loadPages(true); |
| 418 } | 425 } |
| 419 | 426 |
| 420 /** | 427 /** |
| 421 * Loads all URLs. | 428 * Loads all URLs. |
| 422 */ | 429 */ |
| 430 @Test |
| 423 @Manual | 431 @Manual |
| 424 public void testStability() throws IOException, InterruptedException { | 432 public void testStability() throws IOException, InterruptedException { |
| 425 loadPages(false); | 433 loadPages(false); |
| 426 } | 434 } |
| 427 } | 435 } |
| OLD | NEW |