1 /*
2  * Copyright (C) 2018 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 
17 package com.android.car.settings.testutils;
18 
19 import android.net.wifi.WifiConfiguration;
20 import android.net.wifi.WifiManager;
21 
22 import org.robolectric.Shadows;
23 import org.robolectric.annotation.Implementation;
24 import org.robolectric.annotation.Implements;
25 import org.robolectric.annotation.Resetter;
26 
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 
30 @Implements(WifiManager.class)
31 public class ShadowWifiManager extends org.robolectric.shadows.ShadowWifiManager {
32 
33     private static int sResetCalledCount = 0;
34 
35     private final Map<Integer, WifiConfiguration> mNetworkIdToConfiguredNetworks =
36             new LinkedHashMap<>();
37     private int mLastForgottenNetwork = Integer.MIN_VALUE;
38 
39     @Implementation
40     @Override
addNetwork(WifiConfiguration config)41     protected int addNetwork(WifiConfiguration config) {
42         int networkId = mNetworkIdToConfiguredNetworks.size();
43         config.networkId = -1;
44         mNetworkIdToConfiguredNetworks.put(networkId, makeCopy(config, networkId));
45         return networkId;
46     }
47 
getLastAddedNetworkConfiguration()48     public WifiConfiguration getLastAddedNetworkConfiguration() {
49         return mNetworkIdToConfiguredNetworks.get(getLastAddedNetworkId());
50     }
51 
getLastAddedNetworkId()52     public int getLastAddedNetworkId() {
53         return mNetworkIdToConfiguredNetworks.size() - 1;
54     }
55 
verifyFactoryResetCalled(int numTimes)56     public static boolean verifyFactoryResetCalled(int numTimes) {
57         return sResetCalledCount == numTimes;
58     }
59 
60     @Implementation
forget(int netId, WifiManager.ActionListener listener)61     protected void forget(int netId, WifiManager.ActionListener listener) {
62         mLastForgottenNetwork = netId;
63     }
64 
getLastForgottenNetwork()65     public int getLastForgottenNetwork() {
66         return mLastForgottenNetwork;
67     }
68 
69     @Implementation
factoryReset()70     protected void factoryReset() {
71         sResetCalledCount++;
72     }
73 
74     @Resetter
reset()75     public static void reset() {
76         sResetCalledCount = 0;
77     }
78 
makeCopy(WifiConfiguration config, int networkId)79     private WifiConfiguration makeCopy(WifiConfiguration config, int networkId) {
80         WifiConfiguration copy = Shadows.shadowOf(config).copy();
81         copy.networkId = networkId;
82         return copy;
83     }
84 }
85