# Path: tests/mock_eventloop.py
# class MockAsync:
# 
#     def __init__(self):
#         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
#         self.mainloop = GLib.MainLoop()
# 
#     def run(self):
#         main_context = GLib.MainContext.default()
#         while main_context.pending():
#             main_context.iteration(False)
# 
#     def quit(self):
#         self.mainloop.quit()
# 
# Path: examples/microbit_uart.py
# def main(adapter_addrss, device_address):
#     def goodbye(data):


import dbus
import dbusmock
import io
import logging
import subprocess
from pathlib import Path
from unittest import mock
from tests.mock_eventloop import MockAsync
from examples import microbit_uart
    def setUp(self):
        self.obj_bluez.Reset()
        self.dbusmock = dbus.Interface(self.obj_bluez, dbusmock.MOCK_IFACE)
        self.dbusmock_bluez = dbus.Interface(self.obj_bluez, 'org.bluez.Mock')

    @classmethod
    def tearDownClass(cls):
        cls.stop_dbus(cls.system_bus_pid)
        cls.p_mock.terminate()
        cls.p_mock.wait()

    def test_run_uart_example(self):
        # Horrible hardcoded path that refers to characteristic defined in
        # microbit_data. Needs refactoring but this was quick to prove it
        # worked as a test methodology
        tx_path = '/org/bluez/hci0/dev_DD_02_02_02_02_02/service0028/char002b'
        rx_path = '/org/bluez/hci0/dev_DD_02_02_02_02_02/service0028/char0029'
        self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')
        path = self.dbusmock_bluez.AddDevice('hci0',
                                             'DD:02:02:02:02:02',
                                             'micro:bit[uart]')
        bus = self.get_dbus(True)
        dev_obj = bus.get_object('org.bluez', path)
        dev_mock = dbus.Interface(dev_obj, dbusmock.MOCK_IFACE)
        rx_obj = bus.get_object('org.bluez', rx_path)
        rx_mock = dbus.Interface(rx_obj, dbusmock.MOCK_IFACE)
        tx_obj = bus.get_object('org.bluez', tx_path)
        tx_mock = dbus.Interface(tx_obj, dbusmock.MOCK_IFACE)
        with mock.patch('bluezero.async_tools.EventLoop', MockAsync):
            with mock.patch('sys.stdout', new=io.StringIO()) as fake_out:

# Path: tests/mock_eventloop.py
# class MockAsync:
# 
#     def __init__(self):
#         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
#         self.mainloop = GLib.MainLoop()
# 
#     def run(self):
#         main_context = GLib.MainContext.default()
#         while main_context.pending():
#             main_context.iteration(False)
# 
#     def quit(self):
#         self.mainloop.quit()
# 
# Path: examples/microbit_uart.py
# def main(adapter_addrss, device_address):
#     def goodbye(data):


# Path: tests/test_microbit_uart_db_mock.py
import dbus
import dbusmock
import io
import logging
import subprocess
from pathlib import Path
from unittest import mock
from tests.mock_eventloop import MockAsync
from examples import microbit_uart

    def setUp(self):
        self.obj_bluez.Reset()
        self.dbusmock = dbus.Interface(self.obj_bluez, dbusmock.MOCK_IFACE)
        self.dbusmock_bluez = dbus.Interface(self.obj_bluez, 'org.bluez.Mock')

    @classmethod
    def tearDownClass(cls):
        cls.stop_dbus(cls.system_bus_pid)
        cls.p_mock.terminate()
        cls.p_mock.wait()

    def test_run_uart_example(self):
        # Horrible hardcoded path that refers to characteristic defined in
        # microbit_data. Needs refactoring but this was quick to prove it
        # worked as a test methodology
        tx_path = '/org/bluez/hci0/dev_DD_02_02_02_02_02/service0028/char002b'
        rx_path = '/org/bluez/hci0/dev_DD_02_02_02_02_02/service0028/char0029'
        self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')
        path = self.dbusmock_bluez.AddDevice('hci0',
                                             'DD:02:02:02:02:02',
                                             'micro:bit[uart]')
        bus = self.get_dbus(True)
        dev_obj = bus.get_object('org.bluez', path)
        dev_mock = dbus.Interface(dev_obj, dbusmock.MOCK_IFACE)
        rx_obj = bus.get_object('org.bluez', rx_path)
        rx_mock = dbus.Interface(rx_obj, dbusmock.MOCK_IFACE)
        tx_obj = bus.get_object('org.bluez', tx_path)
        tx_mock = dbus.Interface(tx_obj, dbusmock.MOCK_IFACE)
        with mock.patch('bluezero.async_tools.EventLoop', MockAsync):
            with mock.patch('sys.stdout', new=io.StringIO()) as fake_out:

                microbit_uart.main('00:01:02:03:04:05', 'DD:02:02:02:02:02')