1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.content.sync.cts; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import android.accounts.Account; 23 import android.accounts.AccountManager; 24 import android.content.ContentResolver; 25 import android.content.Context; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 /** 35 * Device side code for {@link android.content.cts.InvalidSyncAuthoritiesHostTest} 36 */ 37 @RunWith(AndroidJUnit4.class) 38 public class InvalidSyncAuthoritiesDeviceTest { 39 40 private static final String VALID_TEST_AUTHORITY = "android.content.sync.cts.authority"; 41 private static final String INVALID_TEST_AUTHORITY = "invalid.authority"; 42 private static final String VALID_TEST_ACCOUNT_TYPE = "android.content.sync.cts.accounttype"; 43 44 private Account mInvalidAccount; 45 private Account mValidAccount; 46 private AccountManager mAccountManager; 47 48 @Before 49 public void setUp() { 50 final Context context = InstrumentationRegistry.getTargetContext(); 51 mAccountManager = context.getSystemService(AccountManager.class); 52 mInvalidAccount = new Account("invalid_test_name", "invalid_test_type"); 53 final Account[] accounts = mAccountManager.getAccountsByType(VALID_TEST_ACCOUNT_TYPE); 54 mValidAccount = (accounts.length == 0) ? createTestAccount() : accounts[0]; 55 } 56 57 private Account createTestAccount() { 58 mValidAccount = new Account("testAccount", VALID_TEST_ACCOUNT_TYPE); 59 assertTrue("Failed to create a valid test account", 60 mAccountManager.addAccountExplicitly(mValidAccount, "password", null)); 61 return mValidAccount; 62 } 63 64 @Test 65 public void populateAndTestSyncAutomaticallyBeforeReboot() { 66 ContentResolver.setSyncAutomatically(mValidAccount, VALID_TEST_AUTHORITY, true); 67 ContentResolver.setSyncAutomatically(mValidAccount, INVALID_TEST_AUTHORITY, true); 68 ContentResolver.setSyncAutomatically(mInvalidAccount, INVALID_TEST_AUTHORITY, true); 69 ContentResolver.setSyncAutomatically(mInvalidAccount, VALID_TEST_AUTHORITY, true); 70 71 assertTrue(ContentResolver.getSyncAutomatically(mValidAccount, VALID_TEST_AUTHORITY)); 72 assertTrue(ContentResolver.getSyncAutomatically(mValidAccount, INVALID_TEST_AUTHORITY)); 73 // checking for invalid accounts may already return false depending on when the broadcast 74 // LOGIN_ACCOUNTS_CHANGED_ACTION was received by SyncManager 75 } 76 77 @Test 78 public void testSyncAutomaticallyAfterReboot() { 79 assertTrue(ContentResolver.getSyncAutomatically(mValidAccount, VALID_TEST_AUTHORITY)); 80 assertFalse(ContentResolver.getSyncAutomatically(mValidAccount, INVALID_TEST_AUTHORITY)); 81 assertFalse(ContentResolver.getSyncAutomatically(mInvalidAccount, VALID_TEST_AUTHORITY)); 82 assertFalse(ContentResolver.getSyncAutomatically(mInvalidAccount, INVALID_TEST_AUTHORITY)); 83 } 84 85 @Test 86 public void removeTestAccount() { 87 // To use as a teardown step from the hostside test 88 mAccountManager.removeAccountExplicitly(mValidAccount); 89 } 90 } 91