1 // Copyright 2013 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.
5 package org.chromium.chrome.browser.prerender;
7 import android.test.UiThreadTest;
8 import android.test.suitebuilder.annotation.MediumTest;
9 import android.test.suitebuilder.annotation.SmallTest;
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.base.test.util.Feature;
13 import org.chromium.chrome.browser.ContentViewUtil;
14 import org.chromium.chrome.browser.profiles.Profile;
15 import org.chromium.chrome.shell.ChromeShellTestBase;
16 import org.chromium.chrome.test.util.TestHttpServerClient;
17 import org.chromium.content.browser.test.util.Criteria;
18 import org.chromium.content.browser.test.util.CriteriaHelper;
20 import java.util.concurrent.Callable;
23 * Tests for adding and removing prerenders using the {@link ExternalPrerenderHandler}
25 public class ExternalPrerenderRequestTest extends ChromeShellTestBase {
26 private static final String HOMEPAGE_URL =
27 TestHttpServerClient.getUrl("chrome/test/data/android/prerender/homepage.html");
28 private static final String GOOGLE_URL =
29 TestHttpServerClient.getUrl("chrome/test/data/android/prerender/google.html");
30 private static final String YOUTUBE_URL =
31 TestHttpServerClient.getUrl("chrome/test/data/android/prerender/youtube.html");
32 private static final int PRERENDER_DELAY_MS = 500;
33 private static final int CHECK_COOKIE_STORE_FREQUENCY_MS = 200;
35 private ExternalPrerenderHandler mHandler;
36 private Profile mProfile;
39 public void setUp() throws Exception {
42 // Launch with a non-blank homepage, to trigger cookie store loading.
43 launchChromeShellWithUrl(HOMEPAGE_URL);
44 assertTrue(waitForActiveShellToBeDoneLoading());
45 mHandler = new ExternalPrerenderHandler();
46 final Callable<Profile> profileCallable = new Callable<Profile>() {
48 public Profile call() throws Exception {
49 return Profile.getLastUsedProfile();
52 mProfile = ThreadUtils.runOnUiThreadBlocking(profileCallable);
53 final Callable<Boolean> cookieStoreCallable = new Callable<Boolean>() {
55 public Boolean call() throws Exception {
56 return ExternalPrerenderHandler.checkCookieStoreLoadedForTesting(mProfile);
59 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
61 public boolean isSatisfied() {
62 return ThreadUtils.runOnUiThreadBlockingNoException(cookieStoreCallable);
64 }, CHECK_COOKIE_STORE_FREQUENCY_MS, 5000));
69 @Feature({"Prerender"})
71 * Test adding a prerender and canceling that to add a new one.
73 public void testAddPrerenderAndCancel() throws InterruptedException {
74 long webContentsPtr = mHandler.addPrerender(mProfile, GOOGLE_URL, "", 0, 0);
75 assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl(
76 mProfile, GOOGLE_URL, webContentsPtr));
78 mHandler.cancelCurrentPrerender();
79 assertFalse(ExternalPrerenderHandler.hasPrerenderedUrl(
80 mProfile, GOOGLE_URL, webContentsPtr));
81 ContentViewUtil.destroyNativeWebContents(webContentsPtr);
82 Thread.sleep(PRERENDER_DELAY_MS);
83 webContentsPtr = mHandler.addPrerender(mProfile, YOUTUBE_URL, "", 0, 0);
84 assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl(
85 mProfile, YOUTUBE_URL, webContentsPtr));
91 @Feature({"Prerender"})
93 * Test calling cancel without any added prerenders.
95 public void testCancelPrerender() {
96 mHandler.cancelCurrentPrerender();
97 long webContentsPtr = mHandler.addPrerender(mProfile, GOOGLE_URL, "", 0, 0);
98 assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl(
99 mProfile, GOOGLE_URL, webContentsPtr));
104 @Feature({"Prerender"})
106 * Test adding two prerenders without canceling the first one.
108 public void testAddingPrerendersInaRow() throws InterruptedException {
109 long webContentsPtr = mHandler.addPrerender(mProfile, GOOGLE_URL, "", 0, 0);
110 assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl(
111 mProfile, GOOGLE_URL, webContentsPtr));
112 Thread.sleep(PRERENDER_DELAY_MS);
113 long newWebContentsPtr = mHandler.addPrerender(mProfile, YOUTUBE_URL, "", 0, 0);
114 assertTrue(ExternalPrerenderHandler.hasPrerenderedUrl(
115 mProfile, YOUTUBE_URL, newWebContentsPtr));