1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package platform.test.motion.view
18
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import com.google.common.truth.Correspondence.tolerance
21 import com.google.common.truth.IterableSubject
22 import com.google.common.truth.Truth.assertThat
23 import org.junit.Assert.assertThrows
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import platform.test.motion.view.AnimationSampling.Companion.evenlySampled
27
28 @RunWith(AndroidJUnit4::class)
29 class AnimationSamplingTest {
30
31 @Test
evenlySampled_1_inMiddlenull32 fun evenlySampled_1_inMiddle() {
33 val subject = evenlySampled(1, sampleAtStart = false, sampleAtEnd = false)
34 assertThat(subject.sampleAt).allowTolerance().containsExactly(1 / 2f).inOrder()
35 }
36
37 @Test
evenlySampled_1_atStartnull38 fun evenlySampled_1_atStart() {
39 val subject = evenlySampled(1, sampleAtStart = true, sampleAtEnd = false)
40 assertThat(subject.sampleAt).allowTolerance().containsExactly(0f).inOrder()
41 }
42
43 @Test
evenlySampled_1_atEndnull44 fun evenlySampled_1_atEnd() {
45 val subject = evenlySampled(1, sampleAtStart = false, sampleAtEnd = true)
46 assertThat(subject.sampleAt).allowTolerance().containsExactly(1f).inOrder()
47 }
48
49 @Test
evenlySampled_1_atStartAndEnd_throwsnull50 fun evenlySampled_1_atStartAndEnd_throws() {
51 assertThrows(IllegalArgumentException::class.java) {
52 evenlySampled(1, sampleAtStart = true, sampleAtEnd = true)
53 }
54 }
55
56 @Test
evenlySampled_2_inMiddlenull57 fun evenlySampled_2_inMiddle() {
58 val subject = evenlySampled(2, sampleAtStart = false, sampleAtEnd = false)
59 assertThat(subject.sampleAt).allowTolerance().containsExactly(1 / 3f, 2 / 3f).inOrder()
60 }
61
62 @Test
evenlySampled_2_atStartnull63 fun evenlySampled_2_atStart() {
64 val subject = evenlySampled(2, sampleAtStart = true, sampleAtEnd = false)
65 assertThat(subject.sampleAt).allowTolerance().containsExactly(0f, 1 / 2f).inOrder()
66 }
67
68 @Test
evenlySampled_2_atEndnull69 fun evenlySampled_2_atEnd() {
70 val subject = evenlySampled(2, sampleAtStart = false, sampleAtEnd = true)
71 assertThat(subject.sampleAt).allowTolerance().containsExactly(1 / 2f, 1f).inOrder()
72 }
73
74 @Test
evenlySampled_2_atStartAndEndnull75 fun evenlySampled_2_atStartAndEnd() {
76 val subject = evenlySampled(2, sampleAtStart = true, sampleAtEnd = true)
77 assertThat(subject.sampleAt).allowTolerance().containsExactly(0f, 1f).inOrder()
78 }
79 }
80
allowTolerancenull81 private fun IterableSubject.allowTolerance() = comparingElementsUsing(tolerance(1.0e-10))
82