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 PickupGesturePreferenceControllerTest {
44 
45     private static final String KEY_PICK_UP = "gesture_pick_up";
46 
47     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
48     private Context mContext;
49     @Mock
50     private AmbientDisplayConfiguration mAmbientDisplayConfiguration;
51 
52     private PickupGesturePreferenceController mController;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         mController = new PickupGesturePreferenceController(mContext, KEY_PICK_UP);
58         mController.setConfig(mAmbientDisplayConfiguration);
59     }
60 
61     @Test
testIsChecked_configIsSet_shouldReturnTrue()62     public void testIsChecked_configIsSet_shouldReturnTrue() {
63         // Set the setting to be enabled.
64         when(mAmbientDisplayConfiguration.pickupGestureEnabled(anyInt())).thenReturn(true);
65 
66         assertThat(mController.isChecked()).isTrue();
67     }
68 
69     @Test
testIsChecked_configIsNotSet_shouldReturnFalse()70     public void testIsChecked_configIsNotSet_shouldReturnFalse() {
71         // Set the setting to be disabled.
72         when(mAmbientDisplayConfiguration.pickupGestureEnabled(anyInt())).thenReturn(false);
73 
74         assertThat(mController.isChecked()).isFalse();
75     }
76 
77     @Test
isSuggestionCompleted_ambientDisplayPickup_trueWhenVisited()78     public void isSuggestionCompleted_ambientDisplayPickup_trueWhenVisited() {
79         when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
80         when(mContext.getResources().getString(anyInt())).thenReturn("foo");
81         final Context context = RuntimeEnvironment.application;
82         final SharedPreferences prefs =
83                 new SuggestionFeatureProviderImpl(context).getSharedPrefs(context);
84         prefs.edit().putBoolean(PickupGestureSettings.PREF_KEY_SUGGESTION_COMPLETE, true).commit();
85 
86         assertThat(PickupGesturePreferenceController.isSuggestionComplete(mContext, prefs))
87                 .isTrue();
88     }
89 
90     @Test
getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE()91     public void getAvailabilityStatus_aodNotSupported_UNSUPPORTED_ON_DEVICE() {
92         when(mAmbientDisplayConfiguration.dozePickupSensorAvailable()).thenReturn(false);
93         when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(false);
94         final int availabilityStatus = mController.getAvailabilityStatus();
95 
96         assertThat(availabilityStatus).isEqualTo(UNSUPPORTED_ON_DEVICE);
97     }
98 
99     @Test
getAvailabilityStatus_aodSupported_aodOff_AVAILABLE()100     public void getAvailabilityStatus_aodSupported_aodOff_AVAILABLE() {
101         when(mAmbientDisplayConfiguration.dozePickupSensorAvailable()).thenReturn(true);
102         when(mAmbientDisplayConfiguration.ambientDisplayAvailable()).thenReturn(true);
103         final int availabilityStatus = mController.getAvailabilityStatus();
104 
105         assertThat(availabilityStatus).isEqualTo(AVAILABLE);
106     }
107 
108     @Test
isSliceableCorrectKey_returnsTrue()109     public void isSliceableCorrectKey_returnsTrue() {
110         final PickupGesturePreferenceController controller =
111                 new PickupGesturePreferenceController(mContext, "gesture_pick_up");
112         assertThat(controller.isSliceable()).isTrue();
113     }
114 
115     @Test
isSliceableIncorrectKey_returnsFalse()116     public void isSliceableIncorrectKey_returnsFalse() {
117         final PickupGesturePreferenceController controller =
118                 new PickupGesturePreferenceController(mContext, "bad_key");
119         assertThat(controller.isSliceable()).isFalse();
120     }
121 
122     @Test
isPublicSlice_returnTrue()123     public void isPublicSlice_returnTrue() {
124         assertThat(mController.isPublicSlice()).isTrue();
125     }
126 }
127