Filter.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "../Common/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <RmlUi/Core/Factory.h>
  33. #include <RmlUi/Core/Filter.h>
  34. #include <RmlUi/Core/PropertyDefinition.h>
  35. #include <RmlUi/Core/PropertyDictionary.h>
  36. #include <algorithm>
  37. #include <doctest.h>
  38. using namespace Rml;
  39. class FilterTest;
  40. struct CompiledTestFilter {
  41. String element_id;
  42. const FilterTest* filter;
  43. };
  44. static Vector<CompiledTestFilter> compiled_test_filters;
  45. class FilterTest : public Filter {
  46. public:
  47. FilterTest(float value, Unit unit) : value(value), unit(unit) {}
  48. ~FilterTest() { CHECK(num_compile == num_release); }
  49. CompiledFilterHandle CompileFilter(Element* element) const override
  50. {
  51. compiled_test_filters.push_back({element->GetId(), this});
  52. num_compile += 1;
  53. return CompiledFilterHandle(num_compile);
  54. }
  55. void ReleaseCompiledFilter(Element* /*element*/, CompiledFilterHandle /*filter_handle*/) const override { num_release += 1; }
  56. float value = 0.f;
  57. Unit unit = Unit::UNKNOWN;
  58. mutable int num_compile = 0;
  59. mutable int num_release = 0;
  60. };
  61. class FilterTestInstancer : public FilterInstancer {
  62. public:
  63. enum class ValueType { NumberPercent, Angle };
  64. FilterTestInstancer()
  65. {
  66. id = RegisterProperty("value", "10cm").AddParser("length").GetId();
  67. RegisterShorthand("filter", "value", ShorthandType::FallThrough);
  68. }
  69. SharedPtr<Filter> InstanceFilter(const String& /*name*/, const PropertyDictionary& properties) override
  70. {
  71. const Property* p_value = properties.GetProperty(id);
  72. if (!p_value)
  73. return nullptr;
  74. CHECK(Any(p_value->unit & Unit::LENGTH));
  75. num_instances += 1;
  76. return MakeShared<FilterTest>(p_value->Get<float>(), p_value->unit);
  77. }
  78. int num_instances = 0;
  79. private:
  80. PropertyId id = {};
  81. };
  82. static const String document_filter_rml = R"(
  83. <rml>
  84. <head>
  85. <style>
  86. body {
  87. top: 100px;
  88. left: 200px;
  89. width: 800px;
  90. height: 600px;
  91. }
  92. div {
  93. display: block;
  94. width: 400px;
  95. height: 300px;
  96. }
  97. #a { filter: test(); }
  98. #b { filter: test(0); }
  99. #c,#d { filter: test(1dp); }
  100. </style>
  101. </head>
  102. <body>
  103. <div id="a"/>
  104. <div id="b"/>
  105. <div id="c"/>
  106. <div id="d"/>
  107. </body>
  108. </rml>
  109. )";
  110. TEST_CASE("filter")
  111. {
  112. compiled_test_filters.clear();
  113. Context* context = TestsShell::GetContext();
  114. REQUIRE(context);
  115. FilterTestInstancer instancer;
  116. Rml::Factory::RegisterFilterInstancer("test", &instancer);
  117. ElementDocument* document = context->LoadDocumentFromMemory(document_filter_rml);
  118. document->Show();
  119. TestsShell::RenderLoop();
  120. struct ExpectedCompiledFilter {
  121. float value;
  122. Unit unit;
  123. };
  124. // Map element ID to its expected filter value and unit.
  125. UnorderedMap<String, ExpectedCompiledFilter> expected_compiled_filters = {
  126. {"a", {10.f, Unit::CM}},
  127. {"b", {0.f, Unit::PX}},
  128. {"c", {1.f, Unit::DP}},
  129. {"d", {1.f, Unit::DP}},
  130. };
  131. for (const auto& compiled_filter : compiled_test_filters)
  132. {
  133. INFO("ID #", compiled_filter.element_id);
  134. auto it = expected_compiled_filters.find(compiled_filter.element_id);
  135. const bool id_found = (it != expected_compiled_filters.end());
  136. CHECK(id_found);
  137. if (!id_found)
  138. continue;
  139. const ExpectedCompiledFilter& expected = it->second;
  140. CHECK(compiled_filter.filter->value == expected.value);
  141. CHECK(compiled_filter.filter->unit == expected.unit);
  142. }
  143. // Check that filters are not compiled more than once for each element.
  144. CHECK(compiled_test_filters.size() == expected_compiled_filters.size());
  145. // Filters aren't cached like decorators are, so each element will instance a new decorator even if they refer to
  146. // the same style rule. Thus, here producing 4 instead of 3 unique instances.
  147. CHECK(instancer.num_instances == 4);
  148. document->Close();
  149. TestsShell::ShutdownShell();
  150. }