TestNavigator.cpp 14 KB

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