srctree

Robin Linden parent 1d9420fa 73e1d0f2
layout/test: Migrate to etest2

inlinesplit
layout/box_model_test.cpp added: 65, removed: 65, total 0
@@ -1,17 +1,16 @@
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2022 Mikael Larsson <c.mikael.larsson@gmail.com>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "layout/box_model.h"
 
#include "etest/etest.h"
#include "etest/etest2.h"
#include "geom/geom.h"
 
using etest::expect;
 
int main() {
etest::test("BoxModel box models", [] {
etest::Suite s{};
s.add_test("BoxModel box models", [](etest::IActions &a) {
layout::BoxModel box{
.content{.x = 400, .y = 400, .width = 100, .height = 100}, // x: 400-500, y: 400-500
.padding{.left = 100, .right = 100, .top = 100, .bottom = 100}, // x: 300-600, y: 300-600
@@ -19,12 +18,12 @@ int main() {
.margin{.left = 100, .right = 100, .top = 100, .bottom = 100}, // x: 100-800, y: 100-800
};
 
expect(box.padding_box() == geom::Rect{300, 300, 300, 300});
expect(box.border_box() == geom::Rect{200, 200, 500, 500});
expect(box.margin_box() == geom::Rect{100, 100, 700, 700});
a.expect(box.padding_box() == geom::Rect{300, 300, 300, 300});
a.expect(box.border_box() == geom::Rect{200, 200, 500, 500});
a.expect(box.margin_box() == geom::Rect{100, 100, 700, 700});
});
 
etest::test("BoxModel::contains", [] {
s.add_test("BoxModel box models", [](etest::IActions &a) {
layout::BoxModel box{
.content{.x = 400, .y = 400, .width = 100, .height = 100}, // x: 400-500, y: 400-500
.padding{.left = 100, .right = 100, .top = 100, .bottom = 100}, // x: 300-600, y: 300-600
@@ -32,12 +31,12 @@ int main() {
.margin{.left = 100, .right = 100, .top = 100, .bottom = 100}, // x: 100-800, y: 100-800
};
 
expect(box.contains({450, 450})); // Inside content.
expect(box.contains({300, 300})); // Inside padding.
expect(box.contains({650, 250})); // Inside border.
expect(!box.contains({150, 150})); // Inside margin.
expect(!box.contains({90, 90})); // Outside margin.
a.expect(box.contains({450, 450})); // Inside content.
a.expect(box.contains({300, 300})); // Inside padding.
a.expect(box.contains({650, 250})); // Inside border.
a.expect(!box.contains({150, 150})); // Inside margin.
a.expect(!box.contains({90, 90})); // Outside margin.
});
 
return etest::run_all_tests();
return s.run();
}
 
layout/layout_box_test.cpp added: 65, removed: 65, total 0
@@ -10,7 +10,7 @@
#include "css/property_id.h"
#include "dom/dom.h"
#include "dom/xpath.h"
#include "etest/etest.h"
#include "etest/etest2.h"
#include "style/styled_node.h"
 
#include <cstddef>
@@ -19,8 +19,6 @@
#include <utility>
#include <vector>
 
using etest::expect;
using etest::expect_eq;
using namespace std::literals;
 
namespace {
@@ -37,7 +35,8 @@ void set_up_parent_ptrs(style::StyledNode &parent) {
} // namespace
 
int main() {
etest::test("text", [] {
etest::Suite s{};
s.add_test("text", [](etest::IActions &a) {
dom::Node dom_root =
dom::Element{"html", {}, {dom::Element{"body", {}, {dom::Text{"hello"}, dom::Text{"goodbye"}}}}};
 
@@ -69,13 +68,13 @@ int main() {
}}};
 
auto layout_root = layout::create_layout(style_root, 100);
expect(expected_layout == layout_root);
a.expect(expected_layout == layout_root);
 
expect_eq(expected_layout.children.at(0).children.at(0).children.at(0).text(), "hello");
expect_eq(expected_layout.children.at(0).children.at(0).children.at(1).text(), "goodbye");
a.expect_eq(expected_layout.children.at(0).children.at(0).children.at(0).text(), "hello");
a.expect_eq(expected_layout.children.at(0).children.at(0).children.at(1).text(), "goodbye");
});
 
etest::test("box_at_position", [] {
s.add_test("box_at_position", [](etest::IActions &a) {
dom::Node dom = dom::Element{"dummy"};
style::StyledNode style{dom, {{css::PropertyId::Display, "block"}}};
std::vector<layout::LayoutBox> children{
@@ -91,20 +90,20 @@ int main() {
},
};
 
expect(box_at_position(layout, {-1, -1}) == nullptr);
expect(box_at_position(layout, {101, 101}) == nullptr);
a.expect(box_at_position(layout, {-1, -1}) == nullptr);
a.expect(box_at_position(layout, {101, 101}) == nullptr);
 
expect(box_at_position(layout, {100, 100}) == &layout);
expect(box_at_position(layout, {0, 0}) == &layout);
a.expect(box_at_position(layout, {100, 100}) == &layout);
a.expect(box_at_position(layout, {0, 0}) == &layout);
 
// We don't want to end up in anonymous blocks, so this should return its parent.
expect(box_at_position(layout, {31, 31}) == &layout.children[0]);
a.expect(box_at_position(layout, {31, 31}) == &layout.children[0]);
 
expect(box_at_position(layout, {75, 75}) == &layout.children[0]);
expect(box_at_position(layout, {47, 47}) == &layout.children[0].children[1]);
a.expect(box_at_position(layout, {75, 75}) == &layout.children[0]);
a.expect(box_at_position(layout, {47, 47}) == &layout.children[0].children[1]);
});
 
etest::test("xpath", [] {
s.add_test("xpath", [](etest::IActions &a) {
dom::Node html_node = dom::Element{"html"s};
dom::Node div_node = dom::Element{"div"s};
dom::Node p_node = dom::Element{"p"s};
@@ -125,20 +124,20 @@ int main() {
auto layout = layout::create_layout(styled_node, 123).value();
 
// Verify that we have a shady anon-box to deal with in here.
expect_eq(layout.children.size(), std::size_t{2});
a.expect_eq(layout.children.size(), std::size_t{2});
 
auto const &anon_block = layout.children.at(1);
 
using NodeVec = std::vector<layout::LayoutBox const *>;
expect_eq(dom::nodes_by_xpath(layout, "/html"), NodeVec{&layout});
expect_eq(dom::nodes_by_xpath(layout, "/html/div"), NodeVec{&layout.children[0], &anon_block.children[1]});
expect_eq(dom::nodes_by_xpath(layout, "/html/div/"), NodeVec{});
expect_eq(dom::nodes_by_xpath(layout, "/html/div/p"), NodeVec{&anon_block.children[1].children[0]});
expect_eq(dom::nodes_by_xpath(layout, "/htm/div"), NodeVec{});
expect_eq(dom::nodes_by_xpath(layout, "//div"), NodeVec{&layout.children[0], &anon_block.children[1]});
a.expect_eq(dom::nodes_by_xpath(layout, "/html"), NodeVec{&layout});
a.expect_eq(dom::nodes_by_xpath(layout, "/html/div"), NodeVec{&layout.children[0], &anon_block.children[1]});
a.expect_eq(dom::nodes_by_xpath(layout, "/html/div/"), NodeVec{});
a.expect_eq(dom::nodes_by_xpath(layout, "/html/div/p"), NodeVec{&anon_block.children[1].children[0]});
a.expect_eq(dom::nodes_by_xpath(layout, "/htm/div"), NodeVec{});
a.expect_eq(dom::nodes_by_xpath(layout, "//div"), NodeVec{&layout.children[0], &anon_block.children[1]});
});
 
etest::test("to_string", [] {
s.add_test("to_string", [](etest::IActions &a) {
auto body = dom::Element{"body", {}, {dom::Element{"p", {}, {dom::Text{"!!!\n\n!!!"}}}, dom::Element{"p"}}};
dom::Node dom_root = dom::Element{.name{"html"}, .children{std::move(body)}};
 
@@ -183,8 +182,8 @@ int main() {
" inline {0,0,35,25} {0,0,0,0} {0,0,0,0}\n"
" p\n"
" block {0,30,35,0} {5,15,0,0} {0,0,0,0}\n";
expect_eq(to_string(layout::create_layout(style_root, 0).value()), expected);
a.expect_eq(to_string(layout::create_layout(style_root, 0).value()), expected);
});
 
return etest::run_all_tests();
return s.run();
}
 
layout/layout_property_test.cpp added: 65, removed: 65, total 0
@@ -6,7 +6,7 @@
 
#include "css/property_id.h"
#include "dom/dom.h"
#include "etest/etest.h"
#include "etest/etest2.h"
#include "gfx/color.h"
#include "layout/unresolved_value.h"
#include "style/styled_node.h"
@@ -18,11 +18,11 @@
#include <vector>
 
using namespace std::literals;
using etest::expect_eq;
 
namespace {
template<css::PropertyId IdT>
void expect_property_eq(std::optional<std::string> value,
void expect_property_eq(etest::IActions &a,
std::optional<std::string> value,
auto expected,
std::vector<std::pair<css::PropertyId, std::string>> extra_properties = {},
std::source_location const &loc = std::source_location::current()) {
@@ -39,42 +39,44 @@ void expect_property_eq(std::optional<std::string> value,
 
auto layout = layout::create_layout(styled_node, 123);
if (!layout) {
etest::expect(false, std::nullopt, loc);
a.expect(false, std::nullopt, loc);
return;
}
 
etest::expect_eq(layout->get_property<IdT>(), expected, std::nullopt, loc);
a.expect_eq(layout->get_property<IdT>(), expected, std::nullopt, loc);
};
} // namespace
 
int main() {
etest::test("get_property", [] {
etest::Suite s{};
 
s.add_test("get_property", [](etest::IActions &a) {
dom::Node dom_root = dom::Element{.name{"html"}};
auto style_root = style::StyledNode{.node = dom_root, .properties = {{css::PropertyId::Color, "green"}}};
 
auto layout = layout::create_layout(style_root, 0).value();
expect_eq(layout.get_property<css::PropertyId::Color>(), gfx::Color::from_css_name("green"));
expect_eq(layout.get_property<css::PropertyId::BackgroundColor>(), gfx::Color::from_css_name("transparent"));
a.expect_eq(layout.get_property<css::PropertyId::Color>(), gfx::Color::from_css_name("green"));
a.expect_eq(layout.get_property<css::PropertyId::BackgroundColor>(), gfx::Color::from_css_name("transparent"));
});
 
using enum css::PropertyId;
etest::test("border radius", [] {
expect_property_eq<BorderTopLeftRadius>("2em", std::pair{60, 60}, {{FontSize, "30px"}});
expect_property_eq<BorderTopRightRadius>(std::nullopt, std::pair{0, 0});
expect_property_eq<BorderBottomLeftRadius>(std::nullopt, std::pair{0, 0});
expect_property_eq<BorderBottomRightRadius>("10px/3em", std::pair{10, 90}, {{FontSize, "30px"}});
s.add_test("border radius", [](etest::IActions &a) {
expect_property_eq<BorderTopLeftRadius>(a, "2em", std::pair{60, 60}, {{FontSize, "30px"}});
expect_property_eq<BorderTopRightRadius>(a, std::nullopt, std::pair{0, 0});
expect_property_eq<BorderBottomLeftRadius>(a, std::nullopt, std::pair{0, 0});
expect_property_eq<BorderBottomRightRadius>(a, "10px/3em", std::pair{10, 90}, {{FontSize, "30px"}});
});
 
etest::test("width", [] {
expect_property_eq<MinWidth>("13px", layout::UnresolvedValue{"13px"});
expect_property_eq<MinWidth>("auto", layout::UnresolvedValue{"auto"});
s.add_test("width", [](etest::IActions &a) {
expect_property_eq<MinWidth>(a, "13px", layout::UnresolvedValue{"13px"});
expect_property_eq<MinWidth>(a, "auto", layout::UnresolvedValue{"auto"});
 
expect_property_eq<Width>("42px", layout::UnresolvedValue{"42px"});
expect_property_eq<Width>("auto", layout::UnresolvedValue{"auto"});
expect_property_eq<Width>(a, "42px", layout::UnresolvedValue{"42px"});
expect_property_eq<Width>(a, "auto", layout::UnresolvedValue{"auto"});
 
expect_property_eq<MaxWidth>("420px", layout::UnresolvedValue{"420px"});
expect_property_eq<MaxWidth>("none", layout::UnresolvedValue{"none"});
expect_property_eq<MaxWidth>(a, "420px", layout::UnresolvedValue{"420px"});
expect_property_eq<MaxWidth>(a, "none", layout::UnresolvedValue{"none"});
});
 
return etest::run_all_tests();
return s.run();
}