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