Home | History | Annotate | Download | only in assist
      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.applications.assist;
     18 
     19 import android.content.ComponentName;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.provider.Settings;
     23 import android.support.v7.preference.PreferenceScreen;
     24 import android.support.v7.preference.TwoStatePreference;
     25 
     26 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     27 import com.android.settings.TestConfig;
     28 import com.android.settings.testutils.shadow.ShadowSecureSettings;
     29 import com.android.settingslib.core.lifecycle.Lifecycle;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.mockito.Answers;
     35 import org.mockito.Mock;
     36 import org.mockito.MockitoAnnotations;
     37 import org.robolectric.RuntimeEnvironment;
     38 import org.robolectric.annotation.Config;
     39 import org.robolectric.util.ReflectionHelpers;
     40 
     41 import static com.google.common.truth.Truth.assertThat;
     42 import static org.mockito.Matchers.any;
     43 import static org.mockito.Matchers.anyString;
     44 import static org.mockito.Matchers.eq;
     45 import static org.mockito.Mockito.doReturn;
     46 import static org.mockito.Mockito.spy;
     47 import static org.mockito.Mockito.verify;
     48 import static org.mockito.Mockito.when;
     49 
     50 @RunWith(SettingsRobolectricTestRunner.class)
     51 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     52 public class AssistFlashScreenPreferenceControllerTest {
     53 
     54     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     55     private Context mMockContext;
     56     @Mock
     57     private PreferenceScreen mScreen;
     58     @Mock
     59     private TwoStatePreference mPreference;
     60     @Mock
     61     private AssistFlashScreenPreferenceController.SettingObserver mObserver;
     62     private Context mContext;
     63     private AssistFlashScreenPreferenceController mController;
     64     private Lifecycle mLifecycle;
     65 
     66     @Before
     67     public void setUp() {
     68         MockitoAnnotations.initMocks(this);
     69         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
     70         mLifecycle = new Lifecycle();
     71         mContext = RuntimeEnvironment.application;
     72         mController = spy(new AssistFlashScreenPreferenceController(mContext, mLifecycle));
     73         mLifecycle.addObserver(mController);
     74         ReflectionHelpers.setField(mController, "mSettingObserver", mObserver);
     75     }
     76 
     77     @Test
     78     @Config(shadows = {ShadowSecureSettings.class})
     79     public void isAvailable_hasAssistantAndAllowDisclosure_shouldReturnTrue() {
     80         ReflectionHelpers.setField(mController, "mContext", mMockContext);
     81         final ContentResolver cr = mContext.getContentResolver();
     82         Settings.Secure.putString(cr, Settings.Secure.ASSISTANT, "com.android.settings/assist");
     83         doReturn(true).when(mController).allowDisablingAssistDisclosure();
     84 
     85         assertThat(mController.isAvailable()).isTrue();
     86     }
     87 
     88     @Test
     89     @Config(shadows = {ShadowSecureSettings.class})
     90     public void isAvailable_hasAssistantAndDisallowDisclosure_shouldReturnTrue() {
     91         ReflectionHelpers.setField(mController, "mContext", mMockContext);
     92         final ContentResolver cr = mContext.getContentResolver();
     93         Settings.Secure.putString(cr, Settings.Secure.ASSISTANT, "com.android.settings/assist");
     94         doReturn(false).when(mController).allowDisablingAssistDisclosure();
     95 
     96         assertThat(mController.isAvailable()).isFalse();
     97     }
     98 
     99     @Test
    100     public void isAvailable_hasNoAssistant_shouldReturnFalse() {
    101         Settings.Secure.putString(mContext.getContentResolver(), Settings.Secure.ASSISTANT, "");
    102 
    103         assertThat(mController.isAvailable()).isFalse();
    104     }
    105 
    106     @Test
    107     @Config(shadows = {ShadowSecureSettings.class})
    108     public void onResume_shouldUpdatePreference() {
    109         Settings.Secure.putString(mContext.getContentResolver(),
    110                 Settings.Secure.ASSISTANT, "com.android.settings/assist");
    111         doReturn(true).when(mController).isAvailable();
    112         doReturn(true).when(mController).isPreInstalledAssistant(any(ComponentName.class));
    113         doReturn(true).when(mController).willShowFlash(any(ComponentName.class));
    114 
    115         mController.displayPreference(mScreen);
    116         Settings.Secure.putInt(mContext.getContentResolver(),
    117                 Settings.Secure.ASSIST_DISCLOSURE_ENABLED, 1);
    118 
    119         mLifecycle.onResume();
    120 
    121         verify(mObserver).register(any(ContentResolver.class), eq(true));
    122         verify(mPreference).setChecked(true);
    123     }
    124 }
    125