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.any; 19 import static org.mockito.ArgumentMatchers.anyBoolean; 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.Mockito.doAnswer; 22 import static org.mockito.Mockito.doReturn; 23 24 import android.content.Context; 25 import android.net.wifi.WifiManager; 26 import android.util.Log; 27 28 import com.android.bedstead.dpmwrapper.TestAppSystemServiceFactory.ServiceManagerWrapper; 29 30 import org.mockito.Mockito; 31 import org.mockito.stubbing.Answer; 32 33 import java.util.HashMap; 34 35 final class WifiManagerWrapper extends ServiceManagerWrapper<WifiManager> { 36 37 private static final String TAG = WifiManagerWrapper.class.getSimpleName(); 38 39 private static final HashMap<Context, WifiManager> sSpies = new HashMap<>(); 40 41 @Override getWrapper(Context context, WifiManager manager, Answer<?> answer)42 WifiManager getWrapper(Context context, WifiManager manager, Answer<?> answer) { 43 int userId = context.getUserId(); 44 WifiManager 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 = "WifiManagerWrapper#" + 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 WifiConfigCreator 62 doAnswer(answer).when(spy).addNetwork(any()); 63 doAnswer(answer).when(spy).enableNetwork(anyInt(), anyBoolean()); 64 doAnswer(answer).when(spy).removeNetwork(anyInt()); 65 doAnswer(answer).when(spy).getConfiguredNetworks(); 66 doAnswer(answer).when(spy).updateNetwork(any()); 67 doAnswer(answer).when(spy).saveConfiguration(); 68 doAnswer(answer).when(spy).isWifiEnabled(); 69 doAnswer(answer).when(spy).setWifiEnabled(anyBoolean()); 70 71 // Used by WifiNetworkConfigurationWithoutFineLocationPermissionTest 72 doAnswer(answer).when(spy).getCallerConfiguredNetworks(); 73 } catch (Exception e) { 74 // Should never happen, but needs to be catch as some methods declare checked exceptions 75 Log.wtf("Exception setting mocks", e); 76 } 77 78 sSpies.put(context, spy); 79 Log.d(TAG, "get(): returning new spy for context " + context + " and user " 80 + userId); 81 82 return spy; 83 } 84 } 85