emmental/tests/tracklist/test_row.py

148 lines
5.9 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Tests our TrackView Row widgets."""
import datetime
import dateutil.tz
import pathlib
import emmental.factory
import emmental.tracklist.row
import tests.util
import unittest.mock
from gi.repository import Gtk
from gi.repository import Adw
class TestTrackRowWidgets(tests.util.TestCase):
"""Tests our Track Row widgets."""
def setUp(self):
"""Set up common variables."""
super().setUp()
self.sql.playlists.load()
self.library = self.sql.libraries.create(pathlib.Path("/a/b"))
self.album = self.sql.albums.create("Test Album", "Artist", "2023")
self.medium = self.sql.media.create(self.album, "", number=1)
self.year = self.sql.years.create(2023)
self.track = self.sql.tracks.create(self.library,
pathlib.Path("/a/b/1.ogg"),
self.medium, self.year,
title="Test Title")
self.listitem = Gtk.ListItem()
self.listitem.get_item = unittest.mock.Mock(return_value=self.track)
self.columnrow = Adw.Bin()
self.listrow = Adw.Bin(child=self.columnrow)
def test_track_row(self):
"""Test the base Track Row."""
row = emmental.tracklist.row.TrackRow(self.listitem, "property")
self.assertIsInstance(row, emmental.factory.ListRow)
self.assertEqual(row.property, "property")
row.child = Gtk.Label()
self.columnrow.set_child(row.child)
self.library.online = False
self.track.active = True
row.bind()
self.assertFalse(row.online)
self.assertFalse(self.listitem.get_activatable())
self.assertFalse(row.child.get_sensitive())
self.assertTrue(row.active)
self.assertTrue(self.listrow.get_state_flags() &
Gtk.StateFlags.CHECKED)
self.library.online = True
self.track.active = False
self.assertTrue(row.online)
self.assertTrue(self.listitem.get_activatable())
self.assertTrue(row.child.get_sensitive())
self.assertFalse(row.active)
self.assertFalse(self.listrow.get_state_flags() &
Gtk.StateFlags.CHECKED)
def test_inscription_row(self):
"""Test the base Inscription Row."""
row = emmental.tracklist.row.InscriptionRow(self.listitem, "property")
self.assertIsInstance(row, emmental.tracklist.row.TrackRow)
self.assertIsInstance(row.child, Gtk.Inscription)
self.assertEqual(row.child.get_xalign(), 0.0)
self.assertFalse(row.child.has_css_class("numeric"))
row.child.set_text("Test Text")
self.assertEqual(row.child.get_tooltip_text(), "Test Text")
row = emmental.tracklist.row.InscriptionRow(self.listitem, "property",
xalign=1.0, numeric=True)
self.assertEqual(row.child.get_xalign(), 1.0)
self.assertTrue(row.child.has_css_class("numeric"))
def test_track_string(self):
"""Test the Track String widget."""
row = emmental.tracklist.row.TrackString(self.listitem, "title")
self.assertIsInstance(row, emmental.tracklist.row.InscriptionRow)
self.assertEqual(row.property, "title")
row.bind()
self.assertEqual(row.child.get_text(), "Test Title")
self.track.title = "New Title"
self.assertEqual(row.child.get_text(), "New Title")
def test_length_string(self):
"""Test the LengthString widget."""
row = emmental.tracklist.row.LengthString(self.listitem, "length")
self.assertIsInstance(row, emmental.tracklist.row.InscriptionRow)
self.assertEqual(row.property, "length")
self.assertEqual(row.length, 0.0)
row.bind()
self.assertEqual(row.child.get_text(), "0:00")
self.track.length = 123.45
self.assertEqual(row.child.get_text(), "2:03")
self.track.length = 123.84
self.assertEqual(row.child.get_text(), "2:04")
def test_playcount_string(self):
"""Test the PlayCountString widget."""
row = emmental.tracklist.row.PlayCountString(self.listitem,
"playcount")
self.assertIsInstance(row, emmental.tracklist.row.InscriptionRow)
self.assertEqual(row.property, "playcount")
self.assertEqual(row.playcount, 0)
row.bind()
self.assertEqual(row.child.get_text(), "Unplayed")
self.track.playcount = 1
self.assertEqual(row.child.get_text(), "Played 1 time")
self.track.playcount = 2
self.assertEqual(row.child.get_text(), "Played 2 times")
self.track.playcount = 3
self.assertEqual(row.child.get_text(), "Played 3 times")
def test_timestamp_string(self):
"""Test the TimestampString widget."""
row = emmental.tracklist.row.TimestampString(self.listitem,
"laststarted")
self.assertIsInstance(row, emmental.tracklist.row.InscriptionRow)
self.assertEqual(row.TZ_UTC, dateutil.tz.tzutc())
self.assertEqual(row.property, "laststarted")
self.assertEqual(row.timestamp, None)
row.bind()
self.assertEqual(row.child.get_text(), "Never")
local = datetime.datetime.now()
utc = local.astimezone(dateutil.tz.tzutc())
self.track.laststarted = utc.replace(tzinfo=None)
self.assertEqual(row.child.get_text(), local.strftime("%c"))
def test_path_string(self):
"""Test the PathString widget."""
row = emmental.tracklist.row.PathString(self.listitem, "path")
self.assertIsInstance(row, emmental.tracklist.row.InscriptionRow)
self.assertEqual(row.property, "path")
self.assertEqual(row.path, None)
row.bind()
self.assertEqual(row.child.get_text(), str(pathlib.Path("/a/b/1.ogg")))