TestNavigator.cpp 21 KB

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