综合平台编程器项目的远程存储
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

179 line
6.9 KiB

  1. #include "domain/project_storage.h"
  2. #include "domain/register_repository.h"
  3. #include "services/hmi_editor_service.h"
  4. #include "services/hmi_runtime_service.h"
  5. #include "services/logic_editor_service.h"
  6. #include "services/project_service.h"
  7. #include "services/runtime_mode_service.h"
  8. #include "ui/hmi_editor_widget.h"
  9. #include "ui/logic_editor_widget.h"
  10. #include "ui/main_window.h"
  11. #include <QAction>
  12. #include <QApplication>
  13. #include <QDockWidget>
  14. #include <QLabel>
  15. #include <QLineEdit>
  16. #include <iostream>
  17. #include <stdexcept>
  18. #include <string>
  19. namespace {
  20. class TestProjectStorage final : public ProjectStorage
  21. {
  22. public:
  23. // 主窗口测试使用无副作用存储实现,隔离文件对 UI 行为的影响
  24. ProjectSaveResult save(const Project &, const std::string &) override
  25. {
  26. return {true, ProjectStorageError::None, {}};
  27. }
  28. ProjectLoadResult load(const std::string &) override
  29. {
  30. return {false, {}, ProjectStorageError::FileReadFailed, {}};
  31. }
  32. };
  33. void require(bool condition, const std::string &message)
  34. {
  35. if (!condition)
  36. {
  37. throw std::runtime_error(message);
  38. }
  39. }
  40. template<typename ObjectType>
  41. ObjectType *requiredChild(MainWindow &window, const char *name)
  42. {
  43. // 通过 objectName 取得 Designer 组件,缺失时给出明确测试失败信息
  44. ObjectType *child = window.findChild<ObjectType *>(QString::fromLatin1(name));
  45. require(child != nullptr, std::string("missing UI object ") + name);
  46. return child;
  47. }
  48. void testModeActionsControlEditingAvailability()
  49. {
  50. // 验证模式动作会同步禁用编辑入口,并在失败时恢复当前选择
  51. TestProjectStorage storage;
  52. ProjectService project_service(storage);
  53. HmiEditorService editor_service(project_service);
  54. LogicEditorService logic_editor_service(project_service);
  55. VirtualRegisterRepository repository;
  56. HmiRuntimeService runtime_service(repository);
  57. RuntimeModeService mode_service;
  58. MainWindow window(
  59. mode_service,
  60. project_service,
  61. editor_service,
  62. logic_editor_service,
  63. runtime_service);
  64. window.resize(1000, 640);
  65. window.show();
  66. QApplication::processEvents();
  67. QAction *editing_action = requiredChild<QAction>(window, "editingModeAction");
  68. QAction *offline_action = requiredChild<QAction>(window, "offlineModeAction");
  69. QAction *online_action = requiredChild<QAction>(window, "onlineModeAction");
  70. QAction *add_button_action = requiredChild<QAction>(window, "addButtonAction");
  71. QAction *delete_control_action = requiredChild<QAction>(window, "deleteControlAction");
  72. QAction *add_normally_open_action = requiredChild<QAction>(
  73. window, "addNormallyOpenAction");
  74. QAction *add_normal_coil_action = requiredChild<QAction>(
  75. window, "addNormalCoilAction");
  76. QDockWidget *project_dock = requiredChild<QDockWidget>(window, "projectDock");
  77. QDockWidget *properties_dock = requiredChild<QDockWidget>(window, "propertiesDock");
  78. QLabel *selection = requiredChild<QLabel>(window, "selectionValueLabel");
  79. QLineEdit *text_edit = requiredChild<QLineEdit>(window, "controlTextEdit");
  80. HmiEditorWidget *hmi_editor = requiredChild<HmiEditorWidget>(
  81. window, "hmiEditorWidget");
  82. const qreal compact_scale = hmi_editor->transform().m11();
  83. window.resize(1600, 900);
  84. QApplication::processEvents();
  85. require(hmi_editor->transform().m11() > compact_scale,
  86. "HMI page must refit when the window becomes larger");
  87. require(editing_action->isChecked(), "editing action must be selected initially");
  88. require(project_dock->isEnabled(), "project dock must be enabled while editing");
  89. require(properties_dock->isEnabled(), "properties dock must be enabled while editing");
  90. add_button_action->trigger();
  91. const std::string page_id = editor_service.firstPageId();
  92. require(editor_service.findPage(page_id)->controls.size() == 1,
  93. "adding a control must update the HMI page model");
  94. require(selection->text() == QStringLiteral("button-1"),
  95. "selecting an added control must update the property panel");
  96. require(text_edit->text() == QStringLiteral("按钮"),
  97. "property panel must show the control text");
  98. delete_control_action->trigger();
  99. require(editor_service.findPage(page_id)->controls.empty(),
  100. "deleting a selected control must update the HMI page model");
  101. add_normally_open_action->trigger();
  102. add_normal_coil_action->trigger();
  103. const ControlLogic *logic = logic_editor_service.findLogic(
  104. logic_editor_service.firstLogicId());
  105. require(logic != nullptr && logic->nodes.size() == 2,
  106. "logic actions must add nodes through the logic editor service");
  107. offline_action->trigger();
  108. require(mode_service.mode() == ApplicationMode::OfflineRunning,
  109. "offline action must enter offline running");
  110. require(!project_dock->isEnabled(),
  111. "project dock must be disabled while running");
  112. require(!properties_dock->isEnabled(),
  113. "properties dock must be disabled while running");
  114. require(!add_button_action->isEnabled(),
  115. "HMI add controls must be disabled while running");
  116. require(!add_normally_open_action->isEnabled(),
  117. "logic add nodes must be disabled while running");
  118. online_action->trigger();
  119. require(mode_service.mode() == ApplicationMode::OfflineRunning,
  120. "running modes must not switch directly through the UI");
  121. require(offline_action->isChecked(),
  122. "failed mode changes must restore the active action");
  123. editing_action->trigger();
  124. require(mode_service.mode() == ApplicationMode::Editing,
  125. "editing action must return to editing");
  126. require(project_dock->isEnabled(),
  127. "project dock must be restored after returning to editing");
  128. require(properties_dock->isEnabled(),
  129. "properties dock must be restored after returning to editing");
  130. require(add_button_action->isEnabled(),
  131. "HMI add controls must be restored after returning to editing");
  132. require(add_normally_open_action->isEnabled(),
  133. "logic add nodes must be restored after returning to editing");
  134. online_action->trigger();
  135. require(mode_service.mode() == ApplicationMode::Editing,
  136. "online action must require an initial PLC read");
  137. require(editing_action->isChecked(),
  138. "rejected online running must restore the editing action");
  139. }
  140. } // namespace
  141. int main(int argc, char *argv[])
  142. {
  143. // 无窗口平台使 Qt Widgets 测试可在自动化环境稳定运行
  144. qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("offscreen"));
  145. QApplication application(argc, argv);
  146. try
  147. {
  148. testModeActionsControlEditingAvailability();
  149. }
  150. catch (const std::exception &error)
  151. {
  152. std::cerr << "main window tests failed: " << error.what() << '\n';
  153. return 1;
  154. }
  155. std::cout << "main window tests passed\n";
  156. return 0;
  157. }