OSDN Git Service

profiling: get rid of disk write activity
[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
13     def __del__(self):
14         if self.tempdir:
15             os.system('rm -rf ' + self.tempdir)
16
17     def setup(self):
18         self.tempdir = tempfile.mkdtemp()
19         from gui import application
20         self.app = application.Application(datapath='..', confpath=self.tempdir, filenames=[])
21
22         # fatal exceptions, please
23         def excepthook(exctyp, value, tb):
24             traceback.print_exception (exctyp, value, tb, None, sys.stderr)
25             sys.exit(1)
26         sys.excepthook = excepthook
27
28     def signal_cb(self):
29         self.waiting = False
30
31     def wait_for_idle(self):
32         "wait until the last mypaint idle handler has finished"
33         if not self.app: self.setup()
34         self.signal = False
35         gobject.idle_add(self.signal_cb, priority=gobject.PRIORITY_LOW + 50)
36         self.waiting = True
37         while self.waiting:
38             gtk.main_iteration()
39
40     def wait_for_gui(self):
41         "wait until all GUI updates are done, but don't wait for background tasks"
42         if not self.app: self.setup()
43         self.signal = False
44         gobject.idle_add(self.signal_cb, priority=gobject.PRIORITY_DEFAULT_IDLE - 1)
45         self.waiting = True
46         while self.waiting:
47             gtk.main_iteration()
48
49     def wait_for_duration(self, duration):
50         if not self.app: self.setup()
51         self.signal = False
52         gobject.timeout_add(int(duration*1000.0), self.signal_cb)
53         self.waiting = True
54         while self.waiting:
55             gtk.main_iteration()
56
57     def scroll(self, N=20):
58         tdw = self.app.doc.tdw
59         dx = linspace(-30, 30, N)
60         dy = linspace(-10, 60, N)
61         for i in xrange(N):
62             tdw.scroll(int(dx[i]), int(dy[i]))
63             self.wait_for_idle()
64         # jump back to the start
65         for i in xrange(N):
66             tdw.scroll(-int(dx[i]), -int(dy[i]))
67