# Path: tomate/pomodoro/app.py
# class Application(dbus.service.Object):
#     BUS_NAME = "com.github.Tomate"
#     BUS_PATH = "/com/github/Tomate"
#     BUS_INTERFACE = "com.github.Tomate"
#     SPEC = "tomate.app"
# 
#     @inject(bus="dbus.session", window="tomate.ui.view", plugins="tomate.plugin")
#     def __init__(self, bus, window, plugins: PluginEngine):
#         dbus.service.Object.__init__(self, bus, self.BUS_PATH)
#         self.state = State.STOPPED
#         self._window = window
#         plugins.collect()
# 
#     @dbus.service.method(BUS_INTERFACE, out_signature="b")
#     def IsRunning(self):
#         return self.state == State.STARTED
# 
#     @dbus.service.method(BUS_INTERFACE, out_signature="b")
#     def Run(self):
#         if self.IsRunning():
#             self._window.show()
#         else:
#             self.state = State.STARTED
#             self._window.run()
# 
#         return True
# 
#     @classmethod
#     def from_graph(cls, graph, bus=dbus.SessionBus(mainloop=DBusGMainLoop())):
#         request = bus.request_name(cls.BUS_NAME, dbus.bus.NAME_FLAG_DO_NOT_QUEUE)
# 
#         if request != dbus.bus.REQUEST_NAME_REPLY_EXISTS:
#             graph.register_instance("dbus.session", bus)
#             instance = graph.get(cls.SPEC)
#         else:
#             bus_object = bus.get_object(cls.BUS_NAME, cls.BUS_PATH)
#             instance = dbus.Interface(bus_object, cls.BUS_INTERFACE)
# 
#         return instance
# 
# Path: tomate/pomodoro/app.py
# class State(enum.Enum):
#     STOPPED = 1
#     STARTED = 2


import dbus
import pytest
from dbus.mainloop.glib import DBusGMainLoop
from dbusmock import DBusTestCase
from wiring.scanning import scan_to_graph
from tomate.pomodoro import Application
from tomate.pomodoro.app import State


class TestFromGraph:
    def setup_method(self):
        DBusTestCase.start_session_bus()

    def teardown_method(self):
        DBusTestCase.tearDownClass()

    def test_create_app_instance_when_it_is_not_registered_in_dbus(self, graph, window, plugin_engine):
        graph.register_instance("tomate.ui.view", window)
        graph.register_instance("tomate.plugin", plugin_engine)
        scan_to_graph(["tomate.pomodoro.app"], graph)

        instance = Application.from_graph(graph, DBusTestCase.get_dbus())

        assert isinstance(instance, Application)

    @pytest.fixture()
    def mock_dbus(self):
        mock = DBusTestCase.spawn_server(Application.BUS_NAME, Application.BUS_PATH, Application.BUS_INTERFACE)
        yield mock
        mock.terminate()
        mock.wait()

    def test_get_dbus_interface_when_is_registered_in_dbus(self, graph, mock_dbus):
        instance = Application.from_graph(graph, DBusTestCase.get_dbus())

        assert isinstance(instance, dbus.Interface)
        assert instance.dbus_interface == Application.BUS_INTERFACE

# Path: tomate/pomodoro/app.py
# class Application(dbus.service.Object):
#     BUS_NAME = "com.github.Tomate"
#     BUS_PATH = "/com/github/Tomate"
#     BUS_INTERFACE = "com.github.Tomate"
#     SPEC = "tomate.app"
# 
#     @inject(bus="dbus.session", window="tomate.ui.view", plugins="tomate.plugin")
#     def __init__(self, bus, window, plugins: PluginEngine):
#         dbus.service.Object.__init__(self, bus, self.BUS_PATH)
#         self.state = State.STOPPED
#         self._window = window
#         plugins.collect()
# 
#     @dbus.service.method(BUS_INTERFACE, out_signature="b")
#     def IsRunning(self):
#         return self.state == State.STARTED
# 
#     @dbus.service.method(BUS_INTERFACE, out_signature="b")
#     def Run(self):
#         if self.IsRunning():
#             self._window.show()
#         else:
#             self.state = State.STARTED
#             self._window.run()
# 
#         return True
# 
#     @classmethod
#     def from_graph(cls, graph, bus=dbus.SessionBus(mainloop=DBusGMainLoop())):
#         request = bus.request_name(cls.BUS_NAME, dbus.bus.NAME_FLAG_DO_NOT_QUEUE)
# 
#         if request != dbus.bus.REQUEST_NAME_REPLY_EXISTS:
#             graph.register_instance("dbus.session", bus)
#             instance = graph.get(cls.SPEC)
#         else:
#             bus_object = bus.get_object(cls.BUS_NAME, cls.BUS_PATH)
#             instance = dbus.Interface(bus_object, cls.BUS_INTERFACE)
# 
#         return instance
# 
# Path: tomate/pomodoro/app.py
# class State(enum.Enum):
#     STOPPED = 1
#     STARTED = 2


# Path: tests/pomodoro/test_app.py
import dbus
import pytest
from dbus.mainloop.glib import DBusGMainLoop
from dbusmock import DBusTestCase
from wiring.scanning import scan_to_graph
from tomate.pomodoro import Application
from tomate.pomodoro.app import State



class TestFromGraph:
    def setup_method(self):
        DBusTestCase.start_session_bus()

    def teardown_method(self):
        DBusTestCase.tearDownClass()

    def test_create_app_instance_when_it_is_not_registered_in_dbus(self, graph, window, plugin_engine):
        graph.register_instance("tomate.ui.view", window)
        graph.register_instance("tomate.plugin", plugin_engine)
        scan_to_graph(["tomate.pomodoro.app"], graph)

        instance = Application.from_graph(graph, DBusTestCase.get_dbus())

        assert isinstance(instance, Application)

    @pytest.fixture()
    def mock_dbus(self):
        mock = DBusTestCase.spawn_server(Application.BUS_NAME, Application.BUS_PATH, Application.BUS_INTERFACE)
        yield mock
        mock.terminate()
        mock.wait()

    def test_get_dbus_interface_when_is_registered_in_dbus(self, graph, mock_dbus):
        instance = Application.from_graph(graph, DBusTestCase.get_dbus())

        assert isinstance(instance, dbus.Interface)
        assert instance.dbus_interface == Application.BUS_INTERFACE

        assert instance.object_path == Application.BUS_PATH