Home | History | Annotate | Download | only in enterprise
      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 android.content.Context;
     20 
     21 import com.android.settings.R;
     22 import android.support.v7.preference.Preference;
     23 
     24 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     25 import com.android.settings.TestConfig;
     26 import com.android.settings.core.PreferenceAvailabilityObserver;
     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 
     37 import static com.google.common.truth.Truth.assertThat;
     38 import static org.mockito.Mockito.verify;
     39 import static org.mockito.Mockito.when;
     40 
     41 /**
     42  * Tests for {@link ImePreferenceController}.
     43  */
     44 @RunWith(SettingsRobolectricTestRunner.class)
     45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     46 public final class ImePreferenceControllerTest {
     47 
     48     private static final String DEFAULT_IME_LABEL = "Test IME";
     49     private static final String DEFAULT_IME_TEXT = "Set to Test IME";
     50     private static final String KEY_INPUT_METHOD = "input_method";
     51 
     52     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     53     private Context mContext;
     54     private FakeFeatureFactory mFeatureFactory;
     55     @Mock private PreferenceAvailabilityObserver mObserver;
     56 
     57     private ImePreferenceController mController;
     58 
     59     @Before
     60     public void setUp() {
     61         MockitoAnnotations.initMocks(this);
     62         FakeFeatureFactory.setupForTest(mContext);
     63         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
     64         mController = new ImePreferenceController(mContext, null /* lifecycle */);
     65         when(mContext.getResources().getString(R.string.enterprise_privacy_input_method_name,
     66                 DEFAULT_IME_LABEL)).thenReturn(DEFAULT_IME_TEXT);
     67         mController.setAvailabilityObserver(mObserver);
     68     }
     69 
     70     @Test
     71     public void testGetAvailabilityObserver() {
     72         assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver);
     73     }
     74 
     75     @Test
     76     public void testUpdateState() {
     77         final Preference preference = new Preference(mContext, null, 0, 0);
     78 
     79         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getImeLabelIfOwnerSet())
     80             .thenReturn(DEFAULT_IME_LABEL);
     81         mController.updateState(preference);
     82         assertThat(preference.getSummary()).isEqualTo(DEFAULT_IME_TEXT);
     83     }
     84 
     85     @Test
     86     public void testIsAvailable() {
     87         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getImeLabelIfOwnerSet())
     88             .thenReturn(null);
     89         assertThat(mController.isAvailable()).isFalse();
     90         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_INPUT_METHOD, false);
     91 
     92         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getImeLabelIfOwnerSet())
     93             .thenReturn(DEFAULT_IME_LABEL);
     94         assertThat(mController.isAvailable()).isTrue();
     95         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_INPUT_METHOD, true);
     96     }
     97 
     98     @Test
     99     public void testHandlePreferenceTreeClick() {
    100         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
    101                 .isFalse();
    102     }
    103 
    104     @Test
    105     public void testGetPreferenceKey() {
    106         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_INPUT_METHOD);
    107     }
    108 }
    109