aboutsummaryrefslogtreecommitdiffstats
path: root/chimpchat/test/com/android/chimpchat/adb/LinearInterpolatorTest.java
blob: f9bc72fa303a3d419c933f50b5d8a4b5a89951f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.chimpchat.adb;

import com.google.common.collect.Lists;

import com.android.chimpchat.adb.LinearInterpolator.Point;

import junit.framework.TestCase;

import java.util.List;

/**
 * Unit tests for the LinerInterpolator class.S
 */
public class LinearInterpolatorTest extends TestCase {
    private static class Collector implements LinearInterpolator.Callback {
        private final List<LinearInterpolator.Point> points = Lists.newArrayList();

        public List<LinearInterpolator.Point> getPoints() {
            return points;
        }

        public void end(Point input) {
            points.add(input);
        }

        public void start(Point input) {
            points.add(input);
        }

        public void step(Point input) {
            points.add(input);
        }
    }

    List<Integer> STEP_POINTS = Lists.newArrayList(0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
            1000);
    List<Integer> REVERSE_STEP_POINTS = Lists.newArrayList(1000, 900, 800, 700, 600, 500, 400, 300,
            200, 100, 0);

    public void testLerpRight() {
        LinearInterpolator lerp = new LinearInterpolator(10);
        Collector collector = new Collector();
        lerp.interpolate(new LinearInterpolator.Point(0, 100),
                new LinearInterpolator.Point(1000, 100),
                collector);

        List<LinearInterpolator.Point> points = collector.getPoints();
        assertEquals(11, points.size());
        for (int x = 0; x < points.size(); x++) {
            assertEquals(new Point(STEP_POINTS.get(x), 100), points.get(x));
        }
    }

    public void testLerpLeft() {
        LinearInterpolator lerp = new LinearInterpolator(10);
        Collector collector = new Collector();
        lerp.interpolate(new LinearInterpolator.Point(1000, 100),
                new LinearInterpolator.Point(0, 100),
                collector);

        List<LinearInterpolator.Point> points = collector.getPoints();
        assertEquals(11, points.size());
        for (int x = 0; x < points.size(); x++) {
            assertEquals(new Point(REVERSE_STEP_POINTS.get(x), 100), points.get(x));
        }
    }

    public void testLerpUp() {
        LinearInterpolator lerp = new LinearInterpolator(10);
        Collector collector = new Collector();
        lerp.interpolate(new LinearInterpolator.Point(100, 1000),
                new LinearInterpolator.Point(100, 0),
                collector);

        List<LinearInterpolator.Point> points = collector.getPoints();
        assertEquals(11, points.size());
        for (int x = 0; x < points.size(); x++) {
            assertEquals(new Point(100, REVERSE_STEP_POINTS.get(x)), points.get(x));
        }
    }

    public void testLerpDown() {
        LinearInterpolator lerp = new LinearInterpolator(10);
        Collector collector = new Collector();
        lerp.interpolate(new LinearInterpolator.Point(100, 0),
                new LinearInterpolator.Point(100, 1000),
                collector);

        List<LinearInterpolator.Point> points = collector.getPoints();
        assertEquals(11, points.size());
        for (int x = 0; x < points.size(); x++) {
            assertEquals(new Point(100, STEP_POINTS.get(x)), points.get(x));
        }
    }

    public void testLerpNW() {
        LinearInterpolator lerp = new LinearInterpolator(10);
        Collector collector = new Collector();
        lerp.interpolate(new LinearInterpolator.Point(0, 0),
                new LinearInterpolator.Point(1000, 1000),
                collector);

        List<LinearInterpolator.Point> points = collector.getPoints();
        assertEquals(11, points.size());
        for (int x = 0; x < points.size(); x++) {
            assertEquals(new Point(STEP_POINTS.get(x), STEP_POINTS.get(x)), points.get(x));
        }
    }

    public void testLerpNE() {
        LinearInterpolator lerp = new LinearInterpolator(10);
        Collector collector = new Collector();
        lerp.interpolate(new LinearInterpolator.Point(1000, 1000),
                new LinearInterpolator.Point(0, 0),
                collector);

        List<LinearInterpolator.Point> points = collector.getPoints();
        assertEquals(11, points.size());
        for (int x = 0; x < points.size(); x++) {
            assertEquals(new Point(REVERSE_STEP_POINTS.get(x), REVERSE_STEP_POINTS.get(x)), points.get(x));
        }
    }
}