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