168 lines
6.5 KiB
Python
168 lines
6.5 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 GLib
|
|
from gi.repository import Gio
|
|
from gi.repository import Gdk
|
|
from gi.repository import Gtk
|
|
from gi.repository import Adw
|
|
|
|
|
|
class TestImageFilters(unittest.TestCase):
|
|
"""Test that the global File Filters have been set up properly."""
|
|
|
|
def test_filters(self):
|
|
"""Test the global Filter list."""
|
|
self.assertIsInstance(emmental.sidebar.icon.IMAGE_FILTERS,
|
|
Gio.ListStore)
|
|
|
|
filter = emmental.sidebar.icon.IMAGE_FILTERS[0]
|
|
self.assertIsInstance(filter, Gtk.FileFilter)
|
|
self.assertEqual(filter.get_name(), "Image Files")
|
|
|
|
(name, mime_types) = filter.to_gvariant()
|
|
self.assertEqual(name, "Image Files")
|
|
self.assertTupleEqual(mime_types[0], (1, "inode/directory"))
|
|
self.assertGreater(len(mime_types), 1)
|
|
|
|
|
|
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)
|
|
|
|
self.icon.filepath = tests.util.COVER_JPG
|
|
texture = self.icon._icon.get_custom_image()
|
|
self.assertIsInstance(texture, Gdk.Texture)
|
|
self.assertDictEqual(emmental.texture.CACHE,
|
|
{tests.util.COVER_JPG: texture})
|
|
|
|
self.icon.filepath = None
|
|
self.assertIsNone(self.icon._icon.get_custom_image())
|
|
|
|
|
|
class TestSettable(unittest.TestCase):
|
|
"""Test our icon that can be set by the user."""
|
|
|
|
def setUp(self):
|
|
"""Set up common variables."""
|
|
self.icon = emmental.sidebar.icon.Settable()
|
|
|
|
def test_init(self):
|
|
"""Test that the icon is set up properly."""
|
|
self.assertIsInstance(self.icon, emmental.sidebar.icon.Icon)
|
|
|
|
def test_dialog(self):
|
|
"""Test that the dialog is set up properly."""
|
|
self.assertIsInstance(self.icon._dialog, Gtk.FileDialog)
|
|
self.assertEqual(self.icon._dialog.get_title(), "Pick an Image")
|
|
self.assertEqual(self.icon._dialog.get_filters(),
|
|
emmental.sidebar.icon.IMAGE_FILTERS)
|
|
|
|
def test_setting(self):
|
|
"""Test setting the icon through the FileDialog."""
|
|
self.assertIsInstance(self.icon._long_press, Gtk.GestureLongPress)
|
|
self.assertIn(self.icon._long_press, self.icon.observe_controllers())
|
|
self.assertFalse(self.icon.settable)
|
|
|
|
mock_set_initial_file = unittest.mock.Mock()
|
|
self.icon._dialog.set_initial_file = mock_set_initial_file
|
|
self.icon.settable = True
|
|
|
|
with unittest.mock.patch.object(self.icon._dialog,
|
|
"open") as mock_open:
|
|
self.icon._long_press.emit("pressed", 0, 0)
|
|
mock_set_initial_file.assert_not_called()
|
|
mock_open.assert_called_with(None, None,
|
|
self.icon._Settable__async_ready)
|
|
|
|
with unittest.mock.patch.object(self.icon._dialog,
|
|
"open_finish") as mock_finish:
|
|
task = Gio.Task()
|
|
cover_path = str(tests.util.COVER_JPG)
|
|
mock_finish.return_value = Gio.File.new_for_path(cover_path)
|
|
emmental.texture.CACHE[tests.util.COVER_JPG] = "abcde"
|
|
|
|
self.icon._Settable__async_ready(self.icon._dialog, task)
|
|
mock_finish.assert_called_with(task)
|
|
self.assertEqual(self.icon.filepath, tests.util.COVER_JPG)
|
|
|
|
texture = emmental.texture.CACHE[tests.util.COVER_JPG]
|
|
self.assertIsInstance(texture, Gdk.Texture)
|
|
|
|
def test_clearing(self):
|
|
"""Test clearing the icon by canceling the FileDialog."""
|
|
mock_set_initial_file = unittest.mock.Mock()
|
|
self.icon._dialog.set_initial_file = mock_set_initial_file
|
|
self.icon.filepath = tests.util.COVER_JPG
|
|
self.icon.settable = True
|
|
|
|
with unittest.mock.patch.object(self.icon._dialog,
|
|
"open") as mock_open:
|
|
self.icon._long_press.emit("pressed", 0, 0)
|
|
mock_set_initial_file.assert_called()
|
|
mock_open.assert_called()
|
|
|
|
cover_path = str(tests.util.COVER_JPG)
|
|
call_args = mock_set_initial_file.call_args.args
|
|
self.assertIsInstance(call_args[0], Gio.File)
|
|
self.assertEqual(call_args[0].get_path(), cover_path)
|
|
|
|
with unittest.mock.patch.object(self.icon._dialog, "open_finish",
|
|
side_effect=GLib.Error) as mock_finish:
|
|
task = Gio.Task()
|
|
mock_finish.return_value = None
|
|
|
|
self.icon._Settable__async_ready(self.icon._dialog, task)
|
|
mock_finish.assert_called_with(task)
|
|
self.assertIsNone(self.icon.filepath)
|
|
|
|
def test_not_settable(self):
|
|
"""Test when the icon isn't settable."""
|
|
mock_set_initial_file = unittest.mock.Mock()
|
|
self.icon._dialog.set_initial_file = mock_set_initial_file
|
|
|
|
with unittest.mock.patch.object(self.icon._dialog,
|
|
"open") as mock_open:
|
|
self.icon._long_press.emit("pressed", 0, 0)
|
|
mock_set_initial_file.assert_not_called()
|
|
mock_open.assert_not_called()
|