diff options
author | Daniel Erat <derat@google.com> | 2016-04-12 08:39:00 -0600 |
---|---|---|
committer | Daniel Erat <derat@google.com> | 2016-04-18 16:05:34 +0000 |
commit | be40e20f6995d9d11c8270502120a07785dde270 (patch) | |
tree | ae04e54689219fdc2e97b3b23da11fc4054d3679 /tests | |
parent | 6095c3ef2eb6dbe7321a23b146997ad13875696d (diff) |
Refactor SoftApManagerTest.
SoftApManagerTest formerly had tests that doubled as helper
methods (sometimes even calling each other), making it
difficult to simulate events occurring in different orders.
This change creates a clearer distinction between helpers
and tests, and also tries to classify helpers as either
starting processes or verifying that the code under test did
what was expected.
Bug: 28107595
Change-Id: I743d8abbf73ffcd6988ef16d00286733355582ea
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java | 166 |
1 files changed, 76 insertions, 90 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java b/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java index 9f1893fe2..c091517d7 100644 --- a/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java @@ -47,9 +47,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Locale; -/** - * Unit tests for {@link com.android.server.wifi.SoftApManager}. - */ +/** Unit tests for {@link SoftApManager}. */ @SmallTest public class SoftApManagerTest { @@ -69,6 +67,7 @@ public class SoftApManagerTest { @Mock INetworkManagementService mNmService; @Mock ConnectivityManager mConnectivityManager; @Mock SoftApManager.Listener mListener; + @Mock InterfaceConfiguration mInterfaceConfiguration; /** * Internal BroadcastReceiver that SoftApManager uses to listen for tethering @@ -78,15 +77,18 @@ public class SoftApManagerTest { SoftApManager mSoftApManager; - /** - * Setup test. - */ + /** Sets up test. */ @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mLooper = new MockLooper(); when(mWifiNative.getInterfaceName()).thenReturn(TEST_INTERFACE_NAME); + when(mNmService.getInterfaceConfig(TEST_INTERFACE_NAME)) + .thenReturn(mInterfaceConfiguration); + when(mConnectivityManager.getTetherableWifiRegexs()) + .thenReturn(AVAILABLE_DEVICES); + mSoftApManager = new SoftApManager(mContext, mLooper.getLooper(), mWifiNative, @@ -104,9 +106,7 @@ public class SoftApManagerTest { mLooper.dispatchAll(); } - /** - * Verify startSoftAp will fail if AP configuration is not provided. - */ + /** Verifies startSoftAp will fail if AP configuration is not provided. */ @Test public void startSoftApWithoutConfig() throws Exception { InOrder order = inOrder(mListener); @@ -119,63 +119,12 @@ public class SoftApManagerTest { WifiManager.WIFI_AP_STATE_FAILED, WifiManager.SAP_START_FAILURE_GENERAL); } - /** - * Successfully start soft AP. - */ - @Test - public void startSoftApSuccess() throws Exception { - InOrder order = inOrder(mListener); - - /** - * Only test the default configuration. Testing for different configurations - * are taken care of by ApConfigUtilTest. - */ - WifiConfiguration config = new WifiConfiguration(); - config.apBand = WifiConfiguration.AP_BAND_2GHZ; - when(mWifiNative.isHalStarted()).thenReturn(false); - when(mWifiNative.setCountryCodeHal(TEST_COUNTRY_CODE.toUpperCase(Locale.ROOT))) - .thenReturn(true); - mSoftApManager.start(config); - mLooper.dispatchAll(); - verify(mNmService).startAccessPoint( - any(WifiConfiguration.class), eq(TEST_INTERFACE_NAME)); - order.verify(mListener).onStateChanged(WifiManager.WIFI_AP_STATE_ENABLING, 0); - order.verify(mListener).onStateChanged(WifiManager.WIFI_AP_STATE_ENABLED, 0); - } - - /** - * Test the handling of event that triggers tethering. - * @throws Exception - */ - @Test - public void startTethering() throws Exception { - startSoftApSuccess(); - - /* Inject tethering event to start the tethering process. */ - ArrayList<String> availableList = - new ArrayList<String>(Arrays.asList(AVAILABLE_DEVICES)); - TestUtil.sendTetherStateChanged( - mBroadcastReceiver, mContext, availableList, new ArrayList<String>()); - - InterfaceConfiguration ifcg = mock(InterfaceConfiguration.class); - when(mConnectivityManager.getTetherableWifiRegexs()) - .thenReturn(AVAILABLE_DEVICES); - when(mNmService.getInterfaceConfig(TEST_INTERFACE_NAME)).thenReturn(ifcg); - when(mConnectivityManager.tether(TEST_INTERFACE_NAME)) - .thenReturn(ConnectivityManager.TETHER_ERROR_NO_ERROR); - mLooper.dispatchAll(); - verify(ifcg).setLinkAddress(any(LinkAddress.class)); - verify(ifcg).setInterfaceUp(); - verify(mNmService).setInterfaceConfig(eq(TEST_INTERFACE_NAME), eq(ifcg)); - } - - /** - * Test the handling of tethering timeout after tethering is started. - * @throws Exception - */ + /** Tests the handling of timeout after tethering is started. */ @Test public void tetheringTimedOut() throws Exception { - startTethering(); + startSoftApAndVerifyEnabled(); + announceAvailableForTethering(); + verifyTetheringRequested(); InOrder order = inOrder(mListener); @@ -189,31 +138,17 @@ public class SoftApManagerTest { order.verify(mListener).onStateChanged(WifiManager.WIFI_AP_STATE_DISABLED, 0); } - /** - * Test the handling of tethered notification after tethering is started. - * @throws Exception - */ + /** Tests the handling of tethered notification after tethering is started. */ @Test public void tetherCompleted() throws Exception { - startTethering(); - - /* Inject tethering event indicating that the tethering interface is tethered. */ - ArrayList<String> deviceList = - new ArrayList<String>(Arrays.asList(AVAILABLE_DEVICES)); - TestUtil.sendTetherStateChanged( - mBroadcastReceiver, mContext, deviceList, deviceList); - - mLooper.dispatchAll(); - - /* Verify soft ap is not disabled. */ - verify(mListener, never()).onStateChanged(WifiManager.WIFI_AP_STATE_DISABLING, 0); - verify(mListener, never()).onStateChanged(WifiManager.WIFI_AP_STATE_DISABLED, 0); + startSoftApAndVerifyEnabled(); + announceAvailableForTethering(); + verifyTetheringRequested(); + announceTethered(); + verifySoftApNotDisabled(); } - /** - * Test the handling of stop command when soft ap is not started. - * @throws Exception - */ + /** Tests the handling of stop command when soft AP is not started. */ @Test public void stopWhenNotStarted() throws Exception { mSoftApManager.stop(); @@ -222,13 +157,10 @@ public class SoftApManagerTest { verify(mListener, never()).onStateChanged(anyInt(), anyInt()); } - /** - * Test the handling of stop command when soft ap is started. - * @throws Exception - */ + /** Tests the handling of stop command when soft AP is started. */ @Test public void stopWhenStarted() throws Exception { - startSoftApSuccess(); + startSoftApAndVerifyEnabled(); InOrder order = inOrder(mListener); @@ -239,4 +171,58 @@ public class SoftApManagerTest { order.verify(mListener).onStateChanged(WifiManager.WIFI_AP_STATE_DISABLING, 0); order.verify(mListener).onStateChanged(WifiManager.WIFI_AP_STATE_DISABLED, 0); } + + /** Starts soft AP and verifies that it is enabled successfully. */ + protected void startSoftApAndVerifyEnabled() throws Exception { + InOrder order = inOrder(mListener); + + /** + * Only test the default configuration. Testing for different configurations + * are taken care of by ApConfigUtilTest. + */ + WifiConfiguration config = new WifiConfiguration(); + config.apBand = WifiConfiguration.AP_BAND_2GHZ; + when(mWifiNative.isHalStarted()).thenReturn(false); + when(mWifiNative.setCountryCodeHal(TEST_COUNTRY_CODE.toUpperCase(Locale.ROOT))) + .thenReturn(true); + mSoftApManager.start(config); + mLooper.dispatchAll(); + verify(mNmService).startAccessPoint( + any(WifiConfiguration.class), eq(TEST_INTERFACE_NAME)); + order.verify(mListener).onStateChanged(WifiManager.WIFI_AP_STATE_ENABLING, 0); + order.verify(mListener).onStateChanged(WifiManager.WIFI_AP_STATE_ENABLED, 0); + } + + /** Verifies that soft AP was not disabled. */ + protected void verifySoftApNotDisabled() throws Exception { + verify(mListener, never()).onStateChanged(WifiManager.WIFI_AP_STATE_DISABLING, 0); + verify(mListener, never()).onStateChanged(WifiManager.WIFI_AP_STATE_DISABLED, 0); + } + + /** Sends a broadcast intent indicating that the interface is available for tethering. */ + protected void announceAvailableForTethering() throws Exception { + when(mConnectivityManager.tether(TEST_INTERFACE_NAME)) + .thenReturn(ConnectivityManager.TETHER_ERROR_NO_ERROR); + ArrayList<String> availableList = + new ArrayList<String>(Arrays.asList(AVAILABLE_DEVICES)); + TestUtil.sendTetherStateChanged( + mBroadcastReceiver, mContext, availableList, new ArrayList<String>()); + mLooper.dispatchAll(); + } + + /** Verifies that tethering was requested. */ + protected void verifyTetheringRequested() throws Exception { + verify(mInterfaceConfiguration).setLinkAddress(any(LinkAddress.class)); + verify(mInterfaceConfiguration).setInterfaceUp(); + verify(mNmService).setInterfaceConfig(eq(TEST_INTERFACE_NAME), eq(mInterfaceConfiguration)); + } + + /** Sends a broadcast intent indicating that the interface is tethered. */ + protected void announceTethered() throws Exception { + ArrayList<String> deviceList = + new ArrayList<String>(Arrays.asList(AVAILABLE_DEVICES)); + TestUtil.sendTetherStateChanged( + mBroadcastReceiver, mContext, deviceList, deviceList); + mLooper.dispatchAll(); + } } |