1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Acceptance tests for wxreactor.
Please test on Linux, Win32 and macOS:
1. Startup event is called at startup.
2. Scheduled event is called after 2 seconds.
3. Shutdown takes 3 seconds, both when quiting from menu and when closing
window (e.g. Alt-F4 in metacity). This tests reactor.stop() and
wxApp.ExitEventLoop().
4. 'hello, world' continues to be printed even when modal dialog is open
(use dialog menu item), when menus are held down, when window is being
dragged.
"""
import sys
import time
try:
from wx import (
EVT_MENU,
App as wxApp,
DefaultPosition as wxDefaultPosition,
Frame as wxFrame,
Menu as wxMenu,
MenuBar as wxMenuBar,
MessageDialog as wxMessageDialog,
Size as wxSize,
)
except ImportError:
from wxPython.wx import *
from twisted.internet import wxreactor
from twisted.python import log
wxreactor.install()
from twisted.internet import defer, reactor
# set up so that "hello, world" is printed continuously
dc = None
def helloWorld():
global dc
print("hello, world", time.time())
dc = reactor.callLater(0.1, helloWorld)
dc = reactor.callLater(0.1, helloWorld)
def twoSecondsPassed():
print("two seconds passed")
def printer(s):
print(s)
def shutdown():
print("shutting down in 3 seconds")
if dc.active():
dc.cancel()
reactor.callLater(1, printer, "2...")
reactor.callLater(2, printer, "1...")
reactor.callLater(3, printer, "0...")
d = defer.Deferred()
reactor.callLater(3, d.callback, 1)
return d
def startup():
print("Start up event!")
reactor.callLater(2, twoSecondsPassed)
reactor.addSystemEventTrigger("after", "startup", startup)
reactor.addSystemEventTrigger("before", "shutdown", shutdown)
ID_EXIT = 101
ID_DIALOG = 102
class MyFrame(wxFrame):
def __init__(self, parent, ID, title):
wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, wxSize(300, 200))
menu = wxMenu()
menu.Append(ID_DIALOG, "D&ialog", "Show dialog")
menu.Append(ID_EXIT, "E&xit", "Terminate the program")
menuBar = wxMenuBar()
menuBar.Append(menu, "&File")
self.SetMenuBar(menuBar)
EVT_MENU(self, ID_EXIT, self.DoExit)
EVT_MENU(self, ID_DIALOG, self.DoDialog)
# you really ought to do this instead of reactor.stop() in
# DoExit, but for the sake of testing we'll let closing the
# window shutdown wx without reactor.stop(), to make sure that
# still does the right thing.
# EVT_CLOSE(self, lambda evt: reactor.stop())
def DoDialog(self, event):
dl = wxMessageDialog(
self,
"Check terminal to see if messages are still being " "printed by Twisted.",
)
dl.ShowModal()
dl.Destroy()
def DoExit(self, event):
reactor.stop()
class MyApp(wxApp):
def OnInit(self):
frame = MyFrame(None, -1, "Hello, world")
frame.Show(True)
self.SetTopWindow(frame)
return True
def demo():
log.startLogging(sys.stdout)
app = MyApp(0)
reactor.registerWxApp(app)
reactor.run()
if __name__ == "__main__":
demo()
|