• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
AbstractExtendedMockitoCarServiceTestCase(String... logTags)38     public AbstractExtendedMockitoCarServiceTestCase(String... logTags) {
39         super(logTags);
40     }
41 
42     /**
43      * Mocks a call to {@link CarLocalServices#getService(Class)}.
44      *
45      * @throws IllegalStateException if class didn't override {@link #newSessionBuilder()} and
46      * called {@code spyStatic(CarLocalServices.class)} on the session passed to it.
47      */
mockGetCarLocalService(@onNull Class<T> type, @NonNull T service)48     protected final <T> void mockGetCarLocalService(@NonNull Class<T> type, @NonNull T service) {
49         if (VERBOSE) Log.v(TAG, getLogPrefix() + "mockGetLocalService(" + type.getName() + ")");
50         assertSpied(CarLocalServices.class);
51 
52         beginTrace("mockGetLocalService");
53         doReturn(service).when(() -> CarLocalServices.getService(type));
54         endTrace();
55     }
56 }
57