1// CHECKSTYLE:OFF Generated code
2/* This file is auto-generated from ParallaxIntEffectTest.java.  DO NOT MODIFY. */
3
4/*
5 * Copyright (C) 2016 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package android.support.v17.leanback.widget;
21
22import static junit.framework.Assert.assertEquals;
23
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.util.Property;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.mockito.Mock;
35import org.mockito.Mockito;
36import org.mockito.MockitoAnnotations;
37
38@RunWith(AndroidJUnit4.class)
39@SmallTest
40public class ParallaxFloatEffectTest {
41
42    Parallax<Parallax.FloatProperty> mSource;
43    int mScreenMax;
44    ParallaxEffect.FloatEffect mEffect;
45    @Mock ParallaxTarget mTarget;
46
47    static void assertFloatEquals(float expected, float actual) {
48        org.junit.Assert.assertEquals((double) expected, (double) actual, 0.0001d);
49    }
50
51    @Before
52    public void setUp() throws Exception {
53        MockitoAnnotations.initMocks(this);
54        mSource = new Parallax<Parallax.FloatProperty>() {
55
56            @Override
57            public float getMaxValue() {
58                return mScreenMax;
59            }
60
61            @Override
62            public FloatProperty createProperty(String name, int index) {
63                return new FloatProperty(name, index);
64            }
65        };
66        mEffect = new ParallaxEffect.FloatEffect();
67    }
68
69    @Test
70    public void testOneVariable() {
71        mScreenMax = 1080;
72        Parallax.FloatProperty var1 = mSource.addProperty("var1");
73
74        mEffect.setPropertyRanges(var1.atAbsolute(540), var1.atAbsolute(0));
75        mEffect.target(mTarget);
76
77        // start
78        var1.setValue(mSource, 540);
79        mEffect.performMapping(mSource);
80        verify(mTarget, times(1)).update(0f);
81        Mockito.reset(mTarget);
82
83        // 25% complete
84        var1.setValue(mSource, 405);
85        mEffect.performMapping(mSource);
86        verify(mTarget, times(1)).update(0.25f);
87        Mockito.reset(mTarget);
88
89        // middle
90        var1.setValue(mSource, 270);
91        mEffect.performMapping(mSource);
92        verify(mTarget, times(1)).update(.5f);
93        Mockito.reset(mTarget);
94
95        // 75% complete
96        var1.setValue(mSource, 135);
97        mEffect.performMapping(mSource);
98        verify(mTarget, times(1)).update(0.75f);
99        Mockito.reset(mTarget);
100
101        // end
102        var1.setValue(mSource, 0);
103        mEffect.performMapping(mSource);
104        verify(mTarget, times(1)).update(1f);
105        Mockito.reset(mTarget);
106
107        // after end
108        var1.setValue(mSource, -1000);
109        mEffect.performMapping(mSource);
110        verify(mTarget, times(1)).update(1f);
111        Mockito.reset(mTarget);
112
113        // before start
114        var1.setValue(mSource, 1000);
115        mEffect.performMapping(mSource);
116        verify(mTarget, times(1)).update(0f);
117        Mockito.reset(mTarget);
118
119        // unknown_before
120        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_BEFORE);
121        mEffect.performMapping(mSource);
122        verify(mTarget, times(1)).update(1f);
123        Mockito.reset(mTarget);
124
125        // unknown_after
126        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_AFTER);
127        mEffect.performMapping(mSource);
128        verify(mTarget, times(1)).update(0f);
129        Mockito.reset(mTarget);
130    }
131
132    @Test(expected=IllegalStateException.class)
133    public void testVerifyKeyValueOfSameVariableInDesendantOrder() {
134        mScreenMax = 1080;
135        Parallax.FloatProperty var1 = mSource.addProperty("var1");
136
137        mEffect.setPropertyRanges(var1.atAbsolute(540), var1.atAbsolute(550));
138        mEffect.target(mTarget);
139        var1.setValue(mSource, 0);
140        mEffect.performMapping(mSource);
141    }
142
143    @Test
144    public void testTwoVariable() {
145        mScreenMax = 1080;
146        Parallax.FloatProperty var1 = mSource.addProperty("var1");
147        Parallax.FloatProperty var2 = mSource.addProperty("var2");
148
149        mEffect.setPropertyRanges(var1.atAbsolute(540), var2.atAbsolute(540));
150        mEffect.target(mTarget);
151
152        // start
153        var1.setValue(mSource, 540);
154        var2.setValue(mSource, 840);
155        mEffect.performMapping(mSource);
156        verify(mTarget, times(1)).update(0f);
157        Mockito.reset(mTarget);
158
159        // middle
160        var1.setValue(mSource, 390);
161        var2.setValue(mSource, 690);
162        mEffect.performMapping(mSource);
163        verify(mTarget, times(1)).update(.5f);
164        Mockito.reset(mTarget);
165
166        // end
167        var1.setValue(mSource, 240);
168        var2.setValue(mSource, 540);
169        mEffect.performMapping(mSource);
170        verify(mTarget, times(1)).update(1f);
171        Mockito.reset(mTarget);
172
173        // after end
174        var1.setValue(mSource, 200);
175        var2.setValue(mSource, 500);
176        mEffect.performMapping(mSource);
177        verify(mTarget, times(1)).update(1f);
178        Mockito.reset(mTarget);
179
180        // before start
181        var1.setValue(mSource, 1000);
182        var2.setValue(mSource, 1300);
183        mEffect.performMapping(mSource);
184        verify(mTarget, times(1)).update(0f);
185        Mockito.reset(mTarget);
186
187        // unknown_before
188        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_BEFORE);
189        var2.setValue(mSource, Parallax.FloatProperty.UNKNOWN_BEFORE);
190        mEffect.performMapping(mSource);
191        verify(mTarget, times(1)).update(1f);
192        Mockito.reset(mTarget);
193
194        // unknown_before
195        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_BEFORE);
196        var2.setValue(mSource, -1000);
197        mEffect.performMapping(mSource);
198        verify(mTarget, times(1)).update(1f);
199        Mockito.reset(mTarget);
200
201        // unknown_after
202        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_AFTER);
203        var2.setValue(mSource, Parallax.FloatProperty.UNKNOWN_AFTER);
204        mEffect.performMapping(mSource);
205        verify(mTarget, times(1)).update(0f);
206        Mockito.reset(mTarget);
207
208        // unknown_after
209        var1.setValue(mSource, 1000);
210        var2.setValue(mSource, Parallax.FloatProperty.UNKNOWN_AFTER);
211        mEffect.performMapping(mSource);
212        verify(mTarget, times(1)).update(0f);
213        Mockito.reset(mTarget);
214
215        // unknown_before and less
216        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_BEFORE);
217        var2.setValue(mSource, 500);
218        mEffect.performMapping(mSource);
219        verify(mTarget, times(1)).update(1f);
220        Mockito.reset(mTarget);
221
222        // unknown_before and hit second
223        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_BEFORE);
224        var2.setValue(mSource, 540);
225        mEffect.performMapping(mSource);
226        verify(mTarget, times(1)).update(1f);
227        Mockito.reset(mTarget);
228
229        // unknown_before with estimation
230        var1.setValue(mSource, Parallax.FloatProperty.UNKNOWN_BEFORE);
231        var2.setValue(mSource, 1080);
232        mEffect.performMapping(mSource);
233        verify(mTarget, times(1)).update(0.5f);
234        Mockito.reset(mTarget);
235
236        // unknown_after with estimation
237        var1.setValue(mSource, 0);
238        var2.setValue(mSource, Parallax.FloatProperty.UNKNOWN_AFTER);
239        mEffect.performMapping(mSource);
240        verify(mTarget, times(1)).update(0.5f);
241        Mockito.reset(mTarget);
242    }
243
244    @Test
245    public void testDirectMapping() {
246        mScreenMax = 1080;
247        Parallax.FloatProperty var1 = mSource.addProperty("var1");
248
249        mEffect.setPropertyRanges(var1.atAbsolute((float) 540.45), var1.atAbsolute((float) 0.22));
250        Object object = new Object();
251        final float[] properValue = new float[1];
252        Property<Object, Float> property = new Property<Object, Float>(Float.class, "attr") {
253            @Override
254            public void set(Object object, Float value) {
255                properValue[0] = value;
256            }
257
258            @Override
259            public Float get(Object o) {
260                return properValue[0];
261            }
262        };
263        mTarget = new ParallaxTarget.DirectPropertyTarget<>(object, property);
264        mEffect.target(mTarget);
265
266        var1.setValue(mSource, (float) 540.45);
267        mEffect.performMapping(mSource);
268        assertFloatEquals((float) 540.45, properValue[0]);
269
270        var1.setValue(mSource, (float) 405.85);
271        mEffect.performMapping(mSource);
272        assertFloatEquals((float) 405.85, properValue[0]);
273
274        var1.setValue(mSource, 2000);
275        mEffect.performMapping(mSource);
276        assertFloatEquals((float) 540.45, properValue[0]);
277
278        var1.setValue(mSource, (float) 0.22);
279        mEffect.performMapping(mSource);
280        assertFloatEquals((float) 0.22, properValue[0]);
281
282        var1.setValue(mSource, (float) 0.12);
283        mEffect.performMapping(mSource);
284        assertFloatEquals((float) 0.22, properValue[0]);
285    }
286}
287