Home | History | Annotate | Download | only in widget
      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  */
     16 
     17 package androidx.leanback.widget;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.mockito.Mockito.times;
     21 import static org.mockito.Mockito.verify;
     22 
     23 import android.support.test.filters.SmallTest;
     24 import android.support.test.runner.AndroidJUnit4;
     25 import android.util.Property;
     26 
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 import org.mockito.Mock;
     31 import org.mockito.Mockito;
     32 import org.mockito.MockitoAnnotations;
     33 
     34 @RunWith(AndroidJUnit4.class)
     35 @SmallTest
     36 public class ParallaxIntEffectTest {
     37 
     38     Parallax<Parallax.IntProperty> mSource;
     39     int mScreenMax;
     40     ParallaxEffect.IntEffect mEffect;
     41     @Mock ParallaxTarget mTarget;
     42 
     43     static void assertFloatEquals(float expected, float actual) {
     44         org.junit.Assert.assertEquals((double) expected, (double) actual, 0.0001d);
     45     }
     46 
     47     @Before
     48     public void setUp() throws Exception {
     49         MockitoAnnotations.initMocks(this);
     50         mSource = new Parallax<Parallax.IntProperty>() {
     51 
     52             @Override
     53             public float getMaxValue() {
     54                 return mScreenMax;
     55             }
     56 
     57             @Override
     58             public IntProperty createProperty(String name, int index) {
     59                 return new IntProperty(name, index);
     60             }
     61         };
     62         mEffect = new ParallaxEffect.IntEffect();
     63     }
     64 
     65     @Test
     66     public void testOneVariable() {
     67         mScreenMax = 1080;
     68         Parallax.IntProperty var1 = mSource.addProperty("var1");
     69 
     70         mEffect.setPropertyRanges(var1.atAbsolute(540), var1.atAbsolute(0));
     71         mEffect.target(mTarget);
     72 
     73         // start
     74         var1.setValue(mSource, 540);
     75         mEffect.performMapping(mSource);
     76         verify(mTarget, times(1)).update(0f);
     77         Mockito.reset(mTarget);
     78 
     79         // 25% complete
     80         var1.setValue(mSource, 405);
     81         mEffect.performMapping(mSource);
     82         verify(mTarget, times(1)).update(0.25f);
     83         Mockito.reset(mTarget);
     84 
     85         // middle
     86         var1.setValue(mSource, 270);
     87         mEffect.performMapping(mSource);
     88         verify(mTarget, times(1)).update(.5f);
     89         Mockito.reset(mTarget);
     90 
     91         // 75% complete
     92         var1.setValue(mSource, 135);
     93         mEffect.performMapping(mSource);
     94         verify(mTarget, times(1)).update(0.75f);
     95         Mockito.reset(mTarget);
     96 
     97         // end
     98         var1.setValue(mSource, 0);
     99         mEffect.performMapping(mSource);
    100         verify(mTarget, times(1)).update(1f);
    101         Mockito.reset(mTarget);
    102 
    103         // after end
    104         var1.setValue(mSource, -1000);
    105         mEffect.performMapping(mSource);
    106         verify(mTarget, times(1)).update(1f);
    107         Mockito.reset(mTarget);
    108 
    109         // before start
    110         var1.setValue(mSource, 1000);
    111         mEffect.performMapping(mSource);
    112         verify(mTarget, times(1)).update(0f);
    113         Mockito.reset(mTarget);
    114 
    115         // unknown_before
    116         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
    117         mEffect.performMapping(mSource);
    118         verify(mTarget, times(1)).update(1f);
    119         Mockito.reset(mTarget);
    120 
    121         // unknown_after
    122         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_AFTER);
    123         mEffect.performMapping(mSource);
    124         verify(mTarget, times(1)).update(0f);
    125         Mockito.reset(mTarget);
    126     }
    127 
    128     @Test(expected=IllegalStateException.class)
    129     public void testVerifyKeyValueOfSameVariableInDesendantOrder() {
    130         mScreenMax = 1080;
    131         Parallax.IntProperty var1 = mSource.addProperty("var1");
    132 
    133         mEffect.setPropertyRanges(var1.atAbsolute(540), var1.atAbsolute(550));
    134         mEffect.target(mTarget);
    135         var1.setValue(mSource, 0);
    136         mEffect.performMapping(mSource);
    137     }
    138 
    139     @Test
    140     public void testTwoVariable() {
    141         mScreenMax = 1080;
    142         Parallax.IntProperty var1 = mSource.addProperty("var1");
    143         Parallax.IntProperty var2 = mSource.addProperty("var2");
    144 
    145         mEffect.setPropertyRanges(var1.atAbsolute(540), var2.atAbsolute(540));
    146         mEffect.target(mTarget);
    147 
    148         // start
    149         var1.setValue(mSource, 540);
    150         var2.setValue(mSource, 840);
    151         mEffect.performMapping(mSource);
    152         verify(mTarget, times(1)).update(0f);
    153         Mockito.reset(mTarget);
    154 
    155         // middle
    156         var1.setValue(mSource, 390);
    157         var2.setValue(mSource, 690);
    158         mEffect.performMapping(mSource);
    159         verify(mTarget, times(1)).update(.5f);
    160         Mockito.reset(mTarget);
    161 
    162         // end
    163         var1.setValue(mSource, 240);
    164         var2.setValue(mSource, 540);
    165         mEffect.performMapping(mSource);
    166         verify(mTarget, times(1)).update(1f);
    167         Mockito.reset(mTarget);
    168 
    169         // after end
    170         var1.setValue(mSource, 200);
    171         var2.setValue(mSource, 500);
    172         mEffect.performMapping(mSource);
    173         verify(mTarget, times(1)).update(1f);
    174         Mockito.reset(mTarget);
    175 
    176         // before start
    177         var1.setValue(mSource, 1000);
    178         var2.setValue(mSource, 1300);
    179         mEffect.performMapping(mSource);
    180         verify(mTarget, times(1)).update(0f);
    181         Mockito.reset(mTarget);
    182 
    183         // unknown_before
    184         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
    185         var2.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
    186         mEffect.performMapping(mSource);
    187         verify(mTarget, times(1)).update(1f);
    188         Mockito.reset(mTarget);
    189 
    190         // unknown_before
    191         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
    192         var2.setValue(mSource, -1000);
    193         mEffect.performMapping(mSource);
    194         verify(mTarget, times(1)).update(1f);
    195         Mockito.reset(mTarget);
    196 
    197         // unknown_after
    198         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_AFTER);
    199         var2.setValue(mSource, Parallax.IntProperty.UNKNOWN_AFTER);
    200         mEffect.performMapping(mSource);
    201         verify(mTarget, times(1)).update(0f);
    202         Mockito.reset(mTarget);
    203 
    204         // unknown_after
    205         var1.setValue(mSource, 1000);
    206         var2.setValue(mSource, Parallax.IntProperty.UNKNOWN_AFTER);
    207         mEffect.performMapping(mSource);
    208         verify(mTarget, times(1)).update(0f);
    209         Mockito.reset(mTarget);
    210 
    211         // unknown_before and less
    212         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
    213         var2.setValue(mSource, 500);
    214         mEffect.performMapping(mSource);
    215         verify(mTarget, times(1)).update(1f);
    216         Mockito.reset(mTarget);
    217 
    218         // unknown_before and hit second
    219         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
    220         var2.setValue(mSource, 540);
    221         mEffect.performMapping(mSource);
    222         verify(mTarget, times(1)).update(1f);
    223         Mockito.reset(mTarget);
    224 
    225         // unknown_before with estimation
    226         var1.setValue(mSource, Parallax.IntProperty.UNKNOWN_BEFORE);
    227         var2.setValue(mSource, 1080);
    228         mEffect.performMapping(mSource);
    229         verify(mTarget, times(1)).update(0.5f);
    230         Mockito.reset(mTarget);
    231 
    232         // unknown_after with estimation
    233         var1.setValue(mSource, 0);
    234         var2.setValue(mSource, Parallax.IntProperty.UNKNOWN_AFTER);
    235         mEffect.performMapping(mSource);
    236         verify(mTarget, times(1)).update(0.5f);
    237         Mockito.reset(mTarget);
    238     }
    239 
    240     @Test
    241     public void testDirectMapping() {
    242         mScreenMax = 1080;
    243         Parallax.IntProperty var1 = mSource.addProperty("var1");
    244 
    245         mEffect.setPropertyRanges(var1.atAbsolute((int) 540.45), var1.atAbsolute((int) 0.22));
    246         Object object = new Object();
    247         final int[] properValue = new int[1];
    248         Property<Object, Integer> property = new Property<Object, Integer>(Integer.class, "attr") {
    249             @Override
    250             public void set(Object object, Integer value) {
    251                 properValue[0] = value;
    252             }
    253 
    254             @Override
    255             public Integer get(Object o) {
    256                 return properValue[0];
    257             }
    258         };
    259         mTarget = new ParallaxTarget.DirectPropertyTarget<>(object, property);
    260         mEffect.target(mTarget);
    261 
    262         var1.setValue(mSource, (int) 540.45);
    263         mEffect.performMapping(mSource);
    264         assertEquals((int) 540.45, properValue[0]);
    265 
    266         var1.setValue(mSource, (int) 405.85);
    267         mEffect.performMapping(mSource);
    268         assertEquals((int) 405.85, properValue[0]);
    269 
    270         var1.setValue(mSource, 2000);
    271         mEffect.performMapping(mSource);
    272         assertEquals((int) 540.45, properValue[0]);
    273 
    274         var1.setValue(mSource, (int) 0.22);
    275         mEffect.performMapping(mSource);
    276         assertEquals((int) 0.22, properValue[0]);
    277 
    278         var1.setValue(mSource, (int) 0.12);
    279         mEffect.performMapping(mSource);
    280         assertEquals((int) 0.22, properValue[0]);
    281     }
    282 }
    283