diff options
author | felipeal <felipeal@google.com> | 2020-04-27 14:16:43 -0700 |
---|---|---|
committer | felipeal <felipeal@google.com> | 2020-04-28 17:11:00 -0700 |
commit | 7f731773c6520854171df90acbaa013e713d4a86 (patch) | |
tree | fecb68856ca53ee210e0fdcd75a8db917ab43e36 /tests/carservice_unit_test/src/com/android | |
parent | d8964ac0928b81af424cb8e4c10903bef66c7696 (diff) | |
download | Car-7f731773c6520854171df90acbaa013e713d4a86.tar.gz |
More improvements on android.car.test.utils package.
- Added more helpers to mock static methods on AbstractExtendMockitoTestCase.
- Refactored blocking UserLifeCycleListener into OneEventUserLifecycleListener.
- Moved custom ArgumentMatchers to CarArgumentMatchers.
- Moved common car-related mock calls to CarMockitoHelper.
Bug: 149099817
Test: atest \
CarUserManagerHelperTest InitialUserSetterTest \
UserHelperTest CarPowerManagementServiceTest \
UserHalServiceTest VendorServiceControllerTest \
CarUserManagerUnitTest CarUserNoticeServiceTest CarUserServiceTest
Change-Id: Ia2b9359d5b0a502d72e35eabbe8b2e487885e8d8
Diffstat (limited to 'tests/carservice_unit_test/src/com/android')
6 files changed, 17 insertions, 184 deletions
diff --git a/tests/carservice_unit_test/src/com/android/car/CarPowerManagementServiceTest.java b/tests/carservice_unit_test/src/com/android/car/CarPowerManagementServiceTest.java index 625e472da5..0beba1bcc1 100644 --- a/tests/carservice_unit_test/src/com/android/car/CarPowerManagementServiceTest.java +++ b/tests/carservice_unit_test/src/com/android/car/CarPowerManagementServiceTest.java @@ -16,7 +16,7 @@ package com.android.car; -import static android.car.userlib.InitialUserSetterTest.isUserInfo; +import static android.car.test.mocks.CarArgumentMatchers.isUserInfo; import static android.car.userlib.InitialUserSetterTest.newGuestUser; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer; @@ -878,7 +878,6 @@ public class CarPowerManagementServiceTest extends AbstractExtendMockitoTestCase int value(); } - // TODO(b/149099817): move to common code private interface Visitor<T> { void visit(T t); } diff --git a/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java b/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java index d48bcd3392..877c3f3275 100644 --- a/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java +++ b/tests/carservice_unit_test/src/com/android/car/hal/UserHalServiceTest.java @@ -19,6 +19,8 @@ import static android.car.VehiclePropertyIds.CURRENT_GEAR; import static android.car.VehiclePropertyIds.INITIAL_USER_INFO; import static android.car.VehiclePropertyIds.SWITCH_USER; import static android.car.VehiclePropertyIds.USER_IDENTIFICATION_ASSOCIATION; +import static android.car.test.mocks.CarArgumentMatchers.isProperty; +import static android.car.test.mocks.CarArgumentMatchers.isPropertyWithValues; import static android.hardware.automotive.vehicle.V2_0.InitialUserInfoRequestType.COLD_BOOT; import static android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociationType.CUSTOM_1; import static android.hardware.automotive.vehicle.V2_0.UserIdentificationAssociationType.KEY_FOB; @@ -28,7 +30,6 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.verify; @@ -63,7 +64,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; -import org.mockito.ArgumentMatcher; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; @@ -827,61 +827,6 @@ public final class UserHalServiceTest { // TODO(b/149099817): move stuff below to common code /** - * Custom Mockito matcher to check if a {@link VehiclePropValue} has the given {@code prop}. - */ - public static VehiclePropValue isProperty(int prop) { - return argThat(new PropertyIdMatcher(prop)); - } - - /** - * Custom Mockito matcher to check if a {@link VehiclePropValue} has the given {@code prop} and - * {@code int32} values. - */ - public static VehiclePropValue isPropertyWithValues(int prop, int...values) { - return argThat(new PropertyIdMatcher(prop, values)); - } - - private static class PropertyIdMatcher implements ArgumentMatcher<VehiclePropValue> { - - final int mProp; - private final int[] mValues; - - private PropertyIdMatcher(int prop) { - this(prop, null); - } - - private PropertyIdMatcher(int prop, int[] values) { - mProp = prop; - mValues = values; - } - - @Override - public boolean matches(VehiclePropValue argument) { - Log.v(TAG, "PropertyIdMatcher: argument=" + argument); - if (argument.prop != mProp) { - Log.w(TAG, "PropertyIdMatcher: Invalid prop on " + argument); - return false; - } - if (mValues == null) return true; - // Make sure values match - if (mValues.length != argument.value.int32Values.size()) { - Log.w(TAG, "PropertyIdMatcher: number of values (expected " + mValues.length - + ") mismatch on " + argument); - return false; - } - - for (int i = 0; i < mValues.length; i++) { - if (mValues[i] != argument.value.int32Values.get(i)) { - Log.w(TAG, "PropertyIdMatcher: value mismatch at index " + i + " on " + argument - + ": expected " + Arrays.toString(mValues)); - return false; - } - } - return true; - } - } - - /** * Creates an empty config for the given property. */ private static VehiclePropConfig newConfig(int prop) { diff --git a/tests/carservice_unit_test/src/com/android/car/pm/VendorServiceControllerTest.java b/tests/carservice_unit_test/src/com/android/car/pm/VendorServiceControllerTest.java index 2fc5ccc336..f4bbad3956 100644 --- a/tests/carservice_unit_test/src/com/android/car/pm/VendorServiceControllerTest.java +++ b/tests/carservice_unit_test/src/com/android/car/pm/VendorServiceControllerTest.java @@ -16,20 +16,20 @@ package com.android.car.pm; +import static android.car.test.mocks.CarArgumentMatchers.isUserHandle; + import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.when; import android.annotation.UserIdInt; import android.app.ActivityManager; import android.car.test.mocks.AbstractExtendMockitoTestCase; +import android.car.testapi.OneEventUserLifecycleListener; import android.car.user.CarUserManager; -import android.car.user.CarUserManager.UserLifecycleEvent; import android.car.user.CarUserManager.UserLifecycleEventType; -import android.car.user.CarUserManager.UserLifecycleListener; import android.car.userlib.CarUserManagerHelper; import android.content.ComponentName; import android.content.Context; @@ -44,7 +44,6 @@ import android.os.UserHandle; import android.os.UserManager; import android.util.Log; -import androidx.annotation.Nullable; import androidx.test.core.app.ApplicationProvider; import com.android.car.CarLocalServices; @@ -56,7 +55,6 @@ import com.android.internal.util.Preconditions; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentMatcher; import org.mockito.Mock; import org.mockito.Mockito; @@ -197,7 +195,7 @@ public final class VendorServiceControllerTest extends AbstractExtendMockitoTest } private void mockUserUnlock(@UserIdInt int userId) { - when(mUserManager.isUserUnlockingOrUnlocked(isUser(userId))).thenReturn(true); + when(mUserManager.isUserUnlockingOrUnlocked(isUserHandle(userId))).thenReturn(true); when(mUserManager.isUserUnlockingOrUnlocked(userId)).thenReturn(true); } @@ -213,7 +211,7 @@ public final class VendorServiceControllerTest extends AbstractExtendMockitoTest @UserIdInt int userId) throws InterruptedException { // Adding a blocking listener to ensure CarUserService event notification is completed // before proceeding with test execution. - BlockingUserLifecycleListener blockingListener = new BlockingUserLifecycleListener(); + OneEventUserLifecycleListener blockingListener = new OneEventUserLifecycleListener(); mCarUserService.addUserLifecycleListener(blockingListener); runOnMainThreadAndWaitForIdle(() -> mCarUserService.onUserLifecycleEvent(eventType, @@ -342,63 +340,4 @@ public final class VendorServiceControllerTest extends AbstractExtendMockitoTest return super.getSystemService(name); } } - - // TODO(b/149099817): move stuff below to common code - - /** - * Custom Mockito matcher to check if a {@link UserHandle} has the given {@code userId}. - */ - public static UserHandle isUser(@UserIdInt int userId) { - return argThat(new UserHandleMatcher(userId)); - } - - private static class UserHandleMatcher implements ArgumentMatcher<UserHandle> { - - public final @UserIdInt int userId; - - private UserHandleMatcher(@UserIdInt int userId) { - this.userId = userId; - } - - @Override - public boolean matches(UserHandle argument) { - return argument != null && argument.getIdentifier() == userId; - } - } - - /** - * CarUserService now notifies listener in its own handler thread. This wrapper is used to - * block test thread until listener is notified. - */ - // TODO(b/149099817): Move this class to a common place - private static final class BlockingUserLifecycleListener implements UserLifecycleListener { - - public static final int USER_LIFECYCLE_LISTENER_ON_EVENT_TIMEOUT_SECONDS = 2; - - private final CountDownLatch mLatch = new CountDownLatch(1); - - @Nullable - private UserLifecycleEvent mReceivedEvent; - - @Override - public void onEvent(UserLifecycleEvent event) { - this.mReceivedEvent = event; - mLatch.countDown(); - } - - /** - * Blocks until onEvent is invoked. - */ - @Nullable - public UserLifecycleEvent waitForEvent() throws InterruptedException { - if (!mLatch.await(USER_LIFECYCLE_LISTENER_ON_EVENT_TIMEOUT_SECONDS, - TimeUnit.SECONDS)) { - String errorMessage = "mUserLifecycleListenerWrapper.onEvent not called in " - + USER_LIFECYCLE_LISTENER_ON_EVENT_TIMEOUT_SECONDS + " seconds"; - Log.e(TAG, errorMessage); - fail(errorMessage); - } - return mReceivedEvent; - } - } } diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java index c78ff4878b..5a702b61aa 100644 --- a/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java +++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserManagerUnitTest.java @@ -15,16 +15,13 @@ */ package com.android.car.user; +import static android.car.testapi.CarMockitoHelper.mockHandleRemoteExceptionFromCarServiceWithDefaultValue; import static android.os.UserHandle.USER_SYSTEM; -import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; - import static com.google.common.truth.Truth.assertThat; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.notNull; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doThrow; @@ -79,7 +76,7 @@ public final class CarUserManagerUnitTest extends AbstractExtendMockitoTestCase @Test public void testIsValidUser_headlessSystemUser() { - setHeadlessSystemUserMode(true); + mockIsHeadlessSystemUserMode(true); setExistingUsers(USER_SYSTEM); assertThat(mMgr.isValidUser(USER_SYSTEM)).isFalse(); @@ -87,7 +84,7 @@ public final class CarUserManagerUnitTest extends AbstractExtendMockitoTestCase @Test public void testIsValidUser_nonHeadlessSystemUser() { - setHeadlessSystemUserMode(false); + mockIsHeadlessSystemUserMode(false); setExistingUsers(USER_SYSTEM); assertThat(mMgr.isValidUser(USER_SYSTEM)).isTrue(); @@ -130,7 +127,7 @@ public final class CarUserManagerUnitTest extends AbstractExtendMockitoTestCase @Test public void testSwitchUser_remoteException() throws Exception { expectServiceSwitchUserSucceeds(11); - expectCarHandleExceptionReturnsDefaultValue(); + mockHandleRemoteExceptionFromCarServiceWithDefaultValue(mCar); AndroidFuture<UserSwitchResult> future = mMgr.switchUser(11); @@ -184,14 +181,4 @@ public final class CarUserManagerUnitTest extends AbstractExtendMockitoTestCase user.id = userId; return user; } - - private static void setHeadlessSystemUserMode(boolean mode) { - doReturn(mode).when(() -> UserManager.isHeadlessSystemUserMode()); - } - - private void expectCarHandleExceptionReturnsDefaultValue() { - doAnswer((invocation) -> { - return invocation.getArguments()[1]; - }).when(mCar).handleRemoteExceptionFromCarService(isA(RemoteException.class), any()); - } } diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserNoticeServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserNoticeServiceTest.java index 09c6deb7eb..695db41c69 100644 --- a/tests/carservice_unit_test/src/com/android/car/user/CarUserNoticeServiceTest.java +++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserNoticeServiceTest.java @@ -101,7 +101,7 @@ public class CarUserNoticeServiceTest extends AbstractExtendMockitoTestCase { @Override protected void onSessionBuilder(CustomMockitoSessionBuilder session) { session - .mockStatic(CarLocalServices.class) + .spyStatic(CarLocalServices.class) .mockStatic(Settings.Secure.class); } @@ -113,7 +113,6 @@ public class CarUserNoticeServiceTest extends AbstractExtendMockitoTestCase { doReturn(mCarPowerManager).when(() -> CarLocalServices.createCarPowerManager(mMockContext)); doReturn(mMockCarPowerManagementService) .when(() -> CarLocalServices.getService(CarPowerManagementService.class)); - doReturn(mCarPowerManager).when(() -> CarLocalServices.createCarPowerManager(mMockContext)); doReturn(mMockCarUserService) .when(() -> CarLocalServices.getService(CarUserService.class)); @@ -251,7 +250,7 @@ public class CarUserNoticeServiceTest extends AbstractExtendMockitoTestCase { return latch; } - private CountDownLatch mockKeySettings(String key, int value) { + private static CountDownLatch mockKeySettings(String key, int value) { CountDownLatch latch = new CountDownLatch(1); when(Settings.Secure.getIntForUser(any(), eq(key), anyInt(), diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java index 70024b0ca1..f2052b1445 100644 --- a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java +++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java @@ -27,7 +27,6 @@ import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; @@ -51,6 +50,7 @@ import android.car.CarOccupantZoneManager.OccupantTypeEnum; import android.car.CarOccupantZoneManager.OccupantZoneInfo; import android.car.settings.CarSettings; import android.car.test.mocks.AbstractExtendMockitoTestCase; +import android.car.testapi.OneEventUserLifecycleListener; import android.car.user.CarUserManager; import android.car.user.CarUserManager.UserLifecycleEvent; import android.car.user.CarUserManager.UserLifecycleEventType; @@ -132,8 +132,8 @@ public final class CarUserServiceTest extends AbstractExtendMockitoTestCase { @Mock private Resources mMockedResources; @Mock private Drawable mMockedDrawable; - private final BlockingUserLifecycleListener mUserLifecycleListener = - new BlockingUserLifecycleListener(); + private final OneEventUserLifecycleListener mUserLifecycleListener = + new OneEventUserLifecycleListener(); @Captor private ArgumentCaptor<UsersInfo> mUsersInfoCaptor; @@ -1508,40 +1508,4 @@ public final class CarUserServiceTest extends AbstractExtendMockitoTestCase { return mResultData; } } - - /** - * CarUserService now notifies listener in its own handler thread. This wrapper is used to - * block test thread until listener is notified. - */ - // TODO(b/149099817): Move this class to a common place - private static final class BlockingUserLifecycleListener implements UserLifecycleListener { - - public static final int USER_LIFECYCLE_LISTENER_ON_EVENT_TIMEOUT_SECONDS = 2; - - private final CountDownLatch mLatch = new CountDownLatch(1); - - @Nullable - private UserLifecycleEvent mReceivedEvent; - - @Override - public void onEvent(UserLifecycleEvent event) { - this.mReceivedEvent = event; - mLatch.countDown(); - } - - /** - * Blocks until onEvent is invoked. - */ - @Nullable - public UserLifecycleEvent waitForEvent() throws InterruptedException { - if (!mLatch.await(USER_LIFECYCLE_LISTENER_ON_EVENT_TIMEOUT_SECONDS, - TimeUnit.SECONDS)) { - String errorMessage = "mUserLifecycleListenerWrapper.onEvent not called in " - + USER_LIFECYCLE_LISTENER_ON_EVENT_TIMEOUT_SECONDS + " seconds"; - Log.e(TAG, errorMessage); - fail(errorMessage); - } - return mReceivedEvent; - } - } } |