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