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.browser.omaha; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.Intent; |
| 9 import android.test.InstrumentationTestCase; |
| 10 import android.test.suitebuilder.annotation.SmallTest; |
| 11 |
| 12 import org.chromium.base.test.util.AdvancedMockContext; |
| 13 import org.chromium.base.test.util.Feature; |
| 14 |
| 15 /** Tests the ExponentialBackoffScheduler. */ |
| 16 public class ExponentialBackoffSchedulerTest extends InstrumentationTestCase { |
| 17 private static final String INTENT_STRING = "schedulerIntent"; |
| 18 private static final String PREFERENCE_NAME = "scheduler"; |
| 19 private static final long BACKOFF_MS = 15000; |
| 20 private static final long MAX_MS = 1000000; |
| 21 |
| 22 /** |
| 23 * Checks that the correct number of failures are set/reset. |
| 24 */ |
| 25 @SmallTest |
| 26 @Feature({"Omaha", "Sync"}) |
| 27 public void testExponentialBackoffSchedulerFailureSetting() { |
| 28 Context targetContext = getInstrumentation().getTargetContext(); |
| 29 TestContext context = new TestContext(targetContext); |
| 30 |
| 31 ExponentialBackoffScheduler writer = |
| 32 new ExponentialBackoffScheduler(PREFERENCE_NAME, context, BACKOF
F_MS, MAX_MS); |
| 33 ExponentialBackoffScheduler reader = |
| 34 new ExponentialBackoffScheduler(PREFERENCE_NAME, context, BACKOF
F_MS, MAX_MS); |
| 35 |
| 36 assertEquals("Expected no failures for freshly created class", 0, |
| 37 reader.getNumFailedAttempts()); |
| 38 writer.increaseFailedAttempts(); |
| 39 writer.increaseFailedAttempts(); |
| 40 assertEquals("Expected 2 failures after 2 increments.", 2, reader.getNum
FailedAttempts()); |
| 41 writer.resetFailedAttempts(); |
| 42 assertEquals("Expected 0 failures after reset.", 0, reader.getNumFailedA
ttempts()); |
| 43 } |
| 44 |
| 45 /** |
| 46 * Check that the delay generated by the scheduler is within the correct ran
ge. |
| 47 */ |
| 48 @SmallTest |
| 49 @Feature({"Omaha", "Sync"}) |
| 50 public void testExponentialBackoffSchedulerDelayCalculation() { |
| 51 Context targetContext = getInstrumentation().getTargetContext(); |
| 52 TestContext context = new TestContext(targetContext); |
| 53 MockExponentialBackoffScheduler scheduler = |
| 54 new MockExponentialBackoffScheduler(PREFERENCE_NAME, context, BA
CKOFF_MS, MAX_MS); |
| 55 |
| 56 Intent intent = new Intent(INTENT_STRING); |
| 57 scheduler.createAlarm(intent); |
| 58 |
| 59 // With no failures, expect the base backoff delay. |
| 60 long delay = scheduler.getAlarmTimestamp() - scheduler.getCurrentTime(); |
| 61 assertEquals("Expected delay of " + BACKOFF_MS + " milliseconds.", BACKO
FF_MS, |
| 62 delay); |
| 63 |
| 64 // With two failures, expect a delay within [BACKOFF_MS, BACKOFF_MS * 2^
2]. |
| 65 scheduler.increaseFailedAttempts(); |
| 66 scheduler.increaseFailedAttempts(); |
| 67 scheduler.createAlarm(intent); |
| 68 |
| 69 delay = scheduler.getAlarmTimestamp() - scheduler.getCurrentTime(); |
| 70 final long minDelay = BACKOFF_MS; |
| 71 final long maxDelay = BACKOFF_MS * (1 << scheduler.getNumFailedAttempts(
)); |
| 72 assertTrue("Expected delay greater than the minimum.", delay >= minDelay
); |
| 73 assertTrue("Expected delay within maximum of " + maxDelay, delay <= maxD
elay); |
| 74 } |
| 75 |
| 76 /** |
| 77 * Check that the alarm is being set by the class. |
| 78 */ |
| 79 @SmallTest |
| 80 @Feature({"Omaha", "Sync"}) |
| 81 public void testExponentialBackoffSchedulerAlarmCreation() { |
| 82 Context targetContext = getInstrumentation().getTargetContext(); |
| 83 TestContext context = new TestContext(targetContext); |
| 84 |
| 85 MockExponentialBackoffScheduler scheduler = |
| 86 new MockExponentialBackoffScheduler(PREFERENCE_NAME, context, BA
CKOFF_MS, MAX_MS); |
| 87 |
| 88 Intent intent = new Intent(INTENT_STRING); |
| 89 scheduler.createAlarm(intent); |
| 90 assertTrue("Never requested the alarm manager.", context.mRequestedAlarm
Manager); |
| 91 assertTrue("Never received a call to set the alarm.", scheduler.getAlarm
WasSet()); |
| 92 } |
| 93 |
| 94 /** |
| 95 * Ensures that the AlarmManager is the only service requested. |
| 96 */ |
| 97 private static class TestContext extends AdvancedMockContext { |
| 98 public boolean mRequestedAlarmManager; |
| 99 |
| 100 public TestContext(Context context) { |
| 101 super(context); |
| 102 } |
| 103 |
| 104 /** |
| 105 * Checks that we're requesting the AlarmManager. |
| 106 * @param name Name of the service. Should be the AlarmManager's servic
e name. |
| 107 * @return null since we can't create an AlarmManager. |
| 108 */ |
| 109 @Override |
| 110 public Object getSystemService(final String name) { |
| 111 assertTrue("Requested service other than AlarmManager.", |
| 112 Context.ALARM_SERVICE.equals(name)); |
| 113 mRequestedAlarmManager = true; |
| 114 return super.getSystemService(name); |
| 115 } |
| 116 } |
| 117 } |
OLD | NEW |