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.slices; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.spy; 25 26 import android.accessibilityservice.AccessibilityServiceInfo; 27 import android.content.Context; 28 import android.content.pm.PackageManager; 29 import android.content.pm.ResolveInfo; 30 import android.content.pm.ServiceInfo; 31 import android.text.TextUtils; 32 33 import com.android.settings.R; 34 import com.android.settings.accessibility.AccessibilitySettings; 35 import com.android.settings.accessibility.AccessibilitySlicePreferenceController; 36 import com.android.settings.search.SearchFeatureProvider; 37 import com.android.settings.search.SearchFeatureProviderImpl; 38 import com.android.settings.testutils.FakeFeatureFactory; 39 import com.android.settings.testutils.FakeIndexProvider; 40 import com.android.settingslib.search.SearchIndexableData; 41 42 import org.junit.After; 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.RuntimeEnvironment; 48 import org.robolectric.annotation.Config; 49 50 import java.util.ArrayList; 51 import java.util.List; 52 53 @RunWith(RobolectricTestRunner.class) 54 public class SliceDataConverterTest { 55 56 private static final String FAKE_KEY = "key"; 57 private static final String FAKE_TITLE = "title"; 58 private static final String FAKE_SUMMARY = "summary"; 59 private static final String FAKE_SCREEN_TITLE = "screen_title"; 60 private static final String FAKE_FRAGMENT_CLASSNAME = FakeIndexProvider.class.getName(); 61 private static final String FAKE_CONTROLLER_NAME = FakePreferenceController.class.getName(); 62 private static final String ACCESSIBILITY_FRAGMENT = AccessibilitySettings.class.getName(); 63 private static final String A11Y_CONTROLLER_NAME = 64 AccessibilitySlicePreferenceController.class.getName(); 65 private static final String FAKE_SERVICE_NAME = "fake_service"; 66 private static final String FAKE_ACCESSIBILITY_PACKAGE = "fake_package"; 67 private static final String FAKE_A11Y_SERVICE_NAME = 68 FAKE_ACCESSIBILITY_PACKAGE + "/" + FAKE_SERVICE_NAME; 69 private static final int FAKE_ICON = 1234; 70 71 private Context mContext; 72 73 private SliceDataConverter mSliceDataConverter; 74 private SearchFeatureProvider mSearchFeatureProvider; 75 private FakeFeatureFactory mFakeFeatureFactory; 76 77 @Before setUp()78 public void setUp() { 79 mContext = RuntimeEnvironment.application; 80 mSliceDataConverter = spy(new SliceDataConverter(RuntimeEnvironment.application)); 81 mSearchFeatureProvider = new SearchFeatureProviderImpl(); 82 mFakeFeatureFactory = FakeFeatureFactory.setupForTest(); 83 mFakeFeatureFactory.searchFeatureProvider = mSearchFeatureProvider; 84 } 85 86 @After cleanUp()87 public void cleanUp() { 88 mFakeFeatureFactory.searchFeatureProvider = mock(SearchFeatureProvider.class); 89 } 90 91 @Test 92 @Config(qualifiers = "mcc999") testFakeProvider_convertsFakeData()93 public void testFakeProvider_convertsFakeData() { 94 mSearchFeatureProvider.getSearchIndexableResources().getProviderValues().clear(); 95 mSearchFeatureProvider.getSearchIndexableResources().getProviderValues() 96 .add(new SearchIndexableData(FakeIndexProvider.class, 97 FakeIndexProvider.SEARCH_INDEX_DATA_PROVIDER)); 98 99 doReturn(getFakeService()).when(mSliceDataConverter).getAccessibilityServiceInfoList(); 100 101 List<SliceData> sliceDataList = mSliceDataConverter.getSliceData(); 102 103 assertThat(sliceDataList).hasSize(2); 104 SliceData fakeSlice0 = sliceDataList.get(0); 105 SliceData fakeSlice1 = sliceDataList.get(1); 106 107 // Should not assume the order of the data list. 108 if (TextUtils.equals(fakeSlice0.getKey(), FAKE_KEY)) { 109 assertFakeSlice(fakeSlice0); 110 assertFakeA11ySlice(fakeSlice1); 111 } else { 112 assertFakeSlice(fakeSlice1); 113 assertFakeA11ySlice(fakeSlice0); 114 } 115 } 116 assertFakeSlice(SliceData fakeSlice)117 private void assertFakeSlice(SliceData fakeSlice) { 118 assertThat(fakeSlice.getKey()).isEqualTo(FAKE_KEY); 119 assertThat(fakeSlice.getTitle()).isEqualTo(FAKE_TITLE); 120 assertThat(fakeSlice.getSummary()).isEqualTo(FAKE_SUMMARY); 121 assertThat(fakeSlice.getScreenTitle()).isEqualTo(FAKE_SCREEN_TITLE); 122 assertThat(fakeSlice.getKeywords()).isNull(); 123 assertThat(fakeSlice.getIconResource()).isNotNull(); 124 assertThat(fakeSlice.getUri().toSafeString()) 125 .isEqualTo("content://com.android.settings.slices/action/key"); 126 assertThat(fakeSlice.getFragmentClassName()).isEqualTo(FAKE_FRAGMENT_CLASSNAME); 127 assertThat(fakeSlice.getPreferenceController()).isEqualTo(FAKE_CONTROLLER_NAME); 128 assertThat(fakeSlice.getSliceType()).isEqualTo(SliceData.SliceType.SLIDER); 129 assertThat(fakeSlice.getUnavailableSliceSubtitle()).isEqualTo( 130 "subtitleOfUnavailableSlice"); // from XML 131 assertThat(fakeSlice.isPublicSlice()).isTrue(); 132 } 133 assertFakeA11ySlice(SliceData fakeSlice)134 private void assertFakeA11ySlice(SliceData fakeSlice) { 135 assertThat(fakeSlice.getKey()).isEqualTo(FAKE_A11Y_SERVICE_NAME); 136 assertThat(fakeSlice.getTitle()).isEqualTo(FAKE_TITLE); 137 assertThat(fakeSlice.getSummary()).isNull(); 138 assertThat(fakeSlice.getScreenTitle()).isEqualTo( 139 mContext.getString(R.string.accessibility_settings)); 140 assertThat(fakeSlice.getIconResource()).isEqualTo(FAKE_ICON); 141 assertThat(fakeSlice.getUri()).isNotNull(); 142 assertThat(fakeSlice.getFragmentClassName()).isEqualTo(ACCESSIBILITY_FRAGMENT); 143 assertThat(fakeSlice.getPreferenceController()).isEqualTo(A11Y_CONTROLLER_NAME); 144 } 145 146 // This is fragile. Should be replaced by a proper fake Service if possible. getFakeService()147 private List<AccessibilityServiceInfo> getFakeService() { 148 List<AccessibilityServiceInfo> serviceInfoList = new ArrayList<>(); 149 AccessibilityServiceInfo serviceInfo = spy(new AccessibilityServiceInfo()); 150 151 ResolveInfo resolveInfo = spy(new ResolveInfo()); 152 resolveInfo.serviceInfo = new ServiceInfo(); 153 resolveInfo.serviceInfo.name = FAKE_SERVICE_NAME; 154 resolveInfo.serviceInfo.packageName = FAKE_ACCESSIBILITY_PACKAGE; 155 doReturn(FAKE_TITLE).when(resolveInfo).loadLabel(any(PackageManager.class)); 156 doReturn(FAKE_ICON).when(resolveInfo).getIconResource(); 157 158 doReturn(resolveInfo).when(serviceInfo).getResolveInfo(); 159 serviceInfoList.add(serviceInfo); 160 161 return serviceInfoList; 162 } 163 }