TestViewer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. FileInterface* file = Rml::GetFileInterface();
  123. FileHandle handle = file->Open(file_path);
  124. if (!handle)
  125. {
  126. return Rml::String();
  127. }
  128. const size_t length = file->Length(handle);
  129. UniquePtr<char[]> buf = UniquePtr<char[]>(new char[length + 1]);
  130. const size_t read_length = file->Read(buf.get(), length, handle);
  131. file->Close(handle);
  132. RMLUI_ASSERT(read_length > 0);
  133. RMLUI_ASSERT(read_length <= length);
  134. buf[read_length] = '\0';
  135. return String(buf.get());
  136. }
  137. void TestViewer::ShowSource(SourceType type)
  138. {
  139. if (type == SourceType::None)
  140. {
  141. document_source->Hide();
  142. }
  143. else
  144. {
  145. const String& source_string = (type == SourceType::Test ? source_test : source_reference);
  146. if (source_string.empty())
  147. {
  148. document_source->Hide();
  149. }
  150. else
  151. {
  152. const String rml_source = StringUtilities::EncodeRml(source_string);
  153. Element* element = document_source->GetElementById("code");
  154. RMLUI_ASSERT(element);
  155. element->SetInnerRML(rml_source);
  156. document_source->Show(ModalFlag::None, FocusFlag::None);
  157. }
  158. }
  159. }
  160. void TestViewer::ShowHelp(bool show)
  161. {
  162. if (show)
  163. document_help->Show();
  164. else
  165. document_help->Hide();
  166. }
  167. bool TestViewer::IsHelpVisible() const
  168. {
  169. return document_help->IsVisible();
  170. }
  171. 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)
  172. {
  173. if (document_test)
  174. {
  175. document_test->Close();
  176. document_test = nullptr;
  177. }
  178. if (document_reference)
  179. {
  180. document_reference->Close();
  181. document_reference = nullptr;
  182. }
  183. reference_filename.clear();
  184. source_test.clear();
  185. source_reference.clear();
  186. meta_handler->ClearMetaList();
  187. link_handler->ClearLinkList();
  188. const Rml::String test_path = directory + '/' + filename;
  189. Rml::String reference_path;
  190. // Load test document, and reference document if it exists.
  191. {
  192. source_test = LoadFile(test_path);
  193. if (source_test.empty())
  194. return false;
  195. document_test = context->LoadDocumentFromMemory(source_test);
  196. if (!document_test)
  197. return false;
  198. document_test->Show(ModalFlag::None, FocusFlag::None);
  199. for (const LinkItem& item : link_handler->GetLinkList())
  200. {
  201. if (item.rel == "match")
  202. {
  203. reference_filename = item.href;
  204. break;
  205. }
  206. }
  207. reference_path = directory + '/' + reference_filename;
  208. if (!reference_filename.empty())
  209. {
  210. source_reference = LoadFile(reference_path);
  211. if (!source_reference.empty())
  212. {
  213. document_reference = context->LoadDocumentFromMemory(source_reference);
  214. if (document_reference)
  215. {
  216. document_reference->SetProperty(PropertyId::Left, Property(510.f, Property::PX));
  217. document_reference->Show(ModalFlag::None, FocusFlag::None);
  218. }
  219. }
  220. }
  221. }
  222. // Description Header
  223. {
  224. Element* description_header = document_description->GetElementById("header");
  225. RMLUI_ASSERT(description_header);
  226. description_header->SetInnerRML(CreateString(512, "Test suite %d of %d<br/>Test %d of %d<br/>",
  227. suite_index + 1, number_of_suites, test_index + 1, number_of_tests));
  228. }
  229. // Description Filter
  230. {
  231. Element* description_filter_text = document_description->GetElementById("filter_text");
  232. RMLUI_ASSERT(description_filter_text);
  233. if (filtered_number_of_tests == 0)
  234. description_filter_text->SetInnerRML("No matches");
  235. else if (filtered_number_of_tests < number_of_tests && filtered_test_index >= 0)
  236. description_filter_text->SetInnerRML(CreateString(128, "Filtered %d of %d", filtered_test_index + 1, filtered_number_of_tests));
  237. else if (filtered_number_of_tests < number_of_tests && filtered_test_index < 0)
  238. description_filter_text->SetInnerRML(CreateString(128, "Filtered X of %d", filtered_number_of_tests));
  239. else
  240. description_filter_text->SetInnerRML("");
  241. }
  242. // Description Content
  243. {
  244. String rml_description = Rml::CreateString(512, "<h1>%s</h1><p><a href=\"%s\">%s</a>",
  245. document_test->GetTitle().c_str(), test_path.c_str(), filename.c_str());
  246. if (!reference_filename.empty())
  247. {
  248. if (document_reference)
  249. rml_description += "<br/><a href=\"" + reference_path + "\">" + reference_filename + "</a>";
  250. else
  251. rml_description += "<br/>(missing)&nbsp;" + reference_filename + "";
  252. }
  253. rml_description += "</p>";
  254. const LinkList& link_list = link_handler->GetLinkList();
  255. if(!link_list.empty())
  256. {
  257. rml_description += "<p class=\"links\">";
  258. for (const LinkItem& item : link_list)
  259. {
  260. if (item.rel == "match")
  261. continue;
  262. rml_description += "<a href=\"" + item.href + "\">" + item.rel + "</a> ";
  263. }
  264. rml_description += "</p>";
  265. }
  266. for (const MetaItem& item : meta_handler->GetMetaList())
  267. {
  268. rml_description += "<h3>" + item.name + "</h3>";
  269. rml_description += "<p>" + item.content + "</p>";
  270. }
  271. Element* description_content = document_description->GetElementById("content");
  272. RMLUI_ASSERT(description_content);
  273. description_content->SetInnerRML(rml_description);
  274. // Add link hover and click handler.
  275. Rml::ElementList link_elements;
  276. description_content->GetElementsByTagName(link_elements, "a");
  277. for (Rml::Element* element : link_elements) {
  278. element->AddEventListener(Rml::EventId::Click, &event_listener_links);
  279. element->AddEventListener(Rml::EventId::Mouseover, &event_listener_links);
  280. element->AddEventListener(Rml::EventId::Mouseout, &event_listener_links);
  281. }
  282. }
  283. return true;
  284. }
  285. void TestViewer::SetGoToText(const Rml::String& rml)
  286. {
  287. Element* description_goto = document_description->GetElementById("goto");
  288. RMLUI_ASSERT(description_goto);
  289. description_goto->SetInnerRML(rml);
  290. }