diff --git a/emmental/header/volume.py b/emmental/header/volume.py index 115583d..e00ab62 100644 --- a/emmental/header/volume.py +++ b/emmental/header/volume.py @@ -2,6 +2,7 @@ """A custom Gtk.Box with controls for adjusting the volume.""" from gi.repository import GObject from gi.repository import Gtk +from gi.repository import Adw STEP_SIZE = 0.05 @@ -55,3 +56,27 @@ class VolumeRow(Gtk.ListBoxRow): def __value_changed(self, range: Gtk.Range) -> None: self.volume = range.get_value() + + +class BackgroundRow(Adw.ExpanderRow): + """A VolumeRow for setting Background Listening volume.""" + + enabled = GObject.Property(type=bool, default=False) + volume = GObject.Property(type=float, default=0.5) + + def __init__(self): + """Initialize the BackgroundRow.""" + super().__init__(title="Background Listening", + subtitle="Decrease the volume to help focus") + self._switch = Gtk.Switch(valign=Gtk.Align.CENTER) + self._volume = VolumeRow(volume=self.volume) + + self.add_prefix(self._switch) + self.add_row(self._volume) + + self._switch.bind_property("active", self, "expanded", + GObject.BindingFlags.BIDIRECTIONAL) + self.bind_property("enabled", self._switch, "active", + GObject.BindingFlags.BIDIRECTIONAL) + self.bind_property("volume", self._volume, "volume", + GObject.BindingFlags.BIDIRECTIONAL) diff --git a/tests/header/test_volume.py b/tests/header/test_volume.py index 6b9d830..a0d170f 100644 --- a/tests/header/test_volume.py +++ b/tests/header/test_volume.py @@ -4,6 +4,7 @@ import unittest import emmental.header.volume import tests.util from gi.repository import Gtk +from gi.repository import Adw class TestVolumeRow(unittest.TestCase): @@ -109,3 +110,58 @@ class TestVolumeRow(unittest.TestCase): with self.subTest(value=value): self.assertEqual(format_value(self.vol._scale, value/100), f"{value} %") + + +class TestBackgroundRow(unittest.TestCase): + """Test case for our Background Listening volume row.""" + + def setUp(self): + """Set up common variables.""" + self.background = emmental.header.volume.BackgroundRow() + + def test_init(self): + """Test that the Background Listening row was set up properly.""" + self.assertIsInstance(self.background, Adw.ExpanderRow) + self.assertEqual(self.background.get_title(), "Background Listening") + self.assertEqual(self.background.get_subtitle(), + "Decrease the volume to help focus") + + self.background.set_expanded(True) + self.assertTrue(self.background._switch.get_active()) + self.background.set_expanded(False) + self.assertFalse(self.background._switch.get_active()) + + def test_switch(self): + """Check that the BackgroundRow switch works as intended.""" + self.assertIsInstance(self.background._switch, Gtk.Switch) + self.assertEqual(self.background._switch.get_valign(), + Gtk.Align.CENTER) + self.assertFalse(self.background._switch.get_active()) + + self.background._switch.set_active(True) + self.assertTrue(self.background.get_expanded()) + self.background._switch.set_active(False) + self.assertFalse(self.background.get_expanded()) + + def test_volume(self): + """Check the VolumeRow instance inside the Expander.""" + self.assertIsInstance(self.background._volume, + emmental.header.volume.VolumeRow) + self.assertEqual(self.background.volume, 0.5) + self.assertEqual(self.background._volume.volume, 0.5) + + self.background.volume = 0.75 + self.assertEqual(self.background._volume.volume, 0.75) + + self.background._volume.volume = 0.25 + self.assertEqual(self.background.volume, 0.25) + + def test_enabled(self): + """Check the BackgroundRow enabled property.""" + self.assertFalse(self.background.enabled) + + self.background.enabled = True + self.assertTrue(self.background._switch.get_active()) + + self.background._switch.set_active(False) + self.assertFalse(self.background.enabled)