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.test.util; |
| 6 |
| 7 import android.util.Pair; |
| 8 |
| 9 import com.google.android.apps.chrome.R; |
| 10 |
| 11 import org.chromium.base.ThreadUtils; |
| 12 import org.chromium.chrome.browser.Tab; |
| 13 import org.chromium.chrome.browser.TabLoadStatus; |
| 14 import org.chromium.chrome.browser.omnibox.LocationBarLayout; |
| 15 import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter; |
| 16 import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter.OmniboxResultIt
em; |
| 17 import org.chromium.chrome.browser.omnibox.OmniboxResultsAdapter.OmniboxSuggesti
onDelegate; |
| 18 import org.chromium.chrome.browser.omnibox.OmniboxSuggestion; |
| 19 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
| 20 import org.chromium.content.browser.test.util.Criteria; |
| 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 22 |
| 23 import java.util.concurrent.atomic.AtomicBoolean; |
| 24 import java.util.concurrent.atomic.AtomicReference; |
| 25 |
| 26 /** |
| 27 * Utility class for common methods to test prerendering. |
| 28 */ |
| 29 public class PrerenderTestHelper { |
| 30 private static final int UI_DELAY_MS = 100; |
| 31 private static final int WAIT_FOR_RESPONSE_MS = 10000; |
| 32 private static final int SHORT_TIMEOUT_MS = 200; |
| 33 private static final int MAX_NUM_REPEATS_FOR_TRAINING = 7; |
| 34 |
| 35 private static boolean hasTabPrerenderedUrl(final Tab tab, final String url)
{ |
| 36 final AtomicBoolean result = new AtomicBoolean(); |
| 37 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 38 @Override |
| 39 public void run() { |
| 40 result.set(tab.hasPrerenderedUrl(url)); |
| 41 } |
| 42 }); |
| 43 return result.get(); |
| 44 } |
| 45 |
| 46 /** |
| 47 * Waits for the specified URL to be prerendered. |
| 48 * Returns true if it succeeded, false if it was not prerendered before the
timeout. |
| 49 * shortTimeout should be set to true when expecting this function to return
false, as to |
| 50 * make the tests run faster. |
| 51 */ |
| 52 public static boolean waitForPrerenderUrl(final Tab tab, final String url, |
| 53 boolean shortTimeout) throws InterruptedException { |
| 54 CriteriaHelper.pollForCriteria(new Criteria() { |
| 55 @Override |
| 56 public boolean isSatisfied() { |
| 57 return hasTabPrerenderedUrl(tab, url); |
| 58 } |
| 59 }, shortTimeout ? SHORT_TIMEOUT_MS : WAIT_FOR_RESPONSE_MS, UI_DELAY_MS); |
| 60 |
| 61 return hasTabPrerenderedUrl(tab, url); |
| 62 } |
| 63 |
| 64 private static Pair<OmniboxSuggestion, Integer> waitForOmniboxSuggestion( |
| 65 final LocationBarLayout locationBar, final String url) throws Interr
uptedException { |
| 66 final AtomicReference<Pair<OmniboxSuggestion, Integer>> result = |
| 67 new AtomicReference<Pair<OmniboxSuggestion, Integer>>(); |
| 68 CriteriaHelper.pollForCriteria(new Criteria() { |
| 69 @Override |
| 70 public boolean isSatisfied() { |
| 71 OmniboxResultsAdapter adapter = |
| 72 (OmniboxResultsAdapter) locationBar.getSuggestionList().
getAdapter(); |
| 73 for (int i = 0; i < adapter.getCount(); i++) { |
| 74 OmniboxResultItem popupItem = (OmniboxResultItem) adapter.ge
tItem(i); |
| 75 OmniboxSuggestion matchedSuggestion = popupItem.getSuggestio
n(); |
| 76 if (matchedSuggestion.getUrl().equals(url)) { |
| 77 result.set(new Pair<OmniboxSuggestion, Integer>(matchedS
uggestion, i)); |
| 78 return true; |
| 79 } |
| 80 } |
| 81 return false; |
| 82 } |
| 83 }, SHORT_TIMEOUT_MS, UI_DELAY_MS); |
| 84 return result.get(); |
| 85 } |
| 86 |
| 87 /** |
| 88 * Clears the omnibox. |
| 89 * |
| 90 * @param testBase ChromeTabbedActivityTestBase instance. |
| 91 */ |
| 92 public static void clearOmnibox(ChromeTabbedActivityTestBase testBase) |
| 93 throws InterruptedException { |
| 94 testBase.typeInOmnibox("", false); |
| 95 } |
| 96 |
| 97 /** |
| 98 * Clears the omnibox and types in the url character-by-character. |
| 99 * |
| 100 * @param url url to type into the omnibox. |
| 101 * @param testBase ChromeTabbedActivityTestBase instance. |
| 102 */ |
| 103 public static void clearOmniboxAndTypeUrl(String url, ChromeTabbedActivityTe
stBase testBase) |
| 104 throws InterruptedException { |
| 105 clearOmnibox(testBase); |
| 106 testBase.typeInOmnibox(url, true); |
| 107 } |
| 108 |
| 109 /** |
| 110 * Trains the autocomplete action predictor to pre-render a url. |
| 111 * |
| 112 * @param testUrl Url to prerender |
| 113 * @param testBase ChromeTabbedActivityTestBase instance. |
| 114 */ |
| 115 public static void trainAutocompleteActionPredictorAndTestPrerender(final St
ring testUrl, |
| 116 ChromeTabbedActivityTestBase testBase) |
| 117 throws InterruptedException { |
| 118 // TODO(yusufo): Replace with external prerender handler instead of trai
ning. |
| 119 ChromeTabUtils.newTabFromMenu(testBase.getInstrumentation(), testBase.ge
tActivity()); |
| 120 for (int i = 0; i < MAX_NUM_REPEATS_FOR_TRAINING; i++) { |
| 121 // Navigate to another URL otherwise pre-rendering won't happen. |
| 122 // Sometimes calling clearOmnibox immediately after won't actually |
| 123 // clear it. This seems to help. |
| 124 clearOmnibox(testBase); |
| 125 testBase.typeInOmnibox(testUrl, true); |
| 126 final LocationBarLayout locationBar = |
| 127 (LocationBarLayout) testBase.getActivity().findViewById(R.id
.location_bar); |
| 128 if (waitForPrerenderUrl(testBase.getActivity().getActivityTab(), tes
tUrl, true)) { |
| 129 break; |
| 130 } |
| 131 |
| 132 final Pair<OmniboxSuggestion, Integer> suggestionPair = |
| 133 waitForOmniboxSuggestion(locationBar, testUrl); |
| 134 ThreadUtils.runOnUiThread(new Runnable() { |
| 135 @Override |
| 136 public void run() { |
| 137 OmniboxResultsAdapter ora = |
| 138 (OmniboxResultsAdapter) locationBar.getSuggestionLis
t().getAdapter(); |
| 139 OmniboxSuggestionDelegate suggestionDelegate = |
| 140 ora.getSuggestionDelegate(); |
| 141 suggestionDelegate.onSelection(suggestionPair.first, suggest
ionPair.second); |
| 142 } |
| 143 }); |
| 144 } |
| 145 ChromeTabUtils.closeCurrentTab(testBase.getInstrumentation(), testBase.g
etActivity()); |
| 146 clearOmnibox(testBase); |
| 147 |
| 148 testBase.typeInOmnibox(testUrl, true); |
| 149 final Tab tab = testBase.getActivity().getActivityTab(); |
| 150 ChromeTabbedActivityTestBase.assertTrue("URL was not prerendered.", |
| 151 PrerenderTestHelper.waitForPrerenderUrl(tab, testUrl, false)); |
| 152 } |
| 153 |
| 154 /** |
| 155 * Checks if the load url result is prerendered. |
| 156 * |
| 157 * @param result Result from a page load. |
| 158 * @return Whether the result param indicates a prerendered url. |
| 159 */ |
| 160 public static boolean isLoadUrlResultPrerendered(int result) { |
| 161 return result == TabLoadStatus.FULL_PRERENDERED_PAGE_LOAD |
| 162 || result == TabLoadStatus.PARTIAL_PRERENDERED_PAGE_LOAD; |
| 163 } |
| 164 } |
OLD | NEW |