Add new test BtFactoryResetTest and modify BtStressTest

Add a new test to cover the factoryReset method
in BluetoothAdapter.

Modify the BtStressTest to return failure on first
fail instead of running through all iterations
before determining failure.

Bug: b/30016055
Change-Id: Ie9a86731dc9330fd04b17502cd1aeda44adf75b2
(cherry picked from commit 69cab4ef28619ea66d2b4aea4a1a1a6fcda81206)
diff --git a/acts/tests/google/bt/BtFactoryResetTest.py b/acts/tests/google/bt/BtFactoryResetTest.py
new file mode 100644
index 0000000..2c07492
--- /dev/null
+++ b/acts/tests/google/bt/BtFactoryResetTest.py
@@ -0,0 +1,65 @@
+#/usr/bin/env python3.4
+#
+# 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.
+"""
+Test script to test BluetoothAdapter's resetFactory method.
+"""
+from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
+from acts.test_utils.bt.bt_test_utils import pair_pri_to_sec
+
+
+class BtFactoryResetTest(BluetoothBaseTest):
+    default_timeout = 10
+    grace_timeout = 4
+
+    def __init__(self, controllers):
+        BluetoothBaseTest.__init__(self, controllers)
+        self.pri_dut = self.android_devices[0]
+        self.sec_dut = self.android_devices[1]
+
+    @BluetoothBaseTest.bt_test_wrap
+    def test_factory_reset_bluetooth(self):
+        """Test that BluetoothAdapter.factoryReset removes bonded devices
+
+        After having bonded devices, call the factoryReset method
+        in BluetoothAdapter to clear all bonded devices. Verify that
+        no bonded devices exist after calling function.
+
+        Steps:
+        1. Two Android devices should bond successfully.
+        2. Factory Reset Bluetooth on dut.
+        3. Verify that there are no bonded devices on dut.
+
+        Expected Result:
+        BluetoothAdapter should not have any bonded devices.
+
+        Returns:
+          Pass if True
+          Fail if False
+
+        TAGS: Bluetooth, Factory Reset
+        Priority: 2
+        """
+        if not pair_pri_to_sec(self.pri_dut.droid, self.sec_dut.droid):
+            self.log.error("Failed to bond devices.")
+            return False
+        if not self.pri_dut.droid.bluetoothFactoryReset():
+            self.log.error("BluetoothAdapter failed to factory reset.")
+        self.log.info("Verifying There are no bonded devices.")
+        if len(self.pri_dut.droid.bluetoothGetBondedDevices()) > 0:
+            print(self.pri_dut.droid.bluetoothGetBondedDevices())
+            self.log.error("Bonded devices still exist.")
+            return False
+        return True
diff --git a/acts/tests/google/bt/system_tests/BtStressTest.py b/acts/tests/google/bt/system_tests/BtStressTest.py
index 193fa52..d1d452b 100644
--- a/acts/tests/google/bt/system_tests/BtStressTest.py
+++ b/acts/tests/google/bt/system_tests/BtStressTest.py
@@ -27,6 +27,7 @@
 
 class BtStressTest(BaseTestClass):
     default_timeout = 10
+    iterations = 100
 
     def __init__(self, controllers):
         BaseTestClass.__init__(self, controllers)
@@ -69,19 +70,12 @@
         TAGS: Classic, Stress
         Priority: 1
         """
-        test_result = True
-        test_result_list = []
-        for n in range(100):
+        for n in range(self.iterations):
             self.log.info("Toggling bluetooth iteration {}.".format(n + 1))
-            test_result = reset_bluetooth([self.android_devices[0]])
-            test_result_list.append(test_result)
-            if not test_result:
-                self.log.debug("Failure to reset Bluetooth... continuing")
-        self.log.info("Toggling Bluetooth failed {}/100 times".format(len(
-            test_result_list)))
-        if False in test_result_list:
-            return False
-        return test_result
+            if not reset_bluetooth([self.android_devices[0]]):
+                self.log.error("Failure to reset Bluetooth")
+                return False
+        return True
 
     def test_pair_bluetooth_stress(self):
         """Stress test for pairing BT devices.
@@ -106,10 +100,10 @@
         TAGS: Classic, Stress
         Priority: 1
         """
-        for n in range(100):
+        for n in range(self.iterations):
             self.log.info("Pair bluetooth iteration {}.".format(n + 1))
-            if (pair_pri_to_sec(self.android_devices[0].droid,
-                                self.android_devices[1].droid) == False):
+            if (not pair_pri_to_sec(self.android_devices[0].droid,
+                                self.android_devices[1].droid)):
                 self.log.error("Failed to bond devices.")
                 return False
             for ad in self.android_devices: