Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2015 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 package androidx.leanback.widget;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 
     20 import android.support.test.filters.SmallTest;
     21 import android.support.test.runner.AndroidJUnit4;
     22 
     23 import org.junit.Test;
     24 import org.junit.runner.RunWith;
     25 
     26 /**
     27  * Testing StaggeredGridDefault algorithm
     28  */
     29 @SmallTest
     30 @RunWith(AndroidJUnit4.class)
     31 public class StaggeredGridDefaultTest extends GridTest {
     32 
     33     StaggeredGridDefault mStaggeredGrid;
     34 
     35     @Test
     36     public void testWhenToFillNextRow()  {
     37         mProvider = new Provider(new int[]{100, 100, 100, 100, 40, 100, 100, 30, 100});
     38 
     39         // layout first 8 items then all items
     40         mStaggeredGrid = new StaggeredGridDefault();
     41         mStaggeredGrid.setNumRows(3);
     42         mStaggeredGrid.setSpacing(20);
     43         mStaggeredGrid.setProvider(mProvider);
     44         mStaggeredGrid.appendVisibleItems(210);
     45         assertEquals(dump(mStaggeredGrid) + " Should fill 8 items",
     46                 8, mStaggeredGrid.mLocations.size());
     47         // 2nd fill rest
     48         mStaggeredGrid.appendVisibleItems(100000);
     49         assertEquals(dump(mStaggeredGrid) + " Should fill 9 items",
     50                 9, mStaggeredGrid.mLocations.size());
     51         int row_result1 = mStaggeredGrid.getLocation(8).row;
     52         assertEquals(dump(mStaggeredGrid) + " last item should be placed on row 1",
     53                 1, row_result1);
     54 
     55         // layout all items together
     56         mStaggeredGrid = new StaggeredGridDefault();
     57         mStaggeredGrid.setNumRows(3);
     58         mStaggeredGrid.setSpacing(20);
     59         mStaggeredGrid.setProvider(mProvider);
     60         mStaggeredGrid.appendVisibleItems(100000);
     61         assertEquals(dump(mStaggeredGrid) + " should fill 9 items",
     62                 9, mStaggeredGrid.mLocations.size());
     63         int row_result2 = mStaggeredGrid.getLocation(8).row;
     64 
     65         assertEquals(dump(mStaggeredGrid) + " last item should be placed on row 1",
     66                 1, row_result2);
     67     }
     68 }
     69