WidgetTextInput.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/TestsInterface.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/Elements/ElementFormControlTextArea.h>
  34. #include <RmlUi/Core/Types.h>
  35. #include <doctest.h>
  36. #include <nanobench.h>
  37. using namespace ankerl;
  38. using namespace Rml;
  39. static const String document_rml = R"(
  40. <rml>
  41. <head>
  42. <link type="text/rcss" href="/../Tests/Data/style.rcss"/>
  43. <title>Benchmark Sample</title>
  44. <style>
  45. body {
  46. width: 800px;
  47. height: 600px;
  48. }
  49. textarea {
  50. width: 500px;
  51. height: 300px;
  52. border: 1px #000;
  53. background: #fff;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <textarea id="textarea">Hello, World!</textarea>
  59. </body>
  60. </rml>
  61. )";
  62. TEST_CASE("WidgetTextInput")
  63. {
  64. Context* context = TestsShell::GetContext();
  65. REQUIRE(context);
  66. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  67. REQUIRE(document);
  68. document->Show();
  69. auto el = rmlui_dynamic_cast<ElementFormControlTextArea*>(document->GetElementById("textarea"));
  70. REQUIRE(el);
  71. auto IncrementTime = [system_interface = TestsShell::GetTestsSystemInterface(), t = 0.0]() mutable {
  72. constexpr double dt = 0.5;
  73. t += dt;
  74. system_interface->SetTime(t);
  75. };
  76. struct TestCase {
  77. String name;
  78. String base;
  79. };
  80. const TestCase test_cases[] = {
  81. {"ascii", "abc"},
  82. {"emoji", "😍🌐😎"},
  83. {"japanese", "こんに"},
  84. };
  85. nanobench::Bench bench;
  86. bench.timeUnit(std::chrono::microseconds(1), "us");
  87. bench.relative(true);
  88. bench.complexityN(1);
  89. SUBCASE("FormatText")
  90. {
  91. bench.title("WidgetTextInput.FormatText");
  92. for (const TestCase& test_case : test_cases)
  93. {
  94. for (int num_character_repeats : {10, 20, 50, 100, 200})
  95. {
  96. String value;
  97. for (int i = 0; i < num_character_repeats; i++)
  98. value += test_case.base;
  99. bench.complexityN(num_character_repeats).epochs(1).epochIterations(num_character_repeats >= 100 ? 1 : 0).run(test_case.name, [&] {
  100. el->SetValue(value);
  101. context->Update();
  102. });
  103. }
  104. }
  105. }
  106. SUBCASE("CalculateCharacterIndex")
  107. {
  108. bench.title("WidgetTextInput.CalculateCharacterIndex");
  109. el->SetWordWrap(false);
  110. context->Update();
  111. for (const TestCase& test_case : test_cases)
  112. {
  113. for (int num_character_repeats : {20, 50, 100, 200, 500})
  114. {
  115. String value;
  116. for (int i = 0; i < num_character_repeats; i++)
  117. value += test_case.base;
  118. el->SetValue(value);
  119. el->Focus();
  120. context->ProcessKeyDown(Input::KI_END, 0);
  121. context->ProcessKeyUp(Input::KI_END, 0);
  122. context->Update();
  123. bench.complexityN(num_character_repeats).epochs(1).epochIterations(num_character_repeats >= 100 ? 1 : 0).run(test_case.name, [&] {
  124. context->ProcessMouseMove(250, 50, 0);
  125. context->ProcessMouseButtonDown(0, 0);
  126. context->ProcessMouseMove(350, 50, 0);
  127. context->ProcessMouseButtonUp(0, 0);
  128. IncrementTime();
  129. context->Update();
  130. });
  131. // Sanity check that the above produces a selection as intended.
  132. int selection_start = 0, selection_end = 0;
  133. el->GetSelection(&selection_start, &selection_end, nullptr);
  134. REQUIRE(selection_end - selection_start > 0);
  135. }
  136. }
  137. }
  138. TestsShell::RenderLoop();
  139. document->Close();
  140. }