# 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: bluezero/dbus_tools.py
# def bluez_version():
# def bluez_experimental_mode():
# def interfaces_added(path, interfaces):
# def properties_changed(interface, changed, invalidated, path):
# def get_dbus_obj(dbus_path):
# def get_dbus_iface(iface, dbus_obj):
# def get_managed_objects():
# def get_mac_addr_from_dbus_path(path):
# def get_device_address_from_dbus_path(path):
# def get_adapter_address_from_dbus_path(path):
# def _get_dbus_path2(objects, parent_path, iface_in, prop, value):
# def get_dbus_path(adapter=None,
#                   device=None,
#                   service=None,
#                   characteristic=None,
#                   descriptor=None):
# def get_profile_path(adapter,
#                      device,
#                      profile):
# def get_iface(adapter=None,
#               device=None,
#               service=None,
#               characteristic=None,
#               descriptor=None):
# def get_methods(adapter=None,
#                 device=None,
#                 service=None,
#                 characteristic=None,
#                 descriptor=None):
# def get_props(adapter=None,
#               device=None,
#               service=None,
#               characteristic=None,
#               descriptor=None):
# def get_services(path_obj):
# def get_device_addresses(name_contains):
# def get(dbus_prop_obj, dbus_iface, prop_name, default=None):
# def str_to_dbusarray(word):
# def bytes_to_dbusarray(bytesarray):
# def dbus_to_python(data):


import dbus
import dbusmock
import io
import subprocess
from pathlib import Path
from unittest import mock
from unittest.mock import patch
from tests.mock_eventloop import MockAsync
from bluezero import dbus_tools

class TestAdapterExample(dbusmock.DBusTestCase):
    """
    Test mocking bluetoothd
    """

    @classmethod
    def setUpClass(cls):
        here = Path(__file__).parent
        template = str(here.joinpath('dbusmock_templates', 'bluez_scan.py'))
        cls.start_system_bus()
        cls.dbus_con = cls.get_dbus(True)
        (cls.p_mock, cls.obj_bluez) = cls.spawn_server_template(
            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_dbus_int64(self):
        self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')


# 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: bluezero/dbus_tools.py
# def bluez_version():
# def bluez_experimental_mode():
# def interfaces_added(path, interfaces):
# def properties_changed(interface, changed, invalidated, path):
# def get_dbus_obj(dbus_path):
# def get_dbus_iface(iface, dbus_obj):
# def get_managed_objects():
# def get_mac_addr_from_dbus_path(path):
# def get_device_address_from_dbus_path(path):
# def get_adapter_address_from_dbus_path(path):
# def _get_dbus_path2(objects, parent_path, iface_in, prop, value):
# def get_dbus_path(adapter=None,
#                   device=None,
#                   service=None,
#                   characteristic=None,
#                   descriptor=None):
# def get_profile_path(adapter,
#                      device,
#                      profile):
# def get_iface(adapter=None,
#               device=None,
#               service=None,
#               characteristic=None,
#               descriptor=None):
# def get_methods(adapter=None,
#                 device=None,
#                 service=None,
#                 characteristic=None,
#                 descriptor=None):
# def get_props(adapter=None,
#               device=None,
#               service=None,
#               characteristic=None,
#               descriptor=None):
# def get_services(path_obj):
# def get_device_addresses(name_contains):
# def get(dbus_prop_obj, dbus_iface, prop_name, default=None):
# def str_to_dbusarray(word):
# def bytes_to_dbusarray(bytesarray):
# def dbus_to_python(data):


# Path: tests/test_dbus_tools_mock.py
import dbus
import dbusmock
import io
import subprocess
from pathlib import Path
from unittest import mock
from unittest.mock import patch
from tests.mock_eventloop import MockAsync
from bluezero import dbus_tools


class TestAdapterExample(dbusmock.DBusTestCase):
    """
    Test mocking bluetoothd
    """

    @classmethod
    def setUpClass(cls):
        here = Path(__file__).parent
        template = str(here.joinpath('dbusmock_templates', 'bluez_scan.py'))
        cls.start_system_bus()
        cls.dbus_con = cls.get_dbus(True)
        (cls.p_mock, cls.obj_bluez) = cls.spawn_server_template(
            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_dbus_int64(self):
        self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device')


        result = dbus_tools.dbus_to_python(dbus.Int64(12))