Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/prerender/ExternalPrerenderRequestTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/prerender/ExternalPrerenderRequestTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/prerender/ExternalPrerenderRequestTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..82b6216f96966629773494bb5d1b8de0cf313698 |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/prerender/ExternalPrerenderRequestTest.java |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.prerender; |
| + |
| +import android.test.suitebuilder.annotation.MediumTest; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| +import org.chromium.chrome.test.util.TestHttpServerClient; |
| +import org.chromium.chrome.testshell.ChromiumTestShellTestBase; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| + |
| +import java.util.concurrent.Callable; |
| +import java.util.concurrent.ExecutionException; |
| +import java.util.concurrent.atomic.AtomicInteger; |
| + |
| +/** |
| + * Tests for adding and removing prerenders using the {@link ExternalPrerenderHandler} |
| + */ |
| +public class ExternalPrerenderRequestTest extends ChromiumTestShellTestBase { |
| + private static final String GOOGLE_URL = |
| + TestHttpServerClient.getUrl("clank/test/data/android/prerender/google.html"); |
|
Yaron
2013/11/05 01:17:54
This will fail immediately on the bots. That file
Yusuf
2013/11/05 21:03:08
Copied the data resources over to chrome. Will rem
|
| + private static final String YOUTUBE_URL = |
| + TestHttpServerClient.getUrl("clank/test/data/android/prerender/youtube.html"); |
| + |
| + private final Callable<Integer> mAddGoogleCallable = new Callable<Integer>() { |
| + @Override |
| + public Integer call() throws Exception { |
| + return mHandler.addPrerender(mProfile, GOOGLE_URL, "", 0, 0); |
| + } |
| + }; |
| + private final Callable<Integer> mAddYouTubeCallable = new Callable<Integer>() { |
| + @Override |
| + public Integer call() throws Exception { |
| + return mHandler.addPrerender(mProfile, YOUTUBE_URL, "", 0, 0); |
| + } |
| + }; |
| + private final Runnable mCancelRunnable = new Runnable() { |
| + @Override |
| + public void run() { |
| + mHandler.cancelCurrentPrerender(); |
| + } |
| + }; |
| + private ExternalPrerenderHandler mHandler; |
| + private Profile mProfile; |
| + |
| + @Override |
| + public void setUp() throws Exception { |
| + super.setUp(); |
| + clearAppData(); |
| + launchChromiumTestShellWithBlankPage(); |
| + assertTrue(waitForActiveShellToBeDoneLoading()); |
| + mHandler = new ExternalPrerenderHandler(); |
|
Yaron
2013/11/05 01:17:54
I think this should be done on the ui thread and n
Yusuf
2013/11/05 21:03:08
Made the tests UIThreadTest
Done.
|
| + final Callable<Profile> profileCallable = new Callable<Profile>() { |
| + @Override |
| + public Profile call() throws Exception { |
| + return Profile.getLastUsedProfile(); |
| + } |
| + }; |
| + mProfile = ThreadUtils.runOnUiThreadBlocking(profileCallable); |
| + } |
| + |
| + @MediumTest |
| + @Feature({"Prerender"}) |
| + /** |
| + * Test adding a prerender and canceling that to add a new one. |
| + */ |
| + public void testAddPrerenderAndCancel() throws ExecutionException, InterruptedException { |
| + final Profile profile = mProfile; |
|
Yaron
2013/11/05 01:17:54
You shouldn't need these. mProfile should be fine
Yusuf
2013/11/05 21:03:08
Done.
|
| + |
| + int webContentsPtr = ThreadUtils.runOnUiThreadBlocking(mAddGoogleCallable); |
| + assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl( |
| + profile, GOOGLE_URL, webContentsPtr)); |
| + |
| + ThreadUtils.runOnUiThreadBlocking(mCancelRunnable); |
| + assertFalse(ExternalPrerenderHandler.hasPrerenderedUrl( |
|
Yaron
2013/11/05 01:17:54
Is this class intentionally thread-safe? It doesn'
Yusuf
2013/11/05 21:03:08
Done.
|
| + profile, GOOGLE_URL, webContentsPtr)); |
| + final AtomicInteger youtubeWebContentsPtr = new AtomicInteger(); |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + youtubeWebContentsPtr.set( |
| + ThreadUtils.runOnUiThreadBlockingNoException(mAddYouTubeCallable)); |
| + return youtubeWebContentsPtr.get() != 0; |
| + } |
| + }, 500, 3000)); |
| + assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl( |
| + profile, YOUTUBE_URL, youtubeWebContentsPtr.get())); |
| + |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Prerender"}) |
| + /** |
| + * Test calling cancel without any added prerenders. |
| + */ |
| + public void testCancelPrerender() throws ExecutionException { |
| + final Profile profile = mProfile; |
| + ThreadUtils.runOnUiThreadBlocking(mCancelRunnable); |
| + int webContentsPtr = ThreadUtils.runOnUiThreadBlocking(mAddGoogleCallable); |
| + assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl( |
| + profile, GOOGLE_URL, webContentsPtr)); |
| + } |
| + |
| + @MediumTest |
| + @Feature({"Prerender"}) |
| + /** |
| + * Test adding two prerenders without canceling the first one. |
| + */ |
| + public void testAddingPrerendersInaRow() throws ExecutionException, InterruptedException { |
| + final Profile profile = mProfile; |
| + int webContentsPtr = ThreadUtils.runOnUiThreadBlocking(mAddGoogleCallable); |
| + assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl( |
| + profile, GOOGLE_URL, webContentsPtr)); |
| + final AtomicInteger youtubeWebContentsPtr = new AtomicInteger(); |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + youtubeWebContentsPtr.set( |
| + ThreadUtils.runOnUiThreadBlockingNoException(mAddYouTubeCallable)); |
| + return youtubeWebContentsPtr.get() != 0; |
| + } |
| + }, 500, 3000)); |
| + assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl( |
| + profile, YOUTUBE_URL, youtubeWebContentsPtr.get())); |
| + } |
| +} |