Rerun failed tests and prevent energy info collection

Provide the option to rerun tests in case of
failures.

Prevent BluetoothAdapter.getControllerActivityEnergyInfo
from making calls in tests.

Fix enum values to be compatible with py2 and py3.

Bug: 31966929
Change-Id: Ic9365ce2b04ab2c37b3f5d72e74f65e5a350b10e
(cherry picked from commit b4c57fa6250accd1e8b0ad7f7cb67ccbd25637b9)
diff --git a/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py b/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py
index 2cde519..11abd07 100644
--- a/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py
+++ b/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py
@@ -19,9 +19,11 @@
 
 import os
 import time
+import traceback
 from acts import utils
-from acts.utils import set_location_service
 from acts.base_test import BaseTestClass
+from acts.signals import TestSignal
+from acts.utils import set_location_service
 from acts.controllers import android_device
 from acts.test_utils.bt.bt_test_utils import (
     log_energy_info, reset_bluetooth, setup_multiple_devices_for_bt_test,
@@ -47,13 +49,46 @@
                                         time.time())
             log_string = "[Test ID] {}".format(test_id)
             self.log.info(log_string)
+            try:
+                for ad in self.android_devices:
+                    ad.droid.logI("Started " + log_string)
+                result = fn(self, *args, **kwargs)
+                for ad in self.android_devices:
+                    ad.droid.logI("Finished " + log_string)
+                if result is not True and "bt_auto_rerun" in self.user_params:
+                    self.teardown_test()
+                    log_string = "[Rerun Test ID] {}. 1st run failed.".format(
+                        test_id)
+                    self.log.info(log_string)
+                    self.setup_test()
+                    for ad in self.android_devices:
+                        ad.droid.logI("Rerun Started " + log_string)
+                    result = fn(self, *args, **kwargs)
+                    if result is True:
+                        self.log.info("Rerun passed.")
+                    elif result is False:
+                        self.log.info("Rerun failed.")
+                    else:
+                        # In the event that we have a non-bool or null
+                        # retval, we want to clearly distinguish this in the
+                        # logs from an explicit failure, though the test will
+                        # still be considered a failure for reporting purposes.
+                        self.log.info("Rerun indeterminate.")
+                        result = False
+                return result
+            except TestSignal:
+                raise
+            except Exception as e:
+                self.log.error(traceback.format_exc())
+                self.log.error(str(e))
+                return False
             return fn(self, *args, **kwargs)
 
         return _safe_wrap_test_case
 
     def _reboot_device(self, ad):
         self.log.info("Rebooting device {}.".format(ad.serial))
-        ad.reboot()
+        ad = ad.reboot()
 
     def setup_class(self):
         if "reboot_between_test_class" in self.user_params:
diff --git a/acts/framework/acts/test_utils/bt/bt_gatt_utils.py b/acts/framework/acts/test_utils/bt/bt_gatt_utils.py
index 804da9d..b2d8be6 100644
--- a/acts/framework/acts/test_utils/bt/bt_gatt_utils.py
+++ b/acts/framework/acts/test_utils/bt/bt_gatt_utils.py
@@ -48,12 +48,18 @@
     try:
         event = cen_ad.ed.pop_event(expected_event, default_timeout)
     except Empty:
-        cen_ad.droid.gattClientClose(bluetooth_gatt)
+        try:
+            cen_ad.droid.gattClientClose(bluetooth_gatt)
+        except Exception:
+            self.log.debug("Failed to close gatt client.")
         raise GattTestUtilsError("Could not establish a connection to "
                                  "peripheral. Expected event:".format(
                                      expected_event))
     if event['data']['State'] != GattConnectionState.STATE_CONNECTED.value:
-        cen_ad.droid.gattClientClose(bluetooth_gatt)
+        try:
+            cen_ad.droid.gattClientClose(bluetooth_gatt)
+        except Exception:
+            self.log.debug("Failed to close gatt client.")
         raise GattTestUtilsError("Could not establish a connection to "
                                  "peripheral. Event Details:".format(
                                      pprint.pformat(event)))
diff --git a/acts/framework/acts/test_utils/bt/bt_test_utils.py b/acts/framework/acts/test_utils/bt/bt_test_utils.py
index fae4f6e..5d535bd 100644
--- a/acts/framework/acts/test_utils/bt/bt_test_utils.py
+++ b/acts/framework/acts/test_utils/bt/bt_test_utils.py
@@ -468,7 +468,7 @@
                                         advertise_settings)
     try:
         adv_ad.ed.pop_event(
-            "BleAdvertise{}onSuccess".format(advertise_callback),
+            adv_succ.format(advertise_callback),
             DEFAULT_TIMEOUT)
     except Empty as err:
         raise BtTestUtilsError(
@@ -593,6 +593,8 @@
         A logging string of the Bluetooth energy info reported.
     """
     return_string = "{} Energy info collection:\n".format(state)
+    # Bug: b/31966929
+    return return_string
     for d in android_devices:
         if (d.droid.getBuildModel() != "Nexus 5" or d.droid.getBuildModel() != "Nexus 4"):
 
diff --git a/acts/tests/google/ble/gatt/GattConnectTest.py b/acts/tests/google/ble/gatt/GattConnectTest.py
index 9807f37..3a53e8c 100644
--- a/acts/tests/google/ble/gatt/GattConnectTest.py
+++ b/acts/tests/google/ble/gatt/GattConnectTest.py
@@ -75,6 +75,8 @@
         try:
             disconnect_gatt_connection(self.cen_ad, bluetooth_gatt,
                                        gatt_callback)
+            if bluetooth_gatt in self.bluetooth_gatt_list:
+                self.bluetooth_gatt_list.remove(bluetooth_gatt)
         except GattTestUtilsError as err:
             self.log.error(err)
             return False
@@ -269,11 +271,11 @@
         try:
             disconnect_gatt_connection(self.cen_ad, bluetooth_gatt,
                                        gatt_callback)
+            if bluetooth_gatt in self.bluetooth_gatt_list:
+                self.bluetooth_gatt_list.remove(bluetooth_gatt)
         except GattTestUtilsError as err:
             self.log.error(err)
             return False
-        # Test, not sure if we should remove or not
-        #self.cen_ad.droid.gattClientClose(bluetooth_gatt)
         autoconnect = True
         bluetooth_gatt = self.cen_ad.droid.gattClientConnectGatt(
             gatt_callback, mac_address, autoconnect,
diff --git a/acts/tests/google/ble/gatt/GattToolTest.py b/acts/tests/google/ble/gatt/GattToolTest.py
index fda80c2..c25af74 100644
--- a/acts/tests/google/ble/gatt/GattToolTest.py
+++ b/acts/tests/google/ble/gatt/GattToolTest.py
@@ -146,7 +146,7 @@
         try:
             bluetooth_gatt, gatt_callback = (setup_gatt_connection(
                 self.cen_ad, self.ble_mac_address, self.AUTOCONNECT,
-                GattTransport.TRANSPORT_LE))
+                GattTransport.TRANSPORT_LE.value))
         except GattTestUtilsError as err:
             self.log.error(err)
             return False
@@ -202,7 +202,7 @@
             try:
                 bluetooth_gatt, gatt_callback = (setup_gatt_connection(
                     self.cen_ad, self.ble_mac_address, self.AUTOCONNECT,
-                    GattTransport.TRANSPORT_LE))
+                    GattTransport.TRANSPORT_LE.value))
             except GattTestUtilsError as err:
                 self.log.error(err)
                 return False
@@ -248,7 +248,7 @@
         try:
             bluetooth_gatt, gatt_callback = (setup_gatt_connection(
                 self.cen_ad, self.ble_mac_address, self.AUTOCONNECT,
-                GattTransport.TRANSPORT_LE))
+                GattTransport.TRANSPORT_LE.value))
         except GattTestUtilsError as err:
             self.log.error(err)
             return False
@@ -383,7 +383,7 @@
         try:
             bluetooth_gatt, gatt_callback = (setup_gatt_connection(
                 self.cen_ad, self.ble_mac_address, self.AUTOCONNECT,
-                GattTransport.TRANSPORT_LE))
+                GattTransport.TRANSPORT_LE.value))
         except GattTestUtilsError as err:
             self.log.error(err)
             return False
diff --git a/acts/tests/google/bt/gatt/GattOverBrEdrTest.py b/acts/tests/google/bt/gatt/GattOverBrEdrTest.py
index 1d6ac26..aba2635 100644
--- a/acts/tests/google/bt/gatt/GattOverBrEdrTest.py
+++ b/acts/tests/google/bt/gatt/GattOverBrEdrTest.py
@@ -22,7 +22,6 @@
 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
 from acts.test_utils.bt.bt_test_utils import reset_bluetooth
 from acts.test_utils.bt.GattEnum import GattCharacteristic
-from acts.test_utils.bt.GattEnum import GattDescriptor
 from acts.test_utils.bt.GattEnum import GattService
 from acts.test_utils.bt.GattEnum import GattTransport
 from acts.test_utils.bt.GattEnum import MtuSize
@@ -84,6 +83,8 @@
         try:
             disconnect_gatt_connection(self.cen_ad, bluetooth_gatt,
                                        gatt_callback)
+            if bluetooth_gatt in self.bluetooth_gatt_list:
+                self.bluetooth_gatt_list.remove(bluetooth_gatt)
         except GattTestUtilsError as err:
             self.log.error(err)
             return False
@@ -132,7 +133,7 @@
         try:
             bluetooth_gatt, gatt_callback, adv_callback = (
                 orchestrate_gatt_connection(self.cen_ad, self.per_ad,
-                                            GattTransport.TRANSPORT_BREDR,
+                                            GattTransport.TRANSPORT_BREDR.value,
                                             self.per_droid_mac_address))
             self.bluetooth_gatt_list.append(bluetooth_gatt)
         except GattTestUtilsError as err:
@@ -175,7 +176,7 @@
         try:
             bluetooth_gatt, gatt_callback, adv_callback = (
                 orchestrate_gatt_connection(self.cen_ad, self.per_ad,
-                                            GattTransport.TRANSPORT_BREDR,
+                                            GattTransport.TRANSPORT_BREDR.value,
                                             self.per_droid_mac_address))
             self.bluetooth_gatt_list.append(bluetooth_gatt)
         except GattTestUtilsError as err:
@@ -222,7 +223,7 @@
         try:
             bluetooth_gatt, gatt_callback, adv_callback = (
                 orchestrate_gatt_connection(self.cen_ad, self.per_ad,
-                                            GattTransport.TRANSPORT_BREDR,
+                                            GattTransport.TRANSPORT_BREDR.value,
                                             self.per_droid_mac_address))
             self.bluetooth_gatt_list.append(bluetooth_gatt)
         except GattTestUtilsError as err:
@@ -277,7 +278,7 @@
         try:
             bluetooth_gatt, gatt_callback, adv_callback = (
                 orchestrate_gatt_connection(self.cen_ad, self.per_ad,
-                                            GattTransport.TRANSPORT_BREDR,
+                                            GattTransport.TRANSPORT_BREDR.value,
                                             self.per_droid_mac_address))
             self.bluetooth_gatt_list.append(bluetooth_gatt)
         except GattTestUtilsError as err:
@@ -335,7 +336,7 @@
         try:
             bluetooth_gatt, gatt_callback, adv_callback = (
                 orchestrate_gatt_connection(self.cen_ad, self.per_ad,
-                                            GattTransport.TRANSPORT_BREDR,
+                                            GattTransport.TRANSPORT_BREDR.value,
                                             self.per_droid_mac_address))
             self.bluetooth_gatt_list.append(bluetooth_gatt)
         except GattTestUtilsError as err:
@@ -388,7 +389,7 @@
             try:
                 bluetooth_gatt, gatt_callback, adv_callback = (
                     orchestrate_gatt_connection(self.cen_ad, self.per_ad,
-                                                GattTransport.TRANSPORT_BREDR,
+                                                GattTransport.TRANSPORT_BREDR.value,
                                                 self.per_droid_mac_address))
                 self.bluetooth_gatt_list.append(bluetooth_gatt)
             except GattTestUtilsError as err:
@@ -439,16 +440,20 @@
         try:
             bluetooth_gatt, gatt_callback, adv_callback = (
                 orchestrate_gatt_connection(self.cen_ad, self.per_ad,
-                                            GattTransport.TRANSPORT_BREDR,
+                                            GattTransport.TRANSPORT_BREDR.value,
                                             self.per_droid_mac_address))
             self.bluetooth_gatt_list.append(bluetooth_gatt)
         except GattTestUtilsError as err:
             self.log.error(err)
             return False
         if self.cen_ad.droid.gattClientDiscoverServices(bluetooth_gatt):
-            event = self.cen_ad.ed.pop_event(
-                GattCbStrings.GATT_SERV_DISC.value.format(gatt_callback),
-                self.default_timeout)
+            try:
+                event = self.cen_ad.ed.pop_event(
+                    GattCbStrings.GATT_SERV_DISC.value.format(gatt_callback),
+                    self.default_timeout)
+            except Empty as err:
+                self.log.error("Event not found: {}".format(err))
+                return False
             discovered_services_index = event['data']['ServicesIndex']
         else:
             self.log.info("Failed to discover services.")
@@ -504,3 +509,4 @@
                                     GattCbStrings.DESC_WRITE.value.format(
                                         gatt_callback), self.default_timeout)))
         return True
+