1/*
2 * Copyright (C) 2016 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 */
16package android.support.v17.leanback.widget;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertSame;
20
21import android.support.test.filters.SmallTest;
22import android.support.test.runner.AndroidJUnit4;
23
24import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27
28@RunWith(AndroidJUnit4.class)
29@SmallTest
30public class ParallaxIntTest {
31
32    Parallax<Parallax.IntProperty> mSource;
33    int mScreenMax;
34
35    static void assertFloatEquals(float expected, float actual) {
36        org.junit.Assert.assertEquals((double) expected, (double) actual, 0.0001d);
37    }
38
39    @Before
40    public void setUp() throws Exception {
41        mSource = new Parallax<Parallax.IntProperty>() {
42
43            @Override
44            public float getMaxValue() {
45                return mScreenMax;
46            }
47
48            @Override
49            public IntProperty createProperty(String name, int index) {
50                return new IntProperty(name, index);
51            }
52        };
53    }
54
55    @Test
56    public void testVariable() {
57        mScreenMax = 1080;
58        Parallax.IntProperty var1 = mSource.addProperty("var1");
59        var1.setValue(mSource, 54);
60        assertEquals((int) 54, var1.getValue(mSource));
61        assertEquals(var1.getName(), "var1");
62        var1.set(mSource, (int) 2000);
63        assertEquals((int) 2000, var1.get(mSource).intValue());
64    }
65
66    @Test
67    public void testFixedKeyValue() {
68        mScreenMax = 1080;
69        Parallax.IntProperty var1 = mSource.addProperty("var1");
70
71        Parallax.IntPropertyMarkerValue keyValue = (Parallax.IntPropertyMarkerValue)
72                var1.atAbsolute(1000);
73        assertSame(keyValue.getProperty(), var1);
74        assertEquals((int) 1000, keyValue.getMarkerValue(mSource));
75    }
76
77    @Test
78    public void testFractionOfKeyValue() {
79        mScreenMax = 1080;
80        Parallax.IntProperty var1 = mSource.addProperty("var1");
81
82        Parallax.IntPropertyMarkerValue keyValue = (Parallax.IntPropertyMarkerValue)
83                var1.at(0, 0.5f);
84        assertSame(keyValue.getProperty(), var1);
85        assertEquals((int) 540, keyValue.getMarkerValue(mSource));
86    }
87
88    @Test
89    public void testFixedKeyValueWithFraction() {
90        mScreenMax = 1080;
91        Parallax.IntProperty var1 = mSource.addProperty("var1");
92
93        Parallax.IntPropertyMarkerValue keyValue = (Parallax.IntPropertyMarkerValue)
94                var1.at(-100, 0.5f);
95        assertSame(keyValue.getProperty(), var1);
96        assertEquals((int) 440, keyValue.getMarkerValue(mSource));
97
98        Parallax.IntPropertyMarkerValue keyValue2 = (Parallax.IntPropertyMarkerValue)
99                var1.at(100, 0.5f);
100        assertSame(keyValue2.getProperty(), var1);
101        assertEquals((int) 640, keyValue2.getMarkerValue(mSource));
102    }
103
104    @Test(expected = IllegalStateException.class)
105    public void testVerifyIntPropertys_wrongOrder() {
106        Parallax.IntProperty var1 = mSource.addProperty("var1");
107        Parallax.IntProperty var2 = mSource.addProperty("var2");
108
109        var1.setValue(mSource, (int) 500);
110        var2.setValue(mSource, (int) 499);
111
112        mSource.verifyIntProperties();
113    }
114
115    @Test(expected = IllegalStateException.class)
116    public void testVerifyIntPropertysWrong_combination() {
117        Parallax.IntProperty var1 = mSource.addProperty("var1");
118        Parallax.IntProperty var2 = mSource.addProperty("var2");
119
120        var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
121        var2.setValue(mSource, Parallax.IntProperty.UNKNOWN_AFTER);
122
123        mSource.verifyIntProperties();
124    }
125
126    @Test
127    public void testVerifyIntPropertys_success() {
128        Parallax.IntProperty var1 = mSource.addProperty("var1");
129        Parallax.IntProperty var2 = mSource.addProperty("var2");
130
131        var1.setValue(mSource, (int) 499);
132        var2.setValue(mSource, (int) 500);
133
134        mSource.verifyIntProperties();
135
136        var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
137        var2.setValue(mSource, (int) 500);
138
139        mSource.verifyIntProperties();
140
141        var1.setValue(mSource, (int) 499);
142        var2.setValue(mSource, Parallax.IntProperty.UNKNOWN_AFTER);
143
144        mSource.verifyIntProperties();
145    }
146}
147