TestNavigator.cpp 18 KB

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