Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/omaha/OmahaUpdateInfoBarTest.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.omaha;
6
7 import android.content.Context;
8 import android.test.suitebuilder.annotation.MediumTest;
9
10 import org.chromium.base.test.util.Feature;
11 import org.chromium.base.test.util.UrlUtils;
12 import org.chromium.chrome.browser.ChromeActivity;
13 import org.chromium.chrome.browser.UrlConstants;
14 import org.chromium.chrome.browser.infobar.InfoBar;
15 import org.chromium.chrome.test.ChromeActivityTestCaseBase;
16 import org.chromium.content.browser.test.util.Criteria;
17 import org.chromium.content.browser.test.util.CriteriaHelper;
18
19 import java.util.List;
20
21 /**
22 * Checks to see that Main is properly displaying InfoBars when an update is det ected.
23 */
24 public class OmahaUpdateInfoBarTest extends ChromeActivityTestCaseBase<ChromeAct ivity> {
25 private static final String HTML_FILENAME_1 =
26 "chrome/test/data/android/omahaupdateinfobar/omaha_update_infobar_te st_page_1.html";
27 private static final String HTML_FILENAME_2 =
28 "chrome/test/data/android/omahaupdateinfobar/omaha_update_infobar_te st_page_2.html";
29
30 private static final long MS_TIMEOUT = 2000;
31 private static final long MS_INTERVAL = 500;
32
33 public OmahaUpdateInfoBarTest() {
34 super(ChromeActivity.class);
35 }
36
37 /** Reports versions that we want back to OmahaClient. */
38 private static class MockVersionNumberGetter extends VersionNumberGetter {
39 // Both of these strings must be of the format "#.#.#.#".
40 private final String mCurrentVersion;
41 private final String mLatestVersion;
42
43 private boolean mAskedForCurrentVersion;
44 private boolean mAskedForLatestVersion;
45
46 public MockVersionNumberGetter(String currentVersion, String latestVersi on) {
47 mCurrentVersion = currentVersion;
48 mLatestVersion = latestVersion;
49 }
50
51 @Override
52 public String getCurrentlyUsedVersion(Context applicationContext) {
53 assertNotNull("Never set the current version", mCurrentVersion);
54 mAskedForCurrentVersion = true;
55 return mCurrentVersion;
56 }
57
58 @Override
59 public String getLatestKnownVersion(
60 Context applicationContext, String prefPackage, String prefLates tVersion) {
61 assertNotNull("Never set the latest version", mLatestVersion);
62 mAskedForLatestVersion = true;
63 return mLatestVersion;
64 }
65
66 public boolean askedForCurrentVersion() {
67 return mAskedForCurrentVersion;
68 }
69
70 public boolean askedForLatestVersion() {
71 return mAskedForLatestVersion;
72 }
73 }
74
75 /** Reports a dummy market URL back to OmahaClient. */
76 private static class MockMarketURLGetter extends MarketURLGetter {
77 private final String mURL;
78
79 MockMarketURLGetter(String url) {
80 mURL = url;
81 }
82
83 @Override
84 public String getMarketURL(
85 Context applicationContext, String prefPackage, String prefMarke tUrl) {
86 return mURL;
87 }
88 }
89
90 private MockVersionNumberGetter mMockVersionNumberGetter;
91 private MockMarketURLGetter mMockMarketURLGetter;
92
93 @Override
94 public void setUp() throws Exception {
95 super.setUp();
96
97 // This test explicitly tests for the infobar, so turn it on.
98 OmahaClient.setEnableUpdateDetection(true);
99 }
100
101 @Override
102 public void startMainActivity() throws InterruptedException {
103 // Don't start Main until we've had a chance to override everything.
104 }
105
106 /**
107 * Prepares Main before actually launching it. This is required since we do n't have all of the
108 * info we need in setUp().
109 * @param currentVersion Version to report as the current version of Chrome
110 * @param latestVersion Version to report is available by Omaha
111 * @param url URL to show. null = NTP.
112 */
113 private void prepareAndStartMainActivity(String currentVersion, String lates tVersion,
114 String url) throws Exception {
115 if (url == null) url = UrlConstants.NTP_URL;
116
117 // Report fake versions back to Main when it asks.
118 mMockVersionNumberGetter = new MockVersionNumberGetter(currentVersion, l atestVersion);
119 OmahaClient.setVersionNumberGetterForTests(mMockVersionNumberGetter);
120
121 // Report a dummy URL to Omaha.
122 mMockMarketURLGetter = new MockMarketURLGetter(
123 "https://market.android.com/details?id=com.google.android.apps.c hrome");
124 OmahaClient.setMarketURLGetterForTests(mMockMarketURLGetter);
125
126 // Start up main.
127 startMainActivityWithURL(url);
128
129 // Check to make sure that the version numbers get queried.
130 assertTrue("Main didn't ask Omaha for version numbers.", versionNumbersQ ueried());
131 }
132
133 private boolean versionNumbersQueried() throws Exception {
134 return CriteriaHelper.pollForCriteria(
135 new Criteria() {
136 @Override
137 public boolean isSatisfied() {
138 return mMockVersionNumberGetter.askedForCurrentVersion()
139 && mMockVersionNumberGetter.askedForLatestVersio n();
140 }
141 },
142 MS_TIMEOUT, MS_INTERVAL);
143 }
144
145 /**
146 * Checks to see if one of the InfoBars is a NavigateToURLInfoBar pointing t o the market.
147 */
148 private boolean correctInfoBarExists() {
149 // Check the current InfoBars for a NavigateToURLInfoBar.
150 List<InfoBar> infobars = getInfoBars();
151 if (infobars == null) {
152 return false;
153 }
154
155 // Check if one of the InfoBars is pointing to the market URL.
156 Context context = getInstrumentation().getTargetContext();
157 String expectedURL = OmahaClient.getMarketURL(context);
158 for (int i = 0; i < infobars.size(); ++i) {
159 if (infobars.get(i) instanceof OmahaUpdateInfobar) {
160 OmahaUpdateInfobar bar = (OmahaUpdateInfobar) infobars.get(i);
161 if (expectedURL.equals(bar.getUrl())) {
162 return true;
163 }
164 }
165 }
166 return false;
167 }
168
169 /**
170 * Waits until the NavigateToURLInfoBar appears for the current tab.
171 */
172 private boolean correctInfoBarAdded() throws Exception {
173 return CriteriaHelper.pollForCriteria(
174 new Criteria() {
175 @Override
176 public boolean isSatisfied() {
177 return correctInfoBarExists();
178
179 }
180 },
181 MS_TIMEOUT, MS_INTERVAL);
182 }
183
184 /**
185 * Waits until the NavigateToURLInfoBar goes away.
186 */
187 private boolean correctInfoBarRemoved() throws Exception {
188 return CriteriaHelper.pollForCriteria(
189 new Criteria() {
190 @Override
191 public boolean isSatisfied() {
192 return !correctInfoBarExists();
193
194 }
195 },
196 MS_TIMEOUT, MS_INTERVAL);
197 }
198
199 /**
200 * Checks to see that the InfoBar is created when an update is detected.
201 */
202 private void checkInfobarAppearsAndIsDismissed(String currentVersion, String latestVersion,
203 String url) throws Exception {
204 prepareAndStartMainActivity(currentVersion, latestVersion, url);
205
206 if (url == null) {
207 // The InfoBar shouldn't be created yet because we're on the NTP.
208 assertFalse("InfoBar shown on NTP.", correctInfoBarAdded());
209
210 // Navigate somewhere else and then check for the InfoBar.
211 loadUrl(UrlUtils.getIsolatedTestFileUrl(HTML_FILENAME_1));
212 assertTrue("InfoBar failed to show after navigating from NTP.",
213 correctInfoBarAdded());
214 } else {
215 // The InfoBar be shown ASAP since we're not on the NTP.
216 assertTrue("InfoBar failed to show.", correctInfoBarAdded());
217 }
218
219 // Make sure the InfoBar doesn't disappear immediately.
220 assertFalse("InfoBar was removed too quickly.", correctInfoBarRemoved()) ;
221
222 // Make sure the InfoBar goes away once we navigate somewhere else on th e same tab.
223 loadUrl(UrlUtils.getIsolatedTestFileUrl(HTML_FILENAME_2));
224 assertTrue("InfoBar is still showing", correctInfoBarRemoved());
225 }
226
227 /**
228 * Makes sure that no InfoBar is shown at all, after the initial page load o r after
229 * moving away.
230 */
231 private void checkInfobarDoesNotAppear(String currentVersion, String latestV ersion, String url)
232 throws Exception {
233 prepareAndStartMainActivity(currentVersion, latestVersion, url);
234 assertFalse("Infobar is showing.", correctInfoBarAdded());
235 loadUrl(UrlUtils.getIsolatedTestFileUrl(HTML_FILENAME_2));
236 assertFalse("Infobar is now showing", correctInfoBarAdded());
237 }
238
239 @MediumTest
240 @Feature({"Omaha"})
241 public void testCurrentVersionIsOlderNTP() throws Exception {
242 checkInfobarAppearsAndIsDismissed("0.0.0.0", "1.2.3.4", null);
243 }
244
245 @MediumTest
246 @Feature({"Omaha"})
247 public void testCurrentVersionIsOlderNotNTP() throws Exception {
248 checkInfobarAppearsAndIsDismissed("0.0.0.0", "1.2.3.4",
249 UrlUtils.getIsolatedTestFileUrl(HTML_FILENAME_1));
250 }
251
252 @MediumTest
253 @Feature({"Omaha"})
254 public void testCurrentVersionIsSameNTP() throws Exception {
255 checkInfobarDoesNotAppear("1.2.3.4", "1.2.3.4", null);
256 }
257
258 @MediumTest
259 @Feature({"Omaha"})
260 public void testCurrentVersionIsSameNotNTP() throws Exception {
261 checkInfobarDoesNotAppear("1.2.3.4", "1.2.3.4",
262 UrlUtils.getIsolatedTestFileUrl(HTML_FILENAME_1));
263 }
264
265 @MediumTest
266 @Feature({"Omaha"})
267 public void testCurrentVersionIsNewerNTP() throws Exception {
268 checkInfobarDoesNotAppear("27.0.1453.42", "26.0.1410.49", null);
269 }
270
271 @MediumTest
272 @Feature({"Omaha"})
273 public void testCurrentVersionIsNewerNotNTP() throws Exception {
274 checkInfobarDoesNotAppear("27.0.1453.42", "26.0.1410.49",
275 UrlUtils.getIsolatedTestFileUrl(HTML_FILENAME_1));
276 }
277
278 @MediumTest
279 @Feature({"Omaha"})
280 public void testNoVersionKnownNTP() throws Exception {
281 checkInfobarDoesNotAppear("1.2.3.4", "0", null);
282 }
283
284 @MediumTest
285 @Feature({"Omaha"})
286 public void testNoVersionKnownNotNTP() throws Exception {
287 checkInfobarDoesNotAppear("1.2.3.4", "", UrlUtils.getIsolatedTestFileUrl (HTML_FILENAME_1));
288 }
289 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698