emmental/tests/header/test_volume.py

178 lines
7.3 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Tests our volume controls."""
import unittest
import emmental.header.volume
import tests.util
from gi.repository import Gtk
from gi.repository import Adw
class TestVolumeRow(unittest.TestCase):
"""Test case for our custom volume controls."""
def setUp(self):
"""Set up common variables."""
self.vol = emmental.header.volume.VolumeRow()
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.ListBoxRow)
self.assertIsInstance(self.vol._box, Gtk.Box)
self.assertEqual(self.vol.get_child(), self.vol._box)
self.assertEqual(self.vol._box.get_spacing(), 0)
self.assertEqual(self.vol._box.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)
vol2 = emmental.header.volume.VolumeRow(volume=0.5)
self.assertEqual(vol2.volume, 0.5)
self.assertEqual(vol2._adjustment.get_value(), 0.5)
def test_decrement_button(self):
"""Test the decrement button."""
self.assertIsInstance(self.vol._decrement, Gtk.Button)
self.assertEqual(self.vol._box.get_first_child(), self.vol._decrement)
self.assertEqual(self.vol._decrement.get_tooltip_text(),
"reduce the volume")
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(), 5)
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)
self.vol.decrement()
self.assertAlmostEqual(self.vol.volume, 0.90)
self.assertAlmostEqual(self.vol._scale.get_value(), 0.90)
self.assertAlmostEqual(self.value.value, 0.90)
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_tooltip_text(),
"increase the volume")
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(), 5)
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)
self.vol.increment()
self.assertAlmostEqual(self.vol.volume, 1.0)
self.assertAlmostEqual(self.vol._scale.get_value(), 1.0)
self.assertAlmostEqual(self.value.value, 1.0)
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} %")
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)