emmental/sidebar/test_tagbox.py

170 lines
6.2 KiB
Python

# Copyright 2021 (c) Anna Schumaker.
from . import tagbox
from . import tagrow
from lib import fake
from gi.repository import Gtk
import lib
import unittest
class TestTagBox(unittest.TestCase):
def test_tag_box(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "image-missing")
self.assertEqual(tbox.tagstore, store)
self.assertEqual(tbox.icon, "image-missing")
self.assertEqual(tbox.bus.timeout, 50)
self.assertIsInstance(tbox, Gtk.ScrolledWindow)
self.assertIsInstance(tbox.listbox, Gtk.ListBox)
self.assertIsInstance(tbox.bus, lib.bus.Bus)
viewport = tbox.get_child()
self.assertIsInstance(viewport, Gtk.Viewport)
self.assertEqual(viewport.get_child(), tbox.listbox)
self.assertTrue(tbox.get_vexpand())
self.assertFalse(tbox.listbox.get_activate_on_single_click())
self.assertIn(tbox.tag_added, store.Added.subscribers)
self.assertIn(tbox.tag_removed, store.Removed.subscribers)
def test_tag_box_add_remove_modify(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "missing-icon")
tag = store.add("Test", fake.Track(1))
self.assertIn((tbox.on_tag_added, (tag,)), tbox.bus.passengers)
tbox.bus.complete()
self.assertIsInstance(tag.widgets, tagrow.TagRow)
self.assertEqual(tbox[0], tag.widgets)
self.assertIn(tbox.tag_changed, tag.TrackAdded.subscribers)
self.assertEqual(tag.widgets.label.get_text(), "Test\n1 Track")
tag.add_track(fake.Track(2))
self.assertIn((tbox.on_tag_changed, (tag,)), tbox.bus.passengers)
tbox.bus.complete()
self.assertEqual(tag.widgets.label.get_text(), "Test\n2 Tracks")
self.assertIn(tbox.tag_changed, tag.TrackRemoved.subscribers)
tag.remove_track(fake.Track(2))
self.assertIn((tbox.on_tag_changed, (tag,)), tbox.bus.passengers)
tbox.bus.complete()
self.assertEqual(tag.widgets.label.get_text(), "Test\n1 Track")
store.remove(tag)
self.assertIn((tbox.on_tag_removed, (tag,)), tbox.bus.passengers)
tbox.bus.complete()
self.assertIsNone(tag.widgets)
self.assertIsNone(tbox[0])
self.assertNotIn(tbox.tag_changed, tag.TrackAdded.subscribers)
self.assertNotIn(tbox.tag_changed, tag.TrackRemoved.subscribers)
def test_tag_box_clear(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "missing-icon")
tag1 = store.add("Test1", fake.Track(1))
tag2 = store.add("Test2", fake.Track(1))
tbox.bus.complete()
self.assertEqual(tbox[0].tag, tag1)
self.assertEqual(tbox[1].tag, tag2)
tbox.clear()
self.assertIsNone(tbox[0])
self.assertNotIn(tbox.tag_changed, tag1.TrackAdded.subscribers)
self.assertNotIn(tbox.tag_changed, tag2.TrackAdded.subscribers)
self.assertNotIn(tbox.tag_changed, tag1.TrackRemoved.subscribers)
self.assertNotIn(tbox.tag_changed, tag2.TrackRemoved.subscribers)
def test_tag_box_load(self):
store = lib.tagstore.TagStore()
tag1 = store.add("Test1", fake.Track(1))
tag2 = store.add("Test2", fake.Track(2))
tbox = tagbox.TagBox(store, "missing-icon")
self.assertEqual(tbox[0], tag1.widgets)
self.assertEqual(tbox[1], tag2.widgets)
def test_tag_box_header(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "missing-icon", header=True)
tagAB = store.add("AB")
tbox.bus.complete()
self.assertIsNotNone(tagAB.widgets.get_header())
tagAA = store.add("AA")
tbox.bus.complete()
self.assertIsNotNone(tagAA.widgets.get_header())
self.assertIsNone(tagAB.widgets.get_header())
def test_tag_box_sort(self):
store = lib.tagstore.TagStore()
tbox = tagbox.TagBox(store, "missing-icon")
tagB = store.add("B", fake.Track(1))
tagA = store.add("A", fake.Track(1))
tagC = store.add("C", fake.Track(1))
tbox.bus.complete()
self.assertEqual(tbox[0].tag, tagA)
self.assertEqual(tbox[1].tag, tagB)
self.assertEqual(tbox[2].tag, tagC)
class TestParentTagBox(unittest.TestCase):
def test_parent_tagbox(self):
store = lib.tagstore.TagStore()
superstore = lib.tagstore.TagSuperStore()
ptbox = tagbox.ParentTagBox(store, "missing-icon",
superstore, "missing-icon")
self.assertIsInstance(ptbox, tagbox.ParentTagBox)
self.assertIsInstance(ptbox, tagbox.TagBox)
self.assertEqual(ptbox.tagstore, store)
self.assertEqual(ptbox.icon, "missing-icon")
self.assertEqual(ptbox.childstore, superstore)
self.assertEqual(ptbox.childicon, "missing-icon")
def test_parent_tagbox_add_remove(self):
store = lib.tagstore.TagStore()
superstore = lib.tagstore.TagSuperStore()
ptbox = tagbox.ParentTagBox(store, "missing-icon",
superstore, "missing-icon")
tag = store.add("Test", fake.Track(1))
ptbox.bus.complete()
self.assertIsInstance(tag.widgets, tagrow.ParentTagRow)
tag1 = superstore.add(tag, "child", fake.Track(1))
ptbox.bus.complete()
self.assertIsInstance(tag1.widgets, tagrow.TagRow)
self.assertEqual(tag1.widgets.get_margin_start(), 35)
superstore.remove(tag1)
ptbox.bus.complete()
self.assertIsNone(tag1.widgets)
store.remove(tag)
ptbox.bus.complete()
self.assertIsNone(tag.widgets)
def test_parent_tagbox_load(self):
store = lib.tagstore.TagStore()
superstore = lib.tagstore.TagSuperStore()
tag = store.add("Test", fake.Track(1))
tag1 = superstore.add(tag, "Tag 1", fake.Track(1))
tag2 = superstore.add(tag, "Tag 2", fake.Track(2))
ptbox = tagbox.ParentTagBox(store, "missing-icon",
superstore, "missing-icon")
self.assertEqual(ptbox[0], tag.widgets)
self.assertEqual(ptbox[1], tag1.widgets)
self.assertEqual(ptbox[2], tag2.widgets)
self.assertIsNone(tag1.widgets.get_header())
self.assertIsNone(tag2.widgets.get_header())