rind: Improve the gtk unit test

- Make sure we properly test the idle_id
- Make sure we're testing all the can_activate_accel() code
- Provide a generic main_loop() function that can be used by other test
  code

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-04-08 13:44:50 -04:00
parent 36ff8414fb
commit dc3377bca5
2 changed files with 49 additions and 18 deletions

View File

@ -1,6 +1,7 @@
# Copyright 2019 (c) Anna Schumaker.
import curds
import gi
import time
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
@ -46,9 +47,6 @@ class EmmentalApplication(Gtk.Application):
if self.idle_id == None:
self.idle_id = GLib.idle_add(self.on_idle)
def type_focused(type):
return isinstance(Window.get_focus(), type)
def can_activate_accel(widget, signal):
widget.stop_emission_by_name("can-activate-accel")
return widget.is_visible() and widget.is_sensitive() and \
@ -62,6 +60,15 @@ def can_activate_entry(widget, signal):
widget.grab_focus()
return active
def main_loop(delay=0.0, iteration_delay=0.0):
time.sleep(delay)
while Gtk.events_pending():
Gtk.main_iteration_do(True)
time.sleep(iteration_delay)
def type_focused(type):
return isinstance(Window.get_focus(), type)
def updown_toggled(self, *args):
active = UpDown.get_active()
UpArrow.set_visible(active)

View File

@ -3,33 +3,42 @@ from . import gtk
import curds
import time
import unittest
from gi.repository import Gtk
from gi.repository import Gtk, GObject, GLib
class TestGtk(unittest.TestCase):
def test_builder(self):
self.assertIsInstance(gtk.Builder, Gtk.Builder)
self.assertIsInstance(gtk.Builder.get_object("window"), Gtk.ApplicationWindow)
def test_window(self):
window = gtk.Builder.get_object("window")
self.assertIsInstance(window, Gtk.ApplicationWindow)
self.assertIsNone(gtk.Application.window)
def fake_idle(self):
return GLib.SOURCE_CONTINUE
def test_window(self):
app = gtk.EmmentalApplication()
window = gtk.Builder.get_object("window")
thread = curds.ThreadQueue()
thread.push(gtk.Application.run)
while gtk.Application.window == None:
time.sleep(0.1)
self.assertIsInstance(window, Gtk.ApplicationWindow)
self.assertIsNone(app.window)
thread.push(app.run)
gtk.main_loop(delay=0.1)
self.assertTrue(window.is_visible())
self.assertIsNone(gtk.Application.idle_id)
gtk.Application.task_queued()
self.assertGreater(gtk.Application.idle_id, 0)
idle_id = gtk.Application.idle_id
gtk.Application.task_queued()
self.assertEqual(gtk.Application.idle_id, idle_id)
self.assertIsNone(app.idle_id)
app.task_queued()
self.assertGreater(app.idle_id, 0)
idle_id = app.idle_id
app.task_queued()
self.assertEqual(app.idle_id, idle_id)
gtk.main_loop(delay=0.1)
self.assertIsNone(app.idle_id)
gtk.Application.quit()
app.idle_id = GLib.idle_add(self.fake_idle)
app.quit()
gtk.main_loop(delay=0.1)
thread.stop()
self.assertEqual(app.idle_id, None)
def test_application(self):
self.assertIsInstance(gtk.Application, Gtk.Application)
@ -62,8 +71,23 @@ class TestGtk(unittest.TestCase):
self.assertFalse(gtk.PlistSep.is_visible())
def test_accel(self):
play_button = gtk.Builder.get_object("play_button")
self.assertFalse(gtk.type_focused(Gtk.Entry))
gtk.PlistSearch.grab_focus()
self.assertTrue(gtk.type_focused(Gtk.Entry))
gtk.Window.grab_focus()
self.assertFalse(gtk.type_focused(Gtk.Entry))
signal = GObject.signal_lookup("activate", Gtk.Button)
self.assertTrue(play_button.can_activate_accel(signal))
gtk.PlistSearch.grab_focus()
self.assertFalse(play_button.can_activate_accel(signal))
gtk.Window.grab_focus()
self.assertFalse(gtk.UpDown.get_active())
signal = GObject.signal_lookup("grab-focus", Gtk.SearchEntry)
self.assertTrue(gtk.PlistSearch.can_activate_accel(signal))
self.assertTrue(gtk.UpDown.get_active())
self.assertFalse(gtk.PlistSearch.can_activate_accel(signal))
self.assertFalse(gtk.UpDown.get_active())