# 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()
# 
# def run_pending_events():
#     """
#     Iterate event loop until all pending events are cleared
#     """
#     main_context = GLib.MainContext.default()
#     while main_context.pending():
#         main_context.iteration(False)
# 
# Path: examples/cpu_temperature.py
# CPU_TMP_SRVC = '12341000-1234-1234-1234-123456789abc'
# CPU_TMP_CHRC = '2A6E'
# CPU_FMT_DSCP = '2904'
# def read_value():
# def update_value(characteristic):
# def notify_callback(notifying, characteristic):
# def main(adapter_address):


import dbus
import dbusmock
import subprocess
from pathlib import Path
from unittest import mock
from tests.mock_eventloop import MockAsync, run_pending_events
from examples import cpu_temperature
            template, {}, stdout=subprocess.PIPE)

    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_create_peripheral(self):
        # Test currently has limited value because the RegisterApplication
        # does not publish the peripheral with dbusmock
        # (dbusmock_templates/bluez_scan.py) so further testing cannot happen.

        # Add adapter
        path0 = self.dbusmock_bluez.AddAdapter('hci0', 'My-Peripheral')
        path1 = self.dbusmock_bluez.AddAdapter('hci1', 'My-Adapter')

        # Setup to monitor calls
        bus = self.get_dbus(True)
        peri_obj = bus.get_object('org.bluez', path1)
        peri_mock = dbus.Interface(peri_obj, dbusmock.MOCK_IFACE)
        adptr_obj = bus.get_object('org.bluez', path0)
        adptr_mock = dbus.Interface(adptr_obj, dbusmock.MOCK_IFACE)
        # Create
        with mock.patch('bluezero.async_tools.EventLoop.run', MockAsync.run):

# 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()
# 
# def run_pending_events():
#     """
#     Iterate event loop until all pending events are cleared
#     """
#     main_context = GLib.MainContext.default()
#     while main_context.pending():
#         main_context.iteration(False)
# 
# Path: examples/cpu_temperature.py
# CPU_TMP_SRVC = '12341000-1234-1234-1234-123456789abc'
# CPU_TMP_CHRC = '2A6E'
# CPU_FMT_DSCP = '2904'
# def read_value():
# def update_value(characteristic):
# def notify_callback(notifying, characteristic):
# def main(adapter_address):


# Path: tests/test_peripheral_db_mock.py
import dbus
import dbusmock
import subprocess
from pathlib import Path
from unittest import mock
from tests.mock_eventloop import MockAsync, run_pending_events
from examples import cpu_temperature

            template, {}, stdout=subprocess.PIPE)

    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_create_peripheral(self):
        # Test currently has limited value because the RegisterApplication
        # does not publish the peripheral with dbusmock
        # (dbusmock_templates/bluez_scan.py) so further testing cannot happen.

        # Add adapter
        path0 = self.dbusmock_bluez.AddAdapter('hci0', 'My-Peripheral')
        path1 = self.dbusmock_bluez.AddAdapter('hci1', 'My-Adapter')

        # Setup to monitor calls
        bus = self.get_dbus(True)
        peri_obj = bus.get_object('org.bluez', path1)
        peri_mock = dbus.Interface(peri_obj, dbusmock.MOCK_IFACE)
        adptr_obj = bus.get_object('org.bluez', path0)
        adptr_mock = dbus.Interface(adptr_obj, dbusmock.MOCK_IFACE)
        # Create
        with mock.patch('bluezero.async_tools.EventLoop.run', MockAsync.run):

            cpu_temperature.main(adapter_address='00:01:02:03:04:05')