综合平台编程器项目的远程存储
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.
 
 
 
 

211 rivejä
8.4 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(project_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. QAction *parallel_insert_action = requiredChild<QAction>(
  77. window, "parallelInsertAction");
  78. QDockWidget *project_dock = requiredChild<QDockWidget>(window, "projectDock");
  79. QDockWidget *properties_dock = requiredChild<QDockWidget>(window, "propertiesDock");
  80. QLabel *selection = requiredChild<QLabel>(window, "selectionValueLabel");
  81. QLineEdit *text_edit = requiredChild<QLineEdit>(window, "controlTextEdit");
  82. HmiEditorWidget *hmi_editor = requiredChild<HmiEditorWidget>(
  83. window, "hmiEditorWidget");
  84. const qreal compact_scale = hmi_editor->transform().m11();
  85. window.resize(1600, 900);
  86. QApplication::processEvents();
  87. require(hmi_editor->transform().m11() > compact_scale,
  88. "HMI page must refit when the window becomes larger");
  89. require(editing_action->isChecked(), "editing action must be selected initially");
  90. require(project_dock->isEnabled(), "project dock must be enabled while editing");
  91. require(properties_dock->isEnabled(), "properties dock must be enabled while editing");
  92. add_button_action->trigger();
  93. const std::string page_id = editor_service.firstPageId();
  94. require(editor_service.findPage(page_id)->controls.size() == 1,
  95. "adding a control must update the HMI page model");
  96. require(selection->text() == QStringLiteral("button-1(未绑定)"),
  97. "an unbound added control must be marked in the property panel");
  98. require(text_edit->text() == QStringLiteral("按钮"),
  99. "property panel must show the control text");
  100. delete_control_action->trigger();
  101. require(editor_service.findPage(page_id)->controls.empty(),
  102. "deleting a selected control must update the HMI page model");
  103. add_normally_open_action->trigger();
  104. parallel_insert_action->trigger();
  105. add_normally_open_action->trigger();
  106. add_normal_coil_action->trigger();
  107. const ControlLogic *logic = logic_editor_service.findLogic(
  108. logic_editor_service.firstLogicId());
  109. require(logic != nullptr && logic->rungs.size() == 1,
  110. "logic actions must edit the default ladder rung");
  111. require(logic->rungs.front().stages.size() == 1
  112. && logic->rungs.front().stages.front().branches.size() == 2,
  113. "parallel insert mode must add a branch to the selected stage");
  114. require(logic->rungs.front().output.has_value(),
  115. "coil action must set the fixed ladder output");
  116. offline_action->trigger();
  117. require(mode_service.mode() == ApplicationMode::Editing,
  118. "unconfigured ladder nodes must block offline running");
  119. const LadderRung &rung = logic->rungs.front();
  120. for (const LogicNode &node : rung.stages.front().branches)
  121. {
  122. require(logic_editor_service.updateNodeConfig(
  123. logic_editor_service.firstLogicId(),
  124. node.id,
  125. ContactNodeConfig{
  126. RegisterAddress{RegisterArea::M, 0},
  127. ContactMode::NormallyOpen})
  128. .succeeded,
  129. "applying a contact configuration must complete the ladder node");
  130. }
  131. require(logic_editor_service.updateNodeConfig(
  132. logic_editor_service.firstLogicId(),
  133. rung.output->id,
  134. CoilNodeConfig{
  135. RegisterAddress{RegisterArea::M, 1},
  136. CoilMode::Normal})
  137. .succeeded,
  138. "applying a coil configuration must complete the ladder node");
  139. offline_action->trigger();
  140. require(mode_service.mode() == ApplicationMode::OfflineRunning,
  141. "offline action must enter offline running");
  142. require(!project_dock->isEnabled(),
  143. "project dock must be disabled while running");
  144. require(!properties_dock->isEnabled(),
  145. "properties dock must be disabled while running");
  146. require(!add_button_action->isEnabled(),
  147. "HMI add controls must be disabled while running");
  148. require(!add_normally_open_action->isEnabled(),
  149. "logic add nodes must be disabled while running");
  150. online_action->trigger();
  151. require(mode_service.mode() == ApplicationMode::OfflineRunning,
  152. "running modes must not switch directly through the UI");
  153. require(offline_action->isChecked(),
  154. "failed mode changes must restore the active action");
  155. editing_action->trigger();
  156. require(mode_service.mode() == ApplicationMode::Editing,
  157. "editing action must return to editing");
  158. require(project_dock->isEnabled(),
  159. "project dock must be restored after returning to editing");
  160. require(properties_dock->isEnabled(),
  161. "properties dock must be restored after returning to editing");
  162. require(add_button_action->isEnabled(),
  163. "HMI add controls must be restored after returning to editing");
  164. require(add_normally_open_action->isEnabled(),
  165. "logic add nodes must be restored after returning to editing");
  166. online_action->trigger();
  167. require(mode_service.mode() == ApplicationMode::Editing,
  168. "online action must require an initial PLC read");
  169. require(editing_action->isChecked(),
  170. "rejected online running must restore the editing action");
  171. }
  172. } // namespace
  173. int main(int argc, char *argv[])
  174. {
  175. // 无窗口平台使 Qt Widgets 测试可在自动化环境稳定运行
  176. qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("offscreen"));
  177. QApplication application(argc, argv);
  178. try
  179. {
  180. testModeActionsControlEditingAvailability();
  181. }
  182. catch (const std::exception &error)
  183. {
  184. std::cerr << "main window tests failed: " << error.what() << '\n';
  185. return 1;
  186. }
  187. std::cout << "main window tests passed\n";
  188. return 0;
  189. }