emmental/tests/sidebar/test_icon.py
Anna Schumaker 0584a2398a sidebar: Create an Icon widget
This is mostly a wrapper around an Adw.Avatar to make it easier to load
images from a file.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2023-04-12 14:18:43 -04:00

63 lines
2.2 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Tests our custom icon widgets."""
import unittest
import emmental.sidebar.icon
import tests.util
from gi.repository import Gdk
from gi.repository import Adw
class TestIcon(unittest.TestCase):
"""Test our icon that can also be set from filepath."""
def setUp(self):
"""Set up common variables."""
self.icon = emmental.sidebar.icon.Icon()
def test_init(self):
"""Test that the icon is set up properly."""
self.assertIsInstance(self.icon, Adw.Bin)
self.assertIsInstance(self.icon._icon, Adw.Avatar)
self.assertEqual(self.icon._icon.get_size(), 40)
self.assertEqual(self.icon.get_child(), self.icon._icon)
def test_icon_name(self):
"""Test setting an icon name to the inner icon widget."""
self.assertEqual(self.icon.icon_name, "")
self.icon.icon_name = "audio-x-generic"
self.assertEqual(self.icon._icon.get_icon_name(), "audio-x-generic")
def test_text(self):
"""Test the text property."""
self.assertEqual(self.icon.text, "")
self.icon.text = "Test Playlist"
self.assertEqual(self.icon._icon.get_text(), "Test Playlist")
def test_show_initials(self):
"""Test the show initials property."""
self.assertFalse(self.icon.show_initials)
self.icon.show_initials = True
self.assertTrue(self.icon._icon.get_show_initials())
icon2 = emmental.sidebar.icon.Icon(show_initials=True)
self.assertTrue(icon2._icon.get_show_initials())
def test_filepath(self):
"""Test the filepath property."""
self.assertIsNone(self.icon.filepath)
with unittest.mock.patch("gi.repository.Gdk.Texture.new_from_filename",
wraps=Gdk.Texture.new_from_filename) \
as mock_new:
self.icon.filepath = tests.util.COVER_JPG
mock_new.assert_called_with(str(tests.util.COVER_JPG))
self.assertIsInstance(self.icon._icon.get_custom_image(),
Gdk.Texture)
mock_new.reset_mock()
self.icon.filepath = None
self.assertIsNone(self.icon._icon.get_custom_image())
mock_new.assert_not_called()