@SmallTest
 @Feature({"Sync"})
 public void testGetIntentDestination() {
   assertEquals(
       "org.chromium.sync.notifier.TEST_VALUE",
       InvalidationController.getDestinationClassName(mContext));
 }
 @SmallTest
 @Feature({"Sync"})
 public void testResumingMainActivityWithSyncDisabled() throws Exception {
   // Resuming main activity should NOT trigger a start if sync is disabled.
   setupSync(false);
   mController.onActivityStateChange(ActivityStatus.RESUMED);
   assertEquals(0, mContext.getNumStartedIntents());
 }
 @SmallTest
 @Feature({"Sync"})
 public void testStart() throws Exception {
   mController.start();
   assertEquals(1, mContext.getNumStartedIntents());
   Intent intent = mContext.getStartedIntent(0);
   validateIntentComponent(intent);
   assertNull(intent.getExtras());
 }
 @SmallTest
 @Feature({"Sync"})
 public void testResumingMainActivity() throws Exception {
   // Resuming main activity should trigger a start if sync is enabled.
   setupSync(true);
   mController.onActivityStateChange(ActivityStatus.RESUMED);
   assertEquals(1, mContext.getNumStartedIntents());
   Intent intent = mContext.getStartedIntent(0);
   validateIntentComponent(intent);
   assertNull(intent.getExtras());
 }
 @SmallTest
 @Feature({"Sync"})
 public void testStop() throws Exception {
   mController.stop();
   assertEquals(1, mContext.getNumStartedIntents());
   Intent intent = mContext.getStartedIntent(0);
   validateIntentComponent(intent);
   assertEquals(1, intent.getExtras().size());
   assertTrue(intent.hasExtra(IntentProtocol.EXTRA_STOP));
   assertTrue(intent.getBooleanExtra(IntentProtocol.EXTRA_STOP, false));
 }
  @SmallTest
  @Feature({"Sync"})
  public void testRegisterForSpecificTypes() {
    final String controllerFlag = "resolveModelTypes";
    final ModelTypeResolver resolver =
        new ModelTypeResolver() {
          @Override
          public Set<ModelType> resolveModelTypes(Set<ModelType> modelTypes) {
            mContext.setFlag(controllerFlag);
            return modelTypes;
          }
        };
    InvalidationController controller =
        new InvalidationController(mContext) {
          @Override
          ModelTypeResolver getModelTypeResolver() {
            return resolver;
          }
        };
    Account account = new Account("*****@*****.**", "bogus");
    controller.setRegisteredTypes(
        account, false, Sets.newHashSet(ModelType.BOOKMARK, ModelType.SESSION));
    assertEquals(1, mContext.getNumStartedIntents());

    // Validate destination.
    Intent intent = mContext.getStartedIntent(0);
    validateIntentComponent(intent);
    assertEquals(IntentProtocol.ACTION_REGISTER, intent.getAction());

    // Validate account.
    Account intentAccount = intent.getParcelableExtra(IntentProtocol.EXTRA_ACCOUNT);
    assertEquals(account, intentAccount);

    // Validate registered types.
    Set<String> expectedTypes =
        Sets.newHashSet(ModelType.BOOKMARK.name(), ModelType.SESSION.name());
    Set<String> actualTypes = Sets.newHashSet();
    actualTypes.addAll(intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTERED_TYPES));
    assertEquals(expectedTypes, actualTypes);
    assertTrue(mContext.isFlagSet(controllerFlag));
  }
 @SmallTest
 @Feature({"Sync"})
 public void testPausingMainActivity() throws Exception {
   // Resuming main activity should trigger a stop if sync is enabled.
   setupSync(true);
   mController.onActivityStateChange(ActivityStatus.PAUSED);
   assertEquals(1, mContext.getNumStartedIntents());
   Intent intent = mContext.getStartedIntent(0);
   validateIntentComponent(intent);
   assertEquals(1, intent.getExtras().size());
   assertTrue(intent.hasExtra(IntentProtocol.EXTRA_STOP));
   assertTrue(intent.getBooleanExtra(IntentProtocol.EXTRA_STOP, false));
 }
  @SmallTest
  @Feature({"Sync"})
  public void testRefreshShouldReadValuesFromDiskWithSpecificTypes() {
    // Store some preferences for ModelTypes and account. We are using the helper class
    // for this, so we don't have to deal with low-level details such as preference keys.
    InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext);
    InvalidationPreferences.EditContext edit = invalidationPreferences.edit();
    Set<String> storedModelTypes = new HashSet<String>();
    storedModelTypes.add(ModelType.BOOKMARK.name());
    storedModelTypes.add(ModelType.TYPED_URL.name());
    invalidationPreferences.setSyncTypes(edit, storedModelTypes);
    Account storedAccount = AccountManagerHelper.createAccountFromName("*****@*****.**");
    invalidationPreferences.setAccount(edit, storedAccount);
    invalidationPreferences.commit(edit);

    // Ensure all calls to {@link InvalidationController#setRegisteredTypes} store values
    // we can inspect in the test.
    final AtomicReference<Account> resultAccount = new AtomicReference<Account>();
    final AtomicBoolean resultAllTypes = new AtomicBoolean();
    final AtomicReference<Set<ModelType>> resultTypes = new AtomicReference<Set<ModelType>>();
    InvalidationController controller =
        new InvalidationController(mContext) {
          @Override
          public void setRegisteredTypes(Account account, boolean allTypes, Set<ModelType> types) {
            resultAccount.set(account);
            resultAllTypes.set(allTypes);
            resultTypes.set(types);
          }
        };

    // Execute the test.
    controller.refreshRegisteredTypes();

    // Validate the values.
    assertEquals(storedAccount, resultAccount.get());
    assertEquals(false, resultAllTypes.get());
    assertEquals(ModelType.syncTypesToModelTypes(storedModelTypes), resultTypes.get());
  }
  @SmallTest
  @Feature({"Sync"})
  public void testRegisterForAllTypes() {
    Account account = new Account("*****@*****.**", "bogus");
    mController.setRegisteredTypes(
        account, true, Sets.newHashSet(ModelType.BOOKMARK, ModelType.SESSION));
    assertEquals(1, mContext.getNumStartedIntents());

    // Validate destination.
    Intent intent = mContext.getStartedIntent(0);
    validateIntentComponent(intent);
    assertEquals(IntentProtocol.ACTION_REGISTER, intent.getAction());

    // Validate account.
    Account intentAccount = intent.getParcelableExtra(IntentProtocol.EXTRA_ACCOUNT);
    assertEquals(account, intentAccount);

    // Validate registered types.
    Set<String> expectedTypes = Sets.newHashSet(ModelType.ALL_TYPES_TYPE);
    Set<String> actualTypes = Sets.newHashSet();
    actualTypes.addAll(intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTERED_TYPES));
    assertEquals(expectedTypes, actualTypes);
  }
 @Override
 protected void setUp() throws Exception {
   mContext = new IntentSavingContext(getInstrumentation().getTargetContext());
   mController = InvalidationController.get(mContext);
 }
 @SmallTest
 @Feature({"Sync"})
 public void testGetContractAuthority() throws Exception {
   assertEquals(mContext.getPackageName(), mController.getContractAuthority());
 }