• 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 android.content.Context;
20 import android.speech.tts.TtsEngines;
21 import android.support.v7.preference.Preference;
22 
23 import com.android.settings.SettingsRobolectricTestRunner;
24 import com.android.settings.TestConfig;
25 import com.android.settings.UserDictionarySettings;
26 import com.android.settings.inputmethod.UserDictionaryList;
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.annotation.Config;
36 import org.robolectric.shadows.ShadowApplication;
37 
38 import java.util.TreeSet;
39 
40 import static com.google.common.truth.Truth.assertThat;
41 
42 @RunWith(SettingsRobolectricTestRunner.class)
43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44 public class UserDictionaryPreferenceControllerTest {
45 
46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
47     private Context mContext;
48     @Mock
49     private TtsEngines mTtsEngines;
50     private Preference mPreference;
51     private TestController mController;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56         FakeFeatureFactory.setupForTest(mContext);
57         mController = new TestController(mContext);
58         mPreference = new Preference(ShadowApplication.getInstance().getApplicationContext());
59     }
60 
61     @Test
testIsAvailable_noLocale_shouldReturnFalse()62     public void testIsAvailable_noLocale_shouldReturnFalse() {
63         mController.mLocales = null;
64 
65         assertThat(mController.isAvailable()).isFalse();
66     }
67 
68     @Test
testIsAvailable_hasLocale_shouldReturnTrue()69     public void testIsAvailable_hasLocale_shouldReturnTrue() {
70         mController.mLocales.add("en");
71 
72         assertThat(mController.isAvailable()).isTrue();
73     }
74 
75     @Test
updateState_noLocale_setUserDictionarySettingsAsFragment()76     public void updateState_noLocale_setUserDictionarySettingsAsFragment() {
77         mController.updateState(mPreference);
78 
79         assertThat(mPreference.getFragment())
80                 .isEqualTo(UserDictionarySettings.class.getCanonicalName());
81     }
82 
83     @Test
updateState_singleLocale_setUserDictionarySettingsAsFragment_setLocaleInExtra()84     public void updateState_singleLocale_setUserDictionarySettingsAsFragment_setLocaleInExtra() {
85         mController.mLocales.add("en");
86 
87         mController.updateState(mPreference);
88 
89         assertThat(mPreference.getFragment())
90                 .isEqualTo(UserDictionarySettings.class.getCanonicalName());
91         assertThat(mPreference.getExtras().getString("locale"))
92                 .isEqualTo("en");
93     }
94 
95     @Test
updateState_multiLocale_setUserDictionaryListAsFragment()96     public void updateState_multiLocale_setUserDictionaryListAsFragment() {
97         mController.mLocales.add("en");
98         mController.mLocales.add("de");
99         mController.updateState(mPreference);
100 
101         assertThat(mPreference.getFragment())
102                 .isEqualTo(UserDictionaryList.class.getCanonicalName());
103     }
104 
105     /**
106      * Fake Controller that overrides getDictionaryLocales to make testing the rest of stuff easier.
107      */
108     private class TestController extends UserDictionaryPreferenceController {
109 
110         private TreeSet<String> mLocales = new TreeSet<>();
111 
112         @Override
getDictionaryLocales()113         protected TreeSet<String> getDictionaryLocales() {
114             return mLocales;
115         }
116 
TestController(Context context)117         public TestController(Context context) {
118             super(context);
119         }
120     }
121 
122 }
123