Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright 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 package androidx.percentlayout.widget;
     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 
     25 import android.support.test.annotation.UiThreadTest;
     26 import android.support.test.espresso.UiController;
     27 import android.support.test.espresso.ViewAction;
     28 import android.support.test.filters.SmallTest;
     29 import android.view.View;
     30 import android.view.ViewStub;
     31 
     32 import androidx.annotation.LayoutRes;
     33 import androidx.percentlayout.test.R;
     34 
     35 import org.hamcrest.Description;
     36 import org.hamcrest.Matcher;
     37 import org.hamcrest.TypeSafeMatcher;
     38 import org.junit.After;
     39 import org.junit.Test;
     40 
     41 /**
     42  * Test cases to verify that percent layouts properly account for their own paddings.
     43  */
     44 @SmallTest
     45 public class PercentDynamicLayoutTest
     46         extends BaseInstrumentationTestCase<PercentDynamicLayoutActivity> {
     47     public PercentDynamicLayoutTest() {
     48         super(PercentDynamicLayoutActivity.class);
     49     }
     50 
     51     @UiThreadTest
     52     @After
     53     public void tearDown() {
     54         // Now that the test is done, replace the activity content view with ViewStub so
     55         // that it's ready to be replaced for the next test.
     56         final PercentDynamicLayoutActivity activity = mActivityTestRule.getActivity();
     57         activity.setContentView(R.layout.percent_dynamic_layout);
     58     }
     59 
     60     /**
     61      * Matches views that have parents.
     62      */
     63     private Matcher<View> hasParent() {
     64         return new TypeSafeMatcher<View>() {
     65             @Override
     66             public void describeTo(Description description) {
     67                 description.appendText("has parent");
     68             }
     69 
     70             @Override
     71             public boolean matchesSafely(View view) {
     72                 return view.getParent() != null;
     73             }
     74         };
     75     }
     76 
     77     /**
     78      * Inflates the <code>ViewStub</code> with the passed layout resource.
     79      */
     80     private ViewAction inflateViewStub(final @LayoutRes int layoutResId) {
     81         return new ViewAction() {
     82             @Override
     83             public Matcher<View> getConstraints() {
     84                 return allOf(isAssignableFrom(ViewStub.class), hasParent());
     85             }
     86 
     87             @Override
     88             public String getDescription() {
     89                 return "Inflates view stub";
     90             }
     91 
     92             @Override
     93             public void perform(UiController uiController, View view) {
     94                 uiController.loopMainThreadUntilIdle();
     95 
     96                 ViewStub viewStub = (ViewStub) view;
     97                 viewStub.setLayoutResource(layoutResId);
     98                 viewStub.inflate();
     99 
    100                 uiController.loopMainThreadUntilIdle();
    101             }
    102         };
    103     }
    104 
    105     @Test
    106     public void testPercentFrameWithHorizontalPaddings() {
    107         onView(withId(R.id.percent_layout)).check(doesNotExist());
    108         onView(withId(R.id.percent_stub)).perform(
    109                 inflateViewStub(R.layout.percent_frame_layout_hpaddings));
    110 
    111         final PercentFrameLayout percentFrameLayout =
    112                 (PercentFrameLayout) mActivityTestRule.getActivity().findViewById(
    113                         R.id.percent_layout);
    114         final int containerWidth = percentFrameLayout.getWidth();
    115         final int containerHeight = percentFrameLayout.getHeight();
    116 
    117         final int availableWidth = containerWidth - percentFrameLayout.getPaddingLeft()
    118                 - percentFrameLayout.getPaddingRight();
    119         final int availableHeight = containerHeight - percentFrameLayout.getPaddingTop()
    120                 - percentFrameLayout.getPaddingBottom();
    121 
    122         final View child1 = percentFrameLayout.findViewById(R.id.child1);
    123         final View child2 = percentFrameLayout.findViewById(R.id.child2);
    124 
    125         assertFuzzyEquals("Child 1 width as 50% of the container's available width",
    126                 0.5f * availableWidth, child1.getWidth());
    127         assertFuzzyEquals("Child 1 height as 100% of the container's available height",
    128                 availableHeight, child1.getHeight());
    129         assertFuzzyEquals("Child 2 width as 50% of the container's available width",
    130                 0.5f * availableWidth, child2.getWidth());
    131         assertFuzzyEquals("Child 2 height as 100% of the container's available height",
    132                 availableHeight, child2.getHeight());
    133     }
    134 
    135     @Test
    136     public void testPercentFrameWithVerticalPaddings() {
    137         onView(withId(R.id.percent_layout)).check(doesNotExist());
    138         onView(withId(R.id.percent_stub)).perform(
    139                 inflateViewStub(R.layout.percent_frame_layout_vpaddings));
    140 
    141         final PercentFrameLayout percentFrameLayout =
    142                 (PercentFrameLayout) mActivityTestRule.getActivity().findViewById(
    143                         R.id.percent_layout);
    144         final int containerWidth = percentFrameLayout.getWidth();
    145         final int containerHeight = percentFrameLayout.getHeight();
    146 
    147         final int availableWidth = containerWidth - percentFrameLayout.getPaddingLeft()
    148                 - percentFrameLayout.getPaddingRight();
    149         final int availableHeight = containerHeight - percentFrameLayout.getPaddingTop()
    150                 - percentFrameLayout.getPaddingBottom();
    151 
    152         final View child1 = percentFrameLayout.findViewById(R.id.child1);
    153         final View child2 = percentFrameLayout.findViewById(R.id.child2);
    154 
    155         assertFuzzyEquals("Child 1 width as 100% of the container's available width",
    156                 availableWidth, child1.getWidth());
    157         assertFuzzyEquals("Child 1 height as 50% of the container's available height",
    158                 0.5f * availableHeight, child1.getHeight());
    159         assertFuzzyEquals("Child 2 width as 100% of the container's available width",
    160                 availableWidth, child2.getWidth());
    161         assertFuzzyEquals("Child 2 height as 50% of the container's available height",
    162                 0.5f* availableHeight, child2.getHeight());
    163     }
    164 
    165     @Test
    166     public void testPercentRelativeWithHorizontalPaddings() {
    167         onView(withId(R.id.percent_layout)).check(doesNotExist());
    168         onView(withId(R.id.percent_stub)).perform(
    169                 inflateViewStub(R.layout.percent_relative_layout_hpaddings));
    170 
    171         final PercentRelativeLayout percentRelativeLayout =
    172                 (PercentRelativeLayout) mActivityTestRule.getActivity().findViewById(
    173                         R.id.percent_layout);
    174         final int containerWidth = percentRelativeLayout.getWidth();
    175         final int containerHeight = percentRelativeLayout.getHeight();
    176 
    177         final int availableWidth = containerWidth - percentRelativeLayout.getPaddingLeft()
    178                 - percentRelativeLayout.getPaddingRight();
    179         final int availableHeight = containerHeight - percentRelativeLayout.getPaddingTop()
    180                 - percentRelativeLayout.getPaddingBottom();
    181 
    182         final View child1 = percentRelativeLayout.findViewById(R.id.child1);
    183         final View child2 = percentRelativeLayout.findViewById(R.id.child2);
    184 
    185         assertFuzzyEquals("Child 1 width as 50% of the container's available width",
    186                 0.5f * availableWidth, child1.getWidth());
    187         assertFuzzyEquals("Child 1 height as 100% of the container's available height",
    188                 availableHeight, child1.getHeight());
    189         assertFuzzyEquals("Child 2 width as 50% of the container's available width",
    190                 0.5f * availableWidth, child2.getWidth());
    191         assertFuzzyEquals("Child 2 height as 100% of the container's available height",
    192                 availableHeight, child2.getHeight());
    193     }
    194 
    195     @Test
    196     public void testPercentRelaticeWithVerticalPaddings() {
    197         onView(withId(R.id.percent_layout)).check(doesNotExist());
    198         onView(withId(R.id.percent_stub)).perform(
    199                 inflateViewStub(R.layout.percent_relative_layout_vpaddings));
    200 
    201         final PercentRelativeLayout percentRelativeLayout =
    202                 (PercentRelativeLayout) mActivityTestRule.getActivity().findViewById(
    203                         R.id.percent_layout);
    204         final int containerWidth = percentRelativeLayout.getWidth();
    205         final int containerHeight = percentRelativeLayout.getHeight();
    206 
    207         final int availableWidth = containerWidth - percentRelativeLayout.getPaddingLeft()
    208                 - percentRelativeLayout.getPaddingRight();
    209         final int availableHeight = containerHeight - percentRelativeLayout.getPaddingTop()
    210                 - percentRelativeLayout.getPaddingBottom();
    211 
    212         final View child1 = percentRelativeLayout.findViewById(R.id.child1);
    213         final View child2 = percentRelativeLayout.findViewById(R.id.child2);
    214 
    215         assertFuzzyEquals("Child 1 width as 100% of the container's available width",
    216                 availableWidth, child1.getWidth());
    217         assertFuzzyEquals("Child 1 height as 50% of the container's available height",
    218                 0.5f * availableHeight, child1.getHeight());
    219         assertFuzzyEquals("Child 2 width as 100% of the container's available width",
    220                 availableWidth, child2.getWidth());
    221         assertFuzzyEquals("Child 2 height as 50% of the container's available height",
    222                 0.5f* availableHeight, child2.getHeight());
    223     }
    224 }
    225