Home | History | Annotate | Download | only in app
      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.appcompat.app;
     17 
     18 import static android.support.test.espresso.Espresso.onView;
     19 import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
     20 import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
     21 import static android.support.test.espresso.matcher.ViewMatchers.withId;
     22 
     23 import static org.hamcrest.CoreMatchers.allOf;
     24 import static org.mockito.Mockito.mock;
     25 
     26 import android.support.test.annotation.UiThreadTest;
     27 import android.support.test.espresso.UiController;
     28 import android.support.test.espresso.ViewAction;
     29 import android.support.test.filters.SmallTest;
     30 import android.support.test.rule.ActivityTestRule;
     31 import android.support.test.runner.AndroidJUnit4;
     32 import android.view.View;
     33 import android.view.ViewStub;
     34 
     35 import androidx.annotation.LayoutRes;
     36 import androidx.appcompat.test.R;
     37 import androidx.drawerlayout.widget.DrawerLayout;
     38 
     39 import org.hamcrest.Description;
     40 import org.hamcrest.Matcher;
     41 import org.hamcrest.TypeSafeMatcher;
     42 import org.junit.After;
     43 import org.junit.Rule;
     44 import org.junit.Test;
     45 import org.junit.runner.RunWith;
     46 
     47 /**
     48  * Test cases to verify that <code>DrawerLayout</code> only supports configurations
     49  * with at most one drawer child along each vertical (left / right) edge.
     50  */
     51 @SmallTest
     52 @RunWith(AndroidJUnit4.class)
     53 public class DrawerDynamicLayoutTest {
     54     @Rule
     55     public final ActivityTestRule<DrawerDynamicLayoutActivity> mActivityTestRule =
     56             new ActivityTestRule<>(DrawerDynamicLayoutActivity.class);
     57 
     58     @UiThreadTest
     59     @After
     60     public void tearDown() {
     61         // Now that the test is done, replace the activity content view with ViewStub so
     62         // that it's ready to be replaced for the next test.
     63         final DrawerDynamicLayoutActivity activity = mActivityTestRule.getActivity();
     64         activity.setContentView(R.layout.drawer_dynamic_layout);
     65     }
     66 
     67     /**
     68      * Matches views that have parents.
     69      */
     70     private Matcher<View> hasParent() {
     71         return new TypeSafeMatcher<View>() {
     72             @Override
     73             public void describeTo(Description description) {
     74                 description.appendText("has parent");
     75             }
     76 
     77             @Override
     78             public boolean matchesSafely(View view) {
     79                 return view.getParent() != null;
     80             }
     81         };
     82     }
     83 
     84     /**
     85      * Inflates the <code>ViewStub</code> with the passed layout resource.
     86      */
     87     private ViewAction inflateViewStub(final @LayoutRes int layoutResId) {
     88         return new ViewAction() {
     89             @Override
     90             public Matcher<View> getConstraints() {
     91                 return allOf(isAssignableFrom(ViewStub.class), hasParent());
     92             }
     93 
     94             @Override
     95             public String getDescription() {
     96                 return "Inflates view stub";
     97             }
     98 
     99             @Override
    100             public void perform(UiController uiController, View view) {
    101                 uiController.loopMainThreadUntilIdle();
    102 
    103                 ViewStub viewStub = (ViewStub) view;
    104                 viewStub.setLayoutResource(layoutResId);
    105                 viewStub.inflate();
    106 
    107                 uiController.loopMainThreadUntilIdle();
    108             }
    109         };
    110     }
    111 
    112     @Test
    113     public void testSingleStartDrawer() {
    114         onView(withId(R.id.drawer_layout)).check(doesNotExist());
    115         onView(withId(R.id.drawer_stub)).perform(
    116                 inflateViewStub(R.layout.drawer_dynamic_content_single_start));
    117     }
    118 
    119     @Test(expected=IllegalStateException.class)
    120     public void testDoubleStartDrawers() {
    121         onView(withId(R.id.drawer_layout)).check(doesNotExist());
    122         // Note the expected exception in the @Test annotation, as we expect the DrawerLayout
    123         // to throw exception during the measure pass as it detects two start drawers.
    124         onView(withId(R.id.drawer_stub)).perform(
    125                 inflateViewStub(R.layout.drawer_dynamic_content_double_start));
    126     }
    127 
    128     @Test
    129     public void testSingleEndDrawer() {
    130         onView(withId(R.id.drawer_layout)).check(doesNotExist());
    131         onView(withId(R.id.drawer_stub)).perform(
    132                 inflateViewStub(R.layout.drawer_dynamic_content_single_end));
    133     }
    134 
    135     @Test(expected=IllegalStateException.class)
    136     public void testDoubleEndDrawers() {
    137         onView(withId(R.id.drawer_layout)).check(doesNotExist());
    138         // Note the expected exception in the @Test annotation, as we expect the DrawerLayout
    139         // to throw exception during the measure pass as it detects two end drawers.
    140         onView(withId(R.id.drawer_stub)).perform(
    141                 inflateViewStub(R.layout.drawer_dynamic_content_double_end));
    142     }
    143 
    144     @Test
    145     public void testSingleStartDrawerSingleEndDrawer() {
    146         onView(withId(R.id.drawer_layout)).check(doesNotExist());
    147         onView(withId(R.id.drawer_stub)).perform(
    148                 inflateViewStub(R.layout.drawer_dynamic_content_start_end));
    149     }
    150 
    151     @Test(expected=IllegalStateException.class)
    152     public void testDoubleStartDrawersSingleEndDrawer() {
    153         onView(withId(R.id.drawer_layout)).check(doesNotExist());
    154         // Note the expected exception in the @Test annotation, as we expect the DrawerLayout
    155         // to throw exception during the measure pass as it detects two start drawers.
    156         onView(withId(R.id.drawer_stub)).perform(
    157                 inflateViewStub(R.layout.drawer_dynamic_content_double_start_single_end));
    158     }
    159 
    160     @Test(expected=IllegalStateException.class)
    161     public void testDoubleEndDrawersSingleStartDrawer() {
    162         onView(withId(R.id.drawer_layout)).check(doesNotExist());
    163         // Note the expected exception in the @Test annotation, as we expect the DrawerLayout
    164         // to throw exception during the measure pass as it detects two start drawers.
    165         onView(withId(R.id.drawer_stub)).perform(
    166                 inflateViewStub(R.layout.drawer_dynamic_content_double_end_single_start));
    167     }
    168 
    169     @Test
    170     public void testRemoveUnregisteredListener() {
    171         onView(withId(R.id.drawer_stub)).perform(
    172                 inflateViewStub(R.layout.drawer_dynamic_content_single_start));
    173 
    174         // We do this test here and not in DrawerLayoutTest since we want to be sure that the
    175         // call to DrawerLayout.removeDrawerLayout() didn't have any calls to addDrawerLayout()
    176         // before it. DrawerLayoutTest and its DrawerLayoutActivity register listeners as part
    177         // of their initial setup flow.
    178         final DrawerLayout startDrawer =
    179                 (DrawerLayout) mActivityTestRule.getActivity().findViewById(R.id.drawer_layout);
    180         DrawerLayout.DrawerListener mockedListener = mock(DrawerLayout.DrawerListener.class);
    181         startDrawer.removeDrawerListener(mockedListener);
    182     }
    183 }
    184