# Copyright 2022 (c) Anna Schumaker. """Tests our application window.""" import unittest import emmental.window from gi.repository import Adw from gi.repository import Gtk class TestWindow(unittest.TestCase): """Test case for our custom Gtk.Window.""" def setUp(self): """Set up common variables.""" self.window = emmental.window.Window("Test 1.2.3") def tearDown(self): """Clean up.""" self.window.close() def test_init(self): """Check that the window gets set up properly.""" self.assertIsInstance(self.window, Adw.Window) self.assertIsInstance(self.window._box, Gtk.Box) self.assertIsInstance(self.window._header, Adw.Bin) self.assertIsInstance(self.window._inner_pane, Gtk.Paned) self.assertIsInstance(self.window._layout, emmental.layout.Layout) self.assertIsInstance(self.window._toast, Adw.ToastOverlay) self.assertTrue(self.window.has_css_class("devel")) self.assertEqual(self.window.get_icon_name(), "emmental") self.assertEqual(self.window.get_title(), "Test 1.2.3") self.assertEqual(self.window.get_default_size(), (1600, 900)) def test_content(self): """Check that the Window content is set up properly.""" self.assertEqual(self.window._box.get_orientation(), Gtk.Orientation.VERTICAL) self.assertEqual(self.window._box.get_spacing(), 0) self.assertEqual(self.window.get_content(), self.window._box) self.assertEqual(self.window._box.get_first_child(), self.window._header) self.assertEqual(self.window._header.get_next_sibling(), self.window._toast) self.assertEqual(self.window._toast.get_child(), self.window._layout) self.assertEqual(self.window._layout.content, self.window._inner_pane) self.assertTrue(self.window._layout.has_css_class( "emmental-padding")) self.assertEqual(self.window._inner_pane.get_orientation(), Gtk.Orientation.VERTICAL) self.assertEqual(self.window._inner_pane.get_margin_start(), 8) self.assertFalse(self.window._inner_pane.get_shrink_start_child()) self.assertFalse(self.window._inner_pane.get_resize_start_child()) self.assertTrue(self.window._inner_pane.get_hexpand()) self.assertTrue(self.window._inner_pane.get_vexpand()) self.assertTrue(self.window._inner_pane.has_css_class("emmental-pane")) def test_header(self): """Check setting a widget to the header area.""" self.assertIsNone(self.window.header) self.window.header = Gtk.Label() self.assertEqual(self.window._header.get_child(), self.window.header) window2 = emmental.window.Window(version="1.2.3", header=Gtk.Label()) self.assertIsInstance(window2.header, Gtk.Label) self.assertEqual(window2._header.get_child(), window2.header) def test_sidebar(self): """Check setting a widget to the sidebar area.""" self.assertIsNone(self.window.sidebar) self.window.sidebar = Gtk.Label() self.assertEqual(self.window._layout.sidebar, self.window.sidebar) window2 = emmental.window.Window(version="1.2.3", sidebar=Gtk.Label()) self.assertIsInstance(window2.sidebar, Gtk.Label) self.assertEqual(window2._layout.sidebar, window2.sidebar) def test_show_sidebar(self): """Check setting the show-sidebar property.""" self.assertFalse(self.window.show_sidebar) self.assertFalse(self.window._layout.show_sidebar) self.window.show_sidebar = True self.assertTrue(self.window._layout.show_sidebar) self.window._layout.show_sidebar = False self.assertFalse(self.window.show_sidebar) def test_now_playing(self): """Check setting a widget to the now_playing area.""" self.assertIsNone(self.window.now_playing) self.window.now_playing = Gtk.Label() self.assertEqual(self.window._inner_pane.get_start_child(), self.window.now_playing) window2 = emmental.window.Window(version="1.2.3", now_playing=Gtk.Label()) self.assertIsInstance(window2.now_playing, Gtk.Label) self.assertEqual(window2._inner_pane.get_start_child(), window2.now_playing) def test_now_playing_size(self): """Check setting the size of the now playing area.""" self.assertEqual(self.window.now_playing_size, 250) self.assertEqual(self.window._inner_pane.get_position(), 250) self.window.now_playing_size = 100 self.assertEqual(self.window.now_playing_size, 100) self.assertEqual(self.window._inner_pane.get_position(), 100) self.window._inner_pane.set_position(200) self.assertEqual(self.window.now_playing_size, 200) self.assertEqual(self.window._inner_pane.get_position(), 200) def test_tracklist(self): """Check setting a widget to the tracklist area.""" self.assertIsNone(self.window.tracklist) self.window.tracklist = Gtk.Label() self.assertEqual(self.window._inner_pane.get_end_child(), self.window.tracklist) window2 = emmental.window.Window(version="1.2.3", tracklist=Gtk.Label()) self.assertIsInstance(window2.tracklist, Gtk.Label) self.assertEqual(window2._inner_pane.get_end_child(), window2.tracklist) def test_user_editing(self): """Test the 'user-editing' property.""" self.window.header = Gtk.Entry() self.window.tracklist = Gtk.Button() self.assertFalse(self.window.user_editing) self.window.set_focus(self.window.header) self.assertTrue(self.window.user_editing) self.window.set_focus(self.window.tracklist) self.assertFalse(self.window.user_editing) def test_post_toast(self): """Test posting a Toast message to the window.""" toast = self.window.post_toast("Test Toast") self.assertIsInstance(toast, Adw.Toast) self.assertEqual(toast.get_title(), "Test Toast") def test_close(self): """Test calling close().""" with unittest.mock.patch.object(Gtk.Window, "close") as mock_close: self.window.close(1, 2, 3, 4, 5) mock_close.assert_called() def test_present(self): """Test calling present().""" with unittest.mock.patch.object(Gtk.Window, "present") as mock_present: self.window.present(1, 2, 3, 4, 5) mock_present.assert_called() def test_accelerators(self): """Test that the Window accelerators are set up properly.""" accels = self.window.accelerators self.assertIsInstance(accels, list) self.assertEqual(len(accels), 1) self.assertEqual(accels[0].name, "reset-focus") self.assertEqual(accels[0].func, self.window.set_focus) self.assertListEqual(accels[0].accels, ["Escape"])