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