|
1 import os |
|
2 import sys |
|
3 import unittest |
|
4 import wx |
|
5 import time |
|
6 import traceback |
|
7 from xvfbwrapper import Xvfb |
|
8 |
|
9 vdisplay = None |
|
10 |
|
11 |
|
12 def setUpModule(): |
|
13 vdisplay = Xvfb(width=1280, height=720) |
|
14 vdisplay.start() |
|
15 |
|
16 |
|
17 def tearDownModule(): |
|
18 if vdisplay is not None: |
|
19 vdisplay.stop() |
|
20 |
|
21 |
|
22 class UserApplicationTest(unittest.TestCase): |
|
23 def InstallExceptionHandler(self): |
|
24 def handle_exception(e_type, e_value, e_traceback): |
|
25 # traceback.print_exception(e_type, e_value, e_traceback) |
|
26 self.exc_info = [e_type, e_value, e_traceback] |
|
27 self.exc_info = None |
|
28 self.old_excepthook = sys.excepthook |
|
29 sys.excepthook = handle_exception |
|
30 |
|
31 def StartApp(self): |
|
32 self.app = None |
|
33 |
|
34 def FinishApp(self): |
|
35 wx.CallAfter(self.app.frame.Close) |
|
36 self.app.MainLoop() |
|
37 # time.sleep(0.2) |
|
38 self.app = None |
|
39 |
|
40 def tearDown(self): |
|
41 if self.app is not None and self.app.frame is not None: |
|
42 self.FinishApp() |
|
43 |
|
44 def RunUIActions(self, actions): |
|
45 for act in actions: |
|
46 wx.CallAfter(*act) |
|
47 self.ProcessEvents() |
|
48 |
|
49 def CheckForErrors(self): |
|
50 if self.exc_info is not None: |
|
51 # reraise catched previously exception |
|
52 raise self.exc_info[0], self.exc_info[1], self.exc_info[2] |
|
53 |
|
54 def ProcessEvents(self): |
|
55 for i in range(0, 30): |
|
56 self.CheckForErrors() |
|
57 wx.Yield() |
|
58 time.sleep(0.01) |
|
59 |
|
60 |
|
61 class BeremizApplicationTest(UserApplicationTest): |
|
62 """Test Beremiz as whole application""" |
|
63 def StartApp(self): |
|
64 self.app = Beremiz.BeremizIDELauncher() |
|
65 # disable default exception handler in Beremiz |
|
66 self.app.InstallExceptionHandler = lambda: None |
|
67 self.InstallExceptionHandler() |
|
68 self.app.PreStart() |
|
69 |
|
70 def FinishApp(self): |
|
71 wx.CallAfter(self.app.frame.Close) |
|
72 self.app.MainLoop() |
|
73 time.sleep(1) |
|
74 self.app = None |
|
75 |
|
76 def OpenAllProjectElements(self): |
|
77 # open every object in the project tree |
|
78 self.app.frame.ProjectTree.ExpandAll() |
|
79 self.ProcessEvents() |
|
80 item = self.app.frame.ProjectTree.GetRootItem() |
|
81 while item is not None: |
|
82 self.app.frame.ProjectTree.SelectItem(item, True) |
|
83 self.ProcessEvents() |
|
84 id = self.app.frame.ProjectTree.GetId() |
|
85 event = wx.lib.agw.customtreectrl.TreeEvent( |
|
86 wx.lib.agw.customtreectrl.wxEVT_TREE_ITEM_ACTIVATED, |
|
87 id, item) |
|
88 self.app.frame.OnProjectTreeItemActivated(event) |
|
89 self.ProcessEvents() |
|
90 item = self.app.frame.ProjectTree.GetNextVisible(item) |
|
91 |
|
92 def CheckTestProject(self, project): |
|
93 sys.argv = ["", project] |
|
94 self.StartApp() |
|
95 self.OpenAllProjectElements() |
|
96 |
|
97 user_actions = [ |
|
98 [self.app.frame.SwitchFullScrMode, None], |
|
99 [self.app.frame.SwitchFullScrMode, None], |
|
100 [self.app.frame.CTR._Clean], |
|
101 [self.app.frame.CTR._Build], |
|
102 [self.app.frame.CTR._Connect], |
|
103 [self.app.frame.CTR._Transfer], |
|
104 [self.app.frame.CTR._Run], |
|
105 [self.app.frame.CTR._Stop], |
|
106 [self.app.frame.CTR._Disconnect], |
|
107 ] |
|
108 |
|
109 # user_actions.append([self.app.frame.OnCloseProjectMenu, None]) |
|
110 self.RunUIActions(user_actions) |
|
111 self.FinishApp() |
|
112 |
|
113 def GetProjectPath(self, project): |
|
114 return os.path.abspath(os.path.join(os.path.dirname(__file__), "..", project)) |
|
115 |
|
116 # @unittest.skip("") |
|
117 def testStartUp(self): |
|
118 """Checks whether the app starts and finishes correctly""" |
|
119 self.StartApp() |
|
120 self.FinishApp() |
|
121 |
|
122 # @unittest.skip("") |
|
123 def testOpenExampleProjects(self): |
|
124 """Opens, builds and runs user PLC examples from tests directory""" |
|
125 prj = [ |
|
126 "first_steps", |
|
127 "logging", |
|
128 "svgui", |
|
129 "traffic_lights", |
|
130 "wxGlade", |
|
131 "python", |
|
132 "wiimote", |
|
133 "wxHMI", |
|
134 ] |
|
135 for name in prj: |
|
136 project = self.GetProjectPath(name) |
|
137 print "Testing example " + name |
|
138 self.CheckTestProject(project) |
|
139 |
|
140 |
|
141 if __name__ == '__main__': |
|
142 if __package__ is None: |
|
143 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) |
|
144 global Beremiz |
|
145 import Beremiz |
|
146 unittest.main() |