TestViewer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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::IsNavigationLocked() const
  162. {
  163. if (Element* element = context->GetFocusElement())
  164. {
  165. if (document_test && element->GetOwnerDocument() == document_test)
  166. {
  167. if (document_test->HasAttribute("lock-navigation"))
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. bool TestViewer::LoadTest(const Rml::String& directory, const Rml::String& filename, int test_index, int number_of_tests, int filtered_test_index,
  174. int filtered_number_of_tests, int suite_index, int number_of_suites, bool keep_scroll_position)
  175. {
  176. float scroll_position = 0.f;
  177. if (document_test)
  178. {
  179. scroll_position = document_test->GetScrollTop();
  180. document_test->Close();
  181. document_test = nullptr;
  182. }
  183. if (document_reference)
  184. {
  185. document_reference->Close();
  186. document_reference = nullptr;
  187. }
  188. reference_filename.clear();
  189. source_test.clear();
  190. source_reference.clear();
  191. meta_handler->ClearMetaList();
  192. link_handler->ClearLinkList();
  193. const Rml::String test_path = directory + '/' + filename;
  194. Rml::String reference_path;
  195. // Load test document, and reference document if it exists.
  196. {
  197. source_test = LoadFile(test_path);
  198. if (source_test.empty())
  199. return false;
  200. document_test = context->LoadDocumentFromMemory(source_test, Rml::StringUtilities::Replace(test_path, ':', '|'));
  201. if (!document_test)
  202. return false;
  203. document_test->Show(ModalFlag::None, FocusFlag::None);
  204. if (keep_scroll_position)
  205. document_test->SetScrollTop(scroll_position);
  206. for (const LinkItem& item : link_handler->GetLinkList())
  207. {
  208. if (item.rel == "match")
  209. {
  210. reference_filename = item.href;
  211. break;
  212. }
  213. }
  214. reference_path = directory + '/' + reference_filename;
  215. if (!reference_filename.empty())
  216. {
  217. source_reference = LoadFile(reference_path);
  218. if (!source_reference.empty())
  219. {
  220. document_reference = context->LoadDocumentFromMemory(source_reference, Rml::StringUtilities::Replace(reference_path, ':', '|'));
  221. if (document_reference)
  222. {
  223. document_reference->SetProperty(PropertyId::Left, Property(510.f, Unit::DP));
  224. document_reference->Show(ModalFlag::None, FocusFlag::None);
  225. }
  226. }
  227. }
  228. }
  229. // Description Header
  230. {
  231. Element* description_header = document_description->GetElementById("header");
  232. RMLUI_ASSERT(description_header);
  233. description_header->SetInnerRML(
  234. CreateString("Test suite %d of %d<br/>Test %d of %d<br/>", suite_index + 1, number_of_suites, test_index + 1, number_of_tests));
  235. }
  236. // Description Filter
  237. {
  238. Element* description_filter_text = document_description->GetElementById("filter_text");
  239. RMLUI_ASSERT(description_filter_text);
  240. if (filtered_number_of_tests == 0)
  241. description_filter_text->SetInnerRML("No matches");
  242. else if (filtered_number_of_tests < number_of_tests && filtered_test_index >= 0)
  243. description_filter_text->SetInnerRML(CreateString("Filtered %d of %d", filtered_test_index + 1, filtered_number_of_tests));
  244. else if (filtered_number_of_tests < number_of_tests && filtered_test_index < 0)
  245. description_filter_text->SetInnerRML(CreateString("Filtered X of %d", filtered_number_of_tests));
  246. else
  247. description_filter_text->SetInnerRML("");
  248. }
  249. // Description Content
  250. {
  251. String rml_description =
  252. Rml::CreateString("<h1>%s</h1><p><a href=\"%s\">%s</a>", document_test->GetTitle().c_str(), test_path.c_str(), filename.c_str());
  253. if (!reference_filename.empty())
  254. {
  255. if (document_reference)
  256. rml_description += "<br/><a href=\"" + reference_path + "\">" + reference_filename + "</a>";
  257. else
  258. rml_description += "<br/>(missing)&nbsp;" + reference_filename + "";
  259. }
  260. rml_description += "</p>";
  261. const LinkList& link_list = link_handler->GetLinkList();
  262. if (!link_list.empty())
  263. {
  264. rml_description += "<p class=\"links\">";
  265. for (const LinkItem& item : link_list)
  266. {
  267. if (item.rel == "match")
  268. continue;
  269. rml_description += "<a href=\"" + item.href + "\">" + item.rel + "</a> ";
  270. }
  271. rml_description += "</p>";
  272. }
  273. for (const MetaItem& item : meta_handler->GetMetaList())
  274. {
  275. rml_description += "<h3>" + item.name + "</h3>";
  276. rml_description += "<p>" + item.content + "</p>";
  277. }
  278. Element* description_content = document_description->GetElementById("content");
  279. RMLUI_ASSERT(description_content);
  280. description_content->SetInnerRML(rml_description);
  281. // Add link hover and click handler.
  282. Rml::ElementList link_elements;
  283. description_content->GetElementsByTagName(link_elements, "a");
  284. for (Rml::Element* element : link_elements)
  285. {
  286. element->AddEventListener(Rml::EventId::Click, &event_listener_links);
  287. element->AddEventListener(Rml::EventId::Mouseover, &event_listener_links);
  288. element->AddEventListener(Rml::EventId::Mouseout, &event_listener_links);
  289. }
  290. }
  291. return true;
  292. }
  293. void TestViewer::SetGoToText(const Rml::String& rml)
  294. {
  295. Element* description_goto = document_description->GetElementById("goto");
  296. RMLUI_ASSERT(description_goto);
  297. description_goto->SetInnerRML(rml);
  298. }
  299. void TestViewer::SetAttention(bool active)
  300. {
  301. if (active)
  302. document_description->SetProperty(Rml::PropertyId::BackgroundColor, Rml::Property(Rml::Colourb(100, 100, 30), Rml::Unit::COLOUR));
  303. else
  304. document_description->RemoveProperty(Rml::PropertyId::BackgroundColor);
  305. }