TestNavigator.cpp 16 KB

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