Element.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/Mocks.h"
  29. #include "../Common/TestsShell.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <RmlUi/Core/Factory.h>
  34. #include <doctest.h>
  35. using namespace Rml;
  36. static const String document_clone_rml = R"(
  37. <rml>
  38. <head>
  39. <title>Test</title>
  40. <link type="text/rcss" href="/assets/rml.rcss"/>
  41. <link type="text/rcss" href="/assets/invader.rcss"/>
  42. <style>
  43. body
  44. {
  45. left: 0;
  46. top: 0;
  47. right: 0;
  48. bottom: 0;
  49. }
  50. div {
  51. drag: clone;
  52. background: #ccc;
  53. height: 100px;
  54. }
  55. span {
  56. color: red;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div>This is a <span>sample</span>.</div>
  62. </body>
  63. </rml>
  64. )";
  65. TEST_CASE("Element")
  66. {
  67. Context* context = TestsShell::GetContext();
  68. REQUIRE(context);
  69. ElementDocument* document = context->LoadDocumentFromMemory(document_clone_rml);
  70. REQUIRE(document);
  71. document->Show();
  72. context->Update();
  73. context->Render();
  74. TestsShell::RenderLoop();
  75. SUBCASE("Attribute")
  76. {
  77. auto* button = document->AppendChild(document->CreateElement("button"));
  78. SUBCASE("Event listener")
  79. {
  80. namespace tl = trompeloeil;
  81. static constexpr auto ON_CLICK_ATTRIBUTE = "onclick";
  82. static constexpr auto ON_CLICK_VALUE = "moo";
  83. std::vector<UniquePtr<tl::expectation>> expectations;
  84. UniquePtr<MockEventListener> mockEventListener;
  85. const auto configureMockEventListener = [&]()
  86. {
  87. mockEventListener.reset(new MockEventListener());
  88. expectations.emplace_back(NAMED_ALLOW_CALL(*mockEventListener, OnAttach(button)));
  89. expectations.emplace_back(NAMED_ALLOW_CALL(*mockEventListener, OnDetach(button))
  90. .LR_SIDE_EFFECT(mockEventListener.reset()));
  91. };
  92. MockEventListenerInstancer mockEventListenerInstancer;
  93. const auto configureMockEventListenerInstancer = [&](const auto value)
  94. {
  95. expectations.emplace_back(NAMED_REQUIRE_CALL(mockEventListenerInstancer, InstanceEventListener(value, button))
  96. .LR_SIDE_EFFECT(configureMockEventListener())
  97. .LR_RETURN(mockEventListener.get()));
  98. };
  99. Factory::RegisterEventListenerInstancer(&mockEventListenerInstancer);
  100. configureMockEventListenerInstancer(ON_CLICK_VALUE);
  101. button->SetAttribute(ON_CLICK_ATTRIBUTE, ON_CLICK_VALUE);
  102. SUBCASE("Replacement")
  103. {
  104. static constexpr auto REPLACEMENT_ON_CLICK_VALUE = "boo";
  105. configureMockEventListenerInstancer(REPLACEMENT_ON_CLICK_VALUE);
  106. button->SetAttribute(ON_CLICK_ATTRIBUTE, REPLACEMENT_ON_CLICK_VALUE);
  107. }
  108. button->RemoveAttribute(ON_CLICK_ATTRIBUTE);
  109. }
  110. SUBCASE("Simple")
  111. {
  112. static constexpr auto DISABLED_ATTRIBUTE = "disabled";
  113. REQUIRE_FALSE(button->HasAttribute(DISABLED_ATTRIBUTE));
  114. button->SetAttribute(DISABLED_ATTRIBUTE, "");
  115. REQUIRE(button->HasAttribute(DISABLED_ATTRIBUTE));
  116. SUBCASE("Replacement")
  117. {
  118. static constexpr auto VALUE = "something";
  119. button->SetAttribute(DISABLED_ATTRIBUTE, VALUE);
  120. const auto* attributeValue = button->GetAttribute(DISABLED_ATTRIBUTE);
  121. REQUIRE(attributeValue);
  122. REQUIRE(attributeValue->GetType() == Variant::Type::STRING);
  123. CHECK(attributeValue->Get<String>() == VALUE);
  124. }
  125. button->RemoveAttribute(DISABLED_ATTRIBUTE);
  126. CHECK_FALSE(button->HasAttribute(DISABLED_ATTRIBUTE));
  127. }
  128. }
  129. SUBCASE("Clone")
  130. {
  131. // Simulate input for mouse click and drag
  132. context->ProcessMouseMove(10, 10, 0);
  133. context->Update();
  134. context->Render();
  135. context->ProcessMouseButtonDown(0, 0);
  136. context->Update();
  137. context->Render();
  138. // This should initiate a drag clone.
  139. context->ProcessMouseMove(10, 11, 0);
  140. context->Update();
  141. context->Render();
  142. context->ProcessMouseButtonUp(0, 0);
  143. }
  144. document->Close();
  145. TestsShell::ShutdownShell();
  146. }