Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationServiceTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationServiceTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationServiceTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9d58595231d15c984445257fe573eee8713be4ed |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/invalidation/InvalidationServiceTest.java |
| @@ -0,0 +1,111 @@ |
| +// 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.chrome.browser.invalidation; |
| + |
| +import android.content.ComponentName; |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.content.pm.PackageManager; |
| +import android.test.UiThreadTest; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import com.google.ipc.invalidation.external.client.types.ObjectId; |
| + |
| +import org.chromium.base.test.util.AdvancedMockContext; |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.chrome.shell.ChromeShellTestBase; |
| +import org.chromium.components.invalidation.InvalidationClientService; |
| +import org.chromium.components.invalidation.InvalidationService; |
| +import org.chromium.sync.internal_api.pub.base.ModelType; |
| +import org.chromium.sync.notifier.InvalidationIntentProtocol; |
| +import org.chromium.sync.notifier.SyncStatusHelper; |
| +import org.chromium.sync.test.util.MockSyncContentResolverDelegate; |
| + |
| +import java.util.ArrayList; |
| +import java.util.List; |
| +import java.util.Set; |
| + |
| +/** |
| + * Tests for {@link InvalidationService}. |
| + */ |
| +public class InvalidationServiceTest extends ChromeShellTestBase { |
| + private IntentSavingContext mContext; |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + startChromeBrowserProcessSync(getInstrumentation().getTargetContext()); |
| + mContext = new IntentSavingContext(getInstrumentation().getTargetContext()); |
| + // 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 |
| + @UiThreadTest |
| + @Feature({"Sync"}) |
| + public void testSetRegisteredObjectIds() { |
| + InvalidationService service = InvalidationServiceFactory.getForTest(mContext); |
| + ObjectId bookmark = ModelType.BOOKMARK.toObjectId(); |
| + service.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(InvalidationIntentProtocol.ACTION_REGISTER, intent.getAction()); |
| + |
| + // Validate registered object ids. The bookmark object should not be registered since it is |
| + // a Sync type. |
| + 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()))); |
| + } |
| + |
| + /** |
| + * Asserts that {@code intent} is destined for the correct component. |
| + */ |
| + private static void validateIntentComponent(Intent intent) { |
| + assertNotNull(intent.getComponent()); |
| + assertEquals(InvalidationClientService.class.getName(), |
| + intent.getComponent().getClassName()); |
| + } |
| + |
| + /** |
| + * Mock context that saves all intents given to {@code startService}. |
| + */ |
| + private static class IntentSavingContext extends AdvancedMockContext { |
|
nyquist
2014/09/04 09:01:55
Could this be extracted to a new class and re-used
maxbogue
2014/09/05 16:42:45
Done.
|
| + private final List<Intent> mStartedIntents = new ArrayList<Intent>(); |
| + |
| + IntentSavingContext(Context targetContext) { |
| + super(targetContext); |
| + } |
| + |
| + @Override |
| + public ComponentName startService(Intent intent) { |
| + mStartedIntents.add(intent); |
| + return new ComponentName(this, getClass()); |
| + } |
| + |
| + int getNumStartedIntents() { |
| + return mStartedIntents.size(); |
| + } |
| + |
| + Intent getStartedIntent(int idx) { |
| + return mStartedIntents.get(idx); |
| + } |
| + |
| + @Override |
| + public PackageManager getPackageManager() { |
| + return getBaseContext().getPackageManager(); |
| + } |
| + } |
| + |
| +} |