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 android.content.Context; 20 import android.provider.Settings; 21 22 import com.android.settings.SettingsRobolectricTestRunner; 23 import com.android.settings.TestConfig; 24 import com.android.settings.testutils.FakeFeatureFactory; 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 java.util.ArrayList; 38 import java.util.List; 39 40 import static android.provider.Settings.Secure.ASSIST_GESTURE_ENABLED; 41 import static com.google.common.truth.Truth.assertThat; 42 import static org.mockito.Matchers.anyInt; 43 import static org.mockito.Mockito.mock; 44 import static org.mockito.Mockito.when; 45 46 @RunWith(SettingsRobolectricTestRunner.class) 47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 48 public class AssistGesturePreferenceControllerTest { 49 50 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 51 private Context mContext; 52 private FakeFeatureFactory mFactory; 53 private AssistGesturePreferenceController mController; 54 55 private static final String KEY_ASSIST = "gesture_assist"; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 FakeFeatureFactory.setupForTest(mContext); 61 mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 62 mController = new AssistGesturePreferenceController(mContext, null, KEY_ASSIST); 63 } 64 65 @Test isAvailable_whenSupported_shouldReturnTrue()66 public void isAvailable_whenSupported_shouldReturnTrue() { 67 when(mFactory.assistGestureFeatureProvider.isSupported(mContext)).thenReturn(true); 68 assertThat(mController.isAvailable()).isTrue(); 69 } 70 71 @Test isAvailable_whenUnsupported_shouldReturnFalse()72 public void isAvailable_whenUnsupported_shouldReturnFalse() { 73 when(mFactory.assistGestureFeatureProvider.isSupported(mContext)).thenReturn(false); 74 assertThat(mController.isAvailable()).isFalse(); 75 } 76 77 @Test testSwitchEnabled_configIsSet_shouldReturnTrue()78 public void testSwitchEnabled_configIsSet_shouldReturnTrue() { 79 // Set the setting to be enabled. 80 final Context context = ShadowApplication.getInstance().getApplicationContext(); 81 Settings.System.putInt(context.getContentResolver(), ASSIST_GESTURE_ENABLED, 1); 82 mController = new AssistGesturePreferenceController(context, null, KEY_ASSIST); 83 84 assertThat(mController.isSwitchPrefEnabled()).isTrue(); 85 } 86 87 @Test testSwitchEnabled_configIsNotSet_shouldReturnFalse()88 public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() { 89 // Set the setting to be disabled. 90 final Context context = ShadowApplication.getInstance().getApplicationContext(); 91 Settings.System.putInt(context.getContentResolver(), ASSIST_GESTURE_ENABLED, 0); 92 mController = new AssistGesturePreferenceController(context, null, KEY_ASSIST); 93 94 assertThat(mController.isSwitchPrefEnabled()).isFalse(); 95 } 96 } 97 98