Template.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "../Common/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <doctest.h>
  33. #include <algorithm>
  34. using namespace Rml;
  35. static const String document_body_template_rml = R"(
  36. <rml>
  37. <head>
  38. <link type="text/template" href="/assets/window.rml"/>
  39. <style>
  40. body.window
  41. {
  42. top: 100px;
  43. left: 200px;
  44. width: 600px;
  45. height: 450px;
  46. }
  47. </style>
  48. </head>
  49. <body id="body" class="overridden" template="window">
  50. <p id="p">A paragraph</p>
  51. </body>
  52. </rml>
  53. )";
  54. static const String p_address_body_template = "p#p < div#content < div#window < body#body.window < #root#main";
  55. static const String document_inline_template_rml = R"(
  56. <rml>
  57. <head>
  58. <link type="text/template" href="/assets/window.rml"/>
  59. <style>
  60. body
  61. {
  62. top: 100px;
  63. left: 200px;
  64. width: 600px;
  65. height: 450px;
  66. }
  67. </style>
  68. </head>
  69. <body id="body" class="inline">
  70. <p>Paragraph outside the window.</p>
  71. <div id="template_parent">
  72. <template src="window">
  73. <p id="p">A paragraph</p>
  74. </template>
  75. </div>
  76. </body>
  77. </rml>
  78. )";
  79. static const String p_address_inline_template = "p#p < div#content < div#window < div#template_parent < body#body.inline < #root#main";
  80. TEST_CASE("template")
  81. {
  82. Context* context = TestsShell::GetContext();
  83. REQUIRE(context);
  84. SUBCASE("body")
  85. {
  86. INFO("Expected warning: Body 'class' attribute overridden by template.");
  87. TestsShell::SetNumExpectedWarnings(1);
  88. ElementDocument* document = context->LoadDocumentFromMemory(document_body_template_rml);
  89. REQUIRE(document);
  90. TestsShell::SetNumExpectedWarnings(0);
  91. document->Show();
  92. context->Update();
  93. context->Render();
  94. TestsShell::RenderLoop();
  95. Element* el_p = document->GetElementById("p");
  96. REQUIRE(el_p);
  97. CHECK(el_p->GetAddress() == p_address_body_template);
  98. document->Close();
  99. }
  100. SUBCASE("inline")
  101. {
  102. ElementDocument* document = context->LoadDocumentFromMemory(document_inline_template_rml);
  103. REQUIRE(document);
  104. document->Show();
  105. context->Update();
  106. context->Render();
  107. TestsShell::RenderLoop();
  108. Element* el_p = document->GetElementById("p");
  109. REQUIRE(el_p);
  110. CHECK(el_p->GetAddress() == p_address_inline_template);
  111. document->Close();
  112. }
  113. TestsShell::ShutdownShell();
  114. }