Example #1
0
    def test_ends_when_session_is_running(
        self,
        old_session,
        old_pomodoros,
        new_session,
        new_pomodoros,
        session,
        config,
        bus,
        mocker,
    ):
        session.current = old_session
        session.pomodoros = old_pomodoros

        config.set("Timer", "pomodoro_duration", 0.02)
        config.set("Timer", "longbreak_duration", 0.02)
        config.set("Timer", "shortbreak_duration", 0.02)
        config.parser.getint = config.parser.getfloat

        subscriber = mocker.Mock()
        bus.connect(Events.SESSION_END, subscriber, False)

        session.start()
        run_loop_for(2)

        payload = create_session_end_payload(
            type=new_session,
            pomodoros=new_pomodoros,
            duration=1,
            previous=create_session_payload(type=old_session,
                                            pomodoros=old_pomodoros,
                                            duration=1),
        )
        subscriber.assert_called_once_with(Events.SESSION_END, payload=payload)
def test_show_notification_when_session_starts(event, session, title, message,
                                               bus, plugin):
    plugin.activate()

    payload = create_session_payload(type=session)
    bus.send(event, payload=payload)

    plugin.notification.update.assert_called_once_with(title, message,
                                                       IconPath)
    plugin.notification.show.assert_called_once()
Example #3
0
def test_shows_window_when_session_end(bus, mocker, window):
    window.widget.set_visible(False)
    subscriber = mocker.Mock()
    bus.connect(Events.WINDOW_SHOW, subscriber, weak=False)
    payload = create_session_end_payload(previous=create_session_payload())

    bus.send(Events.SESSION_END, payload=payload)

    assert window.widget.get_visible()
    subscriber.assert_called_once_with(Events.WINDOW_SHOW, payload=None)
Example #4
0
    def test_listens_config_change_when_session_is_not_running(
            self, session, bus, mocker, state, config):
        session.state = state
        subscriber = mocker.Mock()
        bus.connect(Events.SESSION_CHANGE, subscriber, False)

        config.set("timer", SessionType.POMODORO.option, 20)

        payload = create_session_payload(duration=20 * 60)
        subscriber.assert_called_once_with(Events.SESSION_CHANGE,
                                           payload=payload)
Example #5
0
    def test_changes_session_when_it_is_not_running(self, state, old, new, bus,
                                                    session, config, mocker):
        session.state = state
        subscriber = mocker.Mock()
        bus.connect(Events.SESSION_CHANGE, subscriber, False)

        assert session.change(session=new) is True
        assert session.current is new
        payload = create_session_payload(
            type=new, duration=config.get_int("Timer", new.option) * 60)
        subscriber.assert_called_once_with(Events.SESSION_CHANGE,
                                           payload=payload)
Example #6
0
def test_updates_countdown_when_session_state_changes(event, bus, countdown):
    duration = random.randint(1, 100)

    bus.send(event, payload=create_session_payload(duration=duration))

    assert countdown.widget.get_text() == format_time(duration)
Example #7
0
    assert button.props.tooltip_text == tooltip_text

    assert Q.select(button, Q.props("label", label))


def test_disables_buttons_when_session_starts(bus, session_button):
    bus.send(Events.SESSION_START)

    assert session_button.widget.get_sensitive() is False


@pytest.mark.parametrize(
    "event,payload",
    [
        (Events.SESSION_INTERRUPT,
         create_session_payload(type=SessionType.SHORT_BREAK)),
        (
            Events.SESSION_END,
            create_session_end_payload(type=SessionType.SHORT_BREAK,
                                       previous=create_session_payload()),
        ),
    ],
)
def test_changes_selected_button_when_session_finishes(event, payload, bus,
                                                       session_button,
                                                       session):
    bus.send(event, payload=payload)

    assert session_button.widget.get_sensitive() is True
    assert session_button.widget.get_selected(
    ) is SessionType.SHORT_BREAK.value
Example #8
0
class TestHeaderBar:
    @pytest.fixture
    def menu(self, mocker):
        return mocker.Mock(spec=HeaderBarMenu, widget=Gtk.Menu())

    @pytest.fixture
    def headerbar(self, graph, menu, shortcut_engine, session, bus,
                  mocker) -> HeaderBar:
        graph.register_instance("focusyn.bus", bus)
        graph.register_instance("focusyn.session", session)
        graph.register_factory("focusyn.ui.about", mocker.Mock)
        graph.register_factory("focusyn.ui.preference", mocker.Mock)
        graph.register_instance("focusyn.ui.menu", menu)
        graph.register_instance("focusyn.ui.shortcut", shortcut_engine)

        # gtk shortcuts binds leave beyond the scope
        shortcut_engine.disconnect(HeaderBar.START_SHORTCUT)
        shortcut_engine.disconnect(HeaderBar.STOP_SHORTCUT)
        shortcut_engine.disconnect(HeaderBar.RESET_SHORTCUT)

        namespaces = ["focusyn.ui.widgets.headerbar"]

        scan_to_graph(namespaces, graph)

        return graph.get("focusyn.ui.headerbar")

    def test_module(self, graph, headerbar):
        instance = graph.get("focusyn.ui.headerbar")

        assert isinstance(instance, HeaderBar)
        assert instance is headerbar

    @pytest.mark.parametrize(
        "shortcut,action",
        [
            (HeaderBar.START_SHORTCUT, "start"),
            (HeaderBar.STOP_SHORTCUT, "stop"),
            (HeaderBar.RESET_SHORTCUT, "reset"),
        ],
    )
    def test_shortcuts(self, shortcut, action, headerbar, menu, session,
                       shortcut_engine):
        active_shortcut(shortcut_engine, shortcut)
        getattr(session, action).assert_called_once_with()

    @pytest.mark.parametrize(
        "button_name,action",
        [
            (HeaderBar.START_SHORTCUT.name, "start"),
            (HeaderBar.STOP_SHORTCUT.name, "stop"),
            (HeaderBar.RESET_SHORTCUT.name, "reset"),
        ],
    )
    def test_change_session(self, button_name, action, session, headerbar):
        button = Q.select(headerbar.widget, Q.props("name", button_name))

        button.emit("clicked")
        refresh_gui()

        getattr(session, action).assert_called_once_with()

    @pytest.mark.parametrize(
        "button_name,tooltip",
        [
            (HeaderBar.START_SHORTCUT.name, "Starts the session (Ctrl+S)"),
            (HeaderBar.STOP_SHORTCUT.name, "Stops the session (Ctrl+P)"),
            (HeaderBar.RESET_SHORTCUT.name, "Clear session count (Ctrl+R)"),
            (HeaderBarMenu.PREFERENCE_SHORTCUT.name,
             "Open preferences (Ctrl+,)"),
        ],
    )
    def test_buttons_tooltip(self, button_name, tooltip, headerbar):
        button = Q.select(headerbar.widget, Q.props("name", button_name))
        assert tooltip == button.props.tooltip_text

    def test_enable_only_the_stop_button_when_session_starts(
            self, bus, headerbar):
        bus.send(Events.SESSION_START)

        assert Q.select(
            headerbar.widget, Q.props(
                "name", HeaderBar.START_SHORTCUT.name)).props.visible is False
        assert Q.select(
            headerbar.widget, Q.props(
                "name", HeaderBar.STOP_SHORTCUT.name)).props.visible is True
        assert Q.select(
            headerbar.widget, Q.props(
                "name",
                HeaderBar.RESET_SHORTCUT.name)).props.sensitive is False

    def test_disables_reset_button_when_session_is_reset(
            self, headerbar, bus, session):
        reset_button = Q.select(headerbar.widget,
                                Q.props("name", headerbar.RESET_SHORTCUT.name))
        reset_button.props.sensitive = True

        bus.send(Events.SESSION_RESET)

        assert reset_button.props.sensitive is False
        assert headerbar.widget.props.title == "No session yet"

    @pytest.mark.parametrize(
        "event,reset,title,payload",
        [
            (Events.SESSION_INTERRUPT, False, "No session yet",
             create_session_payload()),
            (Events.SESSION_END, True, "Session 1",
             create_session_end_payload(previous=create_session_payload())),
        ],
    )
    def test_buttons_visibility_and_title_in_the_first_session(
            self, event, title, reset, headerbar, payload, bus):
        bus.send(event, payload=payload)

        assert Q.select(
            headerbar.widget, Q.props(
                "name", headerbar.START_SHORTCUT.name)).props.visible is True
        assert Q.select(
            headerbar.widget, Q.props(
                "name", headerbar.STOP_SHORTCUT.name)).props.visible is False
        assert Q.select(
            headerbar.widget, Q.props(
                "name",
                headerbar.RESET_SHORTCUT.name)).props.sensitive is reset
        assert headerbar.widget.props.title == title