OSDN Git Service

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