Add network connectivity check to wifi tests

This CL adds a connectivity check before each tests and does not issue a
connect command if the DUT is already connected to the correct network.
This helps save test time in situations where tests are repeated in the
same configuration wherein the DUT is probably still connected.

Test: Done
Bug: None

Change-Id: I79c62e97efb8f1d66445837bc4c6718aa2bc325c
Signed-off-by: Omar El Ayach <oelayach@google.com>
diff --git a/acts/tests/google/wifi/WifiPingTest.py b/acts/tests/google/wifi/WifiPingTest.py
index d8b7c11..5530166 100644
--- a/acts/tests/google/wifi/WifiPingTest.py
+++ b/acts/tests/google/wifi/WifiPingTest.py
@@ -131,8 +131,12 @@
             sorted(x['rtt'][round(ignored_fraction * len(x['rtt'])):])
             for x in result['ping_results']
         ]
-        mean_rtt = [statistics.mean(x) for x in sorted_rtt]
-        std_rtt = [statistics.stdev(x) for x in sorted_rtt]
+        try:
+            mean_rtt = [statistics.mean(x) for x in sorted_rtt]
+            std_rtt = [statistics.stdev(x) for x in sorted_rtt]
+        except statistics.StatisticsError:
+            self.log.debug("Ping Result: {}".format(result['ping_results']))
+            self.log.debug("Sorted RTT: {}".format(sorted_rtt))
         rtt_at_test_percentile = [
             x[int((1 - self.testclass_params['rtt_test_percentile'] / 100) *
                   len(x))] for x in sorted_rtt
@@ -369,15 +373,21 @@
         self.dut.go_to_sleep()
         band = self.access_point.band_lookup_by_channel(
             testcase_params['channel'])
-        wutils.reset_wifi(self.dut)
-        self.dut.droid.wifiSetCountryCode(
-            self.testclass_params['country_code'])
-        self.main_network[band]['channel'] = testcase_params['channel']
-        wutils.wifi_connect(
-            self.dut,
-            self.main_network[band],
-            num_of_tries=5,
-            check_connectivity=False)
+        current_network = self.dut.droid.wifiGetConnectionInfo()
+        valid_connection = wutils.validate_connection(self.dut)
+        if valid_connection and current_network['SSID'] == self.main_network[
+                band]['SSID']:
+            self.log.info('Already connected to desired network')
+        else:
+            wutils.reset_wifi(self.dut)
+            self.dut.droid.wifiSetCountryCode(
+                self.testclass_params['country_code'])
+            self.main_network[band]['channel'] = testcase_params['channel']
+            wutils.wifi_connect(
+                self.dut,
+                self.main_network[band],
+                num_of_tries=5,
+                check_connectivity=False)
         self.dut_ip = self.dut.droid.connectivityGetIPv4Addresses('wlan0')[0]
 
     def setup_ping_test(self, testcase_params):
diff --git a/acts/tests/google/wifi/WifiRssiTest.py b/acts/tests/google/wifi/WifiRssiTest.py
index 5345cf0..b383317 100644
--- a/acts/tests/google/wifi/WifiRssiTest.py
+++ b/acts/tests/google/wifi/WifiRssiTest.py
@@ -544,16 +544,22 @@
             asserts.skip('Battery level too low. Skipping test.')
         # Turn screen off to preserve battery
         self.dut.go_to_sleep()
-        wutils.wifi_toggle_state(self.dut, True)
-        wutils.reset_wifi(self.dut)
-        self.main_network[
-            testcase_params['band']]['channel'] = testcase_params['channel']
-        self.dut.droid.wifiSetCountryCode(
-            self.testclass_params['country_code'])
-        wutils.wifi_connect(
-            self.dut,
-            self.main_network[testcase_params['band']],
-            num_of_tries=5)
+        current_network = self.dut.droid.wifiGetConnectionInfo()
+        valid_connection = wutils.validate_connection(self.dut)
+        if valid_connection and current_network['SSID'] == self.main_network[
+                testcase_params['band']]['SSID']:
+            self.log.info('Already connected to desired network')
+        else:
+            wutils.wifi_toggle_state(self.dut, True)
+            wutils.reset_wifi(self.dut)
+            self.main_network[testcase_params['band']][
+                'channel'] = testcase_params['channel']
+            self.dut.droid.wifiSetCountryCode(
+                self.testclass_params['country_code'])
+            wutils.wifi_connect(
+                self.dut,
+                self.main_network[testcase_params['band']],
+                num_of_tries=5)
         self.dut_ip = self.dut.droid.connectivityGetIPv4Addresses('wlan0')[0]
 
     def setup_rssi_test(self, testcase_params):
diff --git a/acts/tests/google/wifi/WifiRvrTest.py b/acts/tests/google/wifi/WifiRvrTest.py
index fa0f704..2641520 100644
--- a/acts/tests/google/wifi/WifiRvrTest.py
+++ b/acts/tests/google/wifi/WifiRvrTest.py
@@ -419,15 +419,21 @@
         self.dut.go_to_sleep()
         band = self.access_point.band_lookup_by_channel(
             testcase_params['channel'])
-        wutils.reset_wifi(self.dut)
-        self.dut.droid.wifiSetCountryCode(
-            self.testclass_params['country_code'])
-        self.main_network[band]['channel'] = testcase_params['channel']
-        wutils.wifi_connect(
-            self.dut,
-            self.main_network[band],
-            num_of_tries=5,
-            check_connectivity=False)
+        current_network = self.dut.droid.wifiGetConnectionInfo()
+        valid_connection = wutils.validate_connection(self.dut)
+        if valid_connection and current_network['SSID'] == self.main_network[
+                band]['SSID']:
+            self.log.info('Already connected to desired network')
+        else:
+            wutils.reset_wifi(self.dut)
+            self.dut.droid.wifiSetCountryCode(
+                self.testclass_params['country_code'])
+            self.main_network[band]['channel'] = testcase_params['channel']
+            wutils.wifi_connect(
+                self.dut,
+                self.main_network[band],
+                num_of_tries=5,
+                check_connectivity=False)
         self.dut_ip = self.dut.droid.connectivityGetIPv4Addresses('wlan0')[0]
 
     def setup_rvr_test(self, testcase_params):
diff --git a/acts/tests/google/wifi/WifiSensitivityTest.py b/acts/tests/google/wifi/WifiSensitivityTest.py
index be741d0..944fd6d 100644
--- a/acts/tests/google/wifi/WifiSensitivityTest.py
+++ b/acts/tests/google/wifi/WifiSensitivityTest.py
@@ -364,17 +364,22 @@
         self.dut.go_to_sleep()
         band = self.access_point.band_lookup_by_channel(
             testcase_params['channel'])
-        wutils.reset_wifi(self.dut)
-        self.dut.droid.wifiSetCountryCode(
-            self.testclass_params['country_code'])
-        self.main_network[band]['channel'] = testcase_params['channel']
-        wutils.wifi_connect(
-            self.dut,
-            self.main_network[band],
-            num_of_tries=5,
-            check_connectivity=False)
-        self.dut_ip = self.dut.droid.connectivityGetIPv4Addresses(
-            'wlan0')[0]
+        current_network = self.dut.droid.wifiGetConnectionInfo()
+        valid_connection = wutils.validate_connection(self.dut)
+        if valid_connection and current_network['SSID'] == self.main_network[
+                band]['SSID']:
+            self.log.info('Already connected to desired network')
+        else:
+            wutils.reset_wifi(self.dut)
+            self.dut.droid.wifiSetCountryCode(
+                self.testclass_params['country_code'])
+            self.main_network[band]['channel'] = testcase_params['channel']
+            wutils.wifi_connect(
+                self.dut,
+                self.main_network[band],
+                num_of_tries=5,
+                check_connectivity=False)
+        self.dut_ip = self.dut.droid.connectivityGetIPv4Addresses('wlan0')[0]
         atten_dut_chain_map = wputils.get_atten_dut_chain_map(
             self.attenuators, self.dut, self.ping_server, self.dut_ip)
         for idx, atten in enumerate(self.attenuators):
diff --git a/acts/tests/google/wifi/WifiThroughputStabilityTest.py b/acts/tests/google/wifi/WifiThroughputStabilityTest.py
index 071b5ae..6c3c410 100644
--- a/acts/tests/google/wifi/WifiThroughputStabilityTest.py
+++ b/acts/tests/google/wifi/WifiThroughputStabilityTest.py
@@ -253,16 +253,22 @@
         self.dut.go_to_sleep()
         band = self.access_point.band_lookup_by_channel(
             testcase_params['channel'])
-        wutils.wifi_toggle_state(self.dut, True)
-        wutils.reset_wifi(self.dut)
-        self.dut.droid.wifiSetCountryCode(
-            self.testclass_params['country_code'])
-        self.main_network[band]['channel'] = testcase_params['channel']
-        wutils.wifi_connect(
-            self.dut,
-            self.main_network[band],
-            num_of_tries=5,
-            check_connectivity=False)
+        current_network = self.dut.droid.wifiGetConnectionInfo()
+        valid_connection = wutils.validate_connection(self.dut)
+        if valid_connection and current_network['SSID'] == self.main_network[
+                band]['SSID']:
+            self.log.info('Already connected to desired network')
+        else:
+            wutils.wifi_toggle_state(self.dut, True)
+            wutils.reset_wifi(self.dut)
+            self.dut.droid.wifiSetCountryCode(
+                self.testclass_params['country_code'])
+            self.main_network[band]['channel'] = testcase_params['channel']
+            wutils.wifi_connect(
+                self.dut,
+                self.main_network[band],
+                num_of_tries=5,
+                check_connectivity=False)
         self.dut_ip = self.dut.droid.connectivityGetIPv4Addresses('wlan0')[0]
 
     def setup_throughput_stability_test(self, testcase_params):