| 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.ComponentName; | 9 import android.content.ComponentName; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.content.Intent; | 11 import android.content.Intent; |
| 12 import android.content.pm.PackageManager; | 12 import android.content.pm.PackageManager; |
| 13 import android.test.InstrumentationTestCase; | 13 import android.test.InstrumentationTestCase; |
| 14 import android.test.suitebuilder.annotation.SmallTest; | 14 import android.test.suitebuilder.annotation.SmallTest; |
| 15 | 15 |
| 16 import com.google.ipc.invalidation.external.client.types.ObjectId; | 16 import com.google.ipc.invalidation.external.client.types.ObjectId; |
| 17 | 17 |
| 18 import org.chromium.base.ActivityStatus; | 18 import org.chromium.base.ActivityStatus; |
| 19 import org.chromium.base.CollectionUtil; | 19 import org.chromium.base.CollectionUtil; |
| 20 import org.chromium.base.test.util.AdvancedMockContext; | 20 import org.chromium.base.test.util.AdvancedMockContext; |
| 21 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
| 22 import org.chromium.chrome.browser.identity.UniqueIdentificationGenerator; |
| 23 import org.chromium.chrome.browser.identity.UniqueIdentificationGeneratorFactory
; |
| 22 import org.chromium.sync.internal_api.pub.base.ModelType; | 24 import org.chromium.sync.internal_api.pub.base.ModelType; |
| 23 import org.chromium.sync.notifier.InvalidationIntentProtocol; | 25 import org.chromium.sync.notifier.InvalidationIntentProtocol; |
| 24 import org.chromium.sync.notifier.InvalidationPreferences; | 26 import org.chromium.sync.notifier.InvalidationPreferences; |
| 25 import org.chromium.sync.notifier.InvalidationService; | 27 import org.chromium.sync.notifier.InvalidationService; |
| 26 import org.chromium.sync.notifier.SyncStatusHelper; | 28 import org.chromium.sync.notifier.SyncStatusHelper; |
| 27 import org.chromium.sync.signin.AccountManagerHelper; | 29 import org.chromium.sync.signin.AccountManagerHelper; |
| 28 import org.chromium.sync.signin.ChromeSigninController; | 30 import org.chromium.sync.signin.ChromeSigninController; |
| 29 import org.chromium.sync.test.util.MockSyncContentResolverDelegate; | 31 import org.chromium.sync.test.util.MockSyncContentResolverDelegate; |
| 30 | 32 |
| 33 import java.util.Arrays; |
| 31 import java.util.ArrayList; | 34 import java.util.ArrayList; |
| 32 import java.util.HashSet; | 35 import java.util.HashSet; |
| 33 import java.util.List; | 36 import java.util.List; |
| 34 import java.util.Set; | 37 import java.util.Set; |
| 35 import java.util.concurrent.atomic.AtomicBoolean; | 38 import java.util.concurrent.atomic.AtomicBoolean; |
| 36 import java.util.concurrent.atomic.AtomicReference; | 39 import java.util.concurrent.atomic.AtomicReference; |
| 37 | 40 |
| 41 import javax.annotation.Nullable; |
| 42 |
| 38 /** | 43 /** |
| 39 * Tests for the {@link InvalidationController}. | 44 * Tests for the {@link InvalidationController}. |
| 40 */ | 45 */ |
| 41 public class InvalidationControllerTest extends InstrumentationTestCase { | 46 public class InvalidationControllerTest extends InstrumentationTestCase { |
| 42 private IntentSavingContext mContext; | 47 private IntentSavingContext mContext; |
| 43 private InvalidationController mController; | 48 private InvalidationController mController; |
| 44 | 49 |
| 45 @Override | 50 @Override |
| 46 protected void setUp() throws Exception { | 51 protected void setUp() throws Exception { |
| 47 mContext = new IntentSavingContext(getInstrumentation().getTargetContext
()); | 52 mContext = new IntentSavingContext(getInstrumentation().getTargetContext
()); |
| 48 mController = InvalidationController.get(mContext); | 53 mController = InvalidationController.get(mContext); |
| 49 // We don't want to use the system content resolver, so we override it. | 54 // We don't want to use the system content resolver, so we override it. |
| 50 MockSyncContentResolverDelegate delegate = new MockSyncContentResolverDe
legate(); | 55 MockSyncContentResolverDelegate delegate = new MockSyncContentResolverDe
legate(); |
| 51 // Android master sync can safely always be on. | 56 // Android master sync can safely always be on. |
| 52 delegate.setMasterSyncAutomatically(true); | 57 delegate.setMasterSyncAutomatically(true); |
| 53 SyncStatusHelper.overrideSyncStatusHelperForTests(mContext, delegate); | 58 SyncStatusHelper.overrideSyncStatusHelperForTests(mContext, delegate); |
| 54 } | 59 } |
| 55 | 60 |
| 56 @SmallTest | 61 @SmallTest |
| 57 @Feature({"Sync"}) | 62 @Feature({"Sync"}) |
| 58 public void testStart() throws Exception { | 63 public void testStart() throws Exception { |
| 59 mController.start(); | 64 mController.start(); |
| 60 assertEquals(1, mContext.getNumStartedIntents()); | 65 assertEquals(1, mContext.getNumStartedIntents()); |
| 61 Intent intent = mContext.getStartedIntent(0); | 66 Intent intent = mContext.getStartedIntent(0); |
| 62 validateIntentComponent(intent); | 67 validateIntentComponent(intent); |
| 63 assertNull(intent.getExtras()); | 68 assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME)
); |
| 64 } | 69 } |
| 65 | 70 |
| 66 @SmallTest | 71 @SmallTest |
| 67 @Feature({"Sync"}) | 72 @Feature({"Sync"}) |
| 68 public void testStop() throws Exception { | 73 public void testStop() throws Exception { |
| 69 mController.stop(); | 74 mController.stop(); |
| 70 assertEquals(1, mContext.getNumStartedIntents()); | 75 assertEquals(1, mContext.getNumStartedIntents()); |
| 71 Intent intent = mContext.getStartedIntent(0); | 76 Intent intent = mContext.getStartedIntent(0); |
| 72 validateIntentComponent(intent); | 77 validateIntentComponent(intent); |
| 73 assertEquals(1, intent.getExtras().size()); | 78 assertEquals(2, intent.getExtras().size()); |
| 79 assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME)
); |
| 74 assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_STOP)); | 80 assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_STOP)); |
| 75 assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP,
false)); | 81 assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP,
false)); |
| 76 } | 82 } |
| 77 | 83 |
| 78 @SmallTest | 84 @SmallTest |
| 79 @Feature({"Sync"}) | 85 @Feature({"Sync"}) |
| 80 public void testResumingMainActivity() throws Exception { | 86 public void testResumingMainActivity() throws Exception { |
| 81 // Resuming main activity should trigger a start if sync is enabled. | 87 // Resuming main activity should trigger a start if sync is enabled. |
| 82 setupSync(true); | 88 setupSync(true); |
| 83 mController.onActivityStateChange(ActivityStatus.RESUMED); | 89 mController.onActivityStateChange(ActivityStatus.RESUMED); |
| 84 assertEquals(1, mContext.getNumStartedIntents()); | 90 assertEquals(1, mContext.getNumStartedIntents()); |
| 85 Intent intent = mContext.getStartedIntent(0); | 91 Intent intent = mContext.getStartedIntent(0); |
| 86 validateIntentComponent(intent); | 92 validateIntentComponent(intent); |
| 87 assertNull(intent.getExtras()); | |
| 88 } | 93 } |
| 89 | 94 |
| 90 @SmallTest | 95 @SmallTest |
| 91 @Feature({"Sync"}) | 96 @Feature({"Sync"}) |
| 92 public void testResumingMainActivityWithSyncDisabled() throws Exception { | 97 public void testResumingMainActivityWithSyncDisabled() throws Exception { |
| 93 // Resuming main activity should NOT trigger a start if sync is disabled
. | 98 // Resuming main activity should NOT trigger a start if sync is disabled
. |
| 94 setupSync(false); | 99 setupSync(false); |
| 95 mController.onActivityStateChange(ActivityStatus.RESUMED); | 100 mController.onActivityStateChange(ActivityStatus.RESUMED); |
| 96 assertEquals(0, mContext.getNumStartedIntents()); | 101 assertEquals(0, mContext.getNumStartedIntents()); |
| 97 } | 102 } |
| 98 | 103 |
| 99 @SmallTest | 104 @SmallTest |
| 100 @Feature({"Sync"}) | 105 @Feature({"Sync"}) |
| 101 public void testPausingMainActivity() throws Exception { | 106 public void testPausingMainActivity() throws Exception { |
| 102 // Resuming main activity should trigger a stop if sync is enabled. | 107 // Resuming main activity should trigger a stop if sync is enabled. |
| 103 setupSync(true); | 108 setupSync(true); |
| 104 mController.onActivityStateChange(ActivityStatus.PAUSED); | 109 mController.onActivityStateChange(ActivityStatus.PAUSED); |
| 105 assertEquals(1, mContext.getNumStartedIntents()); | 110 assertEquals(1, mContext.getNumStartedIntents()); |
| 106 Intent intent = mContext.getStartedIntent(0); | 111 Intent intent = mContext.getStartedIntent(0); |
| 107 validateIntentComponent(intent); | 112 validateIntentComponent(intent); |
| 108 assertEquals(1, intent.getExtras().size()); | 113 assertEquals(2, intent.getExtras().size()); |
| 114 assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_CLIENT_NAME)
); |
| 109 assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_STOP)); | 115 assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_STOP)); |
| 110 assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP,
false)); | 116 assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP,
false)); |
| 111 } | 117 } |
| 112 | 118 |
| 113 @SmallTest | 119 @SmallTest |
| 114 @Feature({"Sync"}) | 120 @Feature({"Sync"}) |
| 115 public void testPausingMainActivityWithSyncDisabled() throws Exception { | 121 public void testPausingMainActivityWithSyncDisabled() throws Exception { |
| 116 // Resuming main activity should NOT trigger a stop if sync is disabled. | 122 // Resuming main activity should NOT trigger a stop if sync is disabled. |
| 117 setupSync(false); | 123 setupSync(false); |
| 118 mController.onActivityStateChange(ActivityStatus.PAUSED); | 124 mController.onActivityStateChange(ActivityStatus.PAUSED); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // Validate registered object ids. The bookmark object should not be reg
istered since it is | 311 // Validate registered object ids. The bookmark object should not be reg
istered since it is |
| 306 // a Sync type. | 312 // a Sync type. |
| 307 assertNull(intent.getStringArrayListExtra( | 313 assertNull(intent.getStringArrayListExtra( |
| 308 InvalidationIntentProtocol.EXTRA_REGISTERED_TYPE
S)); | 314 InvalidationIntentProtocol.EXTRA_REGISTERED_TYPE
S)); |
| 309 Set<ObjectId> objectIds = InvalidationIntentProtocol.getRegisteredObject
Ids(intent); | 315 Set<ObjectId> objectIds = InvalidationIntentProtocol.getRegisteredObject
Ids(intent); |
| 310 assertEquals(2, objectIds.size()); | 316 assertEquals(2, objectIds.size()); |
| 311 assertTrue(objectIds.contains(ObjectId.newInstance(1, "a".getBytes()))); | 317 assertTrue(objectIds.contains(ObjectId.newInstance(1, "a".getBytes()))); |
| 312 assertTrue(objectIds.contains(ObjectId.newInstance(2, "b".getBytes()))); | 318 assertTrue(objectIds.contains(ObjectId.newInstance(2, "b".getBytes()))); |
| 313 } | 319 } |
| 314 | 320 |
| 321 @SmallTest |
| 322 @Feature({"Sync"}) |
| 323 public void testFallbackClientId() { |
| 324 // Test that the InvalidationController consistently returns the same ID e
ven when it has to |
| 325 // resort to its "fallback" ID generation code. |
| 326 InvalidationController controller = new InvalidationController(mContext); |
| 327 byte[] id1 = controller.getInvalidatorClientId(); |
| 328 byte[] id2 = controller.getInvalidatorClientId(); |
| 329 |
| 330 assertTrue(Arrays.equals(id1, id2)); |
| 331 |
| 332 // Even if initialize the generator late, the ID will remain consistent. |
| 333 registerHardCodedIdGenerator(); |
| 334 |
| 335 byte[] id3 = controller.getInvalidatorClientId(); |
| 336 assertTrue(Arrays.equals(id2, id3)); |
| 337 } |
| 338 |
| 339 @SmallTest |
| 340 @Feature({"Sync"}) |
| 341 public void testPreRegisteredClientId() { |
| 342 registerHardCodedIdGenerator(); |
| 343 |
| 344 UniqueIdentificationGenerator generator = |
| 345 UniqueIdentificationGeneratorFactory.getInstance(InvalidationControll
er.ID_GENERATOR); |
| 346 String generatorId = generator.getUniqueId(null); |
| 347 |
| 348 InvalidationController controller = new InvalidationController(mContext); |
| 349 byte[] id = controller.getInvalidatorClientId(); |
| 350 byte[] id2 = controller.getInvalidatorClientId(); |
| 351 |
| 352 assertTrue(Arrays.equals(generatorId.getBytes(), id)); |
| 353 assertTrue(Arrays.equals(id, id2)); |
| 354 } |
| 355 |
| 315 /** | 356 /** |
| 316 * Asserts that {@code intent} is destined for the correct component. | 357 * Asserts that {@code intent} is destined for the correct component. |
| 317 */ | 358 */ |
| 318 private static void validateIntentComponent(Intent intent) { | 359 private static void validateIntentComponent(Intent intent) { |
| 319 assertNotNull(intent.getComponent()); | 360 assertNotNull(intent.getComponent()); |
| 320 assertEquals(InvalidationService.class.getName(), | 361 assertEquals(InvalidationService.class.getName(), |
| 321 intent.getComponent().getClassName()); | 362 intent.getComponent().getClassName()); |
| 322 } | 363 } |
| 323 | 364 |
| 324 /** | 365 /** |
| (...skipping 18 matching lines...) Expand all Loading... |
| 343 | 384 |
| 344 Intent getStartedIntent(int idx) { | 385 Intent getStartedIntent(int idx) { |
| 345 return startedIntents.get(idx); | 386 return startedIntents.get(idx); |
| 346 } | 387 } |
| 347 | 388 |
| 348 @Override | 389 @Override |
| 349 public PackageManager getPackageManager() { | 390 public PackageManager getPackageManager() { |
| 350 return getBaseContext().getPackageManager(); | 391 return getBaseContext().getPackageManager(); |
| 351 } | 392 } |
| 352 } | 393 } |
| 394 |
| 395 private static void registerHardCodedIdGenerator() { |
| 396 UniqueIdentificationGeneratorFactory.registerGenerator( |
| 397 InvalidationController.ID_GENERATOR, |
| 398 new UniqueIdentificationGenerator() { |
| 399 public String getUniqueId(@Nullable String salt) { |
| 400 return "Testable ID"; |
| 401 } |
| 402 }, |
| 403 true); |
| 404 } |
| 353 } | 405 } |
| OLD | NEW |