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 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.speech.tts.TextToSpeech; 26 import android.speech.tts.TtsEngines; 27 import android.support.v7.preference.Preference; 28 import android.support.v7.preference.PreferenceScreen; 29 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 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.RuntimeEnvironment; 38 import org.robolectric.annotation.Config; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 public class TtsPreferenceControllerTest { 45 46 @Mock 47 private TtsEngines mTtsEngines; 48 @Mock 49 private PreferenceScreen mScreen; 50 51 private Context mContext; 52 private TtsPreferenceController mController; 53 private Preference mPreference; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mContext = spy(RuntimeEnvironment.application); 59 60 mController = new TtsPreferenceController(mContext, mTtsEngines); 61 mPreference = new Preference(RuntimeEnvironment.application); 62 mPreference.setKey(mController.getPreferenceKey()); 63 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 64 } 65 66 @Test 67 public void testIsAvailable_ttsEngineEmpty_shouldReturnFalse() { 68 // Not available when there is no engine. 69 when(mTtsEngines.getEngines()).thenReturn(new ArrayList<>()); 70 71 assertThat(mController.isAvailable()).isFalse(); 72 73 mController.displayPreference(mScreen); 74 75 assertThat(mPreference.isVisible()).isFalse(); 76 } 77 78 @Test 79 public void testIsAvailable_ttsEngineInstalled_shouldReturnTrue() { 80 final List<TextToSpeech.EngineInfo> infolist = new ArrayList<>(); 81 infolist.add(mock(TextToSpeech.EngineInfo.class)); 82 when(mTtsEngines.getEngines()).thenReturn(infolist); 83 84 assertThat(mController.isAvailable()).isTrue(); 85 86 mController.displayPreference(mScreen); 87 88 assertThat(mPreference.isVisible()).isTrue(); 89 } 90 91 @Test 92 @Config(qualifiers = "mcc999") 93 public void testIsAvailable_ifDisabled_shouldReturnFalse() { 94 assertThat(mController.isAvailable()).isFalse(); 95 } 96 } 97