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.inputmethod; 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.view.inputmethod.InputMethodInfo; 27 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceManager; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.testutils.shadow.ShadowInputMethodManagerWithMethodList; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 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 @Config(shadows = ShadowInputMethodManagerWithMethodList.class) 46 public class InputMethodPreferenceControllerTest { 47 48 private InputMethodPreferenceController mController; 49 private Context mContext; 50 private PreferenceScreen mScreen; 51 private Preference mPreference; 52 53 @Before setUp()54 public void setUp() { 55 mContext = RuntimeEnvironment.application; 56 mScreen = spy(new PreferenceScreen(mContext, null)); 57 mPreference = new Preference(mContext); 58 mController = new InputMethodPreferenceController(mContext, "key"); 59 60 when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); 61 when(mScreen.getContext()).thenReturn(mContext); 62 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 63 64 mController.displayPreference(mScreen); 65 } 66 67 @Test onStart_NoInputMethod_shouldHaveOnePreference()68 public void onStart_NoInputMethod_shouldHaveOnePreference() { 69 mController.onStart(); 70 71 assertThat(mScreen.getPreferenceCount()).isEqualTo(1); 72 } 73 74 @Test onStart_hasInputMethod_shouldHaveCorrectPreferences()75 public void onStart_hasInputMethod_shouldHaveCorrectPreferences() { 76 final List<InputMethodInfo> imis = new ArrayList<>(); 77 imis.add(mock(InputMethodInfo.class)); 78 imis.add(mock(InputMethodInfo.class)); 79 when(imis.get(0).getPackageName()).thenReturn("name1"); 80 when(imis.get(1).getPackageName()).thenReturn("name2"); 81 ShadowInputMethodManagerWithMethodList.getShadow().setEnabledInputMethodList(imis); 82 83 mController.onStart(); 84 85 assertThat(mScreen.getPreferenceCount()).isEqualTo(3); 86 } 87 } 88