OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "mojo/services/media/common/cpp/timeline_function.h" |
| 6 #include "services/media/common/test/test_base.h" |
| 7 |
| 8 namespace mojo { |
| 9 namespace media { |
| 10 namespace { |
| 11 |
| 12 class TimelineFunctionTest : public TestBase { |
| 13 public: |
| 14 // Verifies that a TimelineFunction instantiated in three different ways with |
| 15 // the given arguments has the expected properties. |
| 16 void VerifyBasics(int64_t reference_time, |
| 17 int64_t subject_time, |
| 18 uint32_t reference_delta, |
| 19 uint32_t subject_delta) { |
| 20 TimelineFunction under_test_1(reference_time, subject_time, reference_delta, |
| 21 subject_delta); |
| 22 VerifyBasics(under_test_1, reference_time, subject_time, reference_delta, |
| 23 subject_delta); |
| 24 |
| 25 TimelineFunction under_test_2(reference_time, subject_time, |
| 26 TimelineRate(subject_delta, reference_delta)); |
| 27 VerifyBasics(under_test_2, reference_time, subject_time, reference_delta, |
| 28 subject_delta); |
| 29 |
| 30 TimelineFunction under_test_3(TimelineRate(subject_delta, reference_delta)); |
| 31 VerifyBasics(under_test_3, 0, 0, reference_delta, subject_delta); |
| 32 |
| 33 EXPECT_EQ(under_test_1, under_test_1); |
| 34 EXPECT_EQ(under_test_1, under_test_2); |
| 35 EXPECT_EQ(under_test_2, under_test_1); |
| 36 EXPECT_EQ(under_test_2, under_test_2); |
| 37 |
| 38 if (reference_time == 0 && subject_time == 0) { |
| 39 EXPECT_EQ(under_test_1, under_test_3); |
| 40 EXPECT_EQ(under_test_2, under_test_3); |
| 41 EXPECT_EQ(under_test_3, under_test_1); |
| 42 EXPECT_EQ(under_test_3, under_test_2); |
| 43 } else { |
| 44 EXPECT_NE(under_test_1, under_test_3); |
| 45 EXPECT_NE(under_test_2, under_test_3); |
| 46 EXPECT_NE(under_test_3, under_test_1); |
| 47 EXPECT_NE(under_test_3, under_test_2); |
| 48 } |
| 49 } |
| 50 |
| 51 // Verifies that the given TimelineFunction instantiated with the given |
| 52 // arguments has the expected properties. |
| 53 void VerifyBasics(const TimelineFunction& under_test, |
| 54 int64_t reference_time, |
| 55 int64_t subject_time, |
| 56 uint32_t reference_delta, |
| 57 uint32_t subject_delta) { |
| 58 TimelineRate::Reduce(&subject_delta, &reference_delta); |
| 59 EXPECT_EQ(reference_time, under_test.reference_time()); |
| 60 EXPECT_EQ(subject_time, under_test.subject_time()); |
| 61 EXPECT_EQ(reference_delta, under_test.reference_delta()); |
| 62 EXPECT_EQ(subject_delta, under_test.subject_delta()); |
| 63 EXPECT_EQ(reference_delta, under_test.rate().reference_delta()); |
| 64 EXPECT_EQ(subject_delta, under_test.rate().subject_delta()); |
| 65 } |
| 66 |
| 67 // Verifies that the inverse of a TimelineFunction instantiated in three |
| 68 // different ways with the given arguments has the expected properties. |
| 69 void VerifyInverse(int64_t reference_time, |
| 70 int64_t subject_time, |
| 71 uint32_t reference_delta, |
| 72 uint32_t subject_delta) { |
| 73 TimelineFunction under_test_1(reference_time, subject_time, reference_delta, |
| 74 subject_delta); |
| 75 VerifyBasics(under_test_1.Inverse(), subject_time, reference_time, |
| 76 subject_delta, reference_delta); |
| 77 |
| 78 TimelineFunction under_test_2(reference_time, subject_time, |
| 79 TimelineRate(subject_delta, reference_delta)); |
| 80 VerifyBasics(under_test_2.Inverse(), subject_time, reference_time, |
| 81 subject_delta, reference_delta); |
| 82 |
| 83 TimelineFunction under_test_3(TimelineRate(subject_delta, reference_delta)); |
| 84 VerifyBasics(under_test_3.Inverse(), 0, 0, subject_delta, reference_delta); |
| 85 } |
| 86 |
| 87 // Verifies that TimelineFunction::Apply, in its various forms, works as |
| 88 // expected for the given arguments. |
| 89 void VerifyApply(int64_t reference_time, |
| 90 int64_t subject_time, |
| 91 uint32_t reference_delta, |
| 92 uint32_t subject_delta, |
| 93 int64_t reference_input, |
| 94 int64_t expected_result) { |
| 95 // Verify the static method. |
| 96 EXPECT_EQ(expected_result, TimelineFunction::Apply( |
| 97 reference_time, subject_time, |
| 98 TimelineRate(subject_delta, reference_delta), |
| 99 reference_input)); |
| 100 |
| 101 // Verify the instance method. |
| 102 TimelineFunction under_test(reference_time, subject_time, reference_delta, |
| 103 subject_delta); |
| 104 EXPECT_EQ(expected_result, under_test.Apply(reference_input)); |
| 105 |
| 106 // Verify the operator. |
| 107 EXPECT_EQ(expected_result, under_test(reference_input)); |
| 108 } |
| 109 |
| 110 // Verifies that TimelineFunction::ApplyInverse, in its various forms, works |
| 111 // as |
| 112 // expected for the given arguments. |
| 113 void VerifyApplyInverse(int64_t reference_time, |
| 114 int64_t subject_time, |
| 115 uint32_t reference_delta, |
| 116 uint32_t subject_delta, |
| 117 int64_t subject_input, |
| 118 int64_t expected_result) { |
| 119 // Verify the static method. |
| 120 EXPECT_EQ(expected_result, |
| 121 TimelineFunction::ApplyInverse( |
| 122 reference_time, subject_time, |
| 123 TimelineRate(subject_delta, reference_delta), subject_input)); |
| 124 |
| 125 // Verify the instance method. |
| 126 TimelineFunction under_test(reference_time, subject_time, reference_delta, |
| 127 subject_delta); |
| 128 EXPECT_EQ(expected_result, under_test.ApplyInverse(subject_input)); |
| 129 } |
| 130 |
| 131 // Verifies that TimelineFunction::Compose works as expected with the given |
| 132 // inputs. |
| 133 void VerifyCompose(const TimelineFunction& a, |
| 134 const TimelineFunction& b, |
| 135 bool exact, |
| 136 const TimelineFunction& expected_result) { |
| 137 // Verify the static method. |
| 138 EXPECT_EQ(expected_result, TimelineFunction::Compose(a, b, exact)); |
| 139 } |
| 140 }; |
| 141 |
| 142 // Tests TimelineFunction basics for various instantiation arguments. |
| 143 TEST_F(TimelineFunctionTest, Basics) { |
| 144 VerifyBasics(0, 0, 1, 0); |
| 145 VerifyBasics(0, 0, 1, 1); |
| 146 VerifyBasics(1, 1, 10, 10); |
| 147 VerifyBasics(1234, 5678, 4321, 8765); |
| 148 VerifyBasics(-1234, 5678, 4321, 8765); |
| 149 VerifyBasics(-1234, -5678, 4321, 8765); |
| 150 VerifyBasics(1234, -5678, 4321, 8765); |
| 151 } |
| 152 |
| 153 // Tests TimelineFunction::Inverse. |
| 154 TEST_F(TimelineFunctionTest, Inverse) { |
| 155 VerifyInverse(0, 0, 1, 1); |
| 156 VerifyInverse(1, 1, 10, 10); |
| 157 VerifyInverse(1234, 5678, 4321, 8765); |
| 158 VerifyInverse(-1234, 5678, 4321, 8765); |
| 159 VerifyInverse(-1234, -5678, 4321, 8765); |
| 160 VerifyInverse(1234, -5678, 4321, 8765); |
| 161 } |
| 162 |
| 163 // Tests TimelineFunction::Apply in its variations. |
| 164 TEST_F(TimelineFunctionTest, Apply) { |
| 165 VerifyApply(0, 0, 1, 0, 0, 0); |
| 166 VerifyApply(0, 0, 1, 0, 1000, 0); |
| 167 VerifyApply(0, 1234, 1, 0, 0, 1234); |
| 168 VerifyApply(0, 1234, 1, 0, 1000, 1234); |
| 169 VerifyApply(0, 1234, 1, 0, -1000, 1234); |
| 170 VerifyApply(0, -1234, 1, 0, 0, -1234); |
| 171 VerifyApply(0, -1234, 1, 0, 1000, -1234); |
| 172 VerifyApply(0, -1234, 1, 0, -1000, -1234); |
| 173 VerifyApply(0, 0, 1, 1, 0, 0); |
| 174 VerifyApply(0, 0, 1, 1, 1000, 1000); |
| 175 VerifyApply(0, 1234, 1, 1, 0, 1234); |
| 176 VerifyApply(0, 1234, 1, 1, 1000, 2234); |
| 177 VerifyApply(0, 1234, 1, 1, -1000, 234); |
| 178 VerifyApply(0, -1234, 1, 1, 0, -1234); |
| 179 VerifyApply(0, -1234, 1, 1, 1000, -234); |
| 180 VerifyApply(0, -1234, 1, 1, -1000, -2234); |
| 181 VerifyApply(10, 0, 1, 0, 0, 0); |
| 182 VerifyApply(10, 0, 1, 1, 0, -10); |
| 183 VerifyApply(-10, 0, 1, 0, 0, 0); |
| 184 VerifyApply(-10, 0, 1, 1, 0, 10); |
| 185 VerifyApply(0, 1234, 2, 1, 0, 1234); |
| 186 VerifyApply(0, 1234, 2, 1, 1234, 1234 + 1234 / 2); |
| 187 VerifyApply(0, 1234, 1, 2, 1234, 1234 + 1234 * 2); |
| 188 } |
| 189 |
| 190 // Tests TimelineFunction::Apply in its variations. |
| 191 TEST_F(TimelineFunctionTest, ApplyInverse) { |
| 192 VerifyApplyInverse(0, 0, 1, 1, 0, 0); |
| 193 VerifyApplyInverse(0, 0, 1, 1, 1000, 1000); |
| 194 VerifyApplyInverse(0, 1234, 1, 1, 1234, 0); |
| 195 VerifyApplyInverse(0, 1234, 1, 1, 2234, 1000); |
| 196 VerifyApplyInverse(0, 1234, 1, 1, 234, -1000); |
| 197 VerifyApplyInverse(0, -1234, 1, 1, -1234, 0); |
| 198 VerifyApplyInverse(0, -1234, 1, 1, -234, 1000); |
| 199 VerifyApplyInverse(0, -1234, 1, 1, -2234, -1000); |
| 200 VerifyApplyInverse(10, 0, 1, 1, -10, 0); |
| 201 VerifyApplyInverse(-10, 0, 1, 1, 10, 0); |
| 202 VerifyApplyInverse(0, 1234, 2, 1, 1234, 0); |
| 203 VerifyApplyInverse(0, 1234, 2, 1, 1234 + 1234 / 2, 1234); |
| 204 VerifyApplyInverse(0, 1234, 1, 2, 1234 + 1234 * 2, 1234); |
| 205 } |
| 206 |
| 207 // Tests TimelineFunction::Compose. |
| 208 TEST_F(TimelineFunctionTest, Compose) { |
| 209 VerifyCompose(TimelineFunction(0, 0, 1, 0), TimelineFunction(0, 0, 1, 0), |
| 210 true, TimelineFunction(0, 0, 1, 0)); |
| 211 VerifyCompose(TimelineFunction(0, 0, 1, 1), TimelineFunction(0, 0, 1, 1), |
| 212 true, TimelineFunction(0, 0, 1, 1)); |
| 213 VerifyCompose(TimelineFunction(1, 0, 1, 1), TimelineFunction(0, 0, 1, 1), |
| 214 true, TimelineFunction(0, -1, 1, 1)); |
| 215 VerifyCompose(TimelineFunction(10, 10, 1, 1), TimelineFunction(0, 0, 1, 1), |
| 216 true, TimelineFunction(0, 0, 1, 1)); |
| 217 VerifyCompose(TimelineFunction(0, 0, 1, 2), TimelineFunction(0, 0, 1, 2), |
| 218 true, TimelineFunction(0, 0, 1, 4)); |
| 219 VerifyCompose(TimelineFunction(0, 0, 2, 1), TimelineFunction(0, 0, 2, 1), |
| 220 true, TimelineFunction(0, 0, 4, 1)); |
| 221 VerifyCompose(TimelineFunction(0, 0, 2, 1), TimelineFunction(0, 0, 1, 2), |
| 222 true, TimelineFunction(0, 0, 1, 1)); |
| 223 } |
| 224 |
| 225 } // namespace |
| 226 } // namespace media |
| 227 } // namespace mojo |
OLD | NEW |