1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
|
#########################################################################
#
# Date: Aug 2003 Author: Daniel Stoffler
#
# stoffler@scripps.edu
#
# Copyright: Daniel Stoffler and TSRI
#
#########################################################################
import sys, string, os
from time import sleep
from NetworkEditor.net import Network
from NetworkEditor.simpleNE import NetworkNode, NetworkBuilder
from NetworkEditor.macros import MacroNode, MacroInputNode, MacroOutputNode, \
MacroNetwork
from NetworkEditor.Tests.nodes import PassNode, DialNode, PrintNode
ed = None
node1 = None
node2 = None
def setUp():
global ed
ed = NetworkBuilder("test builder1", withShell=0,
visibleWidth=400, visibleHeight=350)
ed.master.update()
ed.configure(withThreads=0)
def tearDown():
ed.exit_cb()
import gc
gc.collect()
def pause(sleepTime=None):
if sleepTime is None:
from NetworkEditor.Tests import pauseLength as sleepTime
ed.master.update()
sleep(sleepTime)
def test_01_createAndDestroyMacro():
"""this test adds a macro node to a new network named 'test network1'.
It then tests if a new network named 'test macro' is created and if a
Macroinput and a MacroOutputNode are added. Then the MacroNetwork will
collapse and expanded and finally the entire network gets deleted"""
net = Network('createAndDestroyMacro')
ed.addNetwork(net)
# add macro node
node0 = MacroNode(name='test macro')
net.addNode(node0, 200, 200)
# test that a MacroNode in the main network has been created
macronode = net.nodes[0]
assert isinstance(macronode, MacroNode), \
"Expected %s, got %s"%(MacroNode, macronode.__class__)
# test that MacroNode attribute network is set to the main
# network and that the attribute macroNetwork is set to the MacroNetwork
assert isinstance(macronode.network, Network),\
"Expected %s, got %s"%(Network, macronode.network.__class__)
assert hasattr(macronode, 'macroNetwork'),\
"MacroNode has not attribute 'macroNetwork'"
assert isinstance(macronode.macroNetwork, MacroNetwork),\
"Expected %s, got %s"%(MacroNetwork, macronode.macroNetwork.__class__)
macronet = macronode.macroNetwork
# test MacroNetwork name, same as node0.name
assert macronet.name == node0.name == 'test macro',\
"MacroNetwork.name should be 'test macro', but name is '%s'"%macronet.name
# test that a MacroInputNode and a MacroOutputNode have been added to the
# MacroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
assert isinstance(ipnode, MacroInputNode),\
"Expected %s, got %s"%(MacroInputNode, ipnode.__class__)
assert isinstance(opnode, MacroOutputNode),\
"Expected %s, got %s"%(MacroOutputNode, opnode.__class__)
# test that MacroInput and MacroOutputNode know about the MacroNode
assert hasattr(ipnode, 'macroNode'),\
"ipnode has not attr 'macroNode'"
assert hasattr(opnode, 'macroNode'),\
"opnode has no attr 'macroNode'"
# test that the macroNetwork know about the MacroInput and MacroOutputNode
assert hasattr(macronet, 'ipNode'),\
"macronet has no attr 'ipNode'"
assert hasattr(macronet, 'opNode'),\
"macronet has not attr 'opNode'"
# test that a MacroNetwork can be collapsed
macronode.shrink()
pause()
# assert that the MacroNetwork with the name 'test macro' still exists
assert 'test macro' in net.editor.networks.keys(),\
"Current keys are: %s"%net.editor.networks.keys()
assert 'createAndDestroyMacro' in net.editor.networks.keys(),\
"Current keys are: %s"%net.editor.networks.keys()
# test that a MacroNetwork can be expanded again
macronode.expand()
pause()
# assert that the MacroNetwork is back in the list
assert 'test macro' in net.editor.networks.keys(),\
"Current keys are: %s"%net.editor.networks.keys()
assert 'createAndDestroyMacro' in net.editor.networks.keys(),\
"Current keys are: %s"%net.editor.networks.keys()
# shrink macro again
macronode.shrink()
pause()
# delete macro
net.deleteNodes([macronode])
# delete network
ed.deleteNetwork(net)
def test_02_deleteExpandedMacro():
"""This tests if an opended macro network is properly deleted when deleting
the MacroNode in the main network.
PLEASE NOTE: DELETING A MAIN NETWORK WITH A MACRO IS NOT POSSIBLE IN VIPER
BECAUSE WE DISABLE THE 'DELETE' MENU ENTRY."""
net = Network('deleteExpandedMacro')
ed.addNetwork(net)
# add macro node
node0 = MacroNode(name='test macro2')
net.addNode(node0, 200, 200)
macronode = node0
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
# assert both networks are present
assert 'deleteExpandedMacro' in net.editor.networks.keys(),\
"Current keys are: %s"%net.editor.networks.keys()
assert 'test macro2' in net.editor.networks.keys(),\
"Current keys are: %s"%net.editor.networks.keys()
pause()
# delete the MacroNode
net.deleteNodes([macronode])
# assert the MacroNetwork 'test macro2' is gone
assert 'test macro2' not in net.editor.networks.keys(),\
"Current keys are: %s"%net.editor.networks.keys()
pause()
# delete network
ed.deleteNetwork(net)
def test_03_addPorts():
"""This tests if new ports are created upon connecting nodes to a
MacroInputNode or a MacroOutputode"""
net = Network('addPorts')
ed.addNetwork(net)
# add macro node
node0 = MacroNode(name='test macro3')
net.addNode(node0, 200, 200)
macronode = node0
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
# ipnode should have 1 special output port
# opnode should have 1 special input port
# macronode should have 0 input/output ports
assert len(ipnode.outputPorts) == 1,\
"Expected 1, got %s"%len(ipnode.outputPorts)
assert len(opnode.inputPorts) == 1,\
"Expected 1, got %s"%len(opnode.inputPorts)
assert len(macronode.inputPorts) == 0,\
"Expected 0, got %s"%len(macronode.inputPorts)
assert len(macronode.outputPorts) == 0,\
"Expected 0, got %s"%len(macronode.outputPorts)
node1 = PassNode()
macronet.addNode(node1, 150, 150)
macronet.connectNodes(ipnode, node1, 0, 0)
macronet.connectNodes(node1, opnode, 0, 0)
# now, all nodes (except the Pass) should have 1 port more
assert len(ipnode.outputPorts) == 2,\
"Expected 2, got %s"%len(ipnode.outputPorts)
assert len(opnode.inputPorts) == 2,\
"Expected 2, got %s"%len(opnode.inputPorts)
assert len(macronode.inputPorts) == 1,\
"Expected 1, got %s"%len(macronode.inputPorts)
assert len(macronode.outputPorts) == 1,\
"Expected 1, got %s"%len(macronode.outputPorts)
pause()
net.deleteNodes([macronode])
ed.deleteNetwork(net)
def test_04_deletePorts():
"""This tests if new ports that were created upon connecting nodes to a
MacroInputNode or a MacroOutputode are getting destroyed if the connection
is destroyed"""
net = Network('deletePorts')
ed.addNetwork(net)
# add macro node
node0 = MacroNode(name='test macro3')
net.addNode(node0, 200, 200)
macronode = node0
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
# add pass node and connect
node1 = PassNode()
macronet.addNode(node1, 150, 150)
macronet.connectNodes(ipnode, node1, 0, 0)
macronet.connectNodes(node1, opnode, 0, 0)
pause()
# now delete the connection to MacroInputNode
c = node1.inputPorts[0].connections[0]
macronet.deleteConnections([c])
pause()
# macroinputnode should now have only 1 outputport
assert len(ipnode.outputPorts) == 1,\
"Expected 1, got %s"%len(ipnode.outputPorts)
# macronode should have 0 inputports
assert len(macronode.inputPorts) == 0,\
"Expected 0, got %s"%len(macronode.inputPorts)
# delete the connection to MacroOutputNode
c = node1.outputPorts[0].connections[0]
macronet.deleteConnections([c])
pause()
# macrooutputnode should now have only 1 inputport
assert len(opnode.inputPorts) == 1,\
"Expected 1, got %s"%len(opnode.inputPorts)
# macronode should have 0 outputports
assert len(macronode.outputPorts) == 0,\
"Expected 0, got %s"%len(macronode.outputPorts)
net.deleteNodes([macronode])
ed.deleteNetwork(net)
def test_05_deleteMacroConnections():
"""This tests if connections to the Macro node are destroyed when
connections inside the macronet to the ip and op node are destroyed"""
net = Network('deleteMacroConnections')
ed.addNetwork(net)
# add macro node
node0 = MacroNode(name='test macro4')
net.addNode(node0, 200, 200)
macronode = node0
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
# add pass node and connect
node1 = PassNode()
macronet.addNode(node1, 150, 150)
macronet.connectNodes(ipnode, node1, 0, 0)
macronet.connectNodes(node1, opnode, 0, 0)
node0.shrink()
pause()
# connect pass nodes in the main network to the macro node
pass1 = PassNode()
net.addNode(pass1, 50, 50)
pass2 = PassNode()
net.addNode(pass2, 50, 250)
net.connectNodes(pass1, node0, 0, 0)
net.connectNodes(node0, pass2, 0, 0)
pause()
# now delete the pass node in the macro network
node0.expand()
macronet.deleteNodes([node1])
pause()
assert len(pass1.outputPorts[0].connections) == 0,\
"Expected 0, got %s"%len(pass1.outputPorts[0].connections)
assert len(pass2.inputPorts[0].connections) == 0,\
"Expected 0, got %s"%len(pass2.inputPorts[0].connections)
node0.shrink()
pause()
net.deleteNodes([macronode])
ed.deleteNetwork(net)
def test_06_deleteOneOfTwoConnections():
"""If 2 or more nodes connect to the same macro ip or op node, the
port of the ip or op node should NOT be deleted"""
net = Network('deleteOneofTwoConnections')
ed.addNetwork(net)
# add macro node
node0 = MacroNode(name='test macro5')
net.addNode(node0, 200, 200)
macronode = node0
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
# add pass node and connect
node1 = PassNode()
macronet.addNode(node1, 150, 150)
macronet.connectNodes(ipnode, node1, 0, 0)
macronet.connectNodes(node1, opnode, 0, 0)
pause()
# add second pass node and connect
node2 = PassNode()
macronet.addNode(node2, 250, 150)
macronet.connectNodes(ipnode, node2, 1, 0)
# no new outputport has been created
assert len(ipnode.outputPorts) == 2,\
"Expected 2, got %s"%len(ipnode.outputPorts)
pause()
# now delete the second pass node in the macro network
macronet.deleteNodes([node2])
pause()
# no port should have been deleted
assert len(ipnode.outputPorts) == 2,\
"Expected 2, got %s"%len(ipnode.outputPorts)
assert len(macronode.inputPorts) == 1,\
"Expected 1, got %s"%len(macronode.inputPorts)
# connection should still be there
assert len(node1.inputPorts[0].connections) == 1,\
"Expected 1, got %s"%len(node1.inputPorts[0].connections)
node0.shrink()
pause()
net.deleteNodes([macronode])
ed.deleteNetwork(net)
def test_07_whoIsCurrentNetwork():
"""add 2 macros into the main network, expand both, go to the second
macronetwork and shrink it. We should end up in the main network and not
in the macronetwork of the first macro."""
net = Network('whoIsCurrentNetwork')
ed.addNetwork(net)
# add macro node1
node1 = MacroNode(name='macro1')
net.addNode(node1, 200, 200)
# add macro node2
node2 = MacroNode(name='macro2')
net.addNode(node2, 250, 200)
node1.expand()
node2.expand()
# are we in macronetwork of node2?
ed.setNetwork(node2.macroNetwork)
pause()
assert ed.currentNetwork.name == 'macro2',\
"Expected 'macro2', got '%s'"%ed.currentNetwork.name
# shrink node2
node2.shrink()
pause()
assert ed.currentNetwork.name == 'whoIsCurrentNetwork',\
"Expected 'whoIsCurrentNetwork', got '%s'"%ed.currentNetwork.name
net.deleteNodes([node1, node2])
ed.deleteNetwork(net)
def test_08_deleteNetwork():
"""This tests if the macro network is deleted when a network with
a macro node is deleted"""
net = Network('saveDelete')
ed.addNetwork(net)
numberNetworks = len(ed.networks) # store number of current networks
# add macro node1
node1 = MacroNode(name='macro1')
net.addNode(node1, 200, 200)
# now we should have one more network
assert len(ed.networks) == numberNetworks+1,\
"Expected %s, got %s"%(numberNetworks+1, len(ed.networks) )
# add Pass node to macro network, connect
node2 = PassNode()
ed.currentNetwork.addNode(node2, 200, 150)
macronode = node1
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
macronet.connectNodes(ipnode, node2, 0, 0)
macronet.connectNodes(node2, opnode, 0, 0)
pause()
node1.shrink()
ed.deleteNetwork(ed.currentNetwork)
# now we should have one less network
assert len(ed.networks) == numberNetworks-1,\
"Expected %s, got %s"%(numberNetworks-1, len(ed.networks) )
def test_09_saveLoadRunsimpleMacro():
"""build a very simple macro (containing a pass node), connect this to a
Dial and a Print node, save this macro, load it, and run it."""
net = Network('saveLoadTest')
net.runOnNewData.value = True
ed.addNetwork(net)
# add macro node1
node1 = MacroNode(name='macro1')
net.addNode(node1, 200, 200)
# add Pass node to macro network, connect
node2 = PassNode()
ed.currentNetwork.addNode(node2, 200, 150)
macronode = node1
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
macronet.connectNodes(ipnode, node2, 0, 0)
macronet.connectNodes(node2, opnode, 0, 0)
# we have only 1 connection, we want singleConnection so that data is not
# wrapped into a list
opnode.inputPorts[1].configure(singleConnection=True)
pause()
node1.shrink()
# add Dial node, print node and connect
node3 = DialNode()
net.addNode(node3, 75, 75)
net.connectNodes(node3, node1, 0, 0)
node4 = PrintNode()
net.addNode(node4, 75, 250)
net.connectNodes(node1, node4, 0, 0)
pause()
# now save network
# make sure there is no such file
os.system("rm -f tmpMacro_net.py*")
# save the file
ed.saveNetwork('tmpMacro_net.py')
# and delete the network
ed.deleteNetwork(ed.currentNetwork)
# load the file
pause()
ed.loadNetwork('tmpMacro_net.py')
# can we run this network?
dialNode = ed.currentNetwork.nodes[1]
dialNode.inputPorts[0].widget.set(42.0)
pause()
printNode = ed.currentNetwork.nodes[2]
# is the value passed through the macro
assert printNode.inputPorts[0].data == 42.0,\
"Expected 42.0, got %s"%printNode.inputPorts[0].data
# clean up
os.system("rm -f tmpMacro_net.py*")
def test_10_saveLoadRunNestedMacro():
"""build a nested macro (a macro in a macro containing a pass node),
connect this to a Dial and a Print node, save this macro, load it,
and run it."""
net = Network('saveLoadNestedTest')
net.runOnNewData.value = True
ed.addNetwork(net)
# add macro node0
node0 = MacroNode(name='macro0')
net.addNode(node0, 200, 200)
# add macro node inside this macro network
node1 = MacroNode(name='macro1')
ed.currentNetwork.addNode(node1, 200, 200)
# add Pass node to macro network, connect
node2 = PassNode()
ed.currentNetwork.addNode(node2, 200, 150)
macronode = node1
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
macronet.connectNodes(ipnode, node2, 0, 0)
macronet.connectNodes(node2, opnode, 0, 0)
# we have only 1 connection, we want singleConnection so that data is not
# wrapped into a list
opnode.inputPorts[1].configure(singleConnection=True)
pause()
node1.shrink()
macronode = node0
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
macronet.connectNodes(ipnode, node1, 0, 0)
macronet.connectNodes(node1, opnode, 0, 0)
# configure the new input port to be singleConnection, so that the value
# passed through the macro is not wrapped into a list
opnode.inputPorts[1].configure(singleConnection=True)
# we have only 1 connection, we want singleConnection so that data is not
# wrapped into a list
opnode.inputPorts[1].configure(singleConnection=True)
pause()
node0.shrink()
# add Dial node, print node and connect
node3 = DialNode()
net.addNode(node3, 75, 75)
net.connectNodes(node3, node0, 0, 0)
node4 = PrintNode()
net.addNode(node4, 75, 250)
net.connectNodes(node0, node4, 0, 0)
pause()
# now save network
# make sure there is no such file
os.system("rm -f tmpMacro_net.py*")
# save the file
ed.saveNetwork('tmpMacro_net.py')
# and delete the network
ed.deleteNetwork(ed.currentNetwork)
# load the file
pause()
ed.loadNetwork('tmpMacro_net.py')
# can we run this network?
dialNode = ed.currentNetwork.nodes[1]
dialNode.inputPorts[0].widget.set(42.0)
pause()
printNode = ed.currentNetwork.nodes[2]
# is the value passed through the macro
assert printNode.inputPorts[0].data == 42.0,\
"Expected 42.0, got %s"%printNode.inputPorts[0].data
# clean up
os.system("rm -f tmpMacro_net.py*")
def test_11_cutPasteMacro():
"""build a simple macro with just a pass node, cut and paste it."""
net = Network('cutPasteTest')
ed.addNetwork(net)
# add macro node
node1 = MacroNode(name='macro1')
net.addNode(node1, 200, 200)
# add Pass node to macro network, connect
node2 = PassNode()
ed.currentNetwork.addNode(node2, 200, 150)
macronode = node1
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
opnode = macronet.nodes[1]
macronet.connectNodes(ipnode, node2, 0, 0)
macronet.connectNodes(node2, opnode, 0, 0)
pause()
node1.shrink()
# select the macro node, so we can use the cutNetwork_cb()
net.selectNodes([node1])
# now cut this node
net.editor.cutNetwork_cb()
pause()
# now paste the macro back
net.editor.pasteNetwork_cb()
pause()
def test_12_connectNodewithPortCallback():
"""This tests a previous bug where connecting a pass node only to the
macroinput node and then connecting a dial outside to the macro node
would fail, because the pass node input port has an afterConnect()
method which was also added to the newly created input port of the
macro node"""
net = Network('PortCallbackTest')
ed.addNetwork(net)
# add macro node
node1 = MacroNode(name='macro1')
net.addNode(node1, 200, 200)
# add Pass node to macro network, connect only to ipnode
node2 = PassNode()
ed.currentNetwork.addNode(node2, 200, 150)
macronode = node1
macronet = macronode.macroNetwork
ipnode = macronet.nodes[0]
macronet.connectNodes(ipnode, node2, 0, 0)
node1.shrink()
# add Dial node, and connect
node3 = DialNode()
net.addNode(node3, 75, 75)
net.connectNodes(node3, node1, 0, 0)
pause()
# assert that the new port of the macro node has no callbacks
descr = macronode.inputPorts[0].getDescr()
assert not descr.has_key("beforeConnect"),\
"Current keys in descr: %s"%descr.keys()
assert not descr.has_key("afterConnect"),\
"Current keys in descr: %s"%descr.keys()
assert not descr.has_key("beforeDisconnect"),\
"Current keys in descr: %s"%descr.keys()
assert not descr.has_key("afterDisconnect"),\
"Current keys in descr: %s"%descr.keys()
# and try to delete this connection
c = node3.outputPorts[0].connections[0]
net.deleteConnections([c])
pause()
def test_14_saveRestoreFrozen():
"""Test if we can set frozen=True on the macro node, save and restore
the network and frozen is set to True. (Default value is False)"""
net = Network('TestSaveRestoreFrozen')
ed.addNetwork(net)
# add macro node
node1 = MacroNode(name='macro1')
net.addNode(node1, 200, 200)
node1.shrink()
assert hasattr(node1, 'frozen'), \
"Error: Macro node has now attr 'frozen'"
# now set it to True
node1.toggleFrozen_cb()
# now save network
# make sure there is no such file
os.system("rm -f tmpMacroFrozen_net.py*")
# save the file
ed.saveNetwork('tmpMacroFrozen_net.py')
# and delete the network
ed.deleteNetwork(ed.currentNetwork)
# load the file
pause()
ed.loadNetwork('tmpMacroFrozen_net.py')
# test frozen status
node = ed.currentNetwork.nodes[0]
assert node.frozen == True,\
"Error: macro node attribute 'frozen' not set to True"
# clean up
os.system("rm -f tmpMacroFrozen_net.py*")
|