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 17package com.android.server.wifi; 18 19import static org.mockito.Mockito.when; 20 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.Intent; 24import android.net.ConnectivityManager; 25import android.net.NetworkInfo; 26import android.net.wifi.WifiManager; 27 28import java.lang.reflect.Field; 29import java.util.ArrayList; 30 31/** 32 * Utils for wifi tests. 33 */ 34public class TestUtil { 35 36 /** 37 * Override wifi interface using {@code wifiNative}. 38 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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