TestViewer.cpp 9.7 KB

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