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