# 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 self.rgain = "disabled" @GObject.Property def playing(self): return self.play @playing.setter def playing(self, newval): self.play = newval self.emit("playback-start" if newval else "playback-paused") def previous(self): self.prev = True def next(self): self.nxt = True @GObject.Property def volume(self): return self.vol def playpause(self): self.playing = not self.playing @GObject.Property def replaygain(self): return self.rgain @replaygain.setter def replaygain(self, newval): self.rgain = newval @GObject.Signal def playback_start(self): pass @GObject.Signal def playback_paused(self): 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 TestReplayGainComboBox(unittest.TestCase): def test_replay_gain_combobox(self): fake = FakePlayer() combo = controls.ReplayGainComboBox(fake) self.assertIsInstance(combo, Gtk.ComboBoxText) self.assertEqual(combo.player, fake) self.assertEqual(combo.modes, [ "disabled", "track", "album" ]) self.assertEqual(combo.get_active_text(), "ReplayGain Disabled") combo.set_active(1) self.assertEqual(combo.get_active_text(), "ReplayGain Track Mode") self.assertEqual(fake.replaygain, "track") combo.set_active(2) self.assertEqual(combo.get_active_text(), "ReplayGain Album Mode") self.assertEqual(fake.replaygain, "album") class TestReplayGainControl(unittest.TestCase): def test_replay_gain_control(self): fake = FakePlayer() rgc = controls.ReplayGainControl(fake) self.assertIsInstance(rgc, Gtk.Box) self.assertIsInstance(rgc.icon, Gtk.Image) self.assertIsInstance(rgc.rgcombo, controls.ReplayGainComboBox) self.assertEqual(rgc.get_orientation(), Gtk.Orientation.HORIZONTAL) self.assertEqual(rgc.icon.get_icon_name(), "audio-headphones") self.assertEqual(rgc.get_first_child(), rgc.icon) self.assertEqual(rgc.icon.get_next_sibling(), rgc.rgcombo) self.assertTrue(rgc.icon.has_css_class("large-icons")) 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) child = pop.box.get_first_child() self.assertIsInstance(child, controls.AutoPauseControlBox) child = child.get_next_sibling() self.assertIsInstance(child, controls.VolumeControlBox) child = child.get_next_sibling() self.assertIsInstance(child, controls.ReplayGainControl) self.assertEqual(pop.get_child(), pop.box) self.assertEqual(pop.box.get_orientation(), Gtk.Orientation.VERTICAL) class TestControlsMenuIcon(unittest.TestCase): def test_controls_menu_icon(self): apscale = scale.AutoPauseScale() icon = controls.MenuIcon(apscale) self.assertIsInstance(icon, Gtk.Overlay) self.assertIsInstance(icon.icon, Gtk.Image) self.assertIsInstance(icon.label, Gtk.Label) self.assertEqual(icon.icon.get_icon_name(), "pan-down-symbolic") self.assertEqual(icon.icon.get_margin_top(), 5) self.assertEqual(icon.label.get_text(), " ") self.assertEqual(icon.label.get_yalign(), 0) apscale.set_value(0) self.assertEqual(icon.label.get_text(), "0") self.assertIn(icon.icon, icon) self.assertIn(icon.label, icon) class TestControlsMenuButton(unittest.TestCase): def test_controls_menu_button(self): fake = FakePlayer() apscale = scale.AutoPauseScale() menu = controls.MenuButton(fake, apscale) self.assertIsInstance(menu, Gtk.MenuButton) self.assertIsInstance(menu.get_popover(), controls.ControlsPopover) self.assertIsInstance(menu.get_first_child().get_child(), controls.MenuIcon) class TestAudioControls(unittest.TestCase): def test_audio_controls(self): fake = FakePlayer() apscale = scale.AutoPauseScale() ctrl = controls.AudioControls(fake, apscale) self.assertIsInstance(ctrl, Gtk.Box) self.assertEqual(ctrl.get_orientation(), Gtk.Orientation.HORIZONTAL) self.assertTrue(ctrl.has_css_class("linked")) child = ctrl.get_first_child() self.assertIsInstance(child, controls.PreviousButton) child = child.get_next_sibling() self.assertIsInstance(child, controls.PlayPauseButton) child = child.get_next_sibling() self.assertIsInstance(child, controls.NextButton) child = child.get_next_sibling() self.assertIsInstance(child, controls.MenuButton)