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

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