Commit 5f49ed79 authored by Nico Alt's avatar Nico Alt

Add unit tests for toolbar

parent 6cb8c139
......@@ -8,16 +8,17 @@
from gi.repository import Gtk
from briar.gtk.define import APPLICATION_NAME
class Toolbar(Gtk.HeaderBar):
TOOLBAR_UI = "/app/briar/gtk/ui/toolbar_start.ui"
def __init__(self):
super().__init__()
self._builder = Gtk.Builder()
self._builder.add_from_resource("/app/briar/gtk/ui/toolbar_start.ui")
toolbar_start = self._builder.get_object("toolbar_start")
self.set_title("Briar")
self.pack_start(toolbar_start)
self._setup_builder()
self._setup_toolbar()
def show_back_button(self, show, callback=None):
back_button = self._builder.get_object("back_button")
......@@ -28,3 +29,13 @@ class Toolbar(Gtk.HeaderBar):
raise Exception("Callback needed when showing back button")
back_button.show()
back_button.connect("clicked", callback)
def _setup_builder(self):
self._builder = Gtk.Builder()
def _setup_toolbar(self):
self.set_title(APPLICATION_NAME)
self._builder.add_from_resource(self.TOOLBAR_UI)
toolbar_start = self._builder.get_object("toolbar_start")
self.pack_start(toolbar_start)
# Copyright (c) 2019 Nico Alt
# SPDX-License-Identifier: AGPL-3.0-only
# License-Filename: LICENSE.md
import pytest
from unittest.mock import Mock
from briar.gtk.toolbar import Toolbar
def test_show_back_button(mocker):
get_object_mock = mocker.patch("gi.repository.Gtk.Builder.get_object")
back_button_mock = get_object_mock.return_value
callback = Mock()
mocker.patch("briar.gtk.toolbar.Toolbar._setup_toolbar")
Toolbar().show_back_button(True, callback)
get_object_mock.assert_called_once_with("back_button")
back_button_mock.hide.assert_not_called()
back_button_mock.show.assert_called_once()
back_button_mock.connect.assert_called_once()
def test_show_back_button_without_callback(mocker):
get_object_mock = mocker.patch("gi.repository.Gtk.Builder.get_object")
back_button_mock = get_object_mock.return_value
mocker.patch("briar.gtk.toolbar.Toolbar._setup_toolbar")
with pytest.raises(Exception,
match="Callback needed when showing back button"):
Toolbar().show_back_button(True)
get_object_mock.assert_called_once_with("back_button")
back_button_mock.hide.assert_not_called()
back_button_mock.show.assert_not_called()
back_button_mock.connect.assert_not_called()
def test_hide_back_button(mocker):
get_object_mock = mocker.patch("gi.repository.Gtk.Builder.get_object")
back_button_mock = get_object_mock.return_value
mocker.patch("briar.gtk.toolbar.Toolbar._setup_toolbar")
Toolbar().show_back_button(False)
get_object_mock.assert_called_once_with("back_button")
back_button_mock.hide.assert_called_once()
back_button_mock.show.assert_not_called()
back_button_mock.connect.assert_not_called()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment