TestNavigator.cpp 18 KB

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