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 android.content.Context;
20 import android.provider.Settings;
21 import android.support.v7.preference.PreferenceScreen;
22 
23 import com.android.settings.SettingsRobolectricTestRunner;
24 import com.android.settings.TestConfig;
25 
26 import com.android.settings.search2.InlineSwitchPayload;
27 import com.android.settings.search2.ResultPayload;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Answers;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.annotation.Config;
35 import org.robolectric.shadows.ShadowApplication;
36 
37 import static android.provider.Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED;
38 import static com.google.common.truth.Truth.assertThat;
39 import static org.mockito.Mockito.when;
40 
41 @RunWith(SettingsRobolectricTestRunner.class)
42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43 public class DoubleTapPowerPreferenceControllerTest {
44 
45     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
46     private Context mContext;
47     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
48     private PreferenceScreen mScreen;
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         MockitoAnnotations.initMocks(this);
55         mController = new DoubleTapPowerPreferenceController(mContext, null, KEY_DOUBLE_TAP_POWER);
56     }
57 
58     @Test
isAvailable_configIsTrue_shouldReturnTrue()59     public void isAvailable_configIsTrue_shouldReturnTrue() {
60         when(mContext.getResources().
61                 getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled))
62                 .thenReturn(true);
63 
64         assertThat(mController.isAvailable()).isTrue();
65     }
66 
67     @Test
isAvailable_configIsTrue_shouldReturnFalse()68     public void isAvailable_configIsTrue_shouldReturnFalse() {
69         when(mContext.getResources().
70                 getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled))
71                 .thenReturn(false);
72 
73         assertThat(mController.isAvailable()).isFalse();
74     }
75 
76     @Test
testSwitchEnabled_configIsNotSet_shouldReturnTrue()77     public void testSwitchEnabled_configIsNotSet_shouldReturnTrue() {
78         // Set the setting to be enabled.
79         final Context context = ShadowApplication.getInstance().getApplicationContext();
80         Settings.System.putInt(context.getContentResolver(),
81                 CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0);
82         mController = new DoubleTapPowerPreferenceController(context, null, KEY_DOUBLE_TAP_POWER);
83 
84         assertThat(mController.isSwitchPrefEnabled()).isTrue();
85     }
86 
87     @Test
testSwitchEnabled_configIsSet_shouldReturnFalse()88     public void testSwitchEnabled_configIsSet_shouldReturnFalse() {
89         // Set the setting to be disabled.
90         final Context context = ShadowApplication.getInstance().getApplicationContext();
91         Settings.System.putInt(context.getContentResolver(),
92                 CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 1);
93         mController = new DoubleTapPowerPreferenceController(context, null, KEY_DOUBLE_TAP_POWER);
94 
95         assertThat(mController.isSwitchPrefEnabled()).isFalse();
96     }
97 }
98