summaryrefslogtreecommitdiffstats
path: root/sync/android
diff options
context:
space:
mode:
authorrlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-27 02:26:19 +0000
committerrlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-27 02:26:19 +0000
commit4d039829568644cfd7790fc14b27472e5e87ec95 (patch)
tree74155e6d5d9b0d870d78cc8e3ef446ab5ec4ef1d /sync/android
parent085aba972a62c81223d563ccd1182f2a5b832041 (diff)
downloadchromium_src-4d039829568644cfd7790fc14b27472e5e87ec95.zip
chromium_src-4d039829568644cfd7790fc14b27472e5e87ec95.tar.gz
chromium_src-4d039829568644cfd7790fc14b27472e5e87ec95.tar.bz2
(1 of 2) Move InvalidationController to chrome/
Move the InvalidationController Java class from sync/notifier to chrome/browser/invalidation. Move its IntentProtocol inner class into its own class, InvalidationIntentProtocol, and keep it in sync/notifier. In order to prevent this change from breaking the downstream build, a reduced version of InvalidationController has been left behind in the old location. The second part of this patch will remove it. An unfortunate side effect of having two InvalidationControllers is that they may be double-notified when the when the activity state changes, and this may cause the start or stop signals to be sent to the InvalidationService twice. This is wasteful, but otherwise mostly harmless. TBR=jhawkins BUG=172391 Review URL: https://codereview.chromium.org/45103003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@231232 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sync/android')
-rw-r--r--sync/android/java/src/org/chromium/sync/notifier/InvalidationController.java138
-rw-r--r--sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java142
-rw-r--r--sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java17
-rw-r--r--sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java345
-rw-r--r--sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java52
-rw-r--r--sync/android/javatests/src/org/chromium/sync/notifier/signin/SyncStatusHelperTest.java1
6 files changed, 185 insertions, 510 deletions
diff --git a/sync/android/java/src/org/chromium/sync/notifier/InvalidationController.java b/sync/android/java/src/org/chromium/sync/notifier/InvalidationController.java
index 93685db..78da3bd 100644
--- a/sync/android/java/src/org/chromium/sync/notifier/InvalidationController.java
+++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationController.java
@@ -8,18 +8,12 @@ import android.accounts.Account;
import android.content.Context;
import android.content.Intent;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
-import com.google.ipc.invalidation.external.client.types.ObjectId;
-import com.google.protos.ipc.invalidation.Types;
import org.chromium.base.ActivityStatus;
import org.chromium.base.CalledByNative;
-import org.chromium.base.CollectionUtil;
import org.chromium.sync.internal_api.pub.base.ModelType;
-import java.util.ArrayList;
-import java.util.HashSet;
import java.util.Set;
/**
@@ -27,129 +21,6 @@ import java.util.Set;
* client library used by Sync.
*/
public class InvalidationController implements ActivityStatus.StateListener {
- /**
- * Constants and utility methods to create the intents used to communicate between the
- * controller and the invalidation client library.
- */
- public static class IntentProtocol {
- /**
- * Action set on register intents.
- */
- public static final String ACTION_REGISTER =
- "org.chromium.sync.notifier.ACTION_REGISTER_TYPES";
-
- /**
- * Parcelable-valued intent extra containing the account of the user.
- */
- public static final String EXTRA_ACCOUNT = "account";
-
- /**
- * String-list-valued intent extra of the syncable types to sync.
- */
- public static final String EXTRA_REGISTERED_TYPES = "registered_types";
-
- /**
- * Int-array-valued intent extra containing sources of objects to register for.
- * The array is parallel to EXTRA_REGISTERED_OBJECT_NAMES.
- */
- public static final String EXTRA_REGISTERED_OBJECT_SOURCES = "registered_object_sources";
-
- /**
- * String-array-valued intent extra containing names of objects to register for.
- * The array is parallel to EXTRA_REGISTERED_OBJECT_SOURCES.
- */
- public static final String EXTRA_REGISTERED_OBJECT_NAMES = "registered_object_names";
-
- /**
- * Boolean-valued intent extra indicating that the service should be stopped.
- */
- public static final String EXTRA_STOP = "stop";
-
- /**
- * Create an Intent that will start the invalidation listener service and
- * register for the specified types.
- */
- public static Intent createRegisterIntent(Account account,
- boolean allTypes, Set<ModelType> types) {
- Intent registerIntent = new Intent(ACTION_REGISTER);
- String[] selectedTypesArray;
- if (allTypes) {
- selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE};
- } else {
- selectedTypesArray = new String[types.size()];
- int pos = 0;
- for (ModelType type : types) {
- selectedTypesArray[pos++] = type.name();
- }
- }
- registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES,
- CollectionUtil.newArrayList(selectedTypesArray));
- registerIntent.putExtra(EXTRA_ACCOUNT, account);
- return registerIntent;
- }
-
- /**
- * Create an Intent that will start the invalidation listener service and
- * register for the object ids with the specified sources and names.
- * Sync-specific objects are filtered out of the request since Sync types
- * are registered using the other version of createRegisterIntent.
- */
- public static Intent createRegisterIntent(Account account, int[] objectSources,
- String[] objectNames) {
- Preconditions.checkArgument(objectSources.length == objectNames.length,
- "objectSources and objectNames must have the same length");
-
- // Add all non-Sync objects to new lists.
- ArrayList<Integer> sources = new ArrayList<Integer>();
- ArrayList<String> names = new ArrayList<String>();
- for (int i = 0; i < objectSources.length; i++) {
- if (objectSources[i] != Types.ObjectSource.Type.CHROME_SYNC.getNumber()) {
- sources.add(objectSources[i]);
- names.add(objectNames[i]);
- }
- }
-
- Intent registerIntent = new Intent(ACTION_REGISTER);
- registerIntent.putIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES, sources);
- registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES, names);
- registerIntent.putExtra(EXTRA_ACCOUNT, account);
- return registerIntent;
- }
-
- /** Returns whether {@code intent} is a stop intent. */
- public static boolean isStop(Intent intent) {
- return intent.getBooleanExtra(EXTRA_STOP, false);
- }
-
- /** Returns whether {@code intent} is a registered types change intent. */
- public static boolean isRegisteredTypesChange(Intent intent) {
- return intent.hasExtra(EXTRA_REGISTERED_TYPES) ||
- intent.hasExtra(EXTRA_REGISTERED_OBJECT_SOURCES);
- }
-
- /** Returns the object ids for which to register contained in the intent. */
- public static Set<ObjectId> getRegisteredObjectIds(Intent intent) {
- ArrayList<Integer> objectSources =
- intent.getIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES);
- ArrayList<String> objectNames =
- intent.getStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES);
- if (objectSources == null || objectNames == null ||
- objectSources.size() != objectNames.size()) {
- return null;
- }
- Set<ObjectId> objectIds = new HashSet<ObjectId>(objectSources.size());
- for (int i = 0; i < objectSources.size(); i++) {
- objectIds.add(ObjectId.newInstance(
- objectSources.get(i), objectNames.get(i).getBytes()));
- }
- return objectIds;
- }
-
- private IntentProtocol() {
- // Disallow instantiation.
- }
- }
-
private static final Object LOCK = new Object();
private static InvalidationController sInstance;
@@ -164,7 +35,8 @@ public class InvalidationController implements ActivityStatus.StateListener {
* @param types Set of types for which to register. Ignored if {@code allTypes == true}.
*/
public void setRegisteredTypes(Account account, boolean allTypes, Set<ModelType> types) {
- Intent registerIntent = IntentProtocol.createRegisterIntent(account, allTypes, types);
+ Intent registerIntent =
+ InvalidationIntentProtocol.createRegisterIntent(account, allTypes, types);
registerIntent.setClass(mContext, InvalidationService.class);
mContext.startService(registerIntent);
}
@@ -196,7 +68,8 @@ public class InvalidationController implements ActivityStatus.StateListener {
public void setRegisteredObjectIds(int[] objectSources, String[] objectNames) {
InvalidationPreferences invalidationPreferences = new InvalidationPreferences(mContext);
Account account = invalidationPreferences.getSavedSyncedAccount();
- Intent registerIntent = IntentProtocol.createRegisterIntent(account, objectSources,
+ Intent registerIntent =
+ InvalidationIntentProtocol.createRegisterIntent(account, objectSources,
objectNames);
registerIntent.setClass(mContext, InvalidationService.class);
mContext.startService(registerIntent);
@@ -215,7 +88,7 @@ public class InvalidationController implements ActivityStatus.StateListener {
*/
public void stop() {
Intent intent = new Intent(mContext, InvalidationService.class);
- intent.putExtra(IntentProtocol.EXTRA_STOP, true);
+ intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
mContext.startService(intent);
}
@@ -237,7 +110,6 @@ public class InvalidationController implements ActivityStatus.StateListener {
/**
* Creates an instance using {@code context} to send intents.
*/
- @VisibleForTesting
InvalidationController(Context context) {
mContext = Preconditions.checkNotNull(context.getApplicationContext());
ActivityStatus.registerStateListener(this);
diff --git a/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java b/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java
new file mode 100644
index 0000000..c87b894
--- /dev/null
+++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationIntentProtocol.java
@@ -0,0 +1,142 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.sync.notifier;
+
+import android.accounts.Account;
+import android.content.Intent;
+
+import com.google.common.base.Preconditions;
+import com.google.ipc.invalidation.external.client.types.ObjectId;
+import com.google.protos.ipc.invalidation.Types;
+
+import org.chromium.base.CollectionUtil;
+import org.chromium.sync.internal_api.pub.base.ModelType;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Constants and utility methods to create the intents used to communicate between the
+ * controller and the invalidation client library.
+ */
+public class InvalidationIntentProtocol {
+ /**
+ * Action set on register intents.
+ */
+ public static final String ACTION_REGISTER =
+ "org.chromium.sync.notifier.ACTION_REGISTER_TYPES";
+
+ /**
+ * Parcelable-valued intent extra containing the account of the user.
+ */
+ public static final String EXTRA_ACCOUNT = "account";
+
+ /**
+ * String-list-valued intent extra of the syncable types to sync.
+ */
+ public static final String EXTRA_REGISTERED_TYPES = "registered_types";
+
+ /**
+ * Int-array-valued intent extra containing sources of objects to register for.
+ * The array is parallel to EXTRA_REGISTERED_OBJECT_NAMES.
+ */
+ public static final String EXTRA_REGISTERED_OBJECT_SOURCES = "registered_object_sources";
+
+ /**
+ * String-array-valued intent extra containing names of objects to register for.
+ * The array is parallel to EXTRA_REGISTERED_OBJECT_SOURCES.
+ */
+ public static final String EXTRA_REGISTERED_OBJECT_NAMES = "registered_object_names";
+
+ /**
+ * Boolean-valued intent extra indicating that the service should be stopped.
+ */
+ public static final String EXTRA_STOP = "stop";
+
+ /**
+ * Create an Intent that will start the invalidation listener service and
+ * register for the specified types.
+ */
+ public static Intent createRegisterIntent(Account account,
+ boolean allTypes, Set<ModelType> types) {
+ Intent registerIntent = new Intent(ACTION_REGISTER);
+ String[] selectedTypesArray;
+ if (allTypes) {
+ selectedTypesArray = new String[]{ModelType.ALL_TYPES_TYPE};
+ } else {
+ selectedTypesArray = new String[types.size()];
+ int pos = 0;
+ for (ModelType type : types) {
+ selectedTypesArray[pos++] = type.name();
+ }
+ }
+ registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_TYPES,
+ CollectionUtil.newArrayList(selectedTypesArray));
+ registerIntent.putExtra(EXTRA_ACCOUNT, account);
+ return registerIntent;
+ }
+
+ /**
+ * Create an Intent that will start the invalidation listener service and
+ * register for the object ids with the specified sources and names.
+ * Sync-specific objects are filtered out of the request since Sync types
+ * are registered using the other version of createRegisterIntent.
+ */
+ public static Intent createRegisterIntent(Account account, int[] objectSources,
+ String[] objectNames) {
+ Preconditions.checkArgument(objectSources.length == objectNames.length,
+ "objectSources and objectNames must have the same length");
+
+ // Add all non-Sync objects to new lists.
+ ArrayList<Integer> sources = new ArrayList<Integer>();
+ ArrayList<String> names = new ArrayList<String>();
+ for (int i = 0; i < objectSources.length; i++) {
+ if (objectSources[i] != Types.ObjectSource.Type.CHROME_SYNC.getNumber()) {
+ sources.add(objectSources[i]);
+ names.add(objectNames[i]);
+ }
+ }
+
+ Intent registerIntent = new Intent(ACTION_REGISTER);
+ registerIntent.putIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES, sources);
+ registerIntent.putStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES, names);
+ registerIntent.putExtra(EXTRA_ACCOUNT, account);
+ return registerIntent;
+ }
+
+ /** Returns whether {@code intent} is a stop intent. */
+ public static boolean isStop(Intent intent) {
+ return intent.getBooleanExtra(EXTRA_STOP, false);
+ }
+
+ /** Returns whether {@code intent} is a registered types change intent. */
+ public static boolean isRegisteredTypesChange(Intent intent) {
+ return intent.hasExtra(EXTRA_REGISTERED_TYPES) ||
+ intent.hasExtra(EXTRA_REGISTERED_OBJECT_SOURCES);
+ }
+
+ /** Returns the object ids for which to register contained in the intent. */
+ public static Set<ObjectId> getRegisteredObjectIds(Intent intent) {
+ ArrayList<Integer> objectSources =
+ intent.getIntegerArrayListExtra(EXTRA_REGISTERED_OBJECT_SOURCES);
+ ArrayList<String> objectNames =
+ intent.getStringArrayListExtra(EXTRA_REGISTERED_OBJECT_NAMES);
+ if (objectSources == null || objectNames == null ||
+ objectSources.size() != objectNames.size()) {
+ return null;
+ }
+ Set<ObjectId> objectIds = new HashSet<ObjectId>(objectSources.size());
+ for (int i = 0; i < objectSources.size(); i++) {
+ objectIds.add(ObjectId.newInstance(
+ objectSources.get(i), objectNames.get(i).getBytes()));
+ }
+ return objectIds;
+ }
+
+ private InvalidationIntentProtocol() {
+ // Disallow instantiation.
+ }
+}
diff --git a/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java b/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java
index 536f3b1..ea3e73a 100644
--- a/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java
+++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java
@@ -22,7 +22,7 @@ import com.google.protos.ipc.invalidation.Types.ClientType;
import org.chromium.base.ActivityStatus;
import org.chromium.base.CollectionUtil;
import org.chromium.sync.internal_api.pub.base.ModelType;
-import org.chromium.sync.notifier.InvalidationController.IntentProtocol;
+import org.chromium.sync.notifier.InvalidationIntentProtocol;
import org.chromium.sync.notifier.InvalidationPreferences.EditContext;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
@@ -82,21 +82,22 @@ public class InvalidationService extends AndroidListener {
// match the stored account. Then, if a client should be running, ensureClientStartState
// will start a new one if needed. I.e., these two functions work together to restart the
// client when the account changes.
- Account account = intent.hasExtra(IntentProtocol.EXTRA_ACCOUNT) ?
- (Account) intent.getParcelableExtra(IntentProtocol.EXTRA_ACCOUNT) : null;
+ Account account = intent.hasExtra(InvalidationIntentProtocol.EXTRA_ACCOUNT) ?
+ (Account) intent.getParcelableExtra(InvalidationIntentProtocol.EXTRA_ACCOUNT)
+ : null;
ensureAccount(account);
ensureClientStartState();
// Handle the intent.
- if (IntentProtocol.isStop(intent) && sIsClientStarted) {
+ if (InvalidationIntentProtocol.isStop(intent) && sIsClientStarted) {
// If the intent requests that the client be stopped, stop it.
stopClient();
- } else if (IntentProtocol.isRegisteredTypesChange(intent)) {
+ } else if (InvalidationIntentProtocol.isRegisteredTypesChange(intent)) {
// If the intent requests a change in registrations, change them.
- List<String> regTypes =
- intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTERED_TYPES);
+ List<String> regTypes = intent.getStringArrayListExtra(
+ InvalidationIntentProtocol.EXTRA_REGISTERED_TYPES);
setRegisteredTypes(regTypes != null ? new HashSet<String>(regTypes) : null,
- IntentProtocol.getRegisteredObjectIds(intent));
+ InvalidationIntentProtocol.getRegisteredObjectIds(intent));
} else {
// Otherwise, we don't recognize the intent. Pass it to the notification client service.
super.onHandleIntent(intent);
diff --git a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java
deleted file mode 100644
index f38eebf..0000000
--- a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java
+++ /dev/null
@@ -1,345 +0,0 @@
-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-package org.chromium.sync.notifier;
-
-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.ActivityStatus;
-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.InvalidationController.IntentProtocol;
-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;
-
-/**
- * Tests for the {@link InvalidationController}.
- */
-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);
- // We don't want to use the system content resolver, so we override it.
- MockSyncContentResolverDelegate delegate = new MockSyncContentResolverDelegate();
- // Android master sync can safely always be on.
- 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);
- 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 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 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 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 testPausingMainActivityWithSyncDisabled() throws Exception {
- // Resuming main activity should NOT trigger a stop if sync is disabled.
- setupSync(false);
- mController.onActivityStateChange(ActivityStatus.PAUSED);
- 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();
-
- // Create instance.
- new InvalidationController(mContext) {
- @Override
- public void onActivityStateChange(int newState) {
- listenerCallbackCalled.set(true);
- }
- };
-
- // Ensure initial state is correct.
- assertFalse(listenerCallbackCalled.get());
-
- // Ensure we get a callback, which means we have registered for them.
- ActivityStatus.onStateChangeForTesting(new Activity(), ActivityStatus.RESUMED);
- 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());
-
- // 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 = CollectionUtil.newHashSet(ModelType.BOOKMARK.name(),
- ModelType.SESSION.name());
- Set<String> actualTypes = new HashSet<String>();
- actualTypes.addAll(intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTERED_TYPES));
- assertEquals(expectedTypes, actualTypes);
- assertNull(IntentProtocol.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());
-
- // 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 = CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE);
- Set<String> actualTypes = new HashSet<String>();
- actualTypes.addAll(intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTERED_TYPES));
- assertEquals(expectedTypes, actualTypes);
- assertNull(IntentProtocol.getRegisteredObjectIds(intent));
- }
-
- @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());
- 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);
-
- // 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(refreshedTypes);
-
- // Validate the values.
- assertEquals(storedAccount, resultAccount.get());
- assertEquals(false, resultAllTypes.get());
- assertEquals(ModelType.syncTypesToModelTypes(storedModelTypes), resultTypes.get());
- }
-
- @SmallTest
- @Feature({"Sync"})
- public void testRefreshShouldReadValuesFromDiskWithAllTypes() {
- // Store preferences for the ModelType.ALL_TYPES_TYPE 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();
- 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);
-
- // 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(new HashSet<ModelType>());
-
- // Validate the values.
- 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());
-
- // Validate destination.
- Intent intent = mContext.getStartedIntent(0);
- validateIntentComponent(intent);
- assertEquals(IntentProtocol.ACTION_REGISTER, intent.getAction());
-
- // Validate registered object ids. The bookmark object should not be registered since it is
- // a Sync type.
- assertNull(intent.getStringArrayListExtra(IntentProtocol.EXTRA_REGISTERED_TYPES));
- Set<ObjectId> objectIds = IntentProtocol.getRegisteredObjectIds(intent);
- assertEquals(2, objectIds.size());
- assertTrue(objectIds.contains(ObjectId.newInstance(1, "a".getBytes())));
- assertTrue(objectIds.contains(ObjectId.newInstance(2, "b".getBytes())));
- }
-
- /**
- * Asserts that {@code intent} is destined for the correct component.
- */
- private static void validateIntentComponent(Intent intent) {
- assertNotNull(intent.getComponent());
- assertEquals(InvalidationService.class.getName(),
- intent.getComponent().getClassName());
- }
-
- /**
- * Mock context that saves all intents given to {@code startService}.
- */
- 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();
- }
- }
-}
diff --git a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java
index fd8e070..f0d021a 100644
--- a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java
+++ b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationServiceTest.java
@@ -21,7 +21,7 @@ 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.InvalidationController.IntentProtocol;
+import org.chromium.sync.notifier.InvalidationIntentProtocol;
import org.chromium.sync.notifier.InvalidationPreferences.EditContext;
import org.chromium.sync.signin.AccountManagerHelper;
@@ -65,7 +65,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
@Override
public void tearDown() throws Exception {
if (InvalidationService.getIsClientStartedForTest()) {
- Intent stopIntent = new Intent().putExtra(IntentProtocol.EXTRA_STOP, true);
+ Intent stopIntent = new Intent().putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
getService().onHandleIntent(stopIntent);
}
assertFalse(InvalidationService.getIsClientStartedForTest());
@@ -469,7 +469,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
getService().onHandleIntent(startIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
- Intent stopIntent = new Intent().putExtra(IntentProtocol.EXTRA_STOP, true);
+ Intent stopIntent = new Intent().putExtra(InvalidationIntentProtocol.EXTRA_STOP, true);
getService().onHandleIntent(stopIntent);
assertFalse(InvalidationService.getIsClientStartedForTest());
@@ -523,7 +523,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
Set<ModelType> desiredRegistrations = CollectionUtil.newHashSet(
ModelType.BOOKMARK, ModelType.SESSION);
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
- Intent registrationIntent = IntentProtocol.createRegisterIntent(account, false,
+ Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false,
desiredRegistrations);
getService().onHandleIntent(registrationIntent);
@@ -539,7 +539,8 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Send another registration-change intent, this type with all-types set to true, and
// verify that the on-disk state is updated and that no addition Intents are issued.
- getService().onHandleIntent(IntentProtocol.createRegisterIntent(account, true, null));
+ getService().onHandleIntent(
+ InvalidationIntentProtocol.createRegisterIntent(account, true, null));
assertEquals(account, invPrefs.getSavedSyncedAccount());
assertEquals(CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE),
invPrefs.getSavedSyncedTypes());
@@ -549,7 +550,8 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// and verify that it both updates the account, stops thye existing client, and
// starts a new client.
Account account2 = AccountManagerHelper.createAccountFromName("test2@example.com");
- getService().onHandleIntent(IntentProtocol.createRegisterIntent(account2, true, null));
+ getService().onHandleIntent(
+ InvalidationIntentProtocol.createRegisterIntent(account2, true, null));
assertEquals(account2, invPrefs.getSavedSyncedAccount());
assertEquals(3, mStartServiceIntents.size());
assertTrue(isAndroidListenerStartIntent(mStartServiceIntents.get(0)));
@@ -611,15 +613,16 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Register for some object ids.
objectIds.add(ObjectId.newInstance(1, "obj1".getBytes()));
objectIds.add(ObjectId.newInstance(2, "obj2".getBytes()));
- Intent registrationIntent = IntentProtocol.createRegisterIntent(account, new int[] {1, 2},
- new String[] {"obj1", "obj2"});
+ Intent registrationIntent =
+ InvalidationIntentProtocol.createRegisterIntent(account, new int[] {1, 2},
+ new String[] {"obj1", "obj2"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, false /* isReady */));
// Register for some types.
types.add(ModelType.BOOKMARK);
types.add(ModelType.SESSION);
- registrationIntent = IntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, false /* isReady */));
@@ -629,33 +632,35 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Change object id registration with types registered.
objectIds.add(ObjectId.newInstance(3, "obj3".getBytes()));
- registrationIntent = IntentProtocol.createRegisterIntent(account, new int[] {1, 2, 3},
- new String[] {"obj1", "obj2", "obj3"});
+ registrationIntent =
+ InvalidationIntentProtocol.createRegisterIntent(account, new int[] {1, 2, 3},
+ new String[] {"obj1", "obj2", "obj3"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Change type registration with object ids registered.
types.remove(ModelType.BOOKMARK);
- registrationIntent = IntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Unregister all types.
types.clear();
- registrationIntent = IntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Change object id registration with no types registered.
objectIds.remove(ObjectId.newInstance(2, "obj2".getBytes()));
- registrationIntent = IntentProtocol.createRegisterIntent(account, new int[] {1, 3},
- new String[] {"obj1", "obj3"});
+ registrationIntent =
+ InvalidationIntentProtocol.createRegisterIntent(account, new int[] {1, 3},
+ new String[] {"obj1", "obj3"});
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
// Unregister all object ids.
objectIds.clear();
- registrationIntent = IntentProtocol.createRegisterIntent(account, new int[0],
+ registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, new int[0],
new String[0]);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
@@ -663,7 +668,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Change type registration with no object ids registered.
types.add(ModelType.BOOKMARK);
types.add(ModelType.PASSWORD);
- registrationIntent = IntentProtocol.createRegisterIntent(account, false, types);
+ registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false, types);
getService().onHandleIntent(registrationIntent);
assertTrue(expectedObjectIdsRegistered(types, objectIds, true /* isReady */));
}
@@ -676,7 +681,8 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Send register Intent.
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
- Intent registrationIntent = IntentProtocol.createRegisterIntent(account, true, null);
+ Intent registrationIntent =
+ InvalidationIntentProtocol.createRegisterIntent(account, true, null);
getService().onHandleIntent(registrationIntent);
// Verify client started and state written.
@@ -706,8 +712,8 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
// Send register Intent with no desired types.
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
- Intent registrationIntent =
- IntentProtocol.createRegisterIntent(account, false, new HashSet<ModelType>());
+ Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(
+ account, false, new HashSet<ModelType>());
getService().onHandleIntent(registrationIntent);
// Verify client started and state written.
@@ -723,7 +729,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
assertTrue(Arrays.equals(CLIENT_ID, InvalidationService.getClientIdForTest()));
// Choose to register for all types in an already ready client.
- registrationIntent = IntentProtocol.createRegisterIntent(account, true, null);
+ registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, true, null);
getService().onHandleIntent(registrationIntent);
// Ensure registrations are correct.
@@ -746,7 +752,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
Account account = AccountManagerHelper.createAccountFromName("test@example.com");
Set<ModelType> desiredRegistrations = CollectionUtil.newHashSet(
ModelType.BOOKMARK, ModelType.SESSION);
- Intent registrationIntent = IntentProtocol.createRegisterIntent(account, false,
+ Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false,
desiredRegistrations);
getService().onHandleIntent(registrationIntent);
@@ -776,7 +782,7 @@ public class InvalidationServiceTest extends ServiceTestCase<TestableInvalidatio
ModelType.BOOKMARK, ModelType.SESSION);
Set<ObjectId> desiredObjectIds = ModelType.modelTypesToObjectIds(desiredRegistrations);
- Intent registrationIntent = IntentProtocol.createRegisterIntent(account, false,
+ Intent registrationIntent = InvalidationIntentProtocol.createRegisterIntent(account, false,
desiredRegistrations);
getService().onHandleIntent(registrationIntent);
assertTrue(InvalidationService.getIsClientStartedForTest());
diff --git a/sync/android/javatests/src/org/chromium/sync/notifier/signin/SyncStatusHelperTest.java b/sync/android/javatests/src/org/chromium/sync/notifier/signin/SyncStatusHelperTest.java
index a2e83bc..746cd17 100644
--- a/sync/android/javatests/src/org/chromium/sync/notifier/signin/SyncStatusHelperTest.java
+++ b/sync/android/javatests/src/org/chromium/sync/notifier/signin/SyncStatusHelperTest.java
@@ -10,7 +10,6 @@ import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature;
-import org.chromium.sync.notifier.InvalidationController;
import org.chromium.sync.notifier.SyncStatusHelper;
import org.chromium.sync.signin.ChromeSigninController;
import org.chromium.sync.test.util.MockSyncContentResolverDelegate;