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

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

Powered by Google App Engine
This is Rietveld 408576698