1ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov/*
2ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * Copyright 2018 The Android Open Source Project
3ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
4ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * you may not use this file except in compliance with the License.
6ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * You may obtain a copy of the License at
7ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
8ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
10ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * See the License for the specific language governing permissions and
14ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * limitations under the License.
15ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov */
16c1f419c492ca568eebeb1d0feebe442560da57dbAurimas Liutikaspackage androidx.percentlayout.widget;
17ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
18754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static android.support.test.espresso.Espresso.onView;
19754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static android.support.test.espresso.matcher.ViewMatchers.withId;
20754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikas
21ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikovimport android.os.Build;
22754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport android.support.test.filters.SmallTest;
23ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikovimport android.view.View;
24754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikas
251cabbc50ec35fd735a8171c9a8f46a1297fe805bAurimas Liutikasimport androidx.core.view.ViewCompat;
261cabbc50ec35fd735a8171c9a8f46a1297fe805bAurimas Liutikasimport androidx.percentlayout.test.R;
271cabbc50ec35fd735a8171c9a8f46a1297fe805bAurimas Liutikas
28ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikovimport org.junit.Before;
29ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikovimport org.junit.Test;
30ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
31ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov/**
32ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * The arrangement of child views in the layout class in the default LTR (left-to-right) direction
33ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * is as follows:
34ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
35ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  +---------------------------------------------+
36ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
37ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |    TTTTTTTTTTTTTTTTTTTTT                    |
38ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
39ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | S                                           |
40ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | S           CCCCCCCCCCCCCCCCCC              |
41ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | S           CCCCCCCCCCCCCCCCCC              |
42ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | S           CCCCCCCCCCCCCCCCCC           E  |
43ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | S           CCCCCCCCCCCCCCCCCC           E  |
44ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | S           CCCCCCCCCCCCCCCCCC           E  |
45ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |             CCCCCCCCCCCCCCCCCC           E  |
46ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |             CCCCCCCCCCCCCCCCCC           E  |
47ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                          E  |
48ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
49ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                    BBBBBBBBBBBBBBBBBBBBB    |
50ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
51ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  +---------------------------------------------+
52ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
53ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * The arrangement of child views in the layout class in the RTL (right-to-left) direction
54ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * is as follows:
55ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
56ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  +---------------------------------------------+
57ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
58ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                    TTTTTTTTTTTTTTTTTTTTT    |
59ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
60ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                          S  |
61ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |             CCCCCCCCCCCCCCCCCC           S  |
62ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |             CCCCCCCCCCCCCCCCCC           S  |
63ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | E           CCCCCCCCCCCCCCCCCC           S  |
64ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | E           CCCCCCCCCCCCCCCCCC           S  |
65ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | E           CCCCCCCCCCCCCCCCCC           S  |
66ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | E           CCCCCCCCCCCCCCCCCC              |
67ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | E           CCCCCCCCCCCCCCCCCC              |
68ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  | E                                           |
69ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
70ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |    BBBBBBBBBBBBBBBBBBBBB                    |
71ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  |                                             |
72ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *  +---------------------------------------------+
73ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
74ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * Child views are exercising the following percent-based constraints supported by
75ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * <code>PercentRelativeLayout</code>:
76ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
77ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * <ul>
78ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *     <li>Top child (marked with T) - width, aspect ratio, top margin, start margin.</li>
79ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *     <li>Start child (marked with S) - height, aspect ratio, top margin, start margin.</li>
80ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *     <li>Bottom child (marked with B) - width, aspect ratio, bottom margin, end margin.</li>
81ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *     <li>Right child (marked with E) - height, aspect ratio, bottom margin, end margin.</li>
82ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *     <li>Center child (marked with C) - margin (all sides) from the other four children.</li>
83ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * </ul>
84ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov *
85ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * Under LTR direction (pre-v17 devices and v17+ with default direction of en-US locale) we are
86ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * testing the same assertions as <code>PercentRelativeTest</code>. Under RTL direction (on v17+
87ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * devices with Espresso-powered direction switch) we are testing the reverse assertions along the
88ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov * X axis for all child views.
893a4a06955e3d038f262600f760fc4984e4cf2cb5Kirill Grouchnikov *
903a4a06955e3d038f262600f760fc4984e4cf2cb5Kirill Grouchnikov * Note that due to a bug in the core {@link RelativeLayout} (base class of
913a4a06955e3d038f262600f760fc4984e4cf2cb5Kirill Grouchnikov * {@link PercentRelativeLayout}) in how it treats end margin of child views on v17 devices, we are
923a4a06955e3d038f262600f760fc4984e4cf2cb5Kirill Grouchnikov * skipping all tests in this class for v17 devices. This is in line with the overall contract
933a4a06955e3d038f262600f760fc4984e4cf2cb5Kirill Grouchnikov * of percent-based layouts provided by the support library - we do not work around / fix bugs in
943a4a06955e3d038f262600f760fc4984e4cf2cb5Kirill Grouchnikov * the core classes, but rather just provide a translation layer between percentage-based values
953a4a06955e3d038f262600f760fc4984e4cf2cb5Kirill Grouchnikov * and pixel-based ones.
96ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov */
97ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov@SmallTest
98ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikovpublic class PercentRelativeRtlTest extends BaseInstrumentationTestCase<TestRelativeRtlActivity> {
99ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    private PercentRelativeLayout mPercentRelativeLayout;
100ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    private int mContainerWidth;
101ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    private int mContainerHeight;
102ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
103ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    public PercentRelativeRtlTest() {
104ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        super(TestRelativeRtlActivity.class);
105ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
106ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
107ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    @Before
108ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    public void setUp() throws Exception {
1091e40dc0279376a1efa14af632894efa477c3334cKirill Grouchnikov        final TestRelativeRtlActivity activity = mActivityTestRule.getActivity();
110ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        mPercentRelativeLayout = (PercentRelativeLayout) activity.findViewById(R.id.container);
111ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        mContainerWidth = mPercentRelativeLayout.getWidth();
112ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        mContainerHeight = mPercentRelativeLayout.getHeight();
113ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
114ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
115ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    private void switchToRtl() {
116ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        // Force the container to RTL mode
117ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        onView(withId(R.id.container)).perform(
118ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                LayoutDirectionActions.setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL));
119ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
120ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        // Force a full measure + layout pass on the container
121ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        mPercentRelativeLayout.measure(
122ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                View.MeasureSpec.makeMeasureSpec(mContainerWidth, View.MeasureSpec.EXACTLY),
123ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                View.MeasureSpec.makeMeasureSpec(mContainerHeight, View.MeasureSpec.EXACTLY));
124ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        mPercentRelativeLayout.layout(mPercentRelativeLayout.getLeft(),
125ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                mPercentRelativeLayout.getTop(), mPercentRelativeLayout.getRight(),
126ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                mPercentRelativeLayout.getBottom());
127ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
128ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
129ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    @Test
130ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    public void testTopChild() {
131d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        if (Build.VERSION.SDK_INT == 17) {
132d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas            return;
133d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        }
134ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View childToTest = mPercentRelativeLayout.findViewById(R.id.child_top);
135ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
136ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        if (Build.VERSION.SDK_INT >= 17) {
137ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            switchToRtl();
138ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
139ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childRight = childToTest.getRight();
140ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child start margin as 20% of the container",
141ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.2f * mContainerWidth, mContainerWidth - childRight);
142ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        } else {
143ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childLeft = childToTest.getLeft();
144ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child start margin as 20% of the container",
145ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.2f * mContainerWidth, childLeft);
146ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        }
147ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
148ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childTop = childToTest.getTop();
149ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child top margin as 5% of the container",
150ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.05f * mContainerHeight, childTop);
151ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
152ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childWidth = childToTest.getWidth();
153ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childHeight = childToTest.getHeight();
154ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
155ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child width as 50% of the container",
156ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.5f * mContainerWidth, childWidth);
157ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child aspect ratio of 2000%",
158ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.05f * childWidth, childHeight);
159ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
160ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
161ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    @Test
162ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    public void testStartChild() {
163d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        if (Build.VERSION.SDK_INT == 17) {
164d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas            return;
165d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        }
166ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View childToTest = mPercentRelativeLayout.findViewById(R.id.child_start);
167ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
168ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        if (Build.VERSION.SDK_INT >= 17) {
169ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            switchToRtl();
170ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
171ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childRight = childToTest.getRight();
172ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child start margin as 5% of the container",
173ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.05f * mContainerWidth, mContainerWidth - childRight);
174ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        } else {
175ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childLeft = childToTest.getLeft();
176ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child start margin as 5% of the container",
177ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.05f * mContainerWidth, childLeft);
178ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        }
179ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
180ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childWidth = childToTest.getWidth();
181ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childHeight = childToTest.getHeight();
182ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
183ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child height as 50% of the container",
184ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.5f * mContainerHeight, childHeight);
185ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child aspect ratio of 5%",
186ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.05f * childHeight, childWidth);
187ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
188ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childTop = childToTest.getTop();
189ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
190ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child top margin as 20% of the container",
191ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.2f * mContainerHeight, childTop);
192ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
193ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
194ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    @Test
195ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    public void testBottomChild() {
196d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        if (Build.VERSION.SDK_INT == 17) {
197d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas            return;
198d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        }
199ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View childToTest = mPercentRelativeLayout.findViewById(R.id.child_bottom);
200ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
201ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        if (Build.VERSION.SDK_INT >= 17) {
202ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            switchToRtl();
203ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
204ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childLeft = childToTest.getLeft();
205ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child end margin as 20% of the container",
206ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.2f * mContainerWidth, childLeft);
207ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        } else {
208ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childRight = childToTest.getRight();
209ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child end margin as 20% of the container",
210ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.2f * mContainerWidth, mContainerWidth - childRight);
211ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        }
212ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
213ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
214ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childWidth = childToTest.getWidth();
215ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childHeight = childToTest.getHeight();
216ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
217ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child width as 40% of the container",
218ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.4f * mContainerWidth, childWidth);
219ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child aspect ratio of 2000%",
220ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.05f * childWidth, childHeight);
221ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
222ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childBottom = childToTest.getBottom();
223ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
224ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child bottom margin as 5% of the container",
225ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.05f * mContainerHeight, mContainerHeight - childBottom);
226ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
227ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
228ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    @Test
229ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    public void testEndChild() {
230d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        if (Build.VERSION.SDK_INT == 17) {
231d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas            return;
232d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        }
233ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View childToTest = mPercentRelativeLayout.findViewById(R.id.child_end);
234ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
235ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        if (Build.VERSION.SDK_INT >= 17) {
236ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            switchToRtl();
237ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
238ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childLeft = childToTest.getLeft();
239ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child end margin as 5% of the container",
240ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.05f * mContainerWidth, childLeft);
241ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        } else {
242ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            final int childRight = childToTest.getRight();
243ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            assertFuzzyEquals("Child end margin as 5% of the container",
244ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                    0.05f * mContainerWidth, mContainerWidth - childRight);
245ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        }
246ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
247ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childWidth = childToTest.getWidth();
248ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childHeight = childToTest.getHeight();
249ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
250ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child height as 50% of the container",
251ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.4f * mContainerHeight, childHeight);
252ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child aspect ratio of 5%",
253ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.05f * childHeight, childWidth);
254ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
255ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childBottom = childToTest.getBottom();
256ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
257ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child bottom margin as 20% of the container",
258ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                0.2f * mContainerHeight, mContainerHeight - childBottom);
259ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
260ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
261ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    @Test
262ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    public void testCenterChild() {
263d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        if (Build.VERSION.SDK_INT == 17) {
264d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas            return;
265d7a9bb0a020de896f1876ccd06c32f972fef151aAurimas Liutikas        }
266ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View childToTest = mPercentRelativeLayout.findViewById(R.id.child_center);
267ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
268ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        boolean supportsRtl = Build.VERSION.SDK_INT >= 17;
269ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        if (supportsRtl) {
270ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov            switchToRtl();
271ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        }
272ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
273ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childLeft = childToTest.getLeft();
274ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childTop = childToTest.getTop();
275ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childRight = childToTest.getRight();
276ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final int childBottom = childToTest.getBottom();
277ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
278ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View leftChild = supportsRtl
279ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                ? mPercentRelativeLayout.findViewById(R.id.child_end)
280ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                : mPercentRelativeLayout.findViewById(R.id.child_start);
281ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child left margin as 10% of the container",
282ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                leftChild.getRight() + 0.1f * mContainerWidth, childLeft);
283ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
284ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View topChild = mPercentRelativeLayout.findViewById(R.id.child_top);
285ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child top margin as 10% of the container",
286ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                topChild.getBottom() + 0.1f * mContainerHeight, childTop);
287ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
288ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View rightChild = supportsRtl
289ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                ? mPercentRelativeLayout.findViewById(R.id.child_start)
290ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                : mPercentRelativeLayout.findViewById(R.id.child_end);
291ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child right margin as 10% of the container",
292ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                rightChild.getLeft() - 0.1f * mContainerWidth, childRight);
293ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov
294ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        final View bottomChild = mPercentRelativeLayout.findViewById(R.id.child_bottom);
295ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov        assertFuzzyEquals("Child bottom margin as 10% of the container",
296ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov                bottomChild.getTop() - 0.1f * mContainerHeight, childBottom);
297ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov    }
298ab89bf3ae379244f3d86e788d66b7a42d62740feKirill Grouchnikov}
299