// Copyright 2023 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "third_party/blink/renderer/core/paint/line_oriented_rect.h" #include "third_party/blink/renderer/core/layout/geometry/physical_rect.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/blink/renderer/core/testing/core_unit_test_helper.h" namespace blink { // TODO: // What is this functions conceptual purpose? // It is only being used within the context of MaybeNotAtBoxOrigin // return EnclosingRect(rotated rect) TEST(LineOrientedRectTest, EnclosingRect) { gfx::RectF r(0, 10, 20, 30); LineOrientedRect lor = LineOrientedRect::EnclosingRect(r); EXPECT_EQ(lor.offset.x, 0) << ""; EXPECT_EQ(lor.offset.y, 10) << ""; EXPECT_EQ(lor.size.inline_size, 20) << ""; EXPECT_EQ(lor.size.block_size, 30) << ""; } TEST(LineOrientedRectTest, AtBoxOrigin) { PhysicalRect r(0, 0, 20, 30); LineOrientedRect lor = LineOrientedRect::AtBoxOrigin(r, true); EXPECT_EQ(lor.offset.x, 0) << ""; EXPECT_EQ(lor.offset.y, 0) << ""; EXPECT_EQ(lor.size.inline_size, 20) << ""; EXPECT_EQ(lor.size.block_size, 30) << ""; LineOrientedRect lor_veritcal = LineOrientedRect::AtBoxOrigin(r, false); EXPECT_EQ(lor_veritcal.offset.x, 0) << ""; EXPECT_EQ(lor_veritcal.offset.y, 0) << ""; EXPECT_EQ(lor_veritcal.size.inline_size, 30) << ""; EXPECT_EQ(lor_veritcal.size.block_size, 20) << ""; } // TODO: see "Fails" // This test fails, because LineRotation does not to what I expect it to do when // the WritingMode is kHorizontalTb -- I'd expect it to return the idenfity TEST(LineOrientedRectTest, MaybeNotAtBoxOrigin_kHorizontalTB) { PhysicalRect r(1000, 10000, 100, 10); const WritingMode writing_mode = WritingMode::kHorizontalTb; const bool is_horizontal = IsHorizontalWritingMode(writing_mode); const LineOrientedRect rotated_box = LineOrientedRect::AtBoxOrigin(r, is_horizontal); absl::optional rotation = LineRotation(rotated_box, writing_mode); EXPECT_EQ(rotation, AffineTransform()); // Left half of original box r PhysicalRect highlight(1000, 10000, 50, 10); LineOrientedRect rotated = LineOrientedRect::MaybeNotAtBoxOrigin(highlight, rotation); EXPECT_EQ(rotated.offset.x, 1000) << ""; EXPECT_EQ(rotated.offset.y, 10000) << ""; EXPECT_EQ(rotated.size.inline_size, 50) << ""; EXPECT_EQ(rotated.size.block_size, 10) << ""; // Right half of original box r PhysicalRect highlight2(1050, 10000, 50, 10); LineOrientedRect rotated2 = LineOrientedRect::MaybeNotAtBoxOrigin(highlight2, rotation); EXPECT_EQ(rotated2.offset.x, 1050) << ""; EXPECT_EQ(rotated2.offset.y, 10000) << ""; EXPECT_EQ(rotated2.size.inline_size, 50) << ""; EXPECT_EQ(rotated2.size.block_size, 10) << ""; } void Dump(const LineOrientedRect& rect) { fprintf(stderr, ">>> %sx%s@(%s,%s)\n", rect.Width().ToString().Utf8().c_str(), rect.Height().ToString().Utf8().c_str(), rect.X().ToString().Utf8().c_str(), rect.Y().ToString().Utf8().c_str()); } TEST(LineOrientedRectTest, MaybeNotAtBoxOrigin_kSidewaysLr) { PhysicalRect r(1000, 10000, 10, 100); const WritingMode writing_mode = WritingMode::kSidewaysLr; const bool is_horizontal = IsHorizontalWritingMode(writing_mode); const LineOrientedRect rotated_box = LineOrientedRect::AtBoxOrigin(r, is_horizontal); Dump(rotated_box); absl::optional rotation = LineRotation(rotated_box, writing_mode); fprintf(stderr, ">>> %s\n", rotation->ToString(true).Utf8().c_str()); EXPECT_EQ(rotation, AffineTransform(0, -1, 1, 0, -9000, 11010)); // Top half of original box r PhysicalRect highlight(1000, 10000, 10, 50); LineOrientedRect rotated = LineOrientedRect::MaybeNotAtBoxOrigin(highlight, rotation); Dump(rotated); EXPECT_EQ(rotated.offset.x, 1050) << ""; // Fails, 960 (1050-100+10) EXPECT_EQ(rotated.offset.y, 10000) << ""; EXPECT_EQ(rotated.size.inline_size, 50) << ""; EXPECT_EQ(rotated.size.block_size, 10) << ""; // Bottom half of original box r PhysicalRect highlight2(1000, 10050, 10, 50); LineOrientedRect rotated2 = LineOrientedRect::MaybeNotAtBoxOrigin(highlight2, rotation); Dump(rotated2); EXPECT_EQ(rotated2.offset.x, 1000) << ""; // Fails, 910 (1000-100+10) EXPECT_EQ(rotated2.offset.y, 10000) << ""; EXPECT_EQ(rotated2.size.inline_size, 50) << ""; EXPECT_EQ(rotated2.size.block_size, 10) << ""; } // TODO: See "fails" // To get the expectation values, I did the following: // 1. Drew a box on graph paper (A: (0, 0), B: (0, 30), C: (20, 30), D: (20, 0)) // 2. Rotated by 90 around 0, 0 // 3. Translated until point B is at (0, 0), which is a translation in the x by 30 // SO, the rotation I expect is: ("translation(30,0), scale(1,1), angle(90deg), skewxy(0)") // BUT, I got: ("translation(20,0), scale(1,1), angle(90deg), skewxy(0)") // 4. Used the same translation steps for the highlight and highlight2 boxes TEST(LineOrientedRectTest, MaybeNotAtBoxOrigin_kVerticalRl) { PhysicalRect r(1000, 10000, 10, 100); const WritingMode writing_mode = WritingMode::kVerticalRl; const bool is_horizontal = IsHorizontalWritingMode(writing_mode); const LineOrientedRect rotated_box = LineOrientedRect::AtBoxOrigin(r, is_horizontal); Dump(rotated_box); absl::optional rotation = LineRotation(rotated_box, writing_mode); fprintf(stderr, ">>> %s\n", rotation->ToString(true).Utf8().c_str()); EXPECT_EQ(rotation, AffineTransform(0, 1, -1, 0, 11010, 9000)); // Top half of original box r PhysicalRect highlight(1000, 10000, 10, 50); LineOrientedRect rotated = LineOrientedRect::MaybeNotAtBoxOrigin(highlight, rotation); Dump(rotated); EXPECT_EQ(rotated.offset.x, 1000) << ""; EXPECT_EQ(rotated.offset.y, 10000) << ""; EXPECT_EQ(rotated.size.inline_size, 50) << ""; EXPECT_EQ(rotated.size.block_size, 10) << ""; // Bottom half of original box r PhysicalRect highlight2(1000, 10050, 10, 50); LineOrientedRect rotated2 = LineOrientedRect::MaybeNotAtBoxOrigin(highlight2, rotation); Dump(rotated2); EXPECT_EQ(rotated2.offset.x, 1050) << ""; EXPECT_EQ(rotated2.offset.y, 10000) << ""; EXPECT_EQ(rotated2.size.inline_size, 50) << ""; EXPECT_EQ(rotated2.size.block_size, 10) << ""; } } // namespace blink