1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Acceptance tests for wxreactor.
7 Please test on Linux, Win32 and OS X:
8 1. Startup event is called at startup.
9 2. Scheduled event is called after 2 seconds.
10 3. Shutdown takes 3 seconds, both when quiting from menu and when closing
11 window (e.g. Alt-F4 in metacity). This tests reactor.stop() and
12 wxApp.ExitEventLoop().
13 4. 'hello, world' continues to be printed even when modal dialog is open
14 (use dialog menu item), when menus are held down, when window is being
21 from wx import Frame as wxFrame, DefaultPosition as wxDefaultPosition, \
22 Size as wxSize, Menu as wxMenu, MenuBar as wxMenuBar, \
23 EVT_MENU, MessageDialog as wxMessageDialog, App as wxApp
24 except ImportError, e:
25 from wxPython.wx import *
27 from twisted.python import log
28 from twisted.internet import wxreactor
30 from twisted.internet import reactor, defer
33 # set up so that "hello, world" is printed continously
37 print "hello, world", time.time()
38 dc = reactor.callLater(0.1, helloWorld)
39 dc = reactor.callLater(0.1, helloWorld)
41 def twoSecondsPassed():
42 print "two seconds passed"
48 print "shutting down in 3 seconds"
51 reactor.callLater(1, printer, "2...")
52 reactor.callLater(2, printer, "1...")
53 reactor.callLater(3, printer, "0...")
55 reactor.callLater(3, d.callback, 1)
59 print "Start up event!"
61 reactor.callLater(2, twoSecondsPassed)
62 reactor.addSystemEventTrigger("after", "startup", startup)
63 reactor.addSystemEventTrigger("before", "shutdown", shutdown)
69 class MyFrame(wxFrame):
70 def __init__(self, parent, ID, title):
71 wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(300, 200))
73 menu.Append(ID_DIALOG, "D&ialog", "Show dialog")
74 menu.Append(ID_EXIT, "E&xit", "Terminate the program")
76 menuBar.Append(menu, "&File")
77 self.SetMenuBar(menuBar)
78 EVT_MENU(self, ID_EXIT, self.DoExit)
79 EVT_MENU(self, ID_DIALOG, self.DoDialog)
80 # you really ought to do this instead of reactor.stop() in
81 # DoExit, but for the sake of testing we'll let closing the
82 # window shutdown wx without reactor.stop(), to make sure that
83 # still does the right thing.
84 #EVT_CLOSE(self, lambda evt: reactor.stop())
86 def DoDialog(self, event):
87 dl = wxMessageDialog(self, "Check terminal to see if messages are still being "
88 "printed by Twisted.")
92 def DoExit(self, event):
99 frame = MyFrame(None, -1, "Hello, world")
101 self.SetTopWindow(frame)
106 log.startLogging(sys.stdout)
108 reactor.registerWxApp(app)
112 if __name__ == '__main__':