2
0

Localization.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/ComputedValues.h>
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Core.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/ElementDocument.h>
  34. #include <RmlUi/Core/StringUtilities.h>
  35. #include <RmlUi/Core/StyleTypes.h>
  36. #include <doctest.h>
  37. using namespace Rml;
  38. static const String document_localization_rml = R"(
  39. <rml>
  40. <head>
  41. </head>
  42. <body>
  43. <div id="parent" lang="en" dir="ltr">
  44. <span id="cell0"/>
  45. <span id="cell1" lang="nl"/>
  46. <span id="cell2" dir="auto"/>
  47. <span id="cell3" lang="ar" dir="rtl"/>
  48. </div>
  49. </body>
  50. </rml>
  51. )";
  52. TEST_CASE("Localization")
  53. {
  54. Context* context = TestsShell::GetContext();
  55. REQUIRE(context);
  56. ElementDocument* document = context->LoadDocumentFromMemory(document_localization_rml);
  57. REQUIRE(document);
  58. document->Show();
  59. Element* parent_element = document->GetElementById("parent");
  60. REQUIRE(parent_element);
  61. Element* cells[4]{};
  62. for (int i = 0; i < 4; ++i)
  63. {
  64. cells[i] = document->GetElementById(CreateString("cell%d", i));
  65. REQUIRE(cells[i]);
  66. }
  67. TestsShell::RenderLoop();
  68. SUBCASE("Language")
  69. {
  70. REQUIRE(document->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "");
  71. REQUIRE(document->GetComputedValues().language() == "");
  72. REQUIRE(parent_element->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "en");
  73. REQUIRE(parent_element->GetComputedValues().language() == "en");
  74. CHECK(cells[0]->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "en");
  75. CHECK(cells[0]->GetComputedValues().language() == "en");
  76. CHECK(cells[1]->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "nl");
  77. CHECK(cells[1]->GetComputedValues().language() == "nl");
  78. CHECK(cells[2]->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "en");
  79. CHECK(cells[2]->GetComputedValues().language() == "en");
  80. CHECK(cells[3]->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "ar");
  81. CHECK(cells[3]->GetComputedValues().language() == "ar");
  82. SUBCASE("Change language")
  83. {
  84. parent_element->SetAttribute("lang", "es");
  85. TestsShell::RenderLoop();
  86. REQUIRE(parent_element->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "es");
  87. REQUIRE(parent_element->GetComputedValues().language() == "es");
  88. CHECK(cells[0]->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "es");
  89. CHECK(cells[0]->GetComputedValues().language() == "es");
  90. CHECK(cells[1]->GetProperty(PropertyId::RmlUi_Language)->Get<String>() == "nl");
  91. CHECK(cells[1]->GetComputedValues().language() == "nl");
  92. }
  93. }
  94. SUBCASE("Direction")
  95. {
  96. REQUIRE(document->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Auto);
  97. REQUIRE(document->GetComputedValues().direction() == Style::Direction::Auto);
  98. REQUIRE(parent_element->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Ltr);
  99. REQUIRE(parent_element->GetComputedValues().direction() == Style::Direction::Ltr);
  100. CHECK(cells[0]->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Ltr);
  101. CHECK(cells[0]->GetComputedValues().direction() == Style::Direction::Ltr);
  102. CHECK(cells[1]->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Ltr);
  103. CHECK(cells[1]->GetComputedValues().direction() == Style::Direction::Ltr);
  104. CHECK(cells[2]->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Auto);
  105. CHECK(cells[2]->GetComputedValues().direction() == Style::Direction::Auto);
  106. CHECK(cells[3]->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Rtl);
  107. CHECK(cells[3]->GetComputedValues().direction() == Style::Direction::Rtl);
  108. SUBCASE("Change direction")
  109. {
  110. parent_element->SetAttribute("dir", "rtl");
  111. TestsShell::RenderLoop();
  112. REQUIRE(parent_element->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Rtl);
  113. REQUIRE(parent_element->GetComputedValues().direction() == Style::Direction::Rtl);
  114. CHECK(cells[0]->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Rtl);
  115. CHECK(cells[0]->GetComputedValues().direction() == Style::Direction::Rtl);
  116. CHECK(cells[2]->GetProperty(PropertyId::RmlUi_Direction)->Get<Style::Direction>() == Style::Direction::Auto);
  117. CHECK(cells[2]->GetComputedValues().direction() == Style::Direction::Auto);
  118. }
  119. }
  120. document->Close();
  121. TestsShell::ShutdownShell();
  122. }