andrej@1796: import os andrej@1796: import sys andrej@1796: import unittest andrej@1796: import wx andrej@1796: import time andrej@1796: import traceback andrej@1796: from xvfbwrapper import Xvfb andrej@1796: andrej@1796: vdisplay = None andrej@1796: andrej@1796: andrej@1796: def setUpModule(): andrej@1796: vdisplay = Xvfb(width=1280, height=720) andrej@1796: vdisplay.start() andrej@1796: andrej@1796: andrej@1796: def tearDownModule(): andrej@1796: if vdisplay is not None: andrej@1796: vdisplay.stop() andrej@1796: andrej@1796: andrej@1796: class UserApplicationTest(unittest.TestCase): andrej@1796: def InstallExceptionHandler(self): andrej@1796: def handle_exception(e_type, e_value, e_traceback): andrej@1796: # traceback.print_exception(e_type, e_value, e_traceback) andrej@1796: self.exc_info = [e_type, e_value, e_traceback] andrej@1796: self.exc_info = None andrej@1796: self.old_excepthook = sys.excepthook andrej@1796: sys.excepthook = handle_exception andrej@1796: andrej@1796: def StartApp(self): andrej@1796: self.app = None andrej@1796: andrej@1796: def FinishApp(self): andrej@1796: wx.CallAfter(self.app.frame.Close) andrej@1796: self.app.MainLoop() andrej@1796: # time.sleep(0.2) andrej@1796: self.app = None andrej@1796: andrej@1796: def tearDown(self): andrej@1796: if self.app is not None and self.app.frame is not None: andrej@1796: self.FinishApp() andrej@1796: andrej@1796: def RunUIActions(self, actions): andrej@1796: for act in actions: andrej@1796: wx.CallAfter(*act) andrej@1796: self.ProcessEvents() andrej@1796: andrej@1796: def CheckForErrors(self): andrej@1796: if self.exc_info is not None: andrej@1796: # reraise catched previously exception andrej@1796: raise self.exc_info[0], self.exc_info[1], self.exc_info[2] andrej@1796: andrej@1796: def ProcessEvents(self): andrej@1796: for i in range(0, 30): andrej@1796: self.CheckForErrors() andrej@1796: wx.Yield() andrej@1796: time.sleep(0.01) andrej@1796: andrej@1796: andrej@1796: class BeremizApplicationTest(UserApplicationTest): andrej@1796: """Test Beremiz as whole application""" andrej@1796: def StartApp(self): andrej@1796: self.app = Beremiz.BeremizIDELauncher() andrej@1796: # disable default exception handler in Beremiz andrej@1796: self.app.InstallExceptionHandler = lambda: None andrej@1796: self.InstallExceptionHandler() andrej@1796: self.app.PreStart() andrej@1796: andrej@1796: def FinishApp(self): andrej@1796: wx.CallAfter(self.app.frame.Close) andrej@1796: self.app.MainLoop() andrej@1796: time.sleep(1) andrej@1796: self.app = None andrej@1796: andrej@1796: def OpenAllProjectElements(self): andrej@1796: # open every object in the project tree andrej@1796: self.app.frame.ProjectTree.ExpandAll() andrej@1796: self.ProcessEvents() andrej@1796: item = self.app.frame.ProjectTree.GetRootItem() andrej@1796: while item is not None: andrej@1796: self.app.frame.ProjectTree.SelectItem(item, True) andrej@1796: self.ProcessEvents() andrej@1796: id = self.app.frame.ProjectTree.GetId() andrej@1796: event = wx.lib.agw.customtreectrl.TreeEvent( andrej@1796: wx.lib.agw.customtreectrl.wxEVT_TREE_ITEM_ACTIVATED, andrej@1796: id, item) andrej@1796: self.app.frame.OnProjectTreeItemActivated(event) andrej@1796: self.ProcessEvents() andrej@1796: item = self.app.frame.ProjectTree.GetNextVisible(item) andrej@1796: andrej@1796: def CheckTestProject(self, project): andrej@1796: sys.argv = ["", project] andrej@1796: self.StartApp() andrej@1796: self.OpenAllProjectElements() andrej@1796: andrej@1796: user_actions = [ andrej@1796: [self.app.frame.SwitchFullScrMode, None], andrej@1796: [self.app.frame.SwitchFullScrMode, None], andrej@1796: [self.app.frame.CTR._Clean], andrej@1796: [self.app.frame.CTR._Build], andrej@1796: [self.app.frame.CTR._Connect], andrej@1796: [self.app.frame.CTR._Transfer], andrej@1796: [self.app.frame.CTR._Run], andrej@1796: [self.app.frame.CTR._Stop], andrej@1796: [self.app.frame.CTR._Disconnect], andrej@1796: ] andrej@1796: andrej@1796: # user_actions.append([self.app.frame.OnCloseProjectMenu, None]) andrej@1796: self.RunUIActions(user_actions) andrej@1796: self.FinishApp() andrej@1796: andrej@1796: def GetProjectPath(self, project): andrej@1796: return os.path.abspath(os.path.join(os.path.dirname(__file__), "..", project)) andrej@1796: andrej@1796: # @unittest.skip("") andrej@1796: def testStartUp(self): andrej@1796: """Checks whether the app starts and finishes correctly""" andrej@1796: self.StartApp() andrej@1796: self.FinishApp() andrej@1796: andrej@1796: # @unittest.skip("") andrej@1796: def testOpenExampleProjects(self): andrej@1796: """Opens, builds and runs user PLC examples from tests directory""" andrej@1796: prj = [ andrej@1796: "first_steps", andrej@1796: "logging", andrej@1796: "svgui", andrej@1796: "traffic_lights", andrej@1796: "wxGlade", andrej@1796: "python", andrej@1796: "wiimote", andrej@1796: "wxHMI", andrej@1796: ] andrej@1796: for name in prj: andrej@1796: project = self.GetProjectPath(name) andrej@1796: print "Testing example " + name andrej@1796: self.CheckTestProject(project) andrej@1796: andrej@1796: andrej@1796: if __name__ == '__main__': andrej@1796: if __package__ is None: andrej@1796: sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) andrej@1796: global Beremiz andrej@1796: import Beremiz andrej@1796: unittest.main()