OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.sync; | |
6 | |
7 import android.accounts.Account; | |
8 import android.app.Application; | |
9 import android.content.ContentResolver; | |
10 import android.content.Context; | |
11 import android.content.SyncResult; | |
12 import android.os.Bundle; | |
13 import android.test.suitebuilder.annotation.MediumTest; | |
14 | |
15 import com.google.protos.ipc.invalidation.Types; | |
16 | |
17 import org.chromium.base.CommandLine; | |
18 import org.chromium.base.test.util.Feature; | |
19 import org.chromium.chrome.shell.ChromeShellTestBase; | |
20 import org.chromium.sync.AndroidSyncSettings; | |
21 import org.chromium.sync.signin.AccountManagerHelper; | |
22 | |
23 /** | |
24 * Tests for ChromiumSyncAdapter. | |
25 */ | |
26 public class ChromiumSyncAdapterTest extends ChromeShellTestBase { | |
27 | |
28 private static final Account TEST_ACCOUNT = | |
29 AccountManagerHelper.createAccountFromName("test@gmail.com"); | |
30 | |
31 private TestChromiumSyncAdapter mSyncAdapter; | |
32 | |
33 private static class TestChromiumSyncAdapter extends ChromiumSyncAdapter { | |
34 private boolean mSyncRequested; | |
35 private boolean mSyncRequestedForAllTypes; | |
36 private int mObjectSource; | |
37 private String mObjectId; | |
38 private long mVersion; | |
39 private String mPayload; | |
40 | |
41 public TestChromiumSyncAdapter(Context context, Application application)
{ | |
42 super(context, application); | |
43 } | |
44 | |
45 @Override | |
46 protected boolean useAsyncStartup() { | |
47 return true; | |
48 } | |
49 | |
50 @Override | |
51 public void requestSync(int objectSource, String objectId, long version,
String payload) { | |
52 mObjectSource = objectSource; | |
53 mObjectId = objectId; | |
54 mVersion = version; | |
55 mPayload = payload; | |
56 mSyncRequested = true; | |
57 } | |
58 | |
59 @Override | |
60 public void requestSyncForAllTypes() { | |
61 mSyncRequestedForAllTypes = true; | |
62 } | |
63 } | |
64 | |
65 @Override | |
66 protected void setUp() throws Exception { | |
67 super.setUp(); | |
68 launchChromeShellWithBlankPage(); | |
69 mSyncAdapter = new TestChromiumSyncAdapter(getInstrumentation().getTarge
tContext(), | |
70 getActivity().getApplication()); | |
71 } | |
72 | |
73 public void performSyncWithBundle(Bundle bundle) { | |
74 mSyncAdapter.onPerformSync(TEST_ACCOUNT, bundle, | |
75 AndroidSyncSettings.get(getActivity()).getContractAuthority(), | |
76 null, new SyncResult()); | |
77 } | |
78 | |
79 @MediumTest | |
80 @Feature({"Sync"}) | |
81 public void testRequestSyncNoInvalidationData() { | |
82 performSyncWithBundle(new Bundle()); | |
83 assertTrue(mSyncAdapter.mSyncRequestedForAllTypes); | |
84 assertFalse(mSyncAdapter.mSyncRequested); | |
85 assertTrue(CommandLine.isInitialized()); | |
86 } | |
87 | |
88 private void testRequestSyncSpecificDataType(boolean withObjectSource) { | |
89 Bundle extras = new Bundle(); | |
90 if (withObjectSource) { | |
91 extras.putInt(ChromiumSyncAdapter.INVALIDATION_OBJECT_SOURCE_KEY, 61
); | |
92 } | |
93 extras.putString(ChromiumSyncAdapter.INVALIDATION_OBJECT_ID_KEY, "object
id_value"); | |
94 extras.putLong(ChromiumSyncAdapter.INVALIDATION_VERSION_KEY, 42); | |
95 extras.putString(ChromiumSyncAdapter.INVALIDATION_PAYLOAD_KEY, "payload_
value"); | |
96 | |
97 performSyncWithBundle(extras); | |
98 | |
99 assertFalse(mSyncAdapter.mSyncRequestedForAllTypes); | |
100 assertTrue(mSyncAdapter.mSyncRequested); | |
101 if (withObjectSource) { | |
102 assertEquals(61, mSyncAdapter.mObjectSource); | |
103 } else { | |
104 assertEquals(Types.ObjectSource.CHROME_SYNC, mSyncAdapter.mObjectSou
rce); | |
105 } | |
106 assertEquals("objectid_value", mSyncAdapter.mObjectId); | |
107 assertEquals(42, mSyncAdapter.mVersion); | |
108 assertEquals("payload_value", mSyncAdapter.mPayload); | |
109 assertTrue(CommandLine.isInitialized()); | |
110 } | |
111 | |
112 @MediumTest | |
113 @Feature({"Sync"}) | |
114 public void testRequestSyncSpecificDataType() { | |
115 testRequestSyncSpecificDataType(true /* withObjectSource */); | |
116 } | |
117 | |
118 @MediumTest | |
119 @Feature({"Sync"}) | |
120 public void testRequestSyncSpecificDataType_withoutObjectSource() { | |
121 testRequestSyncSpecificDataType(false /* withObjectSource */); | |
122 } | |
123 | |
124 @MediumTest | |
125 @Feature({"Sync"}) | |
126 public void testRequestSyncWhenChromeInBackground() throws InterruptedExcept
ion { | |
127 DelayedSyncControllerTest.sendChromeToBackground(getActivity()); | |
128 performSyncWithBundle(new Bundle()); | |
129 assertFalse(mSyncAdapter.mSyncRequestedForAllTypes); | |
130 assertFalse(mSyncAdapter.mSyncRequested); | |
131 assertTrue(CommandLine.isInitialized()); | |
132 } | |
133 | |
134 @MediumTest | |
135 @Feature({"Sync"}) | |
136 public void testRequestInitializeSync() throws InterruptedException { | |
137 Bundle extras = new Bundle(); | |
138 extras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true); | |
139 performSyncWithBundle(extras); | |
140 assertFalse(mSyncAdapter.mSyncRequestedForAllTypes); | |
141 assertFalse(mSyncAdapter.mSyncRequested); | |
142 } | |
143 } | |
OLD | NEW |