OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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.sync.notifier; | 5 package org.chromium.sync.notifier; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.content.ComponentName; | 8 import android.content.ComponentName; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.os.Bundle; | 10 import android.os.Bundle; |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 public void testReady() { | 134 public void testReady() { |
135 /** | 135 /** |
136 * Test plan: call ready. Verify that the service sets the client id corr
ectly and reissues | 136 * Test plan: call ready. Verify that the service sets the client id corr
ectly and reissues |
137 * pending registrations. | 137 * pending registrations. |
138 */ | 138 */ |
139 | 139 |
140 // Persist some registrations. | 140 // Persist some registrations. |
141 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); | 141 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); |
142 EditContext editContext = invPrefs.edit(); | 142 EditContext editContext = invPrefs.edit(); |
143 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("BOOKMARK
", "SESSION")); | 143 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("BOOKMARK
", "SESSION")); |
| 144 ObjectId objectId = ObjectId.newInstance(1, "obj".getBytes()); |
| 145 invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(objectId)
); |
144 assertTrue(invPrefs.commit(editContext)); | 146 assertTrue(invPrefs.commit(editContext)); |
145 | 147 |
146 // Issue ready. | 148 // Issue ready. |
147 getService().ready(CLIENT_ID); | 149 getService().ready(CLIENT_ID); |
148 assertTrue(Arrays.equals(CLIENT_ID, InvalidationService.getClientIdForTe
st())); | 150 assertTrue(Arrays.equals(CLIENT_ID, InvalidationService.getClientIdForTe
st())); |
149 byte[] otherCid = "otherCid".getBytes(); | 151 byte[] otherCid = "otherCid".getBytes(); |
150 getService().ready(otherCid); | 152 getService().ready(otherCid); |
151 assertTrue(Arrays.equals(otherCid, InvalidationService.getClientIdForTes
t())); | 153 assertTrue(Arrays.equals(otherCid, InvalidationService.getClientIdForTes
t())); |
152 | 154 |
153 // Verify registrations issued. | 155 // Verify registrations issued. |
154 assertEquals(CollectionUtil.newHashSet( | 156 assertEquals(CollectionUtil.newHashSet( |
155 ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId())
, | 157 ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId(),
objectId), |
156 new HashSet<ObjectId>(getService().mRegistrations.get(0))); | 158 new HashSet<ObjectId>(getService().mRegistrations.get(0))); |
157 } | 159 } |
158 | 160 |
159 @SmallTest | 161 @SmallTest |
160 @Feature({"Sync"}) | 162 @Feature({"Sync"}) |
161 public void testReissueRegistrations() { | 163 public void testReissueRegistrations() { |
162 /* | 164 /* |
163 * Test plan: call the reissueRegistrations method of the listener with
both empty and | 165 * Test plan: call the reissueRegistrations method of the listener with
both empty and |
164 * non-empty sets of desired registrations stored in preferences. Verify
that no register | 166 * non-empty sets of desired registrations stored in preferences. Verify
that no register |
165 * intent is set in the first case and that the appropriate register int
ent is sent in | 167 * intent is set in the first case and that the appropriate register int
ent is sent in |
166 * the second. | 168 * the second. |
167 */ | 169 */ |
168 | 170 |
169 // No persisted registrations. | 171 // No persisted registrations. |
170 getService().reissueRegistrations(CLIENT_ID); | 172 getService().reissueRegistrations(CLIENT_ID); |
171 assertTrue(getService().mRegistrations.isEmpty()); | 173 assertTrue(getService().mRegistrations.isEmpty()); |
172 | 174 |
173 // Persist some registrations. | 175 // Persist some registrations. |
174 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); | 176 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); |
175 EditContext editContext = invPrefs.edit(); | 177 EditContext editContext = invPrefs.edit(); |
176 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("BOOKMARK
", "SESSION")); | 178 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("BOOKMARK
", "SESSION")); |
| 179 ObjectId objectId = ObjectId.newInstance(1, "obj".getBytes()); |
| 180 invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(objectId)
); |
177 assertTrue(invPrefs.commit(editContext)); | 181 assertTrue(invPrefs.commit(editContext)); |
178 | 182 |
179 // Reissue registrations and verify that the appropriate registrations a
re issued. | 183 // Reissue registrations and verify that the appropriate registrations a
re issued. |
180 getService().reissueRegistrations(CLIENT_ID); | 184 getService().reissueRegistrations(CLIENT_ID); |
181 assertEquals(1, getService().mRegistrations.size()); | 185 assertEquals(1, getService().mRegistrations.size()); |
182 assertEquals(CollectionUtil.newHashSet( | 186 assertEquals(CollectionUtil.newHashSet( |
183 ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId())
, | 187 ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId(),
objectId), |
184 new HashSet<ObjectId>(getService().mRegistrations.get(0))); | 188 new HashSet<ObjectId>(getService().mRegistrations.get(0))); |
185 } | 189 } |
186 | 190 |
187 @SmallTest | 191 @SmallTest |
188 @Feature({"Sync"}) | 192 @Feature({"Sync"}) |
189 public void testInformRegistrationStatus() { | 193 public void testInformRegistrationStatus() { |
190 /* | 194 /* |
191 * Test plan: call inform registration status under a variety of circums
tances and verify | 195 * Test plan: call inform registration status under a variety of circums
tances and verify |
192 * that the appropriate (un)register calls are issued. | 196 * that the appropriate (un)register calls are issued. |
193 * | 197 * |
194 * 1. Registration of desired object. No calls issued. | 198 * 1. Registration of desired object. No calls issued. |
195 * 2. Unregistration of undesired object. No calls issued. | 199 * 2. Unregistration of undesired object. No calls issued. |
196 * 3. Registration of undesired object. Unregistration issued. | 200 * 3. Registration of undesired object. Unregistration issued. |
197 * 4. Unregistration of desired object. Registration issued. | 201 * 4. Unregistration of desired object. Registration issued. |
198 */ | 202 */ |
199 // Initial test setup: persist a single registration into preferences. | 203 // Initial test setup: persist a single registration into preferences. |
200 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); | 204 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); |
201 EditContext editContext = invPrefs.edit(); | 205 EditContext editContext = invPrefs.edit(); |
202 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("SESSION"
)); | 206 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("SESSION"
)); |
| 207 ObjectId desiredObjectId = ObjectId.newInstance(1, "obj1".getBytes()); |
| 208 ObjectId undesiredObjectId = ObjectId.newInstance(1, "obj2".getBytes()); |
| 209 invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(desiredOb
jectId)); |
203 assertTrue(invPrefs.commit(editContext)); | 210 assertTrue(invPrefs.commit(editContext)); |
204 | 211 |
205 // Cases 1 and 2: calls matching desired state cause no actions. | 212 // Cases 1 and 2: calls matching desired state cause no actions. |
206 getService().informRegistrationStatus(CLIENT_ID, ModelType.SESSION.toObj
ectId(), | 213 getService().informRegistrationStatus(CLIENT_ID, ModelType.SESSION.toObj
ectId(), |
207 RegistrationState.REGISTERED); | 214 RegistrationState.REGISTERED); |
| 215 getService().informRegistrationStatus(CLIENT_ID, desiredObjectId, |
| 216 RegistrationState.REGISTERED); |
208 getService().informRegistrationStatus(CLIENT_ID, ModelType.BOOKMARK.toOb
jectId(), | 217 getService().informRegistrationStatus(CLIENT_ID, ModelType.BOOKMARK.toOb
jectId(), |
209 RegistrationState.UNREGISTERED); | 218 RegistrationState.UNREGISTERED); |
| 219 getService().informRegistrationStatus(CLIENT_ID, undesiredObjectId, |
| 220 RegistrationState.UNREGISTERED); |
210 assertTrue(getService().mRegistrations.isEmpty()); | 221 assertTrue(getService().mRegistrations.isEmpty()); |
211 assertTrue(getService().mUnregistrations.isEmpty()); | 222 assertTrue(getService().mUnregistrations.isEmpty()); |
212 | 223 |
213 // Case 3: registration of undesired object triggers an unregistration. | 224 // Case 3: registration of undesired object triggers an unregistration. |
214 getService().informRegistrationStatus(CLIENT_ID, ModelType.BOOKMARK.toOb
jectId(), | 225 getService().informRegistrationStatus(CLIENT_ID, ModelType.BOOKMARK.toOb
jectId(), |
215 RegistrationState.REGISTERED); | 226 RegistrationState.REGISTERED); |
216 assertEquals(1, getService().mUnregistrations.size()); | 227 getService().informRegistrationStatus(CLIENT_ID, undesiredObjectId, |
| 228 RegistrationState.REGISTERED); |
| 229 assertEquals(2, getService().mUnregistrations.size()); |
217 assertEquals(0, getService().mRegistrations.size()); | 230 assertEquals(0, getService().mRegistrations.size()); |
218 assertEquals(CollectionUtil.newArrayList(ModelType.BOOKMARK.toObjectId()
), | 231 assertEquals(CollectionUtil.newArrayList(ModelType.BOOKMARK.toObjectId()
), |
219 getService().mUnregistrations.get(0)); | 232 getService().mUnregistrations.get(0)); |
| 233 assertEquals(CollectionUtil.newArrayList(undesiredObjectId), |
| 234 getService().mUnregistrations.get(1)); |
220 | 235 |
221 // Case 4: unregistration of a desired object triggers a registration. | 236 // Case 4: unregistration of a desired object triggers a registration. |
222 getService().informRegistrationStatus(CLIENT_ID, ModelType.SESSION.toObj
ectId(), | 237 getService().informRegistrationStatus(CLIENT_ID, ModelType.SESSION.toObj
ectId(), |
223 RegistrationState.UNREGISTERED); | 238 RegistrationState.UNREGISTERED); |
224 assertEquals(1, getService().mUnregistrations.size()); | 239 getService().informRegistrationStatus(CLIENT_ID, desiredObjectId, |
225 assertEquals(1, getService().mRegistrations.size()); | 240 RegistrationState.UNREGISTERED); |
| 241 assertEquals(2, getService().mUnregistrations.size()); |
| 242 assertEquals(2, getService().mRegistrations.size()); |
226 assertEquals(CollectionUtil.newArrayList(ModelType.SESSION.toObjectId())
, | 243 assertEquals(CollectionUtil.newArrayList(ModelType.SESSION.toObjectId())
, |
227 getService().mRegistrations.get(0)); | 244 getService().mRegistrations.get(0)); |
| 245 assertEquals(CollectionUtil.newArrayList(desiredObjectId), |
| 246 getService().mRegistrations.get(1)); |
228 } | 247 } |
229 | 248 |
230 @SmallTest | 249 @SmallTest |
231 @Feature({"Sync"}) | 250 @Feature({"Sync"}) |
232 public void testInformRegistrationFailure() { | 251 public void testInformRegistrationFailure() { |
233 /* | 252 /* |
234 * Test plan: call inform registration failure under a variety of circum
stances and verify | 253 * Test plan: call inform registration failure under a variety of circum
stances and verify |
235 * that the appropriate (un)register calls are issued. | 254 * that the appropriate (un)register calls are issued. |
236 * | 255 * |
237 * 1. Transient registration failure for an object that should be regist
ered. Register | 256 * 1. Transient registration failure for an object that should be regist
ered. Register |
238 * should be called. | 257 * should be called. |
239 * 2. Permanent registration failure for an object that should be regist
ered. No calls. | 258 * 2. Permanent registration failure for an object that should be regist
ered. No calls. |
240 * 3. Transient registration failure for an object that should not be re
gistered. Unregister | 259 * 3. Transient registration failure for an object that should not be re
gistered. Unregister |
241 * should be called. | 260 * should be called. |
242 * 4. Permanent registration failure for an object should not be registe
red. No calls. | 261 * 4. Permanent registration failure for an object should not be registe
red. No calls. |
243 */ | 262 */ |
244 | 263 |
245 // Initial test setup: persist a single registration into preferences. | 264 // Initial test setup: persist a single registration into preferences. |
246 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); | 265 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); |
247 EditContext editContext = invPrefs.edit(); | 266 EditContext editContext = invPrefs.edit(); |
248 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("SESSION"
)); | 267 invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("SESSION"
)); |
| 268 ObjectId desiredObjectId = ObjectId.newInstance(1, "obj1".getBytes()); |
| 269 ObjectId undesiredObjectId = ObjectId.newInstance(1, "obj2".getBytes()); |
| 270 invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(desiredOb
jectId)); |
249 assertTrue(invPrefs.commit(editContext)); | 271 assertTrue(invPrefs.commit(editContext)); |
250 | 272 |
251 // Cases 2 and 4: permanent registration failures never cause calls to b
e made. | 273 // Cases 2 and 4: permanent registration failures never cause calls to b
e made. |
252 getService().informRegistrationFailure(CLIENT_ID, ModelType.SESSION.toOb
jectId(), false, | 274 getService().informRegistrationFailure(CLIENT_ID, ModelType.SESSION.toOb
jectId(), false, |
253 ""); | 275 ""); |
254 getService().informRegistrationFailure(CLIENT_ID, ModelType.BOOKMARK.toO
bjectId(), false, | 276 getService().informRegistrationFailure(CLIENT_ID, ModelType.BOOKMARK.toO
bjectId(), false, |
255 ""); | 277 ""); |
| 278 getService().informRegistrationFailure(CLIENT_ID, desiredObjectId, false
, ""); |
| 279 getService().informRegistrationFailure(CLIENT_ID, undesiredObjectId, fal
se, ""); |
256 assertTrue(getService().mRegistrations.isEmpty()); | 280 assertTrue(getService().mRegistrations.isEmpty()); |
257 assertTrue(getService().mUnregistrations.isEmpty()); | 281 assertTrue(getService().mUnregistrations.isEmpty()); |
258 | 282 |
259 // Case 1: transient failure of a desired registration results in re-reg
istration. | 283 // Case 1: transient failure of a desired registration results in re-reg
istration. |
260 getService().informRegistrationFailure(CLIENT_ID, ModelType.SESSION.toOb
jectId(), true, ""); | 284 getService().informRegistrationFailure(CLIENT_ID, ModelType.SESSION.toOb
jectId(), true, ""); |
261 assertEquals(1, getService().mRegistrations.size()); | 285 getService().informRegistrationFailure(CLIENT_ID, desiredObjectId, true,
""); |
| 286 assertEquals(2, getService().mRegistrations.size()); |
262 assertTrue(getService().mUnregistrations.isEmpty()); | 287 assertTrue(getService().mUnregistrations.isEmpty()); |
263 assertEquals(CollectionUtil.newArrayList(ModelType.SESSION.toObjectId())
, | 288 assertEquals(CollectionUtil.newArrayList(ModelType.SESSION.toObjectId())
, |
264 getService().mRegistrations.get(0)); | 289 getService().mRegistrations.get(0)); |
| 290 assertEquals(CollectionUtil.newArrayList(desiredObjectId), |
| 291 getService().mRegistrations.get(1)); |
265 | 292 |
266 // Case 3: transient failure of an undesired registration results in unr
egistration. | 293 // Case 3: transient failure of an undesired registration results in unr
egistration. |
267 getService().informRegistrationFailure(CLIENT_ID, ModelType.BOOKMARK.toO
bjectId(), true, | 294 getService().informRegistrationFailure(CLIENT_ID, ModelType.BOOKMARK.toO
bjectId(), true, |
268 ""); | 295 ""); |
269 assertEquals(1, getService().mRegistrations.size()); | 296 getService().informRegistrationFailure(CLIENT_ID, undesiredObjectId, tru
e, ""); |
270 assertEquals(1, getService().mUnregistrations.size()); | 297 assertEquals(2, getService().mRegistrations.size()); |
| 298 assertEquals(2, getService().mUnregistrations.size()); |
271 assertEquals(CollectionUtil.newArrayList(ModelType.BOOKMARK.toObjectId()
), | 299 assertEquals(CollectionUtil.newArrayList(ModelType.BOOKMARK.toObjectId()
), |
272 getService().mUnregistrations.get(0)); | 300 getService().mUnregistrations.get(0)); |
| 301 assertEquals(CollectionUtil.newArrayList(undesiredObjectId), |
| 302 getService().mUnregistrations.get(1)); |
273 } | 303 } |
274 | 304 |
275 @SmallTest | 305 @SmallTest |
276 @Feature({"Sync"}) | 306 @Feature({"Sync"}) |
277 public void testInformError() { | 307 public void testInformError() { |
278 /* | 308 /* |
279 * Test plan: call informError with both permanent and transient errors.
Verify that | 309 * Test plan: call informError with both permanent and transient errors.
Verify that |
280 * the transient error causes no action to be taken and that the permane
nt error causes | 310 * the transient error causes no action to be taken and that the permane
nt error causes |
281 * the client to be stopped. | 311 * the client to be stopped. |
282 */ | 312 */ |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 doTestInvalidate(false); | 355 doTestInvalidate(false); |
326 } | 356 } |
327 | 357 |
328 private void doTestInvalidate(boolean hasPayload) { | 358 private void doTestInvalidate(boolean hasPayload) { |
329 /* | 359 /* |
330 * Test plan: call invalidate() with an invalidation that may or may not
have a payload. | 360 * Test plan: call invalidate() with an invalidation that may or may not
have a payload. |
331 * Verify the produced bundle has the correct fields. | 361 * Verify the produced bundle has the correct fields. |
332 */ | 362 */ |
333 // Call invalidate. | 363 // Call invalidate. |
334 int version = 4747; | 364 int version = 4747; |
335 ObjectId objectId = ModelType.BOOKMARK.toObjectId(); | 365 ObjectId objectId = ObjectId.newInstance(55, "BOOKMARK".getBytes()); |
336 final String payload = "testInvalidate-" + hasPayload; | 366 final String payload = "testInvalidate-" + hasPayload; |
337 Invalidation invalidation = hasPayload ? | 367 Invalidation invalidation = hasPayload ? |
338 Invalidation.newInstance(objectId, version, payload.getBytes())
: | 368 Invalidation.newInstance(objectId, version, payload.getBytes())
: |
339 Invalidation.newInstance(objectId, version); | 369 Invalidation.newInstance(objectId, version); |
340 byte[] ackHandle = ("testInvalidate-" + hasPayload).getBytes(); | 370 byte[] ackHandle = ("testInvalidate-" + hasPayload).getBytes(); |
341 getService().invalidate(invalidation, ackHandle); | 371 getService().invalidate(invalidation, ackHandle); |
342 | 372 |
343 // Validate bundle. | 373 // Validate bundle. |
344 assertEquals(1, getService().mRequestedSyncs.size()); | 374 assertEquals(1, getService().mRequestedSyncs.size()); |
345 Bundle syncBundle = getService().mRequestedSyncs.get(0); | 375 Bundle syncBundle = getService().mRequestedSyncs.get(0); |
| 376 assertEquals(55, syncBundle.getInt("objectSource")); |
346 assertEquals("BOOKMARK", syncBundle.getString("objectId")); | 377 assertEquals("BOOKMARK", syncBundle.getString("objectId")); |
347 assertEquals(version, syncBundle.getLong("version")); | 378 assertEquals(version, syncBundle.getLong("version")); |
348 assertEquals(hasPayload ? payload : "", syncBundle.getString("payload"))
; | 379 assertEquals(hasPayload ? payload : "", syncBundle.getString("payload"))
; |
349 | 380 |
350 // Ensure acknowledged. | 381 // Ensure acknowledged. |
351 assertSingleAcknowledgement(ackHandle); | 382 assertSingleAcknowledgement(ackHandle); |
352 } | 383 } |
353 | 384 |
354 @SmallTest | 385 @SmallTest |
355 @Feature({"Sync"}) | 386 @Feature({"Sync"}) |
356 public void testInvalidateUnknownVersion() { | 387 public void testInvalidateUnknownVersion() { |
357 /* | 388 /* |
358 * Test plan: call invalidateUnknownVersion(). Verify the produced bundl
e has the correct | 389 * Test plan: call invalidateUnknownVersion(). Verify the produced bundl
e has the correct |
359 * fields. | 390 * fields. |
360 */ | 391 */ |
361 ObjectId objectId = ModelType.BOOKMARK.toObjectId(); | 392 ObjectId objectId = ObjectId.newInstance(55, "BOOKMARK".getBytes()); |
362 byte[] ackHandle = "testInvalidateUV".getBytes(); | 393 byte[] ackHandle = "testInvalidateUV".getBytes(); |
363 getService().invalidateUnknownVersion(objectId, ackHandle); | 394 getService().invalidateUnknownVersion(objectId, ackHandle); |
364 | 395 |
365 // Validate bundle. | 396 // Validate bundle. |
366 assertEquals(1, getService().mRequestedSyncs.size()); | 397 assertEquals(1, getService().mRequestedSyncs.size()); |
367 Bundle syncBundle = getService().mRequestedSyncs.get(0); | 398 Bundle syncBundle = getService().mRequestedSyncs.get(0); |
| 399 assertEquals(55, syncBundle.getInt("objectSource")); |
368 assertEquals("BOOKMARK", syncBundle.getString("objectId")); | 400 assertEquals("BOOKMARK", syncBundle.getString("objectId")); |
369 assertEquals(0, syncBundle.getLong("version")); | 401 assertEquals(0, syncBundle.getLong("version")); |
370 assertEquals("", syncBundle.getString("payload")); | 402 assertEquals("", syncBundle.getString("payload")); |
371 | 403 |
372 // Ensure acknowledged. | 404 // Ensure acknowledged. |
373 assertSingleAcknowledgement(ackHandle); | 405 assertSingleAcknowledgement(ackHandle); |
374 } | 406 } |
375 | 407 |
376 @SmallTest | 408 @SmallTest |
377 @Feature({"Sync"}) | 409 @Feature({"Sync"}) |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 Intent registrationIntent = IntentProtocol.createRegisterIntent(account,
false, | 526 Intent registrationIntent = IntentProtocol.createRegisterIntent(account,
false, |
495 desiredRegistrations); | 527 desiredRegistrations); |
496 getService().onHandleIntent(registrationIntent); | 528 getService().onHandleIntent(registrationIntent); |
497 | 529 |
498 // Verify client started and state written. | 530 // Verify client started and state written. |
499 assertTrue(InvalidationService.getIsClientStartedForTest()); | 531 assertTrue(InvalidationService.getIsClientStartedForTest()); |
500 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); | 532 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); |
501 assertEquals(account, invPrefs.getSavedSyncedAccount()); | 533 assertEquals(account, invPrefs.getSavedSyncedAccount()); |
502 assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations), | 534 assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations), |
503 invPrefs.getSavedSyncedTypes()); | 535 invPrefs.getSavedSyncedTypes()); |
| 536 assertNull(invPrefs.getSavedObjectIds()); |
504 assertEquals(1, mStartServiceIntents.size()); | 537 assertEquals(1, mStartServiceIntents.size()); |
505 assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0))); | 538 assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0))); |
506 | 539 |
507 // Send another registration-change intent, this type with all-types set
to true, and | 540 // Send another registration-change intent, this type with all-types set
to true, and |
508 // verify that the on-disk state is updated and that no addition Intents
are issued. | 541 // verify that the on-disk state is updated and that no addition Intents
are issued. |
509 getService().onHandleIntent(IntentProtocol.createRegisterIntent(account,
true, null)); | 542 getService().onHandleIntent(IntentProtocol.createRegisterIntent(account,
true, null)); |
510 assertEquals(account, invPrefs.getSavedSyncedAccount()); | 543 assertEquals(account, invPrefs.getSavedSyncedAccount()); |
511 assertEquals(CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE), | 544 assertEquals(CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE), |
512 invPrefs.getSavedSyncedTypes()); | 545 invPrefs.getSavedSyncedTypes()); |
513 assertEquals(1, mStartServiceIntents.size()); | 546 assertEquals(1, mStartServiceIntents.size()); |
514 | 547 |
515 // Finally, send one more registration-change intent, this time with a d
ifferent account, | 548 // Finally, send one more registration-change intent, this time with a d
ifferent account, |
516 // and verify that it both updates the account, stops thye existing clie
nt, and | 549 // and verify that it both updates the account, stops thye existing clie
nt, and |
517 // starts a new client. | 550 // starts a new client. |
518 Account account2 = AccountManagerHelper.createAccountFromName("test2@exa
mple.com"); | 551 Account account2 = AccountManagerHelper.createAccountFromName("test2@exa
mple.com"); |
519 getService().onHandleIntent(IntentProtocol.createRegisterIntent(account2
, true, null)); | 552 getService().onHandleIntent(IntentProtocol.createRegisterIntent(account2
, true, null)); |
520 assertEquals(account2, invPrefs.getSavedSyncedAccount()); | 553 assertEquals(account2, invPrefs.getSavedSyncedAccount()); |
521 assertEquals(3, mStartServiceIntents.size()); | 554 assertEquals(3, mStartServiceIntents.size()); |
522 assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0))); | 555 assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0))); |
523 assertTrue(isAndroidListenerStopIntent(mStartServiceIntents.get(1))); | 556 assertTrue(isAndroidListenerStopIntent(mStartServiceIntents.get(1))); |
524 assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(2))); | 557 assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(2))); |
525 } | 558 } |
526 | 559 |
| 560 /** |
| 561 * Determines if the correct object ids have been written to preferences and
registered with the |
| 562 * invalidation client. |
| 563 * |
| 564 * @param expectedTypes The Sync types expected to be registered. |
| 565 * @param expectedObjectIds The additional object ids expected to be registe
red. |
| 566 * @param isReady Whether the client is ready to register/unregister. |
| 567 */ |
| 568 private boolean expectedObjectIdsRegistered(Set<ModelType> expectedTypes, |
| 569 Set<ObjectId> expectedObjectIds, boolean isReady) { |
| 570 // Get synced types saved to preferences. |
| 571 Set<String> expectedSyncTypes = ModelType.modelTypesToSyncTypes(expected
Types); |
| 572 InvalidationPreferences invPrefs = new InvalidationPreferences(getContex
t()); |
| 573 Set<String> actualSyncTypes = invPrefs.getSavedSyncedTypes(); |
| 574 if (actualSyncTypes == null) { |
| 575 actualSyncTypes = new HashSet<String>(); |
| 576 } |
| 577 |
| 578 // Get object ids saved to preferences. |
| 579 Set<ObjectId> actualObjectIds = invPrefs.getSavedObjectIds(); |
| 580 if (actualObjectIds == null) { |
| 581 actualObjectIds = new HashSet<ObjectId>(); |
| 582 } |
| 583 |
| 584 // Get expected registered object ids. |
| 585 Set<ObjectId> expectedRegisteredIds = new HashSet<ObjectId>(); |
| 586 if (isReady) { |
| 587 expectedRegisteredIds.addAll(ModelType.modelTypesToObjectIds(expecte
dTypes)); |
| 588 expectedRegisteredIds.addAll(expectedObjectIds); |
| 589 } |
| 590 |
| 591 return actualSyncTypes.equals(expectedSyncTypes) && |
| 592 actualObjectIds.equals(expectedObjectIds) && |
| 593 getService().mCurrentRegistrations.equals(expectedRegisteredIds)
; |
| 594 } |
| 595 |
| 596 @SmallTest |
| 597 @Feature({"Sync"}) |
| 598 public void testRegistrationIntentWithTypesAndObjectIds() { |
| 599 /* |
| 600 * Test plan: send a mix of registration-change intents: some for Sync t
ypes and some for |
| 601 * object ids. Verify that registering for Sync types does not interfere
with object id |
| 602 * registration and vice-versa. |
| 603 */ |
| 604 getService().setShouldRunStates(true, true); |
| 605 getService().onCreate(); |
| 606 |
| 607 Account account = AccountManagerHelper.createAccountFromName("test@examp
le.com"); |
| 608 Set<ObjectId> objectIds = new HashSet<ObjectId>(); |
| 609 Set<ModelType> types = new HashSet<ModelType>(); |
| 610 |
| 611 // Register for some object ids. |
| 612 objectIds.add(ObjectId.newInstance(1, "obj1".getBytes())); |
| 613 objectIds.add(ObjectId.newInstance(2, "obj2".getBytes())); |
| 614 Intent registrationIntent = IntentProtocol.createRegisterIntent(account,
new int[] {1, 2}, |
| 615 new String[] {"obj1", "obj2"}); |
| 616 getService().onHandleIntent(registrationIntent); |
| 617 assertTrue(expectedObjectIdsRegistered(types, objectIds, false /* isRead
y */)); |
| 618 |
| 619 // Register for some types. |
| 620 types.add(ModelType.BOOKMARK); |
| 621 types.add(ModelType.SESSION); |
| 622 registrationIntent = IntentProtocol.createRegisterIntent(account, false,
types); |
| 623 getService().onHandleIntent(registrationIntent); |
| 624 assertTrue(expectedObjectIdsRegistered(types, objectIds, false /* isRead
y */)); |
| 625 |
| 626 // Set client to be ready and verify registrations. |
| 627 getService().ready(CLIENT_ID); |
| 628 assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady
*/)); |
| 629 |
| 630 // Change object id registration with types registered. |
| 631 objectIds.add(ObjectId.newInstance(3, "obj3".getBytes())); |
| 632 registrationIntent = IntentProtocol.createRegisterIntent(account, new in
t[] {1, 2, 3}, |
| 633 new String[] {"obj1", "obj2", "obj3"}); |
| 634 getService().onHandleIntent(registrationIntent); |
| 635 assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady
*/)); |
| 636 |
| 637 // Change type registration with object ids registered. |
| 638 types.remove(ModelType.BOOKMARK); |
| 639 registrationIntent = IntentProtocol.createRegisterIntent(account, false,
types); |
| 640 getService().onHandleIntent(registrationIntent); |
| 641 assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady
*/)); |
| 642 |
| 643 // Unregister all types. |
| 644 types.clear(); |
| 645 registrationIntent = IntentProtocol.createRegisterIntent(account, false,
types); |
| 646 getService().onHandleIntent(registrationIntent); |
| 647 assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady
*/)); |
| 648 |
| 649 // Change object id registration with no types registered. |
| 650 objectIds.remove(ObjectId.newInstance(2, "obj2".getBytes())); |
| 651 registrationIntent = IntentProtocol.createRegisterIntent(account, new in
t[] {1, 3}, |
| 652 new String[] {"obj1", "obj3"}); |
| 653 getService().onHandleIntent(registrationIntent); |
| 654 assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady
*/)); |
| 655 |
| 656 // Unregister all object ids. |
| 657 objectIds.clear(); |
| 658 registrationIntent = IntentProtocol.createRegisterIntent(account, new in
t[0], |
| 659 new String[0]); |
| 660 getService().onHandleIntent(registrationIntent); |
| 661 assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady
*/)); |
| 662 |
| 663 // Change type registration with no object ids registered. |
| 664 types.add(ModelType.BOOKMARK); |
| 665 types.add(ModelType.PASSWORD); |
| 666 registrationIntent = IntentProtocol.createRegisterIntent(account, false,
types); |
| 667 getService().onHandleIntent(registrationIntent); |
| 668 assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady
*/)); |
| 669 } |
| 670 |
527 @SmallTest | 671 @SmallTest |
528 @Feature({"Sync"}) | 672 @Feature({"Sync"}) |
529 public void testRegistrationIntentNoProxyTabsUsingReady() { | 673 public void testRegistrationIntentNoProxyTabsUsingReady() { |
530 getService().setShouldRunStates(true, true); | 674 getService().setShouldRunStates(true, true); |
531 getService().onCreate(); | 675 getService().onCreate(); |
532 | 676 |
533 // Send register Intent. | 677 // Send register Intent. |
534 Account account = AccountManagerHelper.createAccountFromName("test@examp
le.com"); | 678 Account account = AccountManagerHelper.createAccountFromName("test@examp
le.com"); |
535 Intent registrationIntent = IntentProtocol.createRegisterIntent(account,
true, null); | 679 Intent registrationIntent = IntentProtocol.createRegisterIntent(account,
true, null); |
536 getService().onHandleIntent(registrationIntent); | 680 getService().onHandleIntent(registrationIntent); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 InvalidationService.CLIENT_TYPE, "unused".getBytes()); | 822 InvalidationService.CLIENT_TYPE, "unused".getBytes()); |
679 return intent.getExtras().keySet().equals(startIntent.getExtras().keySet
()); | 823 return intent.getExtras().keySet().equals(startIntent.getExtras().keySet
()); |
680 } | 824 } |
681 | 825 |
682 /** Returns whether {@code intent} is an {@link AndroidListener} stop intent
. */ | 826 /** Returns whether {@code intent} is an {@link AndroidListener} stop intent
. */ |
683 private boolean isAndroidListenerStopIntent(Intent intent) { | 827 private boolean isAndroidListenerStopIntent(Intent intent) { |
684 Intent stopIntent = AndroidListener.createStopIntent(getContext()); | 828 Intent stopIntent = AndroidListener.createStopIntent(getContext()); |
685 return intent.getExtras().keySet().equals(stopIntent.getExtras().keySet(
)); | 829 return intent.getExtras().keySet().equals(stopIntent.getExtras().keySet(
)); |
686 } | 830 } |
687 } | 831 } |
OLD | NEW |