Document.py

Go to the documentation of this file.
00001 #***************************************************************************
00002 #*   (c) Juergen Riegel (juergen.riegel@web.de) 2003                       *
00003 #*                                                                         *
00004 #*   This file is part of the FreeCAD CAx development system.              *
00005 #*                                                                         *
00006 #*   This program is free software; you can redistribute it and/or modify  *
00007 #*   it under the terms of the GNU Lesser General Public License (LGPL)    *
00008 #*   as published by the Free Software Foundation; either version 2 of     *
00009 #*   the License, or (at your option) any later version.                   *
00010 #*   for detail see the LICENCE text file.                                 *
00011 #*                                                                         *
00012 #*   FreeCAD is distributed in the hope that it will be useful,            *
00013 #*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014 #*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015 #*   GNU Library General Public License for more details.                  *
00016 #*                                                                         *
00017 #*   You should have received a copy of the GNU Library General Public     *
00018 #*   License along with FreeCAD; if not, write to the Free Software        *
00019 #*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020 #*   USA                                                                   *
00021 #*                                                                         *
00022 #*   Juergen Riegel 2003                                                   *
00023 #***************************************************************************/
00024 
00025 import FreeCAD, os, unittest, tempfile
00026 
00027 
00028 #---------------------------------------------------------------------------
00029 # define the functions to test the FreeCAD Document code
00030 #---------------------------------------------------------------------------
00031 
00032 
00033 class DocumentBasicCases(unittest.TestCase):
00034   def setUp(self):
00035     self.Doc = FreeCAD.newDocument("CreateTest")
00036 
00037   def testCreateDestroy(self):
00038     #FIXME: Causes somehow a ref count error but it's _not_ FreeCAD.getDocument()!!!
00039     #If we remove the whole method no error appears.
00040     self.failUnless(FreeCAD.getDocument("CreateTest")!= None,"Creating Document failed")
00041 
00042   def testAddition(self):
00043     # Cannot write a real test case for that but when debugging the
00044     # C-code there shouldn't be a memory leak (see rev. 1814)
00045     self.Doc.openTransaction("Add")
00046     L1 = self.Doc.addObject("App::FeatureTest","Label")
00047     self.Doc.commitTransaction()
00048     self.Doc.undo()
00049 
00050   def testRemoval(self):
00051     # Cannot write a real test case for that but when debugging the
00052     # C-code there shouldn't be a memory leak (see rev. 1814)
00053     self.Doc.openTransaction("Add")
00054     L1 = self.Doc.addObject("App::FeatureTest","Label")
00055     self.Doc.commitTransaction()
00056     self.Doc.openTransaction("Rem")
00057     L1 = self.Doc.removeObject("Label")
00058     self.Doc.commitTransaction()
00059 
00060   def testObjects(self):
00061     L1 = self.Doc.addObject("App::FeatureTest","Label_1")
00062     #call members to check for errors in ref counting
00063     self.Doc.ActiveObject
00064     self.Doc.Objects
00065     self.Doc.UndoMode
00066     self.Doc.UndoRedoMemSize
00067     self.Doc.UndoCount
00068     # test read only mechanismus
00069     try:
00070       self.Doc.UndoCount = 3
00071     except:
00072       FreeCAD.Console.PrintLog("   exception thrown, OK\n")
00073     else:
00074       self.fail("no exeption thrown")
00075     self.Doc.RedoCount
00076     self.Doc.UndoNames
00077     self.Doc.RedoNames
00078     self.Doc.recompute()
00079     self.failUnless(L1.Integer == 4711)
00080     self.failUnless(L1.Float-47.11<0.001)
00081     self.failUnless(L1.Bool    == True)
00082     self.failUnless(L1.String  == "4711")
00083     #temporarily not checked because of strange behavior of boost::fielesystem JR
00084     #self.failUnless(L1.Path  == "c:/temp")
00085     self.failUnless(L1.Angle-3.0<0.001)
00086     self.failUnless(L1.Distance-47.11<0.001)
00087 
00088     # test basic property stuff
00089     self.failUnless(not L1.getDocumentationOfProperty("Source1") == "")
00090     self.failUnless(L1.getGroupOfProperty("Source1") == "Feature Test")
00091     self.failUnless(L1.getTypeOfProperty("Source1") == [])
00092 
00093 
00094     # test the constraint types ( both are constraint to percent range)
00095     self.failUnless(L1.ConstraintInt == 5)
00096     self.failUnless(L1.ConstraintFloat-5.0<0.001)
00097     L1.ConstraintInt = 500
00098     L1.ConstraintFloat = 500.0
00099     self.failUnless(L1.ConstraintInt == 100)
00100     self.failUnless(L1.ConstraintFloat - 100.0 < 0.001)
00101     L1.ConstraintInt = -500
00102     L1.ConstraintFloat = -500.0
00103     self.failUnless(L1.ConstraintInt == 0)
00104     self.failUnless(L1.ConstraintFloat - 0.0 < 0.001)
00105 
00106     # test enum property
00107     self.failUnless(L1.Enum  == "Four")
00108     L1.Enum = "One"
00109     L1.Enum = 2
00110     self.failUnless(L1.Enum  == "Two",     "Different value to 'Two'")
00111     try:
00112       L1.Enum = "SurlyNotInThere!"
00113     except:
00114       FreeCAD.Console.PrintLog("   exception thrown, OK\n")
00115     else:
00116       self.fail("no exeption thrown")
00117 
00118     #self.failUnless(L1.IntegerList  == [4711]   )
00119     #f = L1.FloatList
00120     #self.failUnless(f -47.11<0.001    )
00121     #self.failUnless(L1.Matrix  == [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0] )
00122     #self.failUnless(L1.Vector  == [1.0,2.0,3.0])
00123 
00124     self.failUnless(L1.Label== "Label_1","Invalid object name")
00125     L1.Label="Label_2"
00126     self.Doc.recompute()
00127     self.failUnless(L1.Label== "Label_2","Invalid object name")
00128     self.Doc.removeObject("Label_1")
00129 
00130   def testMem(self):
00131     self.Doc.MemSize
00132 
00133   def testAddRemove(self):
00134     L1 = self.Doc.addObject("App::FeatureTest","Label_1")
00135     # must delete object
00136     self.Doc.removeObject(L1.Name)
00137     try:
00138       L1.Name
00139     except:
00140       self.failUnless(True)
00141     else:
00142       self.failUnless(False)
00143     del L1
00144 
00145     # What do we expect here?
00146     self.Doc.openTransaction("AddRemove")
00147     L2 = self.Doc.addObject("App::FeatureTest","Label_2")
00148     self.Doc.removeObject(L2.Name)
00149     self.Doc.commitTransaction()
00150     self.Doc.undo()
00151     try:
00152       L2.Name
00153     except:
00154       self.failUnless(True)
00155     else:
00156       self.failUnless(False)
00157     del L2
00158 
00159   def tearDown(self):
00160     #closing doc
00161     FreeCAD.closeDocument("CreateTest")
00162 
00163 class DocumentSaveRestoreCases(unittest.TestCase):
00164   def setUp(self):
00165     self.Doc = FreeCAD.newDocument("SaveRestoreTests")
00166     L1 = self.Doc.addObject("App::FeatureTest","Label_1")
00167     L2 = self.Doc.addObject("App::FeatureTest","Label_2")
00168     self.TempPath = tempfile.gettempdir()
00169     FreeCAD.Console.PrintLog( '  Using temp path: ' + self.TempPath + '\n')
00170 
00171   def testSaveAndRestore(self):
00172     # saving and restoring
00173     SaveName = self.TempPath + os.sep + "SaveRestoreTests.FCStd"
00174     self.Doc.FileName = SaveName
00175     self.failUnless(self.Doc.Label_1.TypeTransient == 4711)
00176     self.Doc.Label_1.TypeTransient = 4712
00177     # setup Linking
00178     self.Doc.Label_1.Link = self.Doc.Label_2
00179     self.Doc.Label_2.Link = self.Doc.Label_1
00180     self.Doc.Label_1.LinkSub = (self.Doc.Label_2,["Sub1","Sub2"])
00181     self.Doc.Label_2.LinkSub = (self.Doc.Label_1,["Sub3","Sub4"])
00182     # save the document
00183     self.Doc.save()
00184     FreeCAD.closeDocument("SaveRestoreTests")
00185     self.Doc = FreeCAD.open(SaveName)
00186     self.failUnless(self.Doc.Label_1.Integer == 4711)
00187     self.failUnless(self.Doc.Label_2.Integer == 4711)
00188     # test Linkage
00189     self.failUnless(self.Doc.Label_1.Link == self.Doc.Label_2)
00190     self.failUnless(self.Doc.Label_2.Link == self.Doc.Label_1)
00191     self.failUnless(self.Doc.Label_1.LinkSub == (self.Doc.Label_2,["Sub1","Sub2"]))
00192     self.failUnless(self.Doc.Label_2.LinkSub == (self.Doc.Label_1,["Sub3","Sub4"]))
00193     # do  NOT save transient properties
00194     self.failUnless(self.Doc.Label_1.TypeTransient == 4711)
00195     self.failUnless(self.Doc == FreeCAD.getDocument(self.Doc.Name))
00196 
00197   def testRestore(self):
00198     Doc = FreeCAD.newDocument("RestoreTests")
00199     Doc.addObject("App::FeatureTest","Label_1")
00200     # saving and restoring
00201     Doc.FileName = self.TempPath + os.sep + "Test2.FCStd"
00202     Doc.save()
00203     # restore must first clear the current content
00204     Doc.restore()
00205     self.failUnless(len(Doc.Objects) == 1)
00206     FreeCAD.closeDocument("RestoreTests")
00207 
00208   def testActiveDocument(self):
00209     # open 2nd doc
00210     Second = FreeCAD.newDocument("Active")
00211     FreeCAD.closeDocument("Active")
00212     try:
00213         # There might be no active document anymore
00214         # This also checks for dangling pointers
00215         Active = FreeCAD.activeDocument()
00216         # Second is still a valid object
00217         self.failUnless(Second != Active)
00218     except:
00219         # Okay, no document open
00220         self.failUnless(True)
00221 
00222   def tearDown(self):
00223     #closing doc
00224     FreeCAD.closeDocument("SaveRestoreTests")
00225 
00226 class DocumentRecomputeCases(unittest.TestCase):
00227   def setUp(self):
00228     self.Doc = FreeCAD.newDocument("RecomputeTests")
00229     self.L1 = self.Doc.addObject("App::FeatureTest","Label_1")
00230     self.L2 = self.Doc.addObject("App::FeatureTest","Label_2")
00231     self.L3 = self.Doc.addObject("App::FeatureTest","Label_3")
00232 
00233   def testDescent(self):
00234     # testing the up and downstream stuff
00235     FreeCAD.Console.PrintLog("def testDescent(self):Testcase not implemented\n")
00236     self.L1.Link = self.L2
00237     self.L2.Link = self.L3
00238 
00239 
00240   def tearDown(self):
00241     #closing doc
00242     FreeCAD.closeDocument("RecomputeTests")
00243 
00244 class UndoRedoCases(unittest.TestCase):
00245   def setUp(self):
00246     self.Doc = FreeCAD.newDocument("UndoTest")
00247     self.Doc.addObject("App::FeatureTest","Base")
00248     self.Doc.addObject("App::FeatureTest","Del")
00249     self.Doc.getObject("Del").Integer  = 2
00250 
00251   def testUndoProperties(self):
00252     # switch on the Undo
00253     self.Doc.UndoMode = 1
00254 
00255     # first transaction
00256     self.Doc.openTransaction("Transaction1")
00257     self.Doc.addObject("App::FeatureTest","test1")
00258     self.Doc.getObject("test1").Integer  = 1
00259     self.Doc.getObject("test1").String   = "test1"
00260     self.Doc.getObject("test1").Float    = 1.0
00261     self.Doc.getObject("test1").Bool  = 1
00262 
00263     #self.Doc.getObject("test1").IntegerList  = 1
00264     #self.Doc.getObject("test1").FloatList  = 1.0
00265 
00266     #self.Doc.getObject("test1").Matrix  = (1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0)
00267     #self.Doc.getObject("test1").Vector  = (1.0,1.0,1.0)
00268 
00269     # second transaction
00270     self.Doc.openTransaction("Transaction2")
00271     self.Doc.getObject("test1").Integer  = 2
00272     self.Doc.getObject("test1").String   = "test2"
00273     self.Doc.getObject("test1").Float    = 2.0
00274     self.Doc.getObject("test1").Bool  = 0
00275 
00276     # switch on the Undo OFF
00277     self.Doc.UndoMode = 0
00278 
00279   def testUndoClear(self):
00280     # switch on the Undo
00281     self.Doc.UndoMode = 1
00282     self.assertEqual(self.Doc.UndoNames,[])
00283     self.assertEqual(self.Doc.UndoCount,0)
00284     self.assertEqual(self.Doc.RedoNames,[])
00285     self.assertEqual(self.Doc.RedoCount,0)
00286 
00287     self.Doc.openTransaction("Transaction1")
00288     # becomes the active object
00289     self.Doc.addObject("App::FeatureTest","test1")
00290     self.Doc.commitTransaction()
00291     # removes the active object
00292     self.Doc.undo()
00293     self.assertEqual(self.Doc.ActiveObject,None)
00294     # deletes the active object
00295     self.Doc.clearUndos()
00296     self.assertEqual(self.Doc.ActiveObject,None)
00297 
00298   def testUndo(self):
00299     # switch on the Undo
00300     self.Doc.UndoMode = 1
00301     self.assertEqual(self.Doc.UndoNames,[])
00302     self.assertEqual(self.Doc.UndoCount,0)
00303     self.assertEqual(self.Doc.RedoNames,[])
00304     self.assertEqual(self.Doc.RedoCount,0)
00305 
00306     # first transaction
00307     self.Doc.openTransaction("Transaction1")
00308     self.Doc.addObject("App::FeatureTest","test1")
00309     self.Doc.getObject("test1").Integer  = 1
00310     self.Doc.getObject("Del").Integer  = 1
00311     self.Doc.removeObject("Del")
00312     self.assertEqual(self.Doc.UndoNames,['Transaction1'])
00313     self.assertEqual(self.Doc.UndoCount,1)
00314     self.assertEqual(self.Doc.RedoNames,[])
00315     self.assertEqual(self.Doc.RedoCount,0)
00316 
00317     # second transaction
00318     self.Doc.openTransaction("Transaction2")
00319     self.assertEqual(self.Doc.UndoNames,['Transaction2','Transaction1'])
00320     self.assertEqual(self.Doc.UndoCount,2)
00321     self.assertEqual(self.Doc.RedoNames,[])
00322     self.assertEqual(self.Doc.RedoCount,0)
00323     self.Doc.getObject("test1").Integer  = 2
00324     self.assertEqual(self.Doc.UndoNames,['Transaction2','Transaction1'])
00325     self.assertEqual(self.Doc.UndoCount,2)
00326     self.assertEqual(self.Doc.RedoNames,[])
00327     self.assertEqual(self.Doc.RedoCount,0)
00328 
00329     # abort second transaction
00330     self.Doc.abortTransaction()
00331     self.assertEqual(self.Doc.UndoNames,['Transaction1'])
00332     self.assertEqual(self.Doc.UndoCount,1)
00333     self.assertEqual(self.Doc.RedoNames,[])
00334     self.assertEqual(self.Doc.RedoCount,0)
00335     self.assertEqual(self.Doc.getObject("test1").Integer, 1)
00336 
00337     # again second transaction
00338     self.Doc.openTransaction("Transaction2")
00339     self.Doc.getObject("test1").Integer  = 2
00340     self.assertEqual(self.Doc.UndoNames,['Transaction2','Transaction1'])
00341     self.assertEqual(self.Doc.UndoCount,2)
00342     self.assertEqual(self.Doc.RedoNames,[])
00343     self.assertEqual(self.Doc.RedoCount,0)
00344 
00345     # third transaction
00346     self.Doc.openTransaction("Transaction3")
00347     self.assertEqual(self.Doc.UndoNames,['Transaction3','Transaction2','Transaction1'])
00348     self.assertEqual(self.Doc.UndoCount,3)
00349     self.Doc.getObject("test1").Integer  = 3
00350     self.assertEqual(self.Doc.RedoNames,[])
00351     self.assertEqual(self.Doc.RedoCount,0)
00352 
00353     # fourth transaction
00354     self.Doc.openTransaction("Transaction4")
00355     self.assertEqual(self.Doc.UndoNames,['Transaction4','Transaction3','Transaction2','Transaction1'])
00356     self.assertEqual(self.Doc.UndoCount,4)
00357     self.assertEqual(self.Doc.RedoNames,[])
00358     self.assertEqual(self.Doc.RedoCount,0)
00359     self.Doc.getObject("test1").Integer  = 4
00360 
00361     # undo the fourth transaction
00362     self.Doc.undo()
00363     self.assertEqual(self.Doc.getObject("test1").Integer, 3)
00364     self.assertEqual(self.Doc.UndoNames,['Transaction3','Transaction2','Transaction1'])
00365     self.assertEqual(self.Doc.UndoCount,3)
00366     self.assertEqual(self.Doc.RedoNames,['Transaction4'])
00367     self.assertEqual(self.Doc.RedoCount,1)
00368 
00369     # undo the third transaction
00370     self.Doc.undo()
00371     self.assertEqual(self.Doc.getObject("test1").Integer, 2)
00372     self.assertEqual(self.Doc.UndoNames,['Transaction2','Transaction1'])
00373     self.assertEqual(self.Doc.UndoCount,2)
00374     self.assertEqual(self.Doc.RedoNames,['Transaction3','Transaction4'])
00375     self.assertEqual(self.Doc.RedoCount,2)
00376 
00377     # undo the second transaction
00378     self.Doc.undo()
00379     self.assertEqual(self.Doc.getObject("test1").Integer, 1)
00380     self.assertEqual(self.Doc.UndoNames,['Transaction1'])
00381     self.assertEqual(self.Doc.UndoCount,1)
00382     self.assertEqual(self.Doc.RedoNames,['Transaction2','Transaction3','Transaction4'])
00383     self.assertEqual(self.Doc.RedoCount,3)
00384 
00385     # undo the first transaction
00386     self.Doc.undo()
00387     self.failUnless(self.Doc.getObject("test1") == None)
00388     self.failUnless(self.Doc.getObject("Del").Integer == 2)
00389     self.assertEqual(self.Doc.UndoNames,[])
00390     self.assertEqual(self.Doc.UndoCount,0)
00391     self.assertEqual(self.Doc.RedoNames,['Transaction1','Transaction2','Transaction3','Transaction4'])
00392     self.assertEqual(self.Doc.RedoCount,4)
00393 
00394     # redo the first transaction
00395     self.Doc.redo()
00396     self.assertEqual(self.Doc.getObject("test1").Integer, 1)
00397     self.assertEqual(self.Doc.UndoNames,['Transaction1'])
00398     self.assertEqual(self.Doc.UndoCount,1)
00399     self.assertEqual(self.Doc.RedoNames,['Transaction2','Transaction3','Transaction4'])
00400     self.assertEqual(self.Doc.RedoCount,3)
00401 
00402     # redo the second transaction
00403     self.Doc.redo()
00404     self.assertEqual(self.Doc.getObject("test1").Integer, 2)
00405     self.assertEqual(self.Doc.UndoNames,['Transaction2','Transaction1'])
00406     self.assertEqual(self.Doc.UndoCount,2)
00407     self.assertEqual(self.Doc.RedoNames,['Transaction3','Transaction4'])
00408     self.assertEqual(self.Doc.RedoCount,2)
00409 
00410     # undo the second transaction
00411     self.Doc.undo()
00412     self.assertEqual(self.Doc.getObject("test1").Integer, 1)
00413     self.assertEqual(self.Doc.UndoNames,['Transaction1'])
00414     self.assertEqual(self.Doc.UndoCount,1)
00415     self.assertEqual(self.Doc.RedoNames,['Transaction2','Transaction3','Transaction4'])
00416     self.assertEqual(self.Doc.RedoCount,3)
00417 
00418     # new transaction eight
00419     self.Doc.openTransaction("Transaction8")
00420     self.Doc.getObject("test1").Integer  = 8
00421     self.assertEqual(self.Doc.UndoNames,['Transaction8','Transaction1'])
00422     self.assertEqual(self.Doc.UndoCount,2)
00423     self.assertEqual(self.Doc.RedoNames,[])
00424     self.assertEqual(self.Doc.RedoCount,0)
00425     self.Doc.abortTransaction()
00426     self.assertEqual(self.Doc.UndoNames,['Transaction1'])
00427     self.assertEqual(self.Doc.UndoCount,1)
00428     self.assertEqual(self.Doc.RedoNames,[])
00429     self.assertEqual(self.Doc.RedoCount,0)
00430 
00431     # again new transaction eight
00432     self.Doc.openTransaction("Transaction8")
00433     self.Doc.getObject("test1").Integer  = 8
00434     self.assertEqual(self.Doc.UndoNames,['Transaction8','Transaction1'])
00435     self.assertEqual(self.Doc.UndoCount,2)
00436     self.assertEqual(self.Doc.RedoNames,[])
00437     self.assertEqual(self.Doc.RedoCount,0)
00438 
00439     # again new transaction nine
00440     self.Doc.openTransaction("Transaction9")
00441     self.Doc.getObject("test1").Integer  = 9
00442     self.assertEqual(self.Doc.UndoNames,['Transaction9','Transaction8','Transaction1'])
00443     self.assertEqual(self.Doc.UndoCount,3)
00444     self.assertEqual(self.Doc.RedoNames,[])
00445     self.assertEqual(self.Doc.RedoCount,0)
00446     self.Doc.commitTransaction()
00447     self.assertEqual(self.Doc.UndoNames,['Transaction9','Transaction8','Transaction1'])
00448     self.assertEqual(self.Doc.UndoCount,3)
00449     self.assertEqual(self.Doc.RedoNames,[])
00450     self.assertEqual(self.Doc.RedoCount,0)
00451     self.assertEqual(self.Doc.getObject("test1").Integer, 9)
00452 
00453     # undo the ninth transaction
00454     self.Doc.undo()
00455     self.assertEqual(self.Doc.getObject("test1").Integer, 8)
00456     self.assertEqual(self.Doc.UndoNames,['Transaction8','Transaction1'])
00457     self.assertEqual(self.Doc.UndoCount,2)
00458     self.assertEqual(self.Doc.RedoNames,['Transaction9'])
00459     self.assertEqual(self.Doc.RedoCount,1)
00460 
00461     # switch on the Undo OFF
00462     self.Doc.UndoMode = 0
00463     self.assertEqual(self.Doc.UndoNames,[])
00464     self.assertEqual(self.Doc.UndoCount,0)
00465     self.assertEqual(self.Doc.RedoNames,[])
00466     self.assertEqual(self.Doc.RedoCount,0)
00467 
00468   def testGroup(self):
00469     # Add an object to the group
00470     L2 = self.Doc.addObject("App::FeatureTest","Label_2")
00471     G1 = self.Doc.addObject("App::DocumentObjectGroup","Group")
00472     G1.addObject(L2)
00473     self.failUnless(G1.hasObject(L2))
00474 
00475     # Adding the group to itself must fail
00476     try:
00477       G1.addObject(G1)
00478     except:
00479       FreeCAD.Console.PrintLog("Cannot add group to itself, OK\n")
00480     else:
00481       self.fail("Adding the group to itself must not be possible")
00482 
00483     self.Doc.UndoMode = 1
00484 
00485     # Remove object from group
00486     self.Doc.openTransaction("Remove")
00487     self.Doc.removeObject("Label_2")
00488     self.Doc.commitTransaction()
00489     self.failUnless(G1.getObject("Label_2") == None)
00490     self.Doc.undo()
00491     self.failUnless(G1.getObject("Label_2") != None)
00492 
00493     # Remove first group and then the object
00494     self.Doc.openTransaction("Remove")
00495     self.Doc.removeObject("Group")
00496     self.Doc.removeObject("Label_2")
00497     self.Doc.commitTransaction()
00498     self.Doc.undo()
00499     self.failUnless(G1.getObject("Label_2") != None)
00500 
00501     # Remove first object and then the group in two transactions
00502     self.Doc.openTransaction("Remove")
00503     self.Doc.removeObject("Label_2")
00504     self.Doc.commitTransaction()
00505     self.failUnless(G1.getObject("Label_2") == None)
00506     self.Doc.openTransaction("Remove")
00507     self.Doc.removeObject("Group")
00508     self.Doc.commitTransaction()
00509     self.Doc.undo()
00510     self.Doc.undo()
00511     self.failUnless(G1.getObject("Label_2") != None)
00512 
00513     # Remove first object and then the group in one transaction
00514     self.Doc.openTransaction("Remove")
00515     self.Doc.removeObject("Label_2")
00516     self.failUnless(G1.getObject("Label_2") == None)
00517     self.Doc.removeObject("Group")
00518     self.Doc.commitTransaction()
00519     self.Doc.undo()
00520     # FIXME: See bug #1820554
00521     self.failUnless(G1.getObject("Label_2") != None)
00522 
00523     # Add a second object to the group
00524     L3 = self.Doc.addObject("App::FeatureTest","Label_3")
00525     G1.addObject(L3)
00526     self.Doc.openTransaction("Remove")
00527     self.Doc.removeObject("Label_2")
00528     self.failUnless(G1.getObject("Label_2") == None)
00529     self.Doc.removeObject("Label_3")
00530     self.failUnless(G1.getObject("Label_3") == None)
00531     self.Doc.removeObject("Group")
00532     self.Doc.commitTransaction()
00533     self.Doc.undo()
00534     self.failUnless(G1.getObject("Label_3") != None)
00535     self.failUnless(G1.getObject("Label_2") != None)
00536 
00537     self.Doc.UndoMode = 0
00538 
00539     # Cleanup
00540     self.Doc.removeObject("Group")
00541     self.Doc.removeObject("Label_2")
00542     self.Doc.removeObject("Label_3")
00543 
00544 
00545   def tearDown(self):
00546     # closing doc
00547     FreeCAD.closeDocument("UndoTest")
00548 
00549 
00550 class DocumentPlatformCases(unittest.TestCase):
00551   def setUp(self):
00552     self.Doc = FreeCAD.newDocument("PlatformTests")
00553     self.Doc.addObject("App::FeatureTest", "Test")
00554     self.TempPath = tempfile.gettempdir()
00555     self.DocName = self.TempPath + os.sep + "PlatformTests.FCStd"
00556     self.Doc.FileName = self.DocName
00557 
00558   def testFloatList(self):
00559     self.Doc.Test.FloatList = [-0.05, 2.5, 5.2]
00560 
00561     # saving and restoring
00562     self.Doc.save()
00563     FreeCAD.closeDocument("PlatformTests")
00564     self.Doc = FreeCAD.open(self.DocName)
00565 
00566     self.failUnless(abs(self.Doc.Test.FloatList[0] + .05) < 0.01)
00567     self.failUnless(abs(self.Doc.Test.FloatList[1] - 2.5) < 0.01)
00568     self.failUnless(abs(self.Doc.Test.FloatList[2] - 5.2) < 0.01)
00569 
00570   def testColorList(self):
00571     self.Doc.Test.ColourList = [(1.0,0.5,0.0),(0.0,0.5,1.0)]
00572 
00573     # saving and restoring
00574     self.Doc.save()
00575     FreeCAD.closeDocument("PlatformTests")
00576     self.Doc = FreeCAD.open(self.DocName)
00577 
00578     self.failUnless(abs(self.Doc.Test.ColourList[0][0] - 1.0) < 0.01)
00579     self.failUnless(abs(self.Doc.Test.ColourList[0][1] - 0.5) < 0.01)
00580     self.failUnless(abs(self.Doc.Test.ColourList[0][2] - 0.0) < 0.01)
00581     self.failUnless(abs(self.Doc.Test.ColourList[0][3] - 0.0) < 0.01)
00582     self.failUnless(abs(self.Doc.Test.ColourList[1][0] - 0.0) < 0.01)
00583     self.failUnless(abs(self.Doc.Test.ColourList[1][1] - 0.5) < 0.01)
00584     self.failUnless(abs(self.Doc.Test.ColourList[1][2] - 1.0) < 0.01)
00585     self.failUnless(abs(self.Doc.Test.ColourList[1][3] - 0.0) < 0.01)
00586 
00587   def testVectorList(self):
00588     self.Doc.Test.VectorList = [(-0.05, 2.5, 5.2),(-0.05, 2.5, 5.2)]
00589 
00590     # saving and restoring
00591     self.Doc.save()
00592     FreeCAD.closeDocument("PlatformTests")
00593     self.Doc = FreeCAD.open(self.DocName)
00594 
00595     self.failUnless(len(self.Doc.Test.VectorList) == 2)
00596 
00597   def testPoints(self):
00598     try:
00599       self.Doc.addObject("Points::Feature", "Points")
00600 
00601       # saving and restoring
00602       self.Doc.save()
00603       FreeCAD.closeDocument("PlatformTests")
00604       self.Doc = FreeCAD.open(self.DocName)
00605 
00606       self.failUnless(self.Doc.Points.Points.count() == 0)
00607     except:
00608       pass
00609 
00610   def tearDown(self):
00611     #closing doc
00612     FreeCAD.closeDocument("PlatformTests")
00613 class DocumentFileIncludeCases(unittest.TestCase):
00614   def setUp(self):
00615     self.Doc = FreeCAD.newDocument("FileIncludeTests")
00616     # testing with undo
00617     self.Doc.UndoMode = 1
00618 
00619 
00620   def testApplyFiles(self):
00621     self.Doc.openTransaction("Transaction0")
00622     self.L1 = self.Doc.addObject("App::DocumentObjectFileIncluded","FileObject1")
00623     self.failUnless(self.L1.File =="")
00624     self.Filename = self.L1.File
00625 
00626     self.Doc.openTransaction("Transaction1")
00627     self.TempPath = tempfile.gettempdir()
00628     # creating a file in the Transient directory of the document
00629     file = open(self.Doc.getTempFileName("test"),"w")
00630     file.write("test No1")
00631     file.close()
00632     # applying the file
00633     self.L1.File = (file.name,"Test.txt")
00634     self.failUnless(self.L1.File.split("/")[-1] == "Test.txt")
00635     # read again
00636     file = open(self.L1.File,"r")
00637     self.failUnless(file.read()=="test No1")
00638     file.close()
00639     file = open(self.TempPath+"/testNest.txt","w")
00640     file.write("test No2")
00641     file.close()
00642     # applying the file
00643     self.Doc.openTransaction("Transaction2")
00644     self.L1.File = file.name
00645     self.failUnless(self.L1.File.split("/")[-1] == "Test.txt")
00646     # read again
00647     file = open(self.L1.File,"r")
00648     self.failUnless(file.read()=="test No2")
00649     file.close()
00650     self.Doc.undo()
00651     self.failUnless(self.L1.File.split("/")[-1] == "Test.txt")
00652     # read again
00653     file = open(self.L1.File,"r")
00654     self.failUnless(file.read()=="test No1")
00655     file.close()
00656     self.Doc.undo()
00657     # read again
00658     self.failUnless(self.L1.File == "")
00659     self.Doc.redo()
00660     self.failUnless(self.L1.File.split("/")[-1] == "Test.txt")
00661     # read again
00662     file = open(self.L1.File,"r")
00663     self.failUnless(file.read()=="test No1")
00664     file.close()
00665     self.Doc.redo()
00666     self.failUnless(self.L1.File.split("/")[-1] == "Test.txt")
00667     # read again
00668     file = open(self.L1.File,"r")
00669     self.failUnless(file.read()=="test No2")
00670     file.close()
00671     # Save restore test
00672     self.Doc.FileName = self.TempPath+"/FileIncludeTest.fcstd"
00673     self.Doc.save()
00674     FreeCAD.closeDocument("FileIncludeTests")
00675     self.Doc = FreeCAD.open(self.TempPath+"/FileIncludeTest.fcstd")
00676     # check if the file is still there
00677     self.L1 = self.Doc.getObject("FileObject1")
00678     file = open(self.L1.File,"r")
00679     res = file.read()
00680     FreeCAD.Console.PrintLog( res +"\n")
00681     self.failUnless(res=="test No2")
00682     self.failUnless(self.L1.File.split("/")[-1] == "Test.txt")
00683     file.close()
00684 
00685     # test for bug #94 (File overlap in PropertyFileIncluded)
00686     L2 = self.Doc.addObject("App::DocumentObjectFileIncluded","FileObject2")
00687     L3 = self.Doc.addObject("App::DocumentObjectFileIncluded","FileObject3")
00688 
00689     # creating two files in the Transient directory of the document
00690     file1 = open(self.Doc.getTempFileName("test"),"w")
00691     file1.write("test No1")
00692     file1.close()
00693     file2 = open(self.Doc.getTempFileName("test"),"w")
00694     file2.write("test No2")
00695     file2.close()
00696 
00697     # applying the file with the same base name
00698     L2.File = (file1.name,"Test.txt")
00699     L3.File = (file2.name,"Test.txt")
00700 
00701     file = open(L2.File,"r")
00702     self.failUnless(file.read()=="test No1")
00703     file.close()
00704     file = open(L3.File,"r")
00705     self.failUnless(file.read()=="test No2")
00706     file.close()
00707 
00708 
00709   def tearDown(self):
00710     #closing doc
00711     FreeCAD.closeDocument("FileIncludeTest")
00712 
00713 
00714 class DocumentPropertyCases(unittest.TestCase):
00715   def setUp(self):
00716     self.Doc = FreeCAD.newDocument("PropertyTests")
00717     self.Obj = self.Doc.addObject("App::FeaturePython","Test")
00718 
00719   def testDescent(self):
00720     # testing the up and downstream stuff
00721     props=self.Obj.supportedProperties()
00722     for i in props:
00723         self.Obj.addProperty(i,i)
00724     tempPath = tempfile.gettempdir()
00725     tempFile = tempPath + os.sep + "PropertyTests.FCStd"
00726     self.Doc.FileName = tempFile
00727     self.Doc.save()
00728     FreeCAD.closeDocument("PropertyTests")
00729     self.Doc = FreeCAD.open(tempFile)
00730 
00731   def tearDown(self):
00732     #closing doc
00733     FreeCAD.closeDocument("PropertyTests")

Generated on Wed Nov 23 19:00:07 2011 for FreeCAD by  doxygen 1.6.1