Home | History | Annotate | Download | only in cts
      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 import android.support.test.InstrumentationRegistry;
     27 import android.support.test.runner.AndroidJUnit4;
     28 
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 
     33 /**
     34  * Device side code for {@link android.content.cts.InvalidSyncAuthoritiesHostTest}
     35  */
     36 @RunWith(AndroidJUnit4.class)
     37 public class InvalidSyncAuthoritiesDeviceTest {
     38 
     39     private static final String VALID_TEST_AUTHORITY = "android.content.sync.cts.authority";
     40     private static final String INVALID_TEST_AUTHORITY = "invalid.authority";
     41     private static final String VALID_TEST_ACCOUNT_TYPE = "android.content.sync.cts.accounttype";
     42 
     43     private Account mInvalidAccount;
     44     private Account mValidAccount;
     45     private AccountManager mAccountManager;
     46 
     47     @Before
     48     public void setUp() {
     49         final Context context = InstrumentationRegistry.getTargetContext();
     50         mAccountManager = context.getSystemService(AccountManager.class);
     51         mInvalidAccount = new Account("invalid_test_name", "invalid_test_type");
     52         final Account[] accounts = mAccountManager.getAccountsByType(VALID_TEST_ACCOUNT_TYPE);
     53         mValidAccount = (accounts.length == 0) ? createTestAccount() : accounts[0];
     54     }
     55 
     56     private Account createTestAccount() {
     57         mValidAccount = new Account("testAccount", VALID_TEST_ACCOUNT_TYPE);
     58         assertTrue("Failed to create a valid test account",
     59                 mAccountManager.addAccountExplicitly(mValidAccount, "password", null));
     60         return mValidAccount;
     61     }
     62 
     63     @Test
     64     public void populateAndTestSyncAutomaticallyBeforeReboot() {
     65         ContentResolver.setSyncAutomatically(mValidAccount, VALID_TEST_AUTHORITY, true);
     66         ContentResolver.setSyncAutomatically(mValidAccount, INVALID_TEST_AUTHORITY, true);
     67         ContentResolver.setSyncAutomatically(mInvalidAccount, INVALID_TEST_AUTHORITY, true);
     68         ContentResolver.setSyncAutomatically(mInvalidAccount, VALID_TEST_AUTHORITY, true);
     69 
     70         assertTrue(ContentResolver.getSyncAutomatically(mValidAccount, VALID_TEST_AUTHORITY));
     71         assertTrue(ContentResolver.getSyncAutomatically(mValidAccount, INVALID_TEST_AUTHORITY));
     72         // checking for invalid accounts may already return false depending on when the broadcast
     73         // LOGIN_ACCOUNTS_CHANGED_ACTION was received by SyncManager
     74     }
     75 
     76     @Test
     77     public void testSyncAutomaticallyAfterReboot() {
     78         assertTrue(ContentResolver.getSyncAutomatically(mValidAccount, VALID_TEST_AUTHORITY));
     79         assertFalse(ContentResolver.getSyncAutomatically(mValidAccount, INVALID_TEST_AUTHORITY));
     80         assertFalse(ContentResolver.getSyncAutomatically(mInvalidAccount, VALID_TEST_AUTHORITY));
     81         assertFalse(ContentResolver.getSyncAutomatically(mInvalidAccount, INVALID_TEST_AUTHORITY));
     82     }
     83 
     84     @Test
     85     public void removeTestAccount() {
     86         // To use as a teardown step from the hostside test
     87         mAccountManager.removeAccountExplicitly(mValidAccount);
     88     }
     89 }
     90