TestViewer.cpp 8.9 KB

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