/* * 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 #include #include #include using namespace Rml; static const String document_wrapped_image_rml = R"( Test
)"; 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(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(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"( Test Original size Set height to 50, width auto, attribute set Set height to 50, width auto, css set Set width to 50, height auto, attribute set Set width to 50, height auto, css set Set width to 50 and height to 100, attribute set Set width to 50 and height to 100, css set )"; 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(); }