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 package com.android.adservices.mockito;
17 
18 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
19 
20 import com.android.adservices.service.FakeFlagsFactory;
21 import com.android.adservices.service.Flags;
22 import com.android.adservices.service.FlagsFactory;
23 import com.android.adservices.service.stats.AdServicesLoggerImpl;
24 import com.android.adservices.spe.AdServicesJobScheduler;
25 import com.android.adservices.spe.AdServicesJobServiceFactory;
26 
27 /**
28  * {@link AdServicesStaticMockitoMocker} implementation that uses {@code ExtendedMockito}.
29  *
30  * <p><b>NOTE: </b> most expectations require {@code spyStatic()} or {@code mockStatic()} in the
31  * {@link com.android.dx.mockito.inline.extended.StaticMockitoSession session} ahead of time - this
32  * helper doesn't check that such calls were made, it's up to the caller to do so.
33  */
34 public final class AdServicesExtendedMockitoMocker extends AbstractStaticMocker
35         implements AdServicesStaticMockitoMocker {
36 
AdServicesExtendedMockitoMocker(StaticClassChecker staticClassChecker)37     public AdServicesExtendedMockitoMocker(StaticClassChecker staticClassChecker) {
38         super(staticClassChecker);
39     }
40 
41     @Override
mockGetFlags(Flags mockedFlags)42     public void mockGetFlags(Flags mockedFlags) {
43         logV("mockGetFlags(%s)", mockedFlags);
44         assertSpiedOrMocked(FlagsFactory.class);
45         doReturn(mockedFlags).when(FlagsFactory::getFlags);
46     }
47 
48     @Override
mockGetFlagsForTesting()49     public void mockGetFlagsForTesting() {
50         mockGetFlags(FakeFlagsFactory.getFlagsForTest());
51     }
52 
53     @Override
mockSpeJobScheduler(AdServicesJobScheduler mockedAdServicesJobScheduler)54     public void mockSpeJobScheduler(AdServicesJobScheduler mockedAdServicesJobScheduler) {
55         logV("mockSpeJobScheduler(%s)", mockedAdServicesJobScheduler);
56         assertSpiedOrMocked(AdServicesJobScheduler.class);
57         doReturn(mockedAdServicesJobScheduler).when(AdServicesJobScheduler::getInstance);
58     }
59 
60     @Override
mockAdServicesJobServiceFactory( AdServicesJobServiceFactory mockedAdServicesJobServiceFactory)61     public void mockAdServicesJobServiceFactory(
62             AdServicesJobServiceFactory mockedAdServicesJobServiceFactory) {
63         logV("mockAdServicesJobServiceFactory(%s)", mockedAdServicesJobServiceFactory);
64         assertSpiedOrMocked(AdServicesJobServiceFactory.class);
65         doReturn(mockedAdServicesJobServiceFactory).when(AdServicesJobServiceFactory::getInstance);
66     }
67 
68     @Override
mockAdServicesLoggerImpl(AdServicesLoggerImpl mockedAdServicesLoggerImpl)69     public void mockAdServicesLoggerImpl(AdServicesLoggerImpl mockedAdServicesLoggerImpl) {
70         logV("mockAdServicesLoggerImpl(%s)", mockedAdServicesLoggerImpl);
71         assertSpiedOrMocked(AdServicesLoggerImpl.class);
72         doReturn(mockedAdServicesLoggerImpl).when(AdServicesLoggerImpl::getInstance);
73     }
74 }
75