OSDN Git Service

fix unittest
[mypaint-anime/master.git] / tests / guicontrol.py
1 import gtk, glib, gobject
2 import traceback, tempfile, os, sys
3 from numpy import *
4
5 class GUI:
6     """
7     Class for driving the MyPaint GUI.
8     """
9     def __init__(self):
10         self.app = None
11         self.tempdir = None
12         sys.argv_unicode = sys.argv # FileHandler.save_file passes this to gtk.recent_manager
13
14     def __del__(self):
15         if self.tempdir:
16             os.system('rm -rf ' + self.tempdir)
17
18     def setup(self):
19         self.tempdir = tempfile.mkdtemp()
20         from gui import application
21         os.system('cp -a brushes ' + self.tempdir)
22         self.app = application.Application(datapath=u'..',
23                                            extradata='../desktop',
24                                            confpath=unicode(self.tempdir),
25                                            filenames=[])
26
27         # ignore mouse movements during testing (creating extra strokes)
28         def motion_ignore_cb(*junk1, **junk2):
29             pass
30         self.app.doc.tdw.motion_notify_cb = motion_ignore_cb
31
32         # fatal exceptions, please
33         def excepthook(exctyp, value, tb):
34             traceback.print_exception (exctyp, value, tb, None, sys.stderr)
35             sys.exit(1)
36         sys.excepthook = excepthook
37
38     def signal_cb(self):
39         self.waiting = False
40
41     def wait_for_idle(self):
42         "wait until the last mypaint idle handler has finished"
43         if not self.app: self.setup()
44         self.signal = False
45         gobject.idle_add(self.signal_cb, priority=gobject.PRIORITY_LOW + 50)
46         self.waiting = True
47         while self.waiting:
48             gtk.main_iteration()
49
50     def wait_for_gui(self):
51         "wait until all GUI updates are done, but don't wait for background tasks"
52         if not self.app: self.setup()
53         self.signal = False
54         gobject.idle_add(self.signal_cb, priority=gobject.PRIORITY_DEFAULT_IDLE - 1)
55         self.waiting = True
56         while self.waiting:
57             gtk.main_iteration()
58
59     def wait_for_duration(self, duration):
60         if not self.app: self.setup()
61         self.signal = False
62         gobject.timeout_add(int(duration*1000.0), self.signal_cb)
63         self.waiting = True
64         while self.waiting:
65             gtk.main_iteration()
66
67     def scroll(self, N=20):
68         tdw = self.app.doc.tdw
69         dx = linspace(-30, 30, N)
70         dy = linspace(-10, 60, N)
71         for i in xrange(N):
72             tdw.scroll(int(dx[i]), int(dy[i]))
73             self.wait_for_idle()
74         # jump back to the start
75         for i in xrange(N):
76             tdw.scroll(-int(dx[i]), -int(dy[i]))
77