emmental/tests/test_emmental.py

153 lines
6.7 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Test as much as we can of the Emmental Application."""
import io
import unittest
import unittest.mock
import gi
import emmental
class TestEmmental(unittest.TestCase):
"""Emmental Application test case."""
def setUp(self):
"""Set up common variables."""
self.application = emmental.Application()
def tearDown(self):
"""Clean up."""
self.application.do_shutdown()
def test_version(self):
"""Check that version constants have been set properly."""
self.assertEqual(emmental.MAJOR_VERSION, 3)
self.assertEqual(emmental.MINOR_VERSION, 0)
self.assertEqual(emmental.VERSION_STRING, "Emmental 3.0-debug")
def test_application(self):
"""Check that the application instance is initialized properly."""
self.assertIsInstance(self.application, gi.repository.Adw.Application)
self.assertEqual(self.application.get_application_id(),
"com.nowheycreamery.emmental-debug")
self.assertEqual(self.application.get_property("resource-base-path"),
"/com/nowheycreamery/emmental")
@unittest.mock.patch("sys.stdout")
@unittest.mock.patch("gi.repository.Adw.Application.add_window")
@unittest.mock.patch("emmental.db.Connection.load")
@unittest.mock.patch("gi.repository.Adw.Application.do_startup")
def test_startup(self, mock_startup: unittest.mock.Mock,
mock_load: unittest.mock.Mock,
mock_add_window: unittest.mock.Mock,
mock_stdout: unittest.mock.Mock):
"""Test that the startup signal works as expected."""
self.assertIsNone(self.application.db)
self.assertIsNone(self.application.mpris)
self.assertIsNone(self.application.player)
self.assertIsNone(self.application.win)
self.application.emit("startup")
self.assertIsInstance(self.application.db, emmental.db.Connection)
self.assertIsInstance(self.application.mpris,
emmental.mpris2.Connection)
self.assertIsInstance(self.application.player, emmental.audio.Player)
self.assertIsInstance(self.application.win, emmental.window.Window)
mock_startup.assert_called()
mock_load.assert_called()
mock_add_window.assert_called_with(self.application.win)
@unittest.mock.patch("sys.stdout")
@unittest.mock.patch("gi.repository.Adw.Application.add_window")
@unittest.mock.patch("gi.repository.Adw.Application.do_startup")
def test_activate(self, mock_startup: unittest.mock.Mock,
mock_add_window: unittest.mock.Mock,
mock_stdout: unittest.mock.Mock):
"""Test activating the application."""
self.application.emit("startup")
with unittest.mock.patch.object(self.application.win,
"present") as mock_present:
self.application.emit("activate")
mock_present.assert_called()
@unittest.mock.patch("gi.repository.Adw.Window.close")
def test_shutdown(self, mock_close: unittest.mock.Mock):
"""Test that the shutdown signal works as expected."""
db = self.application.db = emmental.db.Connection()
mpris = self.application.mpris = emmental.mpris2.Connection()
self.application.win = emmental.window.Window("Test 1.2.3")
player = self.application.player = emmental.audio.Player()
self.application.emit("shutdown")
self.assertIsNone(self.application.db)
self.assertIsNone(self.application.mpris)
self.assertIsNone(self.application.player)
self.assertIsNone(self.application.win)
self.assertIsNone(mpris.dbus)
self.assertFalse(db.connected)
self.assertEqual(player.get_state(), gi.repository.Gst.State.NULL)
mock_close.assert_called()
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_window_widgets(self, mock_stdout: io.StringIO):
"""Check that the window widgets are added properly."""
self.application.db = emmental.db.Connection()
self.application.player = emmental.audio.Player()
win = self.application.build_window()
self.assertIsInstance(win, emmental.window.Window)
self.assertIsInstance(win.header, emmental.header.Header)
self.assertIsInstance(win.now_playing, emmental.nowplaying.Card)
self.assertEqual(win.header.title, emmental.VERSION_STRING)
@unittest.mock.patch("emmental.audio.Player.pause")
@unittest.mock.patch("emmental.audio.Player.play")
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_nowplaying(self, mock_stdout: io.StringIO,
play_func: unittest.mock.Mock,
pause_func: unittest.mock.Mock):
"""Check that the nowplaying widget is wired up properly."""
self.application.db = emmental.db.Connection()
self.application.player = emmental.audio.Player()
win = self.application.build_window()
for (property, value) in [("have-track", True), ("playing", True),
("duration", 10), ("position", 5),
("artwork", "/a/b/c.jpg")]:
with self.subTest(property=property, value=value):
self.application.player.set_property(property, value)
self.assertEqual(win.now_playing.get_property(property), value)
win.now_playing.emit("play")
play_func.assert_called()
win.now_playing.emit("pause")
pause_func.assert_called()
for tag in ["title", "album", "artist", "album-artist"]:
with self.subTest(tag=tag):
self.assertEqual(win.now_playing.get_property(tag), "")
self.application.player.set_property(tag, "Test Tag")
self.assertEqual(win.now_playing.get_property(tag), "Test Tag")
self.assertEqual(self.application.autopause, -1)
win.now_playing.autopause = 1
self.assertEqual(self.application.autopause, 1)
self.application.autopause = 2
self.assertEqual(win.now_playing.autopause, 2)
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
def test_replaygain(self, mock_stdout: io.StringIO):
"""Test setting replaygain modes."""
self.application.db = emmental.db.Connection()
self.application.player = emmental.audio.Player()
win = self.application.build_window()
player = self.application.player
win.header.rg_enabled = True
self.assertEqual(player.get_replaygain(), (True, "track"))
win.header.rg_mode = "album"
self.assertEqual(player.get_replaygain(), (True, "album"))