2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.language;
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;
25 import com.android.settings.SettingsRobolectricTestRunner;
26 import com.android.settings.TestConfig;
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;
36 import java.util.ArrayList;
37 import java.util.List;
39 import static com.google.common.truth.Truth.assertThat;
40 import static org.mockito.Matchers.any;
41 import static org.mockito.Matchers.anyString;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
47 @RunWith(SettingsRobolectricTestRunner.class)
48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
49 public class TtsPreferenceControllerTest {
51 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
52 private Context mContext;
54 private TtsEngines mTtsEngines;
56 private PreferenceScreen mScreen;
58 private TtsPreferenceController mController;
62 MockitoAnnotations.initMocks(this);
64 mController = new TtsPreferenceController(mContext, mTtsEngines);
68 public void testIsAvailable_ttsEngineEmpty_shouldReturnFalse() {
70 // Not available when there is no engine.
71 when(mTtsEngines.getEngines()).thenReturn(new ArrayList<>());
73 assertThat(mController.isAvailable()).isFalse();
77 public void testIsAvailable_ttsEngineInstalled_shouldReturnTrue() {
78 final List<TextToSpeech.EngineInfo> infolist = new ArrayList<>();
79 infolist.add(mock(TextToSpeech.EngineInfo.class));
80 when(mTtsEngines.getEngines()).thenReturn(infolist);
82 assertThat(mController.isAvailable()).isTrue();
86 public void displayPreference_notAvailable_shouldRemoveCategory() {
87 when(mScreen.findPreference(anyString())).thenReturn(mock(Preference.class));
89 mController.displayPreference(mScreen);
91 // Remove both category and preference.
92 verify(mScreen, times(2)).removePreference(any(Preference.class));