OSDN Git Service

0e4178413e11d2bbcbd4ae3e6c0157736e0af8db
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / language / TtsPreferenceControllerTest.java
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.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.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;
46
47 @RunWith(SettingsRobolectricTestRunner.class)
48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
49 public class TtsPreferenceControllerTest {
50
51     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
52     private Context mContext;
53     @Mock
54     private TtsEngines mTtsEngines;
55     @Mock
56     private PreferenceScreen mScreen;
57
58     private TtsPreferenceController mController;
59
60     @Before
61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63
64         mController = new TtsPreferenceController(mContext, mTtsEngines);
65     }
66
67     @Test
68     public void testIsAvailable_ttsEngineEmpty_shouldReturnFalse() {
69
70         // Not available when there is no engine.
71         when(mTtsEngines.getEngines()).thenReturn(new ArrayList<>());
72
73         assertThat(mController.isAvailable()).isFalse();
74     }
75
76     @Test
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);
81
82         assertThat(mController.isAvailable()).isTrue();
83     }
84
85     @Test
86     public void displayPreference_notAvailable_shouldRemoveCategory() {
87         when(mScreen.findPreference(anyString())).thenReturn(mock(Preference.class));
88
89         mController.displayPreference(mScreen);
90
91         // Remove both category and preference.
92         verify(mScreen, times(2)).removePreference(any(Preference.class));
93     }
94 }