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