1 /*
2  * Copyright (C) 2020 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 android.car;
17 
18 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
19 
20 import android.annotation.NonNull;
21 import android.car.test.mocks.AbstractExtendedMockitoTestCase;
22 import android.util.Log;
23 
24 import com.android.car.CarLocalServices;
25 
26 /**
27  * Specialized {@link AbstractExtendedMockitoTestCase} implementation that provides
28  * CarService-related helpers.
29  */
30 public abstract class AbstractExtendedMockitoCarServiceTestCase
31         extends AbstractExtendedMockitoTestCase {
32 
33     private static final String TAG = AbstractExtendedMockitoCarServiceTestCase.class
34             .getSimpleName();
35 
36     private static final boolean VERBOSE = false;
37 
38     /**
39      * Mocks a call to {@link CarLocalServices#getService(Class)}.
40      *
41      * @throws IllegalStateException if class didn't override {@link #newSessionBuilder()} and
42      * called {@code spyStatic(CarLocalServices.class)} on the session passed to it.
43      */
mockGetCarLocalService(@onNull Class<T> type, @NonNull T service)44     protected final <T> void mockGetCarLocalService(@NonNull Class<T> type, @NonNull T service) {
45         if (VERBOSE) Log.v(TAG, getLogPrefix() + "mockGetLocalService(" + type.getName() + ")");
46         assertSpied(CarLocalServices.class);
47 
48         beginTrace("mockGetLocalService");
49         doReturn(service).when(() -> CarLocalServices.getService(type));
50         endTrace();
51     }
52 }
53