TestNavigator.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "Screenshot.h"
  31. #include "TestViewer.h"
  32. #include <RmlUi/Core/Context.h>
  33. #include <RmlUi/Core/Element.h>
  34. #include <Shell.h>
  35. // When capturing frames it seems we need to wait at least an extra frame for the newly submitted
  36. // render to be read back out. If we don't wait, we end up saving a screenshot of the previous test.
  37. constexpr int capture_wait_frame_count = 2;
  38. TestNavigator::TestNavigator(ShellRenderInterfaceOpenGL* shell_renderer, Rml::Context* context, TestViewer* viewer, TestSuiteList test_suites)
  39. : shell_renderer(shell_renderer), context(context), viewer(viewer), test_suites(std::move(test_suites))
  40. {
  41. RMLUI_ASSERT(context);
  42. RMLUI_ASSERTMSG(!this->test_suites.empty(), "At least one test suite is required.");
  43. context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, this);
  44. context->GetRootElement()->AddEventListener(Rml::EventId::Textinput, this);
  45. LoadActiveTest();
  46. }
  47. TestNavigator::~TestNavigator()
  48. {
  49. context->GetRootElement()->RemoveEventListener(Rml::EventId::Keydown, this);
  50. context->GetRootElement()->RemoveEventListener(Rml::EventId::Textinput, this);
  51. }
  52. void TestNavigator::Update()
  53. {
  54. if (capture_index >= 0 && capture_wait_frames > 0)
  55. {
  56. capture_wait_frames -= 1;
  57. }
  58. else if (capture_index >= 0)
  59. {
  60. RMLUI_ASSERT(capture_index < CurrentSuite().GetNumTests());
  61. capture_wait_frames += capture_wait_frame_count;
  62. if (!CaptureCurrentView())
  63. {
  64. StopCaptureFullTestSuite();
  65. return;
  66. }
  67. capture_index += 1;
  68. TestSuite& suite = CurrentSuite();
  69. if (capture_index < suite.GetNumTests())
  70. {
  71. suite.SetIndex(capture_index);
  72. LoadActiveTest();
  73. }
  74. else
  75. {
  76. StopCaptureFullTestSuite();
  77. }
  78. }
  79. }
  80. void TestNavigator::ProcessEvent(Rml::Event& event)
  81. {
  82. if (event == Rml::EventId::Keydown)
  83. {
  84. auto key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter< int >("key_identifier", 0);
  85. bool key_ctrl = event.GetParameter< bool >("ctrl_key", false);
  86. bool key_shift = event.GetParameter< bool >("shift_key", false);
  87. if (key_identifier == Rml::Input::KI_LEFT)
  88. {
  89. if (CurrentSuite().SetIndex(CurrentSuite().GetIndex() - 1))
  90. {
  91. LoadActiveTest();
  92. }
  93. }
  94. else if (key_identifier == Rml::Input::KI_RIGHT)
  95. {
  96. if (CurrentSuite().SetIndex(CurrentSuite().GetIndex() + 1))
  97. {
  98. LoadActiveTest();
  99. }
  100. }
  101. else if (key_identifier == Rml::Input::KI_UP)
  102. {
  103. int new_index = std::max(0, index - 1);
  104. if (new_index != index)
  105. {
  106. index = new_index;
  107. LoadActiveTest();
  108. }
  109. }
  110. else if (key_identifier == Rml::Input::KI_DOWN)
  111. {
  112. int new_index = std::min((int)test_suites.size() - 1, index + 1);
  113. if (new_index != index)
  114. {
  115. index = new_index;
  116. LoadActiveTest();
  117. }
  118. }
  119. else if (key_identifier == Rml::Input::KI_F7)
  120. {
  121. if (key_ctrl && key_shift)
  122. StartCaptureFullTestSuite();
  123. else
  124. CaptureCurrentView();
  125. }
  126. else if (key_identifier == Rml::Input::KI_S)
  127. {
  128. if (source_state == SourceType::None)
  129. {
  130. source_state = (key_shift ? SourceType::Reference : SourceType::Test);
  131. }
  132. else
  133. {
  134. if (key_shift)
  135. source_state = (source_state == SourceType::Reference ? SourceType::Test : SourceType::Reference);
  136. else
  137. source_state = SourceType::None;
  138. }
  139. viewer->ShowSource(source_state);
  140. }
  141. else if (key_identifier == Rml::Input::KI_ESCAPE)
  142. {
  143. if (capture_index >= 0)
  144. {
  145. StopCaptureFullTestSuite();
  146. }
  147. else if (source_state != SourceType::None)
  148. {
  149. source_state = SourceType::None;
  150. viewer->ShowSource(source_state);
  151. }
  152. else
  153. {
  154. Shell::RequestExit();
  155. }
  156. }
  157. else if (key_identifier == Rml::Input::KI_C && key_ctrl)
  158. {
  159. if (key_shift)
  160. Shell::SetClipboardText(CurrentSuite().GetDirectory() + '/' + viewer->GetReferenceFilename());
  161. else
  162. Shell::SetClipboardText(CurrentSuite().GetPath());
  163. }
  164. else if (key_identifier == Rml::Input::KI_HOME)
  165. {
  166. CurrentSuite().SetIndex(0);
  167. LoadActiveTest();
  168. }
  169. else if (key_identifier == Rml::Input::KI_END)
  170. {
  171. CurrentSuite().SetIndex(CurrentSuite().GetNumTests() - 1);
  172. LoadActiveTest();
  173. }
  174. else if (goto_index >= 0 && key_identifier == Rml::Input::KI_BACK)
  175. {
  176. if (goto_index <= 0)
  177. {
  178. goto_index = -1;
  179. viewer->SetGoToText("");
  180. }
  181. else
  182. {
  183. goto_index = goto_index / 10;
  184. viewer->SetGoToText(Rml::CreateString(64, "Go To: %d", goto_index));
  185. }
  186. }
  187. }
  188. if (event == Rml::EventId::Textinput)
  189. {
  190. const Rml::String text = event.GetParameter< Rml::String >("text", "");
  191. for (const char c : text)
  192. {
  193. if (c >= '0' && c <= '9')
  194. {
  195. if (goto_index < 0)
  196. goto_index = 0;
  197. goto_index = goto_index * 10 + int(c - '0');
  198. viewer->SetGoToText(Rml::CreateString(64, "Go To: %d", goto_index));
  199. }
  200. else if (goto_index >= 0 && c == '\n')
  201. {
  202. if (goto_index > 0)
  203. {
  204. if (CurrentSuite().SetIndex(goto_index))
  205. {
  206. LoadActiveTest();
  207. }
  208. else
  209. {
  210. viewer->SetGoToText(Rml::CreateString(64, "Go To out of bounds.", goto_index));
  211. }
  212. }
  213. goto_index = -1;
  214. }
  215. }
  216. }
  217. }
  218. void TestNavigator::LoadActiveTest()
  219. {
  220. const TestSuite& suite = CurrentSuite();
  221. viewer->LoadTest(suite.GetDirectory(), suite.GetFilename(), suite.GetIndex(), suite.GetNumTests(), index, (int)test_suites.size());
  222. viewer->ShowSource(source_state);
  223. }
  224. bool TestNavigator::CaptureCurrentView()
  225. {
  226. Rml::String filename = CurrentSuite().GetFilename();
  227. filename = filename.substr(0, filename.rfind('.')) + ".png";
  228. bool result = CaptureScreenshot(shell_renderer, filename, 1060);
  229. return result;
  230. }
  231. void TestNavigator::StopCaptureFullTestSuite()
  232. {
  233. const Rml::String output_directory = GetOutputDirectory();
  234. TestSuite& suite = CurrentSuite();
  235. const int num_tests = suite.GetNumTests();
  236. if (capture_index == num_tests)
  237. {
  238. Rml::Log::Message(Rml::Log::LT_INFO, "Successfully captured %d document screenshots to directory: %s", capture_index, output_directory.c_str());
  239. }
  240. else
  241. {
  242. Rml::Log::Message(Rml::Log::LT_ERROR, "Test suite capture aborted after %d of %d test(s). Output directory: %s", capture_index, num_tests, output_directory.c_str());
  243. }
  244. suite.SetIndex(capture_initial_index);
  245. LoadActiveTest();
  246. capture_index = -1;
  247. capture_initial_index = -1;
  248. capture_wait_frames = -1;
  249. }
  250. void TestNavigator::StartCaptureFullTestSuite()
  251. {
  252. if(capture_index == -1)
  253. {
  254. source_state = SourceType::None;
  255. TestSuite& suite = CurrentSuite();
  256. capture_initial_index = suite.GetIndex();
  257. capture_wait_frames = capture_wait_frame_count;
  258. capture_index = 0;
  259. suite.SetIndex(capture_index);
  260. LoadActiveTest();
  261. }
  262. }