From 99f90f5cb7637ece0358003d3d3355036e6d68f7 Mon Sep 17 00:00:00 2001 From: Sohani Rao Date: Fri, 17 Mar 2017 11:27:46 -0700 Subject: Unit tests for SupplicantStateTracker Create unit test(s) for SupplicantStateTracker. Need to modify constructor of the class for testability. Change-Id: I5fb6a049ee09cb07e7d866abd9ed3b2b737f7a6e Bug: 36485448 Test: Unit tests, Sanity (Power on, wifi On, connect to AP) --- .../com/android/server/wifi/FrameworkFacade.java | 12 ++- .../server/wifi/SupplicantStateTracker.java | 8 +- .../server/wifi/SupplicantStateTrackerTest.java | 102 +++++++++++++++++++++ 3 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 tests/wifitests/src/com/android/server/wifi/SupplicantStateTrackerTest.java diff --git a/service/java/com/android/server/wifi/FrameworkFacade.java b/service/java/com/android/server/wifi/FrameworkFacade.java index c1b5e8d1e..42f0161e9 100644 --- a/service/java/com/android/server/wifi/FrameworkFacade.java +++ b/service/java/com/android/server/wifi/FrameworkFacade.java @@ -24,6 +24,7 @@ import android.database.ContentObserver; import android.net.TrafficStats; import android.net.Uri; import android.net.ip.IpManager; +import android.os.BatteryStats; import android.os.Handler; import android.os.IBinder; import android.os.RemoteException; @@ -31,6 +32,7 @@ import android.os.ServiceManager; import android.provider.Settings; import android.telephony.CarrierConfigManager; +import com.android.internal.app.IBatteryStats; import com.android.server.wifi.util.WifiAsyncChannel; /** @@ -78,13 +80,21 @@ public class FrameworkFacade { return ServiceManager.getService(serviceName); } + /** + * Returns the battery stats interface + * @return IBatteryStats BatteryStats service interface + */ + public IBatteryStats getBatteryService() { + return IBatteryStats.Stub.asInterface(getService(BatteryStats.SERVICE_NAME)); + } + public PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) { return PendingIntent.getBroadcast(context, requestCode, intent, flags); } public SupplicantStateTracker makeSupplicantStateTracker(Context context, WifiConfigManager configManager, Handler handler) { - return new SupplicantStateTracker(context, configManager, handler); + return new SupplicantStateTracker(context, configManager, this, handler); } public boolean getConfigWiFiDisableInECBM(Context context) { diff --git a/service/java/com/android/server/wifi/SupplicantStateTracker.java b/service/java/com/android/server/wifi/SupplicantStateTracker.java index ea5d9ebd5..9ba64b9bc 100644 --- a/service/java/com/android/server/wifi/SupplicantStateTracker.java +++ b/service/java/com/android/server/wifi/SupplicantStateTracker.java @@ -26,7 +26,6 @@ import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.os.RemoteException; -import android.os.ServiceManager; import android.os.UserHandle; import android.util.Log; import android.util.Slog; @@ -49,6 +48,7 @@ public class SupplicantStateTracker extends StateMachine { private static final String TAG = "SupplicantStateTracker"; private static boolean DBG = false; private final WifiConfigManager mWifiConfigManager; + private FrameworkFacade mFacade; private final IBatteryStats mBatteryStats; /* Indicates authentication failure in supplicant broadcast. * TODO: enhance auth failure reporting to include notification @@ -88,12 +88,14 @@ public class SupplicantStateTracker extends StateMachine { return getCurrentState().getName(); } - public SupplicantStateTracker(Context c, WifiConfigManager wcs, Handler t) { + public SupplicantStateTracker(Context c, WifiConfigManager wcs, + FrameworkFacade facade, Handler t) { super(TAG, t.getLooper()); mContext = c; mWifiConfigManager = wcs; - mBatteryStats = (IBatteryStats)ServiceManager.getService(BatteryStats.SERVICE_NAME); + mFacade = facade; + mBatteryStats = mFacade.getBatteryService(); // CHECKSTYLE:OFF IndentationCheck addState(mDefaultState); addState(mUninitializedState, mDefaultState); diff --git a/tests/wifitests/src/com/android/server/wifi/SupplicantStateTrackerTest.java b/tests/wifitests/src/com/android/server/wifi/SupplicantStateTrackerTest.java new file mode 100644 index 000000000..f1a9e945d --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/SupplicantStateTrackerTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.server.wifi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.wifi.SupplicantState; +import android.net.wifi.WifiManager; +import android.net.wifi.WifiSsid; +import android.os.Handler; +import android.os.Message; +import android.os.test.TestLooper; + +import com.android.internal.app.IBatteryStats; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +/** + * Unit tests for {@link android.net.wifi.SupplicantStateTracker}. + */ +public class SupplicantStateTrackerTest { + + private static final String TAG = "SupplicantStateTrackerTest"; + private static final String sSSID = "\"GoogleGuest\""; + private static final WifiSsid sWifiSsid = WifiSsid.createFromAsciiEncoded(sSSID); + private static final String sBSSID = "01:02:03:04:05:06"; + + private @Mock WifiConfigManager mWcm; + private @Mock Context mContext; + private Handler mHandler; + private SupplicantStateTracker mSupplicantStateTracker; + private TestLooper mLooper; + private FrameworkFacade mFacade; + private BroadcastReceiver mWifiBroadcastReceiver; + + private FrameworkFacade getFrameworkFacade() { + FrameworkFacade facade = mock(FrameworkFacade.class); + IBatteryStats batteryStatsService = mock(IBatteryStats.class); + when(facade.getBatteryService()).thenReturn(batteryStatsService); + return facade; + } + + private Message getSupplicantStateChangeMessage(int networkId, WifiSsid wifiSsid, + String bssid, SupplicantState newSupplicantState) { + return Message.obtain(null, WifiMonitor.SUPPLICANT_STATE_CHANGE_EVENT, 0, 0, + new StateChangeResult(networkId, wifiSsid, bssid, newSupplicantState)); + + } + + @Before + public void setUp() { + mLooper = new TestLooper(); + mHandler = new Handler(mLooper.getLooper()); + MockitoAnnotations.initMocks(this); + mFacade = getFrameworkFacade(); + mSupplicantStateTracker = new SupplicantStateTracker(mContext, mWcm, mFacade, mHandler); + } + + /** + * This test verifies that the SupplicantStateTracker sends a broadcast intent upon receiving + * a message when supplicant state changes + */ + @Test + public void testSupplicantStateChangeIntent() { + mWifiBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + assertTrue(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)); + SupplicantState recvdState = + (SupplicantState) intent.getExtra(WifiManager.EXTRA_NEW_STATE, -1); + assertEquals(SupplicantState.SCANNING, recvdState); + } + }; + IntentFilter mIntentFilter = new IntentFilter(); + mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); + mContext.registerReceiver(mWifiBroadcastReceiver, mIntentFilter); + mSupplicantStateTracker.sendMessage(getSupplicantStateChangeMessage(0, sWifiSsid, + sBSSID, SupplicantState.SCANNING)); + } +} -- cgit v1.2.3