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.Ignore; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 import java.util.ArrayList; 44 import java.util.List; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class PhoneLanguagePreferenceControllerTest { 48 49 @Mock 50 private Preference mPreference; 51 @Mock 52 private AssetManager mAssets; 53 54 private Context mContext; 55 private FakeFeatureFactory mFeatureFactory; 56 private PhoneLanguagePreferenceController mController; 57 58 @Before setUp()59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 mContext = spy(RuntimeEnvironment.application); 62 when(mContext.getAssets()).thenReturn(mAssets); 63 mFeatureFactory = FakeFeatureFactory.setupForTest(); 64 mController = new PhoneLanguagePreferenceController(mContext, "key"); 65 } 66 67 @Test testIsAvailable_hasMultipleLocales_shouldReturnTrue()68 public void testIsAvailable_hasMultipleLocales_shouldReturnTrue() { 69 when(mAssets.getLocales()).thenReturn(new String[] {"en", "de"}); 70 71 assertThat(mController.isAvailable()).isTrue(); 72 } 73 74 @Test testIsAvailable_hasSingleLocales_shouldReturnFalse()75 public void testIsAvailable_hasSingleLocales_shouldReturnFalse() { 76 when(mAssets.getLocales()).thenReturn(new String[] {"en"}); 77 78 assertThat(mController.isAvailable()).isFalse(); 79 } 80 81 @Test testGetAvailabilityStatus_hasMultipleLocales_returnAvailable()82 public void testGetAvailabilityStatus_hasMultipleLocales_returnAvailable() { 83 when(mAssets.getLocales()).thenReturn(new String[] {"en", "de"}); 84 85 assertThat(mController.getAvailabilityStatus()) 86 .isEqualTo(BasePreferenceController.AVAILABLE); 87 } 88 89 @Test testGetAvailabilityStatus_hasSingleLocales_returnConditionallyUnavailable()90 public void testGetAvailabilityStatus_hasSingleLocales_returnConditionallyUnavailable() { 91 when(mAssets.getLocales()).thenReturn(new String[] {"en"}); 92 93 assertThat(mController.getAvailabilityStatus()) 94 .isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE); 95 } 96 97 @Ignore 98 @Test 99 @Config(qualifiers = "mcc999") testIsAvailable_ifDisabled_shouldReturnFalse()100 public void testIsAvailable_ifDisabled_shouldReturnFalse() { 101 assertThat(mController.isAvailable()).isFalse(); 102 } 103 104 @Test testUpdateState_shouldUpdateSummary()105 public void testUpdateState_shouldUpdateSummary() { 106 final String testSummary = "test"; 107 when(mFeatureFactory.localeFeatureProvider.getLocaleNames()).thenReturn(testSummary); 108 109 mController.updateState(mPreference); 110 111 verify(mPreference).setSummary(testSummary); 112 } 113 114 @Test testUpdateNonIndexable_shouldAddKey()115 public void testUpdateNonIndexable_shouldAddKey() { 116 final List<String> niks = new ArrayList<>(); 117 mController.updateNonIndexableKeys(niks); 118 assertThat(niks).containsExactly(mController.getPreferenceKey()); 119 } 120 } 121