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

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

Issue 2766373004: Convert the rest of chrome_public_test_apk InstrumentationTestCases to JUnit4 (Closed)
Patch Set: nits and rebase Created 3 years, 8 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
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.omaha; 5 package org.chromium.chrome.browser.omaha;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.content.SharedPreferences; 8 import android.content.SharedPreferences;
9 import android.support.test.InstrumentationRegistry;
9 import android.support.test.filters.SmallTest; 10 import android.support.test.filters.SmallTest;
10 import android.test.InstrumentationTestCase; 11
12 import org.junit.After;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
11 17
12 import org.chromium.base.test.util.AdvancedMockContext; 18 import org.chromium.base.test.util.AdvancedMockContext;
13 import org.chromium.base.test.util.Feature; 19 import org.chromium.base.test.util.Feature;
20 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
14 import org.chromium.chrome.test.omaha.MockRequestGenerator; 21 import org.chromium.chrome.test.omaha.MockRequestGenerator;
15 import org.chromium.chrome.test.omaha.MockRequestGenerator.DeviceType; 22 import org.chromium.chrome.test.omaha.MockRequestGenerator.DeviceType;
16 23
17 import java.io.ByteArrayInputStream; 24 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream; 25 import java.io.ByteArrayOutputStream;
19 import java.io.IOException; 26 import java.io.IOException;
20 import java.io.InputStream; 27 import java.io.InputStream;
21 import java.io.OutputStream; 28 import java.io.OutputStream;
22 import java.net.HttpURLConnection; 29 import java.net.HttpURLConnection;
23 import java.net.MalformedURLException; 30 import java.net.MalformedURLException;
24 import java.net.SocketTimeoutException; 31 import java.net.SocketTimeoutException;
25 import java.net.URL; 32 import java.net.URL;
26 import java.util.ArrayList; 33 import java.util.ArrayList;
27 import java.util.LinkedList; 34 import java.util.LinkedList;
28 import java.util.List; 35 import java.util.List;
29 36
30 /** 37 /**
31 * Tests for the {@link OmahaClient}. 38 * Tests for the {@link OmahaClient}.
32 * Tests override the original OmahaClient's functions with the MockOmahaClient, which 39 * Tests override the original OmahaClient's functions with the MockOmahaClient, which
33 * provides a way to hook into functions to return values that would normally be provided by the 40 * provides a way to hook into functions to return values that would normally be provided by the
34 * system, such as whether Chrome was installed through the system image. 41 * system, such as whether Chrome was installed through the system image.
35 */ 42 */
36 public class OmahaBaseTest extends InstrumentationTestCase { 43 @RunWith(ChromeJUnit4ClassRunner.class)
44 public class OmahaBaseTest {
37 private static class TimestampPair { 45 private static class TimestampPair {
38 public long timestampNextRequest; 46 public long timestampNextRequest;
39 public long timestampNextPost; 47 public long timestampNextPost;
40 48
41 public TimestampPair(long timestampNextRequest, long timestampNextPost) { 49 public TimestampPair(long timestampNextRequest, long timestampNextPost) {
42 this.timestampNextRequest = timestampNextRequest; 50 this.timestampNextRequest = timestampNextRequest;
43 this.timestampNextPost = timestampNextPost; 51 this.timestampNextPost = timestampNextPost;
44 } 52 }
45 } 53 }
46 54
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 return createOmahaBase( 155 return createOmahaBase(
148 ServerResponse.SUCCESS, ConnectionStatus.RESPONDS, DeviceType.HA NDSET); 156 ServerResponse.SUCCESS, ConnectionStatus.RESPONDS, DeviceType.HA NDSET);
149 } 157 }
150 158
151 private MockOmahaBase createOmahaBase( 159 private MockOmahaBase createOmahaBase(
152 ServerResponse response, ConnectionStatus status, DeviceType deviceT ype) { 160 ServerResponse response, ConnectionStatus status, DeviceType deviceT ype) {
153 MockOmahaBase omahaClient = new MockOmahaBase(mDelegate, response, statu s, deviceType); 161 MockOmahaBase omahaClient = new MockOmahaBase(mDelegate, response, statu s, deviceType);
154 return omahaClient; 162 return omahaClient;
155 } 163 }
156 164
157 @Override 165 @Before
158 protected void setUp() throws Exception { 166 public void setUp() throws Exception {
159 super.setUp(); 167 Context targetContext = InstrumentationRegistry.getInstrumentation().get TargetContext();
160 Context targetContext = getInstrumentation().getTargetContext();
161 OmahaBase.setIsDisabledForTesting(false); 168 OmahaBase.setIsDisabledForTesting(false);
162 mContext = new AdvancedMockContext(targetContext); 169 mContext = new AdvancedMockContext(targetContext);
163 } 170 }
164 171
165 @Override 172 @After
166 public void tearDown() throws Exception { 173 public void tearDown() throws Exception {
167 OmahaBase.setIsDisabledForTesting(true); 174 OmahaBase.setIsDisabledForTesting(true);
168 super.tearDown();
169 } 175 }
170 176
171 private class MockOmahaBase extends OmahaBase { 177 private class MockOmahaBase extends OmahaBase {
172 private final LinkedList<MockConnection> mMockConnections = new LinkedLi st<>(); 178 private final LinkedList<MockConnection> mMockConnections = new LinkedLi st<>();
173 179
174 private final boolean mSendValidResponse; 180 private final boolean mSendValidResponse;
175 private final boolean mConnectionTimesOut; 181 private final boolean mConnectionTimesOut;
176 private final boolean mIsOnTablet; 182 private final boolean mIsOnTablet;
177 183
178 public MockOmahaBase(OmahaDelegate delegate, ServerResponse serverRespon se, 184 public MockOmahaBase(OmahaDelegate delegate, ServerResponse serverRespon se,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 220
215 @Override 221 @Override
216 protected HttpURLConnection createConnection() throws RequestFailureExce ption { 222 protected HttpURLConnection createConnection() throws RequestFailureExce ption {
217 MockConnection connection = null; 223 MockConnection connection = null;
218 try { 224 try {
219 URL url = new URL(mDelegate.getRequestGenerator().getServerUrl() ); 225 URL url = new URL(mDelegate.getRequestGenerator().getServerUrl() );
220 connection = new MockConnection(url, mIsOnTablet, mSendValidResp onse, 226 connection = new MockConnection(url, mIsOnTablet, mSendValidResp onse,
221 mSendInstallEvent, mConnectionTimesOut); 227 mSendInstallEvent, mConnectionTimesOut);
222 mMockConnections.addLast(connection); 228 mMockConnections.addLast(connection);
223 } catch (MalformedURLException e) { 229 } catch (MalformedURLException e) {
224 fail("Caught a malformed URL exception: " + e); 230 Assert.fail("Caught a malformed URL exception: " + e);
225 } 231 }
226 return connection; 232 return connection;
227 } 233 }
228 } 234 }
229 235
236 @Test
230 @SmallTest 237 @SmallTest
231 @Feature({"Omaha"}) 238 @Feature({"Omaha"})
232 public void testPipelineFreshInstall() { 239 public void testPipelineFreshInstall() {
233 final long now = 11684; 240 final long now = 11684;
234 241
235 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 242 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
236 mDelegate.getScheduler().setCurrentTime(now); 243 mDelegate.getScheduler().setCurrentTime(now);
237 244
238 // Trigger Omaha. 245 // Trigger Omaha.
239 mOmahaBase = createOmahaBase(); 246 mOmahaBase = createOmahaBase();
240 mOmahaBase.run(); 247 mOmahaBase.run();
241 248
242 // A fresh install results in two requests to the Omaha server: one for the install request 249 // A fresh install results in two requests to the Omaha server: one for the install request
243 // and one for the ping request. 250 // and one for the ping request.
244 assertTrue(mDelegate.mInstallEventWasSent); 251 Assert.assertTrue(mDelegate.mInstallEventWasSent);
245 assertEquals(1, mDelegate.mPostResults.size()); 252 Assert.assertEquals(1, mDelegate.mPostResults.size());
246 assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.get(0).i ntValue()); 253 Assert.assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.g et(0).intValue());
247 assertEquals(2, mDelegate.mGenerateAndPostRequestResults.size()); 254 Assert.assertEquals(2, mDelegate.mGenerateAndPostRequestResults.size());
248 assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0)); 255 Assert.assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0));
249 assertTrue(mDelegate.mGenerateAndPostRequestResults.get(1)); 256 Assert.assertTrue(mDelegate.mGenerateAndPostRequestResults.get(1));
250 257
251 // Successful requests mean that the next scheduled event should be chec king for when the 258 // Successful requests mean that the next scheduled event should be chec king for when the
252 // user is active. 259 // user is active.
253 assertEquals(now + OmahaBase.MS_BETWEEN_REQUESTS, mDelegate.mNextSchedul edTimestamp); 260 Assert.assertEquals(now + OmahaBase.MS_BETWEEN_REQUESTS, mDelegate.mNext ScheduledTimestamp);
254 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY, 261 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY,
255 mDelegate.mTimestampsOnSaveState); 262 mDelegate.mTimestampsOnSaveState);
256 } 263 }
257 264
265 @Test
258 @SmallTest 266 @SmallTest
259 @Feature({"Omaha"}) 267 @Feature({"Omaha"})
260 public void testPipelineRegularPing() { 268 public void testPipelineRegularPing() {
261 final long now = 11684; 269 final long now = 11684;
262 270
263 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 271 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
264 mDelegate.getScheduler().setCurrentTime(now); 272 mDelegate.getScheduler().setCurrentTime(now);
265 273
266 // Record that an install event has already been sent and that we're due for a new request. 274 // Record that an install event has already been sent and that we're due for a new request.
267 SharedPreferences.Editor editor = OmahaBase.getSharedPreferences(mContex t).edit(); 275 SharedPreferences.Editor editor = OmahaBase.getSharedPreferences(mContex t).edit();
268 editor.putBoolean(OmahaBase.PREF_SEND_INSTALL_EVENT, false); 276 editor.putBoolean(OmahaBase.PREF_SEND_INSTALL_EVENT, false);
269 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, now); 277 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, now);
270 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, now); 278 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, now);
271 editor.apply(); 279 editor.apply();
272 280
273 // Trigger Omaha. 281 // Trigger Omaha.
274 mOmahaBase = createOmahaBase(); 282 mOmahaBase = createOmahaBase();
275 mOmahaBase.run(); 283 mOmahaBase.run();
276 284
277 // Only the regular ping should have been sent. 285 // Only the regular ping should have been sent.
278 assertFalse(mDelegate.mInstallEventWasSent); 286 Assert.assertFalse(mDelegate.mInstallEventWasSent);
279 assertEquals(1, mDelegate.mPostResults.size()); 287 Assert.assertEquals(1, mDelegate.mPostResults.size());
280 assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.get(0).i ntValue()); 288 Assert.assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.g et(0).intValue());
281 assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size()); 289 Assert.assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size());
282 assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0)); 290 Assert.assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0));
283 291
284 // Successful requests mean that the next scheduled event should be chec king for when the 292 // Successful requests mean that the next scheduled event should be chec king for when the
285 // user is active. 293 // user is active.
286 assertEquals(now + OmahaBase.MS_BETWEEN_REQUESTS, mDelegate.mNextSchedul edTimestamp); 294 Assert.assertEquals(now + OmahaBase.MS_BETWEEN_REQUESTS, mDelegate.mNext ScheduledTimestamp);
287 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY, 295 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY,
288 mDelegate.mTimestampsOnSaveState); 296 mDelegate.mTimestampsOnSaveState);
289 } 297 }
290 298
299 @Test
291 @SmallTest 300 @SmallTest
292 @Feature({"Omaha"}) 301 @Feature({"Omaha"})
293 public void testTooEarlyToPing() { 302 public void testTooEarlyToPing() {
294 final long now = 0; 303 final long now = 0;
295 final long later = 10000; 304 final long later = 10000;
296 305
297 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 306 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
298 mDelegate.getScheduler().setCurrentTime(now); 307 mDelegate.getScheduler().setCurrentTime(now);
299 308
300 // Put the time for the next request in the future. 309 // Put the time for the next request in the future.
301 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext); 310 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext);
302 prefs.edit().putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, later).ap ply(); 311 prefs.edit().putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, later).ap ply();
303 312
304 // Trigger Omaha. 313 // Trigger Omaha.
305 mOmahaBase = createOmahaBase(); 314 mOmahaBase = createOmahaBase();
306 mOmahaBase.run(); 315 mOmahaBase.run();
307 316
308 // Nothing should have been POSTed. 317 // Nothing should have been POSTed.
309 assertEquals(0, mDelegate.mPostResults.size()); 318 Assert.assertEquals(0, mDelegate.mPostResults.size());
310 assertEquals(0, mDelegate.mGenerateAndPostRequestResults.size()); 319 Assert.assertEquals(0, mDelegate.mGenerateAndPostRequestResults.size());
311 320
312 // The next scheduled event is the request generation. Because there wa s nothing to POST, 321 // The next scheduled event is the request generation. Because there wa s nothing to POST,
313 // its timestamp should have remained unchanged and shouldn't have been considered when the 322 // its timestamp should have remained unchanged and shouldn't have been considered when the
314 // new alarm was scheduled. 323 // new alarm was scheduled.
315 assertEquals(later, mDelegate.mNextScheduledTimestamp); 324 Assert.assertEquals(later, mDelegate.mNextScheduledTimestamp);
316 checkTimestamps(later, now, mDelegate.mTimestampsOnSaveState); 325 checkTimestamps(later, now, mDelegate.mTimestampsOnSaveState);
317 } 326 }
318 327
328 @Test
319 @SmallTest 329 @SmallTest
320 @Feature({"Omaha"}) 330 @Feature({"Omaha"})
321 public void testTooEarlyToPostExistingRequest() { 331 public void testTooEarlyToPostExistingRequest() {
322 final long timeGeneratedRequest = 0L; 332 final long timeGeneratedRequest = 0L;
323 final long now = 10000L; 333 final long now = 10000L;
324 final long timeSendNewPost = 20000L; 334 final long timeSendNewPost = 20000L;
325 final long timeSendNewRequest = 50000L; 335 final long timeSendNewRequest = 50000L;
326 336
327 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 337 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
328 mDelegate.getScheduler().setCurrentTime(now); 338 mDelegate.getScheduler().setCurrentTime(now);
329 339
330 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext); 340 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext);
331 SharedPreferences.Editor editor = prefs.edit(); 341 SharedPreferences.Editor editor = prefs.edit();
332 342
333 // Make it so that a request was generated and is just waiting to be sen t. 343 // Make it so that a request was generated and is just waiting to be sen t.
334 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, timeSendNewRequ est); 344 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, timeSendNewRequ est);
335 editor.putLong(OmahaBase.PREF_TIMESTAMP_OF_REQUEST, timeGeneratedRequest ); 345 editor.putLong(OmahaBase.PREF_TIMESTAMP_OF_REQUEST, timeGeneratedRequest );
336 editor.putString(OmahaBase.PREF_PERSISTED_REQUEST_ID, "persisted_id"); 346 editor.putString(OmahaBase.PREF_PERSISTED_REQUEST_ID, "persisted_id");
337 347
338 // Put the time for the next post in the future. 348 // Put the time for the next post in the future.
339 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, timeSendN ewPost); 349 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, timeSendN ewPost);
340 editor.apply(); 350 editor.apply();
341 351
342 // Trigger Omaha. 352 // Trigger Omaha.
343 mOmahaBase = createOmahaBase(); 353 mOmahaBase = createOmahaBase();
344 mOmahaBase.run(); 354 mOmahaBase.run();
345 355
346 // Request generation code should be skipped. 356 // Request generation code should be skipped.
347 assertNull(mDelegate.mTimestampsOnRegisterNewRequest); 357 Assert.assertNull(mDelegate.mTimestampsOnRegisterNewRequest);
348 358
349 // Should be too early to post, causing it to be rescheduled. 359 // Should be too early to post, causing it to be rescheduled.
350 assertEquals(1, mDelegate.mPostResults.size()); 360 Assert.assertEquals(1, mDelegate.mPostResults.size());
351 assertEquals(OmahaBase.POST_RESULT_SCHEDULED, mDelegate.mPostResults.get (0).intValue()); 361 Assert.assertEquals(
352 assertEquals(0, mDelegate.mGenerateAndPostRequestResults.size()); 362 OmahaBase.POST_RESULT_SCHEDULED, mDelegate.mPostResults.get(0).i ntValue());
363 Assert.assertEquals(0, mDelegate.mGenerateAndPostRequestResults.size());
353 364
354 // The next scheduled event is the POST. Because request generation cod e wasn't run, the 365 // The next scheduled event is the POST. Because request generation cod e wasn't run, the
355 // timestamp for it shouldn't have changed. 366 // timestamp for it shouldn't have changed.
356 assertEquals(timeSendNewPost, mDelegate.mNextScheduledTimestamp); 367 Assert.assertEquals(timeSendNewPost, mDelegate.mNextScheduledTimestamp);
357 checkTimestamps(timeSendNewRequest, timeSendNewPost, mDelegate.mTimestam psOnSaveState); 368 checkTimestamps(timeSendNewRequest, timeSendNewPost, mDelegate.mTimestam psOnSaveState);
358 } 369 }
359 370
371 @Test
360 @SmallTest 372 @SmallTest
361 @Feature({"Omaha"}) 373 @Feature({"Omaha"})
362 public void testPostExistingRequestSuccessfully() { 374 public void testPostExistingRequestSuccessfully() {
363 final long timeGeneratedRequest = 0L; 375 final long timeGeneratedRequest = 0L;
364 final long now = 10000L; 376 final long now = 10000L;
365 final long timeSendNewPost = now; 377 final long timeSendNewPost = now;
366 final long timeRegisterNewRequest = 20000L; 378 final long timeRegisterNewRequest = 20000L;
367 379
368 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 380 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
369 mDelegate.getScheduler().setCurrentTime(now); 381 mDelegate.getScheduler().setCurrentTime(now);
370 382
371 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext); 383 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext);
372 SharedPreferences.Editor editor = prefs.edit(); 384 SharedPreferences.Editor editor = prefs.edit();
373 385
374 // Make it so that a regular <ping> was generated and is just waiting to be sent. 386 // Make it so that a regular <ping> was generated and is just waiting to be sent.
375 editor.putBoolean(OmahaBase.PREF_SEND_INSTALL_EVENT, false); 387 editor.putBoolean(OmahaBase.PREF_SEND_INSTALL_EVENT, false);
376 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, timeRegisterNew Request); 388 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, timeRegisterNew Request);
377 editor.putLong(OmahaBase.PREF_TIMESTAMP_OF_REQUEST, timeGeneratedRequest ); 389 editor.putLong(OmahaBase.PREF_TIMESTAMP_OF_REQUEST, timeGeneratedRequest );
378 editor.putString(OmahaBase.PREF_PERSISTED_REQUEST_ID, "persisted_id"); 390 editor.putString(OmahaBase.PREF_PERSISTED_REQUEST_ID, "persisted_id");
379 391
380 // Send the POST now. 392 // Send the POST now.
381 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, timeSendN ewPost); 393 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, timeSendN ewPost);
382 editor.apply(); 394 editor.apply();
383 395
384 // Trigger Omaha. 396 // Trigger Omaha.
385 mOmahaBase = createOmahaBase(); 397 mOmahaBase = createOmahaBase();
386 mOmahaBase.run(); 398 mOmahaBase.run();
387 399
388 // Registering code shouldn't have fired. 400 // Registering code shouldn't have fired.
389 assertNull(mDelegate.mTimestampsOnRegisterNewRequest); 401 Assert.assertNull(mDelegate.mTimestampsOnRegisterNewRequest);
390 402
391 // Because we didn't send an install event, only one POST should have oc curred. 403 // Because we didn't send an install event, only one POST should have oc curred.
392 assertEquals(1, mDelegate.mPostResults.size()); 404 Assert.assertEquals(1, mDelegate.mPostResults.size());
393 assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.get(0).i ntValue()); 405 Assert.assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.g et(0).intValue());
394 assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size()); 406 Assert.assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size());
395 assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0)); 407 Assert.assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0));
396 408
397 // The next scheduled event is the request generation because there is n othing to POST. 409 // The next scheduled event is the request generation because there is n othing to POST.
398 // A successful POST adjusts all timestamps for the current time. 410 // A successful POST adjusts all timestamps for the current time.
399 assertEquals(timeRegisterNewRequest, mDelegate.mNextScheduledTimestamp); 411 Assert.assertEquals(timeRegisterNewRequest, mDelegate.mNextScheduledTime stamp);
400 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY, 412 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY,
401 mDelegate.mTimestampsOnSaveState); 413 mDelegate.mTimestampsOnSaveState);
402 } 414 }
403 415
416 @Test
404 @SmallTest 417 @SmallTest
405 @Feature({"Omaha"}) 418 @Feature({"Omaha"})
406 public void testPostExistingButFails() { 419 public void testPostExistingButFails() {
407 final long timeGeneratedRequest = 0L; 420 final long timeGeneratedRequest = 0L;
408 final long now = 10000L; 421 final long now = 10000L;
409 final long timeSendNewPost = now; 422 final long timeSendNewPost = now;
410 final long timeRegisterNewRequest = timeGeneratedRequest + OmahaBase.MS_ BETWEEN_REQUESTS; 423 final long timeRegisterNewRequest = timeGeneratedRequest + OmahaBase.MS_ BETWEEN_REQUESTS;
411 424
412 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 425 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
413 mDelegate.getScheduler().setCurrentTime(now); 426 mDelegate.getScheduler().setCurrentTime(now);
(...skipping 10 matching lines...) Expand all
424 // Send the POST now. 437 // Send the POST now.
425 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, timeSendN ewPost); 438 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEXT_POST_ATTEMPT, timeSendN ewPost);
426 editor.apply(); 439 editor.apply();
427 440
428 // Trigger Omaha. 441 // Trigger Omaha.
429 mOmahaBase = createOmahaBase( 442 mOmahaBase = createOmahaBase(
430 ServerResponse.FAILURE, ConnectionStatus.RESPONDS, DeviceType.HA NDSET); 443 ServerResponse.FAILURE, ConnectionStatus.RESPONDS, DeviceType.HA NDSET);
431 mOmahaBase.run(); 444 mOmahaBase.run();
432 445
433 // Registering code shouldn't have fired. 446 // Registering code shouldn't have fired.
434 assertNull(mDelegate.mTimestampsOnRegisterNewRequest); 447 Assert.assertNull(mDelegate.mTimestampsOnRegisterNewRequest);
435 448
436 // Because we didn't send an install event, only one POST should have oc curred. 449 // Because we didn't send an install event, only one POST should have oc curred.
437 assertEquals(1, mDelegate.mPostResults.size()); 450 Assert.assertEquals(1, mDelegate.mPostResults.size());
438 assertEquals(OmahaBase.POST_RESULT_FAILED, mDelegate.mPostResults.get(0) .intValue()); 451 Assert.assertEquals(OmahaBase.POST_RESULT_FAILED, mDelegate.mPostResults .get(0).intValue());
439 assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size()); 452 Assert.assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size());
440 assertFalse(mDelegate.mGenerateAndPostRequestResults.get(0)); 453 Assert.assertFalse(mDelegate.mGenerateAndPostRequestResults.get(0));
441 454
442 // The next scheduled event should be the POST event, which is delayed b y the base delay 455 // The next scheduled event should be the POST event, which is delayed b y the base delay
443 // because no failures have happened yet. 456 // because no failures have happened yet.
444 assertEquals(mDelegate.mTimestampsOnSaveState.timestampNextPost, 457 Assert.assertEquals(mDelegate.mTimestampsOnSaveState.timestampNextPost,
445 mDelegate.mNextScheduledTimestamp); 458 mDelegate.mNextScheduledTimestamp);
446 checkTimestamps(timeRegisterNewRequest, now + OmahaBase.MS_POST_BASE_DEL AY, 459 checkTimestamps(timeRegisterNewRequest, now + OmahaBase.MS_POST_BASE_DEL AY,
447 mDelegate.mTimestampsOnSaveState); 460 mDelegate.mTimestampsOnSaveState);
448 } 461 }
449 462
463 @Test
450 @SmallTest 464 @SmallTest
451 @Feature({"Omaha"}) 465 @Feature({"Omaha"})
452 public void testTimestampWithinBounds() { 466 public void testTimestampWithinBounds() {
453 final long now = 0L; 467 final long now = 0L;
454 final long timeRegisterNewRequest = OmahaBase.MS_BETWEEN_REQUESTS + 1; 468 final long timeRegisterNewRequest = OmahaBase.MS_BETWEEN_REQUESTS + 1;
455 469
456 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 470 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
457 mDelegate.getScheduler().setCurrentTime(now); 471 mDelegate.getScheduler().setCurrentTime(now);
458 472
459 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext); 473 SharedPreferences prefs = OmahaBase.getSharedPreferences(mContext);
460 SharedPreferences.Editor editor = prefs.edit(); 474 SharedPreferences.Editor editor = prefs.edit();
461 475
462 // Indicate that the next request should be generated way past an expect ed timeframe. 476 // Indicate that the next request should be generated way past an expect ed timeframe.
463 editor.putBoolean(OmahaBase.PREF_SEND_INSTALL_EVENT, false); 477 editor.putBoolean(OmahaBase.PREF_SEND_INSTALL_EVENT, false);
464 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, timeRegisterNew Request); 478 editor.putLong(OmahaBase.PREF_TIMESTAMP_FOR_NEW_REQUEST, timeRegisterNew Request);
465 editor.apply(); 479 editor.apply();
466 480
467 // Trigger Omaha. 481 // Trigger Omaha.
468 mOmahaBase = createOmahaBase(); 482 mOmahaBase = createOmahaBase();
469 mOmahaBase.run(); 483 mOmahaBase.run();
470 484
471 // Request generation code should fire. 485 // Request generation code should fire.
472 assertNotNull(mDelegate.mTimestampsOnRegisterNewRequest); 486 Assert.assertNotNull(mDelegate.mTimestampsOnRegisterNewRequest);
473 487
474 // Because we didn't send an install event, only one POST should have oc curred. 488 // Because we didn't send an install event, only one POST should have oc curred.
475 assertEquals(1, mDelegate.mPostResults.size()); 489 Assert.assertEquals(1, mDelegate.mPostResults.size());
476 assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.get(0).i ntValue()); 490 Assert.assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.g et(0).intValue());
477 assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size()); 491 Assert.assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size());
478 assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0)); 492 Assert.assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0));
479 493
480 // The next scheduled event should be the timestamp for a new request ge neration. 494 // The next scheduled event should be the timestamp for a new request ge neration.
481 assertEquals(mDelegate.mTimestampsOnSaveState.timestampNextRequest, 495 Assert.assertEquals(mDelegate.mTimestampsOnSaveState.timestampNextReques t,
482 mDelegate.mNextScheduledTimestamp); 496 mDelegate.mNextScheduledTimestamp);
483 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY, 497 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY,
484 mDelegate.mTimestampsOnSaveState); 498 mDelegate.mTimestampsOnSaveState);
485 } 499 }
486 500
501 @Test
487 @SmallTest 502 @SmallTest
488 @Feature({"Omaha"}) 503 @Feature({"Omaha"})
489 public void testOverdueRequestCausesNewRegistration() { 504 public void testOverdueRequestCausesNewRegistration() {
490 final long timeGeneratedRequest = 0L; 505 final long timeGeneratedRequest = 0L;
491 final long now = 10000L; 506 final long now = 10000L;
492 final long timeSendNewPost = now; 507 final long timeSendNewPost = now;
493 final long timeRegisterNewRequest = 508 final long timeRegisterNewRequest =
494 timeGeneratedRequest + OmahaBase.MS_BETWEEN_REQUESTS * 5; 509 timeGeneratedRequest + OmahaBase.MS_BETWEEN_REQUESTS * 5;
495 510
496 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC); 511 mDelegate = new MockOmahaDelegate(mContext, DeviceType.HANDSET, InstallS ource.ORGANIC);
(...skipping 12 matching lines...) Expand all
509 524
510 // Trigger Omaha. 525 // Trigger Omaha.
511 mOmahaBase = createOmahaBase(); 526 mOmahaBase = createOmahaBase();
512 mOmahaBase.run(); 527 mOmahaBase.run();
513 528
514 // Registering code shouldn't have fired. 529 // Registering code shouldn't have fired.
515 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now, 530 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now,
516 mDelegate.mTimestampsOnRegisterNewRequest); 531 mDelegate.mTimestampsOnRegisterNewRequest);
517 532
518 // Because we didn't send an install event, only one POST should have oc curred. 533 // Because we didn't send an install event, only one POST should have oc curred.
519 assertEquals(1, mDelegate.mPostResults.size()); 534 Assert.assertEquals(1, mDelegate.mPostResults.size());
520 assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.get(0).i ntValue()); 535 Assert.assertEquals(OmahaBase.POST_RESULT_SENT, mDelegate.mPostResults.g et(0).intValue());
521 assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size()); 536 Assert.assertEquals(1, mDelegate.mGenerateAndPostRequestResults.size());
522 assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0)); 537 Assert.assertTrue(mDelegate.mGenerateAndPostRequestResults.get(0));
523 538
524 // The next scheduled event should be the registration event. 539 // The next scheduled event should be the registration event.
525 assertEquals(mDelegate.mTimestampsOnSaveState.timestampNextRequest, 540 Assert.assertEquals(mDelegate.mTimestampsOnSaveState.timestampNextReques t,
526 mDelegate.mNextScheduledTimestamp); 541 mDelegate.mNextScheduledTimestamp);
527 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY, 542 checkTimestamps(now + OmahaBase.MS_BETWEEN_REQUESTS, now + OmahaBase.MS_ POST_BASE_DELAY,
528 mDelegate.mTimestampsOnSaveState); 543 mDelegate.mTimestampsOnSaveState);
529 } 544 }
530 545
531 private void checkTimestamps( 546 private void checkTimestamps(
532 long expectedRequestTimestamp, long expectedPostTimestamp, Timestamp Pair timestamps) { 547 long expectedRequestTimestamp, long expectedPostTimestamp, Timestamp Pair timestamps) {
533 assertEquals(expectedRequestTimestamp, timestamps.timestampNextRequest); 548 Assert.assertEquals(expectedRequestTimestamp, timestamps.timestampNextRe quest);
534 assertEquals(expectedPostTimestamp, timestamps.timestampNextPost); 549 Assert.assertEquals(expectedPostTimestamp, timestamps.timestampNextPost) ;
535 } 550 }
536 551
537 /** 552 /**
538 * Simulates communication with the actual Omaha server. 553 * Simulates communication with the actual Omaha server.
539 */ 554 */
540 private static class MockConnection extends HttpURLConnection { 555 private static class MockConnection extends HttpURLConnection {
541 // Omaha appends a "/" to the URL. 556 // Omaha appends a "/" to the URL.
542 private static final String STRIPPED_MARKET_URL = 557 private static final String STRIPPED_MARKET_URL =
543 "https://market.android.com/details?id=com.google.android.apps.c hrome"; 558 "https://market.android.com/details?id=com.google.android.apps.c hrome";
544 private static final String MARKET_URL = STRIPPED_MARKET_URL + "/"; 559 private static final String MARKET_URL = STRIPPED_MARKET_URL + "/";
(...skipping 10 matching lines...) Expand all
555 private int mContentLength; 570 private int mContentLength;
556 private int mNumTimesResponseCodeRetrieved; 571 private int mNumTimesResponseCodeRetrieved;
557 private boolean mSentRequest; 572 private boolean mSentRequest;
558 private boolean mGotInputStream; 573 private boolean mGotInputStream;
559 private String mRequestPropertyField; 574 private String mRequestPropertyField;
560 private String mRequestPropertyValue; 575 private String mRequestPropertyValue;
561 576
562 MockConnection(URL url, boolean usingTablet, boolean sendValidResponse, 577 MockConnection(URL url, boolean usingTablet, boolean sendValidResponse,
563 boolean sendInstallEvent, boolean connectionTimesOut) { 578 boolean sendInstallEvent, boolean connectionTimesOut) {
564 super(url); 579 super(url);
565 assertEquals(MockRequestGenerator.SERVER_URL, url.toString()); 580 Assert.assertEquals(MockRequestGenerator.SERVER_URL, url.toString()) ;
566 581
567 String mockResponse = buildServerResponseString(usingTablet, sendIns tallEvent); 582 String mockResponse = buildServerResponseString(usingTablet, sendIns tallEvent);
568 mOutputStream = new ByteArrayOutputStream(); 583 mOutputStream = new ByteArrayOutputStream();
569 mServerResponse = new ByteArrayInputStream(mockResponse.getBytes()); 584 mServerResponse = new ByteArrayInputStream(mockResponse.getBytes());
570 mConnectionTimesOut = connectionTimesOut; 585 mConnectionTimesOut = connectionTimesOut;
571 586
572 if (sendValidResponse) { 587 if (sendValidResponse) {
573 mHTTPResponseCode = 200; 588 mHTTPResponseCode = 200;
574 } else { 589 } else {
575 mHTTPResponseCode = 404; 590 mHTTPResponseCode = 404;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 if (mConnectionTimesOut) { 636 if (mConnectionTimesOut) {
622 throw new SocketTimeoutException("Connection timed out."); 637 throw new SocketTimeoutException("Connection timed out.");
623 } 638 }
624 } 639 }
625 640
626 @Override 641 @Override
627 public void disconnect() {} 642 public void disconnect() {}
628 643
629 @Override 644 @Override
630 public void setDoOutput(boolean value) throws IllegalAccessError { 645 public void setDoOutput(boolean value) throws IllegalAccessError {
631 assertTrue("Told the HTTPUrlConnection to send no request.", value); 646 Assert.assertTrue("Told the HTTPUrlConnection to send no request.", value);
632 } 647 }
633 648
634 @Override 649 @Override
635 public void setFixedLengthStreamingMode(int contentLength) { 650 public void setFixedLengthStreamingMode(int contentLength) {
636 mContentLength = contentLength; 651 mContentLength = contentLength;
637 } 652 }
638 653
639 @Override 654 @Override
640 public int getResponseCode() { 655 public int getResponseCode() {
641 if (mNumTimesResponseCodeRetrieved == 0) { 656 if (mNumTimesResponseCodeRetrieved == 0) {
642 // The output stream should now have the generated XML for the r equest. 657 // The output stream should now have the generated XML for the r equest.
643 // Check if its length is correct. 658 // Check if its length is correct.
644 assertEquals("Expected OmahaBase to write out certain number of bytes", 659 Assert.assertEquals("Expected OmahaBase to write out certain num ber of bytes",
645 mContentLength, mOutputStream.toByteArray().length); 660 mContentLength, mOutputStream.toByteArray().length);
646 } 661 }
647 assertTrue("Tried to retrieve response code more than twice", 662 Assert.assertTrue("Tried to retrieve response code more than twice",
648 mNumTimesResponseCodeRetrieved < 2); 663 mNumTimesResponseCodeRetrieved < 2);
649 mNumTimesResponseCodeRetrieved++; 664 mNumTimesResponseCodeRetrieved++;
650 return mHTTPResponseCode; 665 return mHTTPResponseCode;
651 } 666 }
652 667
653 @Override 668 @Override
654 public OutputStream getOutputStream() throws IOException { 669 public OutputStream getOutputStream() throws IOException {
655 mSentRequest = true; 670 mSentRequest = true;
656 connect(); 671 connect();
657 return mOutputStream; 672 return mOutputStream;
658 } 673 }
659 674
660 public String getOutputStreamContents() { 675 public String getOutputStreamContents() {
661 return mOutputStream.toString(); 676 return mOutputStream.toString();
662 } 677 }
663 678
664 @Override 679 @Override
665 public InputStream getInputStream() { 680 public InputStream getInputStream() {
666 assertTrue("Tried to read server response without sending request.", mSentRequest); 681 Assert.assertTrue(
682 "Tried to read server response without sending request.", mS entRequest);
667 mGotInputStream = true; 683 mGotInputStream = true;
668 return mServerResponse; 684 return mServerResponse;
669 } 685 }
670 686
671 @Override 687 @Override
672 public void addRequestProperty(String field, String newValue) { 688 public void addRequestProperty(String field, String newValue) {
673 mRequestPropertyField = field; 689 mRequestPropertyField = field;
674 mRequestPropertyValue = newValue; 690 mRequestPropertyValue = newValue;
675 } 691 }
676 692
(...skipping 11 matching lines...) Expand all
688 704
689 public String getRequestPropertyField() { 705 public String getRequestPropertyField() {
690 return mRequestPropertyField; 706 return mRequestPropertyField;
691 } 707 }
692 708
693 public String getRequestPropertyValue() { 709 public String getRequestPropertyValue() {
694 return mRequestPropertyValue; 710 return mRequestPropertyValue;
695 } 711 }
696 } 712 }
697 } 713 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698