1 /* 2 * Copyright (C) 2019 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.tts; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.pm.PackageManager; 31 import android.content.pm.ResolveInfo; 32 import android.content.pm.ServiceInfo; 33 import android.os.Bundle; 34 import android.speech.tts.TextToSpeech; 35 import android.speech.tts.TtsEngines; 36 37 import com.android.settings.testutils.shadow.ShadowTtsEngines; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.annotation.Config; 46 import org.robolectric.shadow.api.Shadow; 47 import org.robolectric.shadows.ShadowPackageManager; 48 import org.robolectric.shadows.androidx.fragment.FragmentController; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class TtsEnginePreferenceFragmentTest { 52 53 private Context mContext; 54 private TtsEnginePreferenceFragment mTtsEnginePreferenceFragment; 55 56 @Before setUp()57 public void setUp() { 58 mContext = RuntimeEnvironment.application; 59 60 final ResolveInfo info = new ResolveInfo(); 61 final ServiceInfo serviceInfo = spy(new ServiceInfo()); 62 serviceInfo.packageName = mContext.getPackageName(); 63 serviceInfo.name = mContext.getClass().getName(); 64 info.serviceInfo = serviceInfo; 65 doReturn("title").when(serviceInfo).loadLabel(any(PackageManager.class)); 66 doReturn(1).when(serviceInfo).getIconResource(); 67 68 final ShadowPackageManager spm = Shadow.extract(mContext.getPackageManager()); 69 spm.addResolveInfoForIntent( 70 new Intent(TextToSpeech.Engine.INTENT_ACTION_TTS_SERVICE), info); 71 } 72 73 @After tearDown()74 public void tearDown() { 75 ShadowTtsEngines.reset(); 76 } 77 78 @Test getCandidates_AddEngines_returnCorrectEngines()79 public void getCandidates_AddEngines_returnCorrectEngines() { 80 mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(), 81 new Bundle()) 82 .create() 83 .get(); 84 85 assertThat(mTtsEnginePreferenceFragment.getCandidates().size()).isEqualTo(1); 86 } 87 88 @Test 89 @Config(shadows = {ShadowTtsEngines.class}) getDefaultKey_validKey_returnCorrectKey()90 public void getDefaultKey_validKey_returnCorrectKey() { 91 final String TEST_ENGINE = "test_engine"; 92 final TtsEngines engine = mock(TtsEngines.class); 93 ShadowTtsEngines.setInstance(engine); 94 mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(), 95 new Bundle()) 96 .create() 97 .get(); 98 when(engine.getDefaultEngine()).thenReturn(TEST_ENGINE); 99 100 assertThat(mTtsEnginePreferenceFragment.getDefaultKey()).isEqualTo(TEST_ENGINE); 101 } 102 103 @Test 104 @Config(shadows = {ShadowTtsEngines.class}) setDefaultKey_validKey_callingTtsEngineFunction()105 public void setDefaultKey_validKey_callingTtsEngineFunction() { 106 final TtsEngines engine = mock(TtsEngines.class); 107 ShadowTtsEngines.setInstance(engine); 108 mTtsEnginePreferenceFragment = FragmentController.of(new TtsEnginePreferenceFragment(), 109 new Bundle()) 110 .create() 111 .get(); 112 113 mTtsEnginePreferenceFragment.setDefaultKey(mContext.getPackageName()); 114 115 verify(engine).isEngineInstalled(mContext.getPackageName()); 116 } 117 118 @Test setDefaultKey_validKey_updateCheckedState()119 public void setDefaultKey_validKey_updateCheckedState() { 120 mTtsEnginePreferenceFragment = spy(FragmentController.of(new TtsEnginePreferenceFragment(), 121 new Bundle()) 122 .create() 123 .get()); 124 125 mTtsEnginePreferenceFragment.setDefaultKey(mContext.getPackageName()); 126 127 verify(mTtsEnginePreferenceFragment).updateCheckedState(mContext.getPackageName()); 128 } 129 } 130