EventListener.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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) 2019-2024 The RmlUi Team, and contributors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "../Common/TestsShell.h"
  28. #include <RmlUi/Core/Context.h>
  29. #include <RmlUi/Core/Element.h>
  30. #include <RmlUi/Core/ElementDocument.h>
  31. #include <RmlUi/Core/EventListener.h>
  32. #include <RmlUi/Core/EventListenerInstancer.h>
  33. #include <RmlUi/Core/Factory.h>
  34. #include <doctest.h>
  35. using namespace Rml;
  36. static const String document_decorator_rml = R"(
  37. <rml>
  38. <head>
  39. <title>Test</title>
  40. <link type="text/rcss" href="/assets/rml.rcss"/>
  41. <style>
  42. body {
  43. left: 0;
  44. top: 0;
  45. right: 0;
  46. bottom: 0;
  47. }
  48. div, button {
  49. display: block;
  50. background: #333;
  51. height: 64px;
  52. width: 64px;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <div>
  58. <button id="exit" onclick="exit" />
  59. </div>
  60. </body>
  61. </rml>
  62. )";
  63. class DemoEventListenerInstancer : public Rml::EventListenerInstancer {
  64. public:
  65. Rml::EventListener* InstanceEventListener(const Rml::String& value, Rml::Element* element) override;
  66. bool has_exited = false;
  67. };
  68. class DemoEventListener : public Rml::EventListener {
  69. public:
  70. DemoEventListener(const Rml::String& value, Rml::Element* element, DemoEventListenerInstancer* instancer) :
  71. value(value), element(element), instancer(instancer)
  72. {}
  73. void ProcessEvent(Rml::Event& /*event*/) override
  74. {
  75. if (value == "exit")
  76. {
  77. // Test replacing the current element. Need to be careful with regard to lifetime issues. The event's
  78. // current element will be destroyed, so we cannot use it after SetInnerRml(). The library should handle
  79. // this case safely internally when propagating the event further.
  80. Element* parent = element->GetParentNode();
  81. parent->SetInnerRML("<button onclick='confirm_exit' onmouseout='cancel_exit' />");
  82. if (Element* child = parent->GetChild(0))
  83. child->Focus();
  84. }
  85. else if (value == "confirm_exit")
  86. {
  87. instancer->has_exited = true;
  88. }
  89. else if (value == "cancel_exit")
  90. {
  91. if (Element* parent = element->GetParentNode())
  92. parent->SetInnerRML("<button id='exit' onclick='exit' />");
  93. }
  94. }
  95. void OnDetach(Rml::Element* /*element*/) override { delete this; }
  96. private:
  97. Rml::String value;
  98. Rml::Element* element;
  99. DemoEventListenerInstancer* instancer;
  100. };
  101. Rml::EventListener* DemoEventListenerInstancer::InstanceEventListener(const Rml::String& value, Rml::Element* element)
  102. {
  103. return new DemoEventListener(value, element, this);
  104. }
  105. TEST_CASE("event_listener.replace_current_element")
  106. {
  107. Context* context = TestsShell::GetContext();
  108. REQUIRE(context);
  109. DemoEventListenerInstancer event_listener_instancer;
  110. Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
  111. ElementDocument* document = context->LoadDocumentFromMemory(document_decorator_rml, "assets/");
  112. REQUIRE(document);
  113. document->Show();
  114. TestsShell::RenderLoop();
  115. context->ProcessMouseMove(32, 32, 0);
  116. TestsShell::RenderLoop();
  117. context->ProcessMouseButtonDown(0, 0);
  118. TestsShell::RenderLoop();
  119. context->ProcessMouseButtonUp(0, 0);
  120. TestsShell::RenderLoop();
  121. bool exit_cancelled = false;
  122. SUBCASE("move_mouse_outside_button")
  123. {
  124. context->ProcessMouseMove(100, 100, 0);
  125. TestsShell::RenderLoop();
  126. exit_cancelled = true;
  127. }
  128. SUBCASE("move_mouse_within_button")
  129. {
  130. context->ProcessMouseMove(33, 33, 0);
  131. TestsShell::RenderLoop();
  132. }
  133. context->ProcessMouseButtonDown(0, 0);
  134. TestsShell::RenderLoop();
  135. context->ProcessMouseButtonUp(0, 0);
  136. TestsShell::RenderLoop();
  137. CHECK(event_listener_instancer.has_exited == !exit_cancelled);
  138. document->Close();
  139. Rml::Factory::RegisterEventListenerInstancer(nullptr);
  140. TestsShell::ShutdownShell();
  141. }