Merge "Add A11y Slices" into pi-dev am: bc4c392ff4

am: c82cf95105

Change-Id: Iabff6994e9a4eb67d2702eb949f6e60939db0508
This commit is contained in:
Matthew Fritze
2018-04-17 18:54:46 -07:00
committed by android-build-merger
14 changed files with 467 additions and 49 deletions

View File

@@ -1,2 +1,4 @@
com.android.settings.testutils.FakeToggleController
com.android.settings.testutils.FakeSliderController
com.android.settings.testutils.FakeSliderController
com.android.settings.core.TogglePreferenceControllerTest$FakeToggle
com.android.settings.accessibility.AccessibilitySlicePreferenceController

View File

@@ -62,4 +62,9 @@
<bool name="config_show_wifi_ip_address">false</bool>
<bool name="config_show_wifi_mac_address">false</bool>
<bool name="config_disable_uninstall_update">true</bool>
<!-- List of a11y components on the device allowed to be enabled by Settings Slices -->
<string-array name="config_settings_slices_accessibility_components" translatable="false">
<item>fake_package/fake_service</item>
</string-array>
</resources>

View File

@@ -0,0 +1,148 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.accessibility;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
import static com.google.common.truth.Truth.assertThat;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;
import com.android.settings.accessibility.AccessibilitySlicePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.accessibility.AccessibilityUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowAccessibilityManager;
import org.xmlpull.v1.XmlPullParserException;
@RunWith(SettingsRobolectricTestRunner.class)
public class AccessibilitySlicePreferenceControllerTest {
private final String PACKAGE_NAME = "com.android.settings.fake";
private final String CLASS_NAME = "com.android.settings.fake.classname";
private final String SERVICE_NAME = PACKAGE_NAME + "/" + CLASS_NAME;
private Context mContext;
private AccessibilitySlicePreferenceController mController;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
final ContentResolver contentResolver = mContext.getContentResolver();
Settings.Secure.putInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED, 1 /* on */);
Settings.Secure.putString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
SERVICE_NAME);
// Register the fake a11y Service
ShadowAccessibilityManager shadowAccessibilityManager = Shadow.extract(
RuntimeEnvironment.application.getSystemService(AccessibilityManager.class));
shadowAccessibilityManager.setInstalledAccessibilityServiceList(getFakeServiceList());
mController = new AccessibilitySlicePreferenceController(mContext, SERVICE_NAME);
}
@Test
public void getAvailability_availableService_returnsAvailable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailability_unknownService_returnsUnsupported() {
AccessibilitySlicePreferenceController controller =
new AccessibilitySlicePreferenceController(mContext, "fake_service/name");
assertThat(controller.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
}
@Test
public void setChecked_availableService_serviceIsEnabled() {
mController.setChecked(true);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void setNotChecked_availableService_serviceIsDisabled() {
mController.setChecked(false);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void isChecked_serviceEnabled_returnsTrue() {
AccessibilityUtils.setAccessibilityServiceState(mContext,
ComponentName.unflattenFromString(mController.getPreferenceKey()), true);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void isChecked_serviceNotEnabled_returnsFalse() {
AccessibilitySlicePreferenceController controller =
new AccessibilitySlicePreferenceController(mContext, "fake_service/name");
assertThat(controller.isChecked()).isFalse();
}
@Test(expected = IllegalArgumentException.class)
public void illegalServiceName_exceptionThrown() {
new AccessibilitySlicePreferenceController(mContext, "not_split_by_slash");
}
private List<AccessibilityServiceInfo> getFakeServiceList() {
final List<AccessibilityServiceInfo> infoList = new ArrayList<>();
final ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.packageName = PACKAGE_NAME;
serviceInfo.name = CLASS_NAME;
final ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.serviceInfo = serviceInfo;
try {
final AccessibilityServiceInfo info = new AccessibilityServiceInfo(resolveInfo,
mContext);
ComponentName componentName = new ComponentName(PACKAGE_NAME, CLASS_NAME);
info.setComponentName(componentName);
infoList.add(info);
} catch (XmlPullParserException | IOException e) {
}
return infoList;
}
}

View File

@@ -277,7 +277,7 @@ public class SliceBuilderUtilsTest {
final Pair<Boolean, String> pathPair = SliceBuilderUtils.getPathData(uri);
assertThat(pathPair.first).isFalse();
assertThat(pathPair.first).isTrue();
assertThat(pathPair.second).isEqualTo(KEY);
}
@@ -291,7 +291,7 @@ public class SliceBuilderUtilsTest {
final Pair<Boolean, String> pathPair = SliceBuilderUtils.getPathData(uri);
assertThat(pathPair.first).isTrue();
assertThat(pathPair.first).isFalse();
assertThat(pathPair.second).isEqualTo(KEY);
}
@@ -318,7 +318,7 @@ public class SliceBuilderUtilsTest {
final Pair<Boolean, String> pathPair = SliceBuilderUtils.getPathData(uri);
assertThat(pathPair.first).isTrue();
assertThat(pathPair.first).isFalse();
assertThat(pathPair.second).isEqualTo(KEY + "/" + KEY);
}

View File

@@ -17,8 +17,22 @@
package com.android.settings.slices;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.text.TextUtils;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilitySettings;
import com.android.settings.accessibility.AccessibilitySlicePreferenceController;
import com.android.settings.search.FakeIndexProvider;
import com.android.settings.search.SearchFeatureProvider;
import com.android.settings.search.SearchFeatureProviderImpl;
@@ -32,17 +46,29 @@ import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
public class SliceDataConverterTest {
private final String fakeKey = "key";
private final String fakeTitle = "title";
private final String fakeSummary = "summary";
private final String fakeScreenTitle = "screen_title";
private final String fakeFragmentClassName = FakeIndexProvider.class.getName();
private final String fakeControllerName = FakePreferenceController.class.getName();
private final String FAKE_KEY = "key";
private final String FAKE_TITLE = "title";
private final String FAKE_SUMMARY = "summary";
private final String FAKE_SCREEN_TITLE = "screen_title";
private final String FAKE_FRAGMENT_CLASSNAME = FakeIndexProvider.class.getName();
private final String FAKE_CONTROLLER_NAME = FakePreferenceController.class.getName();
private final String ACCESSIBILITY_FRAGMENT = AccessibilitySettings.class.getName();
private final String A11Y_CONTROLLER_NAME =
AccessibilitySlicePreferenceController.class.getName();
private final String FAKE_SERVICE_NAME = "fake_service";
private final String FAKE_ACCESSIBILITY_PACKAGE = "fake_package";
private final String FAKE_A11Y_SERVICE_NAME =
FAKE_ACCESSIBILITY_PACKAGE + "/" + FAKE_SERVICE_NAME;
private final int FAKE_ICON = 1234;
private Context mContext;
private SliceDataConverter mSliceDataConverter;
private SearchFeatureProvider mSearchFeatureProvider;
@@ -50,7 +76,8 @@ public class SliceDataConverterTest {
@Before
public void setUp() {
mSliceDataConverter = new SliceDataConverter(RuntimeEnvironment.application);
mContext = RuntimeEnvironment.application;
mSliceDataConverter = spy(new SliceDataConverter(RuntimeEnvironment.application));
mSearchFeatureProvider = new SearchFeatureProviderImpl();
mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
mFakeFeatureFactory.searchFeatureProvider = mSearchFeatureProvider;
@@ -68,20 +95,64 @@ public class SliceDataConverterTest {
mSearchFeatureProvider.getSearchIndexableResources().getProviderValues()
.add(FakeIndexProvider.class);
doReturn(getFakeService()).when(mSliceDataConverter).getAccessibilityServiceInfoList();
List<SliceData> sliceDataList = mSliceDataConverter.getSliceData();
assertThat(sliceDataList).hasSize(1);
SliceData fakeSlice = sliceDataList.get(0);
assertThat(sliceDataList).hasSize(2);
SliceData fakeSlice0 = sliceDataList.get(0);
SliceData fakeSlice1 = sliceDataList.get(1);
assertThat(fakeSlice.getKey()).isEqualTo(fakeKey);
assertThat(fakeSlice.getTitle()).isEqualTo(fakeTitle);
assertThat(fakeSlice.getSummary()).isEqualTo(fakeSummary);
assertThat(fakeSlice.getScreenTitle()).isEqualTo(fakeScreenTitle);
// Should not assume the order of the data list.
if (TextUtils.equals(fakeSlice0.getKey(), FAKE_KEY)) {
assertFakeSlice(fakeSlice0);
assertFakeA11ySlice(fakeSlice1);
} else {
assertFakeSlice(fakeSlice1);
assertFakeA11ySlice(fakeSlice0);
}
}
private void assertFakeSlice(SliceData fakeSlice) {
assertThat(fakeSlice.getKey()).isEqualTo(FAKE_KEY);
assertThat(fakeSlice.getTitle()).isEqualTo(FAKE_TITLE);
assertThat(fakeSlice.getSummary()).isEqualTo(FAKE_SUMMARY);
assertThat(fakeSlice.getScreenTitle()).isEqualTo(FAKE_SCREEN_TITLE);
assertThat(fakeSlice.getIconResource()).isNotNull();
assertThat(fakeSlice.getUri()).isNull();
assertThat(fakeSlice.getFragmentClassName()).isEqualTo(fakeFragmentClassName);
assertThat(fakeSlice.getPreferenceController()).isEqualTo(fakeControllerName);
assertThat(fakeSlice.getSliceType()).isEqualTo(SliceData.SliceType.SLIDER); // from XML
assertThat(fakeSlice.getFragmentClassName()).isEqualTo(FAKE_FRAGMENT_CLASSNAME);
assertThat(fakeSlice.getPreferenceController()).isEqualTo(FAKE_CONTROLLER_NAME);
assertThat(fakeSlice.getSliceType()).isEqualTo(SliceData.SliceType.SLIDER);
assertThat(fakeSlice.isPlatformDefined()).isTrue(); // from XML
}
private void assertFakeA11ySlice(SliceData fakeSlice) {
assertThat(fakeSlice.getKey()).isEqualTo(FAKE_A11Y_SERVICE_NAME);
assertThat(fakeSlice.getTitle()).isEqualTo(FAKE_TITLE);
assertThat(fakeSlice.getSummary()).isNull();
assertThat(fakeSlice.getScreenTitle()).isEqualTo(
mContext.getString(R.string.accessibility_settings));
assertThat(fakeSlice.getIconResource()).isEqualTo(FAKE_ICON);
assertThat(fakeSlice.getUri()).isNull();
assertThat(fakeSlice.getFragmentClassName()).isEqualTo(ACCESSIBILITY_FRAGMENT);
assertThat(fakeSlice.getPreferenceController()).isEqualTo(A11Y_CONTROLLER_NAME);
}
// This is fragile. Should be replaced by a proper fake Service if possible.
private List<AccessibilityServiceInfo> getFakeService() {
List<AccessibilityServiceInfo> serviceInfoList = new ArrayList<>();
AccessibilityServiceInfo serviceInfo = spy(new AccessibilityServiceInfo());
ResolveInfo resolveInfo = spy(new ResolveInfo());
resolveInfo.serviceInfo = new ServiceInfo();
resolveInfo.serviceInfo.name = FAKE_SERVICE_NAME;
resolveInfo.serviceInfo.packageName = FAKE_ACCESSIBILITY_PACKAGE;
doReturn(FAKE_TITLE).when(resolveInfo).loadLabel(any(PackageManager.class));
doReturn(FAKE_ICON).when(resolveInfo).getIconResource();
doReturn(resolveInfo).when(serviceInfo).getResolveInfo();
serviceInfoList.add(serviceInfo);
return serviceInfoList;
}
}