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 org.mockito.Mockito.mock; 19 import static org.mockito.Mockito.spy; 20 import static org.mockito.Mockito.when; 21 22 import android.accounts.Account; 23 import android.content.Context; 24 import android.os.UserHandle; 25 26 import androidx.fragment.app.FragmentActivity; 27 28 import com.android.settings.testutils.shadow.ShadowContentResolver; 29 30 import org.junit.After; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 import org.robolectric.util.ReflectionHelpers; 37 38 @RunWith(RobolectricTestRunner.class) 39 @Config(shadows = {ShadowContentResolver.class}) 40 public class AccountSyncSettingsTest { 41 42 @After tearDown()43 public void tearDown() { 44 ShadowContentResolver.reset(); 45 } 46 47 @Test onPreferenceTreeClick_nullAuthority_shouldNotCrash()48 public void onPreferenceTreeClick_nullAuthority_shouldNotCrash() { 49 final Context context = RuntimeEnvironment.application; 50 final AccountSyncSettings settings = spy(new AccountSyncSettings()); 51 when(settings.getActivity()).thenReturn(mock(FragmentActivity.class)); 52 final SyncStateSwitchPreference preference = new SyncStateSwitchPreference(context, 53 new Account("acct1", "type1"), "" /* authority */, "testPackage", 1 /* uid */); 54 preference.setOneTimeSyncMode(false); 55 ReflectionHelpers.setField(settings, "mUserHandle", UserHandle.CURRENT); 56 57 settings.onPreferenceTreeClick(preference); 58 // no crash 59 } 60 } 61