| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /*
- * 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-2023 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 "../Common/TestsShell.h"
- #include <RmlUi/Core/Context.h>
- #include <RmlUi/Core/Element.h>
- #include <RmlUi/Core/ElementDocument.h>
- #include <doctest.h>
- using namespace Rml;
- static const String document_wrapped_image_rml = R"(
- <rml>
- <head>
- <title>Test</title>
- <link type="text/rcss" href="/assets/rml.rcss"/>
- <style>
- body {
- left: 0;
- top: 0;
- right: 0;
- bottom: 0;
- }
- </style>
- </head>
- <body>
- <div>
- <img src="/assets/high_scores_alien_1.tga"/>
- </div>
- </body>
- </rml>
- )";
- TEST_CASE("elementimage.dp_ratio")
- {
- Context* context = TestsShell::GetContext();
- ElementDocument* document = context->LoadDocumentFromMemory(document_wrapped_image_rml, "assets/");
- Element* div = document->GetChild(0);
- Element* img = div->GetChild(0);
- document->Show();
- const float img_width = img->GetClientWidth();
- SUBCASE("basic")
- {
- context->SetDensityIndependentPixelRatio(2.0f);
- context->Update();
- CHECK(img->GetClientWidth() == 2.f * img_width);
- }
- SUBCASE("clone")
- {
- context->SetDensityIndependentPixelRatio(2.0f);
- auto clone_ptr = div->Clone();
- context->SetDensityIndependentPixelRatio(4.f);
- Element* clone_div = As<Element*>(document->AppendChild(std::move(clone_ptr)));
- Element* clone_img = clone_div->GetChild(0);
- context->Update();
- CHECK(clone_img->GetClientWidth() == 4.f * img_width);
- }
- SUBCASE("create_image")
- {
- context->SetDensityIndependentPixelRatio(2.0f);
- ElementPtr new_img_ptr = document->CreateElement("img");
- new_img_ptr->SetAttribute("src", "high_scores_alien_1.tga");
- context->SetDensityIndependentPixelRatio(4.0f);
- Element* new_img = As<Element*>(document->AppendChild(std::move(new_img_ptr)));
- context->Update();
- CHECK(new_img->GetClientWidth() == 4.f * img_width);
- }
- TestsShell::RenderLoop();
- document->Close();
- TestsShell::ShutdownShell();
- }
- static const String document_wrapped_image_rml_ratio_test = R"(
- <rml>
- <head>
- <title>Test</title>
- <style>
- .width-50 { width: 50px; }
- .height-50 { height: 50px; }
- .height-100 { height: 100px; }
- </style>
- </head>
- <body>
- <img src="/test_not_loaded/test_512_256.tga" id="test1" alt="Original size" />
- <img src="/test_not_loaded/test_512_256.tga" id="test2" height="50" alt="Set height to 50, width auto, attribute set" />
- <img src="/test_not_loaded/test_512_256.tga" id="test3" class="height-50" alt="Set height to 50, width auto, css set" />
- <img src="/test_not_loaded/test_512_256.tga" id="test4" width="50" alt="Set width to 50, height auto, attribute set" />
- <img src="/test_not_loaded/test_512_256.tga" id="test5" class="width-50" alt="Set width to 50, height auto, css set" />
- <img src="/test_not_loaded/test_512_256.tga" id="test6" width="50" height="100" alt="Set width to 50 and height to 100, attribute set" />
- <img src="/test_not_loaded/test_512_256.tga" id="test7" class="height-100 width-50" alt="Set width to 50 and height to 100, css set" />
- </body>
- </rml>
- )";
- TEST_CASE("elementimage.preserve_ratio")
- {
- Context* context = TestsShell::GetContext();
- ElementDocument* document = context->LoadDocumentFromMemory(document_wrapped_image_rml_ratio_test, "assets/");
- document->Show();
- // Texture size is hardcoded in test render interface to 512x256
- {
- INFO("original image size");
- Element* img_test1 = document->GetElementById("test1");
- CHECK(img_test1->GetClientWidth() == 512);
- CHECK(img_test1->GetClientHeight() == 256);
- }
- {
- INFO("height only - attribute set");
- Element* img_test2 = document->GetElementById("test2");
- CHECK(img_test2->GetClientWidth() == 100);
- CHECK(img_test2->GetClientHeight() == 50);
- }
- {
- INFO("height only - css set");
- Element* img_test3 = document->GetElementById("test3");
- CHECK(img_test3->GetClientWidth() == 100);
- CHECK(img_test3->GetClientHeight() == 50);
- }
- {
- INFO("width only - attribute set");
- Element* img_test4 = document->GetElementById("test4");
- CHECK(img_test4->GetClientWidth() == 50);
- CHECK(img_test4->GetClientHeight() == 25);
- }
- {
- INFO("width only - css set");
- Element* img_test5 = document->GetElementById("test5");
- CHECK(img_test5->GetClientWidth() == 50);
- CHECK(img_test5->GetClientHeight() == 25);
- }
- {
- INFO("height and width - attribute set");
- Element* img_test6 = document->GetElementById("test6");
- CHECK(img_test6->GetClientWidth() == 50);
- CHECK(img_test6->GetClientHeight() == 100);
- }
- {
- INFO("height and width - css set");
- Element* img_test7 = document->GetElementById("test7");
- CHECK(img_test7->GetClientWidth() == 50);
- CHECK(img_test7->GetClientHeight() == 100);
- }
- TestsShell::RenderLoop();
- document->Close();
- TestsShell::ShutdownShell();
- }
|