| 1 | #!/usr/bin/env python3 |
| 2 | import unittest |
| 3 | import subprocess |
| 4 | import sys |
| 5 | import os |
| 6 | |
| 7 | class TestBluezResponse(unittest.TestCase): |
| 8 | |
| 9 | devices = {} |
| 10 | |
| 11 | def setUp(self): |
| 12 | # bluetoothd starts on demand, so make sure it's running |
| 13 | subprocess.call(['service', 'bluetooth', 'start']) |
| 14 | p1 = subprocess.Popen(['hciconfig'], |
| 15 | stdout=subprocess.PIPE, |
| 16 | universal_newlines=True) |
| 17 | p2 = subprocess.Popen(['grep', r'\(^hci\|BD\ Address\)'], |
| 18 | stdin=p1.stdout, stdout=subprocess.PIPE, |
| 19 | universal_newlines=True) |
| 20 | p1.stdout.close() |
| 21 | hciconf_output = p2.communicate()[0].replace('\t', ' ').split('\n') |
| 22 | |
| 23 | device_id = "" |
| 24 | for line in hciconf_output: |
| 25 | if "hci" in line: |
| 26 | device_id = line.split(':')[0] |
| 27 | elif "BD Address" in line: |
| 28 | self.devices[device_id] = line.split()[2] |
| 29 | |
| 30 | if len(self.devices) < 1: |
| 31 | self.skipTest("No bluetooth devices available for testing") |
| 32 | |
| 33 | def testDevice(self): |
| 34 | for dev in self.devices: |
| 35 | ret = subprocess.call(['/usr/share/doc/bluez-test-scripts/examples/list-devices']) |
| 36 | self.assertEqual(ret, 0) |
| 37 | |
| 38 | def testAdapter(self): |
| 39 | for dev in self.devices: |
| 40 | output = subprocess.check_output(['/usr/share/doc/bluez-test-scripts/examples/test-adapter', '-i', dev, 'address'], |
| 41 | universal_newlines=True) |
| 42 | self.assertIn(self.devices[dev], output) |
| 43 | |
| 44 | unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout, verbosity=2)) |