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 = {
45         ShadowContentResolver.class,
46         com.android.settings.testutils.shadow.ShadowFragment.class,
47 })
48 public class AccountSyncSettingsTest {
49     private Context mContext;
50     private AccountSyncSettings mAccountSyncSettings;
51 
52     @Before
setUp()53     public void setUp() {
54         mContext = RuntimeEnvironment.application;
55         mAccountSyncSettings = spy(new TestAccountSyncSettings(mContext));
56     }
57 
58     @After
tearDown()59     public void tearDown() {
60         ShadowContentResolver.reset();
61     }
62 
63     @Test
onPreferenceTreeClick_nullAuthority_shouldNotCrash()64     public void onPreferenceTreeClick_nullAuthority_shouldNotCrash() {
65         when(mAccountSyncSettings.getActivity()).thenReturn(mock(FragmentActivity.class));
66         final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext,
67                 new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */);
68         preference.setOneTimeSyncMode(false);
69         ReflectionHelpers.setField(mAccountSyncSettings, "mUserHandle", UserHandle.CURRENT);
70 
71         mAccountSyncSettings.onPreferenceTreeClick(preference);
72         // no crash
73     }
74 
75     @Test
enabledSyncNowMenu_noSyncStateSwitchPreference_returnFalse()76     public void enabledSyncNowMenu_noSyncStateSwitchPreference_returnFalse() {
77         assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isFalse();
78     }
79 
80     @Test
enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOn_returnTrue()81     public void enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOn_returnTrue() {
82         final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext,
83                 new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */);
84         preference.setChecked(true);
85         mAccountSyncSettings.getPreferenceScreen().addPreference(preference);
86 
87         assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isTrue();
88     }
89 
90     @Test
enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOff_returnFalse()91     public void enabledSyncNowMenu_addSyncStateSwitchPreferenceAndSwitchOff_returnFalse() {
92         final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(mContext,
93                 new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */);
94         preference.setChecked(false);
95         mAccountSyncSettings.getPreferenceScreen().addPreference(preference);
96 
97         assertThat(mAccountSyncSettings.enabledSyncNowMenu()).isFalse();
98     }
99 
100     public static class TestAccountSyncSettings extends AccountSyncSettings {
101         private PreferenceScreen mScreen;
102 
TestAccountSyncSettings(Context context)103         public TestAccountSyncSettings(Context context) {
104             final PreferenceManager preferenceManager = new PreferenceManager(context);
105             mScreen = preferenceManager.createPreferenceScreen(context);
106         }
107 
108         @Override
getPreferenceScreen()109         public PreferenceScreen getPreferenceScreen() {
110             return mScreen;
111         }
112     }
113 }
114