1 /* 2 * Copyright (C) 2016 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.language; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.res.AssetManager; 27 28 import androidx.preference.Preference; 29 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.testutils.FakeFeatureFactory; 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.RuntimeEnvironment; 40 import org.robolectric.annotation.Config; 41 42 import java.util.ArrayList; 43 import java.util.List; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class PhoneLanguagePreferenceControllerTest { 47 48 @Mock 49 private Preference mPreference; 50 @Mock 51 private AssetManager mAssets; 52 53 private Context mContext; 54 private FakeFeatureFactory mFeatureFactory; 55 private PhoneLanguagePreferenceController mController; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mContext = spy(RuntimeEnvironment.application); 61 when(mContext.getAssets()).thenReturn(mAssets); 62 mFeatureFactory = FakeFeatureFactory.setupForTest(); 63 mController = new PhoneLanguagePreferenceController(mContext, "key"); 64 } 65 66 @Test testIsAvailable_hasMultipleLocales_shouldReturnTrue()67 public void testIsAvailable_hasMultipleLocales_shouldReturnTrue() { 68 when(mAssets.getLocales()).thenReturn(new String[] {"en", "de"}); 69 70 assertThat(mController.isAvailable()).isTrue(); 71 } 72 73 @Test testIsAvailable_hasSingleLocales_shouldReturnFalse()74 public void testIsAvailable_hasSingleLocales_shouldReturnFalse() { 75 when(mAssets.getLocales()).thenReturn(new String[] {"en"}); 76 77 assertThat(mController.isAvailable()).isFalse(); 78 } 79 80 @Test testGetAvailabilityStatus_hasMultipleLocales_returnAvailable()81 public void testGetAvailabilityStatus_hasMultipleLocales_returnAvailable() { 82 when(mAssets.getLocales()).thenReturn(new String[] {"en", "de"}); 83 84 assertThat(mController.getAvailabilityStatus()) 85 .isEqualTo(BasePreferenceController.AVAILABLE); 86 } 87 88 @Test testGetAvailabilityStatus_hasSingleLocales_returnConditionallyUnavailable()89 public void testGetAvailabilityStatus_hasSingleLocales_returnConditionallyUnavailable() { 90 when(mAssets.getLocales()).thenReturn(new String[] {"en"}); 91 92 assertThat(mController.getAvailabilityStatus()) 93 .isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE); 94 } 95 96 @Test 97 @Config(qualifiers = "mcc999") testIsAvailable_ifDisabled_shouldReturnFalse()98 public void testIsAvailable_ifDisabled_shouldReturnFalse() { 99 assertThat(mController.isAvailable()).isFalse(); 100 } 101 102 @Test testUpdateState_shouldUpdateSummary()103 public void testUpdateState_shouldUpdateSummary() { 104 final String testSummary = "test"; 105 when(mFeatureFactory.localeFeatureProvider.getLocaleNames()).thenReturn(testSummary); 106 107 mController.updateState(mPreference); 108 109 verify(mPreference).setSummary(testSummary); 110 } 111 112 @Test testUpdateNonIndexable_shouldAddKey()113 public void testUpdateNonIndexable_shouldAddKey() { 114 final List<String> niks = new ArrayList<>(); 115 mController.updateNonIndexableKeys(niks); 116 assertThat(niks).containsExactly(mController.getPreferenceKey()); 117 } 118 } 119