TestPrintOrderManager.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from unittest.mock import patch, MagicMock
  2. from cura.PrintOrderManager import PrintOrderManager
  3. from cura.Scene.CuraSceneNode import CuraSceneNode
  4. def test_getNodeName():
  5. node1 = CuraSceneNode(name="cat", no_setting_override=True)
  6. node2 = CuraSceneNode(name="dog", no_setting_override=True)
  7. assert PrintOrderManager._getNodeName(node1) == "cat"
  8. assert PrintOrderManager._getNodeName(node2) == "dog"
  9. assert PrintOrderManager._getNodeName(None) == ""
  10. def test_getNodeName_truncatesLongName():
  11. node = CuraSceneNode(name="some_name_longer_than_30_characters", no_setting_override=True)
  12. assert PrintOrderManager._getNodeName(node) == "some_name_longer_than_30_chara"
  13. assert PrintOrderManager._getNodeName(node, max_length=10) == "some_name_"
  14. def test_getSingleSelectedNode():
  15. node1 = CuraSceneNode(no_setting_override=True)
  16. with patch("UM.Scene.Selection.Selection.getAllSelectedObjects", MagicMock(return_value=[node1])):
  17. with patch("UM.Scene.Selection.Selection.getSelectedObject", MagicMock(return_value=node1)):
  18. assert PrintOrderManager._getSingleSelectedNode() == node1
  19. def test_getSingleSelectedNode_returnsNoneIfNothingSelected():
  20. with patch("UM.Scene.Selection.Selection.getAllSelectedObjects", MagicMock(return_value=[])):
  21. assert PrintOrderManager._getSingleSelectedNode() is None
  22. def test_getSingleSelectedNode_returnsNoneIfMultipleObjectsSelected():
  23. node1 = CuraSceneNode(no_setting_override=True)
  24. node2 = CuraSceneNode(no_setting_override=True)
  25. with patch("UM.Scene.Selection.Selection.getAllSelectedObjects", MagicMock(return_value=[node1, node2])):
  26. assert PrintOrderManager._getSingleSelectedNode() is None
  27. def test_neighborNodeNamesCorrect_WhenSomeNodeSelected():
  28. node1 = CuraSceneNode(no_setting_override=True, name="node1")
  29. node2 = CuraSceneNode(no_setting_override=True, name="node2")
  30. node3 = CuraSceneNode(no_setting_override=True, name="node3")
  31. node1.printOrder = 1
  32. node2.printOrder = 2
  33. node3.printOrder = 3
  34. with patch.object(PrintOrderManager, "_configureEvents", return_value=None):
  35. with patch.object(PrintOrderManager, "_getSingleSelectedNode", return_value=node1):
  36. print_order_manager = PrintOrderManager(get_nodes=lambda: [node1, node2, node3])
  37. assert print_order_manager.previousNodeName == ""
  38. assert print_order_manager.nextNodeName == "node2"
  39. assert not print_order_manager.shouldEnablePrintBeforeAction
  40. assert print_order_manager.shouldEnablePrintAfterAction
  41. print_order_manager.swapSelectedAndNextNodes() # swaps node1 with node2, result: [node2, node1, node3]
  42. assert print_order_manager.previousNodeName == "node2"
  43. assert print_order_manager.nextNodeName == "node3"
  44. assert print_order_manager.shouldEnablePrintBeforeAction
  45. assert print_order_manager.shouldEnablePrintAfterAction
  46. print_order_manager.swapSelectedAndNextNodes() # swaps node1 with node3, result: [node2, node3, node1]
  47. assert print_order_manager.previousNodeName == "node3"
  48. assert print_order_manager.nextNodeName == ""
  49. assert print_order_manager.shouldEnablePrintBeforeAction
  50. assert not print_order_manager.shouldEnablePrintAfterAction
  51. print_order_manager.swapSelectedAndPreviousNodes() # swaps node1 with node3, result: [node2, node1, node3]
  52. assert print_order_manager.previousNodeName == "node2"
  53. assert print_order_manager.nextNodeName == "node3"
  54. assert print_order_manager.shouldEnablePrintBeforeAction
  55. assert print_order_manager.shouldEnablePrintAfterAction
  56. print_order_manager.swapSelectedAndPreviousNodes() # swaps node1 with node2, result: [node1, node2, node3]
  57. assert print_order_manager.previousNodeName == ""
  58. assert print_order_manager.nextNodeName == "node2"
  59. assert not print_order_manager.shouldEnablePrintBeforeAction
  60. assert print_order_manager.shouldEnablePrintAfterAction
  61. def test_neighborNodeNamesEmpty_WhenNothingSelected():
  62. node1 = CuraSceneNode(no_setting_override=True, name="node1")
  63. node2 = CuraSceneNode(no_setting_override=True, name="node2")
  64. node3 = CuraSceneNode(no_setting_override=True, name="node3")
  65. node1.printOrder = 1
  66. node2.printOrder = 2
  67. node3.printOrder = 3
  68. with patch.object(PrintOrderManager, "_configureEvents", return_value=None):
  69. with patch.object(PrintOrderManager, "_getSingleSelectedNode", return_value=None):
  70. print_order_manager = PrintOrderManager(get_nodes=lambda: [node1, node2, node3])
  71. assert print_order_manager.previousNodeName == ""
  72. assert print_order_manager.nextNodeName == ""
  73. assert not print_order_manager.shouldEnablePrintBeforeAction
  74. assert not print_order_manager.shouldEnablePrintAfterAction
  75. def test_initializePrintOrders():
  76. node1 = CuraSceneNode(no_setting_override=True)
  77. node2 = CuraSceneNode(no_setting_override=True)
  78. # assume print orders are 0
  79. assert node1.printOrder == 0
  80. assert node2.printOrder == 0
  81. PrintOrderManager.initializePrintOrders([node1, node2])
  82. # assert print orders initialized
  83. assert node1.printOrder == 1
  84. assert node2.printOrder == 2
  85. node3 = CuraSceneNode(no_setting_override=True)
  86. node4 = CuraSceneNode(no_setting_override=True)
  87. # assume print orders are 0
  88. assert node3.printOrder == 0
  89. assert node4.printOrder == 0
  90. PrintOrderManager.initializePrintOrders([node2, node1, node3, node4])
  91. # assert print orders not changed for node1 and node2 and initialized for node3 and node4
  92. assert node1.printOrder == 1
  93. assert node2.printOrder == 2
  94. assert node3.printOrder == 3
  95. assert node4.printOrder == 4
  96. def test_updatePrintOrdersAfterGroupOperation():
  97. node1 = CuraSceneNode(no_setting_override=True)
  98. node2 = CuraSceneNode(no_setting_override=True)
  99. node3 = CuraSceneNode(no_setting_override=True)
  100. node4 = CuraSceneNode(no_setting_override=True)
  101. node5 = CuraSceneNode(no_setting_override=True)
  102. node1.printOrder = 1
  103. node2.printOrder = 2
  104. node3.printOrder = 3
  105. node4.printOrder = 4
  106. node5.printOrder = 5
  107. all_nodes = [node1, node2, node3, node4, node5]
  108. grouped_nodes = [node2, node4]
  109. group_node = CuraSceneNode(no_setting_override=True)
  110. PrintOrderManager.updatePrintOrdersAfterGroupOperation(all_nodes, group_node, grouped_nodes)
  111. assert node1.printOrder == 1
  112. assert group_node.printOrder == 2
  113. assert node3.printOrder == 3
  114. assert node5.printOrder == 4
  115. def test_updatePrintOrdersAfterUngroupOperation():
  116. node1 = CuraSceneNode(no_setting_override=True)
  117. node2 = CuraSceneNode(no_setting_override=True)
  118. node3 = CuraSceneNode(no_setting_override=True)
  119. node1.printOrder = 1
  120. node2.printOrder = 2
  121. node3.printOrder = 3
  122. all_nodes = [node1, node2, node3]
  123. node4 = CuraSceneNode(no_setting_override=True)
  124. node5 = CuraSceneNode(no_setting_override=True)
  125. group_node = node2
  126. ungrouped_nodes = [node4, node5]
  127. PrintOrderManager.updatePrintOrdersAfterUngroupOperation(all_nodes, group_node, ungrouped_nodes)
  128. assert node1.printOrder == 1
  129. assert node4.printOrder == 2
  130. assert node5.printOrder == 3
  131. assert node3.printOrder == 4
  132. assert node1 in all_nodes
  133. assert node2 not in all_nodes
  134. assert node3 in all_nodes
  135. assert node4 in all_nodes
  136. assert node5 in all_nodes