1 /* 2 * Copyright (C) 2022 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.server.appsearch; 18 19 import static com.android.server.appsearch.appsindexer.AppsIndexerConfig.DEFAULT_APPS_INDEXER_ENABLED; 20 import static com.android.server.appsearch.contactsindexer.ContactsIndexerConfig.DEFAULT_CONTACTS_INDEXER_ENABLED; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.junit.Assert.assertNull; 25 import static org.mockito.Mockito.doNothing; 26 import static org.mockito.Mockito.doThrow; 27 import static org.mockito.Mockito.mock; 28 import static org.mockito.Mockito.verify; 29 30 import android.annotation.NonNull; 31 import android.content.Context; 32 import android.content.pm.UserInfo; 33 import android.provider.DeviceConfig; 34 35 import androidx.test.core.app.ApplicationProvider; 36 37 import com.android.dx.mockito.inline.extended.ExtendedMockito; 38 import com.android.server.SystemService.TargetUser; 39 import com.android.server.appsearch.AppSearchModule.Lifecycle; 40 import com.android.server.appsearch.appsindexer.AppsIndexerConfig; 41 import com.android.server.appsearch.appsindexer.AppsIndexerManagerService; 42 import com.android.server.appsearch.contactsindexer.ContactsIndexerConfig; 43 import com.android.server.appsearch.contactsindexer.ContactsIndexerManagerService; 44 45 import org.junit.After; 46 import org.junit.Before; 47 import org.junit.Test; 48 import org.mockito.MockitoSession; 49 import org.mockito.quality.Strictness; 50 51 public class AppSearchModuleTest { 52 private static final String NAMESPACE_APPSEARCH = "appsearch"; 53 private static final String KEY_CONTACTS_INDEXER_ENABLED = "contacts_indexer_enabled"; 54 private static final String KEY_APPS_INDEXER_ENABLED = "apps_indexer_enabled"; 55 56 private final ContactsIndexerManagerService mContactsIndexerService = 57 mock(ContactsIndexerManagerService.class); 58 private final AppsIndexerManagerService mAppsIndexerService = 59 mock(AppsIndexerManagerService.class); 60 private final AppSearchManagerService mAppSearchService = mock(AppSearchManagerService.class); 61 62 private TargetUser mUser; 63 private Lifecycle mLifecycle; 64 private MockitoSession mMockitoSession; 65 66 @Before setUp()67 public void setUp() { 68 mMockitoSession = 69 ExtendedMockito.mockitoSession() 70 .mockStatic(DeviceConfig.class) 71 .strictness(Strictness.LENIENT) 72 .startMocking(); 73 Context context = ApplicationProvider.getApplicationContext(); 74 UserInfo userInfo = new UserInfo(context.getUserId(), "default", 0); 75 mUser = new TargetUser(userInfo); 76 77 mLifecycle = 78 new Lifecycle(context) { 79 @NonNull 80 @Override 81 AppsIndexerManagerService createAppsIndexerManagerService( 82 @NonNull Context context, @NonNull AppsIndexerConfig config) { 83 return mAppsIndexerService; 84 } 85 86 @NonNull 87 @Override 88 ContactsIndexerManagerService createContactsIndexerManagerService( 89 @NonNull Context context, @NonNull ContactsIndexerConfig config) { 90 return mContactsIndexerService; 91 } 92 93 @NonNull 94 @Override 95 AppSearchManagerService createAppSearchManagerService( 96 @NonNull Context context, @NonNull Lifecycle lifecycle) { 97 return mAppSearchService; 98 } 99 }; 100 101 ExtendedMockito.doReturn(true) 102 .when( 103 () -> 104 DeviceConfig.getBoolean( 105 NAMESPACE_APPSEARCH, 106 KEY_CONTACTS_INDEXER_ENABLED, 107 DEFAULT_CONTACTS_INDEXER_ENABLED)); 108 ExtendedMockito.doReturn(true) 109 .when( 110 () -> 111 DeviceConfig.getBoolean( 112 NAMESPACE_APPSEARCH, 113 KEY_APPS_INDEXER_ENABLED, 114 DEFAULT_APPS_INDEXER_ENABLED)); 115 } 116 117 @After tearDown()118 public void tearDown() { 119 mMockitoSession.finishMocking(); 120 } 121 122 @Test testBothIndexersEnabled()123 public void testBothIndexersEnabled() { 124 mLifecycle.onStart(); 125 assertThat(mLifecycle.mAppsIndexerManagerService).isNotNull(); 126 assertThat(mLifecycle.mContactsIndexerManagerService).isNotNull(); 127 128 mLifecycle.onUserUnlocking(mUser); 129 verify(mContactsIndexerService).onUserUnlocking(mUser); 130 verify(mAppsIndexerService).onUserUnlocking(mUser); 131 132 mLifecycle.onUserStopping(mUser); 133 verify(mContactsIndexerService).onUserStopping(mUser); 134 verify(mAppsIndexerService).onUserStopping(mUser); 135 } 136 137 @Test testContactsIndexerDisabled()138 public void testContactsIndexerDisabled() { 139 ExtendedMockito.doReturn(false) 140 .when( 141 () -> 142 DeviceConfig.getBoolean( 143 NAMESPACE_APPSEARCH, 144 KEY_CONTACTS_INDEXER_ENABLED, 145 DEFAULT_CONTACTS_INDEXER_ENABLED)); 146 147 mLifecycle.onStart(); 148 assertNull(mLifecycle.mContactsIndexerManagerService); 149 150 mLifecycle.onUserUnlocking(mUser); 151 verify(mAppsIndexerService).onUserUnlocking(mUser); 152 assertNull(mLifecycle.mContactsIndexerManagerService); 153 154 mLifecycle.onUserStopping(mUser); 155 verify(mAppsIndexerService).onUserStopping(mUser); 156 assertNull(mLifecycle.mContactsIndexerManagerService); 157 } 158 159 @Test testAppsIndexerDisabled()160 public void testAppsIndexerDisabled() { 161 ExtendedMockito.doReturn(false) 162 .when( 163 () -> 164 DeviceConfig.getBoolean( 165 NAMESPACE_APPSEARCH, 166 KEY_APPS_INDEXER_ENABLED, 167 DEFAULT_APPS_INDEXER_ENABLED)); 168 169 mLifecycle.onStart(); 170 assertNull(mLifecycle.mAppsIndexerManagerService); 171 172 mLifecycle.onUserUnlocking(mUser); 173 verify(mContactsIndexerService).onUserUnlocking(mUser); 174 assertNull(mLifecycle.mAppsIndexerManagerService); 175 176 mLifecycle.onUserStopping(mUser); 177 verify(mContactsIndexerService).onUserStopping(mUser); 178 assertNull(mLifecycle.mAppsIndexerManagerService); 179 } 180 181 @Test testServicesSetToNullWhenDisabled()182 public void testServicesSetToNullWhenDisabled() { 183 ExtendedMockito.doReturn(false) 184 .when( 185 () -> 186 DeviceConfig.getBoolean( 187 NAMESPACE_APPSEARCH, 188 KEY_CONTACTS_INDEXER_ENABLED, 189 DEFAULT_CONTACTS_INDEXER_ENABLED)); 190 ExtendedMockito.doReturn(false) 191 .when( 192 () -> 193 DeviceConfig.getBoolean( 194 NAMESPACE_APPSEARCH, 195 KEY_APPS_INDEXER_ENABLED, 196 DEFAULT_APPS_INDEXER_ENABLED)); 197 198 mLifecycle.onStart(); 199 assertNull(mLifecycle.mContactsIndexerManagerService); 200 assertNull(mLifecycle.mAppsIndexerManagerService); 201 202 mLifecycle.onUserUnlocking(mUser); 203 assertNull(mLifecycle.mContactsIndexerManagerService); 204 assertNull(mLifecycle.mAppsIndexerManagerService); 205 206 mLifecycle.onUserStopping(mUser); 207 assertNull(mLifecycle.mContactsIndexerManagerService); 208 assertNull(mLifecycle.mAppsIndexerManagerService); 209 } 210 211 @Test testIndexerOnStart_clearsService()212 public void testIndexerOnStart_clearsService() { 213 // Setup AppsIndexerManagerService to throw an error on start 214 doThrow(new RuntimeException("Apps indexer exception")).when(mAppsIndexerService).onStart(); 215 216 mLifecycle.onStart(); 217 assertThat(mLifecycle.mAppsIndexerManagerService).isNull(); 218 assertThat(mLifecycle.mContactsIndexerManagerService).isNotNull(); 219 220 // Setup ContactsIndexerManagerService to throw an error on start 221 doNothing().when(mAppsIndexerService).onStart(); 222 doThrow(new RuntimeException("Contacts indexer exception")) 223 .when(mContactsIndexerService) 224 .onStart(); 225 226 mLifecycle.onStart(); 227 assertThat(mLifecycle.mAppsIndexerManagerService).isNotNull(); 228 assertThat(mLifecycle.mContactsIndexerManagerService).isNull(); 229 } 230 } 231