TestViewer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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-2023 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 "TestViewer.h"
  29. #include "../Common/TestsShell.h"
  30. #include "TestConfig.h"
  31. #include "XmlNodeHandlers.h"
  32. #include <RmlUi/Core/Context.h>
  33. #include <RmlUi/Core/Core.h>
  34. #include <RmlUi/Core/Element.h>
  35. #include <RmlUi/Core/ElementDocument.h>
  36. #include <RmlUi/Core/FileInterface.h>
  37. #include <RmlUi/Core/SystemInterface.h>
  38. #include <RmlUi/Core/Types.h>
  39. #include <RmlUi/Core/XMLParser.h>
  40. #include <Shell.h>
  41. using namespace Rml;
  42. static SharedPtr<XMLNodeHandlerMeta> meta_handler;
  43. static SharedPtr<XMLNodeHandlerLink> link_handler;
  44. static void InitializeXmlNodeHandlers()
  45. {
  46. meta_handler = MakeShared<XMLNodeHandlerMeta>();
  47. Rml::XMLParser::RegisterNodeHandler("meta", meta_handler);
  48. link_handler = MakeShared<XMLNodeHandlerLink>();
  49. Rml::XMLParser::RegisterNodeHandler("link", link_handler);
  50. }
  51. class EventListenerLinks : public Rml::EventListener {
  52. public:
  53. void ProcessEvent(Rml::Event& event) override
  54. {
  55. Rml::Element* element = event.GetCurrentElement();
  56. Rml::String href = element->GetAttribute<Rml::String>("href", "");
  57. if (href.empty() || !hover_text)
  58. return;
  59. if (event == Rml::EventId::Click)
  60. {
  61. if (Rml::SystemInterface* system_interface = Rml::GetSystemInterface())
  62. {
  63. system_interface->SetClipboardText(href);
  64. hover_text->SetInnerRML("Copied to clipboard");
  65. hover_text->SetClass("confirmation", true);
  66. }
  67. }
  68. else if (event == Rml::EventId::Mouseover)
  69. {
  70. hover_text->SetInnerRML(Rml::StringUtilities::EncodeRml(href));
  71. }
  72. else if (event == Rml::EventId::Mouseout)
  73. {
  74. hover_text->SetInnerRML("");
  75. hover_text->SetClass("confirmation", false);
  76. }
  77. }
  78. void SetHoverTextElement(Element* element) { hover_text = element; }
  79. private:
  80. Element* hover_text = nullptr;
  81. };
  82. static EventListenerLinks event_listener_links;
  83. TestViewer::TestViewer(Rml::Context* context) : context(context)
  84. {
  85. InitializeXmlNodeHandlers();
  86. const String local_data_path_prefix = "/../Tests/Data/";
  87. document_description = context->LoadDocument(local_data_path_prefix + "description.rml");
  88. RMLUI_ASSERT(document_description);
  89. event_listener_links.SetHoverTextElement(document_description->GetElementById("hovertext"));
  90. document_description->Show();
  91. document_source = context->LoadDocument(local_data_path_prefix + "view_source.rml");
  92. RMLUI_ASSERT(document_source);
  93. document_help = context->LoadDocument(local_data_path_prefix + "visual_tests_help.rml");
  94. RMLUI_ASSERT(document_help);
  95. if (Element* element = document_help->GetElementById("test_directories"))
  96. {
  97. String rml;
  98. const StringList dirs = GetTestInputDirectories();
  99. for (const String& dir : dirs)
  100. rml += "<value>" + Rml::StringUtilities::EncodeRml(dir) + "</value>";
  101. element->SetInnerRML(rml);
  102. }
  103. if (Element* element = document_help->GetElementById("compare_input"))
  104. {
  105. element->SetInnerRML(Rml::StringUtilities::EncodeRml(GetCompareInputDirectory()));
  106. }
  107. if (Element* element = document_help->GetElementById("capture_output"))
  108. {
  109. element->SetInnerRML(Rml::StringUtilities::EncodeRml(GetCaptureOutputDirectory()));
  110. }
  111. }
  112. TestViewer::~TestViewer()
  113. {
  114. event_listener_links.SetHoverTextElement(nullptr);
  115. for (ElementDocument* doc : {document_test, document_description, document_source, document_reference, document_help})
  116. {
  117. if (doc)
  118. doc->Close();
  119. }
  120. }
  121. static Rml::String LoadFile(const String& file_path)
  122. {
  123. String result;
  124. Rml::GetFileInterface()->LoadFile(file_path, result);
  125. return result;
  126. }
  127. void TestViewer::ShowSource(SourceType type)
  128. {
  129. if (type == SourceType::None)
  130. {
  131. document_source->Hide();
  132. }
  133. else
  134. {
  135. const String& source_string = (type == SourceType::Test ? source_test : source_reference);
  136. if (source_string.empty())
  137. {
  138. document_source->Hide();
  139. }
  140. else
  141. {
  142. const String rml_source = StringUtilities::EncodeRml(source_string);
  143. Element* element = document_source->GetElementById("code");
  144. RMLUI_ASSERT(element);
  145. element->SetInnerRML(rml_source);
  146. document_source->Show(ModalFlag::None, FocusFlag::None);
  147. }
  148. }
  149. }
  150. void TestViewer::ShowHelp(bool show)
  151. {
  152. if (show)
  153. document_help->Show();
  154. else
  155. document_help->Hide();
  156. }
  157. bool TestViewer::IsHelpVisible() const
  158. {
  159. return document_help->IsVisible();
  160. }
  161. bool TestViewer::LoadTest(const Rml::String& directory, const Rml::String& filename, int test_index, int number_of_tests, int filtered_test_index,
  162. int filtered_number_of_tests, int suite_index, int number_of_suites, bool keep_scroll_position)
  163. {
  164. float scroll_position = 0.f;
  165. if (document_test)
  166. {
  167. scroll_position = document_test->GetScrollTop();
  168. document_test->Close();
  169. document_test = nullptr;
  170. }
  171. if (document_reference)
  172. {
  173. document_reference->Close();
  174. document_reference = nullptr;
  175. }
  176. reference_filename.clear();
  177. source_test.clear();
  178. source_reference.clear();
  179. meta_handler->ClearMetaList();
  180. link_handler->ClearLinkList();
  181. const Rml::String test_path = directory + '/' + filename;
  182. Rml::String reference_path;
  183. // Load test document, and reference document if it exists.
  184. {
  185. source_test = LoadFile(test_path);
  186. if (source_test.empty())
  187. return false;
  188. document_test = context->LoadDocumentFromMemory(source_test, Rml::StringUtilities::Replace(test_path, ':', '|'));
  189. if (!document_test)
  190. return false;
  191. document_test->Show(ModalFlag::None, FocusFlag::None);
  192. if (keep_scroll_position)
  193. document_test->SetScrollTop(scroll_position);
  194. for (const LinkItem& item : link_handler->GetLinkList())
  195. {
  196. if (item.rel == "match")
  197. {
  198. reference_filename = item.href;
  199. break;
  200. }
  201. }
  202. reference_path = directory + '/' + reference_filename;
  203. if (!reference_filename.empty())
  204. {
  205. source_reference = LoadFile(reference_path);
  206. if (!source_reference.empty())
  207. {
  208. document_reference = context->LoadDocumentFromMemory(source_reference, Rml::StringUtilities::Replace(reference_path, ':', '|'));
  209. if (document_reference)
  210. {
  211. document_reference->SetProperty(PropertyId::Left, Property(510.f, Unit::DP));
  212. document_reference->Show(ModalFlag::None, FocusFlag::None);
  213. }
  214. }
  215. }
  216. }
  217. // Description Header
  218. {
  219. Element* description_header = document_description->GetElementById("header");
  220. RMLUI_ASSERT(description_header);
  221. description_header->SetInnerRML(
  222. CreateString(512, "Test suite %d of %d<br/>Test %d of %d<br/>", suite_index + 1, number_of_suites, test_index + 1, number_of_tests));
  223. }
  224. // Description Filter
  225. {
  226. Element* description_filter_text = document_description->GetElementById("filter_text");
  227. RMLUI_ASSERT(description_filter_text);
  228. if (filtered_number_of_tests == 0)
  229. description_filter_text->SetInnerRML("No matches");
  230. else if (filtered_number_of_tests < number_of_tests && filtered_test_index >= 0)
  231. description_filter_text->SetInnerRML(CreateString(128, "Filtered %d of %d", filtered_test_index + 1, filtered_number_of_tests));
  232. else if (filtered_number_of_tests < number_of_tests && filtered_test_index < 0)
  233. description_filter_text->SetInnerRML(CreateString(128, "Filtered X of %d", filtered_number_of_tests));
  234. else
  235. description_filter_text->SetInnerRML("");
  236. }
  237. // Description Content
  238. {
  239. String rml_description =
  240. Rml::CreateString(512, "<h1>%s</h1><p><a href=\"%s\">%s</a>", document_test->GetTitle().c_str(), test_path.c_str(), filename.c_str());
  241. if (!reference_filename.empty())
  242. {
  243. if (document_reference)
  244. rml_description += "<br/><a href=\"" + reference_path + "\">" + reference_filename + "</a>";
  245. else
  246. rml_description += "<br/>(missing)&nbsp;" + reference_filename + "";
  247. }
  248. rml_description += "</p>";
  249. const LinkList& link_list = link_handler->GetLinkList();
  250. if (!link_list.empty())
  251. {
  252. rml_description += "<p class=\"links\">";
  253. for (const LinkItem& item : link_list)
  254. {
  255. if (item.rel == "match")
  256. continue;
  257. rml_description += "<a href=\"" + item.href + "\">" + item.rel + "</a> ";
  258. }
  259. rml_description += "</p>";
  260. }
  261. for (const MetaItem& item : meta_handler->GetMetaList())
  262. {
  263. rml_description += "<h3>" + item.name + "</h3>";
  264. rml_description += "<p>" + item.content + "</p>";
  265. }
  266. Element* description_content = document_description->GetElementById("content");
  267. RMLUI_ASSERT(description_content);
  268. description_content->SetInnerRML(rml_description);
  269. // Add link hover and click handler.
  270. Rml::ElementList link_elements;
  271. description_content->GetElementsByTagName(link_elements, "a");
  272. for (Rml::Element* element : link_elements)
  273. {
  274. element->AddEventListener(Rml::EventId::Click, &event_listener_links);
  275. element->AddEventListener(Rml::EventId::Mouseover, &event_listener_links);
  276. element->AddEventListener(Rml::EventId::Mouseout, &event_listener_links);
  277. }
  278. }
  279. return true;
  280. }
  281. void TestViewer::SetGoToText(const Rml::String& rml)
  282. {
  283. Element* description_goto = document_description->GetElementById("goto");
  284. RMLUI_ASSERT(description_goto);
  285. description_goto->SetInnerRML(rml);
  286. }
  287. void TestViewer::SetAttention(bool active)
  288. {
  289. if (active)
  290. document_description->SetProperty(Rml::PropertyId::BackgroundColor, Rml::Property(Rml::Colourb(100, 100, 30), Rml::Unit::COLOUR));
  291. else
  292. document_description->RemoveProperty(Rml::PropertyId::BackgroundColor);
  293. }