Home | History | Annotate | Download | only in animation
      1 // Copyright 2011 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 "cc/animation/timing_function.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace cc {
     10 namespace {
     11 
     12 TEST(TimingFunctionTest, CubicBezierTimingFunction) {
     13   scoped_ptr<CubicBezierTimingFunction> function =
     14       CubicBezierTimingFunction::Create(0.25, 0.0, 0.75, 1.0);
     15 
     16   double epsilon = 0.00015;
     17 
     18   EXPECT_NEAR(function->GetValue(0), 0, epsilon);
     19   EXPECT_NEAR(function->GetValue(0.05), 0.01136, epsilon);
     20   EXPECT_NEAR(function->GetValue(0.1), 0.03978, epsilon);
     21   EXPECT_NEAR(function->GetValue(0.15), 0.079780, epsilon);
     22   EXPECT_NEAR(function->GetValue(0.2), 0.12803, epsilon);
     23   EXPECT_NEAR(function->GetValue(0.25), 0.18235, epsilon);
     24   EXPECT_NEAR(function->GetValue(0.3), 0.24115, epsilon);
     25   EXPECT_NEAR(function->GetValue(0.35), 0.30323, epsilon);
     26   EXPECT_NEAR(function->GetValue(0.4), 0.36761, epsilon);
     27   EXPECT_NEAR(function->GetValue(0.45), 0.43345, epsilon);
     28   EXPECT_NEAR(function->GetValue(0.5), 0.5, epsilon);
     29   EXPECT_NEAR(function->GetValue(0.6), 0.63238, epsilon);
     30   EXPECT_NEAR(function->GetValue(0.65), 0.69676, epsilon);
     31   EXPECT_NEAR(function->GetValue(0.7), 0.75884, epsilon);
     32   EXPECT_NEAR(function->GetValue(0.75), 0.81764, epsilon);
     33   EXPECT_NEAR(function->GetValue(0.8), 0.87196, epsilon);
     34   EXPECT_NEAR(function->GetValue(0.85), 0.92021, epsilon);
     35   EXPECT_NEAR(function->GetValue(0.9), 0.96021, epsilon);
     36   EXPECT_NEAR(function->GetValue(0.95), 0.98863, epsilon);
     37   EXPECT_NEAR(function->GetValue(1), 1, epsilon);
     38 }
     39 
     40 // Tests that the bezier timing function works with knots with y not in (0, 1).
     41 TEST(TimingFunctionTest, CubicBezierTimingFunctionUnclampedYValues) {
     42   scoped_ptr<CubicBezierTimingFunction> function =
     43       CubicBezierTimingFunction::Create(0.5, -1.0, 0.5, 2.0);
     44 
     45   double epsilon = 0.00015;
     46 
     47   EXPECT_NEAR(function->GetValue(0.0), 0.0, epsilon);
     48   EXPECT_NEAR(function->GetValue(0.05), -0.08954, epsilon);
     49   EXPECT_NEAR(function->GetValue(0.1), -0.15613, epsilon);
     50   EXPECT_NEAR(function->GetValue(0.15), -0.19641, epsilon);
     51   EXPECT_NEAR(function->GetValue(0.2), -0.20651, epsilon);
     52   EXPECT_NEAR(function->GetValue(0.25), -0.18232, epsilon);
     53   EXPECT_NEAR(function->GetValue(0.3), -0.11992, epsilon);
     54   EXPECT_NEAR(function->GetValue(0.35), -0.01672, epsilon);
     55   EXPECT_NEAR(function->GetValue(0.4), 0.12660, epsilon);
     56   EXPECT_NEAR(function->GetValue(0.45), 0.30349, epsilon);
     57   EXPECT_NEAR(function->GetValue(0.5), 0.50000, epsilon);
     58   EXPECT_NEAR(function->GetValue(0.55), 0.69651, epsilon);
     59   EXPECT_NEAR(function->GetValue(0.6), 0.87340, epsilon);
     60   EXPECT_NEAR(function->GetValue(0.65), 1.01672, epsilon);
     61   EXPECT_NEAR(function->GetValue(0.7), 1.11992, epsilon);
     62   EXPECT_NEAR(function->GetValue(0.75), 1.18232, epsilon);
     63   EXPECT_NEAR(function->GetValue(0.8), 1.20651, epsilon);
     64   EXPECT_NEAR(function->GetValue(0.85), 1.19641, epsilon);
     65   EXPECT_NEAR(function->GetValue(0.9), 1.15613, epsilon);
     66   EXPECT_NEAR(function->GetValue(0.95), 1.08954, epsilon);
     67   EXPECT_NEAR(function->GetValue(1.0), 1.0, epsilon);
     68 }
     69 
     70 TEST(TimingFunctionTest, CubicBezierTimingFunctionRange) {
     71   double epsilon = 0.00015;
     72   float min, max;
     73 
     74   // Derivative is a constant.
     75   scoped_ptr<CubicBezierTimingFunction> function =
     76       CubicBezierTimingFunction::Create(0.25, (1.0 / 3.0), 0.75, (2.0 / 3.0));
     77   function->Range(&min, &max);
     78   EXPECT_EQ(0.f, min);
     79   EXPECT_EQ(1.f, max);
     80 
     81   // Derivative is linear.
     82   function = CubicBezierTimingFunction::Create(0.25, -0.5, 0.75, (-1.0 / 6.0));
     83   function->Range(&min, &max);
     84   EXPECT_NEAR(min, -0.225, epsilon);
     85   EXPECT_EQ(1.f, max);
     86 
     87   // Derivative has no real roots.
     88   function = CubicBezierTimingFunction::Create(0.25, 0.25, 0.75, 0.5);
     89   function->Range(&min, &max);
     90   EXPECT_EQ(0.f, min);
     91   EXPECT_EQ(1.f, max);
     92 
     93   // Derivative has exactly one real root.
     94   function = CubicBezierTimingFunction::Create(0.0, 1.0, 1.0, 0.0);
     95   function->Range(&min, &max);
     96   EXPECT_EQ(0.f, min);
     97   EXPECT_EQ(1.f, max);
     98 
     99   // Derivative has one root < 0 and one root > 1.
    100   function = CubicBezierTimingFunction::Create(0.25, 0.1, 0.75, 0.9);
    101   function->Range(&min, &max);
    102   EXPECT_EQ(0.f, min);
    103   EXPECT_EQ(1.f, max);
    104 
    105   // Derivative has two roots in [0,1].
    106   function = CubicBezierTimingFunction::Create(0.25, 2.5, 0.75, 0.5);
    107   function->Range(&min, &max);
    108   EXPECT_EQ(0.f, min);
    109   EXPECT_NEAR(max, 1.28818, epsilon);
    110   function = CubicBezierTimingFunction::Create(0.25, 0.5, 0.75, -1.5);
    111   function->Range(&min, &max);
    112   EXPECT_NEAR(min, -0.28818, epsilon);
    113   EXPECT_EQ(1.f, max);
    114 
    115   // Derivative has one root < 0 and one root in [0,1].
    116   function = CubicBezierTimingFunction::Create(0.25, 0.1, 0.75, 1.5);
    117   function->Range(&min, &max);
    118   EXPECT_EQ(0.f, min);
    119   EXPECT_NEAR(max, 1.10755, epsilon);
    120 
    121   // Derivative has one root in [0,1] and one root > 1.
    122   function = CubicBezierTimingFunction::Create(0.25, -0.5, 0.75, 0.9);
    123   function->Range(&min, &max);
    124   EXPECT_NEAR(min, -0.10755, epsilon);
    125   EXPECT_EQ(1.f, max);
    126 
    127   // Derivative has two roots < 0.
    128   function = CubicBezierTimingFunction::Create(0.25, 0.3, 0.75, 0.633);
    129   function->Range(&min, &max);
    130   EXPECT_EQ(0.f, min);
    131   EXPECT_EQ(1.f, max);
    132 
    133   // Derivative has two roots > 1.
    134   function = CubicBezierTimingFunction::Create(0.25, 0.367, 0.75, 0.7);
    135   function->Range(&min, &max);
    136   EXPECT_EQ(0.f, min);
    137   EXPECT_EQ(1.f, max);
    138 }
    139 
    140 }  // namespace
    141 }  // namespace cc
    142