1 /*
2  * Copyright (C) 2016 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.gestures;
18 
19 import static android.provider.Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED;
20 
21 import static com.android.settings.gestures.DoubleTapPowerPreferenceController.OFF;
22 import static com.android.settings.gestures.DoubleTapPowerPreferenceController.ON;
23 import static com.android.settings.gestures.DoubleTapPowerPreferenceController.isSuggestionComplete;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.content.SharedPreferences;
30 import android.provider.Settings;
31 
32 import com.android.settings.dashboard.suggestions.SuggestionFeatureProviderImpl;
33 import com.android.settings.testutils.shadow.SettingsShadowResources;
34 
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.annotation.Config;
42 
43 @RunWith(RobolectricTestRunner.class)
44 @Config(shadows = SettingsShadowResources.class)
45 public class DoubleTapPowerPreferenceControllerTest {
46 
47     private Context mContext;
48     private ContentResolver mContentResolver;
49     private DoubleTapPowerPreferenceController mController;
50     private static final String KEY_DOUBLE_TAP_POWER = "gesture_double_tap_power";
51 
52     @Before
setUp()53     public void setUp() {
54         mContext = RuntimeEnvironment.application;
55         mContentResolver = mContext.getContentResolver();
56         mController = new DoubleTapPowerPreferenceController(mContext, KEY_DOUBLE_TAP_POWER);
57     }
58 
59     @After
tearDown()60     public void tearDown() {
61         SettingsShadowResources.reset();
62     }
63 
64     @Test
isAvailable_configIsTrue_shouldReturnTrue()65     public void isAvailable_configIsTrue_shouldReturnTrue() {
66         SettingsShadowResources.overrideResource(
67                 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled,
68                 Boolean.TRUE);
69 
70         assertThat(mController.isAvailable()).isTrue();
71     }
72 
73     @Test
isAvailable_configIsTrue_shouldReturnFalse()74     public void isAvailable_configIsTrue_shouldReturnFalse() {
75         SettingsShadowResources.overrideResource(
76                 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled,
77                 Boolean.FALSE);
78 
79         assertThat(mController.isAvailable()).isFalse();
80     }
81 
82     @Test
testIsChecked_configIsNotSet_shouldReturnTrue()83     public void testIsChecked_configIsNotSet_shouldReturnTrue() {
84         // Set the setting to be enabled.
85         Settings.Secure.putInt(mContentResolver, CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, ON);
86         mController = new DoubleTapPowerPreferenceController(mContext, KEY_DOUBLE_TAP_POWER);
87 
88         assertThat(mController.isChecked()).isTrue();
89     }
90 
91     @Test
testIsChecked_configIsSet_shouldReturnFalse()92     public void testIsChecked_configIsSet_shouldReturnFalse() {
93         // Set the setting to be disabled.
94         Settings.Secure.putInt(mContentResolver, CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, OFF);
95         mController = new DoubleTapPowerPreferenceController(mContext, KEY_DOUBLE_TAP_POWER);
96 
97         assertThat(mController.isChecked()).isFalse();
98     }
99 
100     @Test
isSuggestionCompleted_doubleTapPower_trueWhenNotAvailable()101     public void isSuggestionCompleted_doubleTapPower_trueWhenNotAvailable() {
102         SettingsShadowResources.overrideResource(
103                 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, false);
104 
105         assertThat(isSuggestionComplete(mContext, null/* prefs */)).isTrue();
106     }
107 
108     @Test
isSuggestionCompleted_doubleTapPower_falseWhenNotVisited()109     public void isSuggestionCompleted_doubleTapPower_falseWhenNotVisited() {
110         SettingsShadowResources.overrideResource(
111                 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, true);
112         // No stored value in shared preferences if not visited yet.
113         final SharedPreferences prefs =
114                 new SuggestionFeatureProviderImpl(mContext).getSharedPrefs(mContext);
115         assertThat(isSuggestionComplete(mContext, prefs)).isFalse();
116     }
117 
118     @Test
isSuggestionCompleted_doubleTapPower_trueWhenVisited()119     public void isSuggestionCompleted_doubleTapPower_trueWhenVisited() {
120         SettingsShadowResources.overrideResource(
121                 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled, true);
122         // No stored value in shared preferences if not visited yet.
123         final SharedPreferences prefs =
124                 new SuggestionFeatureProviderImpl(mContext).getSharedPrefs(mContext);
125         prefs.edit().putBoolean(DoubleTapPowerSettings.PREF_KEY_SUGGESTION_COMPLETE, true).commit();
126 
127         assertThat(isSuggestionComplete(mContext, prefs)).isTrue();
128     }
129 
130     @Test
isSliceableCorrectKey_returnsTrue()131     public void isSliceableCorrectKey_returnsTrue() {
132         final DoubleTapPowerPreferenceController controller =
133                 new DoubleTapPowerPreferenceController(mContext, "gesture_double_tap_power");
134         assertThat(controller.isSliceable()).isTrue();
135     }
136 
137     @Test
isSliceableIncorrectKey_returnsFalse()138     public void isSliceableIncorrectKey_returnsFalse() {
139         final DoubleTapPowerPreferenceController controller =
140                 new DoubleTapPowerPreferenceController(mContext, "bad_key");
141         assertThat(controller.isSliceable()).isFalse();
142     }
143 
144     @Test
isPublicSlice_returnTrue()145     public void isPublicSlice_returnTrue() {
146         assertThat(mController.isPublicSlice()).isTrue();
147     }
148 }
149