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.doReturn;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.admin.DevicePolicyManager;
30 import android.content.Context;
31 import android.content.SharedPreferences;
32 import android.hardware.display.AmbientDisplayConfiguration;
33 
34 import com.android.settings.dashboard.suggestions.SuggestionFeatureProviderImpl;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Answers;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class PickupGesturePreferenceControllerTest {
47 
48     private static final String KEY_PICK_UP = "gesture_pick_up";
49 
50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
51     private Context mContext;
52     @Mock
53     private AmbientDisplayConfiguration mAmbientDisplayConfiguration;
54 
55     private PickupGesturePreferenceController mController;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         doReturn(mock(DevicePolicyManager.class)).when(mContext)
61                 .getSystemService(Context.DEVICE_POLICY_SERVICE);
62         mController = new PickupGesturePreferenceController(mContext, KEY_PICK_UP);
63         mController.setConfig(mAmbientDisplayConfiguration);
64     }
65 
66     @Test
testIsChecked_configIsSet_shouldReturnTrue()67     public void testIsChecked_configIsSet_shouldReturnTrue() {
68         // Set the setting to be enabled.
69         when(mAmbientDisplayConfiguration.pickupGestureEnabled(anyInt())).thenReturn(true);
70 
71         assertThat(mController.isChecked()).isTrue();
72     }
73 
74     @Test
testIsChecked_configIsNotSet_shouldReturnFalse()75     public void testIsChecked_configIsNotSet_shouldReturnFalse() {
76         // Set the setting to be disabled.
77         when(mAmbientDisplayConfiguration.pickupGestureEnabled(anyInt())).thenReturn(false);
78 
79         assertThat(mController.isChecked()).isFalse();
80     }
81 
82     @Test
isSuggestionCompleted_ambientDisplayPickup_trueWhenVisited()83     public void isSuggestionCompleted_ambientDisplayPickup_trueWhenVisited() {
84         when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
85         when(mContext.getResources().getString(anyInt())).thenReturn("foo");
86         final Context context = RuntimeEnvironment.application;
87         final SharedPreferences prefs =
88                 new SuggestionFeatureProviderImpl().getSharedPrefs(context);
89         prefs.edit().putBoolean(PickupGestureSettings.PREF_KEY_SUGGESTION_COMPLETE, true).commit();
90 
91         assertThat(PickupGesturePreferenceController.isSuggestionComplete(mContext, prefs))
92                 .isTrue();
93     }
94 
95     @Test
getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE()96     public void getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE() {
97         when(mAmbientDisplayConfiguration.dozePickupSensorAvailable()).thenReturn(false);
98         when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(false);
99         final int availabilityStatus = mController.getAvailabilityStatus();
100 
101         assertThat(availabilityStatus).isEqualTo(UNSUPPORTED_ON_DEVICE);
102     }
103 
104     @Test
getAvailabilityStatus_aodSupported_aodOff_AVAILABLE()105     public void getAvailabilityStatus_aodSupported_aodOff_AVAILABLE() {
106         when(mAmbientDisplayConfiguration.dozePickupSensorAvailable()).thenReturn(true);
107         when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(true);
108         final int availabilityStatus = mController.getAvailabilityStatus();
109 
110         assertThat(availabilityStatus).isEqualTo(AVAILABLE);
111     }
112 
113     @Test
isSliceableCorrectKey_returnsTrue()114     public void isSliceableCorrectKey_returnsTrue() {
115         final PickupGesturePreferenceController controller =
116                 new PickupGesturePreferenceController(mContext, "gesture_pick_up");
117         assertThat(controller.isSliceable()).isTrue();
118     }
119 
120     @Test
isSliceableIncorrectKey_returnsFalse()121     public void isSliceableIncorrectKey_returnsFalse() {
122         final PickupGesturePreferenceController controller =
123                 new PickupGesturePreferenceController(mContext, "bad_key");
124         assertThat(controller.isSliceable()).isFalse();
125     }
126 
127     @Test
isPublicSlice_returnTrue()128     public void isPublicSlice_returnTrue() {
129         assertThat(mController.isPublicSlice()).isTrue();
130     }
131 }
132