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