1 /* 2 * Copyright (C) 2018 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 package com.android.settings.accounts; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.accounts.Account; 25 import android.content.Context; 26 import android.os.UserHandle; 27 28 import androidx.fragment.app.FragmentActivity; 29 import androidx.preference.PreferenceManager; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.testutils.shadow.ShadowContentResolver; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.annotation.Config; 41 import org.robolectric.util.ReflectionHelpers; 42 43 @RunWith(RobolectricTestRunner.class) 44 @Config(shadows = {ShadowContentResolver.class}) 45 public class AccountSyncSettingsTest { 46 private Context mContext; 47 private AccountSyncSettings mAccountSyncSettings; 48 49 @Before setUp()50 public void setUp() { 51 mContext = RuntimeEnvironment.application; 52 mAccountSyncSettings = spy(new TestAccountSyncSettings(mContext)); 53 } 54 55 @After tearDown()56 public void tearDown() { 57 ShadowContentResolver.reset(); 58 } 59 60 @Test onPreferenceTreeClick_nullAuthority_shouldNotCrash()61 public void onPreferenceTreeClick_nullAuthority_shouldNotCrash() { 62 when(mAccountSyncSettings.getActivity()).thenReturn(mock(FragmentActivity.class)); 63 final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext, 64 new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */); 65 preference.setOneTimeSyncMode(false); 66 ReflectionHelpers.setField(mAccountSyncSettings, "mUserHandle", UserHandle.CURRENT); 67 68 mAccountSyncSettings.onPreferenceTreeClick(preference); 69 // no crash 70 } 71 72 @Test enabledSyncNowMenu_noSyncStateSwitchPreference_returnFalse()73 public void enabledSyncNowMenu_noSyncStateSwitchPreference_returnFalse() { 74 assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isFalse(); 75 } 76 77 @Test enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOn_returnTrue()78 public void enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOn_returnTrue() { 79 final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext, 80 new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */); 81 preference.setChecked(true); 82 mAccountSyncSettings.getPreferenceScreen().addPreference(preference); 83 84 assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isTrue(); 85 } 86 87 @Test enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOff_returnFalse()88 public void enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOff_returnFalse() { 89 final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext, 90 new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */); 91 preference.setChecked(false); 92 mAccountSyncSettings.getPreferenceScreen().addPreference(preference); 93 94 assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isFalse(); 95 } 96 97 public static class TestAccountSyncSettings extends AccountSyncSettings { 98 private PreferenceScreen mScreen; 99 TestAccountSyncSettings(Context context)100 public TestAccountSyncSettings(Context context) { 101 final PreferenceManager preferenceManager = new PreferenceManager(context); 102 mScreen = preferenceManager.createPreferenceScreen(context); 103 } 104 105 @Override getPreferenceScreen()106 public PreferenceScreen getPreferenceScreen() { 107 return mScreen; 108 } 109 } 110 } 111