Home | History | Annotate | Download | only in template
      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 com.android.setupwizardlib.template;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNull;
     21 import static org.junit.Assert.assertSame;
     22 import static org.mockito.AdditionalAnswers.delegatesTo;
     23 import static org.mockito.Matchers.eq;
     24 import static org.mockito.Mockito.doReturn;
     25 import static org.mockito.Mockito.mock;
     26 import static org.mockito.Mockito.spy;
     27 import static org.mockito.Mockito.verifyNoMoreInteractions;
     28 
     29 import android.content.Context;
     30 import android.graphics.Rect;
     31 import android.graphics.drawable.Drawable;
     32 import android.graphics.drawable.InsetDrawable;
     33 import android.os.Build.VERSION;
     34 import android.os.Build.VERSION_CODES;
     35 import android.support.test.InstrumentationRegistry;
     36 import android.support.test.filters.SmallTest;
     37 import android.support.test.runner.AndroidJUnit4;
     38 import android.view.View;
     39 import android.widget.ListAdapter;
     40 import android.widget.ListView;
     41 
     42 import com.android.setupwizardlib.TemplateLayout;
     43 import com.android.setupwizardlib.test.R;
     44 
     45 import org.junit.Before;
     46 import org.junit.Test;
     47 import org.junit.runner.RunWith;
     48 import org.mockito.Mock;
     49 import org.mockito.MockitoAnnotations;
     50 
     51 @RunWith(AndroidJUnit4.class)
     52 @SmallTest
     53 public class ListMixinTest {
     54 
     55     private Context mContext;
     56     private TemplateLayout mTemplateLayout;
     57 
     58     private ListView mListView;
     59 
     60     @Mock
     61     private ListAdapter mAdapter;
     62 
     63     @Before
     64     public void setUp() {
     65         MockitoAnnotations.initMocks(this);
     66 
     67         mContext = InstrumentationRegistry.getTargetContext();
     68         mTemplateLayout = spy(new TemplateLayout(mContext, R.layout.test_template,
     69                 R.id.suw_layout_content));
     70 
     71         mListView = mock(ListView.class, delegatesTo(new ListView(mContext)));
     72         doReturn(1).when(mAdapter).getViewTypeCount();
     73 
     74         doReturn(mListView).when(mTemplateLayout)
     75                 .findManagedViewById(eq(android.R.id.list));
     76         doReturn(true).when(mTemplateLayout).isLayoutDirectionResolved();
     77     }
     78 
     79     @Test
     80     public void testGetListView() {
     81         ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
     82         assertSame(mListView, mixin.getListView());
     83     }
     84 
     85     @Test
     86     public void testGetAdapter() {
     87         mListView.setAdapter(mAdapter);
     88 
     89         ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
     90         assertSame(mAdapter, mixin.getAdapter());
     91     }
     92 
     93     @Test
     94     public void testSetAdapter() {
     95         assertNull(mListView.getAdapter());
     96 
     97         ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
     98         mixin.setAdapter(mAdapter);
     99 
    100         assertSame(mAdapter, mListView.getAdapter());
    101     }
    102 
    103     @Test
    104     public void testDividerInsetLegacy() {
    105         ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
    106         mixin.setDividerInset(123);
    107 
    108         assertEquals(123, mixin.getDividerInset());
    109 
    110         final Drawable divider = mListView.getDivider();
    111         InsetDrawable insetDrawable = (InsetDrawable) divider;
    112         Rect rect = new Rect();
    113         insetDrawable.getPadding(rect);
    114 
    115         assertEquals(new Rect(123, 0, 0, 0), rect);
    116     }
    117 
    118     @Test
    119     public void testDividerInsets() {
    120         ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
    121         mixin.setDividerInsets(123, 456);
    122 
    123         assertEquals(123, mixin.getDividerInsetStart());
    124         assertEquals(456, mixin.getDividerInsetEnd());
    125 
    126         final Drawable divider = mListView.getDivider();
    127         InsetDrawable insetDrawable = (InsetDrawable) divider;
    128         Rect rect = new Rect();
    129         insetDrawable.getPadding(rect);
    130 
    131         assertEquals(new Rect(123, 0, 456, 0), rect);
    132     }
    133 
    134     @Test
    135     public void testDividerInsetLegacyRtl() {
    136         if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
    137             doReturn(View.LAYOUT_DIRECTION_RTL).when(mTemplateLayout).getLayoutDirection();
    138 
    139             ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
    140             mixin.setDividerInset(123);
    141 
    142             assertEquals(123, mixin.getDividerInset());
    143 
    144             final Drawable divider = mListView.getDivider();
    145             InsetDrawable insetDrawable = (InsetDrawable) divider;
    146             Rect rect = new Rect();
    147             insetDrawable.getPadding(rect);
    148 
    149             assertEquals(new Rect(0, 0, 123, 0), rect);
    150         }
    151         // else the test passes
    152     }
    153 
    154     @Test
    155     public void testDividerInsetsRtl() {
    156         if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
    157             doReturn(View.LAYOUT_DIRECTION_RTL).when(mTemplateLayout).getLayoutDirection();
    158 
    159             ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
    160             mixin.setDividerInsets(123, 456);
    161 
    162             assertEquals(123, mixin.getDividerInsetStart());
    163             assertEquals(456, mixin.getDividerInsetEnd());
    164 
    165             final Drawable divider = mListView.getDivider();
    166             InsetDrawable insetDrawable = (InsetDrawable) divider;
    167             Rect rect = new Rect();
    168             insetDrawable.getPadding(rect);
    169 
    170             assertEquals(new Rect(456, 0, 123, 0), rect);
    171         }
    172         // else the test passes
    173     }
    174 
    175     @Test
    176     public void testNoList() {
    177         doReturn(null).when(mTemplateLayout).findManagedViewById(eq(android.R.id.list));
    178 
    179         ListMixin mixin = new ListMixin(mTemplateLayout, null, 0);
    180 
    181         mixin.setAdapter(mAdapter);
    182         mixin.setDividerInset(123);
    183 
    184         assertNull(mixin.getListView());
    185         assertNull(mixin.getAdapter());
    186         mixin.getDividerInset(); // Test that it doesn't crash. The return value is not significant.
    187         assertNull(mixin.getDivider());
    188 
    189         verifyNoMoreInteractions(mListView);
    190     }
    191 }
    192