1 /*
2  * Copyright (C) 2017 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.enterprise;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settings.R;
28 import com.android.settings.testutils.FakeFeatureFactory;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Answers;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class ImePreferenceControllerTest {
40 
41     private static final String DEFAULT_IME_LABEL = "Test IME";
42     private static final String DEFAULT_IME_TEXT = "Set to Test IME";
43     private static final String KEY_INPUT_METHOD = "input_method";
44 
45     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
46     private Context mContext;
47     private FakeFeatureFactory mFeatureFactory;
48 
49     private ImePreferenceController mController;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mFeatureFactory = FakeFeatureFactory.setupForTest();
55         mController = new ImePreferenceController(mContext);
56         when(mContext.getResources().getString(R.string.enterprise_privacy_input_method_name,
57                 DEFAULT_IME_LABEL)).thenReturn(DEFAULT_IME_TEXT);
58     }
59 
60     @Test
testUpdateState()61     public void testUpdateState() {
62         final Preference preference = new Preference(mContext, null, 0, 0);
63 
64         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getImeLabelIfOwnerSet())
65                 .thenReturn(DEFAULT_IME_LABEL);
66         mController.updateState(preference);
67         assertThat(preference.getSummary()).isEqualTo(DEFAULT_IME_TEXT);
68     }
69 
70     @Test
testIsAvailable()71     public void testIsAvailable() {
72         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getImeLabelIfOwnerSet())
73                 .thenReturn(null);
74         assertThat(mController.isAvailable()).isFalse();
75 
76         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getImeLabelIfOwnerSet())
77                 .thenReturn(DEFAULT_IME_LABEL);
78         assertThat(mController.isAvailable()).isTrue();
79     }
80 
81     @Test
testHandlePreferenceTreeClick()82     public void testHandlePreferenceTreeClick() {
83         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
84                 .isFalse();
85     }
86 
87     @Test
testGetPreferenceKey()88     public void testGetPreferenceKey() {
89         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_INPUT_METHOD);
90     }
91 }
92