OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.invalidation; | 5 package org.chromium.chrome.browser.invalidation; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.app.Activity; | 8 import android.app.Activity; |
9 import android.content.ContentResolver; | 9 import android.content.ContentResolver; |
10 import android.content.Context; | 10 import android.content.Context; |
11 import android.os.Bundle; | 11 import android.os.Bundle; |
| 12 import android.support.test.InstrumentationRegistry; |
| 13 import android.support.test.annotation.UiThreadTest; |
12 import android.support.test.filters.SmallTest; | 14 import android.support.test.filters.SmallTest; |
13 import android.test.InstrumentationTestCase; | 15 import android.support.test.rule.UiThreadTestRule; |
| 16 |
| 17 import org.junit.Assert; |
| 18 import org.junit.Before; |
| 19 import org.junit.Rule; |
| 20 import org.junit.Test; |
| 21 import org.junit.runner.RunWith; |
14 | 22 |
15 import org.chromium.base.ActivityState; | 23 import org.chromium.base.ActivityState; |
16 import org.chromium.base.ApplicationStatus; | 24 import org.chromium.base.ApplicationStatus; |
17 import org.chromium.base.test.util.Feature; | 25 import org.chromium.base.test.util.Feature; |
| 26 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
18 import org.chromium.components.invalidation.PendingInvalidation; | 27 import org.chromium.components.invalidation.PendingInvalidation; |
19 | 28 |
20 import java.util.List; | 29 import java.util.List; |
21 | 30 |
22 /** | 31 /** |
23 * Tests for DelayedInvalidationsController. | 32 * Tests for DelayedInvalidationsController. |
24 */ | 33 */ |
25 public class DelayedInvalidationsControllerTest extends InstrumentationTestCase
{ | 34 @RunWith(ChromeJUnit4ClassRunner.class) |
| 35 public class DelayedInvalidationsControllerTest { |
26 private static final String TEST_ACCOUNT = "something@gmail.com"; | 36 private static final String TEST_ACCOUNT = "something@gmail.com"; |
27 | 37 |
28 private static final String OBJECT_ID = "object_id"; | 38 private static final String OBJECT_ID = "object_id"; |
29 private static final int OBJECT_SRC = 4; | 39 private static final int OBJECT_SRC = 4; |
30 private static final long VERSION = 1L; | 40 private static final long VERSION = 1L; |
31 private static final String PAYLOAD = "payload"; | 41 private static final String PAYLOAD = "payload"; |
32 | 42 |
33 private static final String OBJECT_ID_2 = "object_id_2"; | 43 private static final String OBJECT_ID_2 = "object_id_2"; |
34 private static final int OBJECT_SRC_2 = 5; | 44 private static final int OBJECT_SRC_2 = 5; |
35 private static final long VERSION_2 = 2L; | 45 private static final long VERSION_2 = 2L; |
36 private static final String PAYLOAD_2 = "payload_2"; | 46 private static final String PAYLOAD_2 = "payload_2"; |
37 | 47 |
38 private MockDelayedInvalidationsController mController; | 48 private MockDelayedInvalidationsController mController; |
39 private Context mContext; | 49 private Context mContext; |
40 private Activity mPlaceholderActivity; | 50 private Activity mPlaceholderActivity; |
41 | 51 |
| 52 @Rule |
| 53 public UiThreadTestRule mRule = new UiThreadTestRule(); |
| 54 |
42 /** | 55 /** |
43 * Mocks {@link DelayedInvalidationsController} for testing. | 56 * Mocks {@link DelayedInvalidationsController} for testing. |
44 * It intercepts access to the Android Sync Adapter. | 57 * It intercepts access to the Android Sync Adapter. |
45 */ | 58 */ |
46 private static class MockDelayedInvalidationsController extends DelayedInval
idationsController { | 59 private static class MockDelayedInvalidationsController extends DelayedInval
idationsController { |
47 private boolean mInvalidated = false; | 60 private boolean mInvalidated = false; |
48 private List<Bundle> mBundles = null; | 61 private List<Bundle> mBundles = null; |
49 | 62 |
50 private MockDelayedInvalidationsController() {} | 63 private MockDelayedInvalidationsController() {} |
51 | 64 |
52 @Override | 65 @Override |
53 void notifyInvalidationsOnBackgroundThread( | 66 void notifyInvalidationsOnBackgroundThread( |
54 Context context, Account account, List<Bundle> bundles) { | 67 Context context, Account account, List<Bundle> bundles) { |
55 mInvalidated = true; | 68 mInvalidated = true; |
56 mBundles = bundles; | 69 mBundles = bundles; |
57 } | 70 } |
58 } | 71 } |
59 | 72 |
60 @Override | 73 @Before |
61 protected void setUp() throws Exception { | 74 public void setUp() throws Exception { |
62 super.setUp(); | |
63 mController = new MockDelayedInvalidationsController(); | 75 mController = new MockDelayedInvalidationsController(); |
64 mContext = getInstrumentation().getTargetContext(); | 76 mContext = InstrumentationRegistry.getInstrumentation().getTargetContext
(); |
65 | 77 |
66 mPlaceholderActivity = new Activity(); | 78 mPlaceholderActivity = new Activity(); |
67 setApplicationState(ActivityState.CREATED); | 79 setApplicationState(ActivityState.CREATED); |
68 setApplicationState(ActivityState.RESUMED); | 80 setApplicationState(ActivityState.RESUMED); |
69 } | 81 } |
70 | 82 |
71 private void setApplicationState(int newState) { | 83 private void setApplicationState(int newState) { |
72 ApplicationStatus.onStateChangeForTesting(mPlaceholderActivity, newState
); | 84 ApplicationStatus.onStateChangeForTesting(mPlaceholderActivity, newState
); |
73 } | 85 } |
74 | 86 |
| 87 @Test |
75 @SmallTest | 88 @SmallTest |
76 @Feature({"Sync"}) | 89 @Feature({"Sync"}) |
| 90 @UiThreadTest |
77 public void testManualSyncRequestsShouldAlwaysTriggerSync() throws Interrupt
edException { | 91 public void testManualSyncRequestsShouldAlwaysTriggerSync() throws Interrupt
edException { |
78 // Sync should trigger for manual requests when Chrome is in the foregro
und. | 92 // Sync should trigger for manual requests when Chrome is in the foregro
und. |
79 Bundle extras = new Bundle(); | 93 Bundle extras = new Bundle(); |
80 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); | 94 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
81 assertTrue(mController.shouldNotifyInvalidation(extras)); | 95 Assert.assertTrue(mController.shouldNotifyInvalidation(extras)); |
82 | 96 |
83 // Sync should trigger for manual requests when Chrome is in the backgro
und. | 97 // Sync should trigger for manual requests when Chrome is in the backgro
und. |
84 setApplicationState(ActivityState.STOPPED); | 98 setApplicationState(ActivityState.STOPPED); |
85 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); | 99 extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
86 assertTrue(mController.shouldNotifyInvalidation(extras)); | 100 Assert.assertTrue(mController.shouldNotifyInvalidation(extras)); |
87 } | 101 } |
88 | 102 |
| 103 @Test |
89 @SmallTest | 104 @SmallTest |
90 @Feature({"Sync", "Invalidation"}) | 105 @Feature({"Sync", "Invalidation"}) |
| 106 @UiThreadTest |
91 public void testInvalidationsTriggeredWhenChromeIsInForeground() { | 107 public void testInvalidationsTriggeredWhenChromeIsInForeground() { |
92 assertTrue(mController.shouldNotifyInvalidation(new Bundle())); | 108 Assert.assertTrue(mController.shouldNotifyInvalidation(new Bundle())); |
93 } | 109 } |
94 | 110 |
| 111 @Test |
95 @SmallTest | 112 @SmallTest |
96 @Feature({"Sync", "Invalidation"}) | 113 @Feature({"Sync", "Invalidation"}) |
| 114 @UiThreadTest |
97 public void testInvalidationsReceivedWhenChromeIsInBackgroundIsDelayed() | 115 public void testInvalidationsReceivedWhenChromeIsInBackgroundIsDelayed() |
98 throws InterruptedException { | 116 throws InterruptedException { |
99 setApplicationState(ActivityState.STOPPED); | 117 setApplicationState(ActivityState.STOPPED); |
100 assertFalse(mController.shouldNotifyInvalidation(new Bundle())); | 118 Assert.assertFalse(mController.shouldNotifyInvalidation(new Bundle())); |
101 } | 119 } |
102 | 120 |
| 121 @Test |
103 @SmallTest | 122 @SmallTest |
104 @Feature({"Sync", "Invalidation"}) | 123 @Feature({"Sync", "Invalidation"}) |
| 124 @UiThreadTest |
105 public void testOnlySpecificInvalidationsTriggeredOnResume() throws Interrup
tedException { | 125 public void testOnlySpecificInvalidationsTriggeredOnResume() throws Interrup
tedException { |
106 // First make sure there are no pending invalidations. | 126 // First make sure there are no pending invalidations. |
107 mController.clearPendingInvalidations(mContext); | 127 mController.clearPendingInvalidations(mContext); |
108 assertFalse(mController.notifyPendingInvalidations(mContext)); | 128 Assert.assertFalse(mController.notifyPendingInvalidations(mContext)); |
109 assertFalse(mController.mInvalidated); | 129 Assert.assertFalse(mController.mInvalidated); |
110 | 130 |
111 // Create some invalidations. | 131 // Create some invalidations. |
112 PendingInvalidation firstInv = | 132 PendingInvalidation firstInv = |
113 new PendingInvalidation(OBJECT_ID, OBJECT_SRC, VERSION, PAYLOAD)
; | 133 new PendingInvalidation(OBJECT_ID, OBJECT_SRC, VERSION, PAYLOAD)
; |
114 PendingInvalidation secondInv = | 134 PendingInvalidation secondInv = |
115 new PendingInvalidation(OBJECT_ID_2, OBJECT_SRC_2, VERSION_2, PA
YLOAD_2); | 135 new PendingInvalidation(OBJECT_ID_2, OBJECT_SRC_2, VERSION_2, PA
YLOAD_2); |
116 | 136 |
117 // Can't invalidate while Chrome is in the background. | 137 // Can't invalidate while Chrome is in the background. |
118 setApplicationState(ActivityState.STOPPED); | 138 setApplicationState(ActivityState.STOPPED); |
119 assertFalse(mController.shouldNotifyInvalidation(new Bundle())); | 139 Assert.assertFalse(mController.shouldNotifyInvalidation(new Bundle())); |
120 | 140 |
121 // Add multiple pending invalidations. | 141 // Add multiple pending invalidations. |
122 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, firstInv); | 142 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, firstInv); |
123 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, secondInv); | 143 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, secondInv); |
124 | 144 |
125 // Make sure there are pending invalidations. | 145 // Make sure there are pending invalidations. |
126 assertTrue(mController.notifyPendingInvalidations(mContext)); | 146 Assert.assertTrue(mController.notifyPendingInvalidations(mContext)); |
127 assertTrue(mController.mInvalidated); | 147 Assert.assertTrue(mController.mInvalidated); |
128 | 148 |
129 // Ensure only specific invalidations are being notified. | 149 // Ensure only specific invalidations are being notified. |
130 assertEquals(2, mController.mBundles.size()); | 150 Assert.assertEquals(2, mController.mBundles.size()); |
131 PendingInvalidation parsedInv1 = new PendingInvalidation(mController.mBu
ndles.get(0)); | 151 PendingInvalidation parsedInv1 = new PendingInvalidation(mController.mBu
ndles.get(0)); |
132 PendingInvalidation parsedInv2 = new PendingInvalidation(mController.mBu
ndles.get(1)); | 152 PendingInvalidation parsedInv2 = new PendingInvalidation(mController.mBu
ndles.get(1)); |
133 assertTrue(firstInv.equals(parsedInv1) ^ firstInv.equals(parsedInv2)); | 153 Assert.assertTrue(firstInv.equals(parsedInv1) ^ firstInv.equals(parsedIn
v2)); |
134 assertTrue(secondInv.equals(parsedInv1) ^ secondInv.equals(parsedInv2)); | 154 Assert.assertTrue(secondInv.equals(parsedInv1) ^ secondInv.equals(parsed
Inv2)); |
135 } | 155 } |
136 | 156 |
| 157 @Test |
137 @SmallTest | 158 @SmallTest |
138 @Feature({"Sync", "Invalidation"}) | 159 @Feature({"Sync", "Invalidation"}) |
| 160 @UiThreadTest |
139 public void testAllInvalidationsTriggeredOnResume() throws InterruptedExcept
ion { | 161 public void testAllInvalidationsTriggeredOnResume() throws InterruptedExcept
ion { |
140 // First make sure there are no pending invalidations. | 162 // First make sure there are no pending invalidations. |
141 mController.clearPendingInvalidations(mContext); | 163 mController.clearPendingInvalidations(mContext); |
142 assertFalse(mController.notifyPendingInvalidations(mContext)); | 164 Assert.assertFalse(mController.notifyPendingInvalidations(mContext)); |
143 assertFalse(mController.mInvalidated); | 165 Assert.assertFalse(mController.mInvalidated); |
144 | 166 |
145 // Create some invalidations. | 167 // Create some invalidations. |
146 PendingInvalidation firstInv = | 168 PendingInvalidation firstInv = |
147 new PendingInvalidation(OBJECT_ID, OBJECT_SRC, VERSION, PAYLOAD)
; | 169 new PendingInvalidation(OBJECT_ID, OBJECT_SRC, VERSION, PAYLOAD)
; |
148 PendingInvalidation secondInv = | 170 PendingInvalidation secondInv = |
149 new PendingInvalidation(OBJECT_ID_2, OBJECT_SRC_2, VERSION_2, PA
YLOAD_2); | 171 new PendingInvalidation(OBJECT_ID_2, OBJECT_SRC_2, VERSION_2, PA
YLOAD_2); |
150 PendingInvalidation allInvalidations = new PendingInvalidation(new Bundl
e()); | 172 PendingInvalidation allInvalidations = new PendingInvalidation(new Bundl
e()); |
151 assertEquals(allInvalidations.mObjectSource, 0); | 173 Assert.assertEquals(allInvalidations.mObjectSource, 0); |
152 | 174 |
153 // Can't invalidate while Chrome is in the background. | 175 // Can't invalidate while Chrome is in the background. |
154 setApplicationState(ActivityState.STOPPED); | 176 setApplicationState(ActivityState.STOPPED); |
155 assertFalse(mController.shouldNotifyInvalidation(new Bundle())); | 177 Assert.assertFalse(mController.shouldNotifyInvalidation(new Bundle())); |
156 | 178 |
157 // Add multiple pending invalidations. | 179 // Add multiple pending invalidations. |
158 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, firstInv); | 180 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, firstInv); |
159 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, allInvalidati
ons); | 181 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, allInvalidati
ons); |
160 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, secondInv); | 182 mController.addPendingInvalidation(mContext, TEST_ACCOUNT, secondInv); |
161 | 183 |
162 // Make sure there are pending invalidations. | 184 // Make sure there are pending invalidations. |
163 assertTrue(mController.notifyPendingInvalidations(mContext)); | 185 Assert.assertTrue(mController.notifyPendingInvalidations(mContext)); |
164 assertTrue(mController.mInvalidated); | 186 Assert.assertTrue(mController.mInvalidated); |
165 | 187 |
166 // As Invalidation for all ids has been received, it will supersede all
other invalidations. | 188 // As Invalidation for all ids has been received, it will supersede all
other invalidations. |
167 assertEquals(1, mController.mBundles.size()); | 189 Assert.assertEquals(1, mController.mBundles.size()); |
168 assertEquals(allInvalidations, new PendingInvalidation(mController.mBund
les.get(0))); | 190 Assert.assertEquals(allInvalidations, new PendingInvalidation(mControlle
r.mBundles.get(0))); |
169 } | 191 } |
170 } | 192 } |
OLD | NEW |