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