This source file includes following definitions.
- setUp
- Feature
- testStart
- Feature
- testStop
- Feature
- testResumingMainActivity
- Feature
- testResumingMainActivityWithSyncDisabled
- Feature
- testPausingMainActivity
- Feature
- testPausingMainActivityWithSyncDisabled
- setupSync
- Feature
- testEnsureConstructorRegistersListener
- Feature
- testRegisterForSpecificTypes
- Feature
- testRegisterForAllTypes
- Feature
- testRefreshShouldReadValuesFromDiskWithSpecificTypes
- Feature
- testRefreshShouldReadValuesFromDiskWithAllTypes
- Feature
- testSetRegisteredObjectIds
- validateIntentComponent
- startService
- getNumStartedIntents
- getStartedIntent
- getPackageManager
package org.chromium.chrome.browser.invalidation;
import android.accounts.Account;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.google.ipc.invalidation.external.client.types.ObjectId;
import org.chromium.base.ActivityState;
import org.chromium.base.ApplicationState;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.CollectionUtil;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature;
import org.chromium.sync.internal_api.pub.base.ModelType;
import org.chromium.sync.notifier.InvalidationIntentProtocol;
import org.chromium.sync.notifier.InvalidationPreferences;
import org.chromium.sync.notifier.InvalidationService;
import org.chromium.sync.notifier.SyncStatusHelper;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
import org.chromium.sync.test.util.MockSyncContentResolverDelegate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class InvalidationControllerTest extends InstrumentationTestCase {
private IntentSavingContext mContext;
private InvalidationController mController;
@Override
protected void setUp() throws Exception {
mContext = new IntentSavingContext(getInstrumentation().getTargetContext());
mController = InvalidationController.get(mContext);
MockSyncContentResolverDelegate delegate = new MockSyncContentResolverDelegate();
delegate.setMasterSyncAutomatically(true);
SyncStatusHelper.overrideSyncStatusHelperForTests(mContext, delegate);
}
@SmallTest
@Feature({"Sync"})
public void testStart() throws Exception {
mController.start();
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
}
@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(InvalidationIntentProtocol.EXTRA_STOP));
assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP, false));
}
@SmallTest
@Feature({"Sync"})
public void testResumingMainActivity() throws Exception {
setupSync(true);
mController.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
}
@SmallTest
@Feature({"Sync"})
public void testResumingMainActivityWithSyncDisabled() throws Exception {
setupSync(false);
mController.onApplicationStateChange(ApplicationState.HAS_RUNNING_ACTIVITIES);
assertEquals(0, mContext.getNumStartedIntents());
}
@SmallTest
@Feature({"Sync"})
public void testPausingMainActivity() throws Exception {
setupSync(true);
mController.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
assertEquals(1, intent.getExtras().size());
assertTrue(intent.hasExtra(InvalidationIntentProtocol.EXTRA_STOP));
assertTrue(intent.getBooleanExtra(InvalidationIntentProtocol.EXTRA_STOP, false));
}
@SmallTest
@Feature({"Sync"})
public void testPausingMainActivityWithSyncDisabled() throws Exception {
setupSync(false);
mController.onApplicationStateChange(ApplicationState.HAS_PAUSED_ACTIVITIES);
assertEquals(0, mContext.getNumStartedIntents());
}
private void setupSync(boolean syncEnabled) {
Account account = AccountManagerHelper.createAccountFromName("test@gmail.com");
ChromeSigninController chromeSigninController = ChromeSigninController.get(mContext);
chromeSigninController.setSignedInAccountName(account.name);
SyncStatusHelper syncStatusHelper = SyncStatusHelper.get(mContext);
if (syncEnabled) {
syncStatusHelper.enableAndroidSync(account);
} else {
syncStatusHelper.disableAndroidSync(account);
}
}
@SmallTest
@Feature({"Sync"})
public void testEnsureConstructorRegistersListener() throws Exception {
final AtomicBoolean listenerCallbackCalled = new AtomicBoolean();
new InvalidationController(mContext) {
@Override
public void onApplicationStateChange(int newState) {
listenerCallbackCalled.set(true);
}
};
assertFalse(listenerCallbackCalled.get());
ApplicationStatus.onStateChangeForTesting(new Activity(), ActivityState.CREATED);
assertTrue(listenerCallbackCalled.get());
}
@SmallTest
@Feature({"Sync"})
public void testRegisterForSpecificTypes() {
InvalidationController controller = new InvalidationController(mContext);
Account account = new Account("test@example.com", "bogus");
controller.setRegisteredTypes(account, false,
CollectionUtil.newHashSet(ModelType.BOOKMARK, ModelType.SESSION));
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
assertEquals(InvalidationIntentProtocol.ACTION_REGISTER, intent.getAction());
Account intentAccount =
intent.getParcelableExtra(InvalidationIntentProtocol.EXTRA_ACCOUNT);
assertEquals(account, intentAccount);
Set<String> expectedTypes = CollectionUtil.newHashSet(ModelType.BOOKMARK.name(),
ModelType.SESSION.name());
Set<String> actualTypes = new HashSet<String>();
actualTypes.addAll(intent.getStringArrayListExtra(
InvalidationIntentProtocol.EXTRA_REGISTERED_TYPES));
assertEquals(expectedTypes, actualTypes);
assertNull(InvalidationIntentProtocol.getRegisteredObjectIds(intent));
}
@SmallTest
@Feature({"Sync"})
public void testRegisterForAllTypes() {
Account account = new Account("test@example.com", "bogus");
mController.setRegisteredTypes(account, true,
CollectionUtil.newHashSet(ModelType.BOOKMARK, ModelType.SESSION));
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
assertEquals(InvalidationIntentProtocol.ACTION_REGISTER, intent.getAction());
Account intentAccount =
intent.getParcelableExtra(InvalidationIntentProtocol.EXTRA_ACCOUNT);
assertEquals(account, intentAccount);
Set<String> expectedTypes = CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE);
Set<String> actualTypes = new HashSet<String>();
actualTypes.addAll(intent.getStringArrayListExtra(
InvalidationIntentProtocol.EXTRA_REGISTERED_TYPES));
assertEquals(expectedTypes, actualTypes);
assertNull(InvalidationIntentProtocol.getRegisteredObjectIds(intent));
}
@SmallTest
@Feature({"Sync"})
public void testRefreshShouldReadValuesFromDiskWithSpecificTypes() {
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());
Set<ModelType> refreshedTypes = new HashSet<ModelType>();
refreshedTypes.add(ModelType.BOOKMARK);
refreshedTypes.add(ModelType.TYPED_URL);
invalidationPreferences.setSyncTypes(edit, storedModelTypes);
Account storedAccount = AccountManagerHelper.createAccountFromName("test@gmail.com");
invalidationPreferences.setAccount(edit, storedAccount);
invalidationPreferences.commit(edit);
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);
}
};
controller.refreshRegisteredTypes(refreshedTypes);
assertEquals(storedAccount, resultAccount.get());
assertEquals(false, resultAllTypes.get());
assertEquals(ModelType.syncTypesToModelTypes(storedModelTypes), resultTypes.get());
}
@SmallTest
@Feature({"Sync"})
public void testRefreshShouldReadValuesFromDiskWithAllTypes() {
InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext);
InvalidationPreferences.EditContext edit = invalidationPreferences.edit();
List<String> storedModelTypes = new ArrayList<String>();
storedModelTypes.add(ModelType.ALL_TYPES_TYPE);
invalidationPreferences.setSyncTypes(edit, storedModelTypes);
Account storedAccount = AccountManagerHelper.createAccountFromName("test@gmail.com");
invalidationPreferences.setAccount(edit, storedAccount);
invalidationPreferences.commit(edit);
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);
}
};
controller.refreshRegisteredTypes(new HashSet<ModelType>());
assertEquals(storedAccount, resultAccount.get());
assertEquals(true, resultAllTypes.get());
}
@SmallTest
@Feature({"Sync"})
public void testSetRegisteredObjectIds() {
InvalidationController controller = new InvalidationController(mContext);
ObjectId bookmark = ModelType.BOOKMARK.toObjectId();
controller.setRegisteredObjectIds(new int[] {1, 2, bookmark.getSource()},
new String[] {"a", "b", new String(bookmark.getName())});
assertEquals(1, mContext.getNumStartedIntents());
Intent intent = mContext.getStartedIntent(0);
validateIntentComponent(intent);
assertEquals(InvalidationIntentProtocol.ACTION_REGISTER, intent.getAction());
assertNull(intent.getStringArrayListExtra(
InvalidationIntentProtocol.EXTRA_REGISTERED_TYPES));
Set<ObjectId> objectIds = InvalidationIntentProtocol.getRegisteredObjectIds(intent);
assertEquals(2, objectIds.size());
assertTrue(objectIds.contains(ObjectId.newInstance(1, "a".getBytes())));
assertTrue(objectIds.contains(ObjectId.newInstance(2, "b".getBytes())));
}
private static void validateIntentComponent(Intent intent) {
assertNotNull(intent.getComponent());
assertEquals(InvalidationService.class.getName(),
intent.getComponent().getClassName());
}
private static class IntentSavingContext extends AdvancedMockContext {
private final List<Intent> startedIntents = new ArrayList<Intent>();
IntentSavingContext(Context targetContext) {
super(targetContext);
}
@Override
public ComponentName startService(Intent intent) {
startedIntents.add(intent);
return new ComponentName(this, getClass());
}
int getNumStartedIntents() {
return startedIntents.size();
}
Intent getStartedIntent(int idx) {
return startedIntents.get(idx);
}
@Override
public PackageManager getPackageManager() {
return getBaseContext().getPackageManager();
}
}
}