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