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

783 lines
39 KiB

  1. #include "domain/project_storage.h"
  2. #include "domain/register_repository.h"
  3. #include "domain/virtual_register_repository.h"
  4. #include "services/hmi_editor_service.h"
  5. #include "services/editor_history.h"
  6. #include "services/hmi_runtime_service.h"
  7. #include "services/hmi_navigation_service.h"
  8. #include "services/project_service.h"
  9. #include "support/test_support.h"
  10. #include <exception>
  11. #include <iostream>
  12. #include <limits>
  13. #include <stdexcept>
  14. #include <string>
  15. #include <variant>
  16. namespace {
  17. using TestProjectStorage = TestSupport::InMemoryProjectStorage;
  18. using TestSupport::require;
  19. void testControlEditing()
  20. {
  21. // 覆盖控件创建、移动、绑定校验、重命名冲突和删除流程
  22. TestProjectStorage storage;
  23. ProjectService project_service(storage, defaultProjectLimitSettings());
  24. HmiEditorService service(project_service, HmiDefaultSettings{});
  25. const HmiEditorResult page_result = service.ensureDefaultPage();
  26. require(page_result.succeeded, "default HMI page creation must succeed");
  27. const std::string page_id = page_result.id;
  28. const HmiEditorResult button = service.addControl(page_id, HmiControlType::Button);
  29. const HmiEditorResult indicator = service.addControl(page_id, HmiControlType::Indicator);
  30. const HmiEditorResult display = service.addControl(
  31. page_id, HmiControlType::NumericDisplay);
  32. const HmiEditorResult input = service.addControl(page_id, HmiControlType::NumericInput);
  33. require(button.succeeded && indicator.succeeded && display.succeeded
  34. && input.succeeded,
  35. "basic HMI controls must be added");
  36. const HmiPage *page = service.findPage(page_id);
  37. require(page != nullptr && page->controls.size() == 4,
  38. "all added controls must be kept in the page model");
  39. require(service.findControl(page_id, button.id)->binding
  40. == RegisterAddress{RegisterArea::M, 0}
  41. && service.findControl(page_id, indicator.id)->binding
  42. == RegisterAddress{RegisterArea::M, 1}
  43. && service.findControl(page_id, display.id)->binding
  44. == RegisterAddress{RegisterArea::D, 0}
  45. && service.findControl(page_id, input.id)->binding
  46. == RegisterAddress{RegisterArea::D, 1},
  47. "new basic HMI controls must receive sequential M/D addresses");
  48. require(service.moveControl(page_id, button.id, {120, 80, 120, 40}).succeeded,
  49. "a valid control move must succeed");
  50. require(!service.moveControl(page_id, button.id, {790, 460, 120, 40}).succeeded,
  51. "a move outside the page must be rejected");
  52. HmiControl updated = *service.findControl(page_id, button.id);
  53. updated.binding = RegisterAddress{RegisterArea::D, 0};
  54. require(!service.updateControl(page_id, button.id, updated).succeeded,
  55. "buttons must reject D address bindings during editing");
  56. updated.binding = RegisterAddress{RegisterArea::M, 7};
  57. updated.text = "启动";
  58. require(service.updateControl(page_id, button.id, updated).succeeded,
  59. "a button M binding and edited text must be accepted");
  60. HmiControl duplicate = *service.findControl(page_id, indicator.id);
  61. duplicate.id = button.id;
  62. require(service.updateControl(page_id, indicator.id, duplicate).error
  63. == HmiEditorError::DuplicateId,
  64. "duplicate control ids must be rejected");
  65. require(service.removeControl(page_id, indicator.id).succeeded,
  66. "deleting a selected control must succeed");
  67. require(service.findControl(page_id, indicator.id) == nullptr,
  68. "deleted controls must not remain in the model");
  69. }
  70. void testBatchControlAlignment()
  71. {
  72. TestProjectStorage storage;
  73. ProjectService project_service(storage, defaultProjectLimitSettings());
  74. HmiEditorService service(project_service, HmiDefaultSettings{});
  75. const std::string page_id = service.ensureDefaultPage().id;
  76. const HmiEditorResult first = service.addControl(page_id, HmiControlType::Label);
  77. const HmiEditorResult second = service.addControl(page_id, HmiControlType::Label);
  78. const HmiEditorResult third = service.addControl(page_id, HmiControlType::Label);
  79. require(first.succeeded && second.succeeded && third.succeeded,
  80. "controls for alignment testing must be created");
  81. const std::vector<std::string> ids{first.id, second.id, third.id};
  82. const std::vector<HmiRect> original_bounds{
  83. {100, 80, 80, 30},
  84. {240, 120, 100, 40},
  85. {380, 160, 60, 20}};
  86. for (std::size_t index = 0U; index < ids.size(); ++index)
  87. {
  88. HmiControl control = *service.findControl(page_id, ids[index]);
  89. control.bounds = original_bounds[index];
  90. require(service.updateControl(page_id, ids[index], control).succeeded,
  91. "alignment fixtures must be positioned successfully");
  92. }
  93. service.clearHistory();
  94. require(service.alignControls(page_id, ids, HmiAlignment::Left).succeeded,
  95. "left alignment must succeed");
  96. require(service.findControl(page_id, first.id)->bounds.x == 100
  97. && service.findControl(page_id, second.id)->bounds.x == 100
  98. && service.findControl(page_id, third.id)->bounds.x == 100,
  99. "left alignment must use the selected group left edge");
  100. require(service.findControl(page_id, second.id)->bounds.y == 120,
  101. "alignment must preserve the non-aligned coordinate");
  102. require(service.undo().succeeded
  103. && service.findControl(page_id, second.id)->bounds
  104. .x == original_bounds[1].x,
  105. "alignment must be restored by one undo step");
  106. require(service.redo().succeeded
  107. && service.findControl(page_id, third.id)->bounds.x == 100,
  108. "alignment redo must restore the whole batch");
  109. const auto resetBounds = [&service, &page_id, &ids, &original_bounds]
  110. {
  111. for (std::size_t index = 0U; index < ids.size(); ++index)
  112. {
  113. HmiControl control = *service.findControl(page_id, ids[index]);
  114. control.bounds = original_bounds[index];
  115. require(service.updateControl(page_id, ids[index], control).succeeded,
  116. "alignment fixtures must be reset successfully");
  117. }
  118. };
  119. const auto assertBounds = [&service, &page_id, &ids, &resetBounds](
  120. HmiAlignment alignment, const std::vector<HmiRect> &expected,
  121. const char *message)
  122. {
  123. resetBounds();
  124. service.clearHistory();
  125. require(service.alignControls(page_id, ids, alignment).succeeded, message);
  126. for (std::size_t index = 0U; index < ids.size(); ++index)
  127. {
  128. const HmiControl *control = service.findControl(page_id, ids[index]);
  129. require(control != nullptr
  130. && control->bounds.x == expected[index].x
  131. && control->bounds.y == expected[index].y,
  132. "alignment must calculate deterministic target coordinates");
  133. }
  134. };
  135. assertBounds(
  136. HmiAlignment::HorizontalCenter,
  137. {{230, 80, 0, 0}, {220, 120, 0, 0}, {240, 160, 0, 0}},
  138. "horizontal center alignment must succeed");
  139. assertBounds(
  140. HmiAlignment::Right,
  141. {{360, 80, 0, 0}, {340, 120, 0, 0}, {380, 160, 0, 0}},
  142. "right alignment must succeed");
  143. assertBounds(
  144. HmiAlignment::Top,
  145. {{100, 80, 0, 0}, {240, 80, 0, 0}, {380, 80, 0, 0}},
  146. "top alignment must succeed");
  147. assertBounds(
  148. HmiAlignment::VerticalCenter,
  149. {{100, 115, 0, 0}, {240, 110, 0, 0}, {380, 120, 0, 0}},
  150. "vertical center alignment must succeed");
  151. assertBounds(
  152. HmiAlignment::Bottom,
  153. {{100, 150, 0, 0}, {240, 140, 0, 0}, {380, 160, 0, 0}},
  154. "bottom alignment must succeed");
  155. service.clearHistory();
  156. const std::vector<HmiRect> before_invalid{
  157. service.findControl(page_id, first.id)->bounds,
  158. service.findControl(page_id, second.id)->bounds,
  159. service.findControl(page_id, third.id)->bounds};
  160. require(!service.alignControls(
  161. page_id, {first.id, "missing-control"}, HmiAlignment::Left)
  162. .succeeded,
  163. "alignment must reject an unknown control before mutation");
  164. require(!service.canUndo()
  165. && service.findControl(page_id, first.id)->bounds.x
  166. == before_invalid[0].x
  167. && service.findControl(page_id, second.id)->bounds.y
  168. == before_invalid[1].y,
  169. "failed alignment must be atomic and leave history unchanged");
  170. require(!service.alignControls(page_id, {first.id}, HmiAlignment::Left).succeeded,
  171. "alignment must require at least two controls");
  172. HmiControl no_op = *service.findControl(page_id, first.id);
  173. no_op.bounds.x = 100;
  174. HmiControl no_op_second = *service.findControl(page_id, second.id);
  175. no_op_second.bounds.x = 100;
  176. HmiControl no_op_third = *service.findControl(page_id, third.id);
  177. no_op_third.bounds.x = 100;
  178. require(service.updateControl(page_id, first.id, no_op).succeeded
  179. && service.updateControl(page_id, second.id, no_op_second).succeeded
  180. && service.updateControl(page_id, third.id, no_op_third).succeeded,
  181. "alignment no-op fixtures must be positioned successfully");
  182. service.clearHistory();
  183. require(service.alignControls(page_id, ids, HmiAlignment::Left).succeeded
  184. && !service.canUndo(),
  185. "a no-op alignment must not create an undo step");
  186. }
  187. void testRuntimeUsesRegisterRepository()
  188. {
  189. // 运行服务只能经由仓库接口读写 M/D,不依赖具体离线实现
  190. VirtualRegisterRepository repository;
  191. HmiRuntimeService runtime_service(repository);
  192. HmiControl button;
  193. button.id = "start";
  194. button.type = HmiControlType::Button;
  195. button.binding = RegisterAddress{RegisterArea::M, 12};
  196. require(button.buttonOperation == HmiButtonOperation::MomentaryOn,
  197. "new buttons must default to momentary ON");
  198. require(runtime_service.operateButton(button, HmiButtonEvent::Pressed).succeeded,
  199. "momentary button press must be accepted by the repository");
  200. require(repository.readBit(*button.binding).value,
  201. "momentary button press must write ON");
  202. require(runtime_service.operateButton(button, HmiButtonEvent::Released).succeeded,
  203. "momentary button release must be accepted by the repository");
  204. require(!repository.readBit(*button.binding).value,
  205. "momentary button release must write OFF");
  206. button.buttonOperation = HmiButtonOperation::SetOn;
  207. require(runtime_service.operateButton(button, HmiButtonEvent::Pressed).succeeded,
  208. "set ON button press must succeed");
  209. require(repository.readBit(*button.binding).value,
  210. "set ON button press must write ON");
  211. require(runtime_service.operateButton(button, HmiButtonEvent::Released).succeeded,
  212. "set ON button release must be ignored successfully");
  213. require(repository.readBit(*button.binding).value,
  214. "set ON button release must keep the bit ON");
  215. button.buttonOperation = HmiButtonOperation::SetOff;
  216. require(runtime_service.operateButton(button, HmiButtonEvent::Pressed).succeeded,
  217. "set OFF button press must succeed");
  218. require(!repository.readBit(*button.binding).value,
  219. "set OFF button press must write OFF");
  220. button.buttonOperation = HmiButtonOperation::Toggle;
  221. require(runtime_service.operateButton(button, HmiButtonEvent::Pressed).succeeded,
  222. "toggle button press must succeed");
  223. require(repository.readBit(*button.binding).value,
  224. "toggle button press must invert the current value");
  225. button.buttonOperation = HmiButtonOperation::MomentaryOn;
  226. button.buttonEnableCondition = HmiButtonBitEnableCondition{
  227. RegisterAddress{RegisterArea::M, 13}, true};
  228. repository.writeBit({RegisterArea::M, 13}, false);
  229. HmiButtonEnabledResult condition = runtime_service.evaluateButtonEnabled(button);
  230. require(condition.succeeded && !condition.enabled
  231. && condition.error == HmiRuntimeError::ConditionNotMet,
  232. "an M button condition must disable the button when it is false");
  233. require(runtime_service.operateButton(button, HmiButtonEvent::Pressed).error
  234. == HmiRuntimeError::ConditionNotMet,
  235. "a false M button condition must reject the write at the service boundary");
  236. repository.writeBit({RegisterArea::M, 13}, true);
  237. condition = runtime_service.evaluateButtonEnabled(button);
  238. require(condition.succeeded && condition.enabled,
  239. "an M button condition must enable the button when it is true");
  240. require(runtime_service.operateButton(button, HmiButtonEvent::Pressed).succeeded
  241. && repository.readBit(*button.binding).value,
  242. "a satisfied M button condition must allow the configured operation");
  243. repository.writeBit({RegisterArea::M, 13}, false);
  244. require(runtime_service.operateButton(button, HmiButtonEvent::Released).succeeded
  245. && !repository.readBit(*button.binding).value,
  246. "a momentary button release must reset safely after its condition changes");
  247. repository.writeBit({RegisterArea::M, 12}, true);
  248. HmiControl d_button = button;
  249. d_button.buttonEnableCondition = HmiButtonWordEnableCondition{
  250. RegisterAddress{RegisterArea::D, 100},
  251. RegisterDataType::Int16,
  252. HmiButtonConditionOperator::GreaterThanOrEqual,
  253. 50.0};
  254. repository.writeWord({RegisterArea::D, 100}, 50);
  255. condition = runtime_service.evaluateButtonEnabled(d_button);
  256. require(condition.succeeded && condition.enabled,
  257. "a satisfied D button condition must enable the button");
  258. repository.writeWord({RegisterArea::D, 100}, 49);
  259. condition = runtime_service.evaluateButtonEnabled(d_button);
  260. require(condition.succeeded && !condition.enabled,
  261. "a D button condition must compare the current repository value");
  262. HmiControl indicator;
  263. indicator.id = "running";
  264. indicator.type = HmiControlType::Indicator;
  265. indicator.binding = RegisterAddress{RegisterArea::M, 12};
  266. const HmiRuntimeReadResult indicator_value = runtime_service.readControl(indicator);
  267. require(indicator_value.succeeded && indicator_value.bit_value,
  268. "indicators must read M values through the repository");
  269. HmiControl numeric_input;
  270. numeric_input.id = "target";
  271. numeric_input.type = HmiControlType::NumericInput;
  272. numeric_input.binding = RegisterAddress{RegisterArea::D, 9};
  273. require(runtime_service.writeNumericInput(numeric_input, -18).succeeded,
  274. "numeric input runtime writes must be accepted by the repository");
  275. HmiControl numeric_display;
  276. numeric_display.id = "actual";
  277. numeric_display.type = HmiControlType::NumericDisplay;
  278. numeric_display.binding = RegisterAddress{RegisterArea::D, 9};
  279. const HmiRuntimeReadResult numeric_value = runtime_service.readControl(numeric_display);
  280. require(numeric_value.succeeded
  281. && std::get<std::int16_t>(numeric_value.numeric_value) == -18,
  282. "numeric display must read D values through the repository");
  283. HmiControl float_input = numeric_input;
  284. float_input.dataType = RegisterDataType::Float32;
  285. float_input.binding = RegisterAddress{RegisterArea::D, 20};
  286. require(runtime_service.writeNumericInput(float_input, -2.5).succeeded,
  287. "Float32 numeric input must write two consecutive D words");
  288. const HmiRuntimeReadResult float_value = runtime_service.readControl(float_input);
  289. require(float_value.succeeded
  290. && std::get<float>(float_value.numeric_value) == -2.5f,
  291. "Float32 numeric control must decode two consecutive D words");
  292. require(!runtime_service.writeNumericInput(
  293. float_input, std::numeric_limits<double>::infinity()).succeeded,
  294. "Float32 numeric input must reject infinity");
  295. HmiControl int32_input = numeric_input;
  296. int32_input.dataType = RegisterDataType::Int32;
  297. int32_input.binding = RegisterAddress{RegisterArea::D, 30};
  298. require(runtime_service.writeNumericInput(int32_input, 305419896).succeeded,
  299. "Int32 numeric input must write two consecutive D words");
  300. const WordsReadResult int32_words = repository.readWords(*int32_input.binding, 2);
  301. const HmiRuntimeReadResult int32_value = runtime_service.readControl(int32_input);
  302. require(int32_words.succeeded
  303. && static_cast<std::uint16_t>(int32_words.values[0]) == 0x5678U
  304. && static_cast<std::uint16_t>(int32_words.values[1]) == 0x1234U
  305. && int32_value.succeeded
  306. && std::get<std::int32_t>(int32_value.numeric_value) == 0x12345678,
  307. "Int32 HMI read/write must use low-word-first Xinje ordering");
  308. HmiControl double_input = numeric_input;
  309. double_input.dataType = RegisterDataType::Float64;
  310. double_input.binding = RegisterAddress{RegisterArea::D, 40};
  311. require(runtime_service.writeNumericInput(double_input, 1.0).succeeded,
  312. "Double numeric input must write four consecutive D words");
  313. const WordsReadResult double_words = repository.readWords(*double_input.binding, 4);
  314. const HmiRuntimeReadResult double_value = runtime_service.readControl(double_input);
  315. require(double_words.succeeded
  316. && static_cast<std::uint16_t>(double_words.values[0]) == 0x0000U
  317. && static_cast<std::uint16_t>(double_words.values[1]) == 0x0000U
  318. && static_cast<std::uint16_t>(double_words.values[2]) == 0x0000U
  319. && static_cast<std::uint16_t>(double_words.values[3]) == 0x3ff0U
  320. && double_value.succeeded
  321. && std::get<double>(double_value.numeric_value) == 1.0,
  322. "Double HMI read/write must use four low-address-first words");
  323. require(!runtime_service.writeNumericInput(
  324. double_input, std::numeric_limits<double>::quiet_NaN()).succeeded,
  325. "Double numeric input must reject NaN");
  326. double_input.binding = RegisterAddress{RegisterArea::D, 3997};
  327. require(runtime_service.writeNumericInput(double_input, 1.0).error
  328. == HmiRuntimeError::InvalidBinding,
  329. "Double numeric input must reject odd or overflowing start addresses");
  330. }
  331. void testStatusTextRuntimeMapping()
  332. {
  333. VirtualRegisterRepository repository;
  334. HmiRuntimeService runtime(repository);
  335. HmiControl status;
  336. status.id = "machine-status";
  337. status.type = HmiControlType::StatusText;
  338. status.text = "Status";
  339. status.binding = RegisterAddress{RegisterArea::M, 5};
  340. status.statusText = HmiStatusBitTextConfig{"Stopped", "Running"};
  341. require(runtime.readStatusText(status).text == "Stopped",
  342. "an OFF M bit must resolve to the configured OFF text");
  343. repository.writeBit(*status.binding, true);
  344. require(runtime.readStatusText(status).text == "Running",
  345. "an ON M bit must resolve to the configured ON text");
  346. require(runtime.writeNumericInput(status, 1).error
  347. == HmiRuntimeError::UnsupportedControl,
  348. "status text must remain read-only");
  349. status.binding = RegisterAddress{RegisterArea::D, 20};
  350. status.dataType = RegisterDataType::Float32;
  351. status.statusText = HmiStatusWordTextConfig{{
  352. {std::nullopt, 30.0, "Low"},
  353. {30.0, 80.0, "Normal"},
  354. {80.0, std::nullopt, "High"}}};
  355. const auto write_float = [&repository, &status](float value)
  356. {
  357. const auto words = Float32Codec::encode(value);
  358. repository.writeWords(*status.binding, {words[0], words[1]});
  359. };
  360. write_float(29.5f);
  361. require(runtime.readStatusText(status).text == "Low",
  362. "values below the first upper bound must resolve to the first text");
  363. write_float(30.0f);
  364. require(runtime.readStatusText(status).text == "Normal",
  365. "a shared boundary must belong to the range starting at that boundary");
  366. write_float(80.0f);
  367. require(runtime.readStatusText(status).text == "High",
  368. "the final boundary must belong to the unlimited upper range");
  369. repository.writeWords(
  370. *status.binding,
  371. {static_cast<std::int16_t>(0), static_cast<std::int16_t>(0x7fc0)});
  372. require(!runtime.readStatusText(status).succeeded,
  373. "NaN register bits must make status text unavailable");
  374. }
  375. void testHistoryAndAtomicBatchDelete()
  376. {
  377. TestProjectStorage storage;
  378. ProjectService project_service(storage, defaultProjectLimitSettings());
  379. HmiEditorService service(project_service, HmiDefaultSettings{});
  380. const std::string page_id = service.ensureDefaultPage().id;
  381. const HmiEditorResult first = service.addControl(page_id, HmiControlType::Label);
  382. const HmiEditorResult second = service.addControl(page_id, HmiControlType::Label);
  383. const HmiEditorResult third = service.addControl(page_id, HmiControlType::Label);
  384. require(first.succeeded && second.succeeded && third.succeeded,
  385. "controls for history testing must be created");
  386. service.clearHistory();
  387. require(!service.removeControls(page_id, {first.id, "missing-control"}).succeeded,
  388. "batch deletion must reject an unknown control before changing the page");
  389. require(service.findPage(page_id)->controls.size() == 3U
  390. && !service.canUndo(),
  391. "failed batch deletion must be atomic and leave history unchanged");
  392. require(service.removeControls(page_id, {first.id, second.id}).succeeded,
  393. "batch deletion of valid controls must succeed");
  394. require(service.findPage(page_id)->controls.size() == 1U
  395. && service.canUndo(),
  396. "valid batch deletion must remove all selected controls in one history step");
  397. require(service.undo().succeeded && service.findPage(page_id)->controls.size() == 3U,
  398. "HMI undo must restore a deleted batch");
  399. require(service.redo().succeeded && service.findPage(page_id)->controls.size() == 1U,
  400. "HMI redo must reapply a deleted batch");
  401. service.clearHistory();
  402. const HmiControl *remaining = service.findControl(page_id, third.id);
  403. require(remaining != nullptr,
  404. "the unselected control must survive a batch deletion");
  405. require(service.moveControl(page_id, third.id, remaining->bounds).succeeded
  406. && !service.canUndo(),
  407. "a no-op control move must not consume an undo step");
  408. service.clearHistory();
  409. HmiControl candidate = *service.findControl(page_id, third.id);
  410. for (int index = 1; index <= 101; ++index)
  411. {
  412. candidate.bounds.x = index;
  413. require(service.updateControl(page_id, third.id, candidate).succeeded,
  414. "repeated valid HMI edits must succeed");
  415. }
  416. int undo_count = 0;
  417. while (service.undo().succeeded)
  418. {
  419. ++undo_count;
  420. }
  421. require(undo_count == static_cast<int>(EditorHistory<int>::kMaximumEntries),
  422. "HMI history must retain exactly the configured most recent steps");
  423. }
  424. void testBatchPasteControls()
  425. {
  426. TestProjectStorage storage;
  427. ProjectService project_service(storage, defaultProjectLimitSettings());
  428. HmiEditorService service(project_service, HmiDefaultSettings{});
  429. const std::string page_id = service.ensureDefaultPage().id;
  430. const HmiEditorResult first = service.addControl(
  431. page_id, HmiControlType::NumericDisplay);
  432. const HmiEditorResult second = service.addControl(page_id, HmiControlType::Label);
  433. require(first.succeeded && second.succeeded,
  434. "controls for paste testing must be created");
  435. HmiControl first_copy = *service.findControl(page_id, first.id);
  436. HmiControl second_copy = *service.findControl(page_id, second.id);
  437. first_copy.bounds = {100, 80, 80, 32};
  438. first_copy.type = HmiControlType::NumericDisplay;
  439. first_copy.text = "复制 Double";
  440. first_copy.binding = RegisterAddress{RegisterArea::D, 100};
  441. first_copy.dataType = RegisterDataType::Float64;
  442. first_copy.properties[HmiAppearanceProperty::kTextColor] = "#E53935";
  443. second_copy.bounds = {220, 80, 80, 32};
  444. require(service.updateControl(page_id, first.id, first_copy).succeeded
  445. && service.updateControl(page_id, second.id, second_copy).succeeded,
  446. "source controls must be movable before paste");
  447. service.clearHistory();
  448. const HmiEditorResult pasted = service.pasteControls(
  449. page_id, {first_copy, second_copy});
  450. require(pasted.succeeded
  451. && service.findPage(page_id)->controls.size() == 4U
  452. && pasted.id != first.id && pasted.id != second.id,
  453. "batch paste must create controls with fresh ids");
  454. const HmiControl *pasted_control = service.findControl(page_id, pasted.id);
  455. require(pasted_control != nullptr
  456. && pasted_control->bounds.x == first_copy.bounds.x + 20
  457. && pasted_control->bounds.y == first_copy.bounds.y + 20
  458. && pasted_control->text == first_copy.text
  459. && pasted_control->binding == first_copy.binding
  460. && pasted_control->dataType == RegisterDataType::Float64
  461. && pasted_control->properties == first_copy.properties,
  462. "pasted controls must preserve data type, binding and appearance with an offset");
  463. require(service.undo().succeeded
  464. && service.findPage(page_id)->controls.size() == 2U,
  465. "batch control paste must be one undoable operation");
  466. }
  467. void testRuntimeValidationAndPasteBoundaries()
  468. {
  469. TestProjectStorage storage;
  470. ProjectService project_service(storage, defaultProjectLimitSettings());
  471. HmiEditorService editor(project_service, HmiDefaultSettings{});
  472. const std::string page_id = editor.ensureDefaultPage().id;
  473. HmiControl label;
  474. label.id = "label-source";
  475. label.type = HmiControlType::Label;
  476. label.bounds = {760, 360, 32, 32};
  477. label.text = "边界";
  478. require(editor.pasteControls(page_id, {label}, 20, 20).succeeded,
  479. "a paste near the page edge must be clamped into the page");
  480. const HmiPage *page = editor.findPage(page_id);
  481. require(page != nullptr && page->controls.size() == 1U,
  482. "edge paste must add exactly one control");
  483. const HmiControl &edge_copy = page->controls.front();
  484. require(edge_copy.bounds.x + edge_copy.bounds.width <= page->width
  485. && edge_copy.bounds.y + edge_copy.bounds.height <= page->height
  486. && edge_copy.bounds.x >= 0 && edge_copy.bounds.y >= 0,
  487. "edge paste must keep the copied control inside page bounds");
  488. require(!editor.pasteControls(page_id, {}).succeeded,
  489. "an empty HMI clipboard must be rejected");
  490. HmiControl oversized = label;
  491. oversized.bounds = {0, 0, page->width + 1, 32};
  492. require(!editor.pasteControls(page_id, {oversized}).succeeded
  493. && editor.findPage(page_id)->controls.size() == 1U,
  494. "a copied control wider than the target page must fail atomically");
  495. HmiControl unsupported = label;
  496. unsupported.type = HmiControlType::Count;
  497. require(!editor.pasteControls(page_id, {unsupported}).succeeded
  498. && editor.findPage(page_id)->controls.size() == 1U,
  499. "an unsupported copied HMI type must fail atomically");
  500. std::vector<HmiControl> too_many(513U, label);
  501. require(!editor.pasteControls(page_id, too_many).succeeded
  502. && editor.findPage(page_id)->controls.size() == 1U,
  503. "a paste batch above the page limit must be rejected before mutation");
  504. VirtualRegisterRepository repository;
  505. HmiRuntimeService runtime(repository);
  506. HmiControl unbound_indicator;
  507. unbound_indicator.type = HmiControlType::Indicator;
  508. require(runtime.readControl(unbound_indicator).error
  509. == HmiRuntimeError::MissingBinding,
  510. "an unbound runtime indicator must report MissingBinding");
  511. HmiControl wrong_area_indicator = unbound_indicator;
  512. wrong_area_indicator.binding = RegisterAddress{RegisterArea::D, 0};
  513. require(runtime.readControl(wrong_area_indicator).error
  514. == HmiRuntimeError::InvalidBinding,
  515. "an indicator bound to D must report InvalidBinding");
  516. HmiControl invalid_indicator = unbound_indicator;
  517. invalid_indicator.binding = RegisterAddress{RegisterArea::M, 4001};
  518. require(runtime.readControl(invalid_indicator).error
  519. == HmiRuntimeError::InvalidBinding,
  520. "an indicator with an out-of-range address must report InvalidBinding");
  521. require(runtime.readControl(label).error
  522. == HmiRuntimeError::UnsupportedControl,
  523. "a static label must not be treated as a register runtime control");
  524. HmiControl wrong_button = unbound_indicator;
  525. wrong_button.type = HmiControlType::Button;
  526. wrong_button.binding = RegisterAddress{RegisterArea::D, 0};
  527. require(runtime.operateButton(wrong_button, HmiButtonEvent::Pressed).error
  528. == HmiRuntimeError::InvalidBinding,
  529. "a button bound to D must reject runtime writes");
  530. HmiControl wrong_numeric = unbound_indicator;
  531. wrong_numeric.type = HmiControlType::NumericInput;
  532. wrong_numeric.binding = RegisterAddress{RegisterArea::M, 0};
  533. require(runtime.writeNumericInput(wrong_numeric, 1).error
  534. == HmiRuntimeError::InvalidBinding,
  535. "a numeric input bound to M must reject runtime writes");
  536. }
  537. void testAppearanceEditing()
  538. {
  539. TestProjectStorage storage;
  540. ProjectService project_service(storage, defaultProjectLimitSettings());
  541. HmiEditorService service(project_service, HmiDefaultSettings{});
  542. const std::string page_id = service.ensureDefaultPage().id;
  543. const HmiEditorResult label = service.addControl(page_id, HmiControlType::Label);
  544. require(label.succeeded, "a label must be available for appearance editing");
  545. HmiControl appearance = *service.findControl(page_id, label.id);
  546. appearance.properties[HmiAppearanceProperty::kTextColor] = "#E53935";
  547. appearance.properties[HmiAppearanceProperty::kFontSize] = "18";
  548. appearance.properties[HmiAppearanceProperty::kFontBold] = "true";
  549. appearance.properties[HmiAppearanceProperty::kFontItalic] = "false";
  550. require(service.updateControl(page_id, label.id, appearance).succeeded,
  551. "valid appearance properties must be applied atomically");
  552. const HmiControl *updated = service.findControl(page_id, label.id);
  553. require(updated != nullptr
  554. && updated->properties.at(HmiAppearanceProperty::kTextColor) == "#E53935"
  555. && updated->properties.at(HmiAppearanceProperty::kFontSize) == "18"
  556. && updated->properties.at(HmiAppearanceProperty::kFontBold) == "true",
  557. "appearance properties must be stored on the HMI control");
  558. HmiControl invalid = *updated;
  559. invalid.properties[HmiAppearanceProperty::kTextColor] = "invalid";
  560. require(!service.updateControl(page_id, label.id, invalid).succeeded,
  561. "invalid appearance properties must be rejected without a partial update");
  562. require(service.findControl(page_id, label.id)->properties.at(
  563. HmiAppearanceProperty::kTextColor) == "#E53935",
  564. "failed appearance updates must leave the old color intact");
  565. require(service.undo().succeeded,
  566. "appearance updates must participate in HMI undo history");
  567. require(service.findControl(page_id, label.id)->properties.empty(),
  568. "undo must remove the applied appearance properties");
  569. require(service.redo().succeeded
  570. && service.findControl(page_id, label.id)->properties.at(
  571. HmiAppearanceProperty::kFontSize) == "18",
  572. "redo must restore the applied appearance properties");
  573. }
  574. void testPageLifecycleAndNavigation()
  575. {
  576. TestProjectStorage storage;
  577. ProjectService project_service(storage, defaultProjectLimitSettings());
  578. HmiEditorService service(project_service, HmiDefaultSettings{});
  579. const std::string main_id = service.ensureDefaultPage().id;
  580. require(project_service.project().initialHmiPageId == main_id,
  581. "the first page must become the initial HMI page");
  582. const HmiEditorResult settings = service.addPage("Settings");
  583. const HmiEditorResult maintenance = service.addPage("Maintenance");
  584. require(settings.succeeded && maintenance.succeeded,
  585. "multiple HMI pages must be creatable");
  586. require(service.renamePage(settings.id, "Parameters").succeeded,
  587. "an HMI page must be renamable by stable id");
  588. require(service.renamePage(maintenance.id, "Parameters").error
  589. == HmiEditorError::DuplicateName,
  590. "HMI page names must remain unique");
  591. require(service.movePage(maintenance.id, -1).succeeded
  592. && project_service.project().hmiPages.at(1).id == maintenance.id,
  593. "HMI page order must be editable independently from page ids");
  594. const HmiEditorResult label = service.addControl(main_id, HmiControlType::Label);
  595. require(label.succeeded
  596. && service.findControl(main_id, label.id)->isConfigured(),
  597. "the editor service must expose a configured static Label control");
  598. const HmiEditorResult alarm_list = service.addControl(
  599. main_id, HmiControlType::AlarmList);
  600. require(alarm_list.succeeded
  601. && service.findControl(main_id, alarm_list.id)->isConfigured()
  602. && !service.findControl(main_id, alarm_list.id)->binding.has_value(),
  603. "AlarmList must be configured without a register binding");
  604. HmiControl bound_alarm_list = *service.findControl(main_id, alarm_list.id);
  605. bound_alarm_list.binding = RegisterAddress{RegisterArea::M, 20};
  606. require(!service.updateControl(
  607. main_id, alarm_list.id, bound_alarm_list).succeeded,
  608. "AlarmList must reject direct register bindings");
  609. const HmiEditorResult jump = service.addControl(main_id, HmiControlType::PageJump);
  610. require(jump.succeeded, "the editor service must expose PageJump creation");
  611. HmiControl jump_control = *service.findControl(main_id, jump.id);
  612. jump_control.pageJump = HmiPageJumpConfig{maintenance.id};
  613. require(service.updateControl(main_id, jump.id, jump_control).succeeded,
  614. "PageJump target updates must resolve stable page ids");
  615. require(service.removePage(maintenance.id).error == HmiEditorError::PageReferenced,
  616. "a page referenced by PageJump must not be deletable");
  617. require(service.setInitialPage(settings.id).succeeded,
  618. "the user must be able to select a different initial page");
  619. require(service.removePage(settings.id).error
  620. == HmiEditorError::InitialPageCannotBeRemoved,
  621. "the initial page must not be deleted implicitly");
  622. HmiNavigationService navigation(project_service);
  623. const HmiNavigationResult start = navigation.start();
  624. require(start.succeeded && start.pageId == settings.id,
  625. "runtime navigation must start from the persisted initial page");
  626. require(navigation.navigateTo(maintenance.id).succeeded
  627. && navigation.currentPageId() == maintenance.id,
  628. "runtime navigation must switch to an existing page");
  629. require(navigation.navigateTo("missing").error
  630. == HmiNavigationError::PageNotFound,
  631. "runtime navigation must reject an unknown page id");
  632. navigation.stop();
  633. require(navigation.currentPageId().empty(),
  634. "stopping runtime navigation must clear session state");
  635. }
  636. void testPageResizeIsAtomicAndUndoable()
  637. {
  638. TestProjectStorage storage;
  639. ProjectService project_service(storage, defaultProjectLimitSettings());
  640. HmiEditorService service(project_service, HmiDefaultSettings{});
  641. const std::string page_id = service.ensureDefaultPage().id;
  642. const HmiEditorResult label = service.addControl(page_id, HmiControlType::Label);
  643. require(label.succeeded, "page resize fixture must add a control");
  644. HmiControl control = *service.findControl(page_id, label.id);
  645. control.bounds = {700, 340, 80, 32};
  646. require(service.updateControl(page_id, label.id, control).succeeded,
  647. "page resize fixture must place the control near the page edge");
  648. require(service.resizePage(page_id, 1024, 600).succeeded,
  649. "an HMI page must be resizable to a larger valid rectangle");
  650. require(service.findPage(page_id)->width == 1024
  651. && service.findPage(page_id)->height == 600,
  652. "page resize must update both dimensions");
  653. require(service.resizePage(page_id, 700, 300).error
  654. == HmiEditorError::InvalidPage,
  655. "page resize must reject dimensions that clip an existing control");
  656. require(service.findPage(page_id)->width == 1024
  657. && service.findPage(page_id)->height == 600,
  658. "a rejected page resize must leave the original dimensions intact");
  659. require(service.undo().succeeded
  660. && service.findPage(page_id)->width == 800
  661. && service.findPage(page_id)->height == 400,
  662. "page resize must participate in HMI undo history");
  663. require(service.redo().succeeded
  664. && service.findPage(page_id)->width == 1024
  665. && service.findPage(page_id)->height == 600,
  666. "page resize redo must restore the new dimensions");
  667. require(service.resizePage(page_id, 319, 600).error
  668. == HmiEditorError::InvalidPage,
  669. "page width below the business minimum must be rejected");
  670. }
  671. void testConfiguredPageDefaultsAndLimits()
  672. {
  673. TestProjectStorage storage;
  674. ProjectLimitSettings limits;
  675. limits.maximumHmiPages = 1U;
  676. limits.maximumHmiControlsPerPage = 1U;
  677. HmiDefaultSettings defaults;
  678. defaults.pageWidth = 1024;
  679. defaults.pageHeight = 600;
  680. ProjectService project_service(storage, limits);
  681. HmiEditorService service(project_service, defaults);
  682. const HmiEditorResult page = service.ensureDefaultPage();
  683. require(page.succeeded
  684. && service.findPage(page.id)->width == 1024
  685. && service.findPage(page.id)->height == 600,
  686. "configured HMI dimensions must initialize the default page");
  687. require(!service.addPage("Second page").succeeded,
  688. "the HMI editor must use the configured page limit");
  689. require(service.addControl(page.id, HmiControlType::Label).succeeded
  690. && !service.addControl(page.id, HmiControlType::Label).succeeded,
  691. "the HMI editor must use the configured per-page control limit");
  692. }
  693. } // namespace
  694. int main()
  695. {
  696. try
  697. {
  698. // 编辑和运行场景分别验证服务层两条独立职责
  699. testControlEditing();
  700. testBatchControlAlignment();
  701. testHistoryAndAtomicBatchDelete();
  702. testBatchPasteControls();
  703. testRuntimeValidationAndPasteBoundaries();
  704. testAppearanceEditing();
  705. testRuntimeUsesRegisterRepository();
  706. testStatusTextRuntimeMapping();
  707. testPageLifecycleAndNavigation();
  708. testPageResizeIsAtomicAndUndoable();
  709. testConfiguredPageDefaultsAndLimits();
  710. }
  711. catch (const std::exception &error)
  712. {
  713. std::cerr << "HMI editor service tests failed: " << error.what() << '\n';
  714. return 1;
  715. }
  716. std::cout << "HMI editor service tests passed\n";
  717. return 0;
  718. }