Commit f0786d78 authored by Nico Alt's avatar Nico Alt

Add unit tests for Briar's Application

parent 09eafd1c
......@@ -7,4 +7,5 @@ pycodestyle>=2.5.0
pylint>=2.3.1
pytest>=4.6.2
pytest-cov>=2.7.1
pytest-mock>=1.11.0
requests_mock>=1.6.0
......@@ -11,20 +11,20 @@ from gi.repository import Gdk, Gio, GLib, Gtk
from briar.api.api import Api
from briar.gtk.define import APPLICATION_ID, APPLICATION_NAME
from briar.gtk.define import BRIAR_HEADLESS_JAR
from briar.gtk.define import APPLICATION_STYLING, BRIAR_HEADLESS_JAR
from briar.gtk.window import Window
class Application(Gtk.Application):
def __init__(self):
Application._set_application_name()
Application._set_application_name(APPLICATION_NAME)
super().__init__(application_id=APPLICATION_ID)
# pylint: disable=arguments-differ
def do_startup(self):
Gtk.Application.do_startup(self)
Application._setup_styling()
Application._setup_styling(APPLICATION_STYLING)
self._setup_api()
# pylint: disable=arguments-differ
......@@ -38,14 +38,13 @@ class Application(Gtk.Application):
Gio.Application.quit(self)
@staticmethod
def _set_application_name():
GLib.set_application_name(APPLICATION_NAME)
GLib.set_prgname(APPLICATION_NAME)
def _set_application_name(name):
GLib.set_application_name(name)
GLib.set_prgname(name)
@staticmethod
def _setup_styling():
css_provider_file = Gio.File.new_for_uri(
"resource:///app/briar/gtk/ui/application.css")
def _setup_styling(styling):
css_provider_file = Gio.File.new_for_uri(styling)
css_provider = Gtk.CssProvider()
css_provider.load_from_file(css_provider_file)
......
......@@ -10,6 +10,7 @@ from gi.repository import Gio
APPLICATION_ID = "app.briar.gtk"
APPLICATION_NAME = "Briar"
APPLICATION_STYLING = "resource:///app/briar/gtk/ui/application.css"
BRIAR_HEADLESS_JAR = "/app/briar/briar-headless.jar"
App = Gio.Application.get_default # pylint: disable=invalid-name
# Copyright (c) 2019 Nico Alt
# SPDX-License-Identifier: AGPL-3.0-only
# License-Filename: LICENSE.md
from unittest.mock import Mock
from briar.api.api import Api
from briar.gtk.application import Application
from briar.gtk.define import APPLICATION_NAME, APPLICATION_STYLING
from briar.gtk.define import BRIAR_HEADLESS_JAR
from briar.gtk.window import Window
def test_do_startup(mocker):
do_startup_mock = mocker.patch("gi.repository.Gtk.Application.do_startup")
_setup_styling_mock = mocker.patch(
"briar.gtk.application.Application._setup_styling")
_setup_api_mock = mocker.patch(
"briar.gtk.application.Application._setup_api")
Application().do_startup()
do_startup_mock.assert_called_once()
_setup_styling_mock.assert_called_once_with(APPLICATION_STYLING)
_setup_api_mock.assert_called_once()
def test_do_activate(mocker):
_setup_window_mock = mocker.patch(
"briar.gtk.application.Application._setup_window")
Application().do_activate()
_setup_window_mock.assert_called_once()
def test_quit(mocker):
api_mock = mocker.patch("briar.api.api.Api")
api_stop_mock = mocker.patch("briar.api.api.Api.stop")
window_mock = mocker.patch("briar.gtk.window.Window")
window_hide_mock = mocker.patch("briar.gtk.window.Window.hide")
quit_mock = mocker.patch("gi.repository.Gio.Application.quit")
application = Application()
application.api = api_mock
application.window = window_mock
application.quit()
api_stop_mock.assert_called_once()
window_hide_mock.assert_called_once()
quit_mock.assert_called_once()
def test_set_application_name(mocker):
set_application_name_mock = mocker.patch(
"gi.repository.GLib.set_application_name")
set_prgname_mock = mocker.patch("gi.repository.GLib.set_prgname")
name_mock = "Mock"
Application._set_application_name(name_mock)
set_application_name_mock.assert_called_once_with(name_mock)
set_prgname_mock.assert_called_once_with(name_mock)
def test_set_application_name_implicit(mocker):
set_application_name_mock = mocker.patch(
"gi.repository.GLib.set_application_name")
set_prgname_mock = mocker.patch("gi.repository.GLib.set_prgname")
Application()
set_application_name_mock.assert_called_once_with(APPLICATION_NAME)
set_prgname_mock.assert_called_once_with(APPLICATION_NAME)
def test_setup_styling(mocker):
new_for_uri_mock = mocker.patch("gi.repository.Gio.File.new_for_uri")
load_from_file_mock = mocker.patch(
"gi.repository.Gtk.CssProvider.load_from_file")
get_default_mock = mocker.patch("gi.repository.Gdk.Screen.get_default")
add_provider_for_screen_mock = mocker.patch(
"gi.repository.Gtk.StyleContext.add_provider_for_screen")
Application._setup_styling(APPLICATION_STYLING)
new_for_uri_mock.assert_called_with(APPLICATION_STYLING)
load_from_file_mock.assert_called_once()
get_default_mock.assert_called_once()
add_provider_for_screen_mock.assert_called_once()
def test_setup_api(mocker):
application = Application()
assert not hasattr(application, "api")
application._setup_api()
assert isinstance(application.api, Api)
assert application.api._command == ["java", "-jar", BRIAR_HEADLESS_JAR]
def test_setup_window(mocker):
mocker.patch("briar.gtk.window.Window.__init__").return_value = None
window_show_mock = mocker.patch("briar.gtk.window.Window.show")
window_present_mock = mocker.patch("briar.gtk.window.Window.present")
Application()._setup_window()
window_show_mock.assert_called_once()
window_present_mock.assert_called_once()
def test_setup_window_has_attribute(mocker):
mocker.patch("briar.gtk.window.Window.__init__").return_value = None
window_mock = Mock()
application = Application()
application.window = window_mock
application._setup_window()
window_mock.show.assert_not_called()
window_mock.present.assert_called_once()
def test_setup_window_has_none_attribute(mocker):
mocker.patch("briar.gtk.window.Window.__init__").return_value = None
window_show_mock = mocker.patch("briar.gtk.window.Window.show")
window_present_mock = mocker.patch("briar.gtk.window.Window.present")
application = Application()
application.window = None
application._setup_window()
window_show_mock.assert_called_once()
window_present_mock.assert_called_once()
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