• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.common;
17 
18 import android.annotation.CallSuper;
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 
23 import com.android.adservices.mockito.AndroidMocker;
24 import com.android.adservices.mockito.AndroidMockitoMocker;
25 import com.android.adservices.service.Flags;
26 
27 import org.junit.Rule;
28 import org.mockito.Mock;
29 import org.mockito.Spy;
30 import org.mockito.junit.MockitoJUnit;
31 import org.mockito.junit.MockitoRule;
32 import org.mockito.quality.Strictness;
33 
34 // NOTE: currently no subclass needs a custom mocker; once they do, this class should be split
35 // into a AdServicesMockerLessMockitoTestCase (similar to AdServiceExtendedMockitoTestCase /
36 // AdServicesMockerLessExtendedMockitoTestCase)
37 /**
38  * Base class for all unit tests that use "regular Mockito" (i.e., not {@code ExtendedMockito} - for
39  * those, use {@link AdServicesExtendedMockitoTestCase} instead)
40  */
41 public abstract class AdServicesMockitoTestCase extends AdServicesUnitTestCase {
42 
43     @Mock protected Context mMockContext;
44     @Mock protected Flags mMockFlags;
45 
46     /** Spy the {@link AdServicesUnitTestCase#mContext} */
47     @Spy protected final Context mSpyContext = mContext;
48 
49     @Rule(order = 10)
50     public final MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.LENIENT);
51 
52     /** Provides common expectations. */
53     public final Mocker mocker = new Mocker();
54 
55     /** Provides common expectations. */
56     public static final class Mocker implements AndroidMocker {
57 
58         private final AndroidMocker mAndroidMocker = new AndroidMockitoMocker();
59 
60         @Override
mockQueryIntentService(PackageManager mockPm, ResolveInfo... resolveInfos)61         public void mockQueryIntentService(PackageManager mockPm, ResolveInfo... resolveInfos) {
62             mAndroidMocker.mockQueryIntentService(mockPm, resolveInfos);
63         }
64 
65         @Override
mockGetApplicationContext(Context mockContext, Context appContext)66         public void mockGetApplicationContext(Context mockContext, Context appContext) {
67             mAndroidMocker.mockGetApplicationContext(mockContext, appContext);
68         }
69     }
70 
71     // TODO(b/361555631): rename to testAdServicesMockitoTestCaseFixtures() and annotate
72     // it with @MetaTest
73     @CallSuper
74     @Override
assertValidTestCaseFixtures()75     protected void assertValidTestCaseFixtures() throws Exception {
76         super.assertValidTestCaseFixtures();
77 
78         checkProhibitedMockitoFields(AdServicesMockitoTestCase.class, this);
79     }
80 }
81