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 android.content.Context; 22 import android.speech.tts.TextToSpeech; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.Checkable; 27 28 import androidx.preference.PreferenceViewHolder; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.RuntimeEnvironment; 35 36 @RunWith(RobolectricTestRunner.class) 37 public class TtsEnginePreferenceTest { 38 39 private static final String KEY = "test_key"; 40 41 private TtsEnginePreference mPreference; 42 private Context mContext; 43 private PreferenceViewHolder mViewHolder; 44 private View mRootView; 45 private FakeRadioButtonGroupState mState; 46 47 @Before setUp()48 public void setUp() { 49 mContext = RuntimeEnvironment.application; 50 51 final TextToSpeech.EngineInfo info = new TextToSpeech.EngineInfo(); 52 info.system = true; 53 mState = new FakeRadioButtonGroupState(); 54 mPreference = new TtsEnginePreference(mContext, info, mState); 55 mPreference.setKey(KEY); 56 57 // Create preference view holder 58 final LayoutInflater inflater = LayoutInflater.from(mContext); 59 mRootView = View.inflate(mContext, mPreference.getLayoutResource(), null /* parent */); 60 final ViewGroup widgetFrame = mRootView.findViewById(android.R.id.widget_frame); 61 inflater.inflate(mPreference.getWidgetLayoutResource(), widgetFrame); 62 mViewHolder = PreferenceViewHolder.createInstanceForTests(mRootView); 63 } 64 65 @Test onClick_shouldInvokeOnCheckedChangeListener()66 public void onClick_shouldInvokeOnCheckedChangeListener() { 67 mPreference.onBindViewHolder(mViewHolder); 68 69 mPreference.onClick(); 70 71 assertThat(mState.getCurrentKey()).isEqualTo(mPreference.getKey()); 72 } 73 74 public static class FakeRadioButtonGroupState implements 75 TtsEnginePreference.RadioButtonGroupState { 76 77 private String mKey; 78 79 @Override getCurrentChecked()80 public Checkable getCurrentChecked() { 81 return null; 82 } 83 84 @Override getCurrentKey()85 public String getCurrentKey() { 86 return mKey; 87 } 88 89 @Override setCurrentChecked(Checkable current)90 public void setCurrentChecked(Checkable current) { 91 } 92 93 @Override setCurrentKey(String key)94 public void setCurrentKey(String key) { 95 mKey = key; 96 } 97 } 98 } 99