1 /* 2 * Copyright (C) 2022 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 com.android.settings.users; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 24 import android.content.Context; 25 26 import androidx.fragment.app.FragmentActivity; 27 import androidx.test.core.app.ApplicationProvider; 28 29 import com.android.settings.R; 30 import com.android.settings.testutils.shadow.ShadowFragment; 31 import com.android.settingslib.widget.CandidateInfo; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.annotation.Config; 40 41 import java.util.List; 42 43 @RunWith(RobolectricTestRunner.class) 44 @Config(shadows = {ShadowFragment.class}) 45 public class TimeoutToDockUserSettingsTest { 46 @Mock 47 private FragmentActivity mActivity; 48 49 private TimeoutToDockUserSettings mSettings; 50 51 private String[] mEntries; 52 private String[] mValues; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 58 final Context context = spy(ApplicationProvider.getApplicationContext()); 59 mEntries = context.getResources().getStringArray( 60 R.array.switch_to_dock_user_when_docked_timeout_entries); 61 mValues = context.getResources().getStringArray( 62 R.array.switch_to_dock_user_when_docked_timeout_values); 63 mSettings = spy(new TimeoutToDockUserSettings()); 64 65 doReturn(context).when(mSettings).getContext(); 66 doReturn(mActivity).when(mSettings).getActivity(); 67 68 mSettings.onAttach(context); 69 } 70 71 @Test getCandidates_returnsACandidateForEachEntry()72 public void getCandidates_returnsACandidateForEachEntry() { 73 final List<? extends CandidateInfo> candidates = mSettings.getCandidates(); 74 75 for (int i = 0; i < mEntries.length; i++) { 76 assertThat(candidates.get(i).loadLabel().toString()).isEqualTo(mEntries[i]); 77 } 78 } 79 80 @Test defaultKey_settingNotSet_shouldReturnSecondValueAsDefault()81 public void defaultKey_settingNotSet_shouldReturnSecondValueAsDefault() { 82 assertThat(mSettings.getDefaultKey()).isEqualTo( 83 mValues[TimeoutToDockUserSettings.DEFAULT_TIMEOUT_SETTING_VALUE_INDEX]); 84 } 85 86 @Test defaultKey_setToFirstValue_shouldSaveToSettings()87 public void defaultKey_setToFirstValue_shouldSaveToSettings() { 88 final String expectedKey = mValues[0]; 89 mSettings.setDefaultKey(expectedKey); 90 assertThat(mSettings.getDefaultKey()).isEqualTo(expectedKey); 91 } 92 93 @Test defaultKey_setToSecondValue_shouldSaveToSettings()94 public void defaultKey_setToSecondValue_shouldSaveToSettings() { 95 final String expectedKey = mValues[1]; 96 mSettings.setDefaultKey(expectedKey); 97 assertThat(mSettings.getDefaultKey()).isEqualTo(expectedKey); 98 } 99 100 @Test defaultKey_setToThirdValue_shouldSaveToSettings()101 public void defaultKey_setToThirdValue_shouldSaveToSettings() { 102 final String expectedKey = mValues[2]; 103 mSettings.setDefaultKey(expectedKey); 104 assertThat(mSettings.getDefaultKey()).isEqualTo(expectedKey); 105 } 106 } 107