• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.provider.Settings;
25 import android.provider.Settings.Secure;
26 
27 import com.android.settings.search.InlinePayload;
28 import com.android.settings.search.InlineSwitchPayload;
29 import com.android.settings.search.ResultPayload;
30 import com.android.settings.testutils.FakeFeatureFactory;
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 import com.android.settings.testutils.shadow.ShadowSecureSettings;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Answers;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.annotation.Config;
42 
43 @RunWith(SettingsRobolectricTestRunner.class)
44 @Config(shadows = ShadowSecureSettings.class)
45 public class AssistGestureSettingsPreferenceControllerTest {
46 
47     private static final String KEY_ASSIST = "gesture_assist";
48 
49     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
50     private Context mContext;
51     private FakeFeatureFactory mFactory;
52     private AssistGestureSettingsPreferenceController mController;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         mFactory = FakeFeatureFactory.setupForTest();
58         mController = new AssistGestureSettingsPreferenceController(mContext, KEY_ASSIST);
59         mController.setAssistOnly(false);
60     }
61 
62     @Test
isAvailable_whenSupported_shouldReturnTrue()63     public void isAvailable_whenSupported_shouldReturnTrue() {
64         mController.mAssistOnly = false;
65         when(mFactory.assistGestureFeatureProvider.isSensorAvailable(mContext)).thenReturn(true);
66         assertThat(mController.isAvailable()).isTrue();
67     }
68 
69     @Test
isAvailable_whenUnsupported_shouldReturnFalse()70     public void isAvailable_whenUnsupported_shouldReturnFalse() {
71         when(mFactory.assistGestureFeatureProvider.isSupported(mContext)).thenReturn(false);
72         assertThat(mController.isAvailable()).isFalse();
73     }
74 
75     @Test
testPreferenceController_ProperResultPayloadType()76     public void testPreferenceController_ProperResultPayloadType() {
77         final Context context = RuntimeEnvironment.application;
78         AssistGestureSettingsPreferenceController controller =
79                 new AssistGestureSettingsPreferenceController(context, KEY_ASSIST);
80         controller.setAssistOnly(false);
81         ResultPayload payload = controller.getResultPayload();
82         assertThat(payload).isInstanceOf(InlineSwitchPayload.class);
83     }
84 
85     @Test
testSetValue_updatesCorrectly()86     public void testSetValue_updatesCorrectly() {
87         int newValue = 1;
88         ContentResolver resolver = mContext.getContentResolver();
89         Settings.Secure.putInt(resolver, Secure.ASSIST_GESTURE_ENABLED, 0);
90 
91         ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
92         int updatedValue = Settings.Secure.getInt(resolver, Secure.ASSIST_GESTURE_ENABLED, -1);
93 
94         assertThat(updatedValue).isEqualTo(newValue);
95     }
96 
97     @Test
testGetValue_correctValueReturned()98     public void testGetValue_correctValueReturned() {
99         int currentValue = 1;
100         ContentResolver resolver = mContext.getContentResolver();
101         Settings.Secure.putInt(resolver, Settings.Secure.ASSIST_GESTURE_ENABLED, currentValue);
102 
103         int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
104 
105         assertThat(newValue).isEqualTo(currentValue);
106     }
107 }
108 
109