FreeCAD

Форк
0
/
Workbench.py 
138 строк · 5.8 Кб
1
# -*- coding: utf-8 -*-
2
# ***************************************************************************
3
# *   Copyright (c) 2006 Werner Mayer <werner.wm.mayer@gmx.de>              *
4
# *                                                                         *
5
# *   This file is part of the FreeCAD CAx development system.              *
6
# *                                                                         *
7
# *   This program is free software; you can redistribute it and/or modify  *
8
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
9
# *   as published by the Free Software Foundation; either version 2 of     *
10
# *   the License, or (at your option) any later version.                   *
11
# *   for detail see the LICENCE text file.                                 *
12
# *                                                                         *
13
# *   FreeCAD is distributed in the hope that it will be useful,            *
14
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16
# *   GNU Library General Public License for more details.                  *
17
# *                                                                         *
18
# *   You should have received a copy of the GNU Library General Public     *
19
# *   License along with FreeCAD; if not, write to the Free Software        *
20
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
21
# *   USA                                                                   *
22
# *                                                                         *
23
# ***************************************************************************/
24

25
# Workbench test module
26

27
import FreeCAD, FreeCADGui, os, unittest
28
import tempfile
29

30
from PySide import QtWidgets, QtCore
31
from PySide.QtWidgets import QApplication
32

33

34
class CallableCheckWarning:
35
    def __call__(self):
36
        diag = QApplication.activeModalWidget()
37
        if diag:
38
            QtCore.QTimer.singleShot(0, diag, QtCore.SLOT("accept()"))
39

40

41
class WorkbenchTestCase(unittest.TestCase):
42
    def setUp(self):
43
        self.Active = FreeCADGui.activeWorkbench()
44
        FreeCAD.Console.PrintLog(FreeCADGui.activeWorkbench().name())
45

46
    def testActivate(self):
47
        wbs = FreeCADGui.listWorkbenches()
48
        # this gives workbenches a possibility to detect that we're under test environment
49
        FreeCAD.TestEnvironment = True
50
        for i in wbs:
51
            try:
52
                print("Activate workbench '{}'".format(i))
53
                cobj = CallableCheckWarning()
54
                QtCore.QTimer.singleShot(500, cobj)
55
                if FreeCADGui.activeWorkbench().name() != i:
56
                    success = FreeCADGui.activateWorkbench(i)
57
                else:
58
                    # Cannot test activation of an already-active workbench
59
                    success = True
60
                FreeCAD.Console.PrintLog(
61
                    "Active: " + FreeCADGui.activeWorkbench().name() + " Expected: " + i + "\n"
62
                )
63
                self.assertTrue(success, "Test on activating workbench {0} failed".format(i))
64
            except Exception as e:
65
                self.fail("Loading of workbench '{0}' failed: {1}".format(i, e))
66
        del FreeCAD.TestEnvironment
67

68
    def testHandler(self):
69
        import __main__
70

71
        class UnitWorkbench(__main__.Workbench):
72
            MenuText = "Unittest"
73
            ToolTip = "Unittest"
74

75
            def Initialize(self):
76
                cmds = ["Test_Test"]
77
                self.appendToolbar("My Unittest", cmds)
78

79
            def GetClassName(self):
80
                return "Gui::PythonWorkbench"
81

82
        FreeCADGui.addWorkbench(UnitWorkbench())
83
        wbs = FreeCADGui.listWorkbenches()
84
        self.assertTrue("UnitWorkbench" in wbs, "Test on adding workbench handler failed")
85
        FreeCADGui.activateWorkbench("UnitWorkbench")
86
        FreeCADGui.updateGui()
87
        self.assertTrue(
88
            FreeCADGui.activeWorkbench().name() == "UnitWorkbench",
89
            "Test on loading workbench 'Unittest' failed",
90
        )
91
        FreeCADGui.removeWorkbench("UnitWorkbench")
92
        wbs = FreeCADGui.listWorkbenches()
93
        self.assertTrue(not "UnitWorkbench" in wbs, "Test on removing workbench handler failed")
94

95
    def testInvalidType(self):
96
        class MyExtWorkbench(FreeCADGui.Workbench):
97
            def Initialize(self):
98
                pass
99

100
            def GetClassName(self):
101
                return "App::Extension"
102

103
        FreeCADGui.addWorkbench(MyExtWorkbench())
104
        with self.assertRaises(TypeError):
105
            FreeCADGui.activateWorkbench("MyExtWorkbench")
106
        FreeCADGui.removeWorkbench("MyExtWorkbench")
107

108
    def tearDown(self):
109
        FreeCADGui.activateWorkbench(self.Active.name())
110
        FreeCAD.Console.PrintLog(self.Active.name())
111

112

113
class CommandTestCase(unittest.TestCase):
114
    def testPR6889(self):
115
        # Fixes a crash
116
        TempPath = tempfile.gettempdir()
117
        macroName = TempPath + os.sep + "testmacro.py"
118
        macroFile = open(macroName, "w")
119
        macroFile.write("print ('Hello, World!')")
120
        macroFile.close()
121

122
        name = FreeCADGui.Command.createCustomCommand(macroName)
123
        cmd = FreeCADGui.Command.get(name)
124
        cmd.run()
125

126

127
class TestNavigationStyle(unittest.TestCase):
128
    def setUp(self):
129
        self.Doc = FreeCAD.newDocument("CreateTest")
130

131
    def testInvalidStyle(self):
132
        FreeCADGui.getDocument(self.Doc).ActiveView.setNavigationType("App::Extension")
133
        self.assertNotEqual(
134
            FreeCADGui.getDocument(self.Doc).ActiveView.getNavigationType(), "App::Extension"
135
        )
136

137
    def tearDown(self):
138
        FreeCAD.closeDocument("CreateTest")
139

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.