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