TestViewer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/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)
  79. {
  80. hover_text = element;
  81. }
  82. private:
  83. Element* hover_text = nullptr;
  84. };
  85. static EventListenerLinks event_listener_links;
  86. TestViewer::TestViewer(Rml::Context* context) : context(context)
  87. {
  88. InitializeXmlNodeHandlers();
  89. const String local_data_path_prefix = "/../Tests/Data/";
  90. document_description = context->LoadDocument(local_data_path_prefix + "description.rml");
  91. RMLUI_ASSERT(document_description);
  92. event_listener_links.SetHoverTextElement(document_description->GetElementById("hovertext"));
  93. document_description->Show();
  94. document_source = context->LoadDocument(local_data_path_prefix + "view_source.rml");
  95. RMLUI_ASSERT(document_source);
  96. document_help = context->LoadDocument(local_data_path_prefix + "visual_tests_help.rml");
  97. RMLUI_ASSERT(document_help);
  98. if (Element* element = document_help->GetElementById("test_directories"))
  99. {
  100. String rml;
  101. const StringList dirs = GetTestInputDirectories();
  102. for (const String& dir : dirs)
  103. rml += "<value>" + Rml::StringUtilities::EncodeRml(dir) + "</value>";
  104. element->SetInnerRML(rml);
  105. }
  106. if (Element* element = document_help->GetElementById("compare_input"))
  107. {
  108. element->SetInnerRML(Rml::StringUtilities::EncodeRml(GetCompareInputDirectory()));
  109. }
  110. if (Element* element = document_help->GetElementById("capture_output"))
  111. {
  112. element->SetInnerRML(Rml::StringUtilities::EncodeRml(GetCaptureOutputDirectory()));
  113. }
  114. }
  115. TestViewer::~TestViewer()
  116. {
  117. event_listener_links.SetHoverTextElement(nullptr);
  118. for (ElementDocument* doc : { document_test, document_description, document_source, document_reference, document_help })
  119. {
  120. if (doc)
  121. doc->Close();
  122. }
  123. }
  124. static Rml::String LoadFile(const String& file_path)
  125. {
  126. String result;
  127. Rml::GetFileInterface()->LoadFile(file_path, result);
  128. return result;
  129. }
  130. void TestViewer::ShowSource(SourceType type)
  131. {
  132. if (type == SourceType::None)
  133. {
  134. document_source->Hide();
  135. }
  136. else
  137. {
  138. const String& source_string = (type == SourceType::Test ? source_test : source_reference);
  139. if (source_string.empty())
  140. {
  141. document_source->Hide();
  142. }
  143. else
  144. {
  145. const String rml_source = StringUtilities::EncodeRml(source_string);
  146. Element* element = document_source->GetElementById("code");
  147. RMLUI_ASSERT(element);
  148. element->SetInnerRML(rml_source);
  149. document_source->Show(ModalFlag::None, FocusFlag::None);
  150. }
  151. }
  152. }
  153. void TestViewer::ShowHelp(bool show)
  154. {
  155. if (show)
  156. document_help->Show();
  157. else
  158. document_help->Hide();
  159. }
  160. bool TestViewer::IsHelpVisible() const
  161. {
  162. return document_help->IsVisible();
  163. }
  164. 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)
  165. {
  166. if (document_test)
  167. {
  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. for (const LinkItem& item : link_handler->GetLinkList())
  193. {
  194. if (item.rel == "match")
  195. {
  196. reference_filename = item.href;
  197. break;
  198. }
  199. }
  200. reference_path = directory + '/' + reference_filename;
  201. if (!reference_filename.empty())
  202. {
  203. source_reference = LoadFile(reference_path);
  204. if (!source_reference.empty())
  205. {
  206. document_reference = context->LoadDocumentFromMemory(source_reference, Rml::StringUtilities::Replace(reference_path, ':', '|'));
  207. if (document_reference)
  208. {
  209. document_reference->SetProperty(PropertyId::Left, Property(510.f, Property::DP));
  210. document_reference->Show(ModalFlag::None, FocusFlag::None);
  211. }
  212. }
  213. }
  214. }
  215. // Description Header
  216. {
  217. Element* description_header = document_description->GetElementById("header");
  218. RMLUI_ASSERT(description_header);
  219. description_header->SetInnerRML(CreateString(512, "Test suite %d of %d<br/>Test %d of %d<br/>",
  220. suite_index + 1, number_of_suites, test_index + 1, number_of_tests));
  221. }
  222. // Description Filter
  223. {
  224. Element* description_filter_text = document_description->GetElementById("filter_text");
  225. RMLUI_ASSERT(description_filter_text);
  226. if (filtered_number_of_tests == 0)
  227. description_filter_text->SetInnerRML("No matches");
  228. else if (filtered_number_of_tests < number_of_tests && filtered_test_index >= 0)
  229. description_filter_text->SetInnerRML(CreateString(128, "Filtered %d of %d", filtered_test_index + 1, filtered_number_of_tests));
  230. else if (filtered_number_of_tests < number_of_tests && filtered_test_index < 0)
  231. description_filter_text->SetInnerRML(CreateString(128, "Filtered X of %d", filtered_number_of_tests));
  232. else
  233. description_filter_text->SetInnerRML("");
  234. }
  235. // Description Content
  236. {
  237. String rml_description = Rml::CreateString(512, "<h1>%s</h1><p><a href=\"%s\">%s</a>",
  238. document_test->GetTitle().c_str(), test_path.c_str(), filename.c_str());
  239. if (!reference_filename.empty())
  240. {
  241. if (document_reference)
  242. rml_description += "<br/><a href=\"" + reference_path + "\">" + reference_filename + "</a>";
  243. else
  244. rml_description += "<br/>(missing)&nbsp;" + reference_filename + "";
  245. }
  246. rml_description += "</p>";
  247. const LinkList& link_list = link_handler->GetLinkList();
  248. if(!link_list.empty())
  249. {
  250. rml_description += "<p class=\"links\">";
  251. for (const LinkItem& item : link_list)
  252. {
  253. if (item.rel == "match")
  254. continue;
  255. rml_description += "<a href=\"" + item.href + "\">" + item.rel + "</a> ";
  256. }
  257. rml_description += "</p>";
  258. }
  259. for (const MetaItem& item : meta_handler->GetMetaList())
  260. {
  261. rml_description += "<h3>" + item.name + "</h3>";
  262. rml_description += "<p>" + item.content + "</p>";
  263. }
  264. Element* description_content = document_description->GetElementById("content");
  265. RMLUI_ASSERT(description_content);
  266. description_content->SetInnerRML(rml_description);
  267. // Add link hover and click handler.
  268. Rml::ElementList link_elements;
  269. description_content->GetElementsByTagName(link_elements, "a");
  270. for (Rml::Element* element : link_elements) {
  271. element->AddEventListener(Rml::EventId::Click, &event_listener_links);
  272. element->AddEventListener(Rml::EventId::Mouseover, &event_listener_links);
  273. element->AddEventListener(Rml::EventId::Mouseout, &event_listener_links);
  274. }
  275. }
  276. return true;
  277. }
  278. void TestViewer::SetGoToText(const Rml::String& rml)
  279. {
  280. Element* description_goto = document_description->GetElementById("goto");
  281. RMLUI_ASSERT(description_goto);
  282. description_goto->SetInnerRML(rml);
  283. }
  284. void TestViewer::SetAttention(bool active)
  285. {
  286. if (active)
  287. document_description->SetProperty(Rml::PropertyId::BackgroundColor, Rml::Property(Rml::Colourb(100, 100, 30), Rml::Property::COLOUR));
  288. else
  289. document_description->RemoveProperty(Rml::PropertyId::BackgroundColor);
  290. }