1 /* 2 * Copyright (C) 2018 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.accessibility; 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.ComponentName; 26 import android.content.ContentResolver; 27 import android.content.Context; 28 import android.content.pm.ResolveInfo; 29 import android.content.pm.ServiceInfo; 30 import android.provider.Settings; 31 import android.view.accessibility.AccessibilityManager; 32 33 import com.android.settings.testutils.SettingsRobolectricTestRunner; 34 import com.android.settingslib.accessibility.AccessibilityUtils; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.robolectric.RuntimeEnvironment; 40 41 import java.io.IOException; 42 import java.util.ArrayList; 43 import java.util.List; 44 45 import org.robolectric.shadow.api.Shadow; 46 import org.robolectric.shadows.ShadowAccessibilityManager; 47 import org.xmlpull.v1.XmlPullParserException; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 public class AccessibilitySlicePreferenceControllerTest { 51 52 private final String PACKAGE_NAME = "com.android.settings.fake"; 53 private final String CLASS_NAME = "com.android.settings.fake.classname"; 54 private final String SERVICE_NAME = PACKAGE_NAME + "/" + CLASS_NAME; 55 56 private Context mContext; 57 58 private AccessibilitySlicePreferenceController mController; 59 60 @Before 61 public void setUp() { 62 mContext = RuntimeEnvironment.application; 63 64 final ContentResolver contentResolver = mContext.getContentResolver(); 65 Settings.Secure.putInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED, 1 /* on */); 66 Settings.Secure.putString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, 67 SERVICE_NAME); 68 69 // Register the fake a11y Service 70 ShadowAccessibilityManager shadowAccessibilityManager = Shadow.extract( 71 RuntimeEnvironment.application.getSystemService(AccessibilityManager.class)); 72 shadowAccessibilityManager.setInstalledAccessibilityServiceList(getFakeServiceList()); 73 74 mController = new AccessibilitySlicePreferenceController(mContext, SERVICE_NAME); 75 } 76 77 @Test 78 public void getAvailability_availableService_returnsAvailable() { 79 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 80 } 81 82 @Test 83 public void getAvailability_unknownService_returnsUnsupported() { 84 AccessibilitySlicePreferenceController controller = 85 new AccessibilitySlicePreferenceController(mContext, "fake_service/name"); 86 87 assertThat(controller.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 88 } 89 90 @Test 91 public void setChecked_availableService_serviceIsEnabled() { 92 mController.setChecked(true); 93 94 assertThat(mController.isChecked()).isTrue(); 95 } 96 97 @Test 98 public void setNotChecked_availableService_serviceIsDisabled() { 99 mController.setChecked(false); 100 101 assertThat(mController.isChecked()).isFalse(); 102 } 103 104 @Test 105 public void isChecked_serviceEnabled_returnsTrue() { 106 AccessibilityUtils.setAccessibilityServiceState(mContext, 107 ComponentName.unflattenFromString(mController.getPreferenceKey()), true); 108 109 assertThat(mController.isChecked()).isTrue(); 110 } 111 112 @Test 113 public void isChecked_serviceNotEnabled_returnsFalse() { 114 AccessibilitySlicePreferenceController controller = 115 new AccessibilitySlicePreferenceController(mContext, "fake_service/name"); 116 117 assertThat(controller.isChecked()).isFalse(); 118 } 119 120 @Test(expected = IllegalArgumentException.class) 121 public void illegalServiceName_exceptionThrown() { 122 new AccessibilitySlicePreferenceController(mContext, "not_split_by_slash"); 123 } 124 125 private List<AccessibilityServiceInfo> getFakeServiceList() { 126 final List<AccessibilityServiceInfo> infoList = new ArrayList<>(); 127 128 final ServiceInfo serviceInfo = new ServiceInfo(); 129 serviceInfo.packageName = PACKAGE_NAME; 130 serviceInfo.name = CLASS_NAME; 131 132 final ResolveInfo resolveInfo = new ResolveInfo(); 133 resolveInfo.serviceInfo = serviceInfo; 134 135 try { 136 final AccessibilityServiceInfo info = new AccessibilityServiceInfo(resolveInfo, 137 mContext); 138 ComponentName componentName = new ComponentName(PACKAGE_NAME, CLASS_NAME); 139 info.setComponentName(componentName); 140 infoList.add(info); 141 } catch (XmlPullParserException | IOException e) { 142 143 } 144 145 return infoList; 146 } 147 } 148