# Copyright 2023 (c) Anna Schumaker. """Tests our Open button.""" import emmental.header.open import pathlib import unittest from gi.repository import Gio from gi.repository import Gtk class TestButton(unittest.TestCase): """Test the Open button.""" def setUp(self): """Set up common variables.""" self.button = emmental.header.open.Button() def test_button(self): """Check that the button was set up properly.""" self.assertIsInstance(self.button, Gtk.Button) self.assertEqual(self.button.get_icon_name(), "document-open-symbolic") self.assertEqual(self.button.get_tooltip_text(), "open a file for playback") def test_filter(self): """Check that the file filter is set up properly.""" self.assertIsInstance(self.button._filter, Gtk.FileFilter) self.assertIsInstance(self.button._filters, Gio.ListStore) self.assertEqual(self.button._filter.get_name(), "Audio Files") self.assertEqual(self.button._filters[0], self.button._filter) def test_dialog(self): """Check that the file dialog is set up properly.""" self.assertIsInstance(self.button._dialog, Gtk.FileDialog) self.assertEqual(self.button._dialog.get_title(), "Pick a Track") self.assertEqual(self.button._dialog.get_filters(), self.button._filters) self.assertTrue(self.button._dialog.get_modal()) def test_clicked(self): """Test clicking on the button.""" with unittest.mock.patch.object(self.button._dialog, "open") as mock_open: self.button.emit("clicked") mock_open.assert_called_with(None, None, self.button._Button__async_ready) with unittest.mock.patch.object(self.button._dialog, "open_finish") as mock_finish: task = Gio.Task() signal = unittest.mock.Mock() mock_finish.return_value = Gio.File.new_for_path("/a/b/c/1.ogg") self.button.connect("track-requested", signal) self.button._Button__async_ready(self.button._dialog, task) mock_finish.assert_called_with(task) signal.assert_called_with(self.button, pathlib.Path("/a/b/c/1.ogg"))