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.android.settings.core.BasePreferenceController.AVAILABLE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 26 import android.app.Activity; 27 import android.app.admin.DevicePolicyManager; 28 import android.content.Context; 29 30 import com.android.settings.testutils.FakeFeatureFactory; 31 import com.android.settingslib.core.AbstractPreferenceController; 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 import org.robolectric.util.ReflectionHelpers; 42 43 import java.util.ArrayList; 44 import java.util.List; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class GesturesSettingsPreferenceControllerTest { 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private Activity mActivity; 51 52 private GesturesSettingPreferenceController mController; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 doReturn(mock(DevicePolicyManager.class)).when(mActivity) 58 .getSystemService(Context.DEVICE_POLICY_SERVICE); 59 FakeFeatureFactory.setupForTest(); 60 mController = new GesturesSettingPreferenceController(mActivity, "test_key"); 61 } 62 63 @Test isAvailable_hasGesture_shouldReturnTrue()64 public void isAvailable_hasGesture_shouldReturnTrue() { 65 final List<AbstractPreferenceController> mControllers = new ArrayList<>(); 66 mControllers.add(new AbstractPreferenceController(RuntimeEnvironment.application) { 67 @Override 68 public boolean isAvailable() { 69 return true; 70 } 71 72 @Override 73 public String getPreferenceKey() { 74 return "test_key"; 75 } 76 }); 77 ReflectionHelpers.setField(mController, "mGestureControllers", mControllers); 78 79 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 80 } 81 82 @Test isAvailable_noGesture_shouldReturnFalse()83 public void isAvailable_noGesture_shouldReturnFalse() { 84 ReflectionHelpers.setField(mController, "mGestureControllers", 85 new ArrayList<AbstractPreferenceController>()); 86 87 assertThat(mController.isAvailable()).isFalse(); 88 } 89 } 90