1 /* 2 * Copyright (C) 2024 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 package com.android.adservices.mockito; 17 18 import static org.mockito.ArgumentMatchers.any; 19 import static org.mockito.ArgumentMatchers.anyInt; 20 import static org.mockito.Mockito.when; 21 22 import android.content.Context; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 26 import com.android.modules.utils.build.SdkLevel; 27 28 import java.util.Arrays; 29 import java.util.List; 30 import java.util.Objects; 31 32 /** {@link AndroidMocker} implementation that uses {@code Mockito}. */ 33 public final class AndroidMockitoMocker extends AbstractMocker implements AndroidMocker { 34 35 @Override mockQueryIntentService(PackageManager mockPm, ResolveInfo... resolveInfos)36 public void mockQueryIntentService(PackageManager mockPm, ResolveInfo... resolveInfos) { 37 List<ResolveInfo> list = resolveInfos == null ? null : Arrays.asList(resolveInfos); 38 logV("mockQueryIntentService(%s, %s)", mockPm, list); 39 40 Objects.requireNonNull(mockPm, "mockPm cannot be null"); 41 Objects.requireNonNull(resolveInfos, "resolveInfos cannot be null"); 42 assertIsMock("mockPm", mockPm); 43 44 when(mockPm.queryIntentServices(any(), anyInt())).thenReturn(list); 45 if (SdkLevel.isAtLeastT()) { 46 when(mockPm.queryIntentServices(any(), any())).thenReturn(list); 47 } 48 } 49 50 @Override mockGetApplicationContext(Context mockContext, Context appContext)51 public void mockGetApplicationContext(Context mockContext, Context appContext) { 52 logV("mockGetApplicationContext(%s, %s)", mockContext, appContext); 53 Objects.requireNonNull(mockContext, "mockContext cannot be null"); 54 assertIsMock("mockContext", mockContext); 55 56 when(mockContext.getApplicationContext()).thenReturn(appContext); 57 } 58 } 59