/* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.car.test.mocks; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; import static com.google.common.truth.Truth.assertWithMessage; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.when; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.UserIdInt; import android.app.Activity; import android.app.ActivityManager; import android.car.test.util.UserTestingHelper; import android.car.test.util.UserTestingHelper.UserInfoBuilder; import android.car.test.util.Visitor; import android.content.Context; import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.content.pm.UserInfo.UserInfoFlag; import android.os.Binder; import android.os.Handler; import android.os.IBinder; import android.os.IInterface; import android.os.Looper; import android.os.ServiceManager; import android.os.UserHandle; import android.os.UserManager; import android.os.UserManager.RemoveResult; import android.util.Log; import java.util.List; import java.util.Objects; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; /** * Provides common Mockito calls for core Android classes. */ public final class AndroidMockitoHelper { private static final String TAG = AndroidMockitoHelper.class.getSimpleName(); /** * Mocks a call to {@link ActivityManager#getCurrentUser()}. * *
Note: it must be made inside a * {@link com.android.dx.mockito.inline.extended.StaticMockitoSession} built with * {@code spyStatic(ActivityManager.class)}. * * @param userId result of such call */ public static void mockAmGetCurrentUser(@UserIdInt int userId) { doReturn(userId).when(() -> ActivityManager.getCurrentUser()); } /** * Mocks a call to {@link UserManager#isHeadlessSystemUserMode()}. * *
Note: it must be made inside a
* {@linkcom.android.dx.mockito.inline.extended.StaticMockitoSession} built with
* {@code spyStatic(UserManager.class)}.
*
* @param mode result of such call
*/
public static void mockUmIsHeadlessSystemUserMode(boolean mode) {
doReturn(mode).when(() -> UserManager.isHeadlessSystemUserMode());
}
/**
* Mocks {@code UserManager#getUserInfo(userId)} to return a {@link UserInfo} with the given
* {@code flags}.
*/
@NonNull
public static UserInfo mockUmGetUserInfo(@NonNull UserManager um, @UserIdInt int userId,
@UserInfoFlag int flags) {
Objects.requireNonNull(um);
UserInfo user = new UserTestingHelper.UserInfoBuilder(userId).setFlags(flags).build();
mockUmGetUserInfo(um, user);
return user;
}
/**
* Mocks {@code UserManager.getUserInfo(userId)} to return the given {@link UserInfo}.
*/
@NonNull
public static void mockUmGetUserInfo(@NonNull UserManager um, @NonNull UserInfo user) {
when(um.getUserInfo(user.id)).thenReturn(user);
}
/**
* Mocks {@code UserManager#getUserInfo(userId)} when the {@code userId} is the system user's.
*/
@NonNull
public static void mockUmGetSystemUser(@NonNull UserManager um) {
UserInfo user = new UserTestingHelper.UserInfoBuilder(UserHandle.USER_SYSTEM)
.setFlags(UserInfo.FLAG_SYSTEM).build();
when(um.getUserInfo(UserHandle.USER_SYSTEM)).thenReturn(user);
}
/**
* Mocks {@code UserManager#getAliveUsers()} to return the given users.
*/
public static void mockUmGetAliveUsers(@NonNull UserManager um, @NonNull UserInfo... users) {
Objects.requireNonNull(um);
when(um.getAliveUsers()).thenReturn(UserTestingHelper.toList(users));
}
/**
* Mocks {@code UserManager#getAliveUsers()} to return the simple users with the given ids.
*/
public static void mockUmGetAliveUsers(@NonNull UserManager um,
@NonNull @UserIdInt int... userIds) {
List Note: it must be made inside a
* {@link com.android.dx.mockito.inline.extended.StaticMockitoSession} built with
* {@code spyStatic(ServiceManager.class)}.
*
* @param name interface name of the service
* @param binder result of such call
*/
public static void mockSmGetService(@NonNull String name, @NonNull IBinder binder) {
doReturn(binder).when(() -> ServiceManager.getService(name));
}
/**
* Returns mocked binder implementation from the given interface name.
*
* Note: it must be made inside a
* {@link com.android.dx.mockito.inline.extended.StaticMockitoSession} built with
* {@code spyStatic(ServiceManager.class)}.
*
* @param name interface name of the service
* @param binder mocked return of ServiceManager.getService
* @param service binder implementation
*/
public static Note: it must be made inside a
* {@link com.android.dx.mockito.inline.extended.StaticMockitoSession} built with
* {@code spyStatic(Binder.class)}.
*
* @param userId identifier of the {@link UserHandle} that will be returned.
*/
public static void mockBinderGetCallingUserHandle(@UserIdInt int userId) {
doReturn(UserHandle.of(userId)).when(() -> Binder.getCallingUserHandle());
}
/**
* Mocks a call to {@link Context#getSystemService(Class)}.
*/
public static