| 1 | // Copyright (c) 2012 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.sync.notifier.signin; |
| 6 | |
| 7 | import android.accounts.Account; |
| 8 | import android.test.InstrumentationTestCase; |
| 9 | import android.test.suitebuilder.annotation.SmallTest; |
| 10 | |
| 11 | import org.chromium.base.ThreadUtils; |
| 12 | import org.chromium.base.test.util.Feature; |
| 13 | import org.chromium.sync.notifier.InvalidationController; |
| 14 | import org.chromium.sync.notifier.SyncStatusHelper; |
| 15 | import org.chromium.sync.signin.ChromeSigninController; |
| 16 | import org.chromium.sync.test.util.MockSyncContentResolverDelegate; |
| 17 | |
| 18 | public class SyncStatusHelperTest extends InstrumentationTestCase { |
| 19 | |
| 20 | private static class CountingMockSyncContentResolverDelegate |
| 21 | extends MockSyncContentResolverDelegate { |
| 22 | private int mGetMasterSyncAutomaticallyCalls; |
| 23 | private int mGetSyncAutomaticallyCalls; |
| 24 | private int mGetIsSyncableCalls; |
| 25 | |
| 26 | @Override |
| 27 | public boolean getMasterSyncAutomatically() { |
| 28 | mGetMasterSyncAutomaticallyCalls++; |
| 29 | return super.getMasterSyncAutomatically(); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public boolean getSyncAutomatically(Account account, String authority) { |
| 34 | mGetSyncAutomaticallyCalls++; |
| 35 | return super.getSyncAutomatically(account, authority); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public int getIsSyncable(Account account, String authority) { |
| 40 | mGetIsSyncableCalls++; |
| 41 | return super.getIsSyncable(account, authority); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private SyncStatusHelper mHelper; |
| 46 | |
| 47 | private CountingMockSyncContentResolverDelegate mSyncContentResolverDelegate; |
| 48 | |
| 49 | private String mAuthority; |
| 50 | |
| 51 | private Account mTestAccount; |
| 52 | |
| 53 | private Account mAlternateTestAccount; |
| 54 | |
| 55 | @Override |
| 56 | protected void setUp() throws Exception { |
| 57 | mSyncContentResolverDelegate = new CountingMockSyncContentResolverDelegate(); |
| 58 | SyncStatusHelper.overrideSyncStatusHelperForTests( |
| 59 | getInstrumentation().getTargetContext(), mSyncContentResolverDelegate); |
| 60 | mHelper = SyncStatusHelper.get(getInstrumentation().getTargetContext()); |
| 61 | // Need to set the signed in account name to ensure that sync settings notifications |
| 62 | // update the right account. |
| 63 | ChromeSigninController.get( |
| 64 | getInstrumentation().getTargetContext()).setSignedInAccountName( |
| 65 | "account@example.com"); |
| 66 | mAuthority = SyncStatusHelper.get(getInstrumentation().getTargetContext()) |
| 67 | .getContractAuthority(); |
| 68 | mTestAccount = new Account("account@example.com", "com.google"); |
| 69 | mAlternateTestAccount = new Account("alternateAccount@example.com", "com.google"); |
| 70 | super.setUp(); |
| 71 | } |
| 72 | |
| 73 | @SmallTest |
| 74 | @Feature({"Sync"}) |
| 75 | public void testToggleMasterSyncAutomaticallyFromSettings() throws InterruptedException { |
| 76 | mSyncContentResolverDelegate.setMasterSyncAutomatically(true); |
| 77 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 78 | assertTrue("master sync should be set", mHelper.isMasterSyncAutomaticallyEnabled()); |
| 79 | |
| 80 | mSyncContentResolverDelegate.setMasterSyncAutomatically(false); |
| 81 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 82 | assertFalse("master sync should be unset", mHelper.isMasterSyncAutomaticallyEnabled()); |
| 83 | } |
| 84 | |
| 85 | @SmallTest |
| 86 | @Feature({"Sync"}) |
| 87 | public void testToggleAccountSyncFromSettings() throws InterruptedException { |
| 88 | // Turn on syncability. |
| 89 | mSyncContentResolverDelegate.setMasterSyncAutomatically(true); |
| 90 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 91 | |
| 92 | // First sync |
| 93 | mSyncContentResolverDelegate.setIsSyncable(mTestAccount, mAuthority, 1); |
| 94 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 95 | mSyncContentResolverDelegate.setSyncAutomatically(mTestAccount, mAuthority, true); |
| 96 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 97 | assertTrue("sync should be set", mHelper.isSyncEnabled(mTestAccount)); |
| 98 | assertTrue("sync should be set for chrome app", |
| 99 | mHelper.isSyncEnabledForChrome(mTestAccount)); |
| 100 | |
| 101 | // Disable sync automatically for the app |
| 102 | mSyncContentResolverDelegate.setSyncAutomatically(mTestAccount, mAuthority, false); |
| 103 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 104 | assertFalse("sync should be unset", mHelper.isSyncEnabled(mTestAccount)); |
| 105 | assertFalse("sync should be unset for chrome app", |
| 106 | mHelper.isSyncEnabledForChrome(mTestAccount)); |
| 107 | |
| 108 | // Re-enable sync |
| 109 | mSyncContentResolverDelegate.setSyncAutomatically(mTestAccount, mAuthority, true); |
| 110 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 111 | assertTrue("sync should be re-enabled", mHelper.isSyncEnabled(mTestAccount)); |
| 112 | assertTrue("sync should be unset for chrome app", |
| 113 | mHelper.isSyncEnabledForChrome(mTestAccount)); |
| 114 | |
| 115 | // Disabled from master sync |
| 116 | mSyncContentResolverDelegate.setMasterSyncAutomatically(false); |
| 117 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 118 | assertFalse("sync should be disabled due to master sync", |
| 119 | mHelper.isSyncEnabled(mTestAccount)); |
| 120 | assertTrue("sync should be set for chrome app", |
| 121 | mHelper.isSyncEnabledForChrome(mTestAccount)); |
| 122 | } |
| 123 | |
| 124 | @SmallTest |
| 125 | @Feature({"Sync"}) |
| 126 | public void testToggleAccountSyncFromApplication() throws InterruptedException { |
| 127 | // Turn on syncability. |
| 128 | mSyncContentResolverDelegate.setMasterSyncAutomatically(true); |
| 129 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 130 | |
| 131 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 132 | @Override |
| 133 | public void run() { |
| 134 | mHelper.enableAndroidSync(mTestAccount); |
| 135 | } |
| 136 | }); |
| 137 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 138 | assertTrue("account should be synced", mHelper.isSyncEnabled(mTestAccount)); |
| 139 | |
| 140 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 141 | @Override |
| 142 | public void run() { |
| 143 | mHelper.disableAndroidSync(mTestAccount); |
| 144 | } |
| 145 | }); |
| 146 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 147 | assertFalse("account should not be synced", mHelper.isSyncEnabled(mTestAccount)); |
| 148 | } |
| 149 | |
| 150 | @SmallTest |
| 151 | @Feature({"Sync"}) |
| 152 | public void testToggleSyncabilityForMultipleAccounts() throws InterruptedException { |
| 153 | // Turn on syncability. |
| 154 | mSyncContentResolverDelegate.setMasterSyncAutomatically(true); |
| 155 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 156 | |
| 157 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 158 | @Override |
| 159 | public void run() { |
| 160 | mHelper.enableAndroidSync(mTestAccount); |
| 161 | } |
| 162 | }); |
| 163 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 164 | assertTrue("account should be synced", mHelper.isSyncEnabled(mTestAccount)); |
| 165 | |
| 166 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 167 | @Override |
| 168 | public void run() { |
| 169 | mHelper.enableAndroidSync(mAlternateTestAccount); |
| 170 | } |
| 171 | }); |
| 172 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 173 | assertTrue("alternate account should be synced", |
| 174 | mHelper.isSyncEnabled(mAlternateTestAccount)); |
| 175 | |
| 176 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 177 | @Override |
| 178 | public void run() { |
| 179 | mHelper.disableAndroidSync(mAlternateTestAccount); |
| 180 | } |
| 181 | }); |
| 182 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 183 | assertFalse("alternate account should not be synced", |
| 184 | mHelper.isSyncEnabled(mAlternateTestAccount)); |
| 185 | assertTrue("account should still be synced", mHelper.isSyncEnabled(mTestAccount)); |
| 186 | |
| 187 | // Ensure we don't erroneously re-use cached data. |
| 188 | assertFalse("null account should not be synced", mHelper.isSyncEnabled(null)); |
| 189 | } |
| 190 | |
| 191 | @SmallTest |
| 192 | @Feature({"Sync"}) |
| 193 | public void testSyncSettingsCaching() throws InterruptedException { |
| 194 | // Turn on syncability. |
| 195 | mSyncContentResolverDelegate.setMasterSyncAutomatically(true); |
| 196 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 197 | |
| 198 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 199 | @Override |
| 200 | public void run() { |
| 201 | mHelper.enableAndroidSync(mTestAccount); |
| 202 | } |
| 203 | }); |
| 204 | mSyncContentResolverDelegate.waitForLastNotificationCompleted(); |
| 205 | assertTrue("account should be synced", mHelper.isSyncEnabled(mTestAccount)); |
| 206 | |
| 207 | int masterSyncAutomaticallyCalls = |
| 208 | mSyncContentResolverDelegate.mGetMasterSyncAutomaticallyCalls; |
| 209 | int isSyncableCalls = mSyncContentResolverDelegate.mGetIsSyncableCalls; |
| 210 | int getSyncAutomaticallyAcalls = mSyncContentResolverDelegate.mGetSyncAutomaticallyCalls; |
| 211 | |
| 212 | // Do a bunch of reads. |
| 213 | mHelper.isMasterSyncAutomaticallyEnabled(); |
| 214 | mHelper.isSyncEnabled(); |
| 215 | mHelper.isSyncEnabled(mTestAccount); |
| 216 | mHelper.isSyncEnabledForChrome(mTestAccount); |
| 217 | |
| 218 | // Ensure values were read from cache. |
| 219 | assertEquals(masterSyncAutomaticallyCalls, |
| 220 | mSyncContentResolverDelegate.mGetMasterSyncAutomaticallyCalls); |
| 221 | assertEquals(isSyncableCalls, mSyncContentResolverDelegate.mGetIsSyncableCalls); |
| 222 | assertEquals(getSyncAutomaticallyAcalls, |
| 223 | mSyncContentResolverDelegate.mGetSyncAutomaticallyCalls); |
| 224 | |
| 225 | // Do a bunch of reads for alternate account. |
| 226 | mHelper.isMasterSyncAutomaticallyEnabled(); |
| 227 | mHelper.isSyncEnabled(mAlternateTestAccount); |
| 228 | mHelper.isSyncEnabledForChrome(mAlternateTestAccount); |
| 229 | |
| 230 | // Ensure master sync was cached but others are fetched once. |
| 231 | assertEquals(masterSyncAutomaticallyCalls, |
| 232 | mSyncContentResolverDelegate.mGetMasterSyncAutomaticallyCalls); |
| 233 | assertEquals(isSyncableCalls + 1, mSyncContentResolverDelegate.mGetIsSyncableCalls); |
| 234 | assertEquals(getSyncAutomaticallyAcalls + 1, |
| 235 | mSyncContentResolverDelegate.mGetSyncAutomaticallyCalls); |
| 236 | } |
| 237 | |
| 238 | @SmallTest |
| 239 | @Feature({"Sync"}) |
| 240 | public void testGetContractAuthority() throws Exception { |
| 241 | assertEquals("The contract authority should be the package name.", |
| 242 | getInstrumentation().getTargetContext().getPackageName(), |
| 243 | mHelper.getContractAuthority()); |
| 244 | } |
| 245 | } |