Browse Source

Add tests for RCSS selectors and Element::QuerySelector(All)

Michael Ragazzon 5 years ago
parent
commit
4f78dedd31

+ 30 - 3
Tests/Source/DataExpression.cpp

@@ -1,7 +1,34 @@
-#include <doctest.h>
-#include <nanobench.h>
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
 
 #include "../../../Source/Core/DataExpression.cpp"
+#include <doctest.h>
+#include <nanobench.h>
 
 using namespace ankerl;
 using namespace Rml;
@@ -25,7 +52,7 @@ String TestExpression(const String& expression, const char* benchmark_name = nul
 			});
 	}
 
-	if (parser.Parse(false) && false)
+	if (parser.Parse(false))
 	{
 		Program program = parser.ReleaseProgram();
 		AddressList addresses = parser.ReleaseAddresses();

+ 30 - 2
Tests/Source/DataModel.cpp

@@ -1,6 +1,34 @@
-#include <doctest.h>
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
 
 #include "../../../Source/Core/DataModel.cpp"
+#include <doctest.h>
+#include <array>
 
 using namespace Rml;
 
@@ -53,7 +81,7 @@ TEST_CASE("Data variables")
 
 	handle.Bind("data", &data);
 
-	// Test data addresses, setters and assignments and assignments
+	// Test data addresses, setters, and assignments
 	{
 		std::vector<String> test_addresses = { "data.more_fun[1].magic[3]", "data.more_fun[1].magic.size", "data.fun.x", "data.valid" };
 		std::vector<String> expected_results = { ToString(data.more_fun[1].magic[3]), ToString(int(data.more_fun[1].magic.size())), ToString(data.fun.x), ToString(data.valid) };

+ 28 - 1
Tests/Source/GeometryDatabase.cpp

@@ -1,6 +1,33 @@
-#include <doctest.h>
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
 
 #include "../../../Source/Core/GeometryDatabase.cpp"
+#include <doctest.h>
 
 using namespace Rml;
 

+ 198 - 0
Tests/Source/Selectors.cpp

@@ -0,0 +1,198 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "TestsInterface.h"
+#include <RmlUi/Core.h>
+#include <RmlUi/Debugger.h>
+#include <doctest.h>
+
+using namespace Rml;
+
+static const String doc_begin = R"(
+<rml>
+<head>
+	<title>Demo</title>
+	<style>
+		body
+		{
+			width: 400px;
+			height: 300px;
+			margin: auto;
+		}
+)";
+static const String doc_end = R"(
+	</style>
+</head>
+<body>
+	<div  id="X" class="hello"/>
+	<span id="Y" class="world"/>
+	<div  id="Z" class="hello world"/>
+	<div  id="P" class="parent">
+		<h1 id="A"/>
+		<p  id="B"/>
+		<p  id="C"/>
+		<p  id="D"> <span id="D0"/><span id="D1"/> </p>
+		<h3 id="E"/>
+		<p  id="F"> <span id="F0"/> </p>
+		<p  id="G"/>
+		<p  id="H" class="hello"/>
+	</div>
+	<input id="I" type="checkbox" checked/>
+</body>
+</rml>
+)";
+
+struct Selector {
+	String selector;
+	String expected_ids;
+};
+static const std::vector<Selector> selectors =
+{
+	{ "span",                        "Y D0 D1 F0" },
+	{ ".hello",                      "X Z H" },
+	{ ".hello.world",                "Z" },
+	{ "div.hello",                   "X Z" },
+	{ "body .hello",                 "X Z H" },
+	{ "body>.hello",                 "X Z" },
+	{ "body > .hello",               "X Z" },
+	{ ".parent *",                   "A B C D D0 D1 E F F0 G H" },
+	{ ".parent > *",                 "A B C D E F G H" },
+	{ ":checked",                    "I" },
+	{ ".parent :nth-child(odd)",     "A C D0 E F0 G" },
+	{ ".parent > :nth-child(even)",  "B D F H" },
+	{ ":first-child",                "X A D0 F0" },
+	{ ":last-child",                 "D1 F0 H I" },
+	{ "p:nth-child(2)",              "B" },
+	{ "h1:nth-child(2)",             "" },
+	{ "p:nth-child(3n+1)",           "D G" },
+	{ "p:nth-child(3n + 1)",         "D G" },
+	{ "#P > :nth-last-child(2n+1)",  "B D F H" },
+	{ "#P p:nth-of-type(odd)",       "B D G" },
+	{ "p:nth-last-of-type(3n+1)",    "D H" },
+	{ ":first-of-type",              "X Y A B D0 E F0 I" },
+	{ ":last-of-type",               "Y P A D1 E F0 H I" },
+	{ ":only-child",                 "F0" },
+	{ ":only-of-type",               "Y A E F0 I" },
+	{ "span:empty",                  "Y D0 D1 F0" },
+	{ ".hello.world, #P span, #I",   "Z D0 D1 F0 I" },
+	{ "#P * span",                   "D0 D1 F0" },
+};
+
+
+
+static void GetMatchingIds(String& matching_ids, Element* element)
+{
+	String id = element->GetId();
+	if (!id.empty() && element->GetProperty<int>("drag") == (int)Style::Drag::Drag)
+	{
+		matching_ids += id + ' ';
+	}
+
+	for (int i = 0; i < element->GetNumChildren(); i++)
+	{
+		GetMatchingIds(matching_ids, element->GetChild(i));
+	}
+}
+
+static String ElementListToIds(const ElementList& elements)
+{
+	String result;
+
+	for (Element* element : elements)
+	{
+		result += element->GetId() + ' ';
+	}
+	if (!result.empty())
+		result.pop_back();
+
+	return result;
+}
+
+TEST_CASE("Selectors")
+{
+	const Vector2i window_size(1024, 768);
+
+	TestsSystemInterface system_interface;
+	TestsRenderInterface render_interface;
+
+	SetRenderInterface(&render_interface);
+	SetSystemInterface(&system_interface);
+
+	Initialise();
+
+	Context* context = Rml::CreateContext("main", window_size);
+	REQUIRE(context);
+
+	Debugger::Initialise(context);
+
+	for(const Selector& selector : selectors)
+	{
+		// Check RCSS document selectors
+		const String selector_css = selector.selector + " { drag: drag; } ";
+		const String document_string = doc_begin + selector_css + doc_end;
+		ElementDocument* document = context->LoadDocumentFromMemory(document_string);
+		REQUIRE(document);
+
+		document->Show();
+		context->Update();
+
+		SUBCASE("RCSS document selectors")
+		{
+			String matching_ids;
+			GetMatchingIds(matching_ids, document);
+
+			if (!matching_ids.empty())
+				matching_ids.pop_back();
+
+			CHECK_MESSAGE(matching_ids == selector.expected_ids, "Selector: " << selector.selector);
+		}
+
+		SUBCASE("QuerySelector(All)")
+		{
+			ElementList elements;
+			document->QuerySelectorAll(elements, selector.selector);
+			String matching_ids = ElementListToIds(elements);
+
+			Element* first_element = document->QuerySelector(selector.selector);
+			if (first_element)
+			{
+				CHECK_MESSAGE(first_element == elements[0], "QuerySelector does not return the first match of QuerySelectorAll.");
+			}
+			else
+			{
+				CHECK_MESSAGE(elements.empty(), "QuerySelector found nothing, while QuerySelectorAll found " << elements.size() << " element(s).");
+			}
+
+			CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector);
+		}
+
+		context->UnloadDocument(document);
+	}
+
+	Rml::Shutdown();
+}

+ 83 - 0
Tests/Source/TestsInterface.cpp

@@ -0,0 +1,83 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "TestsInterface.h"
+#include <doctest.h>
+#include <chrono>
+
+double TestsSystemInterface::GetElapsedTime()
+{
+	static const auto start_time = std::chrono::steady_clock::now();
+
+	const auto now = std::chrono::steady_clock::now();
+	const double t = std::chrono::duration<double>(now - start_time).count();
+
+	return t;
+}
+
+bool TestsSystemInterface::LogMessage(Rml::Log::Type type, const Rml::String& message)
+{
+	bool result = Rml::SystemInterface::LogMessage(type, message);
+	CHECK_MESSAGE(type >= Rml::Log::Type::LT_INFO, "RmlUi logged: " << message);
+	return result;
+}
+
+
+void TestsRenderInterface::RenderGeometry(Rml::Vertex* /*vertices*/, int /*num_vertices*/, int* /*indices*/, int /*num_indices*/, const Rml::TextureHandle /*texture*/, const Rml::Vector2f& /*translation*/)
+{
+}
+
+void TestsRenderInterface::EnableScissorRegion(bool /*enable*/)
+{
+}
+
+void TestsRenderInterface::SetScissorRegion(int /*x*/, int /*y*/, int /*width*/, int /*height*/)
+{
+}
+
+bool TestsRenderInterface::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& /*source*/)
+{
+	texture_handle = 1;
+	texture_dimensions.x = 512;
+	texture_dimensions.y = 256;
+	return true;
+}
+
+bool TestsRenderInterface::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* /*source*/, const Rml::Vector2i& /*source_dimensions*/)
+{
+	texture_handle = 1;
+	return true;
+}
+
+void TestsRenderInterface::ReleaseTexture(Rml::TextureHandle /*texture_handle*/)
+{
+}
+
+void TestsRenderInterface::SetTransform(const Rml::Matrix4f* /*transform*/)
+{
+}

+ 57 - 0
Tests/Source/TestsInterface.h

@@ -0,0 +1,57 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef RMLUI_TESTS_TESTSINTERFACE_H
+#define RMLUI_TESTS_TESTSINTERFACE_H
+
+#include <RmlUi/Core/SystemInterface.h>
+#include <RmlUi/Core/RenderInterface.h>
+
+class TestsSystemInterface : public Rml::SystemInterface
+{
+public:
+	double GetElapsedTime() override;
+	bool LogMessage(Rml::Log::Type type, const Rml::String& message) override;
+};
+
+
+class TestsRenderInterface : public Rml::RenderInterface
+{
+public:
+	void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f& translation) override;
+
+	void EnableScissorRegion(bool enable) override;
+	void SetScissorRegion(int x, int y, int width, int height) override;
+
+	bool LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) override;
+	bool GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) override;
+	void ReleaseTexture(Rml::TextureHandle texture_handle) override;
+
+	void SetTransform(const Rml::Matrix4f* transform) override;
+};
+#endif

+ 28 - 0
Tests/Source/main.cpp

@@ -1,3 +1,31 @@
+/*
+ * This source file is part of RmlUi, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://github.com/mikke89/RmlUi
+ *
+ * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
+ * Copyright (c) 2019 The RmlUi Team, and contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
 #define ANKERL_NANOBENCH_IMPLEMENT
 #include <nanobench.h>