Add new testcases and reboot option

Add new testcases for beacon scanning
and add a reboot option for really
clean running tests.

Change-Id: I0c8650789b8720938822424d8ee6c662ba51fd8f
(cherry picked from commit 49a23d7017e74e484eec2bdcd647bb05d8441de3)
diff --git a/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py b/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py
index 81f023c..2cde519 100644
--- a/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py
+++ b/acts/framework/acts/test_utils/bt/BluetoothBaseTest.py
@@ -20,12 +20,14 @@
 import os
 import time
 from acts import utils
+from acts.utils import set_location_service
 from acts.base_test import BaseTestClass
 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,
     take_btsnoop_logs)
 from acts.utils import sync_device_time
+import threading
 
 
 class BluetoothBaseTest(BaseTestClass):
@@ -49,8 +51,21 @@
 
         return _safe_wrap_test_case
 
+    def _reboot_device(self, ad):
+        self.log.info("Rebooting device {}.".format(ad.serial))
+        ad.reboot()
+
     def setup_class(self):
+        if "reboot_between_test_class" in self.user_params:
+            threads = []
+            for a in self.android_devices:
+                thread = threading.Thread(target=self._reboot_device, args=([a]))
+                threads.append(thread)
+                thread.start()
+            for t in threads:
+                t.join()
         for a in self.android_devices:
+            set_location_service(a, False)
             sync_device_time(a)
         return setup_multiple_devices_for_bt_test(self.android_devices)
 
diff --git a/acts/tests/google/ble/filtering/UniqueFilteringTest.py b/acts/tests/google/ble/filtering/UniqueFilteringTest.py
index d091102..f336052 100644
--- a/acts/tests/google/ble/filtering/UniqueFilteringTest.py
+++ b/acts/tests/google/ble/filtering/UniqueFilteringTest.py
@@ -19,6 +19,7 @@
 """
 
 import concurrent
+import json
 import pprint
 import time
 
@@ -28,6 +29,7 @@
 from acts.test_utils.bt.BleEnum import ScanSettingsScanMode
 from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
 from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
+from acts.test_utils.bt.bt_test_utils import adv_succ
 from acts.test_utils.bt.bt_test_utils import batch_scan_result
 from acts.test_utils.bt.bt_test_utils import scan_result
 
@@ -64,10 +66,7 @@
         return event['data']['Result']['deviceInfo']['address']
 
     def blescan_verify_onbatchscanresult_event_handler(
-            self,
-            event,
-            system_time_nanos=None,
-            report_delay_nanos=None):
+            self, event, system_time_nanos=None, report_delay_nanos=None):
         test_result = True
         self.log.debug("Verifying onBatchScanResult event")
         self.log.debug(pprint.pformat(event))
@@ -378,13 +377,13 @@
         test_result = True
         self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode(
             AdvertiseSettingsAdvertiseMode.ADVERTISE_MODE_LOW_LATENCY.value)
-        self.adv_ad.droid.bleAddAdvertiseDataManufacturerId(117, [1,2,3])
+        self.adv_ad.droid.bleAddAdvertiseDataManufacturerId(117, [1, 2, 3])
         advertise_callback, advertise_data, advertise_settings = (
             generate_ble_advertise_objects(self.adv_ad.droid))
         self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode(
             AdvertiseSettingsAdvertiseMode.ADVERTISE_MODE_LOW_LATENCY.value)
-        self.adv_ad.droid.bleAddAdvertiseDataManufacturerId(117,
-                                                            [1,2,3,4,5,6,7,8])
+        self.adv_ad.droid.bleAddAdvertiseDataManufacturerId(
+            117, [1, 2, 3, 4, 5, 6, 7, 8])
         advertise_callback1, advertise_data1, advertise_settings1 = (
             generate_ble_advertise_objects(self.adv_ad.droid))
         self.adv_ad.droid.bleStartBleAdvertising(
@@ -397,8 +396,8 @@
             ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value)
         scan_settings = self.scn_ad.droid.bleBuildScanSetting()
         scan_callback = self.scn_ad.droid.bleGenScanCallback()
-        self.scn_ad.droid.bleSetScanFilterManufacturerData(117, [1,2,3],
-                                                           [127,127,127])
+        self.scn_ad.droid.bleSetScanFilterManufacturerData(117, [1, 2, 3],
+                                                           [127, 127, 127])
         self.scn_ad.droid.bleBuildScanFilter(filter_list)
         self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
                                           scan_callback)
@@ -469,3 +468,151 @@
         self.scn_ad.droid.bleStopBleScan(scan_callback2)
         self.adv_ad.droid.bleStopBleAdvertising(advertise_callback)
         return test_result
+
+    @BluetoothBaseTest.bt_test_wrap
+    def test_filter_simulated_ibeacon(self):
+        """Test scan filtering of a simulated ibeacon.
+
+        This test will setup one Android device as an ibeacon and
+        a second Android device will be used to test filtering of
+        the manufacturer data for 60 seconds.
+
+        Steps:
+        1. Start an advertisement with manufacturer id set to 0x004c
+        2. Start a generic scanner.
+        3. Find the advertisement and extract the mac address.
+        4. Stop the first scanner.
+        5. Create a new scanner with scan filter with a mac address filter of
+        what was found in step 3.
+        6. Start the scanner.
+
+        Expected Result:
+        Verify that the advertisement was found in the second scan instance.
+
+        Returns:
+          Pass if True
+          Fail if False
+
+        TAGS: LE, Advertising, Filtering, Scanning
+        Priority: 1
+        """
+        manufacturer_id = 0x4c
+        self.adv_ad.droid.bleAddAdvertiseDataManufacturerId(manufacturer_id, [0x01])
+        self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode(
+            AdvertiseSettingsAdvertiseMode.ADVERTISE_MODE_LOW_LATENCY.value)
+        advertise_callback, advertise_data, advertise_settings = (
+            generate_ble_advertise_objects(self.adv_ad.droid))
+        self.adv_ad.droid.bleStartBleAdvertising(
+            advertise_callback, advertise_data, advertise_settings)
+        expected_event = adv_succ.format(advertise_callback)
+        try:
+            self.adv_ad.ed.pop_event(expected_event)
+        except Empty:
+            self.log.info("Failed to start advertisement.")
+            return False
+
+        self.scn_ad.droid.bleSetScanSettingsScanMode(
+            ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value)
+        self.scn_ad.droid.bleSetScanFilterManufacturerData(manufacturer_id, [0x01])
+        filter_list = self.scn_ad.droid.bleGenFilterList()
+        scan_settings = self.scn_ad.droid.bleBuildScanSetting()
+        scan_filter = self.scn_ad.droid.bleBuildScanFilter(filter_list)
+        scan_callback = self.scn_ad.droid.bleGenScanCallback()
+        self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
+                                          scan_callback)
+        # continuously scan for 60 seconds
+        scan_time = 60
+        end_time = time.time() + scan_time
+        expected_event_name = scan_result.format(scan_callback)
+        event = None
+        while time.time() < end_time:
+            try:
+                event = self.scn_ad.ed.pop_event(expected_event_name,
+                                                 self.default_timeout)
+                found_manufacturer_id = json.loads(event['data']['Result'][
+                    'manufacturerIdList'])
+                if found_manufacturer_id[0] != manufacturer_id:
+                    self.log.error(
+                        "Manufacturer id mismatch. Found {}, Expected {}".
+                        format(found_manufacturer_id, manufacturer_id))
+                    return False
+            except Empty:
+                self.log.error("Unable to find ibeacon advertisement.")
+                return False
+        self.scn_ad.droid.bleStopBleScan(scan_callback)
+        self.adv_ad.droid.bleStopBleAdvertising(advertise_callback)
+        return True
+
+    @BluetoothBaseTest.bt_test_wrap
+    def test_filter_manufacturer_id_bounds(self):
+        """Test scan filtering of lower and upper bounds of allowed manu data
+
+        This test will setup one Android device as an advertiser and the
+        second as the scanner and test the upper and lower bounds of
+        manufacturer data filtering
+
+        Steps:
+        1. Start an advertisement with manufacturer id set to 0x004c
+        2. Start a generic scanner.
+        3. Find the advertisement and extract the mac address.
+        4. Stop the first scanner.
+        5. Create a new scanner with scan filter with a mac address filter of
+        what was found in step 3.
+        6. Start the scanner.
+
+        Expected Result:
+        Verify that the advertisement was found in the second scan instance.
+
+        Returns:
+          Pass if True
+          Fail if False
+
+        TAGS: LE, Advertising, Filtering, Scanning
+        Priority: 1
+        """
+        manufacturer_id_list = [0, 1, 65534, 65535]
+        for manufacturer_id in manufacturer_id_list:
+            self.adv_ad.droid.bleAddAdvertiseDataManufacturerId(
+                manufacturer_id, [0x01])
+            self.adv_ad.droid.bleSetAdvertiseSettingsAdvertiseMode(
+                AdvertiseSettingsAdvertiseMode.ADVERTISE_MODE_LOW_LATENCY.
+                value)
+            advertise_callback, advertise_data, advertise_settings = (
+                generate_ble_advertise_objects(self.adv_ad.droid))
+            self.adv_ad.droid.bleStartBleAdvertising(
+                advertise_callback, advertise_data, advertise_settings)
+            expected_event = adv_succ.format(advertise_callback)
+            try:
+                self.adv_ad.ed.pop_event(expected_event)
+            except Empty:
+                self.log.info("Failed to start advertisement.")
+                return False
+
+            self.scn_ad.droid.bleSetScanSettingsScanMode(
+                ScanSettingsScanMode.SCAN_MODE_LOW_LATENCY.value)
+            self.scn_ad.droid.bleSetScanFilterManufacturerData(manufacturer_id,
+                                                               [0x01])
+            filter_list = self.scn_ad.droid.bleGenFilterList()
+            scan_settings = self.scn_ad.droid.bleBuildScanSetting()
+            scan_filter = self.scn_ad.droid.bleBuildScanFilter(filter_list)
+            scan_callback = self.scn_ad.droid.bleGenScanCallback()
+            self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
+                                              scan_callback)
+            expected_event_name = scan_result.format(scan_callback)
+            event = None
+            try:
+                event = self.scn_ad.ed.pop_event(expected_event_name,
+                                                 self.default_timeout)
+            except Empty:
+                self.log.error("Unable to find beacon advertisement.")
+                return False
+            found_manufacturer_id = json.loads(event['data']['Result'][
+                    'manufacturerIdList'])
+            if found_manufacturer_id[0] != manufacturer_id:
+                self.log.error(
+                    "Manufacturer id mismatch. Found {}, Expected {}".
+                    format(found_manufacturer_id, manufacturer_id))
+                return False
+            self.scn_ad.droid.bleStopBleScan(scan_callback)
+            self.adv_ad.droid.bleStopBleAdvertising(advertise_callback)
+        return True
diff --git a/acts/tests/google/ble/scan/BleOpportunisticScanTest.py b/acts/tests/google/ble/scan/BleOpportunisticScanTest.py
index 40ececa..3408449 100644
--- a/acts/tests/google/ble/scan/BleOpportunisticScanTest.py
+++ b/acts/tests/google/ble/scan/BleOpportunisticScanTest.py
@@ -549,6 +549,7 @@
         self.scn_ad.droid.bleSetScanFilterDeviceName("opp_test")
         filter_list2, scan_settings2, scan_callback2 = (
             generate_ble_scan_objects(self.scn_ad.droid))
+        self.scn_ad.droid.bleBuildScanFilter(filter_list2)
         self.scn_ad.droid.bleStartBleScan(filter_list2, scan_settings2,
                                           scan_callback2)
         self.active_scan_callback_list.append(scan_callback2)
@@ -600,6 +601,7 @@
         self.scn_ad.droid.bleSetScanFilterDeviceName("opp_test")
         filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
             self.scn_ad.droid)
+        self.scn_ad.droid.bleBuildScanFilter(filter_list)
         self.scn_ad.droid.bleStartBleScan(filter_list, scan_settings,
                                           scan_callback)
         self.active_scan_callback_list.append(scan_callback)
diff --git a/acts/tests/google/bt/system_tests/RfcommStressTest.py b/acts/tests/google/bt/system_tests/RfcommStressTest.py
index d3df822..bf24d9c 100644
--- a/acts/tests/google/bt/system_tests/RfcommStressTest.py
+++ b/acts/tests/google/bt/system_tests/RfcommStressTest.py
@@ -70,7 +70,7 @@
         TAGS: Classic, Stress, RFCOMM
         Priority: 1
         """
-        iterations = 10000
+        iterations = 1000
         for n in range(iterations):
             if not orchestrate_rfcomm_connection(self.client_ad,
                                                  self.server_ad):