TestNavigator.cpp 13 KB

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