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