TestCuraSceneController.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. from UM.Scene.SceneNode import SceneNode
  4. from cura.Machines.Models.MultiBuildPlateModel import MultiBuildPlateModel
  5. from cura.Scene.CuraSceneController import CuraSceneController
  6. from cura.UI.ObjectsModel import ObjectsModel
  7. @pytest.fixture
  8. def objects_model() -> ObjectsModel:
  9. return MagicMock(spec=ObjectsModel)
  10. @pytest.fixture
  11. def multi_build_plate_model() -> MultiBuildPlateModel:
  12. return MagicMock(spec=MultiBuildPlateModel)
  13. @pytest.fixture
  14. def mocked_application():
  15. mocked_application = MagicMock()
  16. mocked_controller = MagicMock()
  17. mocked_scene = MagicMock()
  18. mocked_application.getController = MagicMock(return_value=mocked_controller)
  19. mocked_controller.getScene = MagicMock(return_value=mocked_scene)
  20. return mocked_application
  21. def test_setActiveBuildPlate(objects_model, multi_build_plate_model):
  22. with patch("UM.Application.Application.getInstance"):
  23. controller = CuraSceneController(objects_model, multi_build_plate_model)
  24. controller.setActiveBuildPlate(12)
  25. multi_build_plate_model.setActiveBuildPlate.assert_called_once_with(12)
  26. objects_model.setActiveBuildPlate.assert_called_once_with(12)
  27. # Doing it again shouldn't cause another change to be passed along
  28. controller.setActiveBuildPlate(12)
  29. multi_build_plate_model.setActiveBuildPlate.assert_called_once_with(12)
  30. objects_model.setActiveBuildPlate.assert_called_once_with(12)
  31. def test_calcMaxBuildPlateEmptyScene(objects_model, multi_build_plate_model, mocked_application):
  32. mocked_root = MagicMock()
  33. mocked_root.callDecoration = MagicMock(return_value=0)
  34. mocked_application.getController().getScene().getRoot = MagicMock(return_value=mocked_root)
  35. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  36. controller = CuraSceneController(objects_model, multi_build_plate_model)
  37. assert controller._calcMaxBuildPlate() == 0
  38. def test_calcMaxBuildPlateFilledScene(objects_model, multi_build_plate_model, mocked_application):
  39. mocked_root = MagicMock()
  40. mocked_root.callDecoration = MagicMock(return_value = 0)
  41. mocked_child = MagicMock()
  42. mocked_child.callDecoration = MagicMock(return_value = 2)
  43. mocked_root.getAllChildren = MagicMock(return_value = [mocked_child])
  44. mocked_application.getController().getScene().getRoot = MagicMock(return_value=mocked_root)
  45. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  46. controller = CuraSceneController(objects_model, multi_build_plate_model)
  47. assert controller._calcMaxBuildPlate() == 2
  48. def test_updateMaxBuildPlate(objects_model, multi_build_plate_model):
  49. with patch("UM.Application.Application.getInstance"):
  50. controller = CuraSceneController(objects_model, multi_build_plate_model)
  51. controller._calcMaxBuildPlate = MagicMock(return_value = 12)
  52. controller.updateMaxBuildPlate(SceneNode())
  53. # Check if that went well.
  54. multi_build_plate_model.setMaxBuildPlate.assert_called_once_with(12)
  55. # Move to a different active build plate
  56. controller.setActiveBuildPlate(5)
  57. # And check what happens if we move down again!
  58. controller._calcMaxBuildPlate = MagicMock(return_value=2)
  59. controller.updateMaxBuildPlate(SceneNode())
  60. assert controller._active_build_plate == 0 # We don't have any items anywere, so default to 0