emmental/audio/test_controls.py
Anna Schumaker bd49396210 audio: Create a custom ControlsPopover widget
For displaying our menu controls

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2021-09-02 16:26:58 -04:00

181 lines
6.5 KiB
Python

# Copyright 2021 (c) Anna Schumaker.
import unittest
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gst
from . import controls
from . import scale
class FakePlayer(GObject.GObject):
def __init__(self):
GObject.GObject.__init__(self)
self.prev = False
self.nxt = False
self.play = False
self.vol = 1.0
def previous(self): self.prev = True
def next(self): self.nxt = True
def get_volume(self): return self.vol
def playpause(self):
states = { True : Gst.State.PLAYING, False : Gst.State.PAUSED }
self.play = not self.play
self.emit("state-changed", Gst.State.NULL, states[self.play], Gst.State.NULL)
@GObject.Signal(arg_types=(Gst.State, Gst.State, Gst.State))
def state_changed(self, old, new, pending): pass
class TestControlButton(unittest.TestCase):
def test_control_button(self):
fake = FakePlayer()
ctrl = controls.ControlButton(fake, "missing-icon")
self.assertIsInstance(ctrl, Gtk.Button)
self.assertTrue(ctrl.has_css_class("large-icons"))
self.assertEqual(ctrl.get_icon_name(), "missing-icon")
self.assertEqual(ctrl.player, fake)
class TestPreviousButton(unittest.TestCase):
def test_previous_button(self):
fake = FakePlayer()
prev = controls.PreviousButton(fake)
self.assertIsInstance(prev, controls.ControlButton)
self.assertEqual(prev.get_icon_name(), "media-skip-backward")
prev.emit("clicked")
self.assertTrue(fake.prev)
class TestNextButton(unittest.TestCase):
def test_next_button(self):
fake = FakePlayer()
next = controls.NextButton(fake)
self.assertIsInstance(next, controls.ControlButton)
self.assertEqual(next.get_icon_name(), "media-skip-forward")
next.emit("clicked")
self.assertTrue(fake.nxt)
class TestPlayPauseButton(unittest.TestCase):
def test_play_pause_button(self):
fake = FakePlayer()
play = controls.PlayPauseButton(fake)
self.assertIsInstance(play, controls.ControlButton)
self.assertEqual(play.get_icon_name(), "media-playback-start")
play.emit("clicked")
self.assertTrue(fake.play)
self.assertEqual(play.get_icon_name(), "media-playback-pause")
play.emit("clicked")
self.assertFalse(fake.play)
self.assertEqual(play.get_icon_name(), "media-playback-start")
class TestControlScaleBox(unittest.TestCase):
def test_control_scale_box(self):
splus = scale.ScalePlus(1, 10, 1, 5)
ctrlbox = controls.ControlScaleBox(splus)
self.assertIsInstance(ctrlbox, scale.ScaleButtonBox)
self.assertIsInstance(ctrlbox.get_first_child(), Gtk.Image)
self.assertTrue(ctrlbox.get_first_child().has_css_class("large-icons"))
class TestAutoPauseControlBox(unittest.TestCase):
def test_autopause_control_box(self):
apscale = scale.AutoPauseScale()
apbox = controls.AutoPauseControlBox(apscale)
icon = apbox.get_first_child()
self.assertIsInstance(apbox, controls.ControlScaleBox)
apscale.set_value(-1)
self.assertEqual(icon.get_icon_name(), "media-playback-start")
apscale.set_value(0)
self.assertEqual(icon.get_icon_name(), "media-playback-pause")
class TestVolumeControlbox(unittest.TestCase):
def test_volume_control_box(self):
fake = FakePlayer()
vcb = controls.VolumeControlBox(fake)
icon = vcb.get_first_child()
volume = icon.get_next_sibling().scale
self.assertIsInstance(vcb, controls.ControlScaleBox)
self.assertIsInstance(volume, scale.VolumeScale)
volume.set_value(0)
self.assertEqual(icon.get_icon_name(), "audio-volume-muted-symbolic")
volume.set_value(0.3)
self.assertEqual(icon.get_icon_name(), "audio-volume-low-symbolic")
volume.set_value(0.6)
self.assertEqual(icon.get_icon_name(), "audio-volume-medium-symbolic")
volume.set_value(0.9)
self.assertEqual(icon.get_icon_name(), "audio-volume-high-symbolic")
class TestControlsPopover(unittest.TestCase):
def test_controls_popover(self):
fake = FakePlayer()
apscale = scale.AutoPauseScale()
pop = controls.ControlsPopover(fake, apscale)
self.assertIsInstance(pop, Gtk.Popover)
self.assertIsInstance(pop.box, Gtk.Box)
expected = [ controls.AutoPauseControlBox, controls.VolumeControlBox ]
child = pop.box.get_first_child()
while len(expected) > 0:
self.assertIsInstance(child, expected.pop(0))
child = child.get_next_sibling()
self.assertEqual(pop.get_child(), pop.box)
self.assertEqual(pop.box.get_orientation(), Gtk.Orientation.VERTICAL)
class TestControls(unittest.TestCase):
def test_controls_init(self):
ctrl = controls.Controls()
self.assertIsInstance(ctrl, Gtk.Box)
self.assertIsInstance(ctrl.previous, Gtk.Button)
self.assertIsInstance(ctrl.play, Gtk.Button)
self.assertIsInstance(ctrl.pause, Gtk.Button)
self.assertIsInstance(ctrl.next, Gtk.Button)
self.assertIsInstance(ctrl.menu, Gtk.MenuButton)
self.assertIsInstance(ctrl.sizegroup, Gtk.SizeGroup)
self.assertEqual(ctrl.get_orientation(), Gtk.Orientation.HORIZONTAL)
self.assertEqual(ctrl.previous.get_icon_name(), "media-skip-backward")
self.assertEqual(ctrl.play.get_icon_name(), "media-playback-start")
self.assertEqual(ctrl.pause.get_icon_name(), "media-playback-pause")
self.assertEqual(ctrl.next.get_icon_name(), "media-skip-forward")
self.assertTrue(ctrl.has_css_class("linked"))
self.assertTrue(ctrl.has_css_class("large-icons"))
self.assertTrue(ctrl.menu.has_css_class("normal-icons"))
self.assertFalse(ctrl.pause.is_visible())
self.assertIn(ctrl.previous, ctrl)
self.assertIn(ctrl.play, ctrl)
self.assertIn(ctrl.pause, ctrl)
self.assertIn(ctrl.next, ctrl)
self.assertIn(ctrl.menu, ctrl)
self.assertIn(ctrl, ctrl.sizegroup.get_widgets())
def test_controls_state(self):
ctrl = controls.Controls()
self.assertTrue(ctrl.play.is_visible())
self.assertFalse(ctrl.pause.is_visible())
ctrl.set_state(Gst.State.PLAYING)
self.assertFalse(ctrl.play.is_visible())
self.assertTrue(ctrl.pause.is_visible())
ctrl.set_state(Gst.State.READY)
self.assertTrue(ctrl.play.is_visible())
self.assertFalse(ctrl.pause.is_visible())