andrej@1808: #!/usr/bin/env python andrej@1808: # -*- coding: utf-8 -*- andrej@1808: andrej@1808: # This file is part of Beremiz, a Integrated Development Environment for andrej@1808: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1808: # andrej@1808: # Copyright (C) 2017: Andrey Skvortsov andrej@1808: # andrej@1808: # See COPYING file for copyrights details. andrej@1808: # andrej@1808: # This program is free software; you can redistribute it and/or andrej@1808: # modify it under the terms of the GNU General Public License andrej@1808: # as published by the Free Software Foundation; either version 2 andrej@1808: # of the License, or (at your option) any later version. andrej@1808: # andrej@1808: # This program is distributed in the hope that it will be useful, andrej@1808: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1808: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1808: # GNU General Public License for more details. andrej@1808: # andrej@1808: # You should have received a copy of the GNU General Public License andrej@1808: # along with this program; if not, write to the Free Software andrej@1808: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1808: andrej@1796: import os andrej@1796: import sys andrej@1796: import unittest andrej@1809: import pytest andrej@1796: import wx andrej@1796: import time andrej@1796: import traceback andrej@1809: import ddt andrej@1796: andrej@1808: import conftest andrej@1808: import Beremiz 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: self.app = None andrej@1796: andrej@1809: def setUp(self): andrej@1809: self.app = None andrej@1809: 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@1809: @ddt.ddt 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@1808: """Open editor for 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@1809: def CheckTestProject(self, name): andrej@1809: project = self.GetProjectPath(name) andrej@1809: print "Testing example " + name 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: 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: def testStartUp(self): andrej@1796: """Checks whether the app starts and finishes correctly""" andrej@1796: self.StartApp() andrej@1796: self.FinishApp() andrej@1796: andrej@1809: @ddt.data( andrej@1809: "first_steps", andrej@1809: "logging", andrej@1809: "svgui", andrej@1809: "traffic_lights", andrej@1809: "wxGlade", andrej@1809: "python", andrej@1809: "wiimote", andrej@1809: "wxHMI", andrej@1809: ) andrej@1809: @pytest.mark.timeout(30) andrej@1809: def testCheckProject(self, name): andrej@1809: self.CheckTestProject(name) andrej@1796: andrej@1796: andrej@1796: if __name__ == '__main__': andrej@1796: unittest.main()