TestNavigator.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "TestNavigator.h"
  29. #include "CaptureScreen.h"
  30. #include "TestConfig.h"
  31. #include "TestSuite.h"
  32. #include "TestViewer.h"
  33. #include <RmlUi/Core/Context.h>
  34. #include <RmlUi/Core/Element.h>
  35. #include <RmlUi/Core/ElementDocument.h>
  36. #include <RmlUi/Core/Math.h>
  37. #include <RmlUi/Core/SystemInterface.h>
  38. #include <Shell.h>
  39. #include <cstdio>
  40. // When capturing frames it seems we need to wait at least an extra frame for the newly submitted
  41. // render to be read back out. If we don't wait, we end up saving a screenshot of the previous test.
  42. constexpr int iteration_wait_frame_count = 2;
  43. TestNavigator::TestNavigator(Rml::RenderInterface* render_interface, Rml::Context* context, TestViewer* viewer, TestSuiteList _test_suites,
  44. int start_suite, int start_case) : render_interface(render_interface), context(context), viewer(viewer), test_suites(std::move(_test_suites))
  45. {
  46. RMLUI_ASSERT(context);
  47. RMLUI_ASSERTMSG(!test_suites.empty(), "At least one test suite is required.");
  48. context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, this, true);
  49. context->GetRootElement()->AddEventListener(Rml::EventId::Keyup, this, true);
  50. context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, this);
  51. context->GetRootElement()->AddEventListener(Rml::EventId::Textinput, this);
  52. context->GetRootElement()->AddEventListener(Rml::EventId::Change, this);
  53. suite_index = Rml::Math::Clamp(start_suite, 0, (int)test_suites.size() - 1);
  54. if (start_case > 0)
  55. CurrentSuite().SetIndex(start_case);
  56. LoadActiveTest();
  57. }
  58. TestNavigator::~TestNavigator()
  59. {
  60. context->GetRootElement()->RemoveEventListener(Rml::EventId::Keydown, this, true);
  61. context->GetRootElement()->RemoveEventListener(Rml::EventId::Keyup, this, true);
  62. context->GetRootElement()->RemoveEventListener(Rml::EventId::Keydown, this);
  63. context->GetRootElement()->RemoveEventListener(Rml::EventId::Textinput, this);
  64. context->GetRootElement()->RemoveEventListener(Rml::EventId::Change, this);
  65. ReleaseTextureGeometry(render_interface, reference_geometry);
  66. ReleaseTextureGeometry(render_interface, reference_highlight_geometry);
  67. }
  68. void TestNavigator::Update()
  69. {
  70. if (iteration_state != IterationState::None)
  71. {
  72. RMLUI_ASSERT(iteration_index >= 0);
  73. // Capture test document screenshots iteratively every nth frame.
  74. if (iteration_wait_frames > 0)
  75. {
  76. iteration_wait_frames -= 1;
  77. }
  78. else
  79. {
  80. RMLUI_ASSERT(iteration_index < CurrentSuite().GetNumTests());
  81. iteration_wait_frames = iteration_wait_frame_count;
  82. if (iteration_state == IterationState::Capture)
  83. {
  84. if (!CaptureCurrentView())
  85. {
  86. StopTestSuiteIteration();
  87. return;
  88. }
  89. }
  90. else if (iteration_state == IterationState::Comparison)
  91. {
  92. RMLUI_ASSERT((int)comparison_results.size() == CurrentSuite().GetNumTests());
  93. int test_index = CurrentSuite().GetIndex();
  94. comparison_results[test_index] = CompareCurrentView();
  95. }
  96. iteration_index += 1;
  97. if (CurrentSuite().Next())
  98. LoadActiveTest();
  99. else
  100. StopTestSuiteIteration();
  101. }
  102. }
  103. }
  104. void TestNavigator::Render()
  105. {
  106. if (reference_state != ReferenceState::None && source_state == SourceType::None && !viewer->IsHelpVisible())
  107. {
  108. const TextureGeometry& geometry =
  109. (reference_state == ReferenceState::ShowReferenceHighlight ? reference_highlight_geometry : reference_geometry);
  110. if (const Rml::CompiledGeometryHandle handle = render_interface->CompileGeometry(geometry.mesh.vertices, geometry.mesh.indices))
  111. {
  112. render_interface->RenderGeometry(handle, Rml::Vector2f(0, 0), geometry.texture_handle);
  113. render_interface->ReleaseGeometry(handle);
  114. }
  115. }
  116. }
  117. void TestNavigator::ProcessEvent(Rml::Event& event)
  118. {
  119. // Keydown events in capture phase to override text input
  120. if (event == Rml::EventId::Keydown && event.GetPhase() == Rml::EventPhase::Capture)
  121. {
  122. const auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  123. const bool key_ctrl = event.GetParameter<bool>("ctrl_key", false);
  124. const bool key_shift = event.GetParameter<bool>("shift_key", false);
  125. Rml::Element* element_filter_input = event.GetCurrentElement()->GetElementById("filterinput");
  126. RMLUI_ASSERT(element_filter_input);
  127. if (key_identifier == Rml::Input::KI_F5)
  128. {
  129. if (key_ctrl && key_shift)
  130. StartTestSuiteIteration(IterationState::Comparison);
  131. else
  132. {
  133. ComparisonResult result = CompareCurrentView();
  134. const Rml::String compare_path = GetCompareInputDirectory() + '/' + GetImageFilenameFromCurrentTest();
  135. if (result.success)
  136. {
  137. if (result.is_equal)
  138. {
  139. Rml::Log::Message(Rml::Log::LT_INFO, "%s compares EQUAL to the reference image %s.", CurrentSuite().GetFilename().c_str(),
  140. compare_path.c_str());
  141. }
  142. else
  143. {
  144. Rml::Log::Message(Rml::Log::LT_INFO, "%s compares NOT EQUAL to the reference image %s.\nSee diff image written to %s.",
  145. CurrentSuite().GetFilename().c_str(), compare_path.c_str(), GetCaptureOutputDirectory().c_str());
  146. }
  147. if (!result.error_msg.empty())
  148. Rml::Log::Message(Rml::Log::LT_ERROR, "%s", result.error_msg.c_str());
  149. }
  150. else
  151. {
  152. Rml::Log::Message(Rml::Log::LT_ERROR, "Comparison of %s failed.\n%s", CurrentSuite().GetFilename().c_str(),
  153. result.error_msg.c_str());
  154. }
  155. }
  156. }
  157. else if (key_identifier == Rml::Input::KI_F7)
  158. {
  159. if (key_ctrl && key_shift)
  160. {
  161. StartTestSuiteIteration(IterationState::Capture);
  162. }
  163. else
  164. {
  165. const Rml::String filepath = GetCaptureOutputDirectory() + '/' + GetImageFilenameFromCurrentTest();
  166. if (CaptureCurrentView())
  167. Rml::Log::Message(Rml::Log::LT_INFO, "Succesfully captured and saved screenshot to %s", filepath.c_str());
  168. else
  169. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not capture screenshot to %s", filepath.c_str());
  170. }
  171. }
  172. else if (key_identifier == Rml::Input::KI_F1)
  173. {
  174. ShowReference(ReferenceState::None);
  175. viewer->ShowHelp(!viewer->IsHelpVisible());
  176. }
  177. else if (key_identifier == Rml::Input::KI_F && key_ctrl)
  178. {
  179. element_filter_input->Focus();
  180. context->ProcessKeyDown(Rml::Input::KI_A, Rml::Input::KeyModifier::KM_CTRL);
  181. context->ProcessKeyUp(Rml::Input::KI_A, Rml::Input::KeyModifier::KM_CTRL);
  182. }
  183. else if (key_identifier == Rml::Input::KI_R && key_ctrl)
  184. {
  185. LoadActiveTest(true);
  186. }
  187. else if (key_identifier == Rml::Input::KI_S && key_ctrl)
  188. {
  189. if (source_state == SourceType::None)
  190. {
  191. source_state = (key_shift ? SourceType::Reference : SourceType::Test);
  192. }
  193. else
  194. {
  195. if (key_shift)
  196. source_state = (source_state == SourceType::Reference ? SourceType::Test : SourceType::Reference);
  197. else
  198. source_state = SourceType::None;
  199. }
  200. viewer->ShowSource(source_state);
  201. ShowReference(ReferenceState::None);
  202. }
  203. else if (key_identifier == Rml::Input::KI_Q && key_ctrl)
  204. {
  205. if (reference_state != ReferenceState::None)
  206. ShowReference(ReferenceState::None);
  207. else
  208. ShowReference(key_shift ? ReferenceState::ShowReferenceHighlight : ReferenceState::ShowReference);
  209. }
  210. else if (key_identifier == Rml::Input::KI_LSHIFT || key_identifier == Rml::Input::KI_RSHIFT)
  211. {
  212. if (reference_state == ReferenceState::ShowReference)
  213. ShowReference(ReferenceState::ShowReferenceHighlight);
  214. }
  215. else if (key_identifier == Rml::Input::KI_ESCAPE)
  216. {
  217. if (iteration_state != IterationState::None)
  218. {
  219. StopTestSuiteIteration();
  220. }
  221. else if (viewer->IsHelpVisible())
  222. {
  223. viewer->ShowHelp(false);
  224. }
  225. else if (source_state != SourceType::None)
  226. {
  227. source_state = SourceType::None;
  228. viewer->ShowSource(source_state);
  229. }
  230. else if (reference_state != ReferenceState::None)
  231. {
  232. ShowReference(ReferenceState::None);
  233. }
  234. else if (element_filter_input->IsPseudoClassSet("focus"))
  235. {
  236. element_filter_input->Blur();
  237. }
  238. else if (viewer->IsNavigationLocked())
  239. {
  240. element_filter_input->GetOwnerDocument()->Focus();
  241. }
  242. else if (goto_index >= 0)
  243. {
  244. CancelGoTo();
  245. UpdateGoToText();
  246. }
  247. }
  248. else if (key_identifier == Rml::Input::KI_RETURN || key_identifier == Rml::Input::KI_NUMPADENTER)
  249. {
  250. element_filter_input->Blur();
  251. }
  252. else if (key_identifier == Rml::Input::KI_G && key_ctrl)
  253. {
  254. if (goto_index < 0)
  255. {
  256. element_filter_input->Blur();
  257. StartGoTo();
  258. UpdateGoToText();
  259. }
  260. }
  261. }
  262. if (event == Rml::EventId::Keyup && event.GetPhase() == Rml::EventPhase::Capture)
  263. {
  264. const auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  265. if (key_identifier == Rml::Input::KI_LSHIFT || key_identifier == Rml::Input::KI_RSHIFT)
  266. {
  267. if (reference_state == ReferenceState::ShowReferenceHighlight)
  268. ShowReference(ReferenceState::ShowReference);
  269. }
  270. }
  271. // Keydown events in target/bubble phase ignored when focusing on input.
  272. if (event == Rml::EventId::Keydown && event.GetPhase() != Rml::EventPhase::Capture && !viewer->IsNavigationLocked())
  273. {
  274. const auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  275. if (key_identifier == Rml::Input::KI_LEFT)
  276. {
  277. if (CurrentSuite().Previous())
  278. {
  279. LoadActiveTest();
  280. }
  281. }
  282. else if (key_identifier == Rml::Input::KI_RIGHT)
  283. {
  284. if (CurrentSuite().Next())
  285. {
  286. LoadActiveTest();
  287. }
  288. }
  289. else if (key_identifier == Rml::Input::KI_UP)
  290. {
  291. const Rml::String& filter = CurrentSuite().GetFilter();
  292. int new_index = std::max(0, suite_index - 1);
  293. if (new_index != suite_index)
  294. {
  295. suite_index = new_index;
  296. CurrentSuite().SetFilter(filter);
  297. LoadActiveTest();
  298. }
  299. }
  300. else if (key_identifier == Rml::Input::KI_DOWN)
  301. {
  302. const Rml::String& filter = CurrentSuite().GetFilter();
  303. int new_index = std::min((int)test_suites.size() - 1, suite_index + 1);
  304. if (new_index != suite_index)
  305. {
  306. suite_index = new_index;
  307. CurrentSuite().SetFilter(filter);
  308. LoadActiveTest();
  309. }
  310. }
  311. else if (key_identifier == Rml::Input::KI_HOME)
  312. {
  313. CurrentSuite().SetIndex(0, TestSuite::Direction::Forward);
  314. LoadActiveTest();
  315. }
  316. else if (key_identifier == Rml::Input::KI_END)
  317. {
  318. CurrentSuite().SetIndex(CurrentSuite().GetNumTests() - 1, TestSuite::Direction::Backward);
  319. LoadActiveTest();
  320. }
  321. else if (goto_index >= 0 && key_identifier == Rml::Input::KI_BACK)
  322. {
  323. if (goto_index <= 0)
  324. CancelGoTo();
  325. else
  326. goto_index = goto_index / 10;
  327. UpdateGoToText();
  328. }
  329. }
  330. if (event == Rml::EventId::Textinput && goto_index >= 0)
  331. {
  332. const Rml::String text = event.GetParameter<Rml::String>("text", "");
  333. for (const char c : text)
  334. {
  335. if (c >= '0' && c <= '9')
  336. {
  337. goto_index = goto_index * 10 + int(c - '0');
  338. UpdateGoToText();
  339. }
  340. else if (goto_index >= 0 && c == '\n')
  341. {
  342. bool out_of_bounds = false;
  343. if (goto_index > 0)
  344. {
  345. if (CurrentSuite().SetIndex(goto_index - 1))
  346. LoadActiveTest();
  347. else
  348. out_of_bounds = true;
  349. }
  350. CancelGoTo();
  351. UpdateGoToText(out_of_bounds);
  352. }
  353. }
  354. }
  355. if (event == Rml::EventId::Change)
  356. {
  357. Rml::Element* element = event.GetTargetElement();
  358. if (element->GetId() == "filterinput")
  359. {
  360. CurrentSuite().SetFilter(event.GetParameter<Rml::String>("value", ""));
  361. LoadActiveTest();
  362. }
  363. }
  364. }
  365. void TestNavigator::LoadActiveTest(bool keep_scroll_position)
  366. {
  367. const TestSuite& suite = CurrentSuite();
  368. viewer->LoadTest(suite.GetDirectory(), suite.GetFilename(), suite.GetIndex(), suite.GetNumTests(), suite.GetFilterIndex(),
  369. suite.GetNumFilteredTests(), suite_index, (int)test_suites.size(), keep_scroll_position);
  370. viewer->ShowSource(source_state);
  371. ShowReference(ReferenceState::None);
  372. UpdateGoToText();
  373. }
  374. Rml::String TestNavigator::GetImageFilenameFromCurrentTest()
  375. {
  376. const Rml::String& filename = CurrentSuite().GetFilename();
  377. return filename.substr(0, filename.rfind('.')) + ".png";
  378. }
  379. ComparisonResult TestNavigator::CompareCurrentView()
  380. {
  381. const Rml::String filename = GetImageFilenameFromCurrentTest();
  382. ComparisonResult result = CompareScreenToPreviousCapture(render_interface, filename, nullptr, nullptr);
  383. return result;
  384. }
  385. bool TestNavigator::CaptureCurrentView()
  386. {
  387. const Rml::String filename = GetImageFilenameFromCurrentTest();
  388. bool result = CaptureScreenshot(filename, 1060);
  389. return result;
  390. }
  391. void TestNavigator::StartTestSuiteIteration(IterationState new_iteration_state)
  392. {
  393. if (iteration_state != IterationState::None || new_iteration_state == IterationState::None)
  394. return;
  395. source_state = SourceType::None;
  396. TestSuite& suite = CurrentSuite();
  397. if (new_iteration_state == IterationState::Comparison)
  398. {
  399. comparison_results.clear();
  400. comparison_results.resize(suite.GetNumTests());
  401. }
  402. iteration_initial_index = suite.GetIndex();
  403. iteration_wait_frames = iteration_wait_frame_count;
  404. iteration_state = new_iteration_state;
  405. iteration_index = 0;
  406. suite.SetIndex(iteration_index, TestSuite::Direction::Forward);
  407. LoadActiveTest();
  408. }
  409. static bool SaveFile(const Rml::String& file_path, const Rml::String& contents)
  410. {
  411. std::FILE* file = std::fopen(file_path.c_str(), "wt");
  412. if (!file)
  413. return false;
  414. std::fputs(contents.c_str(), file);
  415. std::fclose(file);
  416. return true;
  417. }
  418. void TestNavigator::StopTestSuiteIteration()
  419. {
  420. if (iteration_state == IterationState::None)
  421. return;
  422. const Rml::String output_directory = GetCaptureOutputDirectory();
  423. TestSuite& suite = CurrentSuite();
  424. const int num_tests = suite.GetNumTests();
  425. const int num_filtered_tests = suite.GetNumFilteredTests();
  426. if (iteration_state == IterationState::Capture)
  427. {
  428. if (iteration_index == num_tests)
  429. {
  430. Rml::Log::Message(Rml::Log::LT_INFO, "Successfully captured %d document screenshots to directory: %s", iteration_index,
  431. output_directory.c_str());
  432. }
  433. else if (iteration_index == num_filtered_tests)
  434. {
  435. Rml::Log::Message(Rml::Log::LT_INFO, "Successfully captured %d document screenshots (filtered out of %d total tests) to directory: %s",
  436. iteration_index, num_tests, output_directory.c_str());
  437. }
  438. else
  439. {
  440. Rml::Log::Message(Rml::Log::LT_ERROR, "Test suite capture aborted after %d of %d test(s). Output directory: %s", iteration_index,
  441. num_tests, output_directory.c_str());
  442. }
  443. }
  444. else if (iteration_state == IterationState::Comparison)
  445. {
  446. RMLUI_ASSERT(num_tests == (int)comparison_results.size());
  447. // Indices
  448. Rml::Vector<int> equal;
  449. Rml::Vector<int> not_equal;
  450. Rml::Vector<int> failed;
  451. Rml::Vector<int> skipped;
  452. for (int i = 0; i < (int)comparison_results.size(); i++)
  453. {
  454. const ComparisonResult& comparison = comparison_results[i];
  455. if (comparison.skipped)
  456. skipped.push_back(i);
  457. else if (!comparison.success)
  458. failed.push_back(i);
  459. else if (comparison.is_equal)
  460. equal.push_back(i);
  461. else
  462. not_equal.push_back(i);
  463. }
  464. Rml::String summary = Rml::CreateString(" Total tests: %d\n Not equal: %d\n Failed: %d\n Skipped: %d\n Equal: %d", num_tests,
  465. (int)not_equal.size(), (int)failed.size(), (int)skipped.size(), (int)equal.size());
  466. if (!suite.GetFilter().empty())
  467. summary += "\n Filter applied: " + suite.GetFilter();
  468. if (iteration_index == num_tests)
  469. {
  470. Rml::Log::Message(Rml::Log::LT_INFO, "Compared all test documents to their screenshot captures.\n%s", summary.c_str());
  471. }
  472. else if (iteration_index == num_filtered_tests)
  473. {
  474. Rml::Log::Message(Rml::Log::LT_INFO, "Compared all filtered test documents to their screenshot captures.\n%s", summary.c_str());
  475. }
  476. else
  477. {
  478. Rml::Log::Message(Rml::Log::LT_ERROR, "Test suite comparison aborted after %d of %d test(s).\n%s", iteration_index, num_tests,
  479. summary.c_str());
  480. }
  481. Rml::String log;
  482. log.reserve(comparison_results.size() * 100);
  483. log += "RmlUi VisualTests comparison log output\n---------------------------------------\n\n" + summary;
  484. log += "\n\nNot Equal:\n";
  485. if (!not_equal.empty())
  486. log += " # similarity scores (%) max pixel difference filename\n\n";
  487. for (int i : not_equal)
  488. {
  489. suite.SetIndex(i);
  490. log += Rml::CreateString("%5d %5.1f%% %4d %s\n", i + 1, comparison_results[i].similarity_score * 100.0,
  491. (int)comparison_results[i].max_absolute_difference_single_pixel, suite.GetFilename().c_str());
  492. if (!comparison_results[i].error_msg.empty())
  493. log += " " + comparison_results[i].error_msg + "\n";
  494. }
  495. log += "\nFailed:\n";
  496. for (int i : failed)
  497. {
  498. suite.SetIndex(i);
  499. log += Rml::CreateString("%5d %s\n", i + 1, suite.GetFilename().c_str());
  500. log += " " + comparison_results[i].error_msg + "\n";
  501. }
  502. log += "\nSkipped:\n";
  503. for (int i : skipped)
  504. {
  505. suite.SetIndex(i);
  506. log += Rml::CreateString("%5d %s\n", i + 1, suite.GetFilename().c_str());
  507. }
  508. log += "\nEqual:\n";
  509. for (int i : equal)
  510. {
  511. suite.SetIndex(i);
  512. log += Rml::CreateString("%5d %s\n", i + 1, suite.GetFilename().c_str());
  513. }
  514. const Rml::String log_path = GetCaptureOutputDirectory() + "/comparison.log";
  515. bool save_result = SaveFile(log_path, log);
  516. if (save_result && failed.empty())
  517. Rml::Log::Message(Rml::Log::LT_INFO, "Comparison log output written to %s", log_path.c_str());
  518. else if (save_result && !failed.empty())
  519. Rml::Log::Message(Rml::Log::LT_ERROR, "Comparison log output written to %s.\nSome comparisons failed, see log output for details.",
  520. log_path.c_str());
  521. else
  522. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed writing comparison log output to file %s", log_path.c_str());
  523. }
  524. iteration_index = -1;
  525. iteration_initial_index = -1;
  526. iteration_wait_frames = -1;
  527. iteration_state = IterationState::None;
  528. suite.SetIndex(iteration_initial_index);
  529. LoadActiveTest();
  530. }
  531. void TestNavigator::StartGoTo()
  532. {
  533. goto_index = 0;
  534. const Rml::Rectanglef area = viewer->GetGoToArea();
  535. Rml::GetSystemInterface()->ActivateKeyboard(area.TopLeft(), area.Height());
  536. }
  537. void TestNavigator::CancelGoTo()
  538. {
  539. Rml::GetSystemInterface()->DeactivateKeyboard();
  540. goto_index = -1;
  541. }
  542. void TestNavigator::UpdateGoToText(bool out_of_bounds)
  543. {
  544. if (out_of_bounds)
  545. viewer->SetGoToText("Go To out of bounds");
  546. else if (goto_index > 0)
  547. viewer->SetGoToText(Rml::CreateString("Go To: %d", goto_index));
  548. else if (goto_index == 0)
  549. viewer->SetGoToText("Go To:");
  550. else if (iteration_state == IterationState::Capture)
  551. viewer->SetGoToText("Capturing all tests");
  552. else if (iteration_state == IterationState::Comparison)
  553. viewer->SetGoToText("Comparing all tests");
  554. else if (reference_state == ReferenceState::ShowReference)
  555. viewer->SetGoToText(Rml::CreateString("Showing reference capture (%.1f%% similar)", reference_comparison.similarity_score * 100.));
  556. else if (reference_state == ReferenceState::ShowReferenceHighlight)
  557. viewer->SetGoToText("Showing reference capture (highlight differences)");
  558. else
  559. viewer->SetGoToText("Press 'F1' for keyboard shortcuts.");
  560. }
  561. void TestNavigator::ShowReference(ReferenceState new_reference_state)
  562. {
  563. if (new_reference_state == reference_state || viewer->IsHelpVisible())
  564. return;
  565. Rml::String error_msg;
  566. if (reference_state == ReferenceState::None)
  567. {
  568. reference_comparison =
  569. CompareScreenToPreviousCapture(render_interface, GetImageFilenameFromCurrentTest(), &reference_geometry, &reference_highlight_geometry);
  570. if (!reference_comparison.success || !reference_geometry.texture_handle || !reference_highlight_geometry.texture_handle)
  571. {
  572. error_msg = reference_comparison.error_msg;
  573. new_reference_state = ReferenceState::None;
  574. }
  575. }
  576. if (new_reference_state == ReferenceState::None)
  577. {
  578. ReleaseTextureGeometry(render_interface, reference_geometry);
  579. ReleaseTextureGeometry(render_interface, reference_highlight_geometry);
  580. reference_comparison = {};
  581. }
  582. reference_state = new_reference_state;
  583. viewer->SetAttention(reference_state != ReferenceState::None);
  584. if (!error_msg.empty())
  585. viewer->SetGoToText(error_msg);
  586. else if (reference_comparison.is_equal)
  587. viewer->SetGoToText("EQUAL to reference capture");
  588. else
  589. UpdateGoToText();
  590. }