TestViewer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. TestViewer::TestViewer(Rml::Context* context) : context(context)
  50. {
  51. InitializeXmlNodeHandlers();
  52. const String local_data_path_prefix = "/../Tests/Data/";
  53. document_description = context->LoadDocument(local_data_path_prefix + "description.rml");
  54. RMLUI_ASSERT(document_description);
  55. document_description->Show();
  56. document_source = context->LoadDocument(local_data_path_prefix + "view_source.rml");
  57. RMLUI_ASSERT(document_source);
  58. }
  59. TestViewer::~TestViewer()
  60. {
  61. for (ElementDocument* doc : { document_test, document_description, document_source, document_reference })
  62. {
  63. if (doc)
  64. doc->Close();
  65. }
  66. }
  67. static Rml::String LoadFile(const String& file_path)
  68. {
  69. FileInterface* file = Rml::GetFileInterface();
  70. FileHandle handle = file->Open(file_path);
  71. if (!handle)
  72. {
  73. return Rml::String();
  74. }
  75. const size_t length = file->Length(handle);
  76. UniquePtr<char[]> buf = UniquePtr<char[]>(new char[length + 1]);
  77. const size_t read_length = file->Read(buf.get(), length, handle);
  78. file->Close(handle);
  79. RMLUI_ASSERT(read_length > 0);
  80. RMLUI_ASSERT(read_length <= length);
  81. buf[read_length] = '\0';
  82. return String(buf.get());
  83. }
  84. void TestViewer::ShowSource(SourceType type)
  85. {
  86. if (type == SourceType::None)
  87. {
  88. document_source->Hide();
  89. }
  90. else
  91. {
  92. const String& source_string = (type == SourceType::Test ? source_test : source_reference);
  93. if (source_string.empty())
  94. {
  95. document_source->Hide();
  96. }
  97. else
  98. {
  99. const String rml_source = StringUtilities::EncodeRml(source_string);
  100. Element* element = document_source->GetElementById("code");
  101. RMLUI_ASSERT(element);
  102. element->SetInnerRML(rml_source);
  103. document_source->Show();
  104. }
  105. }
  106. }
  107. bool TestViewer::LoadTest(const Rml::String& directory, const Rml::String& filename, int test_index, int number_of_tests, int suite_index, int number_of_suites)
  108. {
  109. if (document_test)
  110. {
  111. document_test->Close();
  112. document_test = nullptr;
  113. }
  114. if (document_reference)
  115. {
  116. document_reference->Close();
  117. document_reference = nullptr;
  118. }
  119. reference_filename.clear();
  120. source_test.clear();
  121. source_reference.clear();
  122. meta_handler->ClearMetaList();
  123. link_handler->ClearLinkList();
  124. Element* description_test_suite = document_description->GetElementById("test_suite");
  125. RMLUI_ASSERT(description_test_suite);
  126. description_test_suite->SetInnerRML(CreateString(64, "Test suite %d of %d", suite_index + 1, number_of_suites));
  127. SetGoToText("");
  128. source_test = LoadFile(directory + '/' + filename);
  129. if (source_test.empty())
  130. return false;
  131. document_test = context->LoadDocumentFromMemory(source_test);
  132. if (!document_test)
  133. return false;
  134. document_test->Show();
  135. for (const LinkItem& item : link_handler->GetLinkList())
  136. {
  137. if (item.rel == "match")
  138. {
  139. reference_filename = item.href;
  140. break;
  141. }
  142. }
  143. if (!reference_filename.empty())
  144. {
  145. source_reference = LoadFile(directory + '/' + reference_filename);
  146. if (!source_reference.empty())
  147. {
  148. document_reference = context->LoadDocumentFromMemory(source_reference);
  149. if (document_reference)
  150. {
  151. document_reference->SetProperty(PropertyId::Left, Property(510.f, Property::PX));
  152. document_reference->Show();
  153. }
  154. }
  155. }
  156. String rml_description = Rml::CreateString(512, "<h1>%s</h1><p>Test %d of %d.<br/>%s", document_test->GetTitle().c_str(), test_index + 1, number_of_tests, filename.c_str());
  157. if (!reference_filename.empty())
  158. {
  159. if (document_reference)
  160. rml_description += "<br/>" + reference_filename;
  161. else
  162. rml_description += "<br/>(X " + reference_filename + ")";
  163. }
  164. rml_description += "</p>";
  165. const LinkList& link_list = link_handler->GetLinkList();
  166. if(!link_list.empty())
  167. {
  168. rml_description += "<p class=\"links\">";
  169. for (const LinkItem& item : link_list)
  170. {
  171. if (item.rel == "match")
  172. continue;
  173. rml_description += "<a href=\"" + item.href + "\">" + item.rel + "</a> ";
  174. }
  175. rml_description += "</p>";
  176. }
  177. for (const MetaItem& item : meta_handler->GetMetaList())
  178. {
  179. rml_description += "<h3>" + item.name + "</h3>";
  180. rml_description += "<p style=\"min-height: 120px;\">" + item.content + "</p>";
  181. }
  182. Element* description_content = document_description->GetElementById("content");
  183. RMLUI_ASSERT(description_content);
  184. description_content->SetInnerRML(rml_description);
  185. return true;
  186. }
  187. void TestViewer::SetGoToText(const Rml::String& rml)
  188. {
  189. Element* description_goto = document_description->GetElementById("goto");
  190. RMLUI_ASSERT(description_goto);
  191. description_goto->SetInnerRML(rml);
  192. }
  193. const Rml::String& TestViewer::GetReferenceFilename()
  194. {
  195. return reference_filename;
  196. }