TestMatrix.cpp 4.12 KB
Newer Older
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"
#include "mozilla/gfx/Matrix.h"

using namespace mozilla;
using namespace mozilla::gfx;

TEST(Matrix, TransformAndClipRect)
{
  Rect c(100, 100, 100, 100);
  Matrix4x4 m;
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 20, 20), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 50, 20, 20), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 250, 20, 20), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 250, 20, 20), c).IsEmpty());

  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 100, 20), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 100, 20), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 250, 100, 20), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 250, 100, 20), c).IsEmpty());

  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 20, 100), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 150, 20, 100), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 50, 20, 100), c).IsEmpty());
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(250, 150, 20, 100), c).IsEmpty());

32
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 100, 100), c)
33
                  .IsEqualInterior(Rect(100, 100, 50, 50)));
34
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 100, 100), c)
35
                  .IsEqualInterior(Rect(150, 100, 50, 50)));
36
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 150, 100, 100), c)
37
                  .IsEqualInterior(Rect(150, 150, 50, 50)));
38
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 150, 100, 100), c)
39
40
                  .IsEqualInterior(Rect(100, 150, 50, 50)));

41
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(110, 110, 80, 80), c)
42
43
                  .IsEqualInterior(Rect(110, 110, 80, 80)));

44
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 200, 200), c)
45
46
                  .IsEqualInterior(Rect(100, 100, 100, 100)));

47
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 200, 100), c)
48
                  .IsEqualInterior(Rect(100, 100, 100, 50)));
49
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 150, 200, 100), c)
50
                  .IsEqualInterior(Rect(100, 150, 100, 50)));
51
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(50, 50, 100, 200), c)
52
                  .IsEqualInterior(Rect(100, 100, 50, 100)));
53
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 100, 200), c)
54
                  .IsEqualInterior(Rect(150, 100, 50, 100)));
55
56
57
58
59

  Matrix4x4 m2 = Matrix4x4::From2D(Matrix(22.68, 0, 0, 12, 16, 164));
  EXPECT_TRUE(
      m2.TransformAndClipBounds(Rect(0, 0, 100, 100), Rect(1024, 1024, 0, 0))
          .IsEmpty());
60
61
62
63
64
65
66
67

  // Empty rectangles should still have meaningful corners.
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 50, 0, 200), c)
                  .IsEqualEdges(Rect(150, 100, 0, 100)));
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 150, 0, 0), c)
                  .IsEqualEdges(Rect(150, 150, 0, 0)));
  EXPECT_TRUE(m.TransformAndClipBounds(Rect(150, 100, 300, 0), c)
                  .IsEqualEdges(Rect(150, 100, 50, 0)));
68
}
69
70
71

TEST(Matrix4x4Flagged, Mult)
{
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  Matrix4x4Flagged simple =
      Matrix4x4::Translation(Point(42, 42)) * Matrix4x4::Scaling(3, 3, 1);
  // For the general matrix, put a value in every field to make sure
  // nothing gets dropped.
  Matrix4x4 general(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2);

  // Use Matrix4x4::operator*(Matrix4x4).
  // For the purposes of this test, assume that's correct.
  Matrix4x4Flagged realResult = Matrix4x4Flagged(simple.GetMatrix() * general);

  // Check that Matrix4x4Flagged::operator*(Matrix4x4Flagged) produces the same
  // result.
  Matrix4x4Flagged flaggedResult = simple * Matrix4x4Flagged(general);
  EXPECT_EQ(realResult, flaggedResult);

  // Check that Matrix4x4Flagged::operator*(Matrix4x4) produces the same result.
  Matrix4x4Flagged mixedResult = simple * general;
  EXPECT_EQ(realResult, mixedResult);
90
}