emmental/tests/tracklist/test_selection.py

109 lines
4.4 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Tests our Tracklist Selection OSD."""
import emmental.buttons
import emmental.tracklist.selection
import tests.util
import unittest.mock
from gi.repository import Gtk
from gi.repository import Adw
class TestOsd(tests.util.TestCase):
"""Test the Tracklist OSD."""
def setUp(self):
"""Set up common variables."""
super().setUp()
self.db_plist = self.sql.playlists.create("Test Playlist")
self.playlist = emmental.playlist.playlist.Playlist(self.sql,
self.db_plist)
self.model = Gtk.StringList.new(["Test", "OSD", "Strings"])
self.selection = Gtk.MultiSelection(model=self.model)
self.osd = emmental.tracklist.selection.OSD(self.selection)
def test_init(self):
"""Test that the OSD is set up properly."""
self.assertIsInstance(self.osd, Gtk.Overlay)
self.assertEqual(self.osd.selection, self.selection)
self.assertIsNone(self.osd.playlist)
def test_remove_button(self):
"""Test the remove tracks button."""
self.assertIsInstance(self.osd._remove, Gtk.Button)
self.assertIsInstance(self.osd._remove.get_child(), Adw.ButtonContent)
self.assertEqual(self.osd._remove.get_child().get_icon_name(),
"list-remove-symbolic")
self.assertEqual(self.osd._remove.get_child().get_label(), "Remove")
self.assertEqual(self.osd._remove.get_halign(), Gtk.Align.END)
self.assertEqual(self.osd._remove.get_valign(), Gtk.Align.END)
self.assertEqual(self.osd._remove.get_margin_end(), 16)
self.assertEqual(self.osd._remove.get_margin_bottom(), 16)
self.assertTrue(self.osd._remove.has_css_class("destructive-action"))
self.assertTrue(self.osd._remove.has_css_class("pill"))
self.assertIn(self.osd._remove, self.osd)
def test_remove_button_visible(self):
"""Test the remove button visiblity."""
self.assertFalse(self.osd._remove.get_visible())
self.selection.select_item(0, True)
self.assertFalse(self.osd._remove.get_visible())
self.db_plist.user_tracks = False
self.osd.playlist = self.playlist
self.selection.select_item(1, True)
self.assertFalse(self.osd._remove.get_visible())
self.db_plist.user_tracks = True
self.selection.select_item(2, True)
self.assertTrue(self.osd._remove.get_visible())
def test_remove_button_click(self):
"""Test clicking the remove button."""
with unittest.mock.patch.object(self.db_plist,
"remove_track") as mock_remove:
self.selection.select_all()
self.osd._remove.emit("clicked")
mock_remove.assert_not_called()
self.osd.playlist = self.playlist
self.selection.select_all()
self.osd._remove.emit("clicked")
mock_remove.assert_has_calls([unittest.mock.call(self.model[0]),
unittest.mock.call(self.model[1]),
unittest.mock.call(self.model[2])])
def test_selection_properties(self):
"""Test updating properties when the selection changes."""
self.assertFalse(self.osd.have_selected)
self.assertEqual(self.osd.n_selected, 0)
self.selection.select_item(1, True)
self.assertTrue(self.osd.have_selected)
self.assertEqual(self.osd.n_selected, 1)
def test_clear_selection(self):
"""Test the clear_selection() function."""
self.selection.select_item(1, True)
self.osd.clear_selection()
self.assertEqual(self.selection.get_selection().get_size(), 0)
self.assertEqual(self.osd.n_selected, 0)
self.assertFalse(self.osd.have_selected)
self.osd.playlist = self.playlist
self.assertEqual(self.selection.get_selection().get_size(), 0)
self.assertEqual(self.osd.n_selected, 0)
self.assertFalse(self.osd.have_selected)
def test_reset(self):
"""Test the reset() function."""
self.osd.have_selected = True
with unittest.mock.patch.object(self.selection,
"unselect_all") as mock_unselect:
self.osd.reset()
mock_unselect.assert_called()
self.assertFalse(self.osd.have_selected)