OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.sync; | 5 package org.chromium.chrome.browser.invalidation; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.app.Application; | 8 import android.app.Application; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.Intent; | 10 import android.content.Intent; |
11 import android.content.SyncResult; | 11 import android.content.SyncResult; |
12 import android.os.Bundle; | 12 import android.os.Bundle; |
13 import android.test.suitebuilder.annotation.MediumTest; | 13 import android.test.suitebuilder.annotation.MediumTest; |
14 | 14 |
15 import org.chromium.base.ApplicationStatus; | 15 import org.chromium.base.ApplicationStatus; |
16 import org.chromium.base.CommandLine; | 16 import org.chromium.base.CommandLine; |
17 import org.chromium.base.test.util.Feature; | 17 import org.chromium.base.test.util.Feature; |
18 import org.chromium.chrome.browser.ChromeActivity; | 18 import org.chromium.chrome.browser.ChromeActivity; |
19 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 19 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 20 import org.chromium.components.invalidation.PendingInvalidation; |
20 import org.chromium.content.browser.test.util.Criteria; | 21 import org.chromium.content.browser.test.util.Criteria; |
21 import org.chromium.content.browser.test.util.CriteriaHelper; | 22 import org.chromium.content.browser.test.util.CriteriaHelper; |
22 import org.chromium.sync.AndroidSyncSettings; | 23 import org.chromium.sync.AndroidSyncSettings; |
23 import org.chromium.sync.signin.AccountManagerHelper; | 24 import org.chromium.sync.signin.AccountManagerHelper; |
24 | 25 |
25 /** | 26 /** |
26 * Tests for ChromeBrowserSyncAdapter. | 27 * Tests for ChromeBrowserSyncAdapter. |
27 * | 28 * |
28 * TODO(nyquist) Remove this class when Chrome sync starts up the same way as th
e testshell. | 29 * TODO(nyquist) Remove this class when Chrome sync starts up the same way as th
e testshell. |
29 */ | 30 */ |
30 public class ChromeBrowserSyncAdapterTest extends ChromeActivityTestCaseBase<Chr
omeActivity> { | 31 public class ChromeBrowserSyncAdapterTest extends ChromeActivityTestCaseBase<Chr
omeActivity> { |
31 | |
32 private static final Account TEST_ACCOUNT = | 32 private static final Account TEST_ACCOUNT = |
33 AccountManagerHelper.createAccountFromName("test@gmail.com"); | 33 AccountManagerHelper.createAccountFromName("test@gmail.com"); |
34 private static final long WAIT_FOR_LAUNCHER_MS = 10 * 1000; | 34 private static final long WAIT_FOR_LAUNCHER_MS = 10 * 1000; |
35 private static final long POLL_INTERVAL_MS = 100; | 35 private static final long POLL_INTERVAL_MS = 100; |
36 | 36 |
37 private TestChromeSyncAdapter mSyncAdapter; | 37 private TestChromeSyncAdapter mSyncAdapter; |
38 | 38 |
39 public ChromeBrowserSyncAdapterTest() { | 39 public ChromeBrowserSyncAdapterTest() { |
40 super(ChromeActivity.class); | 40 super(ChromeActivity.class); |
41 } | 41 } |
42 | 42 |
43 private static void sendChromeToBackground(Context context) throws Interrupt
edException { | 43 private static void sendChromeToBackground(Context context) throws Interrupt
edException { |
44 Intent intent = new Intent(Intent.ACTION_MAIN); | 44 Intent intent = new Intent(Intent.ACTION_MAIN); |
45 intent.addCategory(Intent.CATEGORY_HOME); | 45 intent.addCategory(Intent.CATEGORY_HOME); |
46 context.startActivity(intent); | 46 context.startActivity(intent); |
47 | 47 |
48 assertTrue("Activity should have been sent to background", | 48 assertTrue("Activity should have been sent to background", |
49 CriteriaHelper.pollForCriteria(new Criteria() { | 49 CriteriaHelper.pollForCriteria(new Criteria() { |
50 @Override | 50 @Override |
51 public boolean isSatisfied() { | 51 public boolean isSatisfied() { |
52 return !ApplicationStatus.hasVisibleActivities(); | 52 return !ApplicationStatus.hasVisibleActivities(); |
53 } | 53 } |
54 }, WAIT_FOR_LAUNCHER_MS, POLL_INTERVAL_MS)); | 54 }, WAIT_FOR_LAUNCHER_MS, POLL_INTERVAL_MS)); |
55 } | 55 } |
56 | 56 |
| 57 private void performSyncWithBundle(Bundle bundle) { |
| 58 mSyncAdapter.onPerformSync(TEST_ACCOUNT, bundle, |
| 59 AndroidSyncSettings.getContractAuthority(getActivity()), null, n
ew SyncResult()); |
| 60 } |
57 | 61 |
58 private static class TestChromeSyncAdapter extends ChromiumSyncAdapter { | 62 private static class TestChromeSyncAdapter extends ChromiumSyncAdapter { |
59 private boolean mSyncRequested; | 63 private boolean mInvalidationRequested; |
60 private boolean mSyncRequestedForAllTypes; | 64 private boolean mInvalidationRequestedForAllTypes; |
61 private int mObjectSource; | 65 private int mObjectSource; |
62 private String mObjectId; | 66 private String mObjectId; |
63 private long mVersion; | 67 private long mVersion; |
64 private String mPayload; | 68 private String mPayload; |
65 | 69 |
66 public TestChromeSyncAdapter(Context context, Application application) { | 70 public TestChromeSyncAdapter(Context context, Application application) { |
67 super(context, application); | 71 super(context, application); |
68 } | 72 } |
69 | 73 |
70 @Override | 74 @Override |
71 protected boolean useAsyncStartup() { | 75 protected boolean useAsyncStartup() { |
72 return false; | 76 return false; |
73 } | 77 } |
74 | 78 |
75 @Override | 79 @Override |
76 public void requestSync(int objectSource, String objectId, long version,
String payload) { | 80 public void notifyInvalidation( |
| 81 int objectSource, String objectId, long version, String payload)
{ |
77 mObjectSource = objectSource; | 82 mObjectSource = objectSource; |
78 mObjectId = objectId; | 83 mObjectId = objectId; |
79 mVersion = version; | 84 mVersion = version; |
80 mPayload = payload; | 85 mPayload = payload; |
81 mSyncRequested = true; | 86 if (objectSource == 0) { |
82 } | 87 mInvalidationRequestedForAllTypes = true; |
83 | 88 } else { |
84 @Override | 89 mInvalidationRequested = true; |
85 public void requestSyncForAllTypes() { | 90 } |
86 mSyncRequestedForAllTypes = true; | |
87 } | 91 } |
88 } | 92 } |
89 | 93 |
90 @Override | 94 @Override |
91 protected void setUp() throws Exception { | 95 protected void setUp() throws Exception { |
92 super.setUp(); | 96 super.setUp(); |
93 mSyncAdapter = new TestChromeSyncAdapter(getInstrumentation().getTargetC
ontext(), | 97 mSyncAdapter = new TestChromeSyncAdapter( |
94 getActivity().getApplication()); | 98 getInstrumentation().getTargetContext(), getActivity().getApplic
ation()); |
95 } | 99 } |
96 | 100 |
97 @Override | 101 @Override |
98 public void startMainActivity() throws InterruptedException { | 102 public void startMainActivity() throws InterruptedException { |
99 startMainActivityOnBlankPage(); | 103 startMainActivityOnBlankPage(); |
100 } | 104 } |
101 | 105 |
102 @MediumTest | 106 @MediumTest |
103 @Feature({"Sync"}) | 107 @Feature({"Sync", "Invalidation"}) |
104 public void testRequestSyncNoInvalidationData() { | 108 public void testRequestInvalidationForAllTypes() { |
105 SyncResult syncResult = new SyncResult(); | 109 performSyncWithBundle(new Bundle()); |
106 mSyncAdapter.onPerformSync(TEST_ACCOUNT, new Bundle(), | 110 assertTrue(mSyncAdapter.mInvalidationRequestedForAllTypes); |
107 AndroidSyncSettings.getContractAuthority(getActivity()), null, s
yncResult); | 111 assertFalse(mSyncAdapter.mInvalidationRequested); |
108 assertTrue(mSyncAdapter.mSyncRequestedForAllTypes); | |
109 assertFalse(mSyncAdapter.mSyncRequested); | |
110 assertTrue(CommandLine.isInitialized()); | 112 assertTrue(CommandLine.isInitialized()); |
111 } | 113 } |
112 | 114 |
113 @MediumTest | 115 @MediumTest |
114 @Feature({"Sync"}) | 116 @Feature({"Sync", "Invalidation"}) |
115 public void testRequestSyncSpecificDataType() { | 117 public void testRequestSpecificInvalidation() { |
116 SyncResult syncResult = new SyncResult(); | 118 String objectId = "objectid_value"; |
117 Bundle extras = new Bundle(); | 119 int objectSource = 65; |
118 extras.putInt(ChromiumSyncAdapter.INVALIDATION_OBJECT_SOURCE_KEY, 65); | 120 long version = 42L; |
119 extras.putString(ChromiumSyncAdapter.INVALIDATION_OBJECT_ID_KEY, "object
id_value"); | 121 String payload = "payload_value"; |
120 extras.putLong(ChromiumSyncAdapter.INVALIDATION_VERSION_KEY, 42); | 122 |
121 extras.putString(ChromiumSyncAdapter.INVALIDATION_PAYLOAD_KEY, "payload_
value"); | 123 performSyncWithBundle( |
122 mSyncAdapter.onPerformSync(TEST_ACCOUNT, extras, | 124 PendingInvalidation.createBundle(objectId, objectSource, version
, payload)); |
123 AndroidSyncSettings.getContractAuthority(getActivity()), null, s
yncResult); | 125 |
124 assertFalse(mSyncAdapter.mSyncRequestedForAllTypes); | 126 assertFalse(mSyncAdapter.mInvalidationRequestedForAllTypes); |
125 assertTrue(mSyncAdapter.mSyncRequested); | 127 assertTrue(mSyncAdapter.mInvalidationRequested); |
126 assertEquals(65, mSyncAdapter.mObjectSource); | 128 assertEquals(objectSource, mSyncAdapter.mObjectSource); |
127 assertEquals("objectid_value", mSyncAdapter.mObjectId); | 129 assertEquals(objectId, mSyncAdapter.mObjectId); |
128 assertEquals(42, mSyncAdapter.mVersion); | 130 assertEquals(version, mSyncAdapter.mVersion); |
129 assertEquals("payload_value", mSyncAdapter.mPayload); | 131 assertEquals(payload, mSyncAdapter.mPayload); |
130 assertTrue(CommandLine.isInitialized()); | 132 assertTrue(CommandLine.isInitialized()); |
131 } | 133 } |
132 | 134 |
133 @MediumTest | 135 @MediumTest |
134 @Feature({"Sync"}) | 136 @Feature({"Sync", "Invalidation"}) |
135 public void testRequestSyncWhenChromeInBackground() throws InterruptedExcept
ion { | 137 public void testInvalidationsHeldWhenChromeInBackground() throws Interrupted
Exception { |
136 sendChromeToBackground(getActivity()); | 138 sendChromeToBackground(getActivity()); |
137 SyncResult syncResult = new SyncResult(); | 139 performSyncWithBundle(new Bundle()); |
138 mSyncAdapter.onPerformSync(TEST_ACCOUNT, new Bundle(), | 140 assertFalse(mSyncAdapter.mInvalidationRequestedForAllTypes); |
139 AndroidSyncSettings.getContractAuthority(getActivity()), null, s
yncResult); | 141 assertFalse(mSyncAdapter.mInvalidationRequested); |
140 assertFalse(mSyncAdapter.mSyncRequestedForAllTypes); | |
141 assertFalse(mSyncAdapter.mSyncRequested); | |
142 } | 142 } |
143 } | 143 } |
OLD | NEW |