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 android.content.Context; 20 import android.speech.tts.TextToSpeech; 21 import android.speech.tts.TtsEngines; 22 import android.support.v7.preference.Preference; 23 import android.support.v7.preference.PreferenceScreen; 24 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 import com.android.settings.TestConfig; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Answers; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.annotation.Config; 35 36 import java.util.ArrayList; 37 import java.util.List; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Matchers.any; 41 import static org.mockito.Mockito.mock; 42 import static org.mockito.Mockito.verify; 43 import static org.mockito.Mockito.when; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47 public class TtsPreferenceControllerTest { 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private Context mContext; 51 @Mock 52 private TtsEngines mTtsEngines; 53 @Mock 54 private PreferenceScreen mScreen; 55 56 private TtsPreferenceController mController; 57 58 @Before 59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 62 mController = new TtsPreferenceController(mContext, mTtsEngines); 63 } 64 65 @Test 66 public void testIsAvailable_ttsEngineEmpty_shouldReturnFalse() { 67 68 // Not available when there is no engine. 69 when(mTtsEngines.getEngines()).thenReturn(new ArrayList<>()); 70 71 assertThat(mController.isAvailable()).isFalse(); 72 } 73 74 @Test 75 public void testIsAvailable_ttsEngineInstalled_shouldReturnTrue() { 76 final List<TextToSpeech.EngineInfo> infolist = new ArrayList<>(); 77 infolist.add(mock(TextToSpeech.EngineInfo.class)); 78 when(mTtsEngines.getEngines()).thenReturn(infolist); 79 80 assertThat(mController.isAvailable()).isTrue(); 81 } 82 83 @Test 84 public void displayPreference_notAvailable_shouldRemoveCategory() { 85 final Preference preference = mock(Preference.class); 86 final Preference category = mock(Preference.class); 87 when(mScreen.getPreferenceCount()).thenReturn(2); 88 when(mScreen.getPreference(0)).thenReturn(preference); 89 when(mScreen.getPreference(1)).thenReturn(category); 90 when(preference.getKey()).thenReturn(mController.getPreferenceKey()); 91 when(category.getKey()).thenReturn("voice_category"); 92 93 mController.displayPreference(mScreen); 94 95 // Remove preference. 96 verify(mScreen).removePreference(any(Preference.class)); 97 } 98 } 99