This source file includes following definitions.
- setUp
- tearDown
- Feature
- testComputeRegistrationOps
- Feature
- testReady
- Feature
- testReissueRegistrations
- Feature
- testInformRegistrationStatus
- Feature
- testInformRegistrationFailure
- Feature
- testInformError
- Feature
- testReadWriteState
- Feature
- testInvalidateWithPayload
- Feature
- testInvalidateWithoutPayload
- doTestInvalidate
- Feature
- testInvalidateUnknownVersion
- Feature
- testInvalidateAll
- assertSingleAcknowledgement
- Feature
- testShouldClientBeRunning
- Feature
- testStartAndStopClient
- Feature
- testClientStopsWhenShouldNotBeRunning
- Feature
- testRegistrationIntent
- expectedObjectIdsRegistered
- Feature
- testRegistrationIntentWithTypesAndObjectIds
- Feature
- testRegistrationIntentNoProxyTabsUsingReady
- Feature
- testRegistrationIntentNoProxyTabsAlreadyWithClientId
- Feature
- testRegistrationIntentWhenClientShouldNotBeRunning
- Feature
- testDeferredRegistrationsIssued
- Feature
- testRegistrationRetries
- createStartIntent
- createStopIntent
- createRegisterIntent
- createRegisterIntent
- isAndroidListenerStartIntent
- isAndroidListenerStopIntent
package org.chromium.sync.notifier;
import android.accounts.Account;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.test.ServiceTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.google.ipc.invalidation.external.client.InvalidationListener.RegistrationState;
import com.google.ipc.invalidation.external.client.contrib.AndroidListener;
import com.google.ipc.invalidation.external.client.types.ErrorInfo;
import com.google.ipc.invalidation.external.client.types.Invalidation;
import com.google.ipc.invalidation.external.client.types.ObjectId;
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.InvalidationPreferences.EditContext;
import org.chromium.sync.signin.AccountManagerHelper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidationService> {
private static final byte[] CLIENT_ID = new byte[]{0, 4, 7};
private List<Intent> mStartServiceIntents;
public InvalidationServiceTest() {
super(TestableInvalidationService.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
mStartServiceIntents = new ArrayList<Intent>();
setContext(new AdvancedMockContext(getContext()) {
@Override
public ComponentName startService(Intent intent) {
mStartServiceIntents.add(intent);
return new ComponentName(this, InvalidationServiceTest.class);
}
});
setupService();
}
@Override
public void tearDown() throws Exception {
if (InvalidationService.getIsClientStartedForTest()) {
Intent stopIntent = createStopIntent();
getService().onHandleIntent(stopIntent);
}
assertFalse(InvalidationService.getIsClientStartedForTest());
super.tearDown();
}
@SmallTest
@Feature({"Sync"})
public void testComputeRegistrationOps() {
Set<ObjectId> regAccumulator = new HashSet<ObjectId>();
Set<ObjectId> unregAccumulator = new HashSet<ObjectId>();
InvalidationService.computeRegistrationOps(
ModelType.modelTypesToObjectIds(
CollectionUtil.newHashSet(ModelType.BOOKMARK, ModelType.SESSION)),
ModelType.modelTypesToObjectIds(
CollectionUtil.newHashSet(ModelType.BOOKMARK, ModelType.SESSION)),
regAccumulator, unregAccumulator);
assertEquals(0, regAccumulator.size());
assertEquals(0, unregAccumulator.size());
InvalidationService.computeRegistrationOps(new HashSet<ObjectId>(),
new HashSet<ObjectId>(), regAccumulator, unregAccumulator);
assertEquals(0, regAccumulator.size());
assertEquals(0, unregAccumulator.size());
Set<ObjectId> desiredTypes =
CollectionUtil.newHashSet(
ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId());
InvalidationService.computeRegistrationOps(
new HashSet<ObjectId>(),
desiredTypes,
regAccumulator, unregAccumulator);
assertEquals(
CollectionUtil.newHashSet(
ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId()),
new HashSet<ObjectId>(regAccumulator));
assertEquals(0, unregAccumulator.size());
regAccumulator.clear();
InvalidationService.computeRegistrationOps(
CollectionUtil.newHashSet(
ModelType.SESSION.toObjectId(), ModelType.TYPED_URL.toObjectId()),
CollectionUtil.newHashSet(
ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId()),
regAccumulator, unregAccumulator);
assertEquals(CollectionUtil.newHashSet(ModelType.BOOKMARK.toObjectId()), regAccumulator);
assertEquals(CollectionUtil.newHashSet(ModelType.TYPED_URL.toObjectId()),
unregAccumulator);
regAccumulator.clear();
unregAccumulator.clear();
}
@SmallTest
@Feature({"Sync"})
public void testReady() {
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
EditContext editContext = invPrefs.edit();
invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("BOOKMARK", "SESSION"));
ObjectId objectId = ObjectId.newInstance(1, "obj".getBytes());
invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(objectId));
assertTrue(invPrefs.commit(editContext));
getService().ready(CLIENT_ID);
assertTrue(Arrays.equals(CLIENT_ID, InvalidationService.getClientIdForTest()));
byte[] otherCid = "otherCid".getBytes();
getService().ready(otherCid);
assertTrue(Arrays.equals(otherCid, InvalidationService.getClientIdForTest()));
assertEquals(CollectionUtil.newHashSet(
ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId(), objectId),
new HashSet<ObjectId>(getService().mRegistrations.get(0)));
}
@SmallTest
@Feature({"Sync"})
public void testReissueRegistrations() {
getService().reissueRegistrations(CLIENT_ID);
assertTrue(getService().mRegistrations.isEmpty());
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
EditContext editContext = invPrefs.edit();
invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("BOOKMARK", "SESSION"));
ObjectId objectId = ObjectId.newInstance(1, "obj".getBytes());
invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(objectId));
assertTrue(invPrefs.commit(editContext));
getService().reissueRegistrations(CLIENT_ID);
assertEquals(1, getService().mRegistrations.size());
assertEquals(CollectionUtil.newHashSet(
ModelType.BOOKMARK.toObjectId(), ModelType.SESSION.toObjectId(), objectId),
new HashSet<ObjectId>(getService().mRegistrations.get(0)));
}
@SmallTest
@Feature({"Sync"})
public void testInformRegistrationStatus() {
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
EditContext editContext = invPrefs.edit();
invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("SESSION"));
ObjectId desiredObjectId = ObjectId.newInstance(1, "obj1".getBytes());
ObjectId undesiredObjectId = ObjectId.newInstance(1, "obj2".getBytes());
invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(desiredObjectId));
assertTrue(invPrefs.commit(editContext));
getService().informRegistrationStatus(CLIENT_ID, ModelType.SESSION.toObjectId(),
RegistrationState.REGISTERED);
getService().informRegistrationStatus(CLIENT_ID, desiredObjectId,
RegistrationState.REGISTERED);
getService().informRegistrationStatus(CLIENT_ID, ModelType.BOOKMARK.toObjectId(),
RegistrationState.UNREGISTERED);
getService().informRegistrationStatus(CLIENT_ID, undesiredObjectId,
RegistrationState.UNREGISTERED);
assertTrue(getService().mRegistrations.isEmpty());
assertTrue(getService().mUnregistrations.isEmpty());
getService().informRegistrationStatus(CLIENT_ID, ModelType.BOOKMARK.toObjectId(),
RegistrationState.REGISTERED);
getService().informRegistrationStatus(CLIENT_ID, undesiredObjectId,
RegistrationState.REGISTERED);
assertEquals(2, getService().mUnregistrations.size());
assertEquals(0, getService().mRegistrations.size());
assertEquals(CollectionUtil.newArrayList(ModelType.BOOKMARK.toObjectId()),
getService().mUnregistrations.get(0));
assertEquals(CollectionUtil.newArrayList(undesiredObjectId),
getService().mUnregistrations.get(1));
getService().informRegistrationStatus(CLIENT_ID, ModelType.SESSION.toObjectId(),
RegistrationState.UNREGISTERED);
getService().informRegistrationStatus(CLIENT_ID, desiredObjectId,
RegistrationState.UNREGISTERED);
assertEquals(2, getService().mUnregistrations.size());
assertEquals(2, getService().mRegistrations.size());
assertEquals(CollectionUtil.newArrayList(ModelType.SESSION.toObjectId()),
getService().mRegistrations.get(0));
assertEquals(CollectionUtil.newArrayList(desiredObjectId),
getService().mRegistrations.get(1));
}
@SmallTest
@Feature({"Sync"})
public void testInformRegistrationFailure() {
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
EditContext editContext = invPrefs.edit();
invPrefs.setSyncTypes(editContext, CollectionUtil.newArrayList("SESSION"));
ObjectId desiredObjectId = ObjectId.newInstance(1, "obj1".getBytes());
ObjectId undesiredObjectId = ObjectId.newInstance(1, "obj2".getBytes());
invPrefs.setObjectIds(editContext, CollectionUtil.newArrayList(desiredObjectId));
assertTrue(invPrefs.commit(editContext));
getService().informRegistrationFailure(CLIENT_ID, ModelType.SESSION.toObjectId(), false,
"");
getService().informRegistrationFailure(CLIENT_ID, ModelType.BOOKMARK.toObjectId(), false,
"");
getService().informRegistrationFailure(CLIENT_ID, desiredObjectId, false, "");
getService().informRegistrationFailure(CLIENT_ID, undesiredObjectId, false, "");
assertTrue(getService().mRegistrations.isEmpty());
assertTrue(getService().mUnregistrations.isEmpty());
getService().informRegistrationFailure(CLIENT_ID, ModelType.SESSION.toObjectId(), true, "");
getService().informRegistrationFailure(CLIENT_ID, desiredObjectId, true, "");
assertEquals(2, getService().mRegistrations.size());
assertTrue(getService().mUnregistrations.isEmpty());
assertEquals(CollectionUtil.newArrayList(ModelType.SESSION.toObjectId()),
getService().mRegistrations.get(0));
assertEquals(CollectionUtil.newArrayList(desiredObjectId),
getService().mRegistrations.get(1));
getService().informRegistrationFailure(CLIENT_ID, ModelType.BOOKMARK.toObjectId(), true,
"");
getService().informRegistrationFailure(CLIENT_ID, undesiredObjectId, true, "");
assertEquals(2, getService().mRegistrations.size());
assertEquals(2, getService().mUnregistrations.size());
assertEquals(CollectionUtil.newArrayList(ModelType.BOOKMARK.toObjectId()),
getService().mUnregistrations.get(0));
assertEquals(CollectionUtil.newArrayList(undesiredObjectId),
getService().mUnregistrations.get(1));
}
@SmallTest
@Feature({"Sync"})
public void testInformError() {
getService().setShouldRunStates(true, true);
getService().onCreate();
getService().onHandleIntent(createStartIntent());
getService().mStartedServices.clear();
getService().informError(ErrorInfo.newInstance(0, true, "transient", null));
assertTrue(getService().mStartedServices.isEmpty());
getService().informError(ErrorInfo.newInstance(0, false, "permanent", null));
assertEquals(1, getService().mStartedServices.size());
Intent sentIntent = getService().mStartedServices.get(0);
Intent stopIntent = AndroidListener.createStopIntent(getContext());
assertTrue(stopIntent.filterEquals(sentIntent));
assertEquals(stopIntent.getExtras().keySet(), sentIntent.getExtras().keySet());
}
@SmallTest
@Feature({"Sync"})
public void testReadWriteState() {
assertNull(getService().readState());
byte[] writtenState = new byte[]{7, 4, 0};
getService().writeState(writtenState);
assertTrue(Arrays.equals(writtenState, getService().readState()));
}
@SmallTest
@Feature({"Sync"})
public void testInvalidateWithPayload() {
doTestInvalidate(true);
}
@SmallTest
@Feature({"Sync"})
public void testInvalidateWithoutPayload() {
doTestInvalidate(false);
}
private void doTestInvalidate(boolean hasPayload) {
int version = 4747;
ObjectId objectId = ObjectId.newInstance(55, "BOOKMARK".getBytes());
final String payload = "testInvalidate-" + hasPayload;
Invalidation invalidation = hasPayload ?
Invalidation.newInstance(objectId, version, payload.getBytes()) :
Invalidation.newInstance(objectId, version);
byte[] ackHandle = ("testInvalidate-" + hasPayload).getBytes();
getService().invalidate(invalidation, ackHandle);
assertEquals(1, getService().mRequestedSyncs.size());
Bundle syncBundle = getService().mRequestedSyncs.get(0);
assertEquals(55, syncBundle.getInt("objectSource"));
assertEquals("BOOKMARK", syncBundle.getString("objectId"));
assertEquals(version, syncBundle.getLong("version"));
assertEquals(hasPayload ? payload : "", syncBundle.getString("payload"));
assertSingleAcknowledgement(ackHandle);
}
@SmallTest
@Feature({"Sync"})
public void testInvalidateUnknownVersion() {
ObjectId objectId = ObjectId.newInstance(55, "BOOKMARK".getBytes());
byte[] ackHandle = "testInvalidateUV".getBytes();
getService().invalidateUnknownVersion(objectId, ackHandle);
assertEquals(1, getService().mRequestedSyncs.size());
Bundle syncBundle = getService().mRequestedSyncs.get(0);
assertEquals(55, syncBundle.getInt("objectSource"));
assertEquals("BOOKMARK", syncBundle.getString("objectId"));
assertEquals(0, syncBundle.getLong("version"));
assertEquals("", syncBundle.getString("payload"));
assertSingleAcknowledgement(ackHandle);
}
@SmallTest
@Feature({"Sync"})
public void testInvalidateAll() {
byte[] ackHandle = "testInvalidateAll".getBytes();
getService().invalidateAll(ackHandle);
assertEquals(1, getService().mRequestedSyncs.size());
Bundle syncBundle = getService().mRequestedSyncs.get(0);
assertEquals(0, syncBundle.keySet().size());
assertSingleAcknowledgement(ackHandle);
}
private void assertSingleAcknowledgement(byte[] ackHandle) {
assertEquals(1, getService().mAcknowledgements.size());
assertTrue(Arrays.equals(ackHandle, getService().mAcknowledgements.get(0)));
}
@SmallTest
@Feature({"Sync"})
public void testShouldClientBeRunning() {
getService().setShouldRunStates(false, false);
assertFalse(getService().shouldClientBeRunning());
getService().setShouldRunStates(false, true);
assertFalse(getService().shouldClientBeRunning());
getService().setShouldRunStates(true, false);
assertFalse(getService().shouldClientBeRunning());
getService().setShouldRunStates(true, true);
assertTrue(getService().shouldClientBeRunning());
}
@SmallTest
@Feature({"Sync"})
public void testStartAndStopClient() {
getService().setShouldRunStates(true, true);
getService().onCreate();
Intent startIntent = createStartIntent();
getService().onHandleIntent(startIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
Intent stopIntent = createStopIntent();
getService().onHandleIntent(stopIntent);
assertFalse(InvalidationService.getIsClientStartedForTest());
assertEquals(2, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
assertTrue(isAndroidListenerStopIntent(mStartServiceIntents.get(1)));
}
@SmallTest
@Feature({"Sync"})
public void testClientStopsWhenShouldNotBeRunning() {
getService().setShouldRunStates(true, true);
getService().onCreate();
Intent startIntent = createStartIntent();
getService().onHandleIntent(startIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
getService().setShouldRunStates(false, false);
getService().onHandleIntent(startIntent);
assertFalse(InvalidationService.getIsClientStartedForTest());
assertEquals(2, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
assertTrue(isAndroidListenerStopIntent(mStartServiceIntents.get(1)));
}
@SmallTest
@Feature({"Sync"})
public void testRegistrationIntent() {
getService().setShouldRunStates(true, true);
getService().onCreate();
Set<ModelType> desiredRegistrations = CollectionUtil.newHashSet(
ModelType.BOOKMARK, ModelType.SESSION);
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Intent registrationIntent = createRegisterIntent(account, false, desiredRegistrations);
getService().onHandleIntent(registrationIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations),
invPrefs.getSavedSyncedTypes());
assertNull(invPrefs.getSavedObjectIds());
assertEquals(1, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
getService().onHandleIntent(createRegisterIntent(account, true, null));
assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE),
invPrefs.getSavedSyncedTypes());
assertEquals(1, mStartServiceIntents.size());
Account account2 = AccountManagerHelper.createAccountFromName("test2@example.com");
getService().onHandleIntent(createRegisterIntent(account2, true, null));
assertEquals(account2, invPrefs.getSavedSyncedAccount());
assertEquals(3, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
assertTrue(isAndroidListenerStopIntent(mStartServiceIntents.get(1)));
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(2)));
}
private boolean expectedObjectIdsRegistered(Set<ModelType> expectedTypes,
Set<ObjectId> expectedObjectIds, boolean isReady) {
Set<String> expectedSyncTypes = ModelType.modelTypesToSyncTypes(expectedTypes);
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
Set<String> actualSyncTypes = invPrefs.getSavedSyncedTypes();
if (actualSyncTypes == null) {
actualSyncTypes = new HashSet<String>();
}
Set<ObjectId> actualObjectIds = invPrefs.getSavedObjectIds();
if (actualObjectIds == null) {
actualObjectIds = new HashSet<ObjectId>();
}
Set<ObjectId> expectedRegisteredIds = new HashSet<ObjectId>();
if (isReady) {
expectedRegisteredIds.addAll(ModelType.modelTypesToObjectIds(expectedTypes));
expectedRegisteredIds.addAll(expectedObjectIds);
}
return actualSyncTypes.equals(expectedSyncTypes) &&
actualObjectIds.equals(expectedObjectIds) &&
getService().mCurrentRegistrations.equals(expectedRegisteredIds);
}
@SmallTest
@Feature({"Sync"})
public void testRegistrationIntentWithTypesAndObjectIds() {
getService().setShouldRunStates(true, true);
getService().onCreate();
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Set<ObjectId> objectIds = new HashSet<ObjectId>();
Set<ModelType> types = new HashSet<ModelType>();
objectIds.add(ObjectId.newInstance(1, "obj1".getBytes()));
objectIds.add(ObjectId.newInstance(2, "obj2".getBytes()));
Intent registrationIntent =
createRegisterIntent(account, new int[] {1, 2}, new String[] {"obj1", "obj2"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, false ));
types.add(ModelType.BOOKMARK);
types.add(ModelType.SESSION);
registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, false ));
getService().ready(CLIENT_ID);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true ));
objectIds.add(ObjectId.newInstance(3, "obj3".getBytes()));
registrationIntent = createRegisterIntent(
account, new int[] {1, 2, 3}, new String[] {"obj1", "obj2", "obj3"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true ));
types.remove(ModelType.BOOKMARK);
registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true ));
types.clear();
registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true ));
objectIds.remove(ObjectId.newInstance(2, "obj2".getBytes()));
registrationIntent = createRegisterIntent(
account, new int[] {1, 3}, new String[] {"obj1", "obj3"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true ));
objectIds.clear();
registrationIntent = createRegisterIntent(account, new int[0], new String[0]);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true ));
types.add(ModelType.BOOKMARK);
types.add(ModelType.PASSWORD);
registrationIntent = createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true ));
}
@SmallTest
@Feature({"Sync"})
public void testRegistrationIntentNoProxyTabsUsingReady() {
getService().setShouldRunStates(true, true);
getService().onCreate();
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Intent registrationIntent = createRegisterIntent(account, true, null);
getService().onHandleIntent(registrationIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE),
invPrefs.getSavedSyncedTypes());
assertEquals(1, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
getService().ready(CLIENT_ID);
assertTrue(Arrays.equals(CLIENT_ID, InvalidationService.getClientIdForTest()));
Set<ObjectId> expectedTypes =
ModelType.modelTypesToObjectIds(EnumSet.allOf(ModelType.class));
assertEquals(expectedTypes, new HashSet<ObjectId>(getService().mRegistrations.get(0)));
}
@SmallTest
@Feature({"Sync"})
public void testRegistrationIntentNoProxyTabsAlreadyWithClientId() {
getService().setShouldRunStates(true, true);
getService().onCreate();
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Intent registrationIntent = createRegisterIntent(account, false, new HashSet<ModelType>());
getService().onHandleIntent(registrationIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(new HashSet<String>(), invPrefs.getSavedSyncedTypes());
assertEquals(1, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
getService().ready(CLIENT_ID);
assertTrue(Arrays.equals(CLIENT_ID, InvalidationService.getClientIdForTest()));
registrationIntent = createRegisterIntent(account, true, null);
getService().onHandleIntent(registrationIntent);
assertEquals(1, getService().mRegistrations.size());
Set<ObjectId> expectedTypes =
ModelType.modelTypesToObjectIds(EnumSet.allOf(ModelType.class));
assertEquals(expectedTypes, new HashSet<ObjectId>(getService().mRegistrations.get(0)));
}
@SmallTest
@Feature({"Sync"})
public void testRegistrationIntentWhenClientShouldNotBeRunning() {
getService().onCreate();
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Set<ModelType> desiredRegistrations = CollectionUtil.newHashSet(
ModelType.BOOKMARK, ModelType.SESSION);
Intent registrationIntent = createRegisterIntent(account, false, desiredRegistrations);
getService().onHandleIntent(registrationIntent);
assertFalse(InvalidationService.getIsClientStartedForTest());
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations),
invPrefs.getSavedSyncedTypes());
assertEquals(0, mStartServiceIntents.size());
}
@SmallTest
@Feature({"Sync"})
public void testDeferredRegistrationsIssued() {
getService().setShouldRunStates(true, true);
getService().onCreate();
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Set<ModelType> desiredRegistrations = CollectionUtil.newHashSet(
ModelType.BOOKMARK, ModelType.SESSION);
Set<ObjectId> desiredObjectIds = ModelType.modelTypesToObjectIds(desiredRegistrations);
Intent registrationIntent = createRegisterIntent(account, false, desiredRegistrations);
getService().onHandleIntent(registrationIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
assertEquals(1, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
InvalidationPreferences invPrefs = new InvalidationPreferences(getContext());
assertEquals(ModelType.modelTypesToSyncTypes(desiredRegistrations),
invPrefs.getSavedSyncedTypes());
assertEquals(desiredObjectIds, getService().readRegistrationsFromPrefs());
getService().reissueRegistrations(CLIENT_ID);
assertEquals(2, mStartServiceIntents.size());
Intent expectedRegisterIntent = AndroidListener.createRegisterIntent(
getContext(),
CLIENT_ID,
desiredObjectIds);
Intent actualRegisterIntent = mStartServiceIntents.get(1);
assertTrue(expectedRegisterIntent.filterEquals(actualRegisterIntent));
assertEquals(expectedRegisterIntent.getExtras().keySet(),
actualRegisterIntent.getExtras().keySet());
assertEquals(
desiredObjectIds,
new HashSet<ObjectId>(getService().mRegistrations.get(0)));
}
@SmallTest
@Feature({"Sync"})
public void testRegistrationRetries() {
}
private Intent createStartIntent() {
Intent intent = new Intent();
return intent;
}
private Intent createStopIntent() {
Intent intent = new Intent();
intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
return intent;
}
private Intent createRegisterIntent(Account account, boolean allTypes, Set<ModelType> types) {
Intent intent = InvalidationIntentProtocol.createRegisterIntent(account, allTypes, types);
return intent;
}
private Intent createRegisterIntent(
Account account, int[] objectSources, String[] objectNames) {
Intent intent = InvalidationIntentProtocol.createRegisterIntent(
account, objectSources, objectNames);
return intent;
}
private boolean isAndroidListenerStartIntent(Intent intent) {
Intent startIntent = AndroidListener.createStartIntent(getContext(),
InvalidationService.CLIENT_TYPE, "unused".getBytes());
return intent.getExtras().keySet().equals(startIntent.getExtras().keySet());
}
private boolean isAndroidListenerStopIntent(Intent intent) {
Intent stopIntent = AndroidListener.createStopIntent(getContext());
return intent.getExtras().keySet().equals(stopIntent.getExtras().keySet());
}
}