Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.car.vms; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
| 24 | import static org.mockito.ArgumentMatchers.isNull; |
| 25 | import static org.mockito.Mockito.atLeast; |
| 26 | import static org.mockito.Mockito.reset; |
| 27 | import static org.mockito.Mockito.times; |
| 28 | import static org.mockito.Mockito.verify; |
| 29 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 30 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
| 33 | import android.car.userlib.CarUserManagerHelper; |
| 34 | import android.content.ComponentName; |
| 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
| 38 | import android.content.ServiceConnection; |
| 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.UserInfo; |
| 41 | import android.content.res.Resources; |
| 42 | import android.os.Binder; |
| 43 | import android.os.Handler; |
| 44 | import android.os.UserHandle; |
| 45 | |
| 46 | import androidx.test.filters.SmallTest; |
| 47 | import androidx.test.runner.AndroidJUnit4; |
| 48 | |
| 49 | import org.junit.After; |
| 50 | import org.junit.Before; |
| 51 | import org.junit.Rule; |
| 52 | import org.junit.Test; |
| 53 | import org.junit.runner.RunWith; |
| 54 | import org.mockito.ArgumentCaptor; |
| 55 | import org.mockito.Captor; |
| 56 | import org.mockito.Mock; |
| 57 | import org.mockito.junit.MockitoJUnit; |
| 58 | import org.mockito.junit.MockitoRule; |
| 59 | |
| 60 | @RunWith(AndroidJUnit4.class) |
| 61 | @SmallTest |
| 62 | public class VmsClientManagerTest { |
| 63 | @Rule |
| 64 | public MockitoRule mMockitoRule = MockitoJUnit.rule(); |
| 65 | @Mock |
| 66 | private Context mContext; |
| 67 | @Mock |
| 68 | private PackageManager mPackageManager; |
| 69 | @Mock |
| 70 | private Resources mResources; |
| 71 | @Mock |
| 72 | private CarUserManagerHelper mUserManager; |
| 73 | private UserInfo mUserInfo; |
| 74 | |
| 75 | @Mock |
| 76 | private VmsClientManager.ConnectionListener mConnectionListener; |
| 77 | private VmsClientManager mClientManager; |
| 78 | |
| 79 | @Captor |
| 80 | private ArgumentCaptor<ServiceConnection> mConnectionCaptor; |
| 81 | |
| 82 | @Before |
| 83 | public void setUp() { |
| 84 | resetContext(); |
| 85 | when(mPackageManager.isPackageAvailable(any())).thenReturn(true); |
| 86 | |
| 87 | when(mResources.getInteger( |
| 88 | com.android.car.R.integer.millisecondsBeforeRebindToVmsPublisher)).thenReturn( |
| 89 | 5); |
| 90 | when(mResources.getStringArray( |
| 91 | com.android.car.R.array.vmsPublisherSystemClients)).thenReturn( |
| 92 | new String[]{ |
| 93 | "com.google.android.apps.vms.test/.VmsSystemClient" |
| 94 | }); |
| 95 | when(mResources.getStringArray( |
| 96 | com.android.car.R.array.vmsPublisherUserClients)).thenReturn( |
| 97 | new String[]{ |
| 98 | "com.google.android.apps.vms.test/.VmsUserClient" |
| 99 | }); |
| 100 | mUserInfo = new UserInfo(10, "Driver", 0); |
| 101 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 102 | |
| 103 | mClientManager = new VmsClientManager(mContext, mUserManager); |
| 104 | mClientManager.registerConnectionListener(mConnectionListener); |
| 105 | } |
| 106 | |
| 107 | @After |
| 108 | public void tearDown() throws Exception { |
| 109 | Thread.sleep(10); // Time to allow for delayed rebinds to settle |
| 110 | verify(mContext, atLeast(0)).getResources(); |
| 111 | verify(mContext, atLeast(0)).getPackageManager(); |
| 112 | verifyNoMoreInteractions(mContext); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testInit() { |
| 117 | mClientManager.init(); |
| 118 | |
| 119 | // Verify registration of boot completed receiver |
| 120 | ArgumentCaptor<IntentFilter> bootFilterCaptor = ArgumentCaptor.forClass(IntentFilter.class); |
| 121 | verify(mContext).registerReceiver(eq(mClientManager.mBootCompletedReceiver), |
| 122 | bootFilterCaptor.capture()); |
| 123 | IntentFilter bootFilter = bootFilterCaptor.getValue(); |
| 124 | assertEquals(1, bootFilter.countActions()); |
| 125 | assertTrue(bootFilter.hasAction(Intent.ACTION_LOCKED_BOOT_COMPLETED)); |
| 126 | |
| 127 | // Verify registration of user switch receiver |
| 128 | ArgumentCaptor<IntentFilter> userFilterCaptor = ArgumentCaptor.forClass(IntentFilter.class); |
| 129 | verify(mContext).registerReceiverAsUser(eq(mClientManager.mUserSwitchReceiver), |
| 130 | eq(UserHandle.ALL), userFilterCaptor.capture(), isNull(), isNull()); |
| 131 | IntentFilter userEventFilter = userFilterCaptor.getValue(); |
| 132 | assertEquals(2, userEventFilter.countActions()); |
| 133 | assertTrue(userEventFilter.hasAction(Intent.ACTION_USER_SWITCHED)); |
| 134 | assertTrue(userEventFilter.hasAction(Intent.ACTION_USER_UNLOCKED)); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testRelease() { |
| 139 | mClientManager.release(); |
| 140 | |
| 141 | // Verify both receivers are unregistered |
| 142 | verify(mContext).unregisterReceiver(mClientManager.mBootCompletedReceiver); |
| 143 | verify(mContext).unregisterReceiver(mClientManager.mUserSwitchReceiver); |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void testLockedBootCompleted() { |
| 148 | notifyLockedBootCompleted(); |
| 149 | notifyLockedBootCompleted(); |
| 150 | |
| 151 | // Multiple events should only trigger a single bind, when successful |
| 152 | verifySystemBind(1); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testLockedBootCompleted_BindFailed() { |
| 157 | when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenReturn(false); |
| 158 | notifyLockedBootCompleted(); |
| 159 | notifyLockedBootCompleted(); |
| 160 | |
| 161 | // Failure state will trigger another attempt on event |
| 162 | verifySystemBind(2); |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void testLockedBootCompleted_BindException() { |
| 167 | when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenThrow( |
| 168 | new SecurityException()); |
| 169 | notifyLockedBootCompleted(); |
| 170 | notifyLockedBootCompleted(); |
| 171 | |
| 172 | // Failure state will trigger another attempt on event |
| 173 | verifySystemBind(2); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testUserSwitched() { |
| 178 | notifyUserSwitched(); |
| 179 | notifyUserSwitched(); |
| 180 | |
| 181 | // Multiple events should only trigger a single bind, when successful |
| 182 | verifyUserBind(1); |
| 183 | } |
| 184 | |
| 185 | @Test |
| 186 | public void testUserSwitched_BindFailed() { |
| 187 | when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenReturn(false); |
| 188 | notifyUserSwitched(); |
| 189 | notifyUserSwitched(); |
| 190 | |
| 191 | // Failure state will trigger another attempt on event |
| 192 | verifyUserBind(2); |
| 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void testUserSwitched_BindException() { |
| 197 | when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenThrow( |
| 198 | new SecurityException()); |
| 199 | notifyUserSwitched(); |
| 200 | notifyUserSwitched(); |
| 201 | |
| 202 | // Failure state will trigger another attempt on event |
| 203 | verifyUserBind(2); |
| 204 | } |
| 205 | |
| 206 | @Test |
| 207 | public void testUserUnlocked() { |
| 208 | notifyUserUnlocked(); |
| 209 | notifyUserUnlocked(); |
| 210 | |
| 211 | // Multiple events should only trigger a single bind, when successful |
| 212 | verifyUserBind(1); |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testUserUnlocked_BindFailed() { |
| 217 | when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenReturn(false); |
| 218 | notifyUserUnlocked(); |
| 219 | notifyUserUnlocked(); |
| 220 | |
| 221 | // Failure state will trigger another attempt on event |
| 222 | verifyUserBind(2); |
| 223 | } |
| 224 | |
| 225 | @Test |
| 226 | public void testUserUnlocked_BindException() { |
| 227 | when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenThrow( |
| 228 | new SecurityException()); |
| 229 | notifyUserUnlocked(); |
| 230 | notifyUserUnlocked(); |
| 231 | |
| 232 | // Failure state will trigger another attempt on event |
| 233 | verifyUserBind(2); |
| 234 | } |
| 235 | |
| 236 | @Test |
| 237 | public void testUserSwitchedAndUnlocked() { |
| 238 | notifyUserSwitched(); |
| 239 | notifyUserUnlocked(); |
| 240 | |
| 241 | // Multiple events should only trigger a single bind, when successful |
| 242 | verifyUserBind(1); |
| 243 | } |
| 244 | |
| 245 | @Test |
| 246 | public void testUserSwitchedToSystemUser() { |
| 247 | mUserInfo = new UserInfo(UserHandle.USER_SYSTEM, "Owner", 0); |
| 248 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 249 | notifyUserSwitched(); |
| 250 | |
| 251 | // System user should not trigger any binding |
| 252 | verifyUserBind(0); |
| 253 | } |
| 254 | |
| 255 | @Test |
| 256 | public void testUnregisterConnectionListener() { |
| 257 | mClientManager.unregisterConnectionListener(mConnectionListener); |
| 258 | notifyLockedBootCompleted(); |
| 259 | verifySystemBind(1); |
| 260 | |
| 261 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 262 | connection.onServiceConnected(null, new Binder()); |
| 263 | verifyZeroInteractions(mConnectionListener); |
| 264 | } |
| 265 | |
| 266 | @Test |
| 267 | public void testOnSystemServiceConnected() { |
| 268 | notifyLockedBootCompleted(); |
| 269 | verifySystemBind(1); |
| 270 | resetContext(); |
| 271 | |
| 272 | Binder binder = new Binder(); |
| 273 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 274 | connection.onServiceConnected(null, binder); |
| 275 | |
| 276 | verify(mConnectionListener).onClientConnected( |
| 277 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 278 | + ".VmsSystemClient U=0"), |
| 279 | eq(binder)); |
| 280 | } |
| 281 | |
| 282 | @Test |
| 283 | public void testOnUserServiceConnected() { |
| 284 | notifyUserSwitched(); |
| 285 | verifyUserBind(1); |
| 286 | resetContext(); |
| 287 | |
| 288 | Binder binder = new Binder(); |
| 289 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 290 | connection.onServiceConnected(null, binder); |
| 291 | |
| 292 | verify(mConnectionListener).onClientConnected( |
| 293 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 294 | + ".VmsUserClient U=10"), |
| 295 | eq(binder)); |
| 296 | } |
| 297 | |
| 298 | @Test |
| 299 | public void testOnSystemServiceDisconnected() throws Exception { |
| 300 | notifyLockedBootCompleted(); |
| 301 | verifySystemBind(1); |
| 302 | resetContext(); |
| 303 | |
| 304 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 305 | connection.onServiceConnected(null, new Binder()); |
| 306 | connection.onServiceDisconnected(null); |
| 307 | |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 308 | verify(mContext).unbindService(connection); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 309 | verify(mConnectionListener).onClientDisconnected( |
| 310 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 311 | + ".VmsSystemClient U=0")); |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 312 | |
| 313 | Thread.sleep(10); |
| 314 | verifySystemBind(1); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Test |
| 318 | public void testOnSystemServiceDisconnected_ServiceNotConnected() throws Exception { |
| 319 | notifyLockedBootCompleted(); |
| 320 | verifySystemBind(1); |
| 321 | resetContext(); |
| 322 | |
| 323 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 324 | connection.onServiceDisconnected(null); |
| 325 | |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 326 | verify(mContext).unbindService(connection); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 327 | verifyZeroInteractions(mConnectionListener); |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 328 | |
| 329 | Thread.sleep(10); |
| 330 | verifySystemBind(1); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Test |
| 334 | public void testOnUserServiceDisconnected() throws Exception { |
| 335 | notifyUserSwitched(); |
| 336 | verifyUserBind(1); |
| 337 | resetContext(); |
| 338 | |
| 339 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 340 | connection.onServiceConnected(null, new Binder()); |
| 341 | connection.onServiceDisconnected(null); |
| 342 | |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 343 | verify(mContext).unbindService(connection); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 344 | verify(mConnectionListener).onClientDisconnected( |
| 345 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 346 | + ".VmsUserClient U=10")); |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 347 | |
| 348 | Thread.sleep(10); |
| 349 | verifyUserBind(1); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | @Test |
| 353 | public void testOnUserServiceDisconnected_ServiceNotConnected() throws Exception { |
| 354 | notifyUserSwitched(); |
| 355 | verifyUserBind(1); |
| 356 | resetContext(); |
| 357 | |
| 358 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 359 | connection.onServiceDisconnected(null); |
| 360 | |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 361 | verify(mContext).unbindService(connection); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 362 | verifyZeroInteractions(mConnectionListener); |
Mark Tabry | 75be386 | 2019-03-11 11:31:01 -0700 | [diff] [blame] | 363 | |
| 364 | Thread.sleep(10); |
| 365 | verifyUserBind(1); |
Mark Tabry | a8fce56 | 2019-01-16 16:24:01 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Test |
| 369 | public void testOnSystemServiceBindingDied() throws Exception { |
| 370 | notifyLockedBootCompleted(); |
| 371 | verifySystemBind(1); |
| 372 | resetContext(); |
| 373 | |
| 374 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 375 | connection.onServiceConnected(null, new Binder()); |
| 376 | connection.onBindingDied(null); |
| 377 | |
| 378 | verify(mContext).unbindService(connection); |
| 379 | verify(mConnectionListener).onClientDisconnected( |
| 380 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 381 | + ".VmsSystemClient U=0")); |
| 382 | |
| 383 | Thread.sleep(10); |
| 384 | verifySystemBind(1); |
| 385 | } |
| 386 | |
| 387 | @Test |
| 388 | public void testOnSystemServiceBindingDied_ServiceNotConnected() throws Exception { |
| 389 | notifyLockedBootCompleted(); |
| 390 | verifySystemBind(1); |
| 391 | resetContext(); |
| 392 | |
| 393 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 394 | connection.onBindingDied(null); |
| 395 | |
| 396 | verify(mContext).unbindService(connection); |
| 397 | verifyZeroInteractions(mConnectionListener); |
| 398 | |
| 399 | Thread.sleep(10); |
| 400 | verifySystemBind(1); |
| 401 | } |
| 402 | |
| 403 | @Test |
| 404 | public void testOnUserServiceBindingDied() throws Exception { |
| 405 | notifyUserSwitched(); |
| 406 | verifyUserBind(1); |
| 407 | resetContext(); |
| 408 | |
| 409 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 410 | connection.onServiceConnected(null, new Binder()); |
| 411 | connection.onBindingDied(null); |
| 412 | |
| 413 | verify(mContext).unbindService(connection); |
| 414 | verify(mConnectionListener).onClientDisconnected( |
| 415 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 416 | + ".VmsUserClient U=10")); |
| 417 | |
| 418 | Thread.sleep(10); |
| 419 | verifyUserBind(1); |
| 420 | } |
| 421 | |
| 422 | @Test |
| 423 | public void testOnUserServiceBindingDied_ServiceNotConnected() throws Exception { |
| 424 | notifyUserSwitched(); |
| 425 | verifyUserBind(1); |
| 426 | resetContext(); |
| 427 | |
| 428 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 429 | connection.onBindingDied(null); |
| 430 | |
| 431 | verify(mContext).unbindService(connection); |
| 432 | verifyZeroInteractions(mConnectionListener); |
| 433 | |
| 434 | Thread.sleep(10); |
| 435 | verifyUserBind(1); |
| 436 | } |
| 437 | |
| 438 | @Test |
| 439 | public void testOnSystemServiceNullBinding() throws Exception { |
| 440 | notifyLockedBootCompleted(); |
| 441 | verifySystemBind(1); |
| 442 | resetContext(); |
| 443 | |
| 444 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 445 | connection.onServiceConnected(null, new Binder()); |
| 446 | connection.onNullBinding(null); |
| 447 | |
| 448 | verify(mContext).unbindService(connection); |
| 449 | verify(mConnectionListener).onClientDisconnected( |
| 450 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 451 | + ".VmsSystemClient U=0")); |
| 452 | } |
| 453 | |
| 454 | @Test |
| 455 | public void testOnSystemServiceNullBinding_ServiceNotConnected() throws Exception { |
| 456 | notifyLockedBootCompleted(); |
| 457 | verifySystemBind(1); |
| 458 | resetContext(); |
| 459 | |
| 460 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 461 | connection.onNullBinding(null); |
| 462 | |
| 463 | verify(mContext).unbindService(connection); |
| 464 | verifyZeroInteractions(mConnectionListener); |
| 465 | } |
| 466 | |
| 467 | @Test |
| 468 | public void testOnUserServiceNullBinding() throws Exception { |
| 469 | notifyUserSwitched(); |
| 470 | verifyUserBind(1); |
| 471 | resetContext(); |
| 472 | |
| 473 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 474 | connection.onServiceConnected(null, new Binder()); |
| 475 | connection.onNullBinding(null); |
| 476 | |
| 477 | verify(mContext).unbindService(connection); |
| 478 | verify(mConnectionListener).onClientDisconnected( |
| 479 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 480 | + ".VmsUserClient U=10")); |
| 481 | } |
| 482 | |
| 483 | @Test |
| 484 | public void testOnUserServiceNullBinding_ServiceNotConnected() throws Exception { |
| 485 | notifyUserSwitched(); |
| 486 | verifyUserBind(1); |
| 487 | resetContext(); |
| 488 | |
| 489 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 490 | connection.onNullBinding(null); |
| 491 | |
| 492 | verify(mContext).unbindService(connection); |
| 493 | verifyZeroInteractions(mConnectionListener); |
| 494 | } |
| 495 | |
| 496 | @Test |
| 497 | public void testOnUserSwitched_UserChange() { |
| 498 | notifyUserSwitched(); |
| 499 | verifyUserBind(1); |
| 500 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 501 | connection.onServiceConnected(null, new Binder()); |
| 502 | resetContext(); |
| 503 | reset(mConnectionListener); |
| 504 | |
| 505 | mUserInfo = new UserInfo(11, "Driver", 0); |
| 506 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 507 | notifyUserSwitched(); |
| 508 | |
| 509 | verify(mContext).unbindService(connection); |
| 510 | verify(mConnectionListener).onClientDisconnected( |
| 511 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 512 | + ".VmsUserClient U=10")); |
| 513 | verifyBind(1, "com.google.android.apps.vms.test/.VmsUserClient", |
| 514 | mUserInfo.getUserHandle()); |
| 515 | } |
| 516 | |
| 517 | @Test |
| 518 | public void testOnUserSwitched_UserChange_ToSystemUser() { |
| 519 | notifyUserSwitched(); |
| 520 | verifyUserBind(1); |
| 521 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 522 | connection.onServiceConnected(null, new Binder()); |
| 523 | resetContext(); |
| 524 | reset(mConnectionListener); |
| 525 | |
| 526 | mUserInfo = new UserInfo(UserHandle.USER_SYSTEM, "Owner", 0); |
| 527 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 528 | notifyUserSwitched(); |
| 529 | |
| 530 | verify(mContext).unbindService(connection); |
| 531 | verify(mConnectionListener).onClientDisconnected( |
| 532 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 533 | + ".VmsUserClient U=10")); |
| 534 | // User processes will not be bound for system user |
| 535 | verifyBind(0, "com.google.android.apps.vms.test/.VmsUserClient", |
| 536 | mUserInfo.getUserHandle()); |
| 537 | } |
| 538 | |
| 539 | @Test |
| 540 | public void testOnUserSwitched_UserChange_ServiceNotConnected() { |
| 541 | notifyUserSwitched(); |
| 542 | verifyUserBind(1); |
| 543 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 544 | resetContext(); |
| 545 | |
| 546 | mUserInfo = new UserInfo(11, "Driver", 0); |
| 547 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 548 | notifyUserSwitched(); |
| 549 | |
| 550 | verify(mContext).unbindService(connection); |
| 551 | verifyBind(1, "com.google.android.apps.vms.test/.VmsUserClient", |
| 552 | mUserInfo.getUserHandle()); |
| 553 | } |
| 554 | |
| 555 | @Test |
| 556 | public void testOnUserUnlocked_UserChange() { |
| 557 | notifyUserUnlocked(); |
| 558 | verifyUserBind(1); |
| 559 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 560 | connection.onServiceConnected(null, new Binder()); |
| 561 | resetContext(); |
| 562 | reset(mConnectionListener); |
| 563 | |
| 564 | mUserInfo = new UserInfo(11, "Driver", 0); |
| 565 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 566 | notifyUserUnlocked(); |
| 567 | |
| 568 | verify(mContext).unbindService(connection); |
| 569 | verify(mConnectionListener).onClientDisconnected( |
| 570 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 571 | + ".VmsUserClient U=10")); |
| 572 | verifyBind(1, "com.google.android.apps.vms.test/.VmsUserClient", |
| 573 | mUserInfo.getUserHandle()); |
| 574 | } |
| 575 | |
| 576 | @Test |
| 577 | public void testOnUserLocked_UserChange_ToSystemUser() { |
| 578 | notifyUserUnlocked(); |
| 579 | verifyUserBind(1); |
| 580 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 581 | connection.onServiceConnected(null, new Binder()); |
| 582 | resetContext(); |
| 583 | reset(mConnectionListener); |
| 584 | |
| 585 | mUserInfo = new UserInfo(UserHandle.USER_SYSTEM, "Owner", 0); |
| 586 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 587 | notifyUserUnlocked(); |
| 588 | |
| 589 | verify(mContext).unbindService(connection); |
| 590 | verify(mConnectionListener).onClientDisconnected( |
| 591 | eq("com.google.android.apps.vms.test/com.google.android.apps.vms.test" |
| 592 | + ".VmsUserClient U=10")); |
| 593 | // User processes will not be bound for system user |
| 594 | verifyBind(0, "com.google.android.apps.vms.test/.VmsUserClient", |
| 595 | mUserInfo.getUserHandle()); |
| 596 | } |
| 597 | |
| 598 | @Test |
| 599 | public void testOnUserUnlocked_UserChange_ServiceNotConnected() { |
| 600 | notifyUserUnlocked(); |
| 601 | verifyUserBind(1); |
| 602 | ServiceConnection connection = mConnectionCaptor.getValue(); |
| 603 | resetContext(); |
| 604 | |
| 605 | mUserInfo = new UserInfo(11, "Driver", 0); |
| 606 | when(mUserManager.getCurrentForegroundUserInfo()).thenReturn(mUserInfo); |
| 607 | notifyUserUnlocked(); |
| 608 | |
| 609 | verify(mContext).unbindService(connection); |
| 610 | verifyBind(1, "com.google.android.apps.vms.test/.VmsUserClient", |
| 611 | mUserInfo.getUserHandle()); |
| 612 | } |
| 613 | |
| 614 | private void resetContext() { |
| 615 | reset(mContext); |
| 616 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
| 617 | when(mContext.bindServiceAsUser(any(), any(), anyInt(), any(), any())).thenReturn(true); |
| 618 | when(mContext.getResources()).thenReturn(mResources); |
| 619 | } |
| 620 | |
| 621 | private void notifyLockedBootCompleted() { |
| 622 | mClientManager.mBootCompletedReceiver.onReceive(mContext, |
| 623 | new Intent(Intent.ACTION_LOCKED_BOOT_COMPLETED)); |
| 624 | } |
| 625 | |
| 626 | private void notifyUserSwitched() { |
| 627 | mClientManager.mUserSwitchReceiver.onReceive(mContext, |
| 628 | new Intent(Intent.ACTION_USER_SWITCHED)); |
| 629 | } |
| 630 | |
| 631 | private void notifyUserUnlocked() { |
| 632 | mClientManager.mUserSwitchReceiver.onReceive(mContext, |
| 633 | new Intent(Intent.ACTION_USER_UNLOCKED)); |
| 634 | } |
| 635 | |
| 636 | private void verifySystemBind(int times) { |
| 637 | verifyBind(times, "com.google.android.apps.vms.test/.VmsSystemClient", |
| 638 | UserHandle.SYSTEM); |
| 639 | } |
| 640 | |
| 641 | private void verifyUserBind(int times) { |
| 642 | verifyBind(times, "com.google.android.apps.vms.test/.VmsUserClient", |
| 643 | mUserInfo.getUserHandle()); |
| 644 | } |
| 645 | |
| 646 | private void verifyBind(int times, String componentName, |
| 647 | UserHandle user) { |
| 648 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 649 | verify(mContext, times(times)).bindServiceAsUser( |
| 650 | intentCaptor.capture(), |
| 651 | mConnectionCaptor.capture(), |
| 652 | eq(Context.BIND_AUTO_CREATE), any(Handler.class), eq(user)); |
| 653 | if (times > 0) { |
| 654 | assertEquals( |
| 655 | ComponentName.unflattenFromString(componentName), |
| 656 | intentCaptor.getValue().getComponent()); |
| 657 | } |
| 658 | } |
| 659 | } |