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 /** Mocks a call to SDK level to return S */ mockSdkLevelS()53 void mockSdkLevelS(); 54 55 /** 56 * Mocks a call to {@link ActivityManager#getCurrentUser()}, returning {@code user}. 57 * 58 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 59 * equivalent annotations) on {@link ActivityManager}. 60 */ mockGetCurrentUser(int user)61 void mockGetCurrentUser(int user); 62 63 // NOTE: current tests are only intercepting d, , and e, but we could add more methods on 64 // demand (even one that takes Level...levels) 65 66 /** 67 * Statically spy on {@code Log.d} for that {@code tag}. 68 * 69 * @return object that can be used to assert the {@code Log.d} calls. 70 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 71 * equivalent annotations) on {@link Log}. 72 */ interceptLogD(String tag)73 LogInterceptor interceptLogD(String tag); 74 75 /** 76 * Statically spy on {@code Log.v} for that {@code tag}. 77 * 78 * @return object that can be used to assert the {@code Log.v} calls. 79 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 80 * equivalent annotations) on {@link Log}. 81 */ interceptLogV(String tag)82 LogInterceptor interceptLogV(String tag); 83 84 /** 85 * Statically spy on {@code Log.e} for that {@code tag}. 86 * 87 * @return object that can be used to assert the {@code Log.e} calls. 88 * @throws IllegalStateException if test didn't call {@code spyStatic} / {@code mockStatic} (or 89 * equivalent annotations) on {@link Log}. 90 */ interceptLogE(String tag)91 LogInterceptor interceptLogE(String tag); 92 } 93