Home | History | Annotate | Download | only in setupwizardlib
      1 /*
      2  * Copyright (C) 2018 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.car.setupwizardlib;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import android.app.Activity;
     22 import android.os.Bundle;
     23 import android.support.v4.app.Fragment;
     24 import android.view.View;
     25 import android.widget.Button;
     26 import android.widget.ImageView;
     27 
     28 import com.android.car.setupwizardlib.robolectric.BaseRobolectricTest;
     29 import com.android.car.setupwizardlib.robolectric.CarSetupWizardLibRobolectricTestRunner;
     30 import com.android.car.setupwizardlib.robolectric.TestHelper;
     31 
     32 import org.junit.Before;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 import org.mockito.Mockito;
     36 import org.robolectric.Robolectric;
     37 
     38 /**
     39  * Unit tests for the {@link BaseActivity}.
     40  */
     41 @RunWith(CarSetupWizardLibRobolectricTestRunner.class)
     42 public class BaseActivityTest extends BaseRobolectricTest {
     43     BaseActivity mBaseActivity;
     44     CarSetupWizardLayout mCarSetupWizardLayout;
     45 
     46     @Before
     47     public void setupBaseActivityAndLayout() {
     48         mBaseActivity = (BaseActivity) Robolectric.buildActivity(BaseActivity.class).create().get();
     49         mCarSetupWizardLayout = mBaseActivity.getCarSetupWizardLayout();
     50     }
     51 
     52     /**
     53      * Test that the BaseActivity's content view is set to be a CarSetupWizardLayout
     54      */
     55     @Test
     56     public void testContentViewIsCarSetupWizardLayout() {
     57         View contentView = mBaseActivity.findViewById(R.id.car_setup_wizard_layout);
     58         assertThat(contentView).isNotNull();
     59         assertThat(contentView instanceof CarSetupWizardLayout).isTrue();
     60     }
     61 
     62     private BaseActivity createSpyBaseActivity() {
     63         BaseActivity spyBaseActivity = Mockito.spy(
     64                 (BaseActivity) Robolectric.buildActivity(BaseActivity.class).get());
     65         spyBaseActivity.onCreate(null);
     66 
     67         return spyBaseActivity;
     68     }
     69 
     70     /**
     71      * Test that the BaseActivity sets the back button listener to call
     72      * {@link BaseActivity#handleBackButton()} when created.
     73      */
     74     @Test
     75     public void testBackButtonListenerIsDefault() {
     76         BaseActivity spyBaseActivity = createSpyBaseActivity();
     77 
     78         ImageView backButton = (ImageView) spyBaseActivity.findViewById(
     79                 R.id.back_button);
     80         backButton.performClick();
     81 
     82         Mockito.verify(spyBaseActivity).handleBackButton();
     83     }
     84 
     85     /**
     86      * Test that the BaseActivity sets the secondary toolbar button listener to the default when
     87      * created.
     88      */
     89     @Test
     90     public void testSecondaryToolbarButtonListenerIsDefault() {
     91         BaseActivity spyBaseActivity = createSpyBaseActivity();
     92 
     93         Button secondaryToolBarButton = (Button) spyBaseActivity.findViewById(
     94                 R.id.secondary_toolbar_button);
     95         secondaryToolBarButton.performClick();
     96 
     97         Mockito.verify(spyBaseActivity).nextAction(Activity.RESULT_OK);
     98     }
     99 
    100 
    101     /**
    102      * Test that the BaseActivity sets the primary toolbar button listener to the default when
    103      * created.
    104      */
    105     @Test
    106     public void testPrimaryToolbarButtonListenerIsDefault() {
    107         BaseActivity spyBaseActivity = createSpyBaseActivity();
    108 
    109         Button primaryToolBarButton = (Button) spyBaseActivity.findViewById(
    110                 R.id.primary_toolbar_button);
    111         primaryToolBarButton.performClick();
    112 
    113         Mockito.verify(spyBaseActivity).nextAction(Activity.RESULT_OK);
    114     }
    115 
    116     private BaseActivity getStartedBaseActivity() {
    117         return (BaseActivity) Robolectric.buildActivity(BaseActivity.class).create().start().get();
    118     }
    119 
    120     private BaseActivity getSavedInstanceStateBaseActivity() {
    121         return (BaseActivity) Robolectric.buildActivity(
    122                 BaseActivity.class).create().saveInstanceState(new Bundle()).get();
    123     }
    124 
    125     /**
    126      * Test that fragment commits are allowed after {@link BaseActivity#onStart()} is called.
    127      */
    128     @Test
    129     public void testFragmentCommitsAllowedAfterOnStart() {
    130         assertThat(getStartedBaseActivity().getAllowFragmentCommits()).isTrue();
    131     }
    132 
    133     /**
    134      * Test that fragment commits are not allowed after {@link BaseActivity#onSaveInstanceState }
    135      * is called.
    136      */
    137     @Test
    138     public void testFragmentCommitsNotAllowedAfterOnSavedInstanceState() {
    139         assertThat(getSavedInstanceStateBaseActivity().getAllowFragmentCommits()).isFalse();
    140     }
    141 
    142 
    143     /**
    144      * Test that {@link BaseActivity#setContentFragment} sets the content fragment and calls the
    145      * expected methods when fragment commits are allowed.
    146      */
    147     @Test
    148     public void testSetContentFragmentWhenFragmentCommitsAllowed() {
    149         BaseActivity spyBaseActivity = Mockito.spy(getStartedBaseActivity());
    150 
    151         Fragment fragment = new Fragment();
    152         spyBaseActivity.setContentFragment(fragment);
    153 
    154         assertThat(spyBaseActivity.getContentFragment()).isEqualTo(fragment);
    155         assertThat(spyBaseActivity.getSupportFragmentManager().getBackStackEntryCount()).isEqualTo(
    156                 0);
    157         // Verify that onContentFragmentSet is called with the test fragment
    158         Mockito.verify(spyBaseActivity).onContentFragmentSet(fragment);
    159     }
    160 
    161     /**
    162      * Test that {@link BaseActivity#setContentFragment} does nothing when fragment commits are not
    163      * allowed.
    164      */
    165     @Test
    166     public void testSetContentFragmentWhenFragmentCommitsNotAllowed() {
    167         BaseActivity spyBaseActivity = Mockito.spy(getSavedInstanceStateBaseActivity());
    168 
    169         Fragment fragment = new Fragment();
    170         spyBaseActivity.setContentFragment(fragment);
    171 
    172         assertThat(spyBaseActivity.getContentFragment()).isEqualTo(null);
    173         assertThat(spyBaseActivity.getSupportFragmentManager().getBackStackEntryCount()).isEqualTo(
    174                 0);
    175         // Verify that onContentFragmentSet is not called
    176         Mockito.verify(spyBaseActivity, Mockito.times(0)).onContentFragmentSet(fragment);
    177     }
    178 
    179     /**
    180      * Test that {@link BaseActivity#setContentFragmentWithBackstack)} sets the content fragment,
    181      * adds it to the fragment backstack, and calls the expected methods when fragment commits
    182      * are allowed.
    183      */
    184     @Test
    185     public void testSetContentFragmentWithBackstackWhenFragmentCommitsAllowed() {
    186         BaseActivity spyBaseActivity = Mockito.spy(getStartedBaseActivity());
    187 
    188         Fragment fragment = new Fragment();
    189         spyBaseActivity.setContentFragmentWithBackstack(fragment);
    190 
    191         assertThat(spyBaseActivity.getContentFragment()).isEqualTo(fragment);
    192         assertThat(spyBaseActivity.getSupportFragmentManager().getBackStackEntryCount()).isEqualTo(
    193                 1);
    194         // Verify that onContentFragmentSet is called with the test fragment
    195         Mockito.verify(spyBaseActivity).onContentFragmentSet(fragment);
    196     }
    197 
    198     /**
    199      * Test that {@link BaseActivity#setContentFragmentWithBackstack)} does nothing when fragment
    200      * commits are not allowed.
    201      */
    202     @Test
    203     public void testSetContentFragmentWithBackstackWhenFragmentCommitsNotAllowed() {
    204         BaseActivity spyBaseActivity = Mockito.spy(getSavedInstanceStateBaseActivity());
    205 
    206         Fragment fragment = new Fragment();
    207         spyBaseActivity.setContentFragment(fragment);
    208 
    209         assertThat(spyBaseActivity.getContentFragment()).isEqualTo(null);
    210         assertThat(spyBaseActivity.getSupportFragmentManager().getBackStackEntryCount()).isEqualTo(
    211                 0);
    212         // Verify that onContentFragmentSet is not called
    213         Mockito.verify(spyBaseActivity, Mockito.times(0)).onContentFragmentSet(fragment);
    214     }
    215 
    216     /**
    217      * Test that {@link BaseActivity#popBackStackImmediate()} returns false when no fragment is
    218      * added to the backstack.
    219      */
    220     @Test
    221     public void testPopBackStackImmediateWithEmptyStack() {
    222         assertThat(mBaseActivity.popBackStackImmediate()).isEqualTo(false);
    223     }
    224 
    225     /**
    226      * Test that {@link BaseActivity#popBackStackImmediate()} returns true when a fragment is
    227      * added to the backstack and that the fragment is popped off of the backstack.
    228      */
    229     @Test
    230     public void testPopBackStackImmediateWithFragmentInStack() {
    231         Fragment fragment = new Fragment();
    232         mBaseActivity.setContentFragmentWithBackstack(fragment);
    233         assertThat(mBaseActivity.popBackStackImmediate()).isEqualTo(true);
    234 
    235         assertThat(mBaseActivity.getContentFragment()).isNull();
    236     }
    237 
    238     /**
    239      * Test that {@link BaseActivity#getContentFragment()} returns the content fragment.
    240      */
    241     @Test
    242     public void testGetContentFragment() {
    243         Fragment fragment = new Fragment();
    244         mBaseActivity.setContentFragment(fragment);
    245 
    246         assertThat(mBaseActivity.getContentFragment()).isEqualTo(
    247                 mBaseActivity.getSupportFragmentManager().findFragmentByTag(
    248                         mBaseActivity.CONTENT_FRAGMENT_TAG));
    249     }
    250 
    251     /**
    252      * Test that {@link BaseActivity#setContentLayout} adds the specified layout to the
    253      * BaseActivity.
    254      */
    255     @Test
    256     public void testSetContentLayout() {
    257         mBaseActivity.setContentLayout(R.layout.base_activity_test_layout);
    258         View contentLayout = mBaseActivity.findViewById(R.id.content_layout);
    259         assertThat(contentLayout).isNotNull();
    260     }
    261 
    262     /**
    263      * Test that {@link BaseActivity#finishAction()} results in a call to
    264      * {@link BaseActivity#finish}.
    265      */
    266     @Test
    267     public void testFinishAction() {
    268         BaseActivity spyBaseActivity = Mockito.spy(mBaseActivity);
    269         spyBaseActivity.finishAction();
    270 
    271         Mockito.verify(spyBaseActivity).finish();
    272     }
    273 
    274     /**
    275      * Test that {@link BaseActivity#finishAction(int)} )} results in a call to
    276      * {@link BaseActivity#nextAction} and {@link BaseActivity#finish}.
    277      */
    278     @Test
    279     public void testFinishActionWithResultCode() {
    280         BaseActivity spyBaseActivity = Mockito.spy(mBaseActivity);
    281         spyBaseActivity.finishAction(BaseActivity.RESULT_OK);
    282 
    283         Mockito.verify(spyBaseActivity).nextAction(BaseActivity.RESULT_OK, null);
    284         Mockito.verify(spyBaseActivity).finish();
    285     }
    286 
    287     /**
    288      * Test that {@link BaseActivity#setBackButtonVisible} sets the back button visible/not visible.
    289      */
    290     @Test
    291     public void testSetBackButtonVisibleTrue() {
    292         mBaseActivity.setBackButtonVisible(true);
    293         TestHelper.assertViewVisible(mCarSetupWizardLayout.getBackButton());
    294     }
    295 
    296     /**
    297      * Test that {@link BaseActivity#setBackButtonVisible} sets the back button visible/not visible.
    298      */
    299     @Test
    300     public void testSetBackButtonVisibleFalse() {
    301         mBaseActivity.setBackButtonVisible(false);
    302         TestHelper.assertViewNotVisible(mCarSetupWizardLayout.getBackButton());
    303     }
    304 
    305     /**
    306      * Test that {@link BaseActivity#setToolbarTitleVisible} sets the toolbar title visible/not
    307      * visible.
    308      */
    309     @Test
    310     public void testSetToolbarTitleVisibleTrue() {
    311         mBaseActivity.setToolbarTitleVisible(true);
    312         TestHelper.assertViewVisible(mCarSetupWizardLayout.getToolbarTitle());
    313     }
    314 
    315     /**
    316      * Test that {@link BaseActivity#setToolbarTitleVisible} sets the toolbar button visible/not
    317      * visible.
    318      */
    319     @Test
    320     public void testSetToolbarTitleVisibleFalse() {
    321         mBaseActivity.setToolbarTitleVisible(false);
    322         TestHelper.assertViewNotVisible(mCarSetupWizardLayout.getToolbarTitle());
    323     }
    324 
    325     /**
    326      * Test that {@link BaseActivity#setToolbarTitleText(String)} sets the toolbar title text.
    327      */
    328     @Test
    329     public void testSetToolbarTitleText() {
    330         mBaseActivity.setToolbarTitleText("title text");
    331         TestHelper.assertTextEqual(mCarSetupWizardLayout.getToolbarTitle(), "title text");
    332     }
    333 
    334     /**
    335      * Test that {@link BaseActivity#setPrimaryToolbarButtonVisible} sets the primary toolbar button
    336      * visible/not visible.
    337      */
    338     @Test
    339     public void testSetPrimaryToolbarButtonVisibleTrue() {
    340         mBaseActivity.setPrimaryToolbarButtonVisible(true);
    341         TestHelper.assertViewVisible(mCarSetupWizardLayout.getPrimaryToolbarButton());
    342     }
    343 
    344     /**
    345      * Test that {@link BaseActivity#setPrimaryToolbarButtonVisible} sets the primary toolbar button
    346      * visible/not visible.
    347      */
    348     @Test
    349     public void testSetPrimaryToolbarButtonVisibleFalse() {
    350         mBaseActivity.setPrimaryToolbarButtonVisible(false);
    351         TestHelper.assertViewNotVisible(mCarSetupWizardLayout.getPrimaryToolbarButton());
    352     }
    353 
    354     /**
    355      * Test that {@link BaseActivity#setPrimaryToolbarButtonEnabled} sets the primary toolbar button
    356      * visible/not visible.
    357      */
    358     @Test
    359     public void testSetPrimaryToolbarButtonEnabledTrue() {
    360         mBaseActivity.setPrimaryToolbarButtonEnabled(true);
    361         TestHelper.assertViewEnabled(mCarSetupWizardLayout.getPrimaryToolbarButton());
    362     }
    363 
    364     /**
    365      * Test that {@link BaseActivity#setPrimaryToolbarButtonEnabled} sets the primary toolbar button
    366      * visible/not visible.
    367      */
    368     @Test
    369     public void testSetPrimaryToolbarButtonEnabledFalse() {
    370         mBaseActivity.setPrimaryToolbarButtonEnabled(false);
    371         TestHelper.assertViewNotEnabled(mCarSetupWizardLayout.getPrimaryToolbarButton());
    372     }
    373 
    374     /**
    375      * Test that {@link BaseActivity#setPrimaryToolbarButtonText(String)} sets the primary
    376      * toolbar title text.
    377      */
    378     @Test
    379     public void testSetPrimaryToolbarButtonText() {
    380         mBaseActivity.setPrimaryToolbarButtonText("button text");
    381         TestHelper.assertTextEqual(mCarSetupWizardLayout.getPrimaryToolbarButton(), "button text");
    382     }
    383 
    384     /**
    385      * Test that {@link BaseActivity#setPrimaryToolbarButtonFlat(boolean)} sets the primary toolbar
    386      * button flat/not flat.
    387      */
    388     @Test
    389     public void testSetPrimaryToolbarButtonFlatTrue() {
    390         mBaseActivity.setPrimaryToolbarButtonFlat(true);
    391         assertThat(mCarSetupWizardLayout.getPrimaryToolbarButtonFlat()).isTrue();
    392     }
    393 
    394     /**
    395      * Test that {@link BaseActivity#setPrimaryToolbarButtonFlat(boolean)} sets the primary toolbar
    396      * button flat/not flat.
    397      */
    398     @Test
    399     public void testSetPrimaryToolbarButtonFlatFalse() {
    400         mBaseActivity.setPrimaryToolbarButtonFlat(false);
    401         assertThat(mCarSetupWizardLayout.getPrimaryToolbarButtonFlat()).isFalse();
    402     }
    403 
    404     /**
    405      * Test that {@link BaseActivity#setPrimaryToolbarButtonOnClickListener} sets the primary
    406      * toolbar button's click listener.
    407      */
    408     @Test
    409     public void testSetPrimaryToolbarButtonOnClickListener() {
    410         View.OnClickListener spyListener = TestHelper.createSpyListener();
    411 
    412         mBaseActivity.setPrimaryToolbarButtonOnClickListener(spyListener);
    413         mBaseActivity.getCarSetupWizardLayout().getPrimaryToolbarButton().performClick();
    414         Mockito.verify(spyListener).onClick(Mockito.any());
    415     }
    416 
    417     /**
    418      * Test that {@link BaseActivity#setSecondaryToolbarButtonVisible} sets the secondary toolbar
    419      * button visible/not visible.
    420      */
    421     @Test
    422     public void testSetSecondaryToolbarButtonVisibleTrue() {
    423         mBaseActivity.setSecondaryToolbarButtonVisible(true);
    424         TestHelper.assertViewVisible(mCarSetupWizardLayout.getSecondaryToolbarButton());
    425     }
    426 
    427     /**
    428      * Test that {@link BaseActivity#setSecondaryToolbarButtonVisible} sets the secondary toolbar
    429      * button visible/not visible.
    430      */
    431     @Test
    432     public void testSetSecondaryToolbarButtonVisibleFalse() {
    433         mBaseActivity.setSecondaryToolbarButtonVisible(false);
    434         TestHelper.assertViewNotVisible(mCarSetupWizardLayout.getSecondaryToolbarButton());
    435     }
    436 
    437     /**
    438      * Test that {@link BaseActivity#setSecondaryToolbarButtonEnabled} sets the secondary toolbar
    439      * button visible/not visible.
    440      */
    441     @Test
    442     public void testSetSecondaryToolbarButtonEnabledTrue() {
    443         mBaseActivity.setSecondaryToolbarButtonEnabled(true);
    444         TestHelper.assertViewEnabled(mCarSetupWizardLayout.getSecondaryToolbarButton());
    445     }
    446 
    447     /**
    448      * Test that {@link BaseActivity#setSecondaryToolbarButtonEnabled} sets the secondary toolbar
    449      * button visible/not visible.
    450      */
    451     @Test
    452     public void testSetSecondaryToolbarButtonEnabledFalse() {
    453         mBaseActivity.setSecondaryToolbarButtonEnabled(false);
    454         TestHelper.assertViewNotEnabled(mCarSetupWizardLayout.getSecondaryToolbarButton());
    455     }
    456 
    457     /**
    458      * Test that {@link BaseActivity#setSecondaryToolbarButtonText(String)} sets the secondary
    459      * toolbar title text.
    460      */
    461     @Test
    462     public void testSetSecondaryToolbarButtonText() {
    463         mBaseActivity.setSecondaryToolbarButtonText("button text");
    464         TestHelper.assertTextEqual(mCarSetupWizardLayout.getSecondaryToolbarButton(),
    465                 "button text");
    466     }
    467 
    468     /**
    469      * Test that {@link BaseActivity#setSecondaryToolbarButtonOnClickListener} sets the secondary
    470      * toolbar button's click listener.
    471      */
    472     @Test
    473     public void testSetSecondaryToolbarButtonOnClickListener() {
    474         View.OnClickListener spyListener = TestHelper.createSpyListener();
    475 
    476         mBaseActivity.setSecondaryToolbarButtonOnClickListener(spyListener);
    477         mBaseActivity.getCarSetupWizardLayout().getSecondaryToolbarButton().performClick();
    478         Mockito.verify(spyListener).onClick(Mockito.any());
    479     }
    480 
    481     /**
    482      * Test that {@link BaseActivity#setProgressBarVisible} sets the progressbar visible/not
    483      * visible.
    484      */
    485     @Test
    486     public void testSetProgressBarVisibleTrue() {
    487         mBaseActivity.setProgressBarVisible(true);
    488         TestHelper.assertViewVisible(mCarSetupWizardLayout.getProgressBar());
    489     }
    490 
    491     /**
    492      * Test that {@link BaseActivity#setProgressBarVisible} sets the progressbar visible/not
    493      * visible.
    494      */
    495     @Test
    496     public void testSetProgressBarVisibleFalse() {
    497         mBaseActivity.setProgressBarVisible(false);
    498         TestHelper.assertViewNotVisible(mCarSetupWizardLayout.getProgressBar());
    499     }
    500 }
    501