• 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 com.android.adservices.common.AdServicesExtendedMockitoTestCase.Mocker;
19 import com.android.adservices.common.logging.AdServicesLoggingUsageRule;
20 import com.android.adservices.errorlogging.ErrorLogUtil;
21 import com.android.adservices.mockito.AdServicesExtendedMockitoRule;
22 import com.android.adservices.mockito.StaticClassChecker;
23 import com.android.adservices.service.Flags;
24 
25 import com.google.common.annotations.VisibleForTesting;
26 
27 import java.util.Objects;
28 
29 /**
30  * Base class for all unit tests that use {@code ExtendedMockito} - for "regular Mockito" use {@link
31  * AdServicesMockitoTestCase} instead).
32  *
33  * <p><b>NOTE:</b> subclasses MUST use {@link
34  * com.android.modules.utils.testing.ExtendedMockitoRule.SpyStatic} and/or {@link
35  * com.android.modules.utils.testing.ExtendedMockitoRule.MockStatic} to set which static classes are
36  * mocked ad/or spied.
37  *
38  * <p><b>NOTE:</b>if the subclass wants to provide its own {@code mocker}, it should extend {@link
39  * AdServicesMockerLessExtendedMockitoTestCase} instead - see {@link AdServicesJobServiceTestCase}
40  * as an example.
41  *
42  * <p>TODO(b/355699778) - Add linter to ensure ErrorLogUtil invocation is not mocked in subclasses.
43  * {@link ErrorLogUtil} is automatically spied for all subclasses to ensure {@link
44  * AdServicesLoggingUsageRule} is enforced for logging verification. Subclasses should not mock
45  * {@link ErrorLogUtil} to avoid interference with mocking behavior needed for the rule.
46  */
47 public abstract class AdServicesExtendedMockitoTestCase
48         extends AdServicesMockerLessExtendedMockitoTestCase<Mocker> {
49 
50     @Override
newMocker(AdServicesExtendedMockitoRule rule, Flags mockFlags)51     protected final Mocker newMocker(AdServicesExtendedMockitoRule rule, Flags mockFlags) {
52         return new Mocker(rule, mockFlags);
53     }
54 
55     public static final class Mocker
56             extends AdServicesMockerLessExtendedMockitoTestCase.InternalMocker {
Mocker(StaticClassChecker checker, Flags flags)57         private Mocker(StaticClassChecker checker, Flags flags) {
58             super(checker, flags);
59         }
60 
61         // Factory methods below are used by AdServicesExtendedMockitoTestCaseXYZMockerTest - there
62         // is one method for each of these tests, so it creates a Mocker object that implements
63         // the mockier interface being tested
64 
65         @VisibleForTesting
forSharedMockerTests()66         static Mocker forSharedMockerTests() {
67             return new Mocker(/* checker= */ null, /* flags= */ null);
68         }
69 
70         @VisibleForTesting
forAndroidMockerTests()71         static Mocker forAndroidMockerTests() {
72             return new Mocker(/* checker= */ null, /* flags= */ null);
73         }
74 
75         @VisibleForTesting
forAndroidStaticMockerTests(StaticClassChecker checker)76         static Mocker forAndroidStaticMockerTests(StaticClassChecker checker) {
77             return new Mocker(
78                     Objects.requireNonNull(checker, "checker cannot be null"), /* flags= */ null);
79         }
80 
81         @VisibleForTesting
forAdServicesPragmaticMockerTests()82         static Mocker forAdServicesPragmaticMockerTests() {
83             return new Mocker(/* checker= */ null, /* flags= */ null);
84         }
85 
86         @VisibleForTesting
forAdServicesFlagsMockerTests(Flags flags)87         static Mocker forAdServicesFlagsMockerTests(Flags flags) {
88             return new Mocker(
89                     /* checker= */ null, Objects.requireNonNull(flags, "flags cannot be null"));
90         }
91 
92         @VisibleForTesting
forAdServicesStaticMockerTests(StaticClassChecker checker)93         static Mocker forAdServicesStaticMockerTests(StaticClassChecker checker) {
94             return new Mocker(
95                     Objects.requireNonNull(checker, "checker cannot be null"), /* flags= */ null);
96         }
97     }
98 }
99