• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.server.wifi;
18 
19 import static org.mockito.Mockito.when;
20 
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.ConnectivityManager;
25 import android.net.NetworkInfo;
26 import android.net.wifi.WifiManager;
27 
28 import java.lang.reflect.Field;
29 import java.util.ArrayList;
30 
31 /**
32  * Utils for wifi tests.
33  */
34 public class TestUtil {
35 
36     /**
37      * Override wifi interface using {@code wifiNative}.
38      */
installWlanWifiNative(WifiNative wifiNative)39     public static void installWlanWifiNative(WifiNative wifiNative) throws Exception {
40         Field field = WifiNative.class.getDeclaredField("wlanNativeInterface");
41         field.setAccessible(true);
42         field.set(null, wifiNative);
43 
44         when(wifiNative.getInterfaceName()).thenReturn("mockWlan");
45     }
46 
47     /**
48      * Send {@link WifiManager#NETWORK_STATE_CHANGED_ACTION} broadcast.
49      */
sendNetworkStateChanged(BroadcastReceiver broadcastReceiver, Context context, NetworkInfo.DetailedState detailedState)50     public static void sendNetworkStateChanged(BroadcastReceiver broadcastReceiver,
51             Context context, NetworkInfo.DetailedState detailedState) {
52         Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION);
53         NetworkInfo networkInfo = new NetworkInfo(0, 0, "", "");
54         networkInfo.setDetailedState(detailedState, "", "");
55         intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo);
56         broadcastReceiver.onReceive(context, intent);
57     }
58 
59     /**
60      * Send {@link WifiManager#SCAN_RESULTS_AVAILABLE_ACTION} broadcast.
61      */
sendScanResultsAvailable(BroadcastReceiver broadcastReceiver, Context context)62     public static void sendScanResultsAvailable(BroadcastReceiver broadcastReceiver,
63             Context context) {
64         Intent intent = new Intent(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
65         broadcastReceiver.onReceive(context, intent);
66     }
67 
68     /**
69      * Send {@link WifiManager#WIFI_SCAN_AVAILABLE} broadcast.
70      */
sendWifiScanAvailable(BroadcastReceiver broadcastReceiver, Context context, int scanAvailable)71     public static void sendWifiScanAvailable(BroadcastReceiver broadcastReceiver,
72             Context context, int scanAvailable) {
73         Intent intent = new Intent(WifiManager.WIFI_SCAN_AVAILABLE);
74         intent.putExtra(WifiManager.EXTRA_SCAN_AVAILABLE, scanAvailable);
75         broadcastReceiver.onReceive(context, intent);
76     }
77 
78     /**
79      * Send {@link WifiManager#WIFI_STATE_CHANGED} broadcast.
80      */
sendWifiStateChanged(BroadcastReceiver broadcastReceiver, Context context, int wifiState)81     public static void sendWifiStateChanged(BroadcastReceiver broadcastReceiver,
82             Context context, int wifiState) {
83         Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
84         intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState);
85         broadcastReceiver.onReceive(context, intent);
86     }
87 
88     /**
89      * Send {@link ConnectivityManager#ACTION_TETHER_STATE_CHANGED} broadcast.
90      */
sendTetherStateChanged(BroadcastReceiver broadcastReceiver, Context context, ArrayList<String> available, ArrayList<String> active)91     public static void sendTetherStateChanged(BroadcastReceiver broadcastReceiver,
92             Context context, ArrayList<String> available, ArrayList<String> active) {
93         Intent intent = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
94         intent.putExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER, available);
95         intent.putExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, active);
96         broadcastReceiver.onReceive(context, intent);
97     }
98 }
99