Home | History | Annotate | Download | only in setupwizardlib
      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;
     18 
     19 import static org.hamcrest.Matchers.instanceOf;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.junit.Assert.assertFalse;
     22 import static org.junit.Assert.assertNotNull;
     23 import static org.junit.Assert.assertNull;
     24 import static org.junit.Assert.assertSame;
     25 import static org.junit.Assert.assertThat;
     26 import static org.junit.Assert.assertTrue;
     27 import static org.robolectric.RuntimeEnvironment.application;
     28 
     29 import android.content.Context;
     30 import android.content.res.ColorStateList;
     31 import android.graphics.Color;
     32 import android.graphics.drawable.ColorDrawable;
     33 import android.graphics.drawable.Drawable;
     34 import android.os.Build;
     35 import android.os.Build.VERSION;
     36 import android.os.Build.VERSION_CODES;
     37 import android.support.annotation.IdRes;
     38 import android.view.ContextThemeWrapper;
     39 import android.view.View;
     40 import android.widget.ProgressBar;
     41 import android.widget.ScrollView;
     42 import android.widget.TextView;
     43 
     44 import com.android.setupwizardlib.robolectric.SuwLibRobolectricTestRunner;
     45 import com.android.setupwizardlib.template.ColoredHeaderMixin;
     46 import com.android.setupwizardlib.template.HeaderMixin;
     47 import com.android.setupwizardlib.template.IconMixin;
     48 import com.android.setupwizardlib.template.ProgressBarMixin;
     49 import com.android.setupwizardlib.view.StatusBarBackgroundLayout;
     50 
     51 import org.junit.Before;
     52 import org.junit.Test;
     53 import org.junit.runner.RunWith;
     54 import org.robolectric.Robolectric;
     55 import org.robolectric.annotation.Config;
     56 
     57 @RunWith(SuwLibRobolectricTestRunner.class)
     58 @Config(sdk = { Config.OLDEST_SDK, Config.NEWEST_SDK })
     59 public class GlifLayoutTest {
     60 
     61     private Context mContext;
     62 
     63     @Before
     64     public void setUp() throws Exception {
     65         mContext = new ContextThemeWrapper(application, R.style.SuwThemeGlif_Light);
     66     }
     67 
     68     @Test
     69     public void testDefaultTemplate() {
     70         GlifLayout layout = new GlifLayout(mContext);
     71         assertDefaultTemplateInflated(layout);
     72     }
     73 
     74     @Test
     75     public void testSetHeaderText() {
     76         GlifLayout layout = new GlifLayout(mContext);
     77         TextView title = (TextView) layout.findViewById(R.id.suw_layout_title);
     78         layout.setHeaderText("Abracadabra");
     79         assertEquals("Header text should be \"Abracadabra\"", "Abracadabra", title.getText());
     80     }
     81 
     82     @Test
     83     public void testAddView() {
     84         @IdRes int testViewId = 123456;
     85         GlifLayout layout = new GlifLayout(mContext);
     86         TextView tv = new TextView(mContext);
     87         tv.setId(testViewId);
     88         layout.addView(tv);
     89         assertDefaultTemplateInflated(layout);
     90         View view = layout.findViewById(testViewId);
     91         assertSame("The view added should be the same text view", tv, view);
     92     }
     93 
     94     @Test
     95     public void testGetScrollView() {
     96         GlifLayout layout = new GlifLayout(mContext);
     97         assertNotNull("Get scroll view should not be null with default template",
     98                 layout.getScrollView());
     99     }
    100 
    101     @Test
    102     public void testSetPrimaryColor() {
    103         GlifLayout layout = new GlifLayout(mContext);
    104         layout.setProgressBarShown(true);
    105         layout.setPrimaryColor(ColorStateList.valueOf(Color.RED));
    106         assertEquals("Primary color should be red",
    107                 ColorStateList.valueOf(Color.RED), layout.getPrimaryColor());
    108 
    109         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    110             ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress);
    111             assertEquals("Progress bar should be tinted red",
    112                     ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList());
    113             assertEquals("Determinate progress bar should also be tinted red",
    114                     ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList());
    115         }
    116     }
    117 
    118     @Config(qualifiers = "sw600dp")
    119     @Test
    120     public void testSetPrimaryColorTablet() {
    121         GlifLayout layout = new GlifLayout(mContext);
    122         layout.setProgressBarShown(true);
    123         layout.setPrimaryColor(ColorStateList.valueOf(Color.RED));
    124         assertEquals("Primary color should be red",
    125                 ColorStateList.valueOf(Color.RED), layout.getPrimaryColor());
    126 
    127         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    128             ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.suw_layout_progress);
    129             assertEquals("Progress bar should be tinted red",
    130                     ColorStateList.valueOf(Color.RED), progressBar.getIndeterminateTintList());
    131             assertEquals("Determinate progress bar should also be tinted red",
    132                     ColorStateList.valueOf(Color.RED), progressBar.getProgressBackgroundTintList());
    133         }
    134 
    135         assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor());
    136     }
    137 
    138     @Test
    139     public void testSetBackgroundBaseColor() {
    140         GlifLayout layout = new GlifLayout(mContext);
    141         layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE));
    142         layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED));
    143 
    144         assertEquals(Color.RED, ((GlifPatternDrawable) getPhoneBackground(layout)).getColor());
    145         assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor());
    146     }
    147 
    148     @Config(qualifiers = "sw600dp")
    149     @Test
    150     public void testSetBackgroundBaseColorTablet() {
    151         GlifLayout layout = new GlifLayout(mContext);
    152         layout.setPrimaryColor(ColorStateList.valueOf(Color.BLUE));
    153         layout.setBackgroundBaseColor(ColorStateList.valueOf(Color.RED));
    154 
    155         assertEquals(Color.RED, ((GlifPatternDrawable) getTabletBackground(layout)).getColor());
    156         assertEquals(Color.RED, layout.getBackgroundBaseColor().getDefaultColor());
    157     }
    158 
    159     @Test
    160     public void testSetBackgroundPatternedTrue() {
    161         GlifLayout layout = new GlifLayout(mContext);
    162         layout.setBackgroundPatterned(true);
    163 
    164         assertThat(getPhoneBackground(layout), instanceOf(GlifPatternDrawable.class));
    165         assertTrue("Background should be patterned", layout.isBackgroundPatterned());
    166     }
    167 
    168     @Test
    169     public void testSetBackgroundPatternedFalse() {
    170         GlifLayout layout = new GlifLayout(mContext);
    171         layout.setBackgroundPatterned(false);
    172 
    173         assertThat(getPhoneBackground(layout), instanceOf(ColorDrawable.class));
    174         assertFalse("Background should not be patterned", layout.isBackgroundPatterned());
    175     }
    176 
    177     @Config(qualifiers = "sw600dp")
    178     @Test
    179     public void testSetBackgroundPatternedTrueTablet() {
    180         GlifLayout layout = new GlifLayout(mContext);
    181         layout.setBackgroundPatterned(true);
    182 
    183         assertThat(getTabletBackground(layout), instanceOf(GlifPatternDrawable.class));
    184         assertTrue("Background should be patterned", layout.isBackgroundPatterned());
    185     }
    186 
    187     @Config(qualifiers = "sw600dp")
    188     @Test
    189     public void testSetBackgroundPatternedFalseTablet() {
    190         GlifLayout layout = new GlifLayout(mContext);
    191         layout.setBackgroundPatterned(false);
    192 
    193         assertThat(getTabletBackground(layout), instanceOf(ColorDrawable.class));
    194         assertFalse("Background should not be patterned", layout.isBackgroundPatterned());
    195     }
    196 
    197     @Test
    198     public void testNonGlifTheme() {
    199         mContext = new ContextThemeWrapper(application, android.R.style.Theme);
    200         new GlifLayout(mContext);
    201         // Inflating with a non-GLIF theme should not crash
    202     }
    203 
    204     @Test
    205     public void testPeekProgressBarNull() {
    206         GlifLayout layout = new GlifLayout(mContext);
    207         assertNull("PeekProgressBar should return null initially", layout.peekProgressBar());
    208     }
    209 
    210     @Test
    211     public void testPeekProgressBar() {
    212         GlifLayout layout = new GlifLayout(mContext);
    213         layout.setProgressBarShown(true);
    214         assertNotNull("Peek progress bar should return the bar after setProgressBarShown(true)",
    215                 layout.peekProgressBar());
    216     }
    217 
    218     @Test
    219     public void testMixins() {
    220         GlifLayout layout = new GlifLayout(mContext);
    221         final HeaderMixin header = layout.getMixin(HeaderMixin.class);
    222         assertTrue("Header should be instance of ColoredHeaderMixin. "
    223                 + "Found " + header.getClass() + " instead.", header instanceof ColoredHeaderMixin);
    224 
    225         assertNotNull("GlifLayout should have icon mixin", layout.getMixin(IconMixin.class));
    226         assertNotNull("GlifLayout should have progress bar mixin",
    227                 layout.getMixin(ProgressBarMixin.class));
    228     }
    229 
    230     @Test
    231     public void testInflateFooter() {
    232         GlifLayout layout = new GlifLayout(mContext);
    233 
    234         final View view = layout.inflateFooter(android.R.layout.simple_list_item_1);
    235         assertEquals(android.R.id.text1, view.getId());
    236         assertNotNull(layout.findViewById(android.R.id.text1));
    237     }
    238 
    239     @Config(qualifiers = "sw600dp")
    240     @Test
    241     public void testInflateFooterTablet() {
    242         testInflateFooter();
    243     }
    244 
    245     @Test
    246     public void testInflateFooterBlankTemplate() {
    247         GlifLayout layout = new GlifLayout(mContext, R.layout.suw_glif_blank_template);
    248 
    249         final View view = layout.inflateFooter(android.R.layout.simple_list_item_1);
    250         assertEquals(android.R.id.text1, view.getId());
    251         assertNotNull(layout.findViewById(android.R.id.text1));
    252     }
    253 
    254     @Config(qualifiers = "sw600dp")
    255     @Test
    256     public void testInflateFooterBlankTemplateTablet() {
    257         testInflateFooterBlankTemplate();
    258     }
    259 
    260     @Test
    261     public void testFooterXml() {
    262         GlifLayout layout = new GlifLayout(
    263                 mContext,
    264                 Robolectric.buildAttributeSet()
    265                         .addAttribute(R.attr.suwFooter, "@android:layout/simple_list_item_1")
    266                         .build());
    267 
    268         assertNotNull(layout.findViewById(android.R.id.text1));
    269     }
    270 
    271     @Test
    272     public void inflateStickyHeader_shouldAddViewToLayout() {
    273         GlifLayout layout = new GlifLayout(mContext);
    274 
    275         final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1);
    276         assertEquals(android.R.id.text1, view.getId());
    277         assertNotNull(layout.findViewById(android.R.id.text1));
    278     }
    279 
    280     @Config(qualifiers = "sw600dp")
    281     @Test
    282     public void inflateStickyHeader_whenOnTablets_shouldAddViewToLayout() {
    283         inflateStickyHeader_shouldAddViewToLayout();
    284     }
    285 
    286     @Test
    287     public void inflateStickyHeader_whenInXml_shouldAddViewToLayout() {
    288         GlifLayout layout = new GlifLayout(
    289                 mContext,
    290                 Robolectric.buildAttributeSet()
    291                         .addAttribute(R.attr.suwStickyHeader, "@android:layout/simple_list_item_1")
    292                         .build());
    293 
    294         assertNotNull(layout.findViewById(android.R.id.text1));
    295     }
    296 
    297     @Test
    298     public void inflateStickyHeader_whenOnBlankTemplate_shouldAddViewToLayout() {
    299         GlifLayout layout = new GlifLayout(mContext, R.layout.suw_glif_blank_template);
    300 
    301         final View view = layout.inflateStickyHeader(android.R.layout.simple_list_item_1);
    302         assertEquals(android.R.id.text1, view.getId());
    303         assertNotNull(layout.findViewById(android.R.id.text1));
    304     }
    305 
    306     @Config(qualifiers = "sw600dp")
    307     @Test
    308     public void inflateStickyHeader_whenOnBlankTemplateTablet_shouldAddViewToLayout() {
    309         inflateStickyHeader_whenOnBlankTemplate_shouldAddViewToLayout();
    310     }
    311 
    312     @Config(minSdk = Config.OLDEST_SDK, maxSdk = Config.NEWEST_SDK)
    313     @Test
    314     public void createFromXml_shouldSetLayoutFullscreen_whenLayoutFullscreenIsNotSet() {
    315         GlifLayout layout = new GlifLayout(
    316                 mContext,
    317                 Robolectric.buildAttributeSet()
    318                         .build());
    319         if (VERSION.SDK_INT >= VERSION_CODES.M) {
    320             assertEquals(
    321                     View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN,
    322                     layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    323         }
    324     }
    325 
    326     @Test
    327     public void createFromXml_shouldNotSetLayoutFullscreen_whenLayoutFullscreenIsFalse() {
    328         GlifLayout layout = new GlifLayout(
    329                 mContext,
    330                 Robolectric.buildAttributeSet()
    331                         .addAttribute(R.attr.suwLayoutFullscreen, "false")
    332                         .build());
    333 
    334         assertEquals(
    335                 0,
    336                 layout.getSystemUiVisibility() & View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    337     }
    338 
    339     private Drawable getPhoneBackground(GlifLayout layout) {
    340         final StatusBarBackgroundLayout patternBg =
    341                 (StatusBarBackgroundLayout) layout.findManagedViewById(R.id.suw_pattern_bg);
    342         return patternBg.getStatusBarBackground();
    343     }
    344 
    345     private Drawable getTabletBackground(GlifLayout layout) {
    346         final View patternBg = layout.findManagedViewById(R.id.suw_pattern_bg);
    347         return patternBg.getBackground();
    348     }
    349 
    350     private void assertDefaultTemplateInflated(GlifLayout layout) {
    351         View title = layout.findViewById(R.id.suw_layout_title);
    352         assertNotNull("@id/suw_layout_title should not be null", title);
    353 
    354         View icon = layout.findViewById(R.id.suw_layout_icon);
    355         assertNotNull("@id/suw_layout_icon should not be null", icon);
    356 
    357         View scrollView = layout.findViewById(R.id.suw_scroll_view);
    358         assertTrue("@id/suw_scroll_view should be a ScrollView", scrollView instanceof ScrollView);
    359     }
    360 }
    361