# 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
def app(graph, window, plugin_engine, mocker) -> Application:
    graph.register_instance("tomate.ui.view", window)
    graph.register_instance("tomate.plugin", plugin_engine)
    graph.register_instance("dbus.session", mocker.Mock())

    scan_to_graph(["tomate.pomodoro.app"], graph)

    return graph.get("tomate.app")


def test_module(graph, app):
    instance = graph.get("tomate.app")

    assert isinstance(instance, Application)
    assert instance is app


def test_collects_plugins_on_start(app, plugin_engine):
    assert plugin_engine.has_plugins() is True


class TestRun:
    def test_start_window_when_app_is_not_running(self, app, window):
        app.state = State.STOPPED

        app.Run()

        window.run.assert_called_once_with()

    def test_shows_window_when_app_is_running(self, app, window):

# 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

def app(graph, window, plugin_engine, mocker) -> Application:
    graph.register_instance("tomate.ui.view", window)
    graph.register_instance("tomate.plugin", plugin_engine)
    graph.register_instance("dbus.session", mocker.Mock())

    scan_to_graph(["tomate.pomodoro.app"], graph)

    return graph.get("tomate.app")


def test_module(graph, app):
    instance = graph.get("tomate.app")

    assert isinstance(instance, Application)
    assert instance is app


def test_collects_plugins_on_start(app, plugin_engine):
    assert plugin_engine.has_plugins() is True


class TestRun:
    def test_start_window_when_app_is_not_running(self, app, window):
        app.state = State.STOPPED

        app.Run()

        window.run.assert_called_once_with()

    def test_shows_window_when_app_is_running(self, app, window):

        app.state = State.STARTED