1 /* 2 * Copyright (C) 2019 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.privacy; 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 android.accessibilityservice.AccessibilityServiceInfo; 25 import android.content.Context; 26 import android.view.accessibility.AccessibilityManager; 27 28 import androidx.preference.Preference; 29 30 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 import org.robolectric.annotation.Config; 40 import org.robolectric.shadow.api.Shadow; 41 import org.robolectric.shadows.ShadowAccessibilityManager; 42 43 import java.util.ArrayList; 44 import java.util.Arrays; 45 46 @RunWith(RobolectricTestRunner.class) 47 @Config(shadows = {ShadowDeviceConfig.class}) 48 public class AccessibilityUsagePreferenceControllerTest { 49 50 private Context mContext; 51 private ShadowAccessibilityManager mAccessibilityManager; 52 53 @Before setUp()54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 mContext = RuntimeEnvironment.application; 57 mAccessibilityManager = Shadow.extract( 58 RuntimeEnvironment.application.getSystemService(AccessibilityManager.class)); 59 } 60 61 @After tearDown()62 public void tearDown() { 63 ShadowAccessibilityManager.reset(); 64 ShadowDeviceConfig.reset(); 65 } 66 67 @Test getAvailabilityStatus_noEnabledServices_shouldReturnUnsupported()68 public void getAvailabilityStatus_noEnabledServices_shouldReturnUnsupported() { 69 mAccessibilityManager.setEnabledAccessibilityServiceList(new ArrayList<>()); 70 final AccessibilityUsagePreferenceController controller = 71 new AccessibilityUsagePreferenceController(mContext, "test_key"); 72 73 assertThat(controller.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 74 } 75 76 @Test getAvailabilityStatus_enabledServices_shouldReturnAvailable()77 public void getAvailabilityStatus_enabledServices_shouldReturnAvailable() { 78 mAccessibilityManager.setEnabledAccessibilityServiceList( 79 new ArrayList<>(Arrays.asList(new AccessibilityServiceInfo()))); 80 final AccessibilityUsagePreferenceController controller = 81 new AccessibilityUsagePreferenceController(mContext, "test_key"); 82 83 assertThat(controller.getAvailabilityStatus()).isEqualTo(AVAILABLE); 84 } 85 86 @Test updateState_noEnabledServices_shouldHidePreference()87 public void updateState_noEnabledServices_shouldHidePreference() { 88 mAccessibilityManager.setEnabledAccessibilityServiceList(new ArrayList<>()); 89 final AccessibilityUsagePreferenceController controller = 90 new AccessibilityUsagePreferenceController(mContext, "test_key"); 91 final Preference preference = new Preference(mContext); 92 preference.setVisible(true); 93 94 controller.updateState(preference); 95 96 assertThat(preference.isVisible()).isFalse(); 97 } 98 } 99