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 17 package com.android.adservices.mockito; 18 19 /** Helper interface providing common expectations for static methods on Android SDK. */ 20 public interface AndroidStaticMocker { 21 22 /** 23 * Mocks a call to {@link Binder#getCallingUidOrThrow()}, returning {@code uid}. 24 * 25 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 26 * equivalent annotations) on {@link Binder}. 27 */ mockGetCallingUidOrThrow(int uid)28 void mockGetCallingUidOrThrow(int uid); 29 30 /** 31 * Same as {@link #mockGetCallingUidOrThrow(int)}, but using the {@code uid} of the calling 32 * process. 33 * 34 * <p>Typically used when code under test calls {@link Binder#getCallingUidOrThrow()} and the 35 * test doesn't care about the result, but it needs to be mocked otherwise the real call would 36 * fail (as the test is not running inside a binder transaction). 37 */ mockGetCallingUidOrThrow()38 void mockGetCallingUidOrThrow(); 39 40 /** Mocks a call to {@link SdkLevel#isAtLeastR()}, returning {@code isIt}. */ mockIsAtLeastR(boolean isIt)41 void mockIsAtLeastR(boolean isIt); 42 43 /** Mocks a call to {@link SdkLevel#isAtLeastS()}, returning {@code isIt}. */ mockIsAtLeastS(boolean isIt)44 void mockIsAtLeastS(boolean isIt); 45 46 /** Mocks a call to {@link SdkLevel#isAtLeastT()}, returning {@code isIt}. */ mockIsAtLeastT(boolean isIt)47 void mockIsAtLeastT(boolean isIt); 48 49 /** Mocks a call to SDK level to return R */ mockSdkLevelR()50 void mockSdkLevelR(); 51 52 /** 53 * Mocks a call to {@link ActivityManager#getCurrentUser()}, returning {@code user}. 54 * 55 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 56 * equivalent annotations) on {@link ActivityManager}. 57 */ mockGetCurrentUser(int user)58 void mockGetCurrentUser(int user); 59 60 // NOTE: current tests are only intercepting d, , and e, but we could add more methods on 61 // demand (even one that takes Level...levels) 62 63 /** 64 * Statically spy on {@code Log.d} for that {@code tag}. 65 * 66 * @return object that can be used to assert the {@code Log.d} calls. 67 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 68 * equivalent annotations) on {@link Log}. 69 */ interceptLogD(String tag)70 LogInterceptor interceptLogD(String tag); 71 72 /** 73 * Statically spy on {@code Log.v} for that {@code tag}. 74 * 75 * @return object that can be used to assert the {@code Log.v} calls. 76 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 77 * equivalent annotations) on {@link Log}. 78 */ interceptLogV(String tag)79 LogInterceptor interceptLogV(String tag); 80 81 /** 82 * Statically spy on {@code Log.e} for that {@code tag}. 83 * 84 * @return object that can be used to assert the {@code Log.e} calls. 85 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 86 * equivalent annotations) on {@link Log}. 87 */ interceptLogE(String tag)88 LogInterceptor interceptLogE(String tag); 89 } 90