# Copyright 2022 (c) Anna Schumaker. """Tests our playback controls.""" import unittest import unittest.mock import emmental.nowplaying.controls from gi.repository import Gtk class TestButtons(unittest.TestCase): """Test the Control Buttons.""" def test_pill(self): """Test that the pill button is configured correctly.""" button = emmental.nowplaying.controls.PillButton() self.assertIsInstance(button, emmental.buttons.Button) self.assertTrue(button.large_icon) self.assertTrue(button.has_css_class("pill")) class TestControls(unittest.TestCase): """Tests the Controls widget.""" def setUp(self): """Set up common variables.""" self.controls = emmental.nowplaying.controls.Controls() self.clicked = unittest.mock.Mock() def test_constants(self): """Test constant values.""" self.assertEqual(emmental.nowplaying.controls.MARGIN, 24) def test_init(self): """Test that controls were initialized properly.""" self.assertIsInstance(self.controls, Gtk.Box) self.assertEqual(self.controls.get_orientation(), Gtk.Orientation.HORIZONTAL) self.assertEqual(self.controls.get_valign(), Gtk.Align.START) self.assertTrue(self.controls.get_homogeneous()) self.assertTrue(self.controls.has_css_class("linked")) self.assertEqual(self.controls.get_margin_top(), emmental.nowplaying.controls.MARGIN) self.assertEqual(self.controls.get_margin_bottom(), emmental.nowplaying.controls.MARGIN) self.assertEqual(self.controls.get_margin_start(), emmental.nowplaying.controls.MARGIN / 2) self.assertEqual(self.controls.get_margin_end(), emmental.nowplaying.controls.MARGIN) self.assertFalse(self.controls.editing) def test_previous_button(self): """Test the previous button.""" self.assertIsInstance(self.controls._prev, emmental.nowplaying.controls.PillButton) self.assertEqual(self.controls._prev.get_tooltip_text(), "previous track") self.assertEqual(self.controls._prev.icon_name, "media-skip-backward") self.assertEqual(self.controls.get_first_child(), self.controls._prev) self.controls._prev.connect("clicked", self.clicked) self.controls._prev.emit("clicked") self.clicked.assert_called_with(self.controls._prev) def test_activate_previous(self): """Test can-activate-prev and the activate_previous() function.""" self.assertFalse(self.controls.can_activate_prev) self.controls.have_previous = True self.assertTrue(self.controls.can_activate_prev) self.controls.editing = True self.assertFalse(self.controls.can_activate_prev) activate = unittest.mock.Mock() self.controls._prev.connect("activate", activate) self.controls.activate_previous() activate.assert_called() def test_play_button(self): """Test the play button.""" self.assertIsInstance(self.controls._play, emmental.nowplaying.controls.PillButton) self.assertEqual(self.controls._play.get_tooltip_text(), "play") self.assertEqual(self.controls._play.icon_name, "play-large") self.assertEqual(self.controls._prev.get_next_sibling(), self.controls._play) self.assertFalse(self.controls.playing) self.assertTrue(self.controls._play.get_visible()) self.controls.playing = True self.assertFalse(self.controls._play.get_visible()) self.controls.playing = False self.assertTrue(self.controls._play.get_visible()) self.controls._play.connect("clicked", self.clicked) self.controls._play.emit("clicked") self.clicked.assert_called_with(self.controls._play) def test_pause_button(self): """Test the pause button.""" self.assertIsInstance(self.controls._pause, emmental.buttons.SplitButton) self.assertEqual(self.controls._play.get_next_sibling(), self.controls._pause) self.assertEqual(self.controls._pause.get_tooltip_text(), "pause") self.assertEqual(self.controls._pause.icon_name, "pause-large") self.assertTrue(self.controls._pause.large_icon) self.assertFalse(self.controls._pause.get_visible()) self.controls.playing = True self.assertTrue(self.controls._pause.get_visible()) self.controls.playing = False self.assertFalse(self.controls._pause.get_visible()) self.controls._pause.connect("clicked", self.clicked) self.controls._pause.emit("clicked") self.clicked.assert_called_with(self.controls._pause) def test_activate_play_pause(self): """Test can-activate-play-pause and the activate_play_pause() func.""" self.assertFalse(self.controls.can_activate_play_pause) self.controls.have_track = True self.assertTrue(self.controls.can_activate_play_pause) self.controls.editing = True self.assertFalse(self.controls.can_activate_play_pause) play = unittest.mock.Mock() self.controls._play.connect("activate", play) self.controls.activate_play_pause() play.assert_called() self.controls.playing = True pause = unittest.mock.Mock() self.controls._pause.connect("activate-primary", pause) self.controls.activate_play_pause() pause.assert_called() def test_autopause_button(self): """Test the autopause button.""" self.assertIsInstance(self.controls._autopause, emmental.nowplaying.autopause.Button) self.assertEqual(self.controls._pause.secondary, self.controls._autopause) self.assertEqual(self.controls.autopause, -1) self.controls._autopause.value = 42 self.assertEqual(self.controls.autopause, 42) self.controls.autopause = 21 self.assertEqual(self.controls._autopause.value, 21) self.controls.playing = False self.assertEqual(self.controls.autopause, -1) def test_next_button(self): """Test the next button.""" self.assertIsInstance(self.controls._next, emmental.nowplaying.controls.PillButton) self.assertEqual(self.controls._next.get_tooltip_text(), "next track") self.assertEqual(self.controls._next.icon_name, "media-skip-forward") self.controls._next.connect("clicked", self.clicked) self.controls._next.emit("clicked") self.clicked.assert_called_with(self.controls._next) def test_activate_next(self): """Test can-activate-next and the activate_next() function.""" self.assertFalse(self.controls.can_activate_next) self.controls.have_next = True self.assertTrue(self.controls.can_activate_next) self.controls.editing = True self.assertFalse(self.controls.can_activate_next) activate = unittest.mock.Mock() self.controls._next.connect("activate", activate) self.controls.activate_next() activate.assert_called() def test_decrease_autopause(self): """Test the decrease_autopause() function.""" with unittest.mock.patch.object(self.controls._autopause, "decrement") as mock_decrement: self.controls.decrease_autopause() mock_decrement.assert_called() def test_increase_autopause(self): """Test the increase_autopause() function.""" with unittest.mock.patch.object(self.controls._autopause, "increment") as mock_increment: self.controls.increase_autopause() mock_increment.assert_called() def test_have_properties(self): """Test the have_{next, previous, track} properties.""" self.assertFalse(self.controls.have_next) self.assertFalse(self.controls.have_previous) self.assertFalse(self.controls.have_track) for button in [self.controls._next, self.controls._prev, self.controls._play, self.controls._pause]: self.assertFalse(button.get_sensitive()) self.controls.have_next = True self.assertTrue(self.controls._next.get_sensitive()) self.controls.have_previous = True self.assertTrue(self.controls._prev.get_sensitive()) self.controls.have_track = True self.assertTrue(self.controls._play.get_sensitive()) self.assertTrue(self.controls._pause.get_sensitive())