File: axis_transform2d_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 7,015 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/354829279): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ui/gfx/geometry/axis_transform2d.h"

#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/decomposed_transform.h"
#include "ui/gfx/geometry/test/geometry_util.h"
#include "ui/gfx/geometry/transform.h"

namespace gfx {
namespace {

TEST(AxisTransform2dTest, Mapping) {
  AxisTransform2d t(1.25f, Vector2dF(3.75f, 55.f));

  PointF p(150.f, 100.f);
  EXPECT_EQ(PointF(191.25f, 180.f), t.MapPoint(p));
  EXPECT_POINTF_EQ(PointF(117.f, 36.f), t.InverseMapPoint(p));

  RectF r(150.f, 100.f, 22.5f, 37.5f);
  EXPECT_EQ(RectF(191.25f, 180.f, 28.125f, 46.875f), t.MapRect(r));
  EXPECT_RECTF_EQ(RectF(117.f, 36.f, 18.f, 30.f), t.InverseMapRect(r));
}

TEST(AxisTransform2dTest, Scaling) {
  {
    AxisTransform2d t(1.25f, Vector2dF(3.75f, 55.f));
    EXPECT_EQ(AxisTransform2d(1.5625f, Vector2dF(3.75f, 55.f)),
              PreScaleAxisTransform2d(t, 1.25));
    t.PreScale(Vector2dF(1.25f, 1.25f));
    EXPECT_EQ(AxisTransform2d(1.5625f, Vector2dF(3.75f, 55.f)), t);
  }

  {
    AxisTransform2d t(1.25f, Vector2dF(3.75f, 55.f));
    EXPECT_EQ(AxisTransform2d(1.5625f, Vector2dF(4.6875f, 68.75f)),
              PostScaleAxisTransform2d(t, 1.25));
    t.PostScale(Vector2dF(1.25f, 1.25f));
    EXPECT_EQ(AxisTransform2d(1.5625f, Vector2dF(4.6875f, 68.75f)), t);
  }
}

TEST(AxisTransform2dTest, Translating) {
  Vector2dF tr(3.f, -5.f);
  {
    AxisTransform2d t(1.25f, Vector2dF(3.75f, 55.f));
    EXPECT_EQ(AxisTransform2d(1.25f, Vector2dF(7.5f, 48.75f)),
              PreTranslateAxisTransform2d(t, tr));
    t.PreTranslate(tr);
    EXPECT_EQ(AxisTransform2d(1.25f, Vector2dF(7.5f, 48.75f)), t);
  }

  {
    AxisTransform2d t(1.25f, Vector2dF(3.75f, 55.f));
    EXPECT_EQ(AxisTransform2d(1.25f, Vector2dF(6.75f, 50.f)),
              PostTranslateAxisTransform2d(t, tr));
    t.PostTranslate(tr);
    EXPECT_EQ(AxisTransform2d(1.25f, Vector2dF(6.75f, 50.f)), t);
  }
}

TEST(AxisTransform2dTest, Concat) {
  AxisTransform2d pre(1.25f, Vector2dF(3.75f, 55.f));
  AxisTransform2d post(0.5f, Vector2dF(10.f, 5.f));
  AxisTransform2d expectation(0.625f, Vector2dF(11.875f, 32.5f));
  EXPECT_EQ(expectation, ConcatAxisTransform2d(post, pre));

  AxisTransform2d post_concat = pre;
  post_concat.PostConcat(post);
  EXPECT_EQ(expectation, post_concat);

  AxisTransform2d pre_concat = post;
  pre_concat.PreConcat(pre);
  EXPECT_EQ(expectation, pre_concat);
}

TEST(AxisTransform2dTest, Inverse) {
  AxisTransform2d t(1.25f, Vector2dF(3.75f, 55.f));
  AxisTransform2d inv_inplace = t;
  inv_inplace.Invert();
  AxisTransform2d inv_out_of_place = InvertAxisTransform2d(t);

  EXPECT_AXIS_TRANSFORM2D_EQ(inv_inplace, inv_out_of_place);
  EXPECT_AXIS_TRANSFORM2D_EQ(AxisTransform2d(),
                             ConcatAxisTransform2d(t, inv_inplace));
  EXPECT_AXIS_TRANSFORM2D_EQ(AxisTransform2d(),
                             ConcatAxisTransform2d(inv_inplace, t));
}

TEST(AxisTransform2dTest, ClampOutput) {
  double entries[][2] = {
      // The first entry is used to initialize the transform.
      // The second entry is used to initialize the object to be mapped.
      {std::numeric_limits<float>::max(),
       std::numeric_limits<float>::infinity()},
      {1, std::numeric_limits<float>::infinity()},
      {-1, std::numeric_limits<float>::infinity()},
      {1, -std::numeric_limits<float>::infinity()},
      {
          std::numeric_limits<float>::max(),
          std::numeric_limits<float>::max(),
      },
      {
          std::numeric_limits<float>::lowest(),
          -std::numeric_limits<float>::infinity(),
      },
  };

  for (double* entry : entries) {
    const float mv = entry[0];
    const float factor = entry[1];

    auto is_valid_point = [&](const PointF& p) -> bool {
      return std::isfinite(p.x()) && std::isfinite(p.y());
    };
    auto is_valid_rect = [&](const RectF& r) -> bool {
      return is_valid_point(r.origin()) && std::isfinite(r.width()) &&
             std::isfinite(r.height());
    };

    auto test = [&](const AxisTransform2d& m) {
      SCOPED_TRACE(base::StringPrintf("m: %s factor: %lg", m.ToString().c_str(),
                                      factor));
      auto p = m.MapPoint(PointF(factor, factor));
      EXPECT_TRUE(is_valid_point(p)) << p.ToString();

      // AxisTransform2d::MapRect() requires non-negative scales.
      if (m.scale().x() >= 0 && m.scale().y() >= 0) {
        auto r = m.MapRect(RectF(factor, factor, factor, factor));
        EXPECT_TRUE(is_valid_rect(r)) << r.ToString();
      }
    };

    test(AxisTransform2d::FromScaleAndTranslation(Vector2dF(mv, mv),
                                                  Vector2dF(mv, mv)));
    test(AxisTransform2d::FromScaleAndTranslation(Vector2dF(mv, mv),
                                                  Vector2dF(0, 0)));
    test(AxisTransform2d::FromScaleAndTranslation(Vector2dF(1, 1),
                                                  Vector2dF(mv, mv)));
  }
}

TEST(AxisTransform2dTest, Decompose) {
  {
    auto transform = AxisTransform2d::FromScaleAndTranslation(
        Vector2dF(2.5, -3.75), Vector2dF(4.25, -5.5));
    DecomposedTransform decomp = transform.Decompose();
    EXPECT_DECOMPOSED_TRANSFORM_EQ((DecomposedTransform{{4.25, -5.5, 0},
                                                        {2.5, -3.75, 1},
                                                        {0, 0, 0},
                                                        {0, 0, 0, 1},
                                                        {0, 0, 0, 1}}),
                                   decomp);
    EXPECT_EQ(Transform(transform), Transform::Compose(decomp));
  }
  {
    auto transform = AxisTransform2d::FromScaleAndTranslation(
        Vector2dF(-2.5, -3.75), Vector2dF(4.25, -5.5));
    DecomposedTransform decomp = transform.Decompose();
    EXPECT_DECOMPOSED_TRANSFORM_EQ((DecomposedTransform{{4.25, -5.5, 0},
                                                        {2.5, 3.75, 1},
                                                        {0, 0, 0},
                                                        {0, 0, 0, 1},
                                                        {0, 0, 1, 0}}),
                                   decomp);
    EXPECT_EQ(Transform(transform), Transform::Compose(decomp));
  }
  {
    auto transform =
        AxisTransform2d::FromScaleAndTranslation(Vector2dF(), Vector2dF());
    DecomposedTransform decomp = transform.Decompose();
    EXPECT_DECOMPOSED_TRANSFORM_EQ(
        (DecomposedTransform{
            {0, 0, 0}, {0, 0, 1}, {0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 1}}),
        decomp);
    EXPECT_EQ(Transform(transform), Transform::Compose(decomp));
  }
}

}  // namespace
}  // namespace gfx