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.matcher.ViewMatchers.withId;
     20 
     21 import android.os.Build;
     22 import android.support.test.filters.SmallTest;
     23 import android.view.View;
     24 
     25 import androidx.core.view.ViewCompat;
     26 import androidx.percentlayout.test.R;
     27 
     28 import org.junit.Before;
     29 import org.junit.Test;
     30 
     31 @SmallTest
     32 public class PercentFrameTest extends BaseInstrumentationTestCase<TestFrameActivity> {
     33     private PercentFrameLayout mPercentFrameLayout;
     34     private int mContainerWidth;
     35     private int mContainerHeight;
     36 
     37     public PercentFrameTest() {
     38         super(TestFrameActivity.class);
     39     }
     40 
     41     @Before
     42     public void setUp() throws Exception {
     43         final TestFrameActivity activity = mActivityTestRule.getActivity();
     44         mPercentFrameLayout = (PercentFrameLayout) activity.findViewById(R.id.container);
     45         mContainerWidth = mPercentFrameLayout.getWidth();
     46         mContainerHeight = mPercentFrameLayout.getHeight();
     47     }
     48 
     49     @Test
     50     public void testWidthHeight() {
     51         View childToTest = mPercentFrameLayout.findViewById(R.id.child_width_height);
     52 
     53         int childWidth = childToTest.getWidth();
     54         int childHeight = childToTest.getHeight();
     55 
     56         assertFuzzyEquals("Child width as 50% of the container",
     57                 0.5f * mContainerWidth, childWidth);
     58         assertFuzzyEquals("Child height as 50% of the container",
     59                 0.5f * mContainerHeight, childHeight);
     60     }
     61 
     62     @Test
     63     public void testWidthAspectRatio() {
     64         View childToTest = mPercentFrameLayout.findViewById(R.id.child_width_ratio);
     65 
     66         int childWidth = childToTest.getWidth();
     67         int childHeight = childToTest.getHeight();
     68 
     69         assertFuzzyEquals("Child width as 60% of the container",
     70                 0.6f * mContainerWidth, childWidth);
     71         assertFuzzyEquals("Child aspect ratio of 120%",
     72                 childWidth / 1.2f, childHeight);
     73     }
     74 
     75     @Test
     76     public void testHeightAspectRatio() {
     77         View childToTest = mPercentFrameLayout.findViewById(R.id.child_height_ratio);
     78 
     79         int childWidth = childToTest.getWidth();
     80         int childHeight = childToTest.getHeight();
     81 
     82         assertFuzzyEquals("Child height as 50% of the container",
     83                 0.5f * mContainerHeight, childHeight);
     84         assertFuzzyEquals("Child aspect ratio of 150%",
     85                 1.5f * childHeight, childWidth);
     86     }
     87 
     88     @Test
     89     public void testMarginsSingle() {
     90         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margins_single);
     91 
     92         int childLeft = childToTest.getLeft();
     93         int childTop = childToTest.getTop();
     94         int childRight = childToTest.getRight();
     95         int childBottom = childToTest.getBottom();
     96 
     97         assertFuzzyEquals("Child left margin as 30% of the container",
     98                 0.3f * mContainerWidth, childLeft);
     99         assertFuzzyEquals("Child top margin as 30% of the container",
    100                 0.3f * mContainerHeight, childTop);
    101         assertFuzzyEquals("Child right margin as 30% of the container",
    102                 0.3f * mContainerWidth, mContainerWidth - childRight);
    103         assertFuzzyEquals("Child bottom margin as 30% of the container",
    104                 0.3f * mContainerHeight, mContainerHeight - childBottom);
    105     }
    106 
    107     @Test
    108     public void testMarginsMultiple() {
    109         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margins_multiple);
    110 
    111         int childLeft = childToTest.getLeft();
    112         int childTop = childToTest.getTop();
    113         int childRight = childToTest.getRight();
    114         int childBottom = childToTest.getBottom();
    115 
    116         assertFuzzyEquals("Child top margin as 10% of the container",
    117                 0.1f * mContainerHeight, childTop);
    118         assertFuzzyEquals("Child left margin as 15% of the container",
    119                 0.15f * mContainerWidth, childLeft);
    120         assertFuzzyEquals("Child bottom margin as 20% of the container",
    121                 0.2f * mContainerHeight, mContainerHeight - childBottom);
    122         assertFuzzyEquals("Child right margin as 25% of the container",
    123                 0.25f * mContainerWidth, mContainerWidth - childRight);
    124     }
    125 
    126     @Test
    127     public void testMarginsTopLeft() {
    128         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margins_top_left);
    129 
    130         int childWidth = childToTest.getWidth();
    131         int childHeight = childToTest.getHeight();
    132         int childLeft = childToTest.getLeft();
    133         int childTop = childToTest.getTop();
    134 
    135         assertFuzzyEquals("Child width as 50% of the container",
    136                 0.5f * mContainerWidth, childWidth);
    137         assertFuzzyEquals("Child height as 50% of the container",
    138                 0.5f * mContainerHeight, childHeight);
    139         assertFuzzyEquals("Child left margin as 20% of the container",
    140                 0.2f * mContainerWidth, childLeft);
    141         assertFuzzyEquals("Child top margin as 20% of the container",
    142                 0.2f * mContainerHeight, childTop);
    143     }
    144 
    145     @Test
    146     public void testMarginsBottomRight() {
    147         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margins_bottom_right);
    148 
    149         int childWidth = childToTest.getWidth();
    150         int childHeight = childToTest.getHeight();
    151         int childRight = childToTest.getRight();
    152         int childBottom = childToTest.getBottom();
    153 
    154         assertFuzzyEquals("Child width as 60% of the container",
    155                 0.6f * mContainerWidth, childWidth);
    156         assertFuzzyEquals("Child height as 60% of the container",
    157                 0.6f * mContainerHeight, childHeight);
    158         assertFuzzyEquals("Child right margin as 10% of the container",
    159                 0.1f * mContainerWidth, mContainerWidth - childRight);
    160         assertFuzzyEquals("Child bottom margin as 10% of the container",
    161                 0.1f * mContainerHeight, mContainerHeight - childBottom);
    162     }
    163 
    164     @Test
    165     public void testMarginStart() throws Throwable {
    166         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margin_start);
    167 
    168         // Under LTR test that start is treated as left
    169         int childLeft = childToTest.getLeft();
    170         assertFuzzyEquals("Child start margin as 20% of the container",
    171                 0.2f * mContainerWidth, childLeft);
    172     }
    173 
    174     @Test
    175     public void testMarginStartRtl() throws Throwable {
    176         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margin_start);
    177 
    178         if (Build.VERSION.SDK_INT >= 17) {
    179             // Force our child to inherit parent's layout direction
    180             onView(withId(R.id.child_margin_start)).perform(
    181                     LayoutDirectionActions.setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_INHERIT));
    182             // And force the container to RTL mode
    183             onView(withId(R.id.container)).perform(
    184                     LayoutDirectionActions.setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL));
    185 
    186             // Force a full measure + layout pass on the container
    187             mPercentFrameLayout.measure(
    188                     View.MeasureSpec.makeMeasureSpec(mContainerWidth, View.MeasureSpec.EXACTLY),
    189                     View.MeasureSpec.makeMeasureSpec(mContainerHeight, View.MeasureSpec.EXACTLY));
    190             mPercentFrameLayout.layout(mPercentFrameLayout.getLeft(),
    191                     mPercentFrameLayout.getTop(), mPercentFrameLayout.getRight(),
    192                     mPercentFrameLayout.getBottom());
    193 
    194             // Start under RTL should be treated as right
    195             int childRight = childToTest.getRight();
    196             assertFuzzyEquals("Child start margin as 20% of the container",
    197                     0.2f * mContainerWidth, mContainerWidth - childRight);
    198         } else {
    199             // On pre-v17 devices test that start is treated as left
    200             int childLeft = childToTest.getLeft();
    201             assertFuzzyEquals("Child start margin as 20% of the container",
    202                     0.2f * mContainerWidth, childLeft);
    203         }
    204     }
    205 
    206     @Test
    207     public void testMarginEnd() throws Throwable {
    208         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margin_end);
    209 
    210         // Under LTR test that end is treated as right
    211         int childRight = childToTest.getRight();
    212         assertFuzzyEquals("Child end margin as 30% of the container",
    213                 0.3f * mContainerWidth, mContainerWidth - childRight);
    214     }
    215 
    216     @Test
    217     public void testMarginEndRtl() throws Throwable {
    218         View childToTest = mPercentFrameLayout.findViewById(R.id.child_margin_end);
    219 
    220         if (Build.VERSION.SDK_INT >= 17) {
    221             // Force our child to inherit parent's layout direction
    222             onView(withId(R.id.child_margin_end)).perform(
    223                     LayoutDirectionActions.setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_INHERIT));
    224             // And force the container to RTL mode
    225             onView(withId(R.id.container)).perform(
    226                     LayoutDirectionActions.setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL));
    227 
    228             // Force a full measure + layout pass on the container
    229             mPercentFrameLayout.measure(
    230                     View.MeasureSpec.makeMeasureSpec(mContainerWidth, View.MeasureSpec.EXACTLY),
    231                     View.MeasureSpec.makeMeasureSpec(mContainerHeight, View.MeasureSpec.EXACTLY));
    232             mPercentFrameLayout.layout(mPercentFrameLayout.getLeft(),
    233                     mPercentFrameLayout.getTop(), mPercentFrameLayout.getRight(),
    234                     mPercentFrameLayout.getBottom());
    235 
    236             // End under RTL should be treated as left
    237             int childLeft = childToTest.getLeft();
    238             assertFuzzyEquals("Child end margin as 30% of the container",
    239                     0.3f * mContainerWidth, childLeft);
    240         } else {
    241             // On pre-v17 devices test that end is treated as right
    242             int childRight = childToTest.getRight();
    243             assertFuzzyEquals("Child end margin as 30% of the container",
    244                     0.3f * mContainerWidth, mContainerWidth - childRight);
    245         }
    246     }
    247 }
    248