1 /*
2  * Copyright (C) 2021 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.bedstead.dpmwrapper;
17 
18 import static org.mockito.ArgumentMatchers.anyInt;
19 import static org.mockito.Mockito.doAnswer;
20 import static org.mockito.Mockito.doReturn;
21 
22 import android.content.Context;
23 import android.os.HardwarePropertiesManager;
24 import android.util.Log;
25 
26 import com.android.bedstead.dpmwrapper.TestAppSystemServiceFactory.ServiceManagerWrapper;
27 
28 import org.mockito.Mockito;
29 import org.mockito.stubbing.Answer;
30 
31 import java.util.HashMap;
32 
33 final class HardwarePropertiesManagerWrapper
34         extends ServiceManagerWrapper<HardwarePropertiesManager> {
35 
36     private static final String TAG = HardwarePropertiesManagerWrapper.class.getSimpleName();
37 
38     private static final HashMap<Context, HardwarePropertiesManager> sSpies = new HashMap<>();
39 
40     @Override
getWrapper(Context context, HardwarePropertiesManager manager, Answer<?> answer)41     HardwarePropertiesManager getWrapper(Context context, HardwarePropertiesManager manager,
42             Answer<?> answer) {
43         int userId = context.getUserId();
44         HardwarePropertiesManager spy = sSpies.get(context);
45         if (spy != null) {
46             Log.d(TAG, "get(): returning cached spy for user " + userId);
47             return spy;
48         }
49 
50         spy = Mockito.spy(manager);
51         String spyString = "HardwarePropertiesManagerWrapper#" + System.identityHashCode(spy);
52         Log.d(TAG, "get(): created spy for user " + context.getUserId() + ": " + spyString);
53 
54         // TODO(b/176993670): ideally there should be a way to automatically mock all DPM methods,
55         // but that's probably not doable, as there is no contract (such as an interface) to specify
56         // which ones should be spied and which ones should not (in fact, if there was an interface,
57         // we wouldn't need Mockito and could wrap the calls using java's DynamicProxy
58         try {
59             doReturn(spyString).when(spy).toString();
60 
61             // Used by HardwarePropertiesManagerTest
62             doAnswer(answer).when(spy).getCpuUsages();
63             doAnswer(answer).when(spy).getDeviceTemperatures(anyInt(), anyInt());
64             doAnswer(answer).when(spy).getFanSpeeds();
65         } catch (Exception e) {
66             // Should never happen, but needs to be catch as some methods declare checked exceptions
67             Log.wtf("Exception setting mocks", e);
68         }
69 
70         sSpies.put(context, spy);
71         Log.d(TAG, "get(): returning new spy for context " + context + " and user "
72                 + userId);
73 
74         return spy;
75     }
76 }
77