Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2017 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.verify;
     21 import static org.mockito.Mockito.when;
     22 
     23 import android.content.Context;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.view.View;
     28 
     29 import androidx.leanback.R;
     30 
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.mockito.Mockito;
     34 
     35 @SmallTest
     36 @RunWith(AndroidJUnit4.class)
     37 
     38 public class ThumbsBarTest {
     39     private Context mContext;
     40     private ThumbsBar mBar;
     41 
     42     /**
     43      * Check ThumbsBar's initialization based on the constructor
     44      */
     45     @Test
     46     public void checkThumbsBarInitialize() {
     47         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
     48         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
     49             @Override
     50             public void run() {
     51                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
     52             }
     53         });
     54         assertEquals(mBar.mThumbHeightInPixel, mContext.getResources().getDimensionPixelSize(
     55                 R.dimen.lb_playback_transport_thumbs_height));
     56         assertEquals(mBar.mThumbWidthInPixel, mContext.getResources().getDimensionPixelSize(
     57                 R.dimen.lb_playback_transport_thumbs_width));
     58         assertEquals(mBar.mHeroThumbHeightInPixel, mContext.getResources().getDimensionPixelSize(
     59                 R.dimen.lb_playback_transport_hero_thumbs_height));
     60         assertEquals(mBar.mHeroThumbWidthInPixel, mContext.getResources().getDimensionPixelSize(
     61                 R.dimen.lb_playback_transport_hero_thumbs_width));
     62     }
     63 
     64     /**
     65      * Check getHeroIndex method when input is an even number
     66      */
     67     @Test
     68     public void checkGetHeroIndexOnEvenNumber() {
     69         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
     70         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
     71             @Override
     72             public void run() {
     73                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
     74             }
     75         });
     76         int childCountForTest = 4;
     77         // according to the algorithm, hero thumb's index should be childCounts / 2
     78         int expectedHeroIndex = 2;
     79         when(mBar.getChildCount()).thenReturn(childCountForTest);
     80         assertEquals(mBar.getHeroIndex(), expectedHeroIndex);
     81     }
     82 
     83     /**
     84      * Check getHeroIndex method when input is an odd number.
     85      */
     86     @Test
     87     public void checkGetHeroIndexOnOddNumber() {
     88         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
     89         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
     90             @Override
     91             public void run() {
     92                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
     93             }
     94         });
     95         int childCountForTest = 5;
     96         // according to the algorithm, hero thumb's index should be childCounts / 2
     97         int expectedHeroIndex = 2;
     98         when(mBar.getChildCount()).thenReturn(childCountForTest);
     99         assertEquals(mBar.getHeroIndex(), expectedHeroIndex);
    100     }
    101 
    102     /**
    103      * Check setThumbSize method.
    104      */
    105     @Test
    106     public void checkSetThumbSize() {
    107         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    108         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    109             @Override
    110             public void run() {
    111                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
    112             }
    113         });
    114         int screenWidthInPixelForTest = 2560;
    115         int screenHeightInPixelForTest = 1600;
    116         int thumbsWidthInPixelForTest = 128;
    117         int thumbsHeightInPixelForTest = 256;
    118         // set screen size explicitly so the thumbs bar will have child view inside of it
    119         mBar.measure(View.MeasureSpec.makeMeasureSpec(screenWidthInPixelForTest,
    120                 View.MeasureSpec.EXACTLY),
    121                 View.MeasureSpec.makeMeasureSpec(screenHeightInPixelForTest,
    122                         View.MeasureSpec.EXACTLY));
    123 
    124         mBar.setThumbSize(thumbsWidthInPixelForTest, thumbsHeightInPixelForTest);
    125         // Verify the behavior of setThumbSize method
    126         assertEquals(mBar.mThumbWidthInPixel, thumbsWidthInPixelForTest);
    127         assertEquals(mBar.mThumbHeightInPixel, thumbsHeightInPixelForTest);
    128         // iterate through all child view to test if its width/ height has been set successfully
    129         for (int i = 0; i < mBar.getChildCount(); i++) {
    130             if (i != mBar.getHeroIndex()) {
    131                 assertEquals(mBar.getChildAt(i).getLayoutParams().width,
    132                         thumbsWidthInPixelForTest);
    133                 assertEquals(mBar.getChildAt(i).getLayoutParams().height,
    134                         thumbsHeightInPixelForTest);
    135             } else {
    136                 assertEquals(mBar.getChildAt(i).getLayoutParams().width,
    137                         mContext.getResources().getDimensionPixelSize(
    138                                 R.dimen.lb_playback_transport_hero_thumbs_width));
    139                 assertEquals(mBar.getChildAt(i).getLayoutParams().height,
    140                         mContext.getResources().getDimensionPixelSize(
    141                                 R.dimen.lb_playback_transport_hero_thumbs_height));
    142             }
    143         }
    144     }
    145 
    146     /**
    147      * Check setHeroThumbSize method.
    148      */
    149     @Test
    150     public void checkSetHeroThumbSize() {
    151         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    152         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    153             @Override
    154             public void run() {
    155                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
    156             }
    157         });
    158         int screenWidthInPixelForTest = 2560;
    159         int screenHeightInPixelForTest = 1600;
    160         int HeroThumbsWidthInPixelForTest = 256;
    161         int HeroThumbsHeightInPixelForTest = 512;
    162         // set screen size explicitly so the thumbs bar will have child view inside of it
    163         mBar.measure(View.MeasureSpec.makeMeasureSpec(
    164                 screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
    165                 View.MeasureSpec.makeMeasureSpec(
    166                         screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
    167         mBar.setHeroThumbSize(HeroThumbsWidthInPixelForTest, HeroThumbsHeightInPixelForTest);
    168         // Verify the behavior of setThumbSize method
    169         assertEquals(mBar.mHeroThumbWidthInPixel, HeroThumbsWidthInPixelForTest);
    170         assertEquals(mBar.mHeroThumbHeightInPixel, HeroThumbsHeightInPixelForTest);
    171         // iterate through all child view to test if its width/ height has been set successfully
    172         for (int i = 0; i < mBar.getChildCount(); i++) {
    173             if (i != mBar.getHeroIndex()) {
    174                 assertEquals(mBar.getChildAt(i).getLayoutParams().width,
    175                         mContext.getResources().getDimensionPixelSize(
    176                                 R.dimen.lb_playback_transport_thumbs_width));
    177                 assertEquals(mBar.getChildAt(i).getLayoutParams().height,
    178                         mContext.getResources().getDimensionPixelSize(
    179                                 R.dimen.lb_playback_transport_thumbs_height));
    180             } else {
    181                 assertEquals(mBar.getChildAt(i).getLayoutParams().width,
    182                         HeroThumbsWidthInPixelForTest);
    183                 assertEquals(mBar.getChildAt(i).getLayoutParams().height,
    184                         HeroThumbsHeightInPixelForTest);
    185             }
    186         }
    187     }
    188 
    189     /**
    190      * Check setThumbSpace method.
    191      */
    192     @Test
    193     public void checkSetThumbSpace() {
    194         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    195         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    196             @Override
    197             public void run() {
    198                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
    199             }
    200         });
    201         int thumbSpaceInPixelForTest = 48;
    202         mBar.setThumbSpace(thumbSpaceInPixelForTest);
    203         assertEquals(mBar.mMeasuredMarginInPixel, thumbSpaceInPixelForTest);
    204         verify(mBar).requestLayout();
    205     }
    206 
    207     /**
    208      * check calculateNumberOfThumbs method when the result from roundUp function is less than 2
    209      *
    210      * Firstly, to make sure the test cases can run on different devices with different screen
    211      * density (i.e. The return value from roundUp function should be the same no matter what kind
    212      * of device/ emulator is connected),
    213      * the screen width for test is set using dp, the pixel value will be computed by
    214      * multiplying context.getResources().getDisplayMetrics().density
    215      *
    216      * In this test case, the screen width is set to 240 in dp, so the calculation result should
    217      * be 1. According to the algorithm of calculateNumOfThumbs, it should be reassigned to 2 and
    218      * the final result should be 3 after counting the hero thumb.
    219      */
    220     @Test
    221     public void checkCalculateNumberOfThumbs1() {
    222         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    223         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    224             @Override
    225             public void run() {
    226                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
    227             }
    228         });
    229         int screenWidthInPixelForTest =
    230                 (int) (240 * mContext.getResources().getDisplayMetrics().density);
    231         int screenHeightInPixelForTest =
    232                 (int) (240 * mContext.getResources().getDisplayMetrics().density);
    233         int expectedChildCounts = 3;
    234         mBar.measure(View.MeasureSpec.makeMeasureSpec(
    235                 screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
    236                 View.MeasureSpec.makeMeasureSpec(
    237                         screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
    238         assertEquals(mBar.getChildCount(), expectedChildCounts);
    239     }
    240 
    241     /**
    242      * check calculateNumberOfThumbs method when the result from roundUp function is an odd number
    243      * and larger than 2.
    244      *
    245      * In this test case, the screen width is set to 680 in dp, so the calculation result should
    246      * be 3. According to the algorithm of calculateNumOfThumbs, it should be incremented by 1, so
    247      * the final result is 5 after counting the hero thumb.
    248      */
    249     @Test
    250     public void checkCalculateNumberOfThumbs2() {
    251         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    252         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    253             @Override
    254             public void run() {
    255                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
    256             }
    257         });
    258         int screenWidthInPixelForTest =
    259                 (int) (680 * mContext.getResources().getDisplayMetrics().density);
    260         int screenHeightInPixelForTest =
    261                 (int) (680 * mContext.getResources().getDisplayMetrics().density);
    262         int expectedChildCounts = 5;
    263         mBar.measure(View.MeasureSpec.makeMeasureSpec(
    264                 screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
    265                 View.MeasureSpec.makeMeasureSpec(
    266                         screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
    267         assertEquals(mBar.getChildCount(), expectedChildCounts);
    268     }
    269 
    270     /**
    271      * check calculateNumberOfThumbs method when the result from roundUp function is an even number
    272      * and larger than 2
    273      *
    274      * In this test case, the screen width is set to 800 in dp, so the calculation result should
    275      * be 4. Finally the result is expected to be 5 after counting the hero thumb.
    276      */
    277     @Test
    278     public void checkCalculateNumberOfThumbs3() {
    279         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    280         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    281             @Override
    282             public void run() {
    283                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
    284             }
    285         });
    286         int screenWidthInPixelForTest =
    287                 (int) (800 * mContext.getResources().getDisplayMetrics().density);
    288         int screenHeightInPixelForTest =
    289                 (int) (800 * mContext.getResources().getDisplayMetrics().density);
    290         int expectedChildCounts = 5;
    291         mBar.measure(View.MeasureSpec.makeMeasureSpec(
    292                 screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
    293                 View.MeasureSpec.makeMeasureSpec(
    294                         screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
    295         assertEquals(mBar.getChildCount(), expectedChildCounts);
    296     }
    297 
    298     /**
    299      * check setNumberOfThumbs method
    300      *
    301      * When user calling setNumberOfThumbs(int numOfThumbs) method. The flag mIsUserSets will be
    302      * toggled to true to honor user's choice, and the result of child view's number should
    303      * not be impacted by calculateNumberOfThumbs(int widthInPixel) method.
    304      *
    305      * In this test case, the screen width is set to 960 in dp, the calculation result from
    306      * calculateNumberOfThumbs method should be 5. But after calling setNumberOfThumbs function to
    307      * set thumbs' number to 3, this value should not impact the final result.
    308      */
    309     @Test
    310     public void checkSetNumberOfThumbs() {
    311         mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    312         InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
    313             @Override
    314             public void run() {
    315                 mBar = Mockito.spy(new ThumbsBar(mContext, null));
    316             }
    317         });
    318         int screenWidthInPixelForTest =
    319                 (int) (960 * mContext.getResources().getDisplayMetrics().density);
    320         int screenHeightInPixelForTest =
    321                 (int) (960 * mContext.getResources().getDisplayMetrics().density);
    322         int numberOfThumbs = 3;
    323         mBar.setNumberOfThumbs(numberOfThumbs);
    324         mBar.measure(View.MeasureSpec.makeMeasureSpec(
    325                 screenWidthInPixelForTest, View.MeasureSpec.EXACTLY),
    326                 View.MeasureSpec.makeMeasureSpec(
    327                         screenHeightInPixelForTest, View.MeasureSpec.EXACTLY));
    328         assertEquals(mBar.getChildCount(), numberOfThumbs);
    329     }
    330 }
    331