OSDN Git Service

Add HciMatchers.Exactly, for more compact tests
authorZach Johnson <zachoverflow@google.com>
Sat, 14 Nov 2020 21:28:09 +0000 (13:28 -0800)
committerZach Johnson <zachoverflow@google.com>
Wed, 18 Nov 2020 22:12:43 +0000 (14:12 -0800)
Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --host
Change-Id: If87927d3e491f234000b7c9321832897e91bba04

gd/cert/matchers.py
gd/hal/cert/simple_hal_test.py

index 6653d15..f57a521 100644 (file)
@@ -14,6 +14,7 @@
 #   See the License for the specific language governing permissions and
 #   limitations under the License.
 
+import logging
 import bluetooth_packets_python3 as bt_packets
 import logging
 
@@ -165,7 +166,11 @@ class HciMatchers(object):
 
     @staticmethod
     def LoopbackOf(packet):
-        data = bytes(hci_packets.LoopbackCommandBuilder(packet).Serialize())
+        return HciMatchers.Exactly(hci_packets.LoopbackCommandBuilder(packet))
+
+    @staticmethod
+    def Exactly(packet):
+        data = bytes(packet.Serialize())
         return lambda event: data == event.payload
 
 
index f983680..d8be7b2 100644 (file)
@@ -20,6 +20,7 @@ from cert.gd_base_test import GdBaseTestClass
 from cert.event_stream import EventStream
 from cert.truth import assertThat
 from cert.py_hal import PyHal
+from cert.matchers import HciMatchers
 from google.protobuf import empty_pb2
 from facade import rootservice_pb2 as facade_rootservice_pb2
 from hal import facade_pb2 as hal_facade_pb2
@@ -50,12 +51,12 @@ class SimpleHalTest(GdBaseTestClass):
         self.cert_hal.close()
         super().teardown_test()
 
-    def test_fetch_hci_event(self):
+    def test_stream_events(self):
         self.dut_hal.send_hci_command(
             hci_packets.LeAddDeviceToConnectListBuilder(hci_packets.ConnectListAddressType.RANDOM, '0C:05:04:03:02:01'))
-        event = hci_packets.LeAddDeviceToConnectListCompleteBuilder(1, hci_packets.ErrorCode.SUCCESS)
 
-        assertThat(self.dut_hal.get_hci_event_stream()).emits(lambda packet: bytes(event.Serialize()) in packet.payload)
+        assertThat(self.dut_hal.get_hci_event_stream()).emits(
+            HciMatchers.Exactly(hci_packets.LeAddDeviceToConnectListCompleteBuilder(1, hci_packets.ErrorCode.SUCCESS)))
 
     def test_loopback_hci_command(self):
         self.dut_hal.send_hci_command(hci_packets.WriteLoopbackModeBuilder(hci_packets.LoopbackMode.ENABLE_LOCAL))
@@ -64,8 +65,7 @@ class SimpleHalTest(GdBaseTestClass):
                                                               '0C:05:04:03:02:01')
         self.dut_hal.send_hci_command(command)
 
-        assertThat(
-            self.dut_hal.get_hci_event_stream()).emits(lambda packet: bytes(command.Serialize()) in packet.payload)
+        assertThat(self.dut_hal.get_hci_event_stream()).emits(HciMatchers.LoopbackOf(command))
 
     def test_inquiry_from_dut(self):
         self.cert_hal.send_hci_command(hci_packets.WriteScanEnableBuilder(hci_packets.ScanEnable.INQUIRY_AND_PAGE_SCAN))