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 com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.content.SharedPreferences;
29 import android.hardware.display.AmbientDisplayConfiguration;
30 
31 import com.android.settings.dashboard.suggestions.SuggestionFeatureProviderImpl;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Answers;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class DoubleTapScreenPreferenceControllerTest {
44 
45     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
46     private Context mContext;
47     @Mock
48     private AmbientDisplayConfiguration mAmbientDisplayConfiguration;
49     private DoubleTapScreenPreferenceController mController;
50 
51     private static final String KEY_DOUBLE_TAP_SCREEN = "gesture_double_tap_screen";
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56         mController = new DoubleTapScreenPreferenceController(mContext, KEY_DOUBLE_TAP_SCREEN);
57         mController.setConfig(mAmbientDisplayConfiguration);
58     }
59 
60     @Test
testIsChecked_configIsSet_shouldReturnTrue()61     public void testIsChecked_configIsSet_shouldReturnTrue() {
62         // Set the setting to be enabled.
63         when(mAmbientDisplayConfiguration.doubleTapGestureEnabled(anyInt())).thenReturn(true);
64 
65         assertThat(mController.isChecked()).isTrue();
66     }
67 
68     @Test
testIsChecked_configIsNotSet_shouldReturnFalse()69     public void testIsChecked_configIsNotSet_shouldReturnFalse() {
70         when(mAmbientDisplayConfiguration.doubleTapGestureEnabled(anyInt())).thenReturn(false);
71 
72         assertThat(mController.isChecked()).isFalse();
73     }
74 
75     @Test
isSuggestionCompleted_ambientDisplay_falseWhenNotVisited()76     public void isSuggestionCompleted_ambientDisplay_falseWhenNotVisited() {
77         when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(true);
78         // No stored value in shared preferences if not visited yet.
79         final Context context = RuntimeEnvironment.application;
80         final SharedPreferences prefs =
81                 new SuggestionFeatureProviderImpl(context).getSharedPrefs(context);
82 
83         assertThat(DoubleTapScreenPreferenceController
84                 .isSuggestionComplete(mAmbientDisplayConfiguration, prefs)).isFalse();
85     }
86 
87     @Test
isSuggestionCompleted_ambientDisplay_trueWhenVisited()88     public void isSuggestionCompleted_ambientDisplay_trueWhenVisited() {
89         when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(false);
90         final Context context = RuntimeEnvironment.application;
91         final SharedPreferences prefs =
92                 new SuggestionFeatureProviderImpl(context).getSharedPrefs(context);
93 
94         prefs.edit().putBoolean(
95                 DoubleTapScreenSettings.PREF_KEY_SUGGESTION_COMPLETE, true).commit();
96 
97         assertThat(DoubleTapScreenPreferenceController
98                 .isSuggestionComplete(mAmbientDisplayConfiguration, prefs)).isTrue();
99     }
100 
101     @Test
getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE()102     public void getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE() {
103         when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(false);
104         when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(false);
105         final int availabilityStatus = mController.getAvailabilityStatus();
106 
107         assertThat(availabilityStatus).isEqualTo(UNSUPPORTED_ON_DEVICE);
108     }
109 
110     @Test
getAvailabilityStatus_aodSupported_aodOff_AVAILABLE()111     public void getAvailabilityStatus_aodSupported_aodOff_AVAILABLE() {
112         when(mAmbientDisplayConfiguration.doubleTapSensorAvailable()).thenReturn(true);
113         when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(true);
114         final int availabilityStatus = mController.getAvailabilityStatus();
115 
116         assertThat(availabilityStatus).isEqualTo(AVAILABLE);
117     }
118 
119     @Test
isSliceableCorrectKey_returnsTrue()120     public void isSliceableCorrectKey_returnsTrue() {
121         final DoubleTapScreenPreferenceController controller =
122                 new DoubleTapScreenPreferenceController(mContext, "gesture_double_tap_screen");
123         assertThat(controller.isSliceable()).isTrue();
124     }
125 
126     @Test
isSliceableIncorrectKey_returnsFalse()127     public void isSliceableIncorrectKey_returnsFalse() {
128         final DoubleTapScreenPreferenceController controller =
129                 new DoubleTapScreenPreferenceController(mContext, "bad_key");
130         assertThat(controller.isSliceable()).isFalse();
131     }
132 
133     @Test
isPublicSlice_returnTrue()134     public void isPublicSlice_returnTrue() {
135         assertThat(mController.isPublicSlice()).isTrue();
136     }
137 }
138