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.search; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.when; 22 23 import android.accessibilityservice.AccessibilityServiceInfo; 24 import android.content.Context; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.content.pm.ServiceInfo; 28 import android.graphics.Color; 29 import android.graphics.drawable.ColorDrawable; 30 import android.view.accessibility.AccessibilityManager; 31 32 import com.android.settings.TestConfig; 33 import com.android.settings.dashboard.SiteMapManager; 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Answers; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.annotation.Config; 43 44 import java.util.ArrayList; 45 import java.util.List; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 49 public class AccessibilityServiceResultLoaderTest { 50 51 private static final String QUERY = "test_query"; 52 53 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 54 private Context mContext; 55 @Mock 56 private PackageManager mPackageManager; 57 @Mock 58 private AccessibilityManager mAccessibilityManager; 59 @Mock 60 private SiteMapManager mSiteMapManager; 61 62 private AccessibilityServiceResultLoader mLoader; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 when(mContext.getSystemService(Context.ACCESSIBILITY_SERVICE)) 68 .thenReturn(mAccessibilityManager); 69 when(mContext.getPackageManager()).thenReturn(mPackageManager); 70 71 mLoader = new AccessibilityServiceResultLoader(mContext, QUERY, mSiteMapManager); 72 } 73 74 @Test query_noService_shouldNotReturnAnything()75 public void query_noService_shouldNotReturnAnything() { 76 assertThat(mLoader.loadInBackground()).isEmpty(); 77 } 78 79 @Test query_hasServiceMatchingTitle_shouldReturnResult()80 public void query_hasServiceMatchingTitle_shouldReturnResult() { 81 addFakeAccessibilityService(); 82 83 List<? extends SearchResult> results = new ArrayList<>(mLoader.loadInBackground()); 84 assertThat(results).hasSize(1); 85 86 SearchResult result = results.get(0); 87 assertThat(result.title).isEqualTo(QUERY); 88 } 89 90 @Test query_serviceDoesNotMatchTitle_shouldReturnResult()91 public void query_serviceDoesNotMatchTitle_shouldReturnResult() { 92 addFakeAccessibilityService(); 93 94 mLoader = new AccessibilityServiceResultLoader(mContext, 95 QUERY + "no_match", mSiteMapManager); 96 97 assertThat(mLoader.loadInBackground()).isEmpty(); 98 } 99 addFakeAccessibilityService()100 private void addFakeAccessibilityService() { 101 final List<AccessibilityServiceInfo> services = new ArrayList<>(); 102 final AccessibilityServiceInfo info = mock(AccessibilityServiceInfo.class); 103 final ResolveInfo resolveInfo = mock(ResolveInfo.class); 104 when(info.getResolveInfo()).thenReturn(resolveInfo); 105 when(resolveInfo.loadIcon(mPackageManager)).thenReturn(new ColorDrawable(Color.BLUE)); 106 when(resolveInfo.loadLabel(mPackageManager)).thenReturn(QUERY); 107 resolveInfo.serviceInfo = new ServiceInfo(); 108 resolveInfo.serviceInfo.packageName = "pkg"; 109 resolveInfo.serviceInfo.name = "class"; 110 services.add(info); 111 112 when(mAccessibilityManager.getInstalledAccessibilityServiceList()).thenReturn(services); 113 } 114 } 115