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 static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.mock; 23 24 import android.app.admin.DevicePolicyManager; 25 import android.content.Context; 26 27 import androidx.preference.Preference; 28 29 import com.android.settings.inputmethod.UserDictionaryList; 30 import com.android.settings.inputmethod.UserDictionarySettings; 31 import com.android.settings.testutils.FakeFeatureFactory; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Answers; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 42 import java.util.TreeSet; 43 44 @RunWith(RobolectricTestRunner.class) 45 public class UserDictionaryPreferenceControllerTest { 46 47 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 48 private Context mContext; 49 private Preference mPreference; 50 private TestController mController; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 doReturn(mock(DevicePolicyManager.class)).when(mContext) 56 .getSystemService(Context.DEVICE_POLICY_SERVICE); 57 FakeFeatureFactory.setupForTest(); 58 mController = new TestController(mContext); 59 mPreference = new Preference(RuntimeEnvironment.application); 60 } 61 62 @Test testIsAvailable_shouldReturnTrue()63 public void testIsAvailable_shouldReturnTrue() { 64 assertThat(mController.isAvailable()).isTrue(); 65 } 66 67 @Test updateState_noLocale_setUserDictionarySettingsAsFragment()68 public void updateState_noLocale_setUserDictionarySettingsAsFragment() { 69 mController.updateState(mPreference); 70 71 assertThat(mPreference.getFragment()) 72 .isEqualTo(UserDictionarySettings.class.getCanonicalName()); 73 } 74 75 @Test updateState_singleLocale_setUserDictionarySettingsAsFragment_setLocaleInExtra()76 public void updateState_singleLocale_setUserDictionarySettingsAsFragment_setLocaleInExtra() { 77 mController.mLocales.add("en"); 78 79 mController.updateState(mPreference); 80 81 final String fragmentName = UserDictionarySettings.class.getCanonicalName(); 82 assertThat(mPreference.getFragment()).isEqualTo(fragmentName); 83 assertThat(mPreference.getExtras().getString("locale")).isEqualTo("en"); 84 } 85 86 @Test updateState_multiLocale_setUserDictionaryListAsFragment()87 public void updateState_multiLocale_setUserDictionaryListAsFragment() { 88 mController.mLocales.add("en"); 89 mController.mLocales.add("de"); 90 mController.updateState(mPreference); 91 92 assertThat(mPreference.getFragment()) 93 .isEqualTo(UserDictionaryList.class.getCanonicalName()); 94 } 95 96 /** 97 * Fake Controller that overrides getDictionaryLocales to make testing the rest of stuff easier. 98 */ 99 private class TestController extends UserDictionaryPreferenceController { 100 101 private TreeSet<String> mLocales = new TreeSet<>(); 102 103 @Override getDictionaryLocales()104 protected TreeSet<String> getDictionaryLocales() { 105 return mLocales; 106 } 107 TestController(Context context)108 private TestController(Context context) { 109 super(context, "test_key"); 110 } 111 } 112 } 113