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/color_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 return nextafter(d, d+1); 24 #endif 25 } 26 27 // Validates that the same interpolations are made as in Blink. 28 TEST(TweenTest, ColorValueBetween) { 29 // From blink's AnimatableColorTest. 30 EXPECT_SKCOLOR_EQ(0xFF00FF00, 31 Tween::ColorValueBetween(-10.0, 0xFF00FF00, 0xFF00FF00)); 32 EXPECT_SKCOLOR_EQ(0xFF00FF00, 33 Tween::ColorValueBetween(-10.0, 0xFF00FF00, 0xFFFF00FF)); 34 EXPECT_SKCOLOR_EQ(0xFF00FF00, 35 Tween::ColorValueBetween(0.0, 0xFF00FF00, 0xFFFF00FF)); 36 EXPECT_SKCOLOR_EQ(0xFF01FE01, 37 Tween::ColorValueBetween(1.0 / 255, 0xFF00FF00, 0xFFFF00FF)); 38 EXPECT_SKCOLOR_EQ(0xFF808080, 39 Tween::ColorValueBetween(0.5, 0xFF00FF00, 0xFFFF00FF)); 40 EXPECT_SKCOLOR_EQ( 41 0xFFFE01FE, 42 Tween::ColorValueBetween(254.0 / 255.0, 0xFF00FF00, 0xFFFF00FF)); 43 EXPECT_SKCOLOR_EQ(0xFFFF00FF, 44 Tween::ColorValueBetween(1.0, 0xFF00FF00, 0xFFFF00FF)); 45 EXPECT_SKCOLOR_EQ(0xFFFF00FF, 46 Tween::ColorValueBetween(10.0, 0xFF00FF00, 0xFFFF00FF)); 47 EXPECT_SKCOLOR_EQ(0xFF0C253E, 48 Tween::ColorValueBetween(3.0 / 16.0, 0xFF001020, 0xFF4080C0)); 49 EXPECT_SKCOLOR_EQ(0x80FF00FF, 50 Tween::ColorValueBetween(0.5, 0x0000FF00, 0xFFFF00FF)); 51 EXPECT_SKCOLOR_EQ(0x60AA55AA, 52 Tween::ColorValueBetween(0.5, 0x4000FF00, 0x80FF00FF)); 53 EXPECT_SKCOLOR_EQ(0x60FFAAFF, 54 Tween::ColorValueBetween(0.5, 0x40FF00FF, 0x80FFFFFF)); 55 EXPECT_SKCOLOR_EQ(0x103060A0, 56 Tween::ColorValueBetween(0.5, 0x10204080, 0x104080C0)); 57 } 58 59 // Ensures that each of the 3 integers in [0, 1, 2] ae selected with equal 60 // weight. 61 TEST(TweenTest, IntValueBetween) { 62 EXPECT_EQ(0, Tween::IntValueBetween(0.0, 0, 2)); 63 EXPECT_EQ(0, Tween::IntValueBetween(0.5 / 3.0, 0, 2)); 64 EXPECT_EQ(0, Tween::IntValueBetween(1.0 / 3.0, 0, 2)); 65 66 EXPECT_EQ(1, Tween::IntValueBetween(next_double(1.0 / 3.0), 0, 2)); 67 EXPECT_EQ(1, Tween::IntValueBetween(1.5 / 3.0, 0, 2)); 68 EXPECT_EQ(1, Tween::IntValueBetween(2.0 / 3.0, 0, 2)); 69 70 EXPECT_EQ(2, Tween::IntValueBetween(next_double(2.0 / 3.0), 0, 2)); 71 EXPECT_EQ(2, Tween::IntValueBetween(2.5 / 3.0, 0, 2)); 72 EXPECT_EQ(2, Tween::IntValueBetween(3.0 / 3.0, 0, 2)); 73 } 74 75 TEST(TweenTest, IntValueBetweenNegative) { 76 EXPECT_EQ(-2, Tween::IntValueBetween(0.0, -2, 0)); 77 EXPECT_EQ(-2, Tween::IntValueBetween(0.5 / 3.0, -2, 0)); 78 EXPECT_EQ(-2, Tween::IntValueBetween(1.0 / 3.0, -2, 0)); 79 80 EXPECT_EQ(-1, Tween::IntValueBetween(next_double(1.0 / 3.0), -2, 0)); 81 EXPECT_EQ(-1, Tween::IntValueBetween(1.5 / 3.0, -2, 0)); 82 EXPECT_EQ(-1, Tween::IntValueBetween(2.0 / 3.0, -2, 0)); 83 84 EXPECT_EQ(0, Tween::IntValueBetween(next_double(2.0 / 3.0), -2, 0)); 85 EXPECT_EQ(0, Tween::IntValueBetween(2.5 / 3.0, -2, 0)); 86 EXPECT_EQ(0, Tween::IntValueBetween(3.0 / 3.0, -2, 0)); 87 } 88 89 TEST(TweenTest, IntValueBetweenReverse) { 90 EXPECT_EQ(2, Tween::IntValueBetween(0.0, 2, 0)); 91 EXPECT_EQ(2, Tween::IntValueBetween(0.5 / 3.0, 2, 0)); 92 EXPECT_EQ(2, Tween::IntValueBetween(1.0 / 3.0, 2, 0)); 93 94 EXPECT_EQ(1, Tween::IntValueBetween(next_double(1.0 / 3.0), 2, 0)); 95 EXPECT_EQ(1, Tween::IntValueBetween(1.5 / 3.0, 2, 0)); 96 EXPECT_EQ(1, Tween::IntValueBetween(2.0 / 3.0, 2, 0)); 97 98 EXPECT_EQ(0, Tween::IntValueBetween(next_double(2.0 / 3.0), 2, 0)); 99 EXPECT_EQ(0, Tween::IntValueBetween(2.5 / 3.0, 2, 0)); 100 EXPECT_EQ(0, Tween::IntValueBetween(3.0 / 3.0, 2, 0)); 101 } 102 103 TEST(TweenTest, LinearIntValueBetween) { 104 EXPECT_EQ(0, Tween::LinearIntValueBetween(0.0, 0, 2)); 105 EXPECT_EQ(0, Tween::LinearIntValueBetween(0.5 / 4.0, 0, 2)); 106 EXPECT_EQ(0, Tween::LinearIntValueBetween(0.99 / 4.0, 0, 2)); 107 108 EXPECT_EQ(1, Tween::LinearIntValueBetween(1.0 / 4.0, 0, 2)); 109 EXPECT_EQ(1, Tween::LinearIntValueBetween(1.5 / 4.0, 0, 2)); 110 EXPECT_EQ(1, Tween::LinearIntValueBetween(2.0 / 4.0, 0, 2)); 111 EXPECT_EQ(1, Tween::LinearIntValueBetween(2.5 / 4.0, 0, 2)); 112 EXPECT_EQ(1, Tween::LinearIntValueBetween(2.99 / 4.0, 0, 2)); 113 114 EXPECT_EQ(2, Tween::LinearIntValueBetween(3.0 / 4.0, 0, 2)); 115 EXPECT_EQ(2, Tween::LinearIntValueBetween(3.5 / 4.0, 0, 2)); 116 EXPECT_EQ(2, Tween::LinearIntValueBetween(4.0 / 4.0, 0, 2)); 117 } 118 119 TEST(TweenTest, LinearIntValueBetweenNegative) { 120 EXPECT_EQ(-2, Tween::LinearIntValueBetween(0.0, -2, 0)); 121 EXPECT_EQ(-2, Tween::LinearIntValueBetween(0.5 / 4.0, -2, 0)); 122 EXPECT_EQ(-2, Tween::LinearIntValueBetween(0.99 / 4.0, -2, 0)); 123 124 EXPECT_EQ(-1, Tween::LinearIntValueBetween(1.0 / 4.0, -2, 0)); 125 EXPECT_EQ(-1, Tween::LinearIntValueBetween(1.5 / 4.0, -2, 0)); 126 EXPECT_EQ(-1, Tween::LinearIntValueBetween(2.0 / 4.0, -2, 0)); 127 EXPECT_EQ(-1, Tween::LinearIntValueBetween(2.5 / 4.0, -2, 0)); 128 EXPECT_EQ(-1, Tween::LinearIntValueBetween(2.99 / 4.0, -2, 0)); 129 130 EXPECT_EQ(0, Tween::LinearIntValueBetween(3.0 / 4.0, -2, 0)); 131 EXPECT_EQ(0, Tween::LinearIntValueBetween(3.5 / 4.0, -2, 0)); 132 EXPECT_EQ(0, Tween::LinearIntValueBetween(4.0 / 4.0, -2, 0)); 133 } 134 135 } // namespace 136 } // namespace gfx 137