Home | History | Annotate | Download | only in gfx
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/gfx/point_base.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 #include "ui/gfx/point.h"
     10 #include "ui/gfx/point_conversions.h"
     11 #include "ui/gfx/point_f.h"
     12 
     13 namespace gfx {
     14 
     15 namespace {
     16 
     17 int TestPointF(const PointF& p) {
     18   return p.x();
     19 }
     20 
     21 }  // namespace
     22 
     23 TEST(PointTest, ToPointF) {
     24   // Check that implicit conversion from integer to float compiles.
     25   Point a(10, 20);
     26   float x = TestPointF(a);
     27   EXPECT_EQ(x, a.x());
     28 
     29   PointF b(10, 20);
     30   EXPECT_EQ(a, b);
     31   EXPECT_EQ(b, a);
     32 }
     33 
     34 TEST(PointTest, IsOrigin) {
     35   EXPECT_FALSE(Point(1, 0).IsOrigin());
     36   EXPECT_FALSE(Point(0, 1).IsOrigin());
     37   EXPECT_FALSE(Point(1, 2).IsOrigin());
     38   EXPECT_FALSE(Point(-1, 0).IsOrigin());
     39   EXPECT_FALSE(Point(0, -1).IsOrigin());
     40   EXPECT_FALSE(Point(-1, -2).IsOrigin());
     41   EXPECT_TRUE(Point(0, 0).IsOrigin());
     42 
     43   EXPECT_FALSE(PointF(0.1f, 0).IsOrigin());
     44   EXPECT_FALSE(PointF(0, 0.1f).IsOrigin());
     45   EXPECT_FALSE(PointF(0.1f, 2).IsOrigin());
     46   EXPECT_FALSE(PointF(-0.1f, 0).IsOrigin());
     47   EXPECT_FALSE(PointF(0, -0.1f).IsOrigin());
     48   EXPECT_FALSE(PointF(-0.1f, -2).IsOrigin());
     49   EXPECT_TRUE(PointF(0, 0).IsOrigin());
     50 }
     51 
     52 TEST(PointTest, VectorArithmetic) {
     53   Point a(1, 5);
     54   Vector2d v1(3, -3);
     55   Vector2d v2(-8, 1);
     56 
     57   static const struct {
     58     Point expected;
     59     Point actual;
     60   } tests[] = {
     61     { Point(4, 2), a + v1 },
     62     { Point(-2, 8), a - v1 },
     63     { a, a - v1 + v1 },
     64     { a, a + v1 - v1 },
     65     { a, a + Vector2d() },
     66     { Point(12, 1), a + v1 - v2 },
     67     { Point(-10, 9), a - v1 + v2 }
     68   };
     69 
     70   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i)
     71     EXPECT_EQ(tests[i].expected.ToString(), tests[i].actual.ToString());
     72 }
     73 
     74 TEST(PointTest, OffsetFromPoint) {
     75   Point a(1, 5);
     76   Point b(-20, 8);
     77   EXPECT_EQ(Vector2d(-20 - 1, 8 - 5).ToString(), (b - a).ToString());
     78 }
     79 
     80 TEST(PointTest, ToRoundedPoint) {
     81   EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0, 0)));
     82   EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.0001f, 0.0001f)));
     83   EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.4999f, 0.4999f)));
     84   EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.5f, 0.5f)));
     85   EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.9999f, 0.9999f)));
     86 
     87   EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10, 10)));
     88   EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.0001f, 10.0001f)));
     89   EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.4999f, 10.4999f)));
     90   EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.5f, 10.5f)));
     91   EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.9999f, 10.9999f)));
     92 
     93   EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10, -10)));
     94   EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.0001f, -10.0001f)));
     95   EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.4999f, -10.4999f)));
     96   EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.5f, -10.5f)));
     97   EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.9999f, -10.9999f)));
     98 }
     99 
    100 TEST(PointTest, Scale) {
    101   EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2).ToString());
    102   EXPECT_EQ(PointF().ToString(), ScalePoint(Point(), 2, 2).ToString());
    103 
    104   EXPECT_EQ(PointF(2, -2).ToString(),
    105             ScalePoint(Point(1, -1), 2).ToString());
    106   EXPECT_EQ(PointF(2, -2).ToString(),
    107             ScalePoint(Point(1, -1), 2, 2).ToString());
    108 
    109   PointF zero;
    110   PointF one(1, -1);
    111 
    112   zero.Scale(2);
    113   zero.Scale(3, 1.5);
    114 
    115   one.Scale(2);
    116   one.Scale(3, 1.5);
    117 
    118   EXPECT_EQ(PointF().ToString(), zero.ToString());
    119   EXPECT_EQ(PointF(6, -3).ToString(), one.ToString());
    120 }
    121 
    122 TEST(PointTest, ClampPoint) {
    123   Point a;
    124 
    125   a = Point(3, 5);
    126   EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
    127   a.SetToMax(Point(2, 4));
    128   EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
    129   a.SetToMax(Point(3, 5));
    130   EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
    131   a.SetToMax(Point(4, 2));
    132   EXPECT_EQ(Point(4, 5).ToString(), a.ToString());
    133   a.SetToMax(Point(8, 10));
    134   EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
    135 
    136   a.SetToMin(Point(9, 11));
    137   EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
    138   a.SetToMin(Point(8, 10));
    139   EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
    140   a.SetToMin(Point(11, 9));
    141   EXPECT_EQ(Point(8, 9).ToString(), a.ToString());
    142   a.SetToMin(Point(7, 11));
    143   EXPECT_EQ(Point(7, 9).ToString(), a.ToString());
    144   a.SetToMin(Point(3, 5));
    145   EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
    146 }
    147 
    148 TEST(PointTest, ClampPointF) {
    149   PointF a;
    150 
    151   a = PointF(3.5f, 5.5f);
    152   EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
    153   a.SetToMax(PointF(2.5f, 4.5f));
    154   EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
    155   a.SetToMax(PointF(3.5f, 5.5f));
    156   EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
    157   a.SetToMax(PointF(4.5f, 2.5f));
    158   EXPECT_EQ(PointF(4.5f, 5.5f).ToString(), a.ToString());
    159   a.SetToMax(PointF(8.5f, 10.5f));
    160   EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
    161 
    162   a.SetToMin(PointF(9.5f, 11.5f));
    163   EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
    164   a.SetToMin(PointF(8.5f, 10.5f));
    165   EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
    166   a.SetToMin(PointF(11.5f, 9.5f));
    167   EXPECT_EQ(PointF(8.5f, 9.5f).ToString(), a.ToString());
    168   a.SetToMin(PointF(7.5f, 11.5f));
    169   EXPECT_EQ(PointF(7.5f, 9.5f).ToString(), a.ToString());
    170   a.SetToMin(PointF(3.5f, 5.5f));
    171   EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
    172 }
    173 
    174 }  // namespace gfx
    175