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.verify;
     25 import static org.mockito.Mockito.when;
     26 
     27 import android.arch.lifecycle.LifecycleOwner;
     28 import android.content.ContentResolver;
     29 import android.content.Context;
     30 import android.provider.Settings;
     31 import android.support.v7.preference.PreferenceScreen;
     32 import android.support.v7.preference.TwoStatePreference;
     33 
     34 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     35 import com.android.settingslib.core.lifecycle.Lifecycle;
     36 
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 import org.mockito.Mock;
     41 import org.mockito.MockitoAnnotations;
     42 import org.robolectric.RuntimeEnvironment;
     43 import org.robolectric.util.ReflectionHelpers;
     44 
     45 @RunWith(SettingsRobolectricTestRunner.class)
     46 public class AssistContextPreferenceControllerTest {
     47 
     48     @Mock
     49     private PreferenceScreen mScreen;
     50     @Mock
     51     private TwoStatePreference mPreference;
     52     @Mock
     53     private AssistContextPreferenceController.SettingObserver mObserver;
     54     private Context mContext;
     55     private AssistContextPreferenceController mController;
     56     private LifecycleOwner mLifecycleOwner;
     57     private Lifecycle mLifecycle;
     58 
     59     @Before
     60     public void setUp() {
     61         MockitoAnnotations.initMocks(this);
     62         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
     63         mLifecycleOwner = () -> mLifecycle;
     64         mLifecycle = new Lifecycle(mLifecycleOwner);
     65         mContext = RuntimeEnvironment.application;
     66         mController = new AssistContextPreferenceController(mContext, mLifecycle);
     67         ReflectionHelpers.setField(mController, "mSettingObserver", mObserver);
     68     }
     69 
     70     @Test
     71     public void isAvailable_hasAssistant_shouldReturnTrue() {
     72         Settings.Secure.putString(mContext.getContentResolver(),
     73                 Settings.Secure.ASSISTANT, "com.android.settings/assist");
     74         assertThat(mController.isAvailable()).isTrue();
     75     }
     76 
     77     @Test
     78     public void isAvailable_hasNoAssistant_shouldReturnFalse() {
     79         Settings.Secure.putString(mContext.getContentResolver(),
     80                 Settings.Secure.ASSISTANT, "");
     81         assertThat(mController.isAvailable()).isFalse();
     82     }
     83 
     84     @Test
     85     public void onResume_shouldUpdatePreference() {
     86         Settings.Secure.putString(mContext.getContentResolver(),
     87                 Settings.Secure.ASSISTANT, "com.android.settings/assist");
     88         mController.displayPreference(mScreen);
     89         Settings.Secure.putInt(mContext.getContentResolver(),
     90                 Settings.Secure.ASSIST_STRUCTURE_ENABLED, 1);
     91 
     92         mLifecycle.handleLifecycleEvent(ON_RESUME);
     93         verify(mObserver).register(any(ContentResolver.class), eq(true));
     94         verify(mPreference).setChecked(true);
     95     }
     96 }
     97