Home | History | Annotate | Download | only in animation
      1 // Copyright 2013 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/animation/tween.h"
      6 
      7 #include <math.h>
      8 
      9 #if defined(OS_WIN)
     10 #include <float.h>
     11 #endif
     12 
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "ui/gfx/test/gfx_util.h"
     15 
     16 namespace gfx {
     17 namespace {
     18 
     19 double next_double(double d) {
     20 #if defined(OS_WIN)
     21   return _nextafter(d, d + 1);
     22 #else
     23   // Step two units of least precision towards positive infinity. On some 32
     24   // bit x86 compilers a single step was not enough due to loss of precision in
     25   // optimized code.
     26   return nextafter(nextafter(d, d + 1), d + 1);
     27 #endif
     28 }
     29 
     30 // Validates that the same interpolations are made as in Blink.
     31 TEST(TweenTest, ColorValueBetween) {
     32   // From blink's AnimatableColorTest.
     33   EXPECT_SKCOLOR_EQ(0xFF00FF00,
     34                   Tween::ColorValueBetween(-10.0, 0xFF00FF00, 0xFF00FF00));
     35   EXPECT_SKCOLOR_EQ(0xFF00FF00,
     36                   Tween::ColorValueBetween(-10.0, 0xFF00FF00, 0xFFFF00FF));
     37   EXPECT_SKCOLOR_EQ(0xFF00FF00,
     38                   Tween::ColorValueBetween(0.0, 0xFF00FF00, 0xFFFF00FF));
     39   EXPECT_SKCOLOR_EQ(0xFF01FE01,
     40                   Tween::ColorValueBetween(1.0 / 255, 0xFF00FF00, 0xFFFF00FF));
     41   EXPECT_SKCOLOR_EQ(0xFF808080,
     42                   Tween::ColorValueBetween(0.5, 0xFF00FF00, 0xFFFF00FF));
     43   EXPECT_SKCOLOR_EQ(
     44       0xFFFE01FE,
     45       Tween::ColorValueBetween(254.0 / 255.0, 0xFF00FF00, 0xFFFF00FF));
     46   EXPECT_SKCOLOR_EQ(0xFFFF00FF,
     47                   Tween::ColorValueBetween(1.0, 0xFF00FF00, 0xFFFF00FF));
     48   EXPECT_SKCOLOR_EQ(0xFFFF00FF,
     49                   Tween::ColorValueBetween(10.0, 0xFF00FF00, 0xFFFF00FF));
     50   EXPECT_SKCOLOR_EQ(0xFF0C253E,
     51                   Tween::ColorValueBetween(3.0 / 16.0, 0xFF001020, 0xFF4080C0));
     52   EXPECT_SKCOLOR_EQ(0x80FF00FF,
     53                   Tween::ColorValueBetween(0.5, 0x0000FF00, 0xFFFF00FF));
     54   EXPECT_SKCOLOR_EQ(0x60AA55AA,
     55                   Tween::ColorValueBetween(0.5, 0x4000FF00, 0x80FF00FF));
     56   EXPECT_SKCOLOR_EQ(0x60FFAAFF,
     57                   Tween::ColorValueBetween(0.5, 0x40FF00FF, 0x80FFFFFF));
     58   EXPECT_SKCOLOR_EQ(0x103060A0,
     59                   Tween::ColorValueBetween(0.5, 0x10204080, 0x104080C0));
     60 }
     61 
     62 // Ensures that each of the 3 integers in [0, 1, 2] ae selected with equal
     63 // weight.
     64 TEST(TweenTest, IntValueBetween) {
     65   EXPECT_EQ(0, Tween::IntValueBetween(0.0, 0, 2));
     66   EXPECT_EQ(0, Tween::IntValueBetween(0.5 / 3.0, 0, 2));
     67   EXPECT_EQ(0, Tween::IntValueBetween(1.0 / 3.0, 0, 2));
     68 
     69   EXPECT_EQ(1, Tween::IntValueBetween(next_double(1.0 / 3.0), 0, 2));
     70   EXPECT_EQ(1, Tween::IntValueBetween(1.5 / 3.0, 0, 2));
     71   EXPECT_EQ(1, Tween::IntValueBetween(2.0 / 3.0, 0, 2));
     72 
     73   EXPECT_EQ(2, Tween::IntValueBetween(next_double(2.0 / 3.0), 0, 2));
     74   EXPECT_EQ(2, Tween::IntValueBetween(2.5 / 3.0, 0, 2));
     75   EXPECT_EQ(2, Tween::IntValueBetween(3.0 / 3.0, 0, 2));
     76 }
     77 
     78 TEST(TweenTest, IntValueBetweenNegative) {
     79   EXPECT_EQ(-2, Tween::IntValueBetween(0.0, -2, 0));
     80   EXPECT_EQ(-2, Tween::IntValueBetween(0.5 / 3.0, -2, 0));
     81   EXPECT_EQ(-2, Tween::IntValueBetween(1.0 / 3.0, -2, 0));
     82 
     83   EXPECT_EQ(-1, Tween::IntValueBetween(next_double(1.0 / 3.0), -2, 0));
     84   EXPECT_EQ(-1, Tween::IntValueBetween(1.5 / 3.0, -2, 0));
     85   EXPECT_EQ(-1, Tween::IntValueBetween(2.0 / 3.0, -2, 0));
     86 
     87   EXPECT_EQ(0, Tween::IntValueBetween(next_double(2.0 / 3.0), -2, 0));
     88   EXPECT_EQ(0, Tween::IntValueBetween(2.5 / 3.0, -2, 0));
     89   EXPECT_EQ(0, Tween::IntValueBetween(3.0 / 3.0, -2, 0));
     90 }
     91 
     92 TEST(TweenTest, IntValueBetweenReverse) {
     93   EXPECT_EQ(2, Tween::IntValueBetween(0.0, 2, 0));
     94   EXPECT_EQ(2, Tween::IntValueBetween(0.5 / 3.0, 2, 0));
     95   EXPECT_EQ(2, Tween::IntValueBetween(1.0 / 3.0, 2, 0));
     96 
     97   EXPECT_EQ(1, Tween::IntValueBetween(next_double(1.0 / 3.0), 2, 0));
     98   EXPECT_EQ(1, Tween::IntValueBetween(1.5 / 3.0, 2, 0));
     99   EXPECT_EQ(1, Tween::IntValueBetween(2.0 / 3.0, 2, 0));
    100 
    101   EXPECT_EQ(0, Tween::IntValueBetween(next_double(2.0 / 3.0), 2, 0));
    102   EXPECT_EQ(0, Tween::IntValueBetween(2.5 / 3.0, 2, 0));
    103   EXPECT_EQ(0, Tween::IntValueBetween(3.0 / 3.0, 2, 0));
    104 }
    105 
    106 TEST(TweenTest, LinearIntValueBetween) {
    107   EXPECT_EQ(0, Tween::LinearIntValueBetween(0.0, 0, 2));
    108   EXPECT_EQ(0, Tween::LinearIntValueBetween(0.5 / 4.0, 0, 2));
    109   EXPECT_EQ(0, Tween::LinearIntValueBetween(0.99 / 4.0, 0, 2));
    110 
    111   EXPECT_EQ(1, Tween::LinearIntValueBetween(1.0 / 4.0, 0, 2));
    112   EXPECT_EQ(1, Tween::LinearIntValueBetween(1.5 / 4.0, 0, 2));
    113   EXPECT_EQ(1, Tween::LinearIntValueBetween(2.0 / 4.0, 0, 2));
    114   EXPECT_EQ(1, Tween::LinearIntValueBetween(2.5 / 4.0, 0, 2));
    115   EXPECT_EQ(1, Tween::LinearIntValueBetween(2.99 / 4.0, 0, 2));
    116 
    117   EXPECT_EQ(2, Tween::LinearIntValueBetween(3.0 / 4.0, 0, 2));
    118   EXPECT_EQ(2, Tween::LinearIntValueBetween(3.5 / 4.0, 0, 2));
    119   EXPECT_EQ(2, Tween::LinearIntValueBetween(4.0 / 4.0, 0, 2));
    120 }
    121 
    122 TEST(TweenTest, LinearIntValueBetweenNegative) {
    123   EXPECT_EQ(-2, Tween::LinearIntValueBetween(0.0, -2, 0));
    124   EXPECT_EQ(-2, Tween::LinearIntValueBetween(0.5 / 4.0, -2, 0));
    125   EXPECT_EQ(-2, Tween::LinearIntValueBetween(0.99 / 4.0, -2, 0));
    126 
    127   EXPECT_EQ(-1, Tween::LinearIntValueBetween(1.0 / 4.0, -2, 0));
    128   EXPECT_EQ(-1, Tween::LinearIntValueBetween(1.5 / 4.0, -2, 0));
    129   EXPECT_EQ(-1, Tween::LinearIntValueBetween(2.0 / 4.0, -2, 0));
    130   EXPECT_EQ(-1, Tween::LinearIntValueBetween(2.5 / 4.0, -2, 0));
    131   EXPECT_EQ(-1, Tween::LinearIntValueBetween(2.99 / 4.0, -2, 0));
    132 
    133   EXPECT_EQ(0, Tween::LinearIntValueBetween(3.0 / 4.0, -2, 0));
    134   EXPECT_EQ(0, Tween::LinearIntValueBetween(3.5 / 4.0, -2, 0));
    135   EXPECT_EQ(0, Tween::LinearIntValueBetween(4.0 / 4.0, -2, 0));
    136 }
    137 
    138 }  // namespace
    139 }  // namespace gfx
    140