# Copyright 2022 (c) Anna Schumaker. """Tests our volume controls.""" import unittest import emmental.header.volume import tests.util from gi.repository import Gtk class TestControls(unittest.TestCase): """Test case for our custom volume controls.""" def setUp(self): """Set up common variables.""" self.vol = emmental.header.volume.Controls() self.value = tests.util.FloatObject(value=1.0) self.vol.bind_property("volume", self.value, "value") def test_step_size(self): """Test the step size constant.""" self.assertEqual(emmental.header.volume.STEP_SIZE, 0.05) def test_volume(self): """Check that the volume Controls are set up properly.""" self.assertIsInstance(self.vol, Gtk.Box) self.assertEqual(self.vol.get_spacing(), 0) self.assertEqual(self.vol.get_orientation(), Gtk.Orientation.HORIZONTAL) self.assertEqual(self.vol.volume, 1.0) self.vol.volume = 0.85 self.assertAlmostEqual(self.vol._scale.get_value(), 0.85) self.assertAlmostEqual(self.value.value, 0.85) def test_decrement_button(self): """Test the decrement button.""" self.assertIsInstance(self.vol._decrement, Gtk.Button) self.assertEqual(self.vol.get_first_child(), self.vol._decrement) self.assertEqual(self.vol._decrement.get_icon_name(), "list-remove-symbolic") self.assertEqual(self.vol._decrement.get_valign(), Gtk.Align.END) self.assertEqual(self.vol._decrement.get_margin_bottom(), 6) self.assertFalse(self.vol._decrement.get_has_frame()) self.vol._decrement.emit("clicked") self.assertAlmostEqual(self.vol.volume, 0.95) self.assertAlmostEqual(self.vol._scale.get_value(), 0.95) self.assertAlmostEqual(self.value.value, 0.95) def test_scale(self): """Check that the volume slider has been set up properly.""" self.assertIsInstance(self.vol._adjustment, Gtk.Adjustment) self.assertIsInstance(self.vol._scale, Gtk.Scale) self.assertEqual(self.vol._decrement.get_next_sibling(), self.vol._scale) self.assertEqual(self.vol._scale.get_adjustment(), self.vol._adjustment) self.assertEqual(self.vol._adjustment.get_lower(), 0.0) self.assertEqual(self.vol._adjustment.get_upper(), 1.0) self.assertEqual(self.vol._adjustment.get_step_increment(), 0.05) self.assertEqual(self.vol._adjustment.get_value(), 1.0) self.assertEqual(self.vol._scale.get_orientation(), Gtk.Orientation.HORIZONTAL) self.assertEqual(self.vol._scale.get_value(), 1.0) self.assertEqual(self.vol._scale.get_valign(), Gtk.Align.END) self.assertTrue(self.vol._scale.get_draw_value()) self.assertTrue(self.vol._scale.get_hexpand()) self.vol._scale.set_value(0.85) self.assertAlmostEqual(self.vol.volume, 0.85) self.assertAlmostEqual(self.vol._scale.get_value(), 0.85) self.assertAlmostEqual(self.value.value, 0.85) def test_increment_button(self): """Test the decrement button.""" self.assertIsInstance(self.vol._increment, Gtk.Button) self.assertEqual(self.vol._scale.get_next_sibling(), self.vol._increment) self.assertEqual(self.vol._increment.get_icon_name(), "list-add-symbolic") self.assertEqual(self.vol._increment.get_valign(), Gtk.Align.END) self.assertEqual(self.vol._increment.get_margin_bottom(), 6) self.assertFalse(self.vol._increment.get_has_frame()) self.vol.volume = 0.9 self.vol._increment.emit("clicked") self.assertAlmostEqual(self.vol.volume, 0.95) self.assertAlmostEqual(self.vol._scale.get_value(), 0.95) self.assertAlmostEqual(self.value.value, 0.95) def test_format_value(self): """Check that the scale value is formatted correctly.""" format_value = emmental.header.volume.format_value_func for value in range(101): with self.subTest(value=value): self.assertEqual(format_value(self.vol._scale, value/100), f"{value} %")