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