| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.invalidation; | 5 package org.chromium.chrome.browser.invalidation; |
| 6 | 6 |
| 7 import android.content.Intent; | 7 import android.content.Intent; |
| 8 import android.support.test.InstrumentationRegistry; | 8 import android.support.test.InstrumentationRegistry; |
| 9 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
| 10 import android.support.test.rule.UiThreadTestRule; | 10 import android.support.test.rule.UiThreadTestRule; |
| 11 | 11 |
| 12 import com.google.ipc.invalidation.external.client.types.ObjectId; | 12 import com.google.ipc.invalidation.external.client.types.ObjectId; |
| 13 | 13 |
| 14 import org.junit.Assert; | 14 import org.junit.Assert; |
| 15 import org.junit.Before; | 15 import org.junit.Before; |
| 16 import org.junit.Rule; | 16 import org.junit.Rule; |
| 17 import org.junit.Test; | 17 import org.junit.Test; |
| 18 import org.junit.runner.RunWith; | 18 import org.junit.runner.RunWith; |
| 19 | 19 |
| 20 import org.chromium.base.ContextUtils; | |
| 21 import org.chromium.base.test.BaseJUnit4ClassRunner; | 20 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 22 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
| 23 import org.chromium.chrome.test.invalidation.IntentSavingContext; | 22 import org.chromium.chrome.test.invalidation.IntentSavingContext; |
| 24 import org.chromium.components.invalidation.InvalidationService; | 23 import org.chromium.components.invalidation.InvalidationService; |
| 25 import org.chromium.components.sync.AndroidSyncSettings; | 24 import org.chromium.components.sync.AndroidSyncSettings; |
| 26 import org.chromium.components.sync.ModelType; | 25 import org.chromium.components.sync.ModelType; |
| 27 import org.chromium.components.sync.ModelTypeHelper; | 26 import org.chromium.components.sync.ModelTypeHelper; |
| 28 import org.chromium.components.sync.notifier.InvalidationIntentProtocol; | 27 import org.chromium.components.sync.notifier.InvalidationIntentProtocol; |
| 29 import org.chromium.components.sync.test.util.MockSyncContentResolverDelegate; | 28 import org.chromium.components.sync.test.util.MockSyncContentResolverDelegate; |
| 30 import org.chromium.content.browser.test.NativeLibraryTestRule; | 29 import org.chromium.content.browser.test.NativeLibraryTestRule; |
| 31 | 30 |
| 32 import java.util.Set; | 31 import java.util.Set; |
| 33 | 32 |
| 34 /** | 33 /** |
| 35 * Tests for {@link InvalidationService}. | 34 * Tests for {@link InvalidationService}. |
| 36 */ | 35 */ |
| 37 @RunWith(BaseJUnit4ClassRunner.class) | 36 @RunWith(BaseJUnit4ClassRunner.class) |
| 38 public class InvalidationServiceTest { | 37 public class InvalidationServiceTest { |
| 39 @Rule | 38 @Rule |
| 40 public NativeLibraryTestRule mActivityTestRule = new NativeLibraryTestRule()
; | 39 public NativeLibraryTestRule mActivityTestRule = new NativeLibraryTestRule()
; |
| 41 | 40 |
| 42 @Rule | 41 @Rule |
| 43 public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule(); | 42 public UiThreadTestRule mUiThreadTestRule = new UiThreadTestRule(); |
| 44 | 43 |
| 45 private IntentSavingContext mAppContext; | 44 private IntentSavingContext mContext; |
| 46 | 45 |
| 47 @Before | 46 @Before |
| 48 public void setUp() throws Exception { | 47 public void setUp() throws Exception { |
| 49 mActivityTestRule.loadNativeLibraryAndInitBrowserProcess(); | 48 mActivityTestRule.loadNativeLibraryAndInitBrowserProcess(); |
| 50 mAppContext = new IntentSavingContext(InstrumentationRegistry.getInstrum
entation() | 49 mContext = new IntentSavingContext( |
| 51 .getTargetContext() | 50 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
| 52 .getApplicationContext()); | |
| 53 // TODO(wnwen): Remove mAppContext and just use application context. | |
| 54 // We don't want to use the system content resolver, so we override it. | 51 // We don't want to use the system content resolver, so we override it. |
| 55 MockSyncContentResolverDelegate delegate = new MockSyncContentResolverDe
legate(); | 52 MockSyncContentResolverDelegate delegate = new MockSyncContentResolverDe
legate(); |
| 56 // Android master sync can safely always be on. | 53 // Android master sync can safely always be on. |
| 57 delegate.setMasterSyncAutomatically(true); | 54 delegate.setMasterSyncAutomatically(true); |
| 58 AndroidSyncSettings.overrideForTests(mAppContext, delegate); | 55 AndroidSyncSettings.overrideForTests(mContext, delegate); |
| 59 ContextUtils.initApplicationContextForTests(mAppContext); | |
| 60 } | 56 } |
| 61 | 57 |
| 62 @Test | 58 @Test |
| 63 @SmallTest | 59 @SmallTest |
| 64 @Feature({"Sync"}) | 60 @Feature({"Sync"}) |
| 65 public void testSetRegisteredObjectIds() throws Throwable { | 61 public void testSetRegisteredObjectIds() throws Throwable { |
| 66 mUiThreadTestRule.runOnUiThread(new Runnable() { | 62 mUiThreadTestRule.runOnUiThread(new Runnable() { |
| 67 @Override | 63 @Override |
| 68 public void run() { | 64 public void run() { |
| 69 InvalidationService service = InvalidationServiceFactory.getForT
est(); | 65 InvalidationService service = InvalidationServiceFactory.getForT
est(mContext); |
| 70 ObjectId bookmark = ModelTypeHelper.toObjectId(ModelType.BOOKMAR
KS); | 66 ObjectId bookmark = ModelTypeHelper.toObjectId(ModelType.BOOKMAR
KS); |
| 71 service.setRegisteredObjectIds(new int[] {1, 2, bookmark.getSour
ce()}, | 67 service.setRegisteredObjectIds(new int[] {1, 2, bookmark.getSour
ce()}, |
| 72 new String[] {"a", "b", new String(bookmark.getName())})
; | 68 new String[] {"a", "b", new String(bookmark.getName())})
; |
| 73 Assert.assertEquals(1, mAppContext.getNumStartedIntents()); | 69 Assert.assertEquals(1, mContext.getNumStartedIntents()); |
| 74 | 70 |
| 75 // Validate destination. | 71 // Validate destination. |
| 76 Intent intent = mAppContext.getStartedIntent(0); | 72 Intent intent = mContext.getStartedIntent(0); |
| 77 validateIntentComponent(intent); | 73 validateIntentComponent(intent); |
| 78 Assert.assertEquals(InvalidationIntentProtocol.ACTION_REGISTER,
intent.getAction()); | 74 Assert.assertEquals(InvalidationIntentProtocol.ACTION_REGISTER,
intent.getAction()); |
| 79 | 75 |
| 80 // Validate registered object ids. The bookmark object should no
t be registered | 76 // Validate registered object ids. The bookmark object should no
t be registered |
| 81 // since it is a Sync type. | 77 // since it is a Sync type. |
| 82 Assert.assertNull(intent.getStringArrayListExtra( | 78 Assert.assertNull(intent.getStringArrayListExtra( |
| 83 InvalidationIntentProtocol.EXTRA_REGISTERED_TYPES)); | 79 InvalidationIntentProtocol.EXTRA_REGISTERED_TYPES)); |
| 84 Set<ObjectId> objectIds = InvalidationIntentProtocol.getRegister
edObjectIds(intent); | 80 Set<ObjectId> objectIds = InvalidationIntentProtocol.getRegister
edObjectIds(intent); |
| 85 Assert.assertEquals(2, objectIds.size()); | 81 Assert.assertEquals(2, objectIds.size()); |
| 86 Assert.assertTrue(objectIds.contains(ObjectId.newInstance(1, "a"
.getBytes()))); | 82 Assert.assertTrue(objectIds.contains(ObjectId.newInstance(1, "a"
.getBytes()))); |
| 87 Assert.assertTrue(objectIds.contains(ObjectId.newInstance(2, "b"
.getBytes()))); | 83 Assert.assertTrue(objectIds.contains(ObjectId.newInstance(2, "b"
.getBytes()))); |
| 88 } | 84 } |
| 89 }); | 85 }); |
| 90 } | 86 } |
| 91 | 87 |
| 92 /** | 88 /** |
| 93 * Asserts that {@code intent} is destined for the correct component. | 89 * Asserts that {@code intent} is destined for the correct component. |
| 94 */ | 90 */ |
| 95 private static void validateIntentComponent(Intent intent) { | 91 private static void validateIntentComponent(Intent intent) { |
| 96 Assert.assertNotNull(intent.getComponent()); | 92 Assert.assertNotNull(intent.getComponent()); |
| 97 Assert.assertEquals(ChromeInvalidationClientService.class.getName(), | 93 Assert.assertEquals(ChromeInvalidationClientService.class.getName(), |
| 98 intent.getComponent().getClassName()); | 94 intent.getComponent().getClassName()); |
| 99 } | 95 } |
| 100 | 96 |
| 101 } | 97 } |
| OLD | NEW |