TestNavigator.cpp 16 KB

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