From 14f2a42d4cc550ffdb68c5eceb8e39a572b2d6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Thu, 10 Aug 2017 12:11:18 +0200 Subject: [PATCH] #327: Kotlinize the test code (#346) * Convert tests in `demo-playground` * Convert tests in `flexbox` --- .../apps/flexbox/test/MainActivityTest.java | 406 -- .../apps/flexbox/test/MainActivityTest.kt | 305 ++ flexbox/build.gradle | 2 + .../android/flexbox/FakeFlexContainer.java | 237 - .../android/flexbox/FakeFlexContainer.kt | 143 + .../android/flexbox/FlexboxHelperTest.java | 437 -- .../android/flexbox/FlexboxHelperTest.kt | 430 ++ ...eActivity.java => ConfigChangeActivity.kt} | 26 +- .../flexbox/test/FlexboxAndroidTest.java | 4051 ----------------- .../flexbox/test/FlexboxAndroidTest.kt | 3841 ++++++++++++++++ .../FlexboxLayoutManagerConfigChangeTest.java | 202 - .../FlexboxLayoutManagerConfigChangeTest.kt | 168 + .../test/FlexboxLayoutManagerTest.java | 3618 --------------- .../flexbox/test/FlexboxLayoutManagerTest.kt | 3186 +++++++++++++ ...stActivity.java => FlexboxTestActivity.kt} | 13 +- .../flexbox/test/IsEqualAllowingError.java | 72 - .../flexbox/test/IsEqualAllowingError.kt | 51 + .../flexbox/test/NestedInnerAdapter.java | 68 - .../flexbox/test/NestedInnerAdapter.kt | 46 + .../flexbox/test/NestedOuterAdapter.java | 95 - .../flexbox/test/NestedOuterAdapter.kt | 63 + .../android/flexbox/test/TestAdapter.java | 94 - .../android/flexbox/test/TestAdapter.kt | 71 + .../test/TestAdapterMultiViewTypes.java | 95 - .../flexbox/test/TestAdapterMultiViewTypes.kt | 83 + .../test/{TestUtil.java => TestUtil.kt} | 17 +- ...{TestViewHolder.java => TestViewHolder.kt} | 19 +- 27 files changed, 8418 insertions(+), 9421 deletions(-) delete mode 100644 demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.java create mode 100644 demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.kt rename flexbox/src/androidTest/java/com/google/android/flexbox/test/{ConfigChangeActivity.java => ConfigChangeActivity.kt} (54%) delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.kt rename flexbox/src/androidTest/java/com/google/android/flexbox/test/{FlexboxTestActivity.java => FlexboxTestActivity.kt} (73%) delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.kt delete mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.java create mode 100644 flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.kt rename flexbox/src/androidTest/java/com/google/android/flexbox/test/{TestUtil.java => TestUtil.kt} (62%) rename flexbox/src/androidTest/java/com/google/android/flexbox/test/{TestViewHolder.java => TestViewHolder.kt} (66%) diff --git a/demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.java b/demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.java deleted file mode 100644 index 267f02c..0000000 --- a/demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.java +++ /dev/null @@ -1,406 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.apps.flexbox.test; - -import com.google.android.flexbox.MainActivity; -import com.google.android.apps.flexbox.R; -import com.google.android.flexbox.AlignContent; -import com.google.android.flexbox.AlignItems; -import com.google.android.flexbox.FlexDirection; -import com.google.android.flexbox.FlexWrap; -import com.google.android.flexbox.FlexboxLayout; -import com.google.android.flexbox.JustifyContent; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import android.content.pm.ActivityInfo; -import android.support.design.widget.NavigationView; -import android.support.test.InstrumentationRegistry; -import android.support.test.filters.FlakyTest; -import android.support.test.filters.MediumTest; -import android.support.test.rule.ActivityTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.support.v4.view.MenuItemCompat; -import android.view.Menu; -import android.widget.ArrayAdapter; -import android.widget.RadioGroup; -import android.widget.Spinner; -import android.widget.TextView; - -import static android.support.test.espresso.Espresso.onView; -import static android.support.test.espresso.action.ViewActions.click; -import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard; -import static android.support.test.espresso.action.ViewActions.replaceText; -import static android.support.test.espresso.matcher.ViewMatchers.withId; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; - - -/** - * Integration tests for {@link MainActivity}. - */ -@RunWith(AndroidJUnit4.class) -@MediumTest -public class MainActivityTest { - - - @Rule - public ActivityTestRule mActivityRule = - new ActivityTestRule<>(MainActivity.class); - - @Test - @FlakyTest - public void testAddFlexItem() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - int beforeCount = flexboxLayout.getChildCount(); - onView(withId(R.id.add_fab)).perform(click()); - - assertThat(flexboxLayout.getChildCount(), is(beforeCount + 1)); - } - - @Test - @FlakyTest - public void testRemoveFlexItem() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - int beforeCount = flexboxLayout.getChildCount(); - onView(withId(R.id.remove_fab)).perform(click()); - - assertThat(flexboxLayout.getChildCount(), is(beforeCount - 1)); - } - - @Test - @FlakyTest - public void testConfigurationChange() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - onView(withId(R.id.add_fab)).perform(click()); - onView(withId(R.id.add_fab)).perform(click()); - onView(withId(R.id.add_fab)).perform(click()); - int beforeCount = flexboxLayout.getChildCount(); - - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // Verify the flex items are restored across the configuration change. - assertThat(flexboxLayout.getChildCount(), is(beforeCount)); - } - - @Test - @SuppressWarnings("unchecked") - @FlakyTest - public void testFlexDirectionSpinner() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - NavigationView navigationView = (NavigationView) activity.findViewById(R.id.nav_view); - assertNotNull(navigationView); - Menu menu = navigationView.getMenu(); - final Spinner spinner = (Spinner) MenuItemCompat - .getActionView(menu.findItem(R.id.menu_item_flex_direction)); - ArrayAdapter spinnerAdapter = (ArrayAdapter) - spinner.getAdapter(); - - final int columnPosition = spinnerAdapter.getPosition(activity.getString(R.string.column)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(columnPosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - final int rowReversePosition = spinnerAdapter - .getPosition(activity.getString(R.string.row_reverse)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(rowReversePosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - } - - @Test - @SuppressWarnings("unchecked") - @FlakyTest - public void testFlexWrapSpinner() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - NavigationView navigationView = (NavigationView) activity.findViewById(R.id.nav_view); - assertNotNull(navigationView); - Menu menu = navigationView.getMenu(); - final Spinner spinner = (Spinner) MenuItemCompat - .getActionView(menu.findItem(R.id.menu_item_flex_wrap)); - ArrayAdapter spinnerAdapter = (ArrayAdapter) - spinner.getAdapter(); - - final int wrapReversePosition = spinnerAdapter - .getPosition(activity.getString(R.string.wrap_reverse)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(wrapReversePosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP_REVERSE)); - - final int noWrapPosition = spinnerAdapter - .getPosition(activity.getString(R.string.nowrap)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(noWrapPosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.NOWRAP)); - } - - @Test - @SuppressWarnings("unchecked") - @FlakyTest - public void testJustifyContentSpinner() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - NavigationView navigationView = (NavigationView) activity.findViewById(R.id.nav_view); - assertNotNull(navigationView); - Menu menu = navigationView.getMenu(); - final Spinner spinner = (Spinner) MenuItemCompat - .getActionView(menu.findItem(R.id.menu_item_justify_content)); - ArrayAdapter spinnerAdapter = (ArrayAdapter) - spinner.getAdapter(); - - final int spaceBetweenPosition = spinnerAdapter - .getPosition(activity.getString(R.string.space_between)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(spaceBetweenPosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_BETWEEN)); - - final int centerPosition = spinnerAdapter - .getPosition(activity.getString(R.string.center)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(centerPosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.CENTER)); - } - - @Test - @SuppressWarnings("unchecked") - @FlakyTest - public void testAlignItemsSpinner() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - NavigationView navigationView = (NavigationView) activity.findViewById(R.id.nav_view); - assertNotNull(navigationView); - Menu menu = navigationView.getMenu(); - final Spinner spinner = (Spinner) MenuItemCompat - .getActionView(menu.findItem(R.id.menu_item_align_items)); - ArrayAdapter spinnerAdapter = (ArrayAdapter) - spinner.getAdapter(); - - final int baselinePosition = spinnerAdapter - .getPosition(activity.getString(R.string.baseline)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(baselinePosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getAlignItems(), - is(AlignItems.BASELINE)); - - final int flexEndPosition = spinnerAdapter - .getPosition(activity.getString(R.string.flex_end)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(flexEndPosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_END)); - } - - @Test - @SuppressWarnings("unchecked") - @FlakyTest - public void testAlignContentSpinner() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - NavigationView navigationView = (NavigationView) activity.findViewById(R.id.nav_view); - assertNotNull(navigationView); - Menu menu = navigationView.getMenu(); - final Spinner spinner = (Spinner) MenuItemCompat - .getActionView(menu.findItem(R.id.menu_item_align_content)); - ArrayAdapter spinnerAdapter = (ArrayAdapter) - spinner.getAdapter(); - - final int spaceAroundPosition = spinnerAdapter - .getPosition(activity.getString(R.string.space_around)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(spaceAroundPosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getAlignContent(), - is(AlignContent.SPACE_AROUND)); - - final int stretchPosition = spinnerAdapter - .getPosition(activity.getString(R.string.stretch)); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - spinner.setSelection(stretchPosition); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.STRETCH)); - } - - @Test - @FlakyTest - public void testEditFragment_changeOrder() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - onView(withId(R.id.textview1)).perform(click()); - onView(withId(R.id.edit_text_order)).perform(replaceText("3"), closeSoftKeyboard()); - onView(withId(R.id.button_ok)).perform(click()); - TextView first = (TextView) flexboxLayout.getReorderedChildAt(0); - TextView second = (TextView) flexboxLayout.getReorderedChildAt(1); - TextView third = (TextView) flexboxLayout.getReorderedChildAt(2); - - assertThat(first.getText().toString(), is("2")); - assertThat(second.getText().toString(), is("3")); - assertThat(third.getText().toString(), is("1")); - } - - @Test - @FlakyTest - public void testEditFragment_changeFlexGrow() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - onView(withId(R.id.textview1)).perform(click()); - onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1"), closeSoftKeyboard()); - onView(withId(R.id.button_ok)).perform(click()); - TextView first = (TextView) activity.findViewById(R.id.textview1); - TextView second = (TextView) activity.findViewById(R.id.textview2); - TextView third = (TextView) activity.findViewById(R.id.textview3); - assertNotNull(first); - assertNotNull(second); - assertNotNull(third); - - assertThat(first.getWidth(), - is(flexboxLayout.getWidth() - second.getWidth() - third.getWidth())); - } - - @Test - @FlakyTest - public void testEditFragment_changeFlexGrowFloat() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - onView(withId(R.id.textview1)).perform(click()); - onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1.0"), closeSoftKeyboard()); - onView(withId(R.id.button_ok)).perform(click()); - TextView first = (TextView) activity.findViewById(R.id.textview1); - TextView second = (TextView) activity.findViewById(R.id.textview2); - TextView third = (TextView) activity.findViewById(R.id.textview3); - assertNotNull(first); - assertNotNull(second); - assertNotNull(third); - - assertThat(first.getWidth(), - is(flexboxLayout.getWidth() - second.getWidth() - third.getWidth())); - } - - @Test - @FlakyTest - public void testEditFragment_changeFlexBasisPercent() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - onView(withId(R.id.textview1)).perform(click()); - onView(withId(R.id.edit_text_flex_basis_percent)) - .perform(replaceText("50"), closeSoftKeyboard()); - onView(withId(R.id.button_ok)).perform(click()); - TextView first = (TextView) activity.findViewById(R.id.textview1); - TextView second = (TextView) activity.findViewById(R.id.textview2); - TextView third = (TextView) activity.findViewById(R.id.textview3); - assertNotNull(first); - assertNotNull(second); - assertNotNull(third); - - assertTrue(first.getWidth() - 1 <= flexboxLayout.getWidth() / 2 - || flexboxLayout.getWidth() / 2 <= first.getWidth() + 1); - } - - @Test - @FlakyTest - public void testSwitchRecyclerViewFragment() { - MainActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - assertNotNull(flexboxLayout); - NavigationView navigationView = (NavigationView) activity.findViewById(R.id.nav_view); - assertNotNull(navigationView); - assertNull(activity.findViewById(R.id.recyclerview)); - assertNotNull(activity.findViewById(R.id.flexbox_layout)); - - final RadioGroup radioGroup = (RadioGroup) navigationView.getHeaderView(0) - .findViewById(R.id.radiogroup_container_implementation); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - radioGroup.check(R.id.radiobutton_recyclerview); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertNotNull(activity.findViewById(R.id.recyclerview)); - assertNull(activity.findViewById(R.id.flexbox_layout)); - } -} diff --git a/demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.kt b/demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.kt new file mode 100644 index 0000000..81ffc55 --- /dev/null +++ b/demo-playground/src/androidTest/java/com/google/android/apps/flexbox/test/MainActivityTest.kt @@ -0,0 +1,305 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.apps.flexbox.test + +import android.content.pm.ActivityInfo +import android.support.design.widget.NavigationView +import android.support.test.InstrumentationRegistry +import android.support.test.espresso.Espresso.onView +import android.support.test.espresso.action.ViewActions.* +import android.support.test.espresso.matcher.ViewMatchers.withId +import android.support.test.filters.FlakyTest +import android.support.test.filters.MediumTest +import android.support.test.rule.ActivityTestRule +import android.support.test.runner.AndroidJUnit4 +import android.view.View +import android.widget.ArrayAdapter +import android.widget.RadioGroup +import android.widget.Spinner +import android.widget.TextView +import com.google.android.apps.flexbox.R +import com.google.android.flexbox.* +import junit.framework.Assert.* +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.core.Is.`is` +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Integration tests for [MainActivity]. + */ +@RunWith(AndroidJUnit4::class) +@MediumTest +class MainActivityTest { + + @JvmField + @Rule + var activityRule = ActivityTestRule(MainActivity::class.java) + + @Test + @FlakyTest + fun testAddFlexItem() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + val beforeCount = flexboxLayout.childCount + onView(withId(R.id.add_fab)).perform(click()) + + assertThat(flexboxLayout.childCount, `is`(beforeCount + 1)) + } + + @Test + @FlakyTest + fun testRemoveFlexItem() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + val beforeCount = flexboxLayout.childCount + onView(withId(R.id.remove_fab)).perform(click()) + + assertThat(flexboxLayout.childCount, `is`(beforeCount - 1)) + } + + @Test + @FlakyTest + fun testConfigurationChange() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + onView(withId(R.id.add_fab)).perform(click()) + onView(withId(R.id.add_fab)).perform(click()) + onView(withId(R.id.add_fab)).perform(click()) + val beforeCount = flexboxLayout.childCount + + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // Verify the flex items are restored across the configuration change. + assertThat(flexboxLayout.childCount, `is`(beforeCount)) + } + + @Test + @FlakyTest + fun testFlexDirectionSpinner() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + val navigationView = activity.findViewById(R.id.nav_view) + assertNotNull(navigationView) + val menu = navigationView.menu + val spinner = menu.findItem(R.id.menu_item_flex_direction).actionView as Spinner + val spinnerAdapter = spinner.adapter as ArrayAdapter + + val columnPosition = spinnerAdapter.getPosition(activity.getString(R.string.column)) + activity.runOnUiThread { spinner.setSelection(columnPosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + val rowReversePosition = spinnerAdapter.getPosition(activity.getString(R.string.row_reverse)) + activity.runOnUiThread { spinner.setSelection(rowReversePosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + } + + @Test + @FlakyTest + fun testFlexWrapSpinner() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + val navigationView = activity.findViewById(R.id.nav_view) + assertNotNull(navigationView) + val menu = navigationView.menu + val spinner = menu.findItem(R.id.menu_item_flex_wrap).actionView as Spinner + val spinnerAdapter = spinner.adapter as ArrayAdapter + + val wrapReversePosition = spinnerAdapter.getPosition(activity.getString(R.string.wrap_reverse)) + activity.runOnUiThread { spinner.setSelection(wrapReversePosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) + + val noWrapPosition = spinnerAdapter.getPosition(activity.getString(R.string.nowrap)) + activity.runOnUiThread { spinner.setSelection(noWrapPosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) + } + + @Test + @FlakyTest + fun testJustifyContentSpinner() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout + assertNotNull(flexboxLayout) + val navigationView = activity.findViewById(R.id.nav_view) as NavigationView + assertNotNull(navigationView) + val menu = navigationView.menu + val spinner = menu.findItem(R.id.menu_item_justify_content).actionView as Spinner + val spinnerAdapter = spinner.adapter as ArrayAdapter + + val spaceBetweenPosition = spinnerAdapter.getPosition(activity.getString(R.string.space_between)) + activity.runOnUiThread { spinner.setSelection(spaceBetweenPosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + + val centerPosition = spinnerAdapter.getPosition(activity.getString(R.string.center)) + activity.runOnUiThread { spinner.setSelection(centerPosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.CENTER)) + } + + @Test + @FlakyTest + fun testAlignItemsSpinner() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + val navigationView = activity.findViewById(R.id.nav_view) + assertNotNull(navigationView) + val menu = navigationView.menu + val spinner = menu.findItem(R.id.menu_item_align_items).actionView as Spinner + val spinnerAdapter = spinner.adapter as ArrayAdapter + + val baselinePosition = spinnerAdapter.getPosition(activity.getString(R.string.baseline)) + activity.runOnUiThread { spinner.setSelection(baselinePosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.alignItems, `is`(AlignItems.BASELINE)) + + val flexEndPosition = spinnerAdapter.getPosition(activity.getString(R.string.flex_end)) + activity.runOnUiThread { spinner.setSelection(flexEndPosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) + } + + @Test + @FlakyTest + fun testAlignContentSpinner() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + val navigationView = activity.findViewById(R.id.nav_view) + assertNotNull(navigationView) + val menu = navigationView.menu + val spinner = menu.findItem(R.id.menu_item_align_content).actionView as Spinner + val spinnerAdapter = spinner.adapter as ArrayAdapter + + val spaceAroundPosition = spinnerAdapter.getPosition(activity.getString(R.string.space_around)) + activity.runOnUiThread { spinner.setSelection(spaceAroundPosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_AROUND)) + + val stretchPosition = spinnerAdapter.getPosition(activity.getString(R.string.stretch)) + activity.runOnUiThread { spinner.setSelection(stretchPosition) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) + } + + @Test + @FlakyTest + fun testEditFragment_changeOrder() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout + assertNotNull(flexboxLayout) + onView(withId(R.id.textview1)).perform(click()) + onView(withId(R.id.edit_text_order)).perform(replaceText("3"), closeSoftKeyboard()) + onView(withId(R.id.button_ok)).perform(click()) + val first = flexboxLayout.getReorderedChildAt(0) as TextView + val second = flexboxLayout.getReorderedChildAt(1) as TextView + val third = flexboxLayout.getReorderedChildAt(2) as TextView + + assertThat(first.text.toString(), `is`("2")) + assertThat(second.text.toString(), `is`("3")) + assertThat(third.text.toString(), `is`("1")) + } + + @Test + @FlakyTest + fun testEditFragment_changeFlexGrow() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout + assertNotNull(flexboxLayout) + onView(withId(R.id.textview1)).perform(click()) + onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1"), closeSoftKeyboard()) + onView(withId(R.id.button_ok)).perform(click()) + val first = activity.findViewById(R.id.textview1) as TextView + val second = activity.findViewById(R.id.textview2) as TextView + val third = activity.findViewById(R.id.textview3) as TextView + assertNotNull(first) + assertNotNull(second) + assertNotNull(third) + + assertThat(first.width, `is`(flexboxLayout.width - second.width - third.width)) + } + + @Test + @FlakyTest + fun testEditFragment_changeFlexGrowFloat() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout + assertNotNull(flexboxLayout) + onView(withId(R.id.textview1)).perform(click()) + onView(withId(R.id.edit_text_flex_grow)).perform(replaceText("1.0"), closeSoftKeyboard()) + onView(withId(R.id.button_ok)).perform(click()) + val first = activity.findViewById(R.id.textview1) as TextView + val second = activity.findViewById(R.id.textview2) as TextView + val third = activity.findViewById(R.id.textview3) as TextView + assertNotNull(first) + assertNotNull(second) + assertNotNull(third) + + assertThat(first.width, `is`(flexboxLayout.width - second.width - third.width)) + } + + @Test + @FlakyTest + fun testEditFragment_changeFlexBasisPercent() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) as FlexboxLayout + assertNotNull(flexboxLayout) + onView(withId(R.id.textview1)).perform(click()) + onView(withId(R.id.edit_text_flex_basis_percent)) + .perform(replaceText("50"), closeSoftKeyboard()) + onView(withId(R.id.button_ok)).perform(click()) + val first = activity.findViewById(R.id.textview1) + val second = activity.findViewById(R.id.textview2) + val third = activity.findViewById(R.id.textview3) + assertNotNull(first) + assertNotNull(second) + assertNotNull(third) + + assertTrue(first.width - 1 <= flexboxLayout.width / 2 || flexboxLayout.width / 2 <= first.width + 1) + } + + @Test + @FlakyTest + fun testSwitchRecyclerViewFragment() { + val activity = activityRule.activity + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + assertNotNull(flexboxLayout) + val navigationView = activity.findViewById(R.id.nav_view) + assertNotNull(navigationView) + assertNull(activity.findViewById(R.id.recyclerview)) + assertNotNull(activity.findViewById(R.id.flexbox_layout)) + + val radioGroup = navigationView.getHeaderView(0) + .findViewById(R.id.radiogroup_container_implementation) + activity.runOnUiThread { radioGroup.check(R.id.radiobutton_recyclerview) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertNotNull(activity.findViewById(R.id.recyclerview)) + assertNull(activity.findViewById(R.id.flexbox_layout)) + } +} diff --git a/flexbox/build.gradle b/flexbox/build.gradle index dea7b62..00411f3 100644 --- a/flexbox/build.gradle +++ b/flexbox/build.gradle @@ -15,6 +15,7 @@ */ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' apply from: 'maven.gradle' apply from: 'bintray.gradle' @@ -48,4 +49,5 @@ dependencies { androidTestCompile "com.android.support:support-annotations:${rootProject.ext.supportLibVersion}" androidTestCompile "com.android.support.test:runner:${rootProject.ext.testRunnerVersion}" androidTestCompile "com.android.support.test.espresso:espresso-core:${rootProject.ext.espressoVersion}" + androidTestCompile "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.kotlinVersion}" } diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.java b/flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.java deleted file mode 100644 index c08fc64..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox; - -import android.view.View; -import android.view.ViewGroup; - -import java.util.ArrayList; -import java.util.List; - -/** - * Fake implementation of {@link FlexContainer}. - */ -class FakeFlexContainer implements FlexContainer { - - private List mViews = new ArrayList<>(); - - private List mFlexLines = new ArrayList<>(); - - @FlexDirection - private int mFlexDirection = FlexDirection.ROW; - - @FlexWrap - private int mFlexWrap = FlexWrap.WRAP; - - @JustifyContent - private int mJustifyContent = JustifyContent.FLEX_START; - - @AlignItems - private int mAlignItems = AlignItems.STRETCH; - - @AlignContent - private int mAlignContent = AlignContent.STRETCH; - - @Override - public int getFlexItemCount() { - return mViews.size(); - } - - @Override - public View getFlexItemAt(int index) { - return mViews.get(index); - } - - @Override - public View getReorderedFlexItemAt(int index) { - return mViews.get(index); - } - - @Override - public void addView(View view) { - mViews.add(view); - } - - @Override - public void addView(View view, int index) { - mViews.add(index, view); - } - - @Override - public void removeAllViews() { - mViews.clear(); - } - - @Override - public void removeViewAt(int index) { - mViews.remove(index); - } - - @Override - public int getFlexDirection() { - return mFlexDirection; - } - - @Override - public void setFlexDirection(@FlexDirection int flexDirection) { - mFlexDirection = flexDirection; - } - - @Override - public int getFlexWrap() { - return mFlexWrap; - } - - @Override - public void setFlexWrap(@FlexWrap int flexWrap) { - mFlexWrap = flexWrap; - } - - @Override - public int getJustifyContent() { - return mJustifyContent; - } - - @Override - public void setJustifyContent(@JustifyContent int justifyContent) { - mJustifyContent = justifyContent; - } - - @Override - public int getAlignContent() { - return mAlignContent; - } - - @Override - public void setAlignContent(@AlignContent int alignContent) { - mAlignContent = alignContent; - } - - @Override - public int getAlignItems() { - return mAlignItems; - } - - @Override - public void setAlignItems(@AlignItems int alignItems) { - mAlignItems = alignItems; - } - - @Override - public List getFlexLines() { - return mFlexLines; - } - - @Override - public boolean isMainAxisDirectionHorizontal() { - return mFlexDirection == FlexDirection.ROW || mFlexDirection == FlexDirection.ROW_REVERSE; - } - - @Override - public int getDecorationLengthMainAxis(View view, int index, int indexInFlexLine) { - return 0; - } - - @Override - public int getDecorationLengthCrossAxis(View view) { - return 0; - } - - @Override - public int getPaddingTop() { - return 0; - } - - @Override - public int getPaddingLeft() { - return 0; - } - - @Override - public int getPaddingRight() { - return 0; - } - - @Override - public int getPaddingBottom() { - return 0; - } - - @Override - public int getPaddingStart() { - return 0; - } - - @Override - public int getPaddingEnd() { - return 0; - } - - @Override - public int getChildWidthMeasureSpec(int widthSpec, int padding, int childDimension) { - return ViewGroup.getChildMeasureSpec(widthSpec, padding, childDimension); - } - - @Override - public int getChildHeightMeasureSpec(int heightSpec, int padding, int childDimension) { - return ViewGroup.getChildMeasureSpec(heightSpec, padding, childDimension); - } - - @Override - public int getLargestMainSize() { - int largestSize = Integer.MIN_VALUE; - for (FlexLine flexLine : mFlexLines) { - largestSize = Math.max(largestSize, flexLine.mMainSize); - } - return largestSize; - } - - @Override - public int getSumOfCrossSize() { - int sum = 0; - for (int i = 0, size = mFlexLines.size(); i < size; i++) { - FlexLine flexLine = mFlexLines.get(i); - sum += flexLine.mCrossSize; - } - return sum; - } - - @Override - public void onNewFlexItemAdded(View view, int index, int indexInFlexLine, FlexLine flexLine) { - // No op - } - - @Override - public void onNewFlexLineAdded(FlexLine flexLine) { - // No op - } - - @Override - public void setFlexLines(List flexLines) { - mFlexLines = flexLines; - } - - @Override - public List getFlexLinesInternal() { - return mFlexLines; - } - - @Override - public void updateViewCache(int position, View view) { - // No op - } -} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.kt new file mode 100644 index 0000000..aec9bc5 --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/FakeFlexContainer.kt @@ -0,0 +1,143 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox + +import android.view.View +import android.view.ViewGroup + +/** + * Fake implementation of [FlexContainer]. + */ +internal class FakeFlexContainer : FlexContainer { + + private val views = mutableListOf() + + private var flexLines = mutableListOf() + + @FlexDirection + private var flexDirection = FlexDirection.ROW + + @FlexWrap + private var flexWrap = FlexWrap.WRAP + + @JustifyContent + private var justifyContent = JustifyContent.FLEX_START + + @AlignItems + private var alignItems = AlignItems.STRETCH + + @AlignContent + private var alignContent = AlignContent.STRETCH + + override fun getFlexItemCount() = views.size + + override fun getFlexItemAt(index: Int) = views[index] + + override fun getReorderedFlexItemAt(index: Int) = views[index] + + override fun addView(view: View) { + views.add(view) + } + + override fun addView(view: View, index: Int) { + views.add(index, view) + } + + override fun removeAllViews() { + views.clear() + } + + override fun removeViewAt(index: Int) { + views.removeAt(index) + } + + override fun getFlexDirection() = flexDirection + + override fun setFlexDirection(@FlexDirection flexDirection: Int) { + this.flexDirection = flexDirection + } + + override fun getFlexWrap() = flexWrap + + override fun setFlexWrap(@FlexWrap flexWrap: Int) { + this.flexWrap = flexWrap + } + + override fun getJustifyContent() = justifyContent + + override fun setJustifyContent(@JustifyContent justifyContent: Int) { + this.justifyContent = justifyContent + } + + override fun getAlignContent() = alignContent + + override fun setAlignContent(@AlignContent alignContent: Int) { + this.alignContent = alignContent + } + + override fun getAlignItems() = alignItems + + override fun setAlignItems(@AlignItems alignItems: Int) { + this.alignItems = alignItems + } + + override fun getFlexLines() = flexLines + + override fun isMainAxisDirectionHorizontal(): Boolean { + return flexDirection == FlexDirection.ROW || flexDirection == FlexDirection.ROW_REVERSE + } + + override fun getDecorationLengthMainAxis(view: View, index: Int, indexInFlexLine: Int) = 0 + + override fun getDecorationLengthCrossAxis(view: View) = 0 + + override fun getPaddingTop() = 0 + + override fun getPaddingLeft() = 0 + + override fun getPaddingRight() = 0 + + override fun getPaddingBottom() = 0 + + override fun getPaddingStart() = 0 + + override fun getPaddingEnd() = 0 + + override fun getChildWidthMeasureSpec(widthSpec: Int, padding: Int, childDimension: Int): Int { + return ViewGroup.getChildMeasureSpec(widthSpec, padding, childDimension) + } + + override fun getChildHeightMeasureSpec(heightSpec: Int, padding: Int, childDimension: Int): Int { + return ViewGroup.getChildMeasureSpec(heightSpec, padding, childDimension) + } + + override fun getLargestMainSize() = flexLines.maxBy { it.mMainSize }?.mMainSize ?: Integer.MIN_VALUE + + override fun getSumOfCrossSize() = flexLines.sumBy { it.mCrossSize } + + override fun onNewFlexItemAdded(view: View, index: Int, indexInFlexLine: Int, flexLine: FlexLine) = Unit + + override fun onNewFlexLineAdded(flexLine: FlexLine) = Unit + + override fun setFlexLines(flexLines: List) { + this.flexLines = flexLines.toMutableList() + } + + override fun getFlexLinesInternal() = flexLines + + override fun updateViewCache(position: Int, view: View) = Unit +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.java b/flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.java deleted file mode 100644 index 4f52cda..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.java +++ /dev/null @@ -1,437 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox; - -import static com.google.android.flexbox.test.IsEqualAllowingError.isEqualAllowingError; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; - -import android.app.Activity; -import android.support.test.rule.ActivityTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.view.View; - -import com.google.android.flexbox.test.FlexboxTestActivity; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** - * Unit tests for {@link FlexboxHelper}. - */ -@RunWith(AndroidJUnit4.class) -public class FlexboxHelperTest { - - @Rule - public ActivityTestRule mActivityRule = - new ActivityTestRule<>(FlexboxTestActivity.class); - - private FlexboxHelper mFlexboxHelper; - - private FlexContainer mFlexContainer; - - @Before - public void setUp() { - mFlexContainer = new FakeFlexContainer(); - mFlexboxHelper = new FlexboxHelper(mFlexContainer); - } - - @Test - public void testCalculateHorizontalFlexLines() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(100, 100); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(200, 100); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(300, 100); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(400, 100); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexWrap(FlexWrap.WRAP); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - int heightMeasureSpec = View.MeasureSpec - .makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED); - - mFlexboxHelper.ensureIndexToFlexLine(mFlexContainer.getFlexItemCount()); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateHorizontalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - - assertEquals(3, result.mFlexLines.size()); - assertEquals(300, result.mFlexLines.get(0).getMainSize()); - assertEquals(300, result.mFlexLines.get(1).getMainSize()); - assertEquals(400, result.mFlexLines.get(2).getMainSize()); - assertEquals(100, result.mFlexLines.get(0).getCrossSize()); - assertEquals(100, result.mFlexLines.get(1).getCrossSize()); - assertEquals(100, result.mFlexLines.get(2).getCrossSize()); - - assertNotNull(mFlexboxHelper.mIndexToFlexLine); - assertEquals(0, mFlexboxHelper.mIndexToFlexLine[0]); - assertEquals(0, mFlexboxHelper.mIndexToFlexLine[1]); - assertEquals(1, mFlexboxHelper.mIndexToFlexLine[2]); - assertEquals(2, mFlexboxHelper.mIndexToFlexLine[3]); - - FlexLine firstLine = result.mFlexLines.get(0); - assertEquals(0, firstLine.mFirstIndex); - assertEquals(1, firstLine.mLastIndex); - FlexLine secondLine = result.mFlexLines.get(1); - assertEquals(2, secondLine.mFirstIndex); - assertEquals(2, secondLine.mLastIndex); - FlexLine thirdLine = result.mFlexLines.get(2); - assertEquals(3, thirdLine.mFirstIndex); - assertEquals(3, thirdLine.mLastIndex); - } - - @Test - public void testCalculateVerticalFlexLines() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(100, 100); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(100, 200); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(100, 300); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(100, 400); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexWrap(FlexWrap.WRAP); - mFlexContainer.setFlexDirection(FlexDirection.COLUMN); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED); - int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - - mFlexboxHelper.ensureIndexToFlexLine(mFlexContainer.getFlexItemCount()); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - - assertEquals(3, result.mFlexLines.size()); - assertEquals(300, result.mFlexLines.get(0).getMainSize()); - assertEquals(300, result.mFlexLines.get(1).getMainSize()); - assertEquals(400, result.mFlexLines.get(2).getMainSize()); - assertEquals(100, result.mFlexLines.get(0).getCrossSize()); - assertEquals(100, result.mFlexLines.get(1).getCrossSize()); - assertEquals(100, result.mFlexLines.get(2).getCrossSize()); - - assertNotNull(mFlexboxHelper.mIndexToFlexLine); - assertEquals(0, mFlexboxHelper.mIndexToFlexLine[0]); - assertEquals(0, mFlexboxHelper.mIndexToFlexLine[1]); - assertEquals(1, mFlexboxHelper.mIndexToFlexLine[2]); - assertEquals(2, mFlexboxHelper.mIndexToFlexLine[3]); - - FlexLine firstLine = result.mFlexLines.get(0); - assertEquals(0, firstLine.mFirstIndex); - assertEquals(1, firstLine.mLastIndex); - FlexLine secondLine = result.mFlexLines.get(1); - assertEquals(2, secondLine.mFirstIndex); - assertEquals(2, secondLine.mLastIndex); - FlexLine thirdLine = result.mFlexLines.get(2); - assertEquals(3, thirdLine.mFirstIndex); - assertEquals(3, thirdLine.mLastIndex); - } - - @Test - public void testDetermineMainSize_direction_row_flexGrowSet() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(100, 100); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(200, 100); - lp2.setFlexGrow(1.0f); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(300, 100); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(400, 100); - lp4.setFlexGrow(2.0f); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexDirection(FlexDirection.ROW); - mFlexContainer.setFlexWrap(FlexWrap.WRAP); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - int heightMeasureSpec = View.MeasureSpec - .makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateHorizontalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - mFlexContainer.setFlexLines(result.mFlexLines); - mFlexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec); - - assertThat(view1.getMeasuredWidth(), is(100)); - assertThat(view1.getMeasuredHeight(), is(100)); - // view2 will expand to fill the left space in the first flex line since flex grow is set - assertThat(view2.getMeasuredWidth(), is(400)); - assertThat(view2.getMeasuredHeight(), is(100)); - assertThat(view3.getMeasuredWidth(), is(300)); - assertThat(view3.getMeasuredHeight(), is(100)); - // view4 will expand to fill the left space in the first flex line since flex grow is set - assertThat(view4.getMeasuredWidth(), is(500)); - assertThat(view4.getMeasuredHeight(), is(100)); - } - - @Test - public void testDetermineMainSize_direction_column_flexGrowSet() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(100, 100); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(100, 200); - lp2.setFlexGrow(1.0f); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(100, 300); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(100, 400); - lp4.setFlexGrow(2.0f); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexDirection(FlexDirection.COLUMN); - mFlexContainer.setFlexWrap(FlexWrap.WRAP); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED); - int heightMeasureSpec = View.MeasureSpec - .makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - mFlexContainer.setFlexLines(result.mFlexLines); - mFlexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec); - - assertThat(view1.getMeasuredWidth(), is(100)); - assertThat(view1.getMeasuredHeight(), is(100)); - assertThat(view2.getMeasuredWidth(), is(100)); - // view2 will expand to fill the left space in the first flex line since flex grow is set - assertThat(view2.getMeasuredHeight(), is(400)); - assertThat(view3.getMeasuredWidth(), is(100)); - assertThat(view3.getMeasuredHeight(), is(300)); - assertThat(view4.getMeasuredWidth(), is(100)); - // view4 will expand to fill the left space in the first flex line since flex grow is set - assertThat(view4.getMeasuredHeight(), is(500)); - } - - @Test - public void testDetermineMainSize_direction_row_flexShrinkSet() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(200, 100); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(200, 100); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(200, 100); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(200, 100); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexDirection(FlexDirection.ROW); - mFlexContainer.setFlexWrap(FlexWrap.NOWRAP); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - int heightMeasureSpec = View.MeasureSpec - .makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - mFlexContainer.setFlexLines(result.mFlexLines); - mFlexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec); - - // Flex shrink is set to 1.0 (default value) for all views. - // They should be shrank equally for the amount overflown the width - assertThat(view1.getMeasuredWidth(), is(125)); - assertThat(view1.getMeasuredHeight(), is(100)); - assertThat(view2.getMeasuredWidth(), is(125)); - assertThat(view2.getMeasuredHeight(), is(100)); - assertThat(view3.getMeasuredWidth(), is(125)); - assertThat(view3.getMeasuredHeight(), is(100)); - assertThat(view4.getMeasuredWidth(), is(125)); - assertThat(view4.getMeasuredHeight(), is(100)); - } - - @Test - public void testDetermineMainSize_direction_column_flexShrinkSet() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(100, 200); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(100, 200); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(100, 200); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(100, 200); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexDirection(FlexDirection.COLUMN); - mFlexContainer.setFlexWrap(FlexWrap.NOWRAP); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED); - int heightMeasureSpec = View.MeasureSpec - .makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - mFlexContainer.setFlexLines(result.mFlexLines); - mFlexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec); - - // Flex shrink is set to 1.0 (default value) for all views. - // They should be shrank equally for the amount overflown the height - assertThat(view1.getMeasuredWidth(), is(100)); - assertThat(view1.getMeasuredHeight(), is(125)); - assertThat(view2.getMeasuredWidth(), is(100)); - assertThat(view2.getMeasuredHeight(), is(125)); - assertThat(view3.getMeasuredWidth(), is(100)); - assertThat(view3.getMeasuredHeight(), is(125)); - assertThat(view4.getMeasuredWidth(), is(100)); - assertThat(view4.getMeasuredHeight(), is(125)); - } - - @Test - public void testDetermineCrossSize_direction_row_alignContent_stretch() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(100, 100); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(200, 100); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(300, 100); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(400, 100); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexDirection(FlexDirection.ROW); - mFlexContainer.setFlexWrap(FlexWrap.WRAP); - mFlexContainer.setAlignContent(AlignContent.STRETCH); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - int heightMeasureSpec = View.MeasureSpec - .makeMeasureSpec(1000, View.MeasureSpec.EXACTLY); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateHorizontalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - mFlexContainer.setFlexLines(result.mFlexLines); - mFlexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec); - mFlexboxHelper - .determineCrossSize(widthMeasureSpec, heightMeasureSpec, 0); - mFlexboxHelper.stretchViews(); - - // align content is set to Align.STRETCH, the cross size for each flex line is stretched - // to distribute the remaining free space along the cross axis - // (remaining height in this case) - assertThat(view1.getMeasuredHeight(), isEqualAllowingError(333)); - assertThat(view2.getMeasuredHeight(), isEqualAllowingError(333)); - assertThat(view3.getMeasuredHeight(), isEqualAllowingError(333)); - assertThat(view4.getMeasuredHeight(), isEqualAllowingError(333)); - } - - @Test - public void testDetermineCrossSize_direction_column_alignContent_stretch() throws Throwable { - Activity activity = mActivityRule.getActivity(); - FlexboxLayout.LayoutParams lp1 = new FlexboxLayout.LayoutParams(100, 100); - View view1 = new View(activity); - view1.setLayoutParams(lp1); - FlexboxLayout.LayoutParams lp2 = new FlexboxLayout.LayoutParams(100, 200); - View view2 = new View(activity); - view2.setLayoutParams(lp2); - FlexboxLayout.LayoutParams lp3 = new FlexboxLayout.LayoutParams(100, 300); - View view3 = new View(activity); - view3.setLayoutParams(lp3); - FlexboxLayout.LayoutParams lp4 = new FlexboxLayout.LayoutParams(100, 400); - View view4 = new View(activity); - view4.setLayoutParams(lp4); - mFlexContainer.addView(view1); - mFlexContainer.addView(view2); - mFlexContainer.addView(view3); - mFlexContainer.addView(view4); - mFlexContainer.setFlexDirection(FlexDirection.COLUMN); - mFlexContainer.setFlexWrap(FlexWrap.WRAP); - mFlexContainer.setAlignContent(AlignContent.STRETCH); - int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY); - int heightMeasureSpec = View.MeasureSpec - .makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - FlexboxHelper.FlexLinesResult result = new FlexboxHelper.FlexLinesResult(); - mFlexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec); - mFlexContainer.setFlexLines(result.mFlexLines); - mFlexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec); - mFlexboxHelper - .determineCrossSize(widthMeasureSpec, heightMeasureSpec, 0); - mFlexboxHelper.stretchViews(); - - // align content is set to Align.STRETCH, the cross size for each flex line is stretched - // to distribute the remaining free space along the cross axis - // (remaining width in this case) - assertThat(view1.getMeasuredWidth(), isEqualAllowingError(333)); - assertThat(view2.getMeasuredWidth(), isEqualAllowingError(333)); - assertThat(view3.getMeasuredWidth(), isEqualAllowingError(333)); - assertThat(view4.getMeasuredWidth(), isEqualAllowingError(333)); - } - - @Test - public void testMakeCombinedLong() { - int higher = -1; - int lower = 10; - long combined = mFlexboxHelper.makeCombinedLong(lower, higher); - assertThat(mFlexboxHelper.extractHigherInt(combined), is(higher)); - assertThat(mFlexboxHelper.extractLowerInt(combined), is(lower)); - - higher = Integer.MAX_VALUE; - lower = Integer.MIN_VALUE; - combined = mFlexboxHelper.makeCombinedLong(lower, higher); - assertThat(mFlexboxHelper.extractHigherInt(combined), is(higher)); - assertThat(mFlexboxHelper.extractLowerInt(combined), is(lower)); - - higher = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY); - lower = View.MeasureSpec.makeMeasureSpec(300, View.MeasureSpec.UNSPECIFIED); - combined = mFlexboxHelper.makeCombinedLong(lower, higher); - assertThat(mFlexboxHelper.extractHigherInt(combined), is(higher)); - assertThat(mFlexboxHelper.extractLowerInt(combined), is(lower)); - } -} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.kt new file mode 100644 index 0000000..d1a2d92 --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/FlexboxHelperTest.kt @@ -0,0 +1,430 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox + +import android.support.test.rule.ActivityTestRule +import android.support.test.runner.AndroidJUnit4 +import android.view.View +import com.google.android.flexbox.test.FlexboxTestActivity +import com.google.android.flexbox.test.IsEqualAllowingError.Companion.isEqualAllowingError +import junit.framework.Assert.assertEquals +import junit.framework.Assert.assertNotNull +import org.hamcrest.Matchers.`is` +import org.junit.Assert.assertThat +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Unit tests for [FlexboxHelper]. + */ +@RunWith(AndroidJUnit4::class) +class FlexboxHelperTest { + + @JvmField + @Rule + var activityRule = ActivityTestRule(FlexboxTestActivity::class.java) + + private lateinit var flexboxHelper: FlexboxHelper + + private lateinit var flexContainer: FlexContainer + + @Before + fun setUp() { + flexContainer = FakeFlexContainer() + flexboxHelper = FlexboxHelper(flexContainer) + } + + @Test + @Throws(Throwable::class) + fun testCalculateHorizontalFlexLines() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(100, 100) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(200, 100) + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(300, 100) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(400, 100) + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexWrap = FlexWrap.WRAP + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED) + + flexboxHelper.ensureIndexToFlexLine(flexContainer.flexItemCount) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateHorizontalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + + assertEquals(3, result.mFlexLines.size) + assertEquals(300, result.mFlexLines[0].mainSize) + assertEquals(300, result.mFlexLines[1].mainSize) + assertEquals(400, result.mFlexLines[2].mainSize) + assertEquals(100, result.mFlexLines[0].crossSize) + assertEquals(100, result.mFlexLines[1].crossSize) + assertEquals(100, result.mFlexLines[2].crossSize) + + assertNotNull(flexboxHelper.mIndexToFlexLine) + assertEquals(0, flexboxHelper.mIndexToFlexLine!![0]) + assertEquals(0, flexboxHelper.mIndexToFlexLine!![1]) + assertEquals(1, flexboxHelper.mIndexToFlexLine!![2]) + assertEquals(2, flexboxHelper.mIndexToFlexLine!![3]) + + val firstLine = result.mFlexLines[0] + assertEquals(0, firstLine.mFirstIndex) + assertEquals(1, firstLine.mLastIndex) + val secondLine = result.mFlexLines[1] + assertEquals(2, secondLine.mFirstIndex) + assertEquals(2, secondLine.mLastIndex) + val thirdLine = result.mFlexLines[2] + assertEquals(3, thirdLine.mFirstIndex) + assertEquals(3, thirdLine.mLastIndex) + } + + @Test + @Throws(Throwable::class) + fun testCalculateVerticalFlexLines() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(100, 100) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(100, 200) + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(100, 300) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(100, 400) + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexWrap = FlexWrap.WRAP + flexContainer.flexDirection = FlexDirection.COLUMN + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + + flexboxHelper.ensureIndexToFlexLine(flexContainer.flexItemCount) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + + assertEquals(3, result.mFlexLines.size) + assertEquals(300, result.mFlexLines[0].mainSize) + assertEquals(300, result.mFlexLines[1].mainSize) + assertEquals(400, result.mFlexLines[2].mainSize) + assertEquals(100, result.mFlexLines[0].crossSize) + assertEquals(100, result.mFlexLines[1].crossSize) + assertEquals(100, result.mFlexLines[2].crossSize) + + assertNotNull(flexboxHelper.mIndexToFlexLine) + assertEquals(0, flexboxHelper.mIndexToFlexLine!![0]) + assertEquals(0, flexboxHelper.mIndexToFlexLine!![1]) + assertEquals(1, flexboxHelper.mIndexToFlexLine!![2]) + assertEquals(2, flexboxHelper.mIndexToFlexLine!![3]) + + val firstLine = result.mFlexLines[0] + assertEquals(0, firstLine.mFirstIndex) + assertEquals(1, firstLine.mLastIndex) + val secondLine = result.mFlexLines[1] + assertEquals(2, secondLine.mFirstIndex) + assertEquals(2, secondLine.mLastIndex) + val thirdLine = result.mFlexLines[2] + assertEquals(3, thirdLine.mFirstIndex) + assertEquals(3, thirdLine.mLastIndex) + } + + @Test + @Throws(Throwable::class) + fun testDetermineMainSize_direction_row_flexGrowSet() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(100, 100) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(200, 100) + lp2.flexGrow = 1.0f + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(300, 100) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(400, 100) + lp4.flexGrow = 2.0f + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexDirection = FlexDirection.ROW + flexContainer.flexWrap = FlexWrap.WRAP + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateHorizontalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + flexContainer.flexLines = result.mFlexLines + flexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec) + + assertThat(view1.measuredWidth, `is`(100)) + assertThat(view1.measuredHeight, `is`(100)) + // view2 will expand to fill the left space in the first flex line since flex grow is set + assertThat(view2.measuredWidth, `is`(400)) + assertThat(view2.measuredHeight, `is`(100)) + assertThat(view3.measuredWidth, `is`(300)) + assertThat(view3.measuredHeight, `is`(100)) + // view4 will expand to fill the left space in the first flex line since flex grow is set + assertThat(view4.measuredWidth, `is`(500)) + assertThat(view4.measuredHeight, `is`(100)) + } + + @Test + @Throws(Throwable::class) + fun testDetermineMainSize_direction_column_flexGrowSet() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(100, 100) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(100, 200) + lp2.flexGrow = 1.0f + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(100, 300) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(100, 400) + lp4.flexGrow = 2.0f + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexDirection = FlexDirection.COLUMN + flexContainer.flexWrap = FlexWrap.WRAP + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + flexContainer.flexLines = result.mFlexLines + flexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec) + + assertThat(view1.measuredWidth, `is`(100)) + assertThat(view1.measuredHeight, `is`(100)) + assertThat(view2.measuredWidth, `is`(100)) + // view2 will expand to fill the left space in the first flex line since flex grow is set + assertThat(view2.measuredHeight, `is`(400)) + assertThat(view3.measuredWidth, `is`(100)) + assertThat(view3.measuredHeight, `is`(300)) + assertThat(view4.measuredWidth, `is`(100)) + // view4 will expand to fill the left space in the first flex line since flex grow is set + assertThat(view4.measuredHeight, `is`(500)) + } + + @Test + @Throws(Throwable::class) + fun testDetermineMainSize_direction_row_flexShrinkSet() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(200, 100) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(200, 100) + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(200, 100) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(200, 100) + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexDirection = FlexDirection.ROW + flexContainer.flexWrap = FlexWrap.NOWRAP + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + flexContainer.flexLines = result.mFlexLines + flexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec) + + // Flex shrink is set to 1.0 (default value) for all views. + // They should be shrank equally for the amount overflown the width + assertThat(view1.measuredWidth, `is`(125)) + assertThat(view1.measuredHeight, `is`(100)) + assertThat(view2.measuredWidth, `is`(125)) + assertThat(view2.measuredHeight, `is`(100)) + assertThat(view3.measuredWidth, `is`(125)) + assertThat(view3.measuredHeight, `is`(100)) + assertThat(view4.measuredWidth, `is`(125)) + assertThat(view4.measuredHeight, `is`(100)) + } + + @Test + @Throws(Throwable::class) + fun testDetermineMainSize_direction_column_flexShrinkSet() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(100, 200) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(100, 200) + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(100, 200) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(100, 200) + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexDirection = FlexDirection.COLUMN + flexContainer.flexWrap = FlexWrap.NOWRAP + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.UNSPECIFIED) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + flexContainer.flexLines = result.mFlexLines + flexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec) + + // Flex shrink is set to 1.0 (default value) for all views. + // They should be shrank equally for the amount overflown the height + assertThat(view1.measuredWidth, `is`(100)) + assertThat(view1.measuredHeight, `is`(125)) + assertThat(view2.measuredWidth, `is`(100)) + assertThat(view2.measuredHeight, `is`(125)) + assertThat(view3.measuredWidth, `is`(100)) + assertThat(view3.measuredHeight, `is`(125)) + assertThat(view4.measuredWidth, `is`(100)) + assertThat(view4.measuredHeight, `is`(125)) + } + + @Test + @Throws(Throwable::class) + fun testDetermineCrossSize_direction_row_alignContent_stretch() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(100, 100) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(200, 100) + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(300, 100) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(400, 100) + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexDirection = FlexDirection.ROW + flexContainer.flexWrap = FlexWrap.WRAP + flexContainer.alignContent = AlignContent.STRETCH + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateHorizontalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + flexContainer.flexLines = result.mFlexLines + flexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec) + flexboxHelper.determineCrossSize(widthMeasureSpec, heightMeasureSpec, 0) + flexboxHelper.stretchViews() + + // align content is set to Align.STRETCH, the cross size for each flex line is stretched + // to distribute the remaining free space along the cross axis + // (remaining height in this case) + assertThat(view1.measuredHeight, isEqualAllowingError(333)) + assertThat(view2.measuredHeight, isEqualAllowingError(333)) + assertThat(view3.measuredHeight, isEqualAllowingError(333)) + assertThat(view4.measuredHeight, isEqualAllowingError(333)) + } + + @Test + @Throws(Throwable::class) + fun testDetermineCrossSize_direction_column_alignContent_stretch() { + val activity = activityRule.activity + val lp1 = FlexboxLayout.LayoutParams(100, 100) + val view1 = View(activity) + view1.layoutParams = lp1 + val lp2 = FlexboxLayout.LayoutParams(100, 200) + val view2 = View(activity) + view2.layoutParams = lp2 + val lp3 = FlexboxLayout.LayoutParams(100, 300) + val view3 = View(activity) + view3.layoutParams = lp3 + val lp4 = FlexboxLayout.LayoutParams(100, 400) + val view4 = View(activity) + view4.layoutParams = lp4 + flexContainer.addView(view1) + flexContainer.addView(view2) + flexContainer.addView(view3) + flexContainer.addView(view4) + flexContainer.flexDirection = FlexDirection.COLUMN + flexContainer.flexWrap = FlexWrap.WRAP + flexContainer.alignContent = AlignContent.STRETCH + val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.EXACTLY) + val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + val result = FlexboxHelper.FlexLinesResult() + flexboxHelper.calculateVerticalFlexLines(result, widthMeasureSpec, heightMeasureSpec) + flexContainer.flexLines = result.mFlexLines + flexboxHelper.determineMainSize(widthMeasureSpec, heightMeasureSpec) + flexboxHelper.determineCrossSize(widthMeasureSpec, heightMeasureSpec, 0) + flexboxHelper.stretchViews() + + // align content is set to Align.STRETCH, the cross size for each flex line is stretched + // to distribute the remaining free space along the cross axis + // (remaining width in this case) + assertThat(view1.measuredWidth, isEqualAllowingError(333)) + assertThat(view2.measuredWidth, isEqualAllowingError(333)) + assertThat(view3.measuredWidth, isEqualAllowingError(333)) + assertThat(view4.measuredWidth, isEqualAllowingError(333)) + } + + @Test + fun testMakeCombinedLong() { + var higher = -1 + var lower = 10 + var combined = flexboxHelper.makeCombinedLong(lower, higher) + assertThat(flexboxHelper.extractHigherInt(combined), `is`(higher)) + assertThat(flexboxHelper.extractLowerInt(combined), `is`(lower)) + + higher = Integer.MAX_VALUE + lower = Integer.MIN_VALUE + combined = flexboxHelper.makeCombinedLong(lower, higher) + assertThat(flexboxHelper.extractHigherInt(combined), `is`(higher)) + assertThat(flexboxHelper.extractLowerInt(combined), `is`(lower)) + + higher = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY) + lower = View.MeasureSpec.makeMeasureSpec(300, View.MeasureSpec.UNSPECIFIED) + combined = flexboxHelper.makeCombinedLong(lower, higher) + assertThat(flexboxHelper.extractHigherInt(combined), `is`(higher)) + assertThat(flexboxHelper.extractLowerInt(combined), `is`(lower)) + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/ConfigChangeActivity.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/ConfigChangeActivity.kt similarity index 54% rename from flexbox/src/androidTest/java/com/google/android/flexbox/test/ConfigChangeActivity.java rename to flexbox/src/androidTest/java/com/google/android/flexbox/test/ConfigChangeActivity.kt index e3c08c3..3ede024 100644 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/ConfigChangeActivity.java +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/ConfigChangeActivity.kt @@ -14,26 +14,26 @@ * limitations under the License. */ -package com.google.android.flexbox.test; +package com.google.android.flexbox.test -import android.app.Activity; -import android.content.res.Configuration; -import android.util.Log; +import android.app.Activity +import android.content.res.Configuration +import android.util.Log -import com.google.android.flexbox.FlexboxLayout; +import com.google.android.flexbox.FlexboxLayout /** - * Activity for testing the {@link FlexboxLayout} that handles configuration changes by itself + * Activity for testing the [FlexboxLayout] that handles configuration changes by itself * instead of letting the system take care of those. */ -public class ConfigChangeActivity extends Activity { +class ConfigChangeActivity : Activity() { + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) - private static final String TAG = "ConfigChangeActivity"; - - @Override - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); + Log.d(TAG, "onConfigurationChanged: " + newConfig) + } - Log.d(TAG, "onConfigurationChanged: " + newConfig); + companion object { + private const val TAG = "ConfigChangeActivity" } } diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.java deleted file mode 100644 index e98590b..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.java +++ /dev/null @@ -1,4051 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - - -import static android.support.test.espresso.Espresso.onView; -import static android.support.test.espresso.assertion.PositionAssertions.isCompletelyAbove; -import static android.support.test.espresso.assertion.PositionAssertions.isBottomAlignedWith; -import static android.support.test.espresso.assertion.PositionAssertions.isCompletelyBelow; -import static android.support.test.espresso.assertion.PositionAssertions.isCompletelyLeftOf; -import static android.support.test.espresso.assertion.PositionAssertions.isCompletelyRightOf; -import static android.support.test.espresso.assertion.PositionAssertions.isLeftAlignedWith; -import static android.support.test.espresso.assertion.PositionAssertions.isRightAlignedWith; -import static android.support.test.espresso.assertion.PositionAssertions.isTopAlignedWith; -import static android.support.test.espresso.assertion.ViewAssertions.matches; -import static android.support.test.espresso.matcher.ViewMatchers.withId; - -import static com.google.android.flexbox.test.IsEqualAllowingError.isEqualAllowingError; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import android.content.Context; -import android.graphics.drawable.Drawable; -import android.support.annotation.LayoutRes; -import android.support.test.InstrumentationRegistry; -import android.support.test.espresso.ViewAssertion; -import android.support.test.filters.FlakyTest; -import android.support.test.filters.MediumTest; -import android.support.test.rule.ActivityTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.support.v4.content.res.ResourcesCompat; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import com.google.android.flexbox.AlignContent; -import com.google.android.flexbox.AlignItems; -import com.google.android.flexbox.FlexDirection; -import com.google.android.flexbox.FlexLine; -import com.google.android.flexbox.FlexWrap; -import com.google.android.flexbox.FlexboxLayout; -import com.google.android.flexbox.JustifyContent; - -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.List; - - -/** - * Integration tests for {@link FlexboxLayout}. - */ -@RunWith(AndroidJUnit4.class) -@MediumTest -public class FlexboxAndroidTest { - - @Rule - public ActivityTestRule mActivityRule = - new ActivityTestRule<>(FlexboxTestActivity.class); - - @Test - @FlakyTest - public void testLoadFromLayoutXml() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_simple); - - assertNotNull(flexboxLayout); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.CENTER)); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.CENTER)); - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.CENTER)); - assertThat(flexboxLayout.getChildCount(), is(1)); - - View child = flexboxLayout.getChildAt(0); - FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) child.getLayoutParams(); - assertThat(lp.getOrder(), is(2)); - assertThat(lp.getFlexGrow(), is(1f)); - assertThat(lp.getAlignSelf(), is(AlignItems.STRETCH)); - } - - @Test - @FlakyTest - public void testOrderAttribute_fromLayoutXml() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_order_test); - - assertNotNull(flexboxLayout); - assertThat(flexboxLayout.getChildCount(), is(4)); - // order: -1, index 1 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(0)).getText().toString(), - is(String.valueOf(2))); - // order: 0, index 2 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(1)).getText().toString(), - is(String.valueOf(3))); - // order: 1, index 3 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(2)).getText().toString(), - is(String.valueOf(4))); - // order: 2, index 0 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(3)).getText().toString(), - is(String.valueOf(1))); - } - - @Test - @FlakyTest - public void testOrderAttribute_fromCode() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - TextView fifth = createTextView(activity, String.valueOf(5), 0); - TextView sixth = createTextView(activity, String.valueOf(6), -10); - flexboxLayout.addView(fifth); - flexboxLayout.addView(sixth); - } - }); - - assertThat(flexboxLayout.getChildCount(), is(6)); - // order: -10, index 5 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(0)).getText().toString(), - is(String.valueOf(6))); - // order: -1, index 1 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(1)).getText().toString(), - is(String.valueOf(2))); - // order: 0, index 2 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(2)).getText().toString(), - is(String.valueOf(3))); - // order: 0, index 4 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(3)).getText().toString(), - is(String.valueOf(5))); - // order: 1, index 3 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(4)).getText().toString(), - is(String.valueOf(4))); - // order: 2, index 0 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(5)).getText().toString(), - is(String.valueOf(1))); - } - - @Test - @FlakyTest - public void testChangeOrder_fromChildSetLayoutParams() throws Throwable { - final FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_order_test); - - assertThat(flexboxLayout.getChildCount(), is(4)); - // order: -1, index 1 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(0)).getText().toString(), - is(String.valueOf(2))); - // order: 0, index 2 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(1)).getText().toString(), - is(String.valueOf(3))); - // order: 0, index 3 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(2)).getText().toString(), - is(String.valueOf(4))); - // order: 2, index 0 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(3)).getText().toString(), - is(String.valueOf(1))); - - // By changing the order and calling the setLayoutParams, the reordered array in the - // FlexboxLayout (mReordereredIndices) will be recreated without adding a new View. - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - View view1 = flexboxLayout.getChildAt(0); - FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) - view1.getLayoutParams(); - lp.setOrder(-3); - view1.setLayoutParams(lp); - } - }); - // order: -3, index 0 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(3)).getText().toString(), - is(String.valueOf(1))); - // order: -1, index 1 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(0)).getText().toString(), - is(String.valueOf(2))); - // order: 0, index 2 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(1)).getText().toString(), - is(String.valueOf(3))); - // order: 1, index 3 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(2)).getText().toString(), - is(String.valueOf(4))); - } - - @Test - @FlakyTest - public void testOrderAttribute_addViewInMiddle() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - TextView fifth = createTextView(activity, String.valueOf(5), 0); - // Add the new TextView in the middle of the indices - flexboxLayout.addView(fifth, 2); - } - }); - - assertNotNull(flexboxLayout); - assertThat(flexboxLayout.getChildCount(), is(5)); - // order: -1, index 1 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(0)).getText().toString(), - is(String.valueOf(2))); - // order: 0, index 2 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(1)).getText().toString(), - is(String.valueOf(5))); - // order: 0, index 3 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(2)).getText().toString(), - is(String.valueOf(3))); - // order: 0, index 4 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(3)).getText().toString(), - is(String.valueOf(4))); - // order: 2, index 0 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(4)).getText().toString(), - is(String.valueOf(1))); - } - - @Test - @FlakyTest - public void testOrderAttribute_removeLastView() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.removeViewAt(flexboxLayout.getChildCount() - 1); - } - }); - - assertNotNull(flexboxLayout); - assertThat(flexboxLayout.getChildCount(), is(3)); - // order: -1, index 1 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(0)).getText().toString(), - is(String.valueOf(2))); - // order: 0, index 2 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(1)).getText().toString(), - is(String.valueOf(3))); - // order: 2, index 0 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(2)).getText().toString(), - is(String.valueOf(1))); - } - - @Test - @FlakyTest - public void testOrderAttribute_removeViewInMiddle() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.removeViewAt(2); - } - }); - - assertNotNull(flexboxLayout); - assertThat(flexboxLayout.getChildCount(), is(3)); - // order: -1, index 1 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(0)).getText().toString(), - is(String.valueOf(2))); - // order: 0, index 3 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(1)).getText().toString(), - is(String.valueOf(4))); - // order: 2, index 0 - assertThat(((TextView) flexboxLayout.getReorderedChildAt(2)).getText().toString(), - is(String.valueOf(1))); - } - - @Test - @FlakyTest - public void testFlexWrap_wrap() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - // The width of the FlexboxLayout is not enough for placing the three text views. - // The third text view should be placed below the first one - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - FlexboxTestActivity activity = mActivityRule.getActivity(); - assertThat(flexLine1.getMainSize(), is(TestUtil.dpToPixel(activity, 320))); - assertThat(flexLine1.getCrossSize(), is(TestUtil.dpToPixel(activity, 120))); - FlexLine flexLine2 = flexLines.get(1); - assertThat(flexLine2.getMainSize(), is(TestUtil.dpToPixel(activity, 160))); - assertThat(flexLine2.getCrossSize(), is(TestUtil.dpToPixel(activity, 120))); - } - - @Test - @FlakyTest - public void testFlexWrap_nowrap() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.NOWRAP); - flexboxLayout.setAlignItems(AlignItems.FLEX_START); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.NOWRAP)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - // The width of the FlexboxLayout is not enough for placing the three text views. - // But the flexWrap attribute is set to NOWRAP, the third text view is placed - // to the right of the second one and overflowing the parent FlexboxLayout. - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - assertThat(flexboxLayout.getFlexLines().size(), is(1)); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(1)); - FlexLine flexLine = flexLines.get(0); - FlexboxTestActivity activity = mActivityRule.getActivity(); - assertThat(flexLine.getMainSize(), is(TestUtil.dpToPixel(activity, 480))); - assertThat(flexLine.getCrossSize(), is(TestUtil.dpToPixel(activity, 300))); - } - - @Test - @FlakyTest - public void testFlexWrap_wrap_reverse() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP_REVERSE)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - // The width of the FlexboxLayout is not enough for placing the three text views. - // There should be two flex lines same as WRAP, but the layout starts from bottom - // to top in FlexWrap.WRAP_REVERSE - onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - assertThat(flexboxLayout.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testFlexWrap_wrap_flexDirection_column() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - // The height of the FlexboxLayout is not enough for placing the three text views. - // The third text view should be placed right of the first one - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - assertThat(flexboxLayout.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testFlexWrap_nowrap_flexDirection_column() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - flexboxLayout.setFlexWrap(FlexWrap.NOWRAP); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.NOWRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - // The height of the FlexboxLayout is not enough for placing the three text views. - // But the flexWrap attribute is set to NOWRAP, the third text view is placed - // below the second one and overflowing the parent FlexboxLayout. - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - assertThat(flexboxLayout.getFlexLines().size(), is(1)); - } - - @Test - @FlakyTest - public void testFlexWrap_wrap_reverse_flexDirection_column() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP_REVERSE)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - // The width of the FlexboxLayout is not enough for placing the three text views. - // There should be two flex lines same as WRAP, but the layout starts from right - // to left in FlexWrap.WRAP_REVERSE - onView(withId(R.id.text3)).check(isCompletelyLeftOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyLeftOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - assertThat(flexboxLayout.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testFlexItem_match_parent() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_item_match_parent); - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - - assertThat(text1.getWidth(), is(flexboxLayout.getWidth())); - assertThat(text2.getWidth(), is(flexboxLayout.getWidth())); - assertThat(text3.getWidth(), is(flexboxLayout.getWidth())); - assertThat(flexboxLayout.getHeight(), - is(text1.getHeight() + text2.getHeight() + text3.getHeight())); - } - - @Test - @FlakyTest - public void testFlexItem_match_parent_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_flex_item_match_parent_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - - assertThat(text1.getHeight(), is(flexboxLayout.getHeight())); - assertThat(text2.getHeight(), is(flexboxLayout.getHeight())); - assertThat(text3.getHeight(), is(flexboxLayout.getHeight())); - assertThat(flexboxLayout.getWidth(), - is(text1.getWidth() + text2.getWidth() + text3.getWidth())); - } - - @Test - @FlakyTest - public void testFlexboxLayout_wrapContent() throws Throwable { - createFlexboxLayout(R.layout.activity_flexbox_wrap_content); - // The parent FlexboxLayout's layout_width and layout_height are set to wrap_content - // The size of the FlexboxLayout is aligned with three text views. - - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFlexboxLayout_wrapped_with_ScrollView() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_flexbox_wrapped_with_scrollview); - - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - - // The heightMode of the FlexboxLayout is set as MeasureSpec.UNSPECIFIED, the height of the - // layout will be expanded to include the all children views - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(flexboxLayout.getHeight(), is(textView1.getHeight() + textView3.getHeight())); - } - - @Test - @FlakyTest - public void testFlexboxLayout_wrapped_with_HorizontalScrollView() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_flexbox_wrapped_with_horizontalscrollview); - - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - // The widthMode of the FlexboxLayout is set as MeasureSpec.UNSPECIFIED, the widht of the - // layout will be expanded to include the all children views - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(flexboxLayout.getWidth(), is(textView1.getWidth() + textView2.getWidth() + - textView3.getWidth())); - } - - @Test - @FlakyTest - public void testJustifyContent_flexStart() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.FLEX_START)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexStart_withParentPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_justify_content_with_parent_padding); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.FLEX_START)); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - TextView text1 = activity.findViewById(R.id.text1); - // Both the parent FrameLayout and the FlexboxLayout have different padding values - // but the text1.getLeft should be the padding value for the FlexboxLayout, not including - // the parent's padding value - assertThat(text1.getLeft(), is(flexboxLayout.getPaddingLeft())); - assertThat(text1.getTop(), is(flexboxLayout.getPaddingTop())); - } - - @Test - @FlakyTest - public void testJustifyContent_flexEnd() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.FLEX_END); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.FLEX_END)); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text3))); - onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text2))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexEnd_withParentPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_justify_content_with_parent_padding, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.FLEX_END); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.FLEX_END)); - onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text3))); - onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text2))); - TextView text3 = activity.findViewById(R.id.text3); - // Both the parent FrameLayout and the FlexboxLayout have different padding values - // but the text3.getRight should be the padding value for the FlexboxLayout, not including - // the parent's padding value - assertThat(flexboxLayout.getWidth() - text3.getRight(), - is(flexboxLayout.getPaddingRight())); - assertThat(text3.getTop(), is(flexboxLayout.getPaddingTop())); - } - - @Test - @FlakyTest - public void testJustifyContent_center() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.CENTER); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.CENTER)); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth() - - textView3.getWidth(); - space = space / 2; - assertThat(textView1.getLeft(), isEqualAllowingError(space)); - assertThat(flexboxLayout.getRight() - textView3.getRight(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_center_withParentPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_justify_content_with_parent_padding, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.CENTER); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.CENTER)); - - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth() - - textView3.getWidth() - flexboxLayout.getPaddingLeft() - - flexboxLayout.getPaddingRight(); - space = space / 2; - assertThat(textView1.getLeft() - flexboxLayout.getPaddingLeft(), - isEqualAllowingError(space)); - assertThat(flexboxLayout.getWidth() - textView3.getRight() - - flexboxLayout.getPaddingRight(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent( - JustifyContent.SPACE_BETWEEN); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_BETWEEN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth() - - textView3.getWidth(); - space = space / 2; - assertThat(textView2.getLeft() - textView1.getRight(), isEqualAllowingError(space)); - assertThat(textView3.getLeft() - textView2.getRight(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_withPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final int padding = 40; - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent( - JustifyContent.SPACE_BETWEEN); - flexboxLayout.setPadding(padding, padding, padding, padding); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_BETWEEN)); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth() - - textView3.getWidth() - padding * 2; - space = space / 2; - assertThat(textView1.getLeft(), is(padding)); - assertThat(flexboxLayout.getRight() - textView3.getRight(), is(padding)); - assertThat(textView2.getLeft() - textView1.getRight(), isEqualAllowingError(space)); - assertThat(textView3.getLeft() - textView2.getRight(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.SPACE_AROUND); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_AROUND)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth() - - textView3.getWidth(); - space = space / 6; // Divide by the number of children * 2 - assertTrue(space - 1 <= textView1.getLeft() && textView1.getLeft() <= space + 1); - int spaceInMiddle = space * 2; - assertThat(textView2.getLeft() - textView1.getRight(), isEqualAllowingError(spaceInMiddle)); - assertThat(textView3.getLeft() - textView2.getRight(), isEqualAllowingError(spaceInMiddle)); - assertThat(flexboxLayout.getRight() - textView3.getRight(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_withPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final int padding = 40; - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.SPACE_AROUND); - flexboxLayout.setPadding(padding, padding, padding, padding); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_AROUND)); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth() - - textView3.getWidth() - padding * 2; - space = space / 6; // Divide by the number of children * 2 - assertThat(textView1.getLeft() - padding, isEqualAllowingError(space)); - - int spaceInMiddle = space * 2; - assertThat(textView2.getLeft() - textView1.getRight(), isEqualAllowingError(spaceInMiddle)); - assertThat(textView3.getLeft() - textView2.getRight(), isEqualAllowingError(spaceInMiddle)); - assertThat(flexboxLayout.getRight() - textView3.getRight() - padding, - isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_flexStart_flexDirection_column() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.FLEX_START)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexEnd_flexDirection_column() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.FLEX_END); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.FLEX_END)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text3))); - onView(withId(R.id.text1)).check(isCompletelyAbove(withId(R.id.text2))); - } - - @Test - @FlakyTest - public void testJustifyContent_center_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.CENTER); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), is(JustifyContent.CENTER)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getHeight() - textView1.getHeight() - textView2.getHeight() - - textView3.getHeight(); - space = space / 2; - assertThat(textView1.getTop(), isEqualAllowingError(space)); - assertThat(flexboxLayout.getBottom() - textView3.getBottom(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent( - JustifyContent.SPACE_BETWEEN); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_BETWEEN)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getHeight() - textView1.getHeight() - textView2.getHeight() - - textView3.getHeight(); - space = space / 2; - assertThat(textView2.getTop() - textView1.getBottom(), isEqualAllowingError(space)); - assertThat(textView3.getTop() - textView2.getBottom(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_flexDirection_column_withPadding() - throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final int padding = 40; - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent( - JustifyContent.SPACE_BETWEEN); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - flexboxLayout.setPadding(padding, padding, padding, padding); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_BETWEEN)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getHeight() - textView1.getHeight() - textView2.getHeight() - - textView3.getHeight() - padding * 2; - space = space / 2; - assertThat(textView1.getTop(), is(padding)); - assertThat(flexboxLayout.getBottom() - textView3.getBottom(), is(padding)); - assertThat(textView2.getTop() - textView1.getBottom(), isEqualAllowingError(space)); - assertThat(textView3.getTop() - textView2.getBottom(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.SPACE_AROUND); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_AROUND)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getHeight() - textView1.getHeight() - textView2.getHeight() - - textView3.getHeight(); - space = space / 6; // Divide by the number of children * 2 - assertThat(textView1.getTop(), isEqualAllowingError(space)); - int spaceInMiddle = space * 2; - assertThat(textView2.getTop() - textView1.getBottom(), isEqualAllowingError(spaceInMiddle)); - assertThat(textView3.getTop() - textView2.getBottom(), isEqualAllowingError(spaceInMiddle)); - assertThat(flexboxLayout.getBottom() - textView3.getBottom(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_flexDirection_column_withPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final int padding = 40; - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.SPACE_AROUND); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - flexboxLayout.setPadding(padding, padding, padding, padding); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_AROUND)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getHeight() - textView1.getHeight() - textView2.getHeight() - - textView3.getHeight() - padding * 2; - space = space / 6; // Divide by the number of children * 2 - assertThat(textView1.getTop() - padding, isEqualAllowingError(space)); - int spaceInMiddle = space * 2; - assertThat(textView2.getTop() - textView1.getBottom(), isEqualAllowingError(spaceInMiddle)); - assertThat(textView3.getTop() - textView2.getBottom(), isEqualAllowingError(spaceInMiddle)); - assertThat(flexboxLayout.getBottom() - textView3.getBottom() - padding, - isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_including_gone_views() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_justify_content_with_gone, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent(JustifyContent.SPACE_AROUND); - } - } - ); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_AROUND)); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView3.getWidth(); - space = space / 4; // Divide by the number of visible children * 2 - assertThat(textView1.getLeft(), isEqualAllowingError(space)); - int spaceInMiddle = space * 2; - assertThat(textView3.getLeft() - textView1.getRight(), isEqualAllowingError(spaceInMiddle)); - assertThat(flexboxLayout.getRight() - textView3.getRight(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_including_gone_views() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_justify_content_with_gone, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setJustifyContent( - JustifyContent.SPACE_BETWEEN); - } - }); - - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_BETWEEN)); - - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getWidth() - textView1.getWidth() - textView3.getWidth(); - assertThat(textView3.getLeft() - textView1.getRight(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_including_gone_views_direction_column() - throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_justify_content_with_gone, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - flexboxLayout.setJustifyContent(JustifyContent.SPACE_AROUND); - } - }); - - assertThat(flexboxLayout.getFlexDirection(), - is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_AROUND)); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getHeight() - textView1.getHeight() - textView3.getHeight(); - space = space / 4; // Divide by the number of visible children * 2 - assertThat(textView1.getTop(), isEqualAllowingError(space)); - int spaceInMiddle = space * 2; - assertThat(textView3.getTop() - textView1.getBottom(), isEqualAllowingError(spaceInMiddle)); - assertThat(flexboxLayout.getBottom() - textView3.getBottom(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_including_gone_views_direction_column() - throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_justify_content_with_gone, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - flexboxLayout.setJustifyContent( - JustifyContent.SPACE_BETWEEN); - } - }); - - assertThat(flexboxLayout.getFlexDirection(), - is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getJustifyContent(), - is(JustifyContent.SPACE_BETWEEN)); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - int space = flexboxLayout.getHeight() - textView1.getHeight() - textView3.getHeight(); - assertThat(textView3.getTop() - textView1.getBottom(), isEqualAllowingError(space)); - } - - @Test - @FlakyTest - public void testFlexGrow_withExactParentLength() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_grow_test); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - // the third TextView is expanded to the right edge of the FlexboxLayout - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getWidth(), - is(flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth())); - } - - @Test - @FlakyTest - public void testFlexGrow_withExactParentLength_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_grow_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - // the third TextView is expanded to the bottom edge of the FlexboxLayout - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getHeight(), - is(flexboxLayout.getHeight() - textView1.getHeight() - textView2.getHeight())); - } - - @Test - @FlakyTest - public void testFlexGrow_including_view_gone() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_grow_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - TextView textView2 = activity.findViewById(R.id.text2); - textView2.setVisibility(View.GONE); - } - }); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - // the third TextView is expanded to the right edge of the FlexboxLayout - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView2.getVisibility(), is(View.GONE)); - assertThat(textView3.getWidth(), is(flexboxLayout.getWidth() - textView1.getWidth())); - } - - @Test - @FlakyTest - public void testAlignContent_stretch() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.STRETCH)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - // the third TextView is wrapped to the next flex line - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - TextView textView3 = activity.findViewById(R.id.text3); - int flexLineCrossSize = flexboxLayout.getHeight() / 2; - // Two flex line's cross sizes are expanded to the half of the height of the FlexboxLayout. - // The third textView's top should be aligned witht the second flex line. - assertThat(textView3.getTop(), is(flexLineCrossSize)); - } - - @Test - @FlakyTest - public void testAlignContent_flexStart() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.FLEX_START); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_START)); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - // the third TextView is wrapped to the next flex line - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getTop(), is(textView1.getHeight())); - } - - @Test - @FlakyTest - public void testAlignContent_flexEnd() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.FLEX_END); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_END)); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isCompletelyAbove(withId(R.id.text3))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text3))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getBottom(), is(flexboxLayout.getBottom() - textView3.getHeight())); - } - - @Test - @FlakyTest - public void testAlignContent_flexEnd_parentPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.FLEX_END); - flexboxLayout.setPadding(32, 32, 32, 32); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_END)); - onView(withId(R.id.text1)).check(isCompletelyAbove(withId(R.id.text3))); - onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text3))); - - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getBottom(), - is(flexboxLayout.getBottom() - flexboxLayout.getPaddingBottom())); - } - - @Test - @FlakyTest - public void testAlignContent_flexEnd_parentPadding_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.FLEX_END); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - flexboxLayout.setPadding(32, 32, 32, 32); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_END)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text3))); - onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text3))); - - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getRight(), - is(flexboxLayout.getRight() - flexboxLayout.getPaddingRight())); - } - - @Test - @FlakyTest - public void testAlignContent_center() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.CENTER); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.CENTER)); - - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - int spaceAboveAndBottom = flexboxLayout.getHeight() - textView1.getHeight() - textView3 - .getHeight(); - spaceAboveAndBottom /= 2; - - assertThat(textView1.getTop(), isEqualAllowingError(spaceAboveAndBottom)); - assertThat(flexboxLayout.getBottom() - textView3.getBottom(), - isEqualAllowingError(spaceAboveAndBottom)); - assertThat(flexboxLayout.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testAlignContent_spaceBetween() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.SPACE_BETWEEN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.SPACE_BETWEEN)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - assertThat(flexboxLayout.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testAlignContent_spaceBetween_withPadding() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.SPACE_BETWEEN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.SPACE_BETWEEN)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testAlignContent_spaceAround() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.SPACE_AROUND); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.SPACE_AROUND)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - - int spaceAround = flexboxLayout.getHeight() - textView1.getHeight() - textView3.getHeight(); - spaceAround /= 4; // Divide by the number of flex lines * 2 - - assertThat(textView1.getTop(), isEqualAllowingError(spaceAround)); - int spaceInMiddle = textView1.getBottom() + spaceAround * 2; - assertThat(textView3.getTop(), isEqualAllowingError(spaceInMiddle)); - assertThat(flexboxLayout.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testAlignContent_stretch_parentWrapContent() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - ViewGroup.LayoutParams parentLp = flexboxLayout.getLayoutParams(); - parentLp.height = ViewGroup.LayoutParams.WRAP_CONTENT; - flexboxLayout.setLayoutParams(parentLp); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.STRETCH)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - // the third TextView is wrapped to the next flex line - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - // alignContent is only effective if the parent's height/width mode is MeasureSpec.EXACTLY. - // The size of the flex lines don't change even if the alingContent is set to - // ALIGN_CONTENT_STRETCH - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getTop(), is(textView1.getHeight())); - assertThat(flexboxLayout.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testAlignContent_stretch_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.STRETCH)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - // the third TextView is wrapped to the next flex line - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView textView3 = activity.findViewById(R.id.text3); - int flexLineCrossSize = flexboxLayout.getWidth() / 2; - // Two flex line's cross sizes are expanded to the half of the width of the FlexboxLayout. - // The third textView's left should be aligned with the second flex line. - assertThat(textView3.getLeft(), is(flexLineCrossSize)); - } - - @Test - @FlakyTest - public void testAlignContent_flexStart_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.FLEX_START); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_START)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - // the third TextView is wrapped to the next flex line - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getLeft(), is(textView1.getWidth())); - } - - @Test - @FlakyTest - public void testAlignContent_flexEnd_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.FLEX_END); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_END)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text3))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text3))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getRight(), is(flexboxLayout.getRight() - textView3.getWidth())); - } - - @Test - @FlakyTest - public void testAlignContent_center_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.CENTER); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.CENTER)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - int spaceLeftAndRight = flexboxLayout.getWidth() - textView1.getWidth() - - textView3.getWidth(); - spaceLeftAndRight /= 2; - - assertThat(textView1.getLeft(), isEqualAllowingError(spaceLeftAndRight)); - assertThat(textView3.getRight(), - isEqualAllowingError(flexboxLayout.getRight() - spaceLeftAndRight)); - } - - @Test - @FlakyTest - public void testAlignContent_spaceBetween_flexDirection_column() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.SPACE_BETWEEN); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.SPACE_BETWEEN)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testAlignContent_spaceAround_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.SPACE_AROUND); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.SPACE_AROUND)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - - int spaceAround = flexboxLayout.getWidth() - textView1.getWidth() - textView3.getWidth(); - spaceAround /= 4; // Divide by the number of flex lines * 2 - - assertThat(textView1.getLeft(), isEqualAllowingError(spaceAround)); - int spaceInMiddle = textView1.getRight() + spaceAround * 2; - assertThat(textView3.getLeft(), isEqualAllowingError(spaceInMiddle)); - } - - @Test - @FlakyTest - public void testAlignContent_stretch_parentWrapContent_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - ViewGroup.LayoutParams parentLp = flexboxLayout.getLayoutParams(); - parentLp.width = ViewGroup.LayoutParams.WRAP_CONTENT; - flexboxLayout.setLayoutParams(parentLp); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.STRETCH)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - // the third TextView is wrapped to the next flex line - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - // alignContent is only effective if the parent's height/width mode is MeasureSpec.EXACTLY. - // The size of the flex lines don't change even if the alingContent is set to - // ALIGN_CONTENT_STRETCH - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView3.getLeft(), is(textView1.getWidth())); - } - - @Test - @FlakyTest - public void testAlignContent_flexEnd_wrapReverse_contentOverflowed() throws Throwable { - FlexboxLayout flexboxLayout = - createFlexboxLayout(R.layout.activity_align_content_test_overflowed, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.FLEX_END); - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_END)); - onView(withId(R.id.text6)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text5)).check(isCompletelyLeftOf(withId(R.id.text6))); - onView(withId(R.id.text4)).check(isCompletelyBelow(withId(R.id.text6))); - } - - @Test - @FlakyTest - public void testAlignContent_flexStart_wrapReverse_contentOverflowed() throws Throwable { - FlexboxLayout flexboxLayout = - createFlexboxLayout(R.layout.activity_align_content_test_overflowed, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent( - AlignContent.FLEX_START); - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.FLEX_START)); - onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text1))); - } - - @Test - @FlakyTest - public void testAlignContent_spaceBetween_wrapReverse_contentOverflowed() throws Throwable { - FlexboxLayout flexboxLayout = - createFlexboxLayout(R.layout.activity_align_content_test_overflowed, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent( - AlignContent.SPACE_BETWEEN); - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.SPACE_BETWEEN)); - onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text1))); - } - - @Test - @FlakyTest - public void testAlignContent_center_wrapReverse_contentOverflowed() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = - createFlexboxLayout(R.layout.activity_align_content_test_overflowed, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent(AlignContent.CENTER); - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.CENTER)); - TextView textView6 = activity.findViewById(R.id.text6); - TextView textView4 = activity.findViewById(R.id.text4); - TextView textView2 = activity.findViewById(R.id.text2); - - assertThat(textView6.getTop() - flexboxLayout.getTop(), isEqualAllowingError( - (flexboxLayout.getHeight() - textView6.getHeight() - textView4.getHeight() - - textView2.getHeight()) / 2)); - } - - @Test - @FlakyTest - public void testAlignContent_spaceAround_wrapReverse_contentOverflowed() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = - createFlexboxLayout(R.layout.activity_align_content_test_overflowed, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignContent( - AlignContent.SPACE_AROUND); - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.SPACE_AROUND)); - TextView textView6 = activity.findViewById(R.id.text6); - TextView textView4 = activity.findViewById(R.id.text4); - TextView textView2 = activity.findViewById(R.id.text2); - - assertThat(textView6.getTop() - flexboxLayout.getTop(), isEqualAllowingError( - (flexboxLayout.getHeight() - textView6.getHeight() - textView4.getHeight() - - textView2.getHeight()) / 2)); - } - - @Test - @FlakyTest - public void testAlignItems_stretch() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_stretch_test); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.STRETCH)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getHeight(), isEqualAllowingError(flexLineSize)); - assertThat(textView2.getHeight(), isEqualAllowingError(flexLineSize)); - assertThat(textView3.getHeight(), isEqualAllowingError(flexLineSize)); - } - - @Test - @FlakyTest - public void testAlignSelf_stretch() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_align_self_stretch_test); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - // There should be 2 flex lines in the layout with the given layout. - // Only the first TextView's alignSelf is set to ALIGN_SELF_STRETCH - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getHeight(), isEqualAllowingError(flexLineSize)); - assertThat(textView2.getHeight(), not(flexLineSize)); - assertThat(textView3.getHeight(), not(flexLineSize)); - } - - @Test - @FlakyTest - public void testAlignSelf_stretch_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_self_stretch_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - // There should be 2 flex lines in the layout with the given layout. - // Only the first TextView's alignSelf is set to ALIGN_SELF_STRETCH - int flexLineSize = flexboxLayout.getWidth() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getWidth(), isEqualAllowingError(flexLineSize)); - assertThat(textView2.getWidth(), not(flexLineSize)); - assertThat(textView3.getWidth(), not(flexLineSize)); - } - - @Test - @FlakyTest - public void testAlignItems_flexStart() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_START)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getHeight(), not(flexLineSize)); - assertThat(textView2.getHeight(), not(flexLineSize)); - assertThat(textView3.getHeight(), not(flexLineSize)); - assertThat(textView3.getTop(), isEqualAllowingError(flexLineSize)); - } - - @Test - @FlakyTest - public void testAlignItems_flexEnd() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignItems(AlignItems.FLEX_END); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_END)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getHeight(), not(flexLineSize)); - assertThat(textView2.getHeight(), not(flexLineSize)); - assertThat(textView3.getHeight(), not(flexLineSize)); - assertThat(textView1.getBottom(), isEqualAllowingError(flexLineSize)); - assertThat(textView2.getBottom(), isEqualAllowingError(flexLineSize)); - assertThat(textView3.getBottom(), is(flexboxLayout.getBottom())); - } - - @Test - @FlakyTest - public void testAlignItems_flexEnd_parentPadding() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_align_items_parent_padding_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignItems(AlignItems.FLEX_END); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_END)); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - assertThat(textView1.getBottom(), - is(flexboxLayout.getBottom() - flexboxLayout.getPaddingBottom())); - assertThat(textView2.getBottom(), - is(flexboxLayout.getBottom() - flexboxLayout.getPaddingBottom())); - } - - @Test - @FlakyTest - public void testAlignItems_flexEnd_parentPadding_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_align_items_parent_padding_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignItems(AlignItems.FLEX_END); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_END)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - assertThat(textView1.getRight(), - is(flexboxLayout.getRight() - flexboxLayout.getPaddingRight())); - assertThat(textView2.getRight(), - is(flexboxLayout.getRight() - flexboxLayout.getPaddingRight())); - } - - @Test - @FlakyTest - public void testAlignItems_center() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignItems(AlignItems.CENTER); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.CENTER)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - // All TextView's heights are the same. No issues should be found if using the first - // TextView to calculate the space above and below - int spaceAboveAndBelow = (flexLineSize - textView1.getHeight()) / 2; - assertThat(textView1.getHeight(), not(flexLineSize)); - assertThat(textView2.getHeight(), not(flexLineSize)); - assertThat(textView3.getHeight(), not(flexLineSize)); - assertThat(textView1.getTop(), isEqualAllowingError(spaceAboveAndBelow)); - assertThat(textView2.getTop(), isEqualAllowingError(spaceAboveAndBelow)); - assertThat(textView3.getTop(), isEqualAllowingError(flexLineSize + spaceAboveAndBelow)); - } - - @Test - @FlakyTest - public void testAlignItems_flexEnd_wrapReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - flexboxLayout.setAlignItems(AlignItems.FLEX_END); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_END)); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP_REVERSE)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - - assertThat(textView1.getHeight(), not(flexLineSize)); - assertThat(textView2.getHeight(), not(flexLineSize)); - assertThat(textView3.getHeight(), not(flexLineSize)); - assertThat(textView1.getTop(), - isEqualAllowingError(flexboxLayout.getHeight() - flexLineSize)); - assertThat(textView2.getTop(), - isEqualAllowingError(flexboxLayout.getHeight() - flexLineSize)); - assertThat(textView3.getTop(), is(0)); - } - - @Test - @FlakyTest - public void testAlignItems_center_wrapReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - flexboxLayout.setAlignItems(AlignItems.CENTER); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.CENTER)); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP_REVERSE)); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - - // All TextView's heights are the same. No issues should be found if using the first - // TextView to calculate the space above and below - int spaceAboveAndBelow = (flexLineSize - textView1.getHeight()) / 2; - assertThat(textView1.getHeight(), not(flexLineSize)); - assertThat(textView2.getHeight(), not(flexLineSize)); - assertThat(textView3.getHeight(), not(flexLineSize)); - assertThat(textView1.getBottom(), - isEqualAllowingError(flexboxLayout.getHeight() - spaceAboveAndBelow)); - assertThat(textView2.getBottom(), - isEqualAllowingError(flexboxLayout.getHeight() - spaceAboveAndBelow)); - assertThat(textView3.getBottom(), isEqualAllowingError( - flexboxLayout.getHeight() - flexLineSize - spaceAboveAndBelow)); - } - - @Test - @FlakyTest - public void testAlignItems_flexStart_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_START)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getWidth() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getWidth(), not(flexLineSize)); - assertThat(textView2.getWidth(), not(flexLineSize)); - assertThat(textView3.getWidth(), not(flexLineSize)); - assertThat(textView3.getLeft(), isEqualAllowingError(flexLineSize)); - } - - @Test - @FlakyTest - public void testAlignItems_flexEnd_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignItems(AlignItems.FLEX_END); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_END)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getHeight() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getWidth(), not(flexLineSize)); - assertThat(textView2.getWidth(), not(flexLineSize)); - assertThat(textView3.getWidth(), not(flexLineSize)); - assertThat(textView1.getRight(), isEqualAllowingError(flexLineSize)); - assertThat(textView2.getRight(), isEqualAllowingError(flexLineSize)); - assertThat(textView3.getRight(), is(flexboxLayout.getRight())); - } - - @Test - @FlakyTest - public void testAlignItems_center_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setAlignItems(AlignItems.CENTER); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.CENTER)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getWidth() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - // All TextView's widths are the same. No issues should be found if using the first - // TextView to calculate the space left and right - int spaceLeftAndRight = (flexLineSize - textView1.getWidth()) / 2; - assertThat(textView1.getWidth(), not(flexLineSize)); - assertThat(textView2.getWidth(), not(flexLineSize)); - assertThat(textView3.getWidth(), not(flexLineSize)); - assertThat(textView1.getLeft(), isEqualAllowingError(spaceLeftAndRight)); - assertThat(textView2.getLeft(), isEqualAllowingError(spaceLeftAndRight)); - assertThat(textView3.getLeft(), isEqualAllowingError(flexLineSize + spaceLeftAndRight)); - } - - @Test - @FlakyTest - public void testAlignItems_flexEnd_wrapReverse_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - flexboxLayout.setAlignItems(AlignItems.FLEX_END); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.FLEX_END)); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP_REVERSE)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getWidth() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - - assertThat(textView1.getWidth(), not(flexLineSize)); - assertThat(textView2.getWidth(), not(flexLineSize)); - assertThat(textView3.getWidth(), not(flexLineSize)); - assertThat(textView1.getLeft(), - isEqualAllowingError(flexboxLayout.getWidth() - flexLineSize)); - assertThat(textView2.getLeft(), - isEqualAllowingError(flexboxLayout.getWidth() - flexLineSize)); - assertThat(textView3.getLeft(), is(0)); - } - - @Test - @FlakyTest - public void testAlignItems_center_wrapReverse_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - flexboxLayout.setAlignItems(AlignItems.CENTER); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.CENTER)); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP_REVERSE)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - - // There should be 2 flex lines in the layout with the given layout. - int flexLineSize = flexboxLayout.getWidth() / 2; - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - - // All TextView's widths are the same. No issues should be found if using the first - // TextView to calculate the space above and below - int spaceLeftAndRight = (flexLineSize - textView1.getWidth()) / 2; - assertThat(textView1.getWidth(), not(flexLineSize)); - assertThat(textView2.getWidth(), not(flexLineSize)); - assertThat(textView3.getWidth(), not(flexLineSize)); - assertThat(textView1.getRight(), - isEqualAllowingError(flexboxLayout.getWidth() - spaceLeftAndRight)); - assertThat(textView2.getRight(), - isEqualAllowingError(flexboxLayout.getWidth() - spaceLeftAndRight)); - assertThat(textView3.getRight(), - isEqualAllowingError(flexboxLayout.getWidth() - flexLineSize - spaceLeftAndRight)); - } - - @Test - @FlakyTest - public void testAlignItems_baseline() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - createFlexboxLayout(R.layout.activity_align_items_baseline_test); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int topPluBaseline1 = textView1.getTop() + textView1.getBaseline(); - int topPluBaseline2 = textView2.getTop() + textView2.getBaseline(); - int topPluBaseline3 = textView3.getTop() + textView3.getBaseline(); - - assertThat(topPluBaseline1, is(topPluBaseline2)); - assertThat(topPluBaseline2, is(topPluBaseline3)); - } - - @Test - @FlakyTest - public void testAlignItems_baseline_wrapContent() throws Throwable { - // This test verifies the issue that baseline calculation is broken on API level +24 - // https://github.com/google/flexbox-layout/issues/341 - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout layout = - createFlexboxLayout(R.layout.activity_align_items_baseline_wrap_content); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int topPluBaseline1 = textView1.getTop() + textView1.getBaseline(); - int topPluBaseline2 = textView2.getTop() + textView2.getBaseline(); - int topPluBaseline3 = textView3.getTop() + textView3.getBaseline(); - - assertThat(topPluBaseline1, is(topPluBaseline2)); - assertThat(topPluBaseline2, is(topPluBaseline3)); - assertThat(layout.getFlexLines().size(), is(1)); - assertTrue(layout.getFlexLines().get(0).getCrossSize() > textView1.getHeight()); - } - - @Test - @FlakyTest - public void testAlignItems_baseline_wrapReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - createFlexboxLayout(R.layout.activity_align_items_baseline_test, new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.WRAP_REVERSE); - } - }); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int bottomPluBaseline1 = textView1.getBottom() + textView1.getBaseline(); - int bottomPluBaseline2 = textView2.getBottom() + textView2.getBaseline(); - int bottomPluBaseline3 = textView3.getBottom() + textView3.getBaseline(); - - assertThat(bottomPluBaseline1, is(bottomPluBaseline2)); - assertThat(bottomPluBaseline2, is(bottomPluBaseline3)); - } - - @Test - @FlakyTest - public void testFlexDirection_row_reverse() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.ROW_REVERSE); - } - }); - - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - - // The layout direction should be right to left - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFlexDirection_column_reverse() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN_REVERSE); - } - }); - - assertThat(flexboxLayout.getFlexDirection(), - is(FlexDirection.COLUMN_REVERSE)); - onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFlexBasisPercent_wrap() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_flex_basis_percent_test); - - // The text1 length is 50%, the text2 length is 60% and the wrap property is WRAP, - // the text2 should be on the second flex line. - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - FlexboxLayout.LayoutParams lp1 = (FlexboxLayout.LayoutParams) textView1.getLayoutParams(); - FlexboxLayout.LayoutParams lp2 = (FlexboxLayout.LayoutParams) textView2.getLayoutParams(); - assertThat(textView1.getWidth(), - is(Math.round(flexboxLayout.getWidth() * lp1.getFlexBasisPercent()))); - assertThat(textView2.getWidth(), - is(Math.round(flexboxLayout.getWidth() * lp2.getFlexBasisPercent()))); - } - - @Test - @FlakyTest - public void testFlexBasisPercent_nowrap() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_basis_percent_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.NOWRAP); - } - }); - - // The text1 length is 50%, the text2 length is 60% and the text3 has the fixed width, - // but the flex wrap attribute is NOWRAP, and flexShrink attributes for all - // children are the default value (1), three text views are shrank to fit in a single flex - // line. - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.NOWRAP)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int totalWidth = textView1.getWidth() + textView2.getWidth() + textView3.getWidth(); - // Allowing minor different length with the flex container since the sum of the three text - // views width is not always the same as the flex container's main size caused by round - // errors in calculating the percent lengths. - assertThat(flexboxLayout.getWidth(), isEqualAllowingError(totalWidth)); - } - - @Test - @FlakyTest - public void testFlexBasisPercent_wrap_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_basis_percent_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - // The text1 length is 50%, the text2 length is 60% and the wrap property is WRAP, - // the text2 should be on the second flex line. - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - FlexboxLayout.LayoutParams lp1 = (FlexboxLayout.LayoutParams) textView1.getLayoutParams(); - FlexboxLayout.LayoutParams lp2 = (FlexboxLayout.LayoutParams) textView2.getLayoutParams(); - assertThat(textView1.getHeight(), - is(Math.round(flexboxLayout.getHeight() * lp1.getFlexBasisPercent()))); - assertThat(textView2.getHeight(), - is(Math.round(flexboxLayout.getHeight() * lp2.getFlexBasisPercent()))); - } - - @Test - @FlakyTest - public void testFlexBasisPercent_nowrap_flexDirection_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_flex_basis_percent_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.NOWRAP); - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - - // The text1 length is 50%, the text2 length is 60% and the text3 has the fixed height, - // but the flex wrap attribute is NOWRAP, and flexShrink attributes for all - // children are the default value (1), three text views are shrank to fit in a single - // flex line. - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.NOWRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - int totalHeight = textView1.getHeight() + textView2.getHeight() + textView3.getHeight(); - // Allowing minor different length with the flex container since the sum of the three text - // views width is not always the same as the flex container's main size caused by round - // errors in calculating the percent lengths. - assertThat(flexboxLayout.getHeight(), isEqualAllowingError(totalHeight)); - } - - @Test - @FlakyTest - public void testMinWidth_initial_width_less_than_minWidth() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the minWidth attribute works as a minimum constraint - // If the initial view width is less than the value of minWidth. - // The textView1's layout_width is set to wrap_content and its text is "1" apparently - // the initial measured width is less than the value of layout_minWidth (100dp) - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_minwidth_test); - TextView textView1 = activity.findViewById(R.id.text1); - int minWidth = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMinWidth(); - - onView(withId(R.id.text1)).check(hasWidth(minWidth)); - onView(withId(R.id.text2)).check(hasWidth(flexboxLayout.getWidth() - minWidth)); - } - - @Test - @FlakyTest - public void testMinWidth_works_as_lower_bound_shrink_to() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the minWidth attribute works as a lower bound - // when the view would shrink less than the minWidth if the minWidth weren't set - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_minwidth_lower_bound_test); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - TextView textView4 = activity.findViewById(R.id.text4); - int minWidth = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMinWidth(); - - onView(withId(R.id.text1)).check(hasWidth(minWidth)); - assertEquals(flexboxLayout.getWidth(), - textView1.getWidth() + textView2.getWidth() + textView3.getWidth() + textView4 - .getWidth()); - } - - @Test - @FlakyTest - public void testMinHeight_initial_height_less_than_minHeight() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the minHeight attribute works as a minimum constraint - // If the initial view height is less than the value of minHeight. - // The textView1's layout_height is set to wrap_content and its text is "1" apparently - // the initial measured height is less than the value of layout_minHeight (100dp) - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_minheight_test); - TextView textView1 = activity.findViewById(R.id.text1); - int minHeight = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMinHeight(); - - onView(withId(R.id.text1)).check(hasHeight(minHeight)); - onView(withId(R.id.text2)).check(hasHeight(flexboxLayout.getHeight() - minHeight)); - } - - @Test - @FlakyTest - public void testMinHeight_works_as_lower_bound_shrink_to() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the minHeight attribute works as a lower bound - // when the view would shrink less than the minHeight if the minHeight weren't set - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_minheight_lower_bound_test); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - TextView textView4 = activity.findViewById(R.id.text4); - int minHeight = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMinHeight(); - - onView(withId(R.id.text1)).check(hasHeight(minHeight)); - assertEquals(flexboxLayout.getHeight(), - textView1.getHeight() + textView2.getHeight() + textView3.getHeight() - + textView4.getHeight()); - } - - @Test - @FlakyTest - public void testMaxWidth_initial_width_more_than_maxWidth() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the maxWidth attribute works as a maximum constraint - // ff the initial view width is more than the value of maxWidth. - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_maxwidth_test); - TextView textView1 = activity.findViewById(R.id.text1); - int maxWidth = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMaxWidth(); - - onView(withId(R.id.text1)).check(hasWidth(maxWidth)); - onView(withId(R.id.text2)).check(hasWidth(flexboxLayout.getWidth() - maxWidth)); - } - - @Test - @FlakyTest - public void testMaxWidth_works_as_upper_bound_expand_to() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the maxWidth attribute works as a upper bound - // when the view would expand more than the maxWidth if the maxWidth weren't set - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_maxwidth_upper_bound_test); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - int maxWidth = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMaxWidth(); - - onView(withId(R.id.text1)).check(hasWidth(maxWidth)); - assertEquals(flexboxLayout.getWidth(), textView1.getWidth() + textView2.getWidth()); - } - - @Test - @FlakyTest - public void testMaxHeight_initial_height_more_than_maxHeight() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the maxHeight attribute works as a maximum constraint - // ff the initial view height is more than the value of maxHeight. - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_maxheight_test); - TextView textView1 = activity.findViewById(R.id.text1); - int maxHeight = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMaxHeight(); - - onView(withId(R.id.text1)).check(hasHeight(maxHeight)); - onView(withId(R.id.text2)).check(hasHeight(flexboxLayout.getHeight() - maxHeight)); - } - - @Test - @FlakyTest - public void testMaxHeight_works_as_lower_bound_expand_to() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - - // This test case verifies if the maxHeight attribute works as a upper bound - // when the view would expand more than the maxHeight if the maxHeight weren't set - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_maxheight_upper_bound_test); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - int maxHeight = ((FlexboxLayout.LayoutParams) textView1.getLayoutParams()).getMaxHeight(); - - onView(withId(R.id.text1)).check(hasHeight(maxHeight)); - assertEquals(flexboxLayout.getHeight(), textView1.getHeight() + textView2.getHeight()); - } - - @Test - @FlakyTest - public void testView_visibility_gone() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_views_visibility_gone); - - // The text1 and text2's visibility are gone, so the visible view starts from text3 - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text4)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text4)).check(isCompletelyRightOf(withId(R.id.text3))); - onView(withId(R.id.text5)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text5)).check(isCompletelyBelow(withId(R.id.text3))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - TextView textView4 = activity.findViewById(R.id.text4); - TextView textView5 = activity.findViewById(R.id.text5); - assertThat(textView1.getVisibility(), is(View.GONE)); - assertThat(textView2.getVisibility(), is(View.GONE)); - assertThat(textView4.getLeft(), is(textView3.getRight())); - assertThat(textView5.getTop(), is(textView3.getBottom())); - } - - @Test - @FlakyTest - public void testView_visibility_gone_first_item_in_flex_line_horizontal() throws Throwable { - // This test verifies if the FlexboxLayout is visible when the visibility of the first - // flex item in the second flex line (or arbitrary flex lines other than the first flex - // line) - // is set to "gone" - // There was an issue reported for that - // https://github.com/google/flexbox-layout/issues/47 - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_visibility_gone_first_item_in_flex_line_row); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - - assertTrue(flexboxLayout.getHeight() > 0); - assertThat(flexboxLayout.getHeight(), is(textView1.getHeight() + textView3.getHeight())); - } - - @Test - @FlakyTest - public void testView_visibility_gone_first_item_in_flex_line_vertical() throws Throwable { - // This test verifies if the FlexboxLayout is visible when the visibility of the first - // flex item in the second flex line (or arbitrary flex lines other than the first flex - // line) - // is set to "gone" - // There was an issue reported for that - // https://github.com/google/flexbox-layout/issues/47 - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_visibility_gone_first_item_in_flex_line_column); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView3 = activity.findViewById(R.id.text3); - - assertTrue(flexboxLayout.getWidth() > 0); - assertThat(flexboxLayout.getWidth(), is(textView1.getWidth() + textView3.getWidth())); - } - - @Test - @FlakyTest - public void testView_visibility_invisible() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_views_visibility_invisible); - - // The text1 and text2's visibility are invisible, these views take space like visible views - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - - TextView textView1 = activity.findViewById(R.id.text1); - TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(textView1.getVisibility(), is(View.INVISIBLE)); - assertThat(textView2.getVisibility(), is(View.INVISIBLE)); - assertThat(textView3.getTop(), is(textView1.getBottom())); - } - - @Test - @FlakyTest - public void testWrapBefore() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_wrap_before_test); - - // layout_wrapBefore for the text2 and text3 are set to true, the text2 and text3 should - // be the first item for each flex line. - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - TextView textView1 = activity.findViewById(R.id.text1); - final TextView textView2 = activity.findViewById(R.id.text2); - TextView textView3 = activity.findViewById(R.id.text3); - assertThat(flexboxLayout.getHeight(), is(textView1.getHeight() + textView2.getHeight() + - textView3.getHeight())); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - FlexboxLayout.LayoutParams lp2 = (FlexboxLayout.LayoutParams) - textView2.getLayoutParams(); - lp2.setWrapBefore(false); - textView2.setLayoutParams(lp2); - } - }); - - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - assertThat(flexboxLayout.getHeight(), is(textView1.getHeight() + textView3.getHeight())); - } - - @Test - @FlakyTest - public void testWrapBefore_nowrap() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_wrap_before_test, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexWrap(FlexWrap.NOWRAP); - } - }); - - // layout_wrapBefore for the text2 and text3 are set to true, but the flexWrap is set to - // NOWRAP, three text views should not be wrapped. - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.NOWRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testWrap_parentPadding_horizontal() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_parent_padding_horizontal_test); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering - // parent padding, the second TextView should be wrapped - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - assertThat(flexboxLayout.getHeight(), - is(flexboxLayout.getPaddingTop() + flexboxLayout.getPaddingBottom() + - text1.getHeight() + text2.getHeight())); - } - - @Test - @FlakyTest - public void testWrap_parentPadding_vertical() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_parent_padding_vertical_test); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - // The sum of height of TextView1 and TextView2 is not enough for wrapping, but considering - // parent padding, the second TextView should be wrapped - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - assertThat(flexboxLayout.getWidth(), - is(flexboxLayout.getPaddingLeft() + flexboxLayout.getPaddingRight() + - text1.getWidth() + text2.getWidth())); - } - - @Test - @FlakyTest - public void testWrap_childMargin_horizontal() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_child_margin_horizontal_test); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering - // the margin for the TextView2, the second TextView should be wrapped - onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - FlexboxLayout.LayoutParams lp2 = (FlexboxLayout.LayoutParams) text2.getLayoutParams(); - assertThat(flexboxLayout.getHeight(), - is(text1.getHeight() + text2.getHeight() + lp2.topMargin + lp2.bottomMargin)); - } - - @Test - @FlakyTest - public void testFirstItemLarge_horizontal() throws Throwable { - // This test verifies a empty flex line is not added when the first flex item is large - // and judged wrapping is required with the first item. - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_first_item_large_horizontal_test); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.STRETCH)); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.STRETCH)); - // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering - // the margin for the TextView2, the second TextView should be wrapped - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - assertThat(flexboxLayout.getHeight(), - is(text1.getHeight() + text2.getHeight() + text3.getHeight())); - } - - @Test - @FlakyTest - public void testFirstItemLarge_vertical() throws Throwable { - // This test verifies a empty flex line is not added when the first flex item is large - // and judged wrapping is required with the first item. - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_first_item_large_vertical_test); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getAlignItems(), is(AlignItems.STRETCH)); - assertThat(flexboxLayout.getAlignContent(), is(AlignContent.STRETCH)); - // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering - // the margin for the TextView2, the second TextView should be wrapped - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - assertThat(flexboxLayout.getWidth(), - is(text1.getWidth() + text2.getWidth() + text3.getWidth())); - } - - @Test - @FlakyTest - public void testWrap_childMargin_vertical() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_child_margin_vertical_test); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - // The sum of height of TextView1 and TextView2 is not enough for wrapping, but considering - // the margin of the TextView2, the second TextView should be wrapped - onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))); - onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - FlexboxLayout.LayoutParams lp2 = (FlexboxLayout.LayoutParams) text2.getLayoutParams(); - assertThat(flexboxLayout.getWidth(), - is(text1.getWidth() + text2.getWidth() + lp2.leftMargin + lp2.rightMargin)); - } - - @Test - @FlakyTest - public void testEmptyChildren() throws Throwable { - FlexboxLayout flexboxLayout = createFlexboxLayout(R.layout.activity_empty_children); - - assertThat(flexboxLayout.getChildCount(), is(0)); - } - - @Test - @FlakyTest - public void testDivider_directionRow_verticalBeginning() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerVertical(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int widthSumFirstRow = text1.getWidth() + text2.getWidth() + text3.getWidth() + divider - .getIntrinsicWidth(); - assertThat(text3.getRight(), is(widthSumFirstRow)); - assertThat(text1.getLeft(), is(not(flexboxLayout.getLeft()))); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - // The right should be 90 * 3 + 10 (divider) - assertThat(flexLine1.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 280))); - assertThat(flexLine1.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - FlexLine flexLine2 = flexLines.get(1); - // The right should be 140 * 2 + 10 (divider) - assertThat(flexLine2.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 290))); - assertThat(flexLine2.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_verticalMiddle() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_MIDDLE); - } - } - ); - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_MIDDLE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - // Three text views are placed in the first row, thus two vertical middle dividers should - // be placed - int widthSumFirstRow = text1.getWidth() + text2.getWidth() + text3.getWidth() - + divider.getIntrinsicWidth() * 2; - assertThat(text3.getRight(), is(widthSumFirstRow)); - assertThat(text1.getLeft(), is(flexboxLayout.getLeft())); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - // The right should be 90 * 3 + 10 * 2(divider) - assertThat(flexLine1.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 290))); - assertThat(flexLine1.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - FlexLine flexLine2 = flexLines.get(1); - // The right should be 140 * 2 + 10 (divider) - assertThat(flexLine2.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 290))); - assertThat(flexLine2.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_verticalEnd() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_END); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - // Three text views are placed in the first row, thus two vertical middle dividers should - // be placed - int widthSumFirstRow = text1.getWidth() + text2.getWidth() + text3.getWidth() - + divider.getIntrinsicWidth(); - assertThat(text3.getRight() + divider.getIntrinsicWidth(), is(widthSumFirstRow)); - assertThat(text1.getLeft(), is(flexboxLayout.getLeft())); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - // The right should be 90 * 3 + 10 (divider) - assertThat(flexLine1.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 280))); - assertThat(flexLine1.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - FlexLine flexLine2 = flexLines.get(1); - // The right should be 140 * 2 + 10 (divider) - assertThat(flexLine2.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 290))); - assertThat(flexLine2.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_verticalAll() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setShowDividerVertical( - FlexboxLayout.SHOW_DIVIDER_BEGINNING - | FlexboxLayout.SHOW_DIVIDER_MIDDLE - | FlexboxLayout.SHOW_DIVIDER_END); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerVertical(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - // Three text views are placed in the first row, thus two vertical middle dividers should - // be placed - int widthSumFirstRow = text1.getWidth() + text2.getWidth() + text3.getWidth() - + divider.getIntrinsicWidth() * 4; - assertThat(text3.getRight() + divider.getIntrinsicWidth(), is(widthSumFirstRow)); - assertThat(text1.getLeft(), is(not(flexboxLayout.getLeft()))); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - // The right should be 90 * 3 + 10 * 4 (divider) - assertThat(flexLine1.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 310))); - assertThat(flexLine1.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - FlexLine flexLine2 = flexLines.get(1); - // The right should be 140 * 2 + 10 * 3 (divider) - assertThat(flexLine2.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 310))); - assertThat(flexLine2.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_horizontalBeginning() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setDividerDrawableHorizontal(divider); - flexboxLayout.setShowDividerHorizontal( - FlexboxLayout.SHOW_DIVIDER_BEGINNING); - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_NONE); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerHorizontal(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_NONE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - assertNotNull(divider); - int heightSum = text1.getHeight() + text4.getHeight() + divider.getIntrinsicHeight(); - assertThat(text4.getBottom(), is(heightSum)); - assertThat(text1.getTop(), is(not(flexboxLayout.getTop()))); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - // There is a horizontal divider at the beginning. Top and bottom coordinates are shifted - // by the amount of 15 - // The right should be 90 * 3 - assertThat(flexLine1.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(flexLine1.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - FlexLine flexLine2 = flexLines.get(1); - assertThat(flexLine2.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 280))); - assertThat(flexLine2.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_horizontalMiddle() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawableHorizontal(divider); - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_MIDDLE); - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_NONE); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerHorizontal(), is(FlexboxLayout.SHOW_DIVIDER_MIDDLE)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_NONE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSum = text1.getHeight() + text4.getHeight() + divider.getIntrinsicHeight(); - assertThat(text4.getBottom(), is(heightSum)); - assertThat(text1.getTop(), is(flexboxLayout.getTop())); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - // The right should be 90 * 3 - assertThat(flexLine1.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(flexLine1.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // There is a horizontal divider at the middle. Top and bottom coordinates are shifted - // by the amount of 15 - FlexLine flexLine2 = flexLines.get(1); - // The right should be 140 - assertThat(flexLine2.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 280))); - assertThat(flexLine2.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_horizontalEnd() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawableHorizontal(divider); - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_END); - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_NONE); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerHorizontal(), is(FlexboxLayout.SHOW_DIVIDER_END)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_NONE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSum = text1.getHeight() + text4.getHeight() + divider.getIntrinsicHeight(); - assertThat(text4.getBottom() + divider.getIntrinsicHeight(), is(heightSum)); - assertThat(text1.getTop(), is(flexboxLayout.getTop())); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - // The right should be 90 * 3 - assertThat(flexLine1.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(flexLine1.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // There is a horizontal divider at the middle. Top and bottom coordinates are shifted - // by the amount of 15 - FlexLine flexLine2 = flexLines.get(1); - // The right should be 140 - assertThat(flexLine2.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 280))); - assertThat(flexLine2.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_horizontalAll() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawableHorizontal(divider); - flexboxLayout.setShowDividerHorizontal( - FlexboxLayout.SHOW_DIVIDER_BEGINNING - | FlexboxLayout.SHOW_DIVIDER_MIDDLE - | FlexboxLayout.SHOW_DIVIDER_END); - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_NONE); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerHorizontal(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_NONE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSum = text1.getHeight() + text4.getHeight() + divider.getIntrinsicHeight() * 3; - assertThat(text4.getBottom() + divider.getIntrinsicHeight(), is(heightSum)); - assertThat(text1.getTop(), is(not(flexboxLayout.getTop()))); - } - - @Test - @FlakyTest - public void testDivider_directionRow_all_thickDivider() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_row, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable thickDivider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider_thick, - null); - flexboxLayout.setDividerDrawableVertical(thickDivider); - flexboxLayout.setShowDividerVertical( - FlexboxLayout.SHOW_DIVIDER_BEGINNING - | FlexboxLayout.SHOW_DIVIDER_MIDDLE - | FlexboxLayout.SHOW_DIVIDER_END); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(flexboxLayout.getShowDividerVertical(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider_thick, null); - // The sum of three text views and the sum of thick dividers don't fit in one line. - // The last text view should be placed to the next line. - assertNotNull(divider); - int widthSumFirstRow = text1.getWidth() + text2.getWidth() - + divider.getIntrinsicWidth() * 3; - assertThat(text2.getRight() + divider.getIntrinsicWidth(), is(widthSumFirstRow)); - assertThat(text1.getLeft(), is(not(flexboxLayout.getLeft()))); - assertThat(text3.getBottom(), is(text1.getHeight() + text2.getHeight())); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_horizontalBeginning() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerHorizontal(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSumFirstRow = text1.getHeight() + text2.getHeight() + text3.getHeight() + divider - .getIntrinsicHeight(); - assertThat(text3.getBottom(), is(heightSumFirstRow)); - assertThat(text1.getTop(), is(not(flexboxLayout.getTop()))); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - assertThat(flexLine1.getCrossSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 90 * 3 + 15 - assertThat(flexLine1.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 285))); - FlexLine flexLine2 = flexLines.get(1); - assertThat(flexLine2.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 140 * 2 + 15 - assertThat(flexLine2.getMainSize(), isEqualAllowingError(TestUtil.dpToPixel(activity, 295))); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_horizontalMiddle() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_MIDDLE); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerHorizontal(), is(FlexboxLayout.SHOW_DIVIDER_MIDDLE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSumFirstRow = text1.getHeight() + text2.getHeight() + text3.getHeight() + divider - .getIntrinsicHeight() * 2; - assertThat(text3.getBottom(), is(heightSumFirstRow)); - assertThat(text1.getTop(), is(flexboxLayout.getTop())); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - assertThat(flexLine1.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 90 * 3 + 15 * 2 - assertThat(flexLine1.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 300))); - FlexLine flexLine2 = flexLines.get(1); - assertThat(flexLine2.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 140 * 2 + 15 - assertThat(flexLine2.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 295))); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_horizontalEnd() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_END); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerHorizontal(), is(FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSumFirstRow = text1.getHeight() + text2.getHeight() + text3.getHeight() + divider - .getIntrinsicHeight(); - assertThat(text3.getBottom() + divider.getIntrinsicHeight(), is(heightSumFirstRow)); - assertThat(text1.getTop(), is(flexboxLayout.getTop())); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - assertThat(flexLine1.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 90 * 3 + 15 - assertThat(flexLine1.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 285))); - FlexLine flexLine2 = flexLines.get(1); - assertThat(flexLine2.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 140 * 2 + 15 - assertThat(flexLine2.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 295))); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_horizontalAll() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setShowDividerHorizontal( - FlexboxLayout.SHOW_DIVIDER_BEGINNING - | FlexboxLayout.SHOW_DIVIDER_MIDDLE - | FlexboxLayout.SHOW_DIVIDER_END); - } - } - ); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerHorizontal(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSumFirstRow = text1.getHeight() + text2.getHeight() + text3.getHeight() + divider - .getIntrinsicHeight() * 4; - assertThat(text3.getBottom() + divider.getIntrinsicHeight(), is(heightSumFirstRow)); - assertThat(text1.getTop(), is(not(flexboxLayout.getTop()))); - List flexLines = flexboxLayout.getFlexLines(); - assertThat(flexLines.size(), is(2)); - FlexLine flexLine1 = flexLines.get(0); - assertThat(flexLine1.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 90 * 3 + 15 * 4 - assertThat(flexLine1.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 330))); - FlexLine flexLine2 = flexLines.get(1); - assertThat(flexLine2.getCrossSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - // The bottom should be 140 * 2 + 15 * 3 - assertThat(flexLine2.getMainSize(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 325))); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_verticalBeginning() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawableVertical(divider); - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_BEGINNING); - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_NONE); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerVertical(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING)); - assertThat(flexboxLayout.getShowDividerHorizontal(), is(FlexboxLayout.SHOW_DIVIDER_NONE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int widthSum = text1.getWidth() + text4.getWidth() + divider.getIntrinsicWidth(); - assertThat(text4.getRight(), is(widthSum)); - assertThat(text1.getLeft(), is(not(flexboxLayout.getLeft()))); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_verticalMiddle() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawableVertical(divider); - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_MIDDLE); - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_NONE); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_MIDDLE)); - assertThat(flexboxLayout.getShowDividerHorizontal(), is(FlexboxLayout.SHOW_DIVIDER_NONE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int widthSum = text1.getWidth() + text4.getWidth() + divider.getIntrinsicWidth(); - assertThat(text4.getRight(), is(widthSum)); - assertThat(text1.getLeft(), is(flexboxLayout.getLeft())); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_verticalEnd() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawableHorizontal(divider); - flexboxLayout.setShowDividerVertical(FlexboxLayout.SHOW_DIVIDER_END); - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_NONE); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerVertical(), is(FlexboxLayout.SHOW_DIVIDER_END)); - assertThat(flexboxLayout.getShowDividerHorizontal(), is(FlexboxLayout.SHOW_DIVIDER_NONE)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int widthSum = text1.getWidth() + text4.getWidth() + divider.getIntrinsicWidth(); - assertThat(text4.getRight() + divider.getIntrinsicWidth(), is(widthSum)); - assertThat(text1.getLeft(), is(flexboxLayout.getLeft())); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_verticalAll() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawableVertical(divider); - flexboxLayout.setShowDividerVertical( - FlexboxLayout.SHOW_DIVIDER_BEGINNING - | FlexboxLayout.SHOW_DIVIDER_MIDDLE - | FlexboxLayout.SHOW_DIVIDER_END); - flexboxLayout.setShowDividerHorizontal(FlexboxLayout.SHOW_DIVIDER_NONE); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerVertical(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int widthSum = text1.getWidth() + text4.getWidth() + divider.getIntrinsicWidth() * 3; - assertThat(text4.getRight() + divider.getIntrinsicWidth(), is(widthSum)); - assertThat(text1.getLeft(), is(not(flexboxLayout.getLeft()))); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_vertical_horizontal_All() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - flexboxLayout.setDividerDrawable(divider); - flexboxLayout.setShowDivider( - FlexboxLayout.SHOW_DIVIDER_BEGINNING - | FlexboxLayout.SHOW_DIVIDER_MIDDLE - | FlexboxLayout.SHOW_DIVIDER_END); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerVertical(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - assertThat(flexboxLayout.getShowDividerHorizontal(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - TextView text4 = activity.findViewById(R.id.text4); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider, null); - assertNotNull(divider); - int heightSum = text1.getHeight() + text2.getHeight() + text3.getHeight() - + divider.getIntrinsicHeight() * 4; - int widthSum = text1.getWidth() + text4.getWidth() + divider.getIntrinsicWidth() * 3; - assertThat(text3.getBottom() + divider.getIntrinsicHeight(), is(heightSum)); - assertThat(text4.getRight() + divider.getIntrinsicWidth(), is(widthSum)); - assertThat(text1.getLeft(), is(not(flexboxLayout.getLeft()))); - assertThat(text1.getTop(), is(not(flexboxLayout.getTop()))); - } - - @Test - @FlakyTest - public void testDivider_directionColumn_all_thickDivider() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_divider_test_direction_column, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - Drawable thickDivider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider_thick, - null); - flexboxLayout.setDividerDrawableHorizontal(thickDivider); - flexboxLayout.setShowDividerHorizontal( - FlexboxLayout.SHOW_DIVIDER_BEGINNING - | FlexboxLayout.SHOW_DIVIDER_MIDDLE - | FlexboxLayout.SHOW_DIVIDER_END); - } - }); - - assertThat(flexboxLayout.getFlexWrap(), is(FlexWrap.WRAP)); - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayout.getShowDividerHorizontal(), - is(FlexboxLayout.SHOW_DIVIDER_BEGINNING | FlexboxLayout.SHOW_DIVIDER_MIDDLE | - FlexboxLayout.SHOW_DIVIDER_END)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - TextView text3 = activity.findViewById(R.id.text3); - Drawable divider = ResourcesCompat - .getDrawable(activity.getResources(), R.drawable.divider_thick, null); - // The sum of three text views and the sum of thick dividers don't fit in one line. - // The last text view should be placed to the next line. - assertNotNull(divider); - int heightSum = text1.getHeight() + text2.getHeight() - + divider.getIntrinsicHeight() * 3; - assertThat(text2.getBottom() + divider.getIntrinsicHeight(), is(heightSum)); - assertThat(text1.getTop(), is(not(flexboxLayout.getTop()))); - assertThat(text3.getRight(), is(text1.getWidth() + text3.getWidth())); - } - - @Test - @FlakyTest - public void testZeroWidth_wrapContentHeight_positiveFlexGrow() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_zero_width_positive_flexgrow); - - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - // Both text view's layout_width is set to 0dp, layout_height is set to wrap_content and - // layout_flexGrow is set to 1. And the text2 has a longer text than the text1. - // So if the cross size calculation (height) is wrong, the height of two text view do not - // match because text2 is trying to expand vertically. - // This assertion verifies that isn't happening. Finally both text views expand horizontally - // enough to contain their texts in one line. - assertThat(text1.getHeight(), is(text2.getHeight())); - assertThat(text1.getWidth() + text2.getWidth(), - isEqualAllowingError(flexboxLayout.getWidth())); - } - - @Test - @FlakyTest - public void testZeroHeight_wrapContentWidth_positiveFlexGrow() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_zero_height_positive_flexgrow); - - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - TextView text1 = activity.findViewById(R.id.text1); - TextView text2 = activity.findViewById(R.id.text2); - assertThat(text1.getWidth(), is(not(text2.getWidth()))); - assertThat(text1.getHeight() + text2.getHeight(), - isEqualAllowingError(flexboxLayout.getHeight())); - } - - @Test - @FlakyTest - public void testFlexDirection_row_alignItems_center_margin_oneSide() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_direction_row_align_items_center_margin_oneside); - - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.ROW)); - - TextView text1 = activity.findViewById(R.id.text1); - assertThat(text1.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 30))); - assertThat(flexboxLayout.getBottom() - text1.getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - } - - @Test - @FlakyTest - public void testFlexDirection_column_alignItems_center_margin_oneSide() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_direction_column_align_items_center_margin_oneside); - - assertThat(flexboxLayout.getFlexDirection(), is(FlexDirection.COLUMN)); - - TextView text1 = activity.findViewById(R.id.text1); - assertThat(text1.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 30))); - assertThat(flexboxLayout.getRight() - text1.getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - } - - @Test - @FlakyTest - public void testChildBottomMarginIncluded_flexContainerWrapContent_directionRow_flexGrow() - throws Throwable { - // This test is to verify the case where: - // - layout_height is set to wrap_content for the FlexboxLayout - // - Bottom (or top) margin is set to a child - // - The child which the has the bottom (top) margin has the largest height in the - // same flex line (or only a single child exists) - // - The child has a positive layout_flexGrow attribute set - // If these conditions were met, the height of the FlexboxLayout didn't take the bottom - // margin on the child into account - // See https://github.com/google/flexbox-layout/issues/154 - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_content_child_bottom_margin_row_grow); - - // layout_height for text1: 24dp, layout_marginBottom: 12dp - assertThat(flexboxLayout.getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(mActivityRule.getActivity(), 36))); - } - - @Test - @FlakyTest - public void testChildEndMarginIncluded_flexContainerWrapContent_directionColumn_flexGrow() - throws Throwable { - // This test is to verify the case where: - // - layout_width is set to wrap_content for the FlexboxLayout - // - End (or start) margin is set to a child - // - The child which the has the end (start) margin has the largest width in the - // same flex line (or only a single child exists) - // - The child has a positive layout_flexGrow attribute set - // If these conditions were met, the width of the FlexboxLayout didn't take the bottom - // margin on the child into account - // See https://github.com/google/flexbox-layout/issues/154 - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_content_child_bottom_margin_column_grow); - - // layout_width for text1: 24dp, layout_marginEnd: 12dp - assertThat(flexboxLayout.getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(mActivityRule.getActivity(), 36))); - } - - @Test - @FlakyTest - public void testChildBottomMarginIncluded_flexContainerWrapContent_directionRow_flexShrink() - throws Throwable { - // This test is to verify the case where: - // - layout_height is set to wrap_content for the FlexboxLayout - // - flex_wrap is set to nowrap for the FlexboxLayout - // - Bottom (or top) margin is set to a child - // - The child which the has the bottom (top) margin has the largest height in the - // same flex line - // - The child has a positive layout_flexShrink attribute set - // - The sum of children width overflows parent's width (shrink will happen) - // If these conditions were met, the height of the FlexboxLayout didn't take the bottom - // margin on the child into account - // See https://github.com/google/flexbox-layout/issues/154 - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_content_child_bottom_margin_row_shrink); - - // layout_height for text1: 24dp, layout_marginBottom: 12dp - assertThat(flexboxLayout.getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(mActivityRule.getActivity(), 36))); - } - - @Test - @FlakyTest - public void testChildBottomMarginIncluded_flexContainerWrapContent_directionColumn_flexShrink() - throws Throwable { - // This test is to verify the case where: - // - layout_width is set to wrap_content for the FlexboxLayout - // - flex_wrap is set to nowrap for the FlexboxLayout - // - End (or start) margin is set to a child - // - The child which the has the end (start) margin has the largest width in the - // same flex line - // - The child has a positive layout_flexShrink attribute set - // - The sum of children height overflows parent's height (shrink will happen) - // If these conditions were met, the height of the FlexboxLayout didn't take the bottom - // margin on the child into account - // See https://github.com/google/flexbox-layout/issues/154 - FlexboxLayout flexboxLayout = createFlexboxLayout( - R.layout.activity_wrap_content_child_bottom_margin_column_shrink); - - // layout_width for text1: 24dp, layout_marginEnd: 12dp - assertThat(flexboxLayout.getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(mActivityRule.getActivity(), 36))); - } - - @Test - @FlakyTest - public void testChildNeedsRemeasure_row() throws Throwable { - createFlexboxLayout(R.layout.activity_child_needs_remeasure_row); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testChildNeedsRemeasure_column() throws Throwable { - createFlexboxLayout(R.layout.activity_child_needs_remeasure_column); - onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFirstViewGone_firstLineSingleItem_row() throws Throwable { - // This test verifies the case where the first view's visibility is gone and the second - // view is in the next flex line. In that case, the second view's position is misplaced. - // https://github.com/google/flexbox-layout/issues/283 - createFlexboxLayout(R.layout.activity_first_view_gone_first_line_single_item); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFirstViewGone_firstLineSingleItem_column() throws Throwable { - // This test verifies the case where the first view's visibility is gone and the second - // view is in the next flex line. In that case, the second view's position is misplaced. - // https://github.com/google/flexbox-layout/issues/283 - createFlexboxLayout(R.layout.activity_first_view_gone_first_line_single_item, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFirstViewGone_flexGrowSetForRestOfItems_row() throws Throwable { - // This test verifies the case where the first view's visibility is gone and the second - // view and third view have the layout_flexGrow attribute set. In that case, the second - // view's position is misplaced and the third view becomes invisible . - // https://github.com/google/flexbox-layout/issues/303 - createFlexboxLayout(R.layout.activity_first_view_gone_layout_grow_set_for_rest); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFirstViewGone_flexGrowSetForRestOfItems_column() throws Throwable { - // This test verifies the case where the first view's visibility is gone and the second - // view and third view have the layout_flexGrow attribute set. In that case, the second - // view's position is misplaced and the third view becomes invisible . - // https://github.com/google/flexbox-layout/issues/303 - createFlexboxLayout(R.layout.activity_first_view_gone_layout_grow_set_for_rest, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFirstViewGone_flexShrinkSetForRestOfItems_row() throws Throwable { - createFlexboxLayout(R.layout.activity_first_view_gone_layout_shrink_set_for_rest); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - } - - @Test - @FlakyTest - public void testFirstViewGone_flexShrinkSetForRestOfItems_column() throws Throwable { - createFlexboxLayout(R.layout.activity_first_view_gone_layout_shrink_set_for_rest, - new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - flexboxLayout.setFlexDirection(FlexDirection.COLUMN); - } - }); - onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); - onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); - } - - private FlexboxLayout createFlexboxLayout(@LayoutRes final int activityLayoutResId) - throws Throwable { - return createFlexboxLayout(activityLayoutResId, Configuration.EMPTY); - } - - private FlexboxLayout createFlexboxLayout(@LayoutRes final int activityLayoutResId, - final Configuration configuration) throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(activityLayoutResId); - FlexboxLayout flexboxLayout = activity.findViewById(R.id.flexbox_layout); - configuration.apply(flexboxLayout); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - return (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); - } - - private static TextView createTextView(Context context, String text, int order) { - TextView textView = new TextView(context); - textView.setText(text); - FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams( - ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - lp.setOrder(order); - textView.setLayoutParams(lp); - return textView; - } - - private static ViewAssertion hasWidth(final int width) { - return matches(new TypeSafeMatcher() { - @Override - public void describeTo(Description description) { - description.appendText("expected width: " + width); - } - - @Override - protected void describeMismatchSafely(View item, Description mismatchDescription) { - mismatchDescription.appendText("actual width: " + item.getWidth()); - } - - @Override - protected boolean matchesSafely(View item) { - return item.getWidth() == width; - } - }); - } - - private static ViewAssertion hasHeight(final int height) { - return matches(new TypeSafeMatcher() { - @Override - public void describeTo(Description description) { - description.appendText("expected height: " + height); - } - - @Override - protected void describeMismatchSafely(View item, Description mismatchDescription) { - mismatchDescription.appendText("actual height: " + item.getHeight()); - } - - @Override - protected boolean matchesSafely(View item) { - return item.getHeight() == height; - } - }); - } - - private interface Configuration { - - Configuration EMPTY = new Configuration() { - @Override - public void apply(FlexboxLayout flexboxLayout) { - } - }; - - void apply(FlexboxLayout flexboxLayout); - } -} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt new file mode 100644 index 0000000..1ba8c8d --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxAndroidTest.kt @@ -0,0 +1,3841 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import android.content.Context +import android.support.annotation.LayoutRes +import android.support.test.InstrumentationRegistry +import android.support.test.espresso.Espresso.onView +import android.support.test.espresso.ViewAssertion +import android.support.test.espresso.assertion.PositionAssertions.* +import android.support.test.espresso.assertion.ViewAssertions.matches +import android.support.test.espresso.matcher.ViewMatchers.withId +import android.support.test.filters.FlakyTest +import android.support.test.filters.MediumTest +import android.support.test.rule.ActivityTestRule +import android.support.test.runner.AndroidJUnit4 +import android.support.v4.content.res.ResourcesCompat +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import com.google.android.flexbox.* +import com.google.android.flexbox.test.IsEqualAllowingError.Companion.isEqualAllowingError +import junit.framework.Assert.assertEquals +import junit.framework.Assert.assertTrue +import org.hamcrest.Description +import org.hamcrest.TypeSafeMatcher +import org.hamcrest.core.Is.`is` +import org.hamcrest.core.IsNot.not +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertThat +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Integration tests for [FlexboxLayout]. + */ +@RunWith(AndroidJUnit4::class) +@MediumTest +class FlexboxAndroidTest { + + @JvmField + @Rule + var activityRule = ActivityTestRule(FlexboxTestActivity::class.java) + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testLoadFromLayoutXml() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_simple) + + assertNotNull(flexboxLayout) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.CENTER)) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.CENTER)) + assertThat(flexboxLayout.alignItems, `is`(AlignItems.CENTER)) + assertThat(flexboxLayout.childCount, `is`(1)) + + val child = flexboxLayout.getChildAt(0) + val lp = child.layoutParams as FlexboxLayout.LayoutParams + assertThat(lp.order, `is`(2)) + assertThat(lp.flexGrow, `is`(1f)) + assertThat(lp.alignSelf, `is`(AlignItems.STRETCH)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testOrderAttribute_fromLayoutXml() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_order_test) + + assertNotNull(flexboxLayout) + assertThat(flexboxLayout.childCount, `is`(4)) + // order: -1, index 1 + assertThat((flexboxLayout.getReorderedChildAt(0) as TextView).text.toString(), `is`("2")) + // order: 0, index 2 + assertThat((flexboxLayout.getReorderedChildAt(1) as TextView).text.toString(), `is`("3")) + // order: 1, index 3 + assertThat((flexboxLayout.getReorderedChildAt(2) as TextView).text.toString(), `is`("4")) + // order: 2, index 0 + assertThat((flexboxLayout.getReorderedChildAt(3) as TextView).text.toString(), `is`("1")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testOrderAttribute_fromCode() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val fifth = createTextView(activity, "5", 0) + val sixth = createTextView(activity, "6", -10) + flexboxLayout.addView(fifth) + flexboxLayout.addView(sixth) + } + }) + + assertThat(flexboxLayout.childCount, `is`(6)) + // order: -10, index 5 + assertThat((flexboxLayout.getReorderedChildAt(0) as TextView).text.toString(), `is`("6")) + // order: -1, index 1 + assertThat((flexboxLayout.getReorderedChildAt(1) as TextView).text.toString(), `is`("2")) + // order: 0, index 2 + assertThat((flexboxLayout.getReorderedChildAt(2) as TextView).text.toString(), `is`("3")) + // order: 0, index 4 + assertThat((flexboxLayout.getReorderedChildAt(3) as TextView).text.toString(), `is`("5")) + // order: 1, index 3 + assertThat((flexboxLayout.getReorderedChildAt(4) as TextView).text.toString(), `is`("4")) + // order: 2, index 0 + assertThat((flexboxLayout.getReorderedChildAt(5) as TextView).text.toString(), `is`("1")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChangeOrder_fromChildSetLayoutParams() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_order_test) + + assertThat(flexboxLayout.childCount, `is`(4)) + // order: -1, index 1 + assertThat((flexboxLayout.getReorderedChildAt(0) as TextView).text.toString(), `is`("2")) + // order: 0, index 2 + assertThat((flexboxLayout.getReorderedChildAt(1) as TextView).text.toString(), `is`("3")) + // order: 0, index 3 + assertThat((flexboxLayout.getReorderedChildAt(2) as TextView).text.toString(), `is`("4")) + // order: 2, index 0 + assertThat((flexboxLayout.getReorderedChildAt(3) as TextView).text.toString(), `is`("1")) + + // By changing the order and calling the setLayoutParams, the reordered array in the + // FlexboxLayout (mReorderedIndices) will be recreated without adding a new View. + activityRule.runOnUiThread { + val view1 = flexboxLayout.getChildAt(0) + val lp = view1.layoutParams as FlexboxLayout.LayoutParams + lp.order = -3 + view1.layoutParams = lp + } + // order: -3, index 0 + assertThat((flexboxLayout.getReorderedChildAt(3) as TextView).text.toString(), `is`("1")) + // order: -1, index 1 + assertThat((flexboxLayout.getReorderedChildAt(0) as TextView).text.toString(), `is`("2")) + // order: 0, index 2 + assertThat((flexboxLayout.getReorderedChildAt(1) as TextView).text.toString(), `is`("3")) + // order: 1, index 3 + assertThat((flexboxLayout.getReorderedChildAt(2) as TextView).text.toString(), `is`("4")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testOrderAttribute_addViewInMiddle() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val fifth = createTextView(activity, "5", 0) + // Add the new TextView in the middle of the indices + flexboxLayout.addView(fifth, 2) + } + }) + + assertNotNull(flexboxLayout) + assertThat(flexboxLayout.childCount, `is`(5)) + // order: -1, index 1 + assertThat((flexboxLayout.getReorderedChildAt(0) as TextView).text.toString(), `is`("2")) + // order: 0, index 2 + assertThat((flexboxLayout.getReorderedChildAt(1) as TextView).text.toString(), `is`("5")) + // order: 0, index 3 + assertThat((flexboxLayout.getReorderedChildAt(2) as TextView).text.toString(), `is`("3")) + // order: 0, index 4 + assertThat((flexboxLayout.getReorderedChildAt(3) as TextView).text.toString(), `is`("4")) + // order: 2, index 0 + assertThat((flexboxLayout.getReorderedChildAt(4) as TextView).text.toString(), `is`("1")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testOrderAttribute_removeLastView() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.removeViewAt(flexboxLayout.childCount - 1) + } + }) + + assertNotNull(flexboxLayout) + assertThat(flexboxLayout.childCount, `is`(3)) + // order: -1, index 1 + assertThat((flexboxLayout.getReorderedChildAt(0) as TextView).text.toString(), `is`("2")) + // order: 0, index 2 + assertThat((flexboxLayout.getReorderedChildAt(1) as TextView).text.toString(), `is`("3")) + // order: 2, index 0 + assertThat((flexboxLayout.getReorderedChildAt(2) as TextView).text.toString(), `is`("1")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testOrderAttribute_removeViewInMiddle() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_order_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.removeViewAt(2) + } + }) + + assertNotNull(flexboxLayout) + assertThat(flexboxLayout.childCount, `is`(3)) + // order: -1, index 1 + assertThat((flexboxLayout.getReorderedChildAt(0) as TextView).text.toString(), `is`("2")) + // order: 0, index 3 + assertThat((flexboxLayout.getReorderedChildAt(1) as TextView).text.toString(), `is`("4")) + // order: 2, index 0 + assertThat((flexboxLayout.getReorderedChildAt(2) as TextView).text.toString(), `is`("1")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexWrap_wrap() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + // The width of the FlexboxLayout is not enough for placing the three text views. + // The third text view should be placed below the first one + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + val activity = activityRule.activity + assertThat(flexLine1.mainSize, `is`(activity.dpToPixel(320))) + assertThat(flexLine1.crossSize, `is`(activity.dpToPixel(120))) + val flexLine2 = flexLines[1] + assertThat(flexLine2.mainSize, `is`(activity.dpToPixel(160))) + assertThat(flexLine2.crossSize, `is`(activity.dpToPixel(120))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexWrap_nowrap() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.NOWRAP + flexboxLayout.alignItems = AlignItems.FLEX_START + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + // The width of the FlexboxLayout is not enough for placing the three text views. + // But the flexWrap attribute is set to NOWRAP, the third text view is placed + // to the right of the second one and overflowing the parent FlexboxLayout. + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + assertThat(flexboxLayout.flexLines.size, `is`(1)) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(1)) + val flexLine = flexLines[0] + val activity = activityRule.activity + assertThat(flexLine.mainSize, `is`(activity.dpToPixel(480))) + assertThat(flexLine.crossSize, `is`(activity.dpToPixel(300))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexWrap_wrap_reverse() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + // The width of the FlexboxLayout is not enough for placing the three text views. + // There should be two flex lines same as WRAP, but the layout starts from bottom + // to top in FlexWrap.WRAP_REVERSE + onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + assertThat(flexboxLayout.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexWrap_wrap_flexDirection_column() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + // The height of the FlexboxLayout is not enough for placing the three text views. + // The third text view should be placed right of the first one + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + assertThat(flexboxLayout.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexWrap_nowrap_flexDirection_column() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + flexboxLayout.flexWrap = FlexWrap.NOWRAP + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + // The height of the FlexboxLayout is not enough for placing the three text views. + // But the flexWrap attribute is set to NOWRAP, the third text view is placed + // below the second one and overflowing the parent FlexboxLayout. + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + assertThat(flexboxLayout.flexLines.size, `is`(1)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexWrap_wrap_reverse_flexDirection_column() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + // The width of the FlexboxLayout is not enough for placing the three text views. + // There should be two flex lines same as WRAP, but the layout starts from right + // to left in FlexWrap.WRAP_REVERSE + onView(withId(R.id.text3)).check(isCompletelyLeftOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyLeftOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + assertThat(flexboxLayout.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexItem_match_parent() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_item_match_parent) + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + + assertThat(text1.width, `is`(flexboxLayout.width)) + assertThat(text2.width, `is`(flexboxLayout.width)) + assertThat(text3.width, `is`(flexboxLayout.width)) + assertThat(flexboxLayout.height, `is`(text1.height + text2.height + text3.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexItem_match_parent_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_flex_item_match_parent_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + + assertThat(text1.height, `is`(flexboxLayout.height)) + assertThat(text2.height, `is`(flexboxLayout.height)) + assertThat(text3.height, `is`(flexboxLayout.height)) + assertThat(flexboxLayout.width, `is`(text1.width + text2.width + text3.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexboxLayout_wrapContent() { + createFlexboxLayout(R.layout.activity_flexbox_wrap_content) + // The parent FlexboxLayout's layout_width and layout_height are set to wrap_content + // The size of the FlexboxLayout is aligned with three text views. + + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexboxLayout_wrapped_with_ScrollView() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flexbox_wrapped_with_scrollview) + + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + + // The heightMode of the FlexboxLayout is set as MeasureSpec.UNSPECIFIED, the height of the + // layout will be expanded to include the all children views + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + assertThat(flexboxLayout.height, `is`(textView1.height + textView3.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexboxLayout_wrapped_with_HorizontalScrollView() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_flexbox_wrapped_with_horizontalscrollview) + + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + // The widthMode of the FlexboxLayout is set as MeasureSpec.UNSPECIFIED, the width of the + // layout will be expanded to include the all children views + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(flexboxLayout.width, `is`(textView1.width + textView2.width + textView3.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexStart() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.FLEX_START)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexStart_withParentPadding() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_justify_content_with_parent_padding) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.FLEX_START)) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + val text1 = activity.findViewById(R.id.text1) + // Both the parent FrameLayout and the FlexboxLayout have different padding values + // but the text1.getLeft should be the padding value for the FlexboxLayout, not including + // the parent's padding value + assertThat(text1.left, `is`(flexboxLayout.paddingLeft)) + assertThat(text1.top, `is`(flexboxLayout.paddingTop)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexEnd() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.FLEX_END + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.FLEX_END)) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text3))) + onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text2))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexEnd_withParentPadding() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_justify_content_with_parent_padding, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.FLEX_END + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.FLEX_END)) + onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text3))) + onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text2))) + val text3 = activity.findViewById(R.id.text3) + // Both the parent FrameLayout and the FlexboxLayout have different padding values + // but the text3.getRight should be the padding value for the FlexboxLayout, not including + // the parent's padding value + assertThat(flexboxLayout.width - text3.right, `is`(flexboxLayout.paddingRight)) + assertThat(text3.top, `is`(flexboxLayout.paddingTop)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_center() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.CENTER + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.CENTER)) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val space = (flexboxLayout.width - textView1.width - textView2.width - textView3.width) / 2 + assertThat(textView1.left, isEqualAllowingError(space)) + assertThat(flexboxLayout.right - textView3.right, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_center_withParentPadding() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_justify_content_with_parent_padding, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.CENTER + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.CENTER)) + + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.width - textView1.width - textView2.width - + textView3.width - flexboxLayout.paddingLeft - flexboxLayout.paddingRight + space /= 2 + assertThat(textView1.left - flexboxLayout.paddingLeft, isEqualAllowingError(space)) + assertThat(flexboxLayout.width - textView3.right - flexboxLayout.paddingRight, + isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_BETWEEN + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.width - textView1.width - textView2.width - textView3.width + space /= 2 + assertThat(textView2.left - textView1.right, isEqualAllowingError(space)) + assertThat(textView3.left - textView2.right, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_withPadding() { + val activity = activityRule.activity + val padding = 40 + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_BETWEEN + flexboxLayout.setPadding(padding, padding, padding, padding) + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.width - textView1.width - textView2.width - textView3.width - + padding * 2 + space /= 2 + assertThat(textView1.left, `is`(padding)) + assertThat(flexboxLayout.right - textView3.right, `is`(padding)) + assertThat(textView2.left - textView1.right, isEqualAllowingError(space)) + assertThat(textView3.left - textView2.right, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_AROUND + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.width - textView1.width - textView2.width - textView3.width + space /= 6 // Divide by the number of children * 2 + assertTrue(space - 1 <= textView1.left && textView1.left <= space + 1) + val spaceInMiddle = space * 2 + assertThat(textView2.left - textView1.right, isEqualAllowingError(spaceInMiddle)) + assertThat(textView3.left - textView2.right, isEqualAllowingError(spaceInMiddle)) + assertThat(flexboxLayout.right - textView3.right, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_withPadding() { + val activity = activityRule.activity + val padding = 40 + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_AROUND + flexboxLayout.setPadding(padding, padding, padding, padding) + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.width - textView1.width - textView2.width - textView3.width - + padding * 2 + space /= 6 // Divide by the number of children * 2 + assertThat(textView1.left - padding, isEqualAllowingError(space)) + + val spaceInMiddle = space * 2 + assertThat(textView2.left - textView1.right, isEqualAllowingError(spaceInMiddle)) + assertThat(textView3.left - textView2.right, isEqualAllowingError(spaceInMiddle)) + assertThat(flexboxLayout.right - textView3.right - padding, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexStart_flexDirection_column() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.FLEX_START)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexEnd_flexDirection_column() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.FLEX_END + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.FLEX_END)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text3))) + onView(withId(R.id.text1)).check(isCompletelyAbove(withId(R.id.text2))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_center_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.CENTER + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.CENTER)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.height - textView1.height - textView2.height - textView3.height + space /= 2 + assertThat(textView1.top, isEqualAllowingError(space)) + assertThat(flexboxLayout.bottom - textView3.bottom, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_BETWEEN + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.height - textView1.height - textView2.height - textView3.height + space /= 2 + assertThat(textView2.top - textView1.bottom, isEqualAllowingError(space)) + assertThat(textView3.top - textView2.bottom, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_flexDirection_column_withPadding() { + val activity = activityRule.activity + val padding = 40 + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_BETWEEN + flexboxLayout.flexDirection = FlexDirection.COLUMN + flexboxLayout.setPadding(padding, padding, padding, padding) + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.height - textView1.height - textView2.height - textView3.height - padding * 2 + space /= 2 + assertThat(textView1.top, `is`(padding)) + assertThat(flexboxLayout.bottom - textView3.bottom, `is`(padding)) + assertThat(textView2.top - textView1.bottom, isEqualAllowingError(space)) + assertThat(textView3.top - textView2.bottom, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_AROUND + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.height - textView1.height - textView2.height - textView3.height + space /= 6 // Divide by the number of children * 2 + assertThat(textView1.top, isEqualAllowingError(space)) + val spaceInMiddle = space * 2 + assertThat(textView2.top - textView1.bottom, isEqualAllowingError(spaceInMiddle)) + assertThat(textView3.top - textView2.bottom, isEqualAllowingError(spaceInMiddle)) + assertThat(flexboxLayout.bottom - textView3.bottom, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_flexDirection_column_withPadding() { + val activity = activityRule.activity + val padding = 40 + val flexboxLayout = createFlexboxLayout(R.layout.activity_justify_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_AROUND + flexboxLayout.flexDirection = FlexDirection.COLUMN + flexboxLayout.setPadding(padding, padding, padding, padding) + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.height - textView1.height - textView2.height - textView3.height - + padding * 2 + space /= 6 // Divide by the number of children * 2 + assertThat(textView1.top - padding, isEqualAllowingError(space)) + val spaceInMiddle = space * 2 + assertThat(textView2.top - textView1.bottom, isEqualAllowingError(spaceInMiddle)) + assertThat(textView3.top - textView2.bottom, isEqualAllowingError(spaceInMiddle)) + assertThat(flexboxLayout.bottom - textView3.bottom - padding, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_including_gone_views() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_justify_content_with_gone, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_AROUND + } + } + ) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.width - textView1.width - textView3.width + space /= 4 // Divide by the number of visible children * 2 + assertThat(textView1.left, isEqualAllowingError(space)) + val spaceInMiddle = space * 2 + assertThat(textView3.left - textView1.right, isEqualAllowingError(spaceInMiddle)) + assertThat(flexboxLayout.right - textView3.right, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_including_gone_views() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_justify_content_with_gone, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.justifyContent = JustifyContent.SPACE_BETWEEN + } + }) + + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + val space = flexboxLayout.width - textView1.width - textView3.width + assertThat(textView3.left - textView1.right, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_including_gone_views_direction_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_justify_content_with_gone, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + flexboxLayout.justifyContent = JustifyContent.SPACE_AROUND + } + }) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + var space = flexboxLayout.height - textView1.height - textView3.height + space /= 4 // Divide by the number of visible children * 2 + assertThat(textView1.top, isEqualAllowingError(space)) + val spaceInMiddle = space * 2 + assertThat(textView3.top - textView1.bottom, isEqualAllowingError(spaceInMiddle)) + assertThat(flexboxLayout.bottom - textView3.bottom, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_including_gone_views_direction_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_justify_content_with_gone, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + flexboxLayout.justifyContent = JustifyContent.SPACE_BETWEEN + } + }) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + val space = flexboxLayout.height - textView1.height - textView3.height + assertThat(textView3.top - textView1.bottom, isEqualAllowingError(space)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexGrow_withExactParentLength() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_grow_test) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + // the third TextView is expanded to the right edge of the FlexboxLayout + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.width, `is`(flexboxLayout.width - textView1.width - textView2.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexGrow_withExactParentLength_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_grow_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + // the third TextView is expanded to the bottom edge of the FlexboxLayout + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.height, `is`(flexboxLayout.height - textView1.height - textView2.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexGrow_including_view_gone() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_grow_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val textView2 = activity.findViewById(R.id.text2) + textView2.visibility = View.GONE + } + }) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + // the third TextView is expanded to the right edge of the FlexboxLayout + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView2.visibility, `is`(View.GONE)) + assertThat(textView3.width, `is`(flexboxLayout.width - textView1.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_stretch() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + // the third TextView is wrapped to the next flex line + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + val textView3 = activity.findViewById(R.id.text3) + val flexLineCrossSize = flexboxLayout.height / 2 + // Two flex line's cross sizes are expanded to the half of the height of the FlexboxLayout. + // The third textView's top should be aligned width the second flex line. + assertThat(textView3.top, `is`(flexLineCrossSize)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexStart() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_START + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_START)) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + // the third TextView is wrapped to the next flex line + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.top, `is`(textView1.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexEnd() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_END + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_END)) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isCompletelyAbove(withId(R.id.text3))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text3))) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.bottom, `is`(flexboxLayout.bottom - textView3.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexEnd_parentPadding() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_END + flexboxLayout.setPadding(32, 32, 32, 32) + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_END)) + onView(withId(R.id.text1)).check(isCompletelyAbove(withId(R.id.text3))) + onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text3))) + + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.bottom, `is`(flexboxLayout.bottom - flexboxLayout.paddingBottom)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexEnd_parentPadding_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_END + flexboxLayout.flexDirection = FlexDirection.COLUMN + flexboxLayout.setPadding(32, 32, 32, 32) + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_END)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text3))) + onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text3))) + + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.right, `is`(flexboxLayout.right - flexboxLayout.paddingRight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_center() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.CENTER + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.CENTER)) + + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + var spaceAboveAndBottom = flexboxLayout.height - textView1.height - textView3.height + spaceAboveAndBottom /= 2 + + assertThat(textView1.top, isEqualAllowingError(spaceAboveAndBottom)) + assertThat(flexboxLayout.bottom - textView3.bottom, isEqualAllowingError(spaceAboveAndBottom)) + assertThat(flexboxLayout.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_spaceBetween() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.SPACE_BETWEEN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_BETWEEN)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + assertThat(flexboxLayout.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_spaceBetween_withPadding() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.SPACE_BETWEEN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_BETWEEN)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_spaceAround() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.SPACE_AROUND + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_AROUND)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + + var spaceAround = flexboxLayout.height - textView1.height - textView3.height + spaceAround /= 4 // Divide by the number of flex lines * 2 + + assertThat(textView1.top, isEqualAllowingError(spaceAround)) + val spaceInMiddle = textView1.bottom + spaceAround * 2 + assertThat(textView3.top, isEqualAllowingError(spaceInMiddle)) + assertThat(flexboxLayout.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_stretch_parentWrapContent() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val parentLp = flexboxLayout.layoutParams + parentLp.height = ViewGroup.LayoutParams.WRAP_CONTENT + flexboxLayout.layoutParams = parentLp + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + // the third TextView is wrapped to the next flex line + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + // alignContent is only effective if the parent's height/width mode is MeasureSpec.EXACTLY. + // The size of the flex lines don't change even if the alignContent is set to + // ALIGN_CONTENT_STRETCH + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.top, `is`(textView1.height)) + assertThat(flexboxLayout.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_stretch_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + // the third TextView is wrapped to the next flex line + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val textView3 = activity.findViewById(R.id.text3) + val flexLineCrossSize = flexboxLayout.width / 2 + // Two flex line's cross sizes are expanded to the half of the width of the FlexboxLayout. + // The third textView's left should be aligned with the second flex line. + assertThat(textView3.left, `is`(flexLineCrossSize)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexStart_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_START + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_START)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + // the third TextView is wrapped to the next flex line + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.left, `is`(textView1.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexEnd_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_END + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_END)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isCompletelyLeftOf(withId(R.id.text3))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text3))) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.right, `is`(flexboxLayout.right - textView3.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_center_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.CENTER + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.CENTER)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + val spaceLeftAndRight = (flexboxLayout.width - textView1.width - textView3.width) / 2 + + assertThat(textView1.left, isEqualAllowingError(spaceLeftAndRight)) + assertThat(textView3.right, isEqualAllowingError(flexboxLayout.right - spaceLeftAndRight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_spaceBetween_flexDirection_column() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.SPACE_BETWEEN + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_BETWEEN)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_spaceAround_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.SPACE_AROUND + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_AROUND)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + + var spaceAround = flexboxLayout.width - textView1.width - textView3.width + spaceAround /= 4 // Divide by the number of flex lines * 2 + + assertThat(textView1.left, isEqualAllowingError(spaceAround)) + val spaceInMiddle = textView1.right + spaceAround * 2 + assertThat(textView3.left, isEqualAllowingError(spaceInMiddle)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_stretch_parentWrapContent_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val parentLp = flexboxLayout.layoutParams + parentLp.width = ViewGroup.LayoutParams.WRAP_CONTENT + flexboxLayout.layoutParams = parentLp + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + // the third TextView is wrapped to the next flex line + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + // alignContent is only effective if the parent's height/width mode is MeasureSpec.EXACTLY. + // The size of the flex lines don't change even if the alignContent is set to + // ALIGN_CONTENT_STRETCH + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView3.left, `is`(textView1.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexEnd_wrapReverse_contentOverflowed() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test_overflowed, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_END + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_END)) + onView(withId(R.id.text6)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text5)).check(isCompletelyLeftOf(withId(R.id.text6))) + onView(withId(R.id.text4)).check(isCompletelyBelow(withId(R.id.text6))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_flexStart_wrapReverse_contentOverflowed() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test_overflowed, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.FLEX_START + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.FLEX_START)) + onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text1))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_spaceBetween_wrapReverse_contentOverflowed() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test_overflowed, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.SPACE_BETWEEN + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_BETWEEN)) + onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyAbove(withId(R.id.text1))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_center_wrapReverse_contentOverflowed() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test_overflowed, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.CENTER + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.CENTER)) + val textView6 = activity.findViewById(R.id.text6) + val textView4 = activity.findViewById(R.id.text4) + val textView2 = activity.findViewById(R.id.text2) + + assertThat(textView6.top - flexboxLayout.top, isEqualAllowingError( + (flexboxLayout.height - textView6.height - textView4.height - textView2.height) / 2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignContent_spaceAround_wrapReverse_contentOverflowed() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_content_test_overflowed, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignContent = AlignContent.SPACE_AROUND + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.SPACE_AROUND)) + val textView6 = activity.findViewById(R.id.text6) + val textView4 = activity.findViewById(R.id.text4) + val textView2 = activity.findViewById(R.id.text2) + + assertThat(textView6.top - flexboxLayout.top, isEqualAllowingError( + (flexboxLayout.height - textView6.height - textView4.height - textView2.height) / 2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_stretch() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_stretch_test) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.STRETCH)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.height, isEqualAllowingError(flexLineSize)) + assertThat(textView2.height, isEqualAllowingError(flexLineSize)) + assertThat(textView3.height, isEqualAllowingError(flexLineSize)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignSelf_stretch() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_align_self_stretch_test) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + // There should be 2 flex lines in the layout with the given layout. + // Only the first TextView's alignSelf is set to ALIGN_SELF_STRETCH + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.height, isEqualAllowingError(flexLineSize)) + assertThat(textView2.height, not(flexLineSize)) + assertThat(textView3.height, not(flexLineSize)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignSelf_stretch_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_self_stretch_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + // There should be 2 flex lines in the layout with the given layout. + // Only the first TextView's alignSelf is set to ALIGN_SELF_STRETCH + val flexLineSize = flexboxLayout.width / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.width, isEqualAllowingError(flexLineSize)) + assertThat(textView2.width, not(flexLineSize)) + assertThat(textView3.width, not(flexLineSize)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexStart() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_START)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.height, not(flexLineSize)) + assertThat(textView2.height, not(flexLineSize)) + assertThat(textView3.height, not(flexLineSize)) + assertThat(textView3.top, isEqualAllowingError(flexLineSize)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexEnd() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignItems = AlignItems.FLEX_END + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.height, not(flexLineSize)) + assertThat(textView2.height, not(flexLineSize)) + assertThat(textView3.height, not(flexLineSize)) + assertThat(textView1.bottom, isEqualAllowingError(flexLineSize)) + assertThat(textView2.bottom, isEqualAllowingError(flexLineSize)) + assertThat(textView3.bottom, `is`(flexboxLayout.bottom)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexEnd_parentPadding() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_align_items_parent_padding_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignItems = AlignItems.FLEX_END + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + assertThat(textView1.bottom, `is`(flexboxLayout.bottom - flexboxLayout.paddingBottom)) + assertThat(textView2.bottom, `is`(flexboxLayout.bottom - flexboxLayout.paddingBottom)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexEnd_parentPadding_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_align_items_parent_padding_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignItems = AlignItems.FLEX_END + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + assertThat(textView1.right, `is`(flexboxLayout.right - flexboxLayout.paddingRight)) + assertThat(textView2.right, `is`(flexboxLayout.right - flexboxLayout.paddingRight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_center() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignItems = AlignItems.CENTER + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.CENTER)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + // All TextView's heights are the same. No issues should be found if using the first + // TextView to calculate the space above and below + val spaceAboveAndBelow = (flexLineSize - textView1.height) / 2 + assertThat(textView1.height, not(flexLineSize)) + assertThat(textView2.height, not(flexLineSize)) + assertThat(textView3.height, not(flexLineSize)) + assertThat(textView1.top, isEqualAllowingError(spaceAboveAndBelow)) + assertThat(textView2.top, isEqualAllowingError(spaceAboveAndBelow)) + assertThat(textView3.top, isEqualAllowingError(flexLineSize + spaceAboveAndBelow)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexEnd_wrapReverse() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + flexboxLayout.alignItems = AlignItems.FLEX_END + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + + assertThat(textView1.height, not(flexLineSize)) + assertThat(textView2.height, not(flexLineSize)) + assertThat(textView3.height, not(flexLineSize)) + assertThat(textView1.top, isEqualAllowingError(flexboxLayout.height - flexLineSize)) + assertThat(textView2.top, isEqualAllowingError(flexboxLayout.height - flexLineSize)) + assertThat(textView3.top, `is`(0)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_center_wrapReverse() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + flexboxLayout.alignItems = AlignItems.CENTER + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.CENTER)) + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + + // All TextView's heights are the same. No issues should be found if using the first + // TextView to calculate the space above and below + val spaceAboveAndBelow = (flexLineSize - textView1.height) / 2 + assertThat(textView1.height, not(flexLineSize)) + assertThat(textView2.height, not(flexLineSize)) + assertThat(textView3.height, not(flexLineSize)) + assertThat(textView1.bottom, isEqualAllowingError(flexboxLayout.height - spaceAboveAndBelow)) + assertThat(textView2.bottom, isEqualAllowingError(flexboxLayout.height - spaceAboveAndBelow)) + assertThat(textView3.bottom, + isEqualAllowingError(flexboxLayout.height - flexLineSize - spaceAboveAndBelow)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexStart_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_START)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.width / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.width, not(flexLineSize)) + assertThat(textView2.width, not(flexLineSize)) + assertThat(textView3.width, not(flexLineSize)) + assertThat(textView3.left, isEqualAllowingError(flexLineSize)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexEnd_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignItems = AlignItems.FLEX_END + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.height / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.width, not(flexLineSize)) + assertThat(textView2.width, not(flexLineSize)) + assertThat(textView3.width, not(flexLineSize)) + assertThat(textView1.right, isEqualAllowingError(flexLineSize)) + assertThat(textView2.right, isEqualAllowingError(flexLineSize)) + assertThat(textView3.right, `is`(flexboxLayout.right)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_center_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.alignItems = AlignItems.CENTER + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.CENTER)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.width / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + // All TextView's widths are the same. No issues should be found if using the first + // TextView to calculate the space left and right + val spaceLeftAndRight = (flexLineSize - textView1.width) / 2 + assertThat(textView1.width, not(flexLineSize)) + assertThat(textView2.width, not(flexLineSize)) + assertThat(textView3.width, not(flexLineSize)) + assertThat(textView1.left, isEqualAllowingError(spaceLeftAndRight)) + assertThat(textView2.left, isEqualAllowingError(spaceLeftAndRight)) + assertThat(textView3.left, isEqualAllowingError(flexLineSize + spaceLeftAndRight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_flexEnd_wrapReverse_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + flexboxLayout.alignItems = AlignItems.FLEX_END + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.FLEX_END)) + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.width / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + + assertThat(textView1.width, not(flexLineSize)) + assertThat(textView2.width, not(flexLineSize)) + assertThat(textView3.width, not(flexLineSize)) + assertThat(textView1.left, isEqualAllowingError(flexboxLayout.width - flexLineSize)) + assertThat(textView2.left, isEqualAllowingError(flexboxLayout.width - flexLineSize)) + assertThat(textView3.left, `is`(0)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_center_wrapReverse_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_align_items_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + flexboxLayout.alignItems = AlignItems.CENTER + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + assertThat(flexboxLayout.alignItems, `is`(AlignItems.CENTER)) + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP_REVERSE)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + + // There should be 2 flex lines in the layout with the given layout. + val flexLineSize = flexboxLayout.width / 2 + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + + // All TextView's widths are the same. No issues should be found if using the first + // TextView to calculate the space above and below + val spaceLeftAndRight = (flexLineSize - textView1.width) / 2 + assertThat(textView1.width, not(flexLineSize)) + assertThat(textView2.width, not(flexLineSize)) + assertThat(textView3.width, not(flexLineSize)) + assertThat(textView1.right, isEqualAllowingError(flexboxLayout.width - spaceLeftAndRight)) + assertThat(textView2.right, isEqualAllowingError(flexboxLayout.width - spaceLeftAndRight)) + assertThat(textView3.right, + isEqualAllowingError(flexboxLayout.width - flexLineSize - spaceLeftAndRight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_baseline() { + val activity = activityRule.activity + createFlexboxLayout(R.layout.activity_align_items_baseline_test) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val topPluBaseline1 = textView1.top + textView1.baseline + val topPluBaseline2 = textView2.top + textView2.baseline + val topPluBaseline3 = textView3.top + textView3.baseline + + assertThat(topPluBaseline1, `is`(topPluBaseline2)) + assertThat(topPluBaseline2, `is`(topPluBaseline3)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_baseline_wrapContent() { + // This test verifies the issue that baseline calculation is broken on API level +24 + // https://github.com/google/flexbox-layout/issues/341 + val activity = activityRule.activity + val layout = createFlexboxLayout(R.layout.activity_align_items_baseline_wrap_content) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val topPluBaseline1 = textView1.top + textView1.baseline + val topPluBaseline2 = textView2.top + textView2.baseline + val topPluBaseline3 = textView3.top + textView3.baseline + + assertThat(topPluBaseline1, `is`(topPluBaseline2)) + assertThat(topPluBaseline2, `is`(topPluBaseline3)) + assertThat(layout.flexLines.size, `is`(1)) + assertTrue(layout.flexLines[0].crossSize > textView1.height) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_baseline_wrapReverse() { + val activity = activityRule.activity + createFlexboxLayout(R.layout.activity_align_items_baseline_test, object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.WRAP_REVERSE + } + }) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val bottomPluBaseline1 = textView1.bottom + textView1.baseline + val bottomPluBaseline2 = textView2.bottom + textView2.baseline + val bottomPluBaseline3 = textView3.bottom + textView3.baseline + + assertThat(bottomPluBaseline1, `is`(bottomPluBaseline2)) + assertThat(bottomPluBaseline2, `is`(bottomPluBaseline3)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexDirection_row_reverse() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.ROW_REVERSE + } + }) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + + // The layout direction should be right to left + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyLeftOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexDirection_column_reverse() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_wrap_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN_REVERSE + } + }) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN_REVERSE)) + onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyAbove(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexBasisPercent_wrap() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_flex_basis_percent_test) + + // The text1 length is 50%, the text2 length is 60% and the wrap property is WRAP, + // the text2 should be on the second flex line. + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val lp1 = textView1.layoutParams as FlexboxLayout.LayoutParams + val lp2 = textView2.layoutParams as FlexboxLayout.LayoutParams + assertThat(textView1.width, `is`(Math.round(flexboxLayout.width * lp1.flexBasisPercent))) + assertThat(textView2.width, `is`(Math.round(flexboxLayout.width * lp2.flexBasisPercent))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexBasisPercent_nowrap() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_basis_percent_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.NOWRAP + } + }) + + // The text1 length is 50%, the text2 length is 60% and the text3 has the fixed width, + // but the flex wrap attribute is NOWRAP, and flexShrink attributes for all + // children are the default value (1), three text views are shrank to fit in a single flex + // line. + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val totalWidth = textView1.width + textView2.width + textView3.width + // Allowing minor different length with the flex container since the sum of the three text + // views width is not always the same as the flex container's main size caused by round + // errors in calculating the percent lengths. + assertThat(flexboxLayout.width, isEqualAllowingError(totalWidth)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexBasisPercent_wrap_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_basis_percent_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + // The text1 length is 50%, the text2 length is 60% and the wrap property is WRAP, + // the text2 should be on the second flex line. + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val lp1 = textView1.layoutParams as FlexboxLayout.LayoutParams + val lp2 = textView2.layoutParams as FlexboxLayout.LayoutParams + assertThat(textView1.height, `is`(Math.round(flexboxLayout.height * lp1.flexBasisPercent))) + assertThat(textView2.height, `is`(Math.round(flexboxLayout.height * lp2.flexBasisPercent))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexBasisPercent_nowrap_flexDirection_column() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_flex_basis_percent_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.NOWRAP + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + + // The text1 length is 50%, the text2 length is 60% and the text3 has the fixed height, + // but the flex wrap attribute is NOWRAP, and flexShrink attributes for all + // children are the default value (1), three text views are shrank to fit in a single + // flex line. + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val totalHeight = textView1.height + textView2.height + textView3.height + // Allowing minor different length with the flex container since the sum of the three text + // views width is not always the same as the flex container's main size caused by round + // errors in calculating the percent lengths. + assertThat(flexboxLayout.height, isEqualAllowingError(totalHeight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMinWidth_initial_width_less_than_minWidth() { + val activity = activityRule.activity + + // This test case verifies if the minWidth attribute works as a minimum constraint + // If the initial view width is less than the value of minWidth. + // The textView1's layout_width is set to wrap_content and its text is "1" apparently + // the initial measured width is less than the value of layout_minWidth (100dp) + val flexboxLayout = createFlexboxLayout(R.layout.activity_minwidth_test) + val textView1 = activity.findViewById(R.id.text1) + val minWidth = (textView1.layoutParams as FlexboxLayout.LayoutParams).minWidth + + onView(withId(R.id.text1)).check(hasWidth(minWidth)) + onView(withId(R.id.text2)).check(hasWidth(flexboxLayout.width - minWidth)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMinWidth_works_as_lower_bound_shrink_to() { + val activity = activityRule.activity + + // This test case verifies if the minWidth attribute works as a lower bound + // when the view would shrink less than the minWidth if the minWidth weren't set + val flexboxLayout = createFlexboxLayout(R.layout.activity_minwidth_lower_bound_test) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val textView4 = activity.findViewById(R.id.text4) + val minWidth = (textView1.layoutParams as FlexboxLayout.LayoutParams).minWidth + + onView(withId(R.id.text1)).check(hasWidth(minWidth)) + assertEquals(flexboxLayout.width, textView1.width + textView2.width + textView3.width + + textView4.width) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMinHeight_initial_height_less_than_minHeight() { + val activity = activityRule.activity + + // This test case verifies if the minHeight attribute works as a minimum constraint + // If the initial view height is less than the value of minHeight. + // The textView1's layout_height is set to wrap_content and its text is "1" apparently + // the initial measured height is less than the value of layout_minHeight (100dp) + val flexboxLayout = createFlexboxLayout(R.layout.activity_minheight_test) + val textView1 = activity.findViewById(R.id.text1) + val minHeight = (textView1.layoutParams as FlexboxLayout.LayoutParams).minHeight + + onView(withId(R.id.text1)).check(hasHeight(minHeight)) + onView(withId(R.id.text2)).check(hasHeight(flexboxLayout.height - minHeight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMinHeight_works_as_lower_bound_shrink_to() { + val activity = activityRule.activity + + // This test case verifies if the minHeight attribute works as a lower bound + // when the view would shrink less than the minHeight if the minHeight weren't set + val flexboxLayout = createFlexboxLayout(R.layout.activity_minheight_lower_bound_test) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val textView4 = activity.findViewById(R.id.text4) + val minHeight = (textView1.layoutParams as FlexboxLayout.LayoutParams).minHeight + + onView(withId(R.id.text1)).check(hasHeight(minHeight)) + assertEquals(flexboxLayout.height, textView1.height + textView2.height + textView3.height + + textView4.height) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMaxWidth_initial_width_more_than_maxWidth() { + val activity = activityRule.activity + + // This test case verifies if the maxWidth attribute works as a maximum constraint + // ff the initial view width is more than the value of maxWidth. + val flexboxLayout = createFlexboxLayout(R.layout.activity_maxwidth_test) + val textView1 = activity.findViewById(R.id.text1) + val maxWidth = (textView1.layoutParams as FlexboxLayout.LayoutParams).maxWidth + + onView(withId(R.id.text1)).check(hasWidth(maxWidth)) + onView(withId(R.id.text2)).check(hasWidth(flexboxLayout.width - maxWidth)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMaxWidth_works_as_upper_bound_expand_to() { + val activity = activityRule.activity + + // This test case verifies if the maxWidth attribute works as a upper bound + // when the view would expand more than the maxWidth if the maxWidth weren't set + val flexboxLayout = createFlexboxLayout(R.layout.activity_maxwidth_upper_bound_test) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val maxWidth = (textView1.layoutParams as FlexboxLayout.LayoutParams).maxWidth + + onView(withId(R.id.text1)).check(hasWidth(maxWidth)) + assertEquals(flexboxLayout.width, textView1.width + textView2.width) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMaxHeight_initial_height_more_than_maxHeight() { + val activity = activityRule.activity + + // This test case verifies if the maxHeight attribute works as a maximum constraint + // ff the initial view height is more than the value of maxHeight. + val flexboxLayout = createFlexboxLayout(R.layout.activity_maxheight_test) + val textView1 = activity.findViewById(R.id.text1) + val maxHeight = (textView1.layoutParams as FlexboxLayout.LayoutParams).maxHeight + + onView(withId(R.id.text1)).check(hasHeight(maxHeight)) + onView(withId(R.id.text2)).check(hasHeight(flexboxLayout.height - maxHeight)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testMaxHeight_works_as_lower_bound_expand_to() { + val activity = activityRule.activity + + // This test case verifies if the maxHeight attribute works as a upper bound + // when the view would expand more than the maxHeight if the maxHeight weren't set + val flexboxLayout = createFlexboxLayout(R.layout.activity_maxheight_upper_bound_test) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val maxHeight = (textView1.layoutParams as FlexboxLayout.LayoutParams).maxHeight + + onView(withId(R.id.text1)).check(hasHeight(maxHeight)) + assertEquals(flexboxLayout.height, textView1.height + textView2.height) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testView_visibility_gone() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_views_visibility_gone) + + // The text1 and text2's visibility are gone, so the visible view starts from text3 + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text4)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text4)).check(isCompletelyRightOf(withId(R.id.text3))) + onView(withId(R.id.text5)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text5)).check(isCompletelyBelow(withId(R.id.text3))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + val textView4 = activity.findViewById(R.id.text4) + val textView5 = activity.findViewById(R.id.text5) + assertThat(textView1.visibility, `is`(View.GONE)) + assertThat(textView2.visibility, `is`(View.GONE)) + assertThat(textView4.left, `is`(textView3.right)) + assertThat(textView5.top, `is`(textView3.bottom)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testView_visibility_gone_first_item_in_flex_line_horizontal() { + // This test verifies if the FlexboxLayout is visible when the visibility of the first + // flex item in the second flex line (or arbitrary flex lines other than the first flex + // line) is set to "gone" + // There was an issue reported for that + // https://github.com/google/flexbox-layout/issues/47 + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_visibility_gone_first_item_in_flex_line_row) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + + assertTrue(flexboxLayout.height > 0) + assertThat(flexboxLayout.height, `is`(textView1.height + textView3.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testView_visibility_gone_first_item_in_flex_line_vertical() { + // This test verifies if the FlexboxLayout is visible when the visibility of the first + // flex item in the second flex line (or arbitrary flex lines other than the first flex + // line) is set to "gone" + // There was an issue reported for that + // https://github.com/google/flexbox-layout/issues/47 + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_visibility_gone_first_item_in_flex_line_column) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + val textView1 = activity.findViewById(R.id.text1) + val textView3 = activity.findViewById(R.id.text3) + + assertTrue(flexboxLayout.width > 0) + assertThat(flexboxLayout.width, `is`(textView1.width + textView3.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testView_visibility_invisible() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_views_visibility_invisible) + + // The text1 and text2's visibility are invisible, these views take space like visible views + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(textView1.visibility, `is`(View.INVISIBLE)) + assertThat(textView2.visibility, `is`(View.INVISIBLE)) + assertThat(textView3.top, `is`(textView1.bottom)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testWrapBefore() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_wrap_before_test) + + // layout_wrapBefore for the text2 and text3 are set to true, the text2 and text3 should + // be the first item for each flex line. + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + val textView1 = activity.findViewById(R.id.text1) + val textView2 = activity.findViewById(R.id.text2) + val textView3 = activity.findViewById(R.id.text3) + assertThat(flexboxLayout.height, `is`(textView1.height + textView2.height + textView3.height)) + + activityRule.runOnUiThread { + val lp2 = textView2.layoutParams as FlexboxLayout.LayoutParams + lp2.isWrapBefore = false + textView2.layoutParams = lp2 + } + + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + assertThat(flexboxLayout.height, `is`(textView1.height + textView3.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testWrapBefore_nowrap() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_wrap_before_test, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexWrap = FlexWrap.NOWRAP + } + }) + + // layout_wrapBefore for the text2 and text3 are set to true, but the flexWrap is set to + // NOWRAP, three text views should not be wrapped. + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.NOWRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testWrap_parentPadding_horizontal() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_wrap_parent_padding_horizontal_test) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering + // parent padding, the second TextView should be wrapped + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + assertThat(flexboxLayout.height, `is`(flexboxLayout.paddingTop + flexboxLayout.paddingBottom + + text1.height + text2.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testWrap_parentPadding_vertical() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_wrap_parent_padding_vertical_test) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + // The sum of height of TextView1 and TextView2 is not enough for wrapping, but considering + // parent padding, the second TextView should be wrapped + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + assertThat(flexboxLayout.width, `is`(flexboxLayout.paddingLeft + flexboxLayout.paddingRight + + text1.width + text2.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testWrap_childMargin_horizontal() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_wrap_child_margin_horizontal_test) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering + // the margin for the TextView2, the second TextView should be wrapped + onView(withId(R.id.text2)).check(isCompletelyBelow(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyRightOf(withId(R.id.text2))) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val lp2 = text2.layoutParams as FlexboxLayout.LayoutParams + assertThat(flexboxLayout.height, `is`(text1.height + text2.height + lp2.topMargin + + lp2.bottomMargin)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstItemLarge_horizontal() { + // This test verifies a empty flex line is not added when the first flex item is large + // and judged wrapping is required with the first item. + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_first_item_large_horizontal_test) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.alignItems, `is`(AlignItems.STRETCH)) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) + // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering + // the margin for the TextView2, the second TextView should be wrapped + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + assertThat(flexboxLayout.height, `is`(text1.height + text2.height + text3.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstItemLarge_vertical() { + // This test verifies a empty flex line is not added when the first flex item is large + // and judged wrapping is required with the first item. + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_first_item_large_vertical_test) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.alignItems, `is`(AlignItems.STRETCH)) + assertThat(flexboxLayout.alignContent, `is`(AlignContent.STRETCH)) + // The sum of width of TextView1 and TextView2 is not enough for wrapping, but considering + // the margin for the TextView2, the second TextView should be wrapped + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + assertThat(flexboxLayout.width, `is`(text1.width + text2.width + text3.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testWrap_childMargin_vertical() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_wrap_child_margin_vertical_test) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + // The sum of height of TextView1 and TextView2 is not enough for wrapping, but considering + // the margin of the TextView2, the second TextView should be wrapped + onView(withId(R.id.text2)).check(isCompletelyRightOf(withId(R.id.text1))) + onView(withId(R.id.text3)).check(isCompletelyBelow(withId(R.id.text2))) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val lp2 = text2.layoutParams as FlexboxLayout.LayoutParams + assertThat(flexboxLayout.width, + `is`(text1.width + text2.width + lp2.leftMargin + lp2.rightMargin)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testEmptyChildren() { + val flexboxLayout = createFlexboxLayout(R.layout.activity_empty_children) + + assertThat(flexboxLayout.childCount, `is`(0)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_verticalBeginning() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_divider_test_direction_row) + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val widthSumFirstRow = text1.width + text2.width + text3.width + divider!!.intrinsicWidth + assertThat(text3.right, `is`(widthSumFirstRow)) + assertThat(text1.left, `is`(not(flexboxLayout.left))) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + // The right should be 90 * 3 + 10 (divider) + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(280))) + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + val flexLine2 = flexLines[1] + // The right should be 140 * 2 + 10 (divider) + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(290))) + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_verticalMiddle() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_MIDDLE + } + } + ) + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_MIDDLE)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + // Three text views are placed in the first row, thus two vertical middle dividers should + // be placed + val widthSumFirstRow = text1.width + text2.width + text3.width + divider!!.intrinsicWidth * 2 + assertThat(text3.right, `is`(widthSumFirstRow)) + assertThat(text1.left, `is`(flexboxLayout.left)) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + // The right should be 90 * 3 + 10 * 2(divider) + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(290))) + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + val flexLine2 = flexLines[1] + // The right should be 140 * 2 + 10 (divider) + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(290))) + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_verticalEnd() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_END + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + // Three text views are placed in the first row, thus two vertical middle dividers should + // be placed + val widthSumFirstRow = text1.width + text2.width + text3.width + divider!!.intrinsicWidth + assertThat(text3.right + divider.intrinsicWidth, `is`(widthSumFirstRow)) + assertThat(text1.left, `is`(flexboxLayout.left)) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + // The right should be 90 * 3 + 10 (divider) + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(280))) + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + val flexLine2 = flexLines[1] + // The right should be 140 * 2 + 10 (divider) + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(290))) + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_verticalAll() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or FlexboxLayout.SHOW_DIVIDER_END + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerVertical, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + // Three text views are placed in the first row, thus two vertical middle dividers should + // be placed + val widthSumFirstRow = text1.width + text2.width + text3.width + divider!!.intrinsicWidth * 4 + assertThat(text3.right + divider.intrinsicWidth, `is`(widthSumFirstRow)) + assertThat(text1.left, `is`(not(flexboxLayout.left))) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + // The right should be 90 * 3 + 10 * 4 (divider) + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(310))) + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + val flexLine2 = flexLines[1] + // The right should be 140 * 2 + 10 * 3 (divider) + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(310))) + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_horizontalBeginning() { + val activity = activityRule.activity + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.dividerDrawableHorizontal = divider + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_BEGINNING + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_NONE + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_NONE)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + assertNotNull(divider) + val heightSum = text1.height + text4.height + divider!!.intrinsicHeight + assertThat(text4.bottom, `is`(heightSum)) + assertThat(text1.top, `is`(not(flexboxLayout.top))) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + // There is a horizontal divider at the beginning. Top and bottom coordinates are shifted + // by the amount of 15 + // The right should be 90 * 3 + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + val flexLine2 = flexLines[1] + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(280))) + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_horizontalMiddle() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.dividerDrawableHorizontal = divider + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_MIDDLE + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_NONE + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_MIDDLE)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_NONE)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSum = text1.height + text4.height + divider!!.intrinsicHeight + assertThat(text4.bottom, `is`(heightSum)) + assertThat(text1.top, `is`(flexboxLayout.top)) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + // The right should be 90 * 3 + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // There is a horizontal divider at the middle. Top and bottom coordinates are shifted + // by the amount of 15 + val flexLine2 = flexLines[1] + // The right should be 140 + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(280))) + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_horizontalEnd() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.dividerDrawableHorizontal = divider + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_END + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_NONE + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_END)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_NONE)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSum = text1.height + text4.height + divider!!.intrinsicHeight + assertThat(text4.bottom + divider.intrinsicHeight, `is`(heightSum)) + assertThat(text1.top, `is`(flexboxLayout.top)) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + // The right should be 90 * 3 + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // There is a horizontal divider at the middle. Top and bottom coordinates are shifted + // by the amount of 15 + val flexLine2 = flexLines[1] + // The right should be 140 + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(280))) + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_horizontalAll() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.dividerDrawableHorizontal = divider + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or FlexboxLayout.SHOW_DIVIDER_END + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_NONE + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerHorizontal, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_NONE)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSum = text1.height + text4.height + divider!!.intrinsicHeight * 3 + assertThat(text4.bottom + divider.intrinsicHeight, `is`(heightSum)) + assertThat(text1.top, `is`(not(flexboxLayout.top))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionRow_all_thickDivider() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_row, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val thickDivider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider_thick, null) + flexboxLayout.dividerDrawableVertical = thickDivider + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or FlexboxLayout.SHOW_DIVIDER_END + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + assertThat(flexboxLayout.showDividerVertical, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider_thick, null) + // The sum of three text views and the sum of thick dividers don't fit in one line. + // The last text view should be placed to the next line. + assertNotNull(divider) + val widthSumFirstRow = text1.width + text2.width + divider!!.intrinsicWidth * 3 + assertThat(text2.right + divider.intrinsicWidth, `is`(widthSumFirstRow)) + assertThat(text1.left, `is`(not(flexboxLayout.left))) + assertThat(text3.bottom, `is`(text1.height + text2.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_horizontalBeginning() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_divider_test_direction_column) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSumFirstRow = text1.height + text2.height + text3.height + divider!!.intrinsicHeight + assertThat(text3.bottom, `is`(heightSumFirstRow)) + assertThat(text1.top, `is`(not(flexboxLayout.top))) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 90 * 3 + 15 + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(285))) + val flexLine2 = flexLines[1] + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 140 * 2 + 15 + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(295))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_horizontalMiddle() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_MIDDLE + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_MIDDLE)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSumFirstRow = text1.height + text2.height + text3.height + divider!!.intrinsicHeight * 2 + assertThat(text3.bottom, `is`(heightSumFirstRow)) + assertThat(text1.top, `is`(flexboxLayout.top)) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 90 * 3 + 15 * 2 + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(300))) + val flexLine2 = flexLines[1] + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 140 * 2 + 15 + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(295))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_horizontalEnd() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_END + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSumFirstRow = text1.height + text2.height + text3.height + divider!!.intrinsicHeight + assertThat(text3.bottom + divider.intrinsicHeight, `is`(heightSumFirstRow)) + assertThat(text1.top, `is`(flexboxLayout.top)) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 90 * 3 + 15 + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(285))) + val flexLine2 = flexLines[1] + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 140 * 2 + 15 + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(295))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_horizontalAll() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or FlexboxLayout.SHOW_DIVIDER_END + } + } + ) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerHorizontal, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSumFirstRow = text1.height + text2.height + text3.height + divider!!.intrinsicHeight * 4 + assertThat(text3.bottom + divider.intrinsicHeight, `is`(heightSumFirstRow)) + assertThat(text1.top, `is`(not(flexboxLayout.top))) + val flexLines = flexboxLayout.flexLines + assertThat(flexLines.size, `is`(2)) + val flexLine1 = flexLines[0] + assertThat(flexLine1.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 90 * 3 + 15 * 4 + assertThat(flexLine1.mainSize, isEqualAllowingError(activity.dpToPixel(330))) + val flexLine2 = flexLines[1] + assertThat(flexLine2.crossSize, isEqualAllowingError(activity.dpToPixel(80))) + // The bottom should be 140 * 2 + 15 * 3 + assertThat(flexLine2.mainSize, isEqualAllowingError(activity.dpToPixel(325))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_verticalBeginning() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.dividerDrawableVertical = divider + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_BEGINNING + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_NONE + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_NONE)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val widthSum = text1.width + text4.width + divider!!.intrinsicWidth + assertThat(text4.right, `is`(widthSum)) + assertThat(text1.left, `is`(not(flexboxLayout.left))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_verticalMiddle() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.dividerDrawableVertical = divider + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_MIDDLE + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_NONE + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_MIDDLE)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_NONE)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val widthSum = text1.width + text4.width + divider!!.intrinsicWidth + assertThat(text4.right, `is`(widthSum)) + assertThat(text1.left, `is`(flexboxLayout.left)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_verticalEnd() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.dividerDrawableHorizontal = divider + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_END + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_NONE + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerVertical, `is`(FlexboxLayout.SHOW_DIVIDER_END)) + assertThat(flexboxLayout.showDividerHorizontal, `is`(FlexboxLayout.SHOW_DIVIDER_NONE)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val widthSum = text1.width + text4.width + divider!!.intrinsicWidth + assertThat(text4.right + divider.intrinsicWidth, `is`(widthSum)) + assertThat(text1.left, `is`(flexboxLayout.left)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_verticalAll() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.dividerDrawableVertical = divider + flexboxLayout.showDividerVertical = FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or FlexboxLayout.SHOW_DIVIDER_END + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_NONE + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerVertical, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val widthSum = text1.width + text4.width + divider!!.intrinsicWidth * 3 + assertThat(text4.right + divider.intrinsicWidth, `is`(widthSum)) + assertThat(text1.left, `is`(not(flexboxLayout.left))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_vertical_horizontal_All() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val divider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider, null) + flexboxLayout.setDividerDrawable(divider) + flexboxLayout.setShowDivider( + FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or FlexboxLayout.SHOW_DIVIDER_END) + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerVertical, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + assertThat(flexboxLayout.showDividerHorizontal, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val text4 = activity.findViewById(R.id.text4) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + assertNotNull(divider) + val heightSum = text1.height + text2.height + text3.height + divider!!.intrinsicHeight * 4 + val widthSum = text1.width + text4.width + divider.intrinsicWidth * 3 + assertThat(text3.bottom + divider.intrinsicHeight, `is`(heightSum)) + assertThat(text4.right + divider.intrinsicWidth, `is`(widthSum)) + assertThat(text1.left, `is`(not(flexboxLayout.left))) + assertThat(text1.top, `is`(not(flexboxLayout.top))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDivider_directionColumn_all_thickDivider() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_divider_test_direction_column, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + val thickDivider = ResourcesCompat + .getDrawable(activity.resources, R.drawable.divider_thick, null) + flexboxLayout.dividerDrawableHorizontal = thickDivider + flexboxLayout.showDividerHorizontal = FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or FlexboxLayout.SHOW_DIVIDER_END + } + }) + + assertThat(flexboxLayout.flexWrap, `is`(FlexWrap.WRAP)) + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayout.showDividerHorizontal, + `is`(FlexboxLayout.SHOW_DIVIDER_BEGINNING or FlexboxLayout.SHOW_DIVIDER_MIDDLE or + FlexboxLayout.SHOW_DIVIDER_END)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + val text3 = activity.findViewById(R.id.text3) + val divider = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider_thick, null) + // The sum of three text views and the sum of thick dividers don't fit in one line. + // The last text view should be placed to the next line. + assertNotNull(divider) + val heightSum = text1.height + text2.height + divider!!.intrinsicHeight * 3 + assertThat(text2.bottom + divider.intrinsicHeight, `is`(heightSum)) + assertThat(text1.top, `is`(not(flexboxLayout.top))) + assertThat(text3.right, `is`(text1.width + text3.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testZeroWidth_wrapContentHeight_positiveFlexGrow() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_zero_width_positive_flexgrow) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + // Both text view's layout_width is set to 0dp, layout_height is set to wrap_content and + // layout_flexGrow is set to 1. And the text2 has a longer text than the text1. + // So if the cross size calculation (height) is wrong, the height of two text view do not + // match because text2 is trying to expand vertically. + // This assertion verifies that isn't happening. Finally both text views expand horizontally + // enough to contain their texts in one line. + assertThat(text1.height, `is`(text2.height)) + assertThat(text1.width + text2.width, isEqualAllowingError(flexboxLayout.width)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testZeroHeight_wrapContentWidth_positiveFlexGrow() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout(R.layout.activity_zero_height_positive_flexgrow) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + val text1 = activity.findViewById(R.id.text1) + val text2 = activity.findViewById(R.id.text2) + assertThat(text1.width, `is`(not(text2.width))) + assertThat(text1.height + text2.height, isEqualAllowingError(flexboxLayout.height)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexDirection_row_alignItems_center_margin_oneSide() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_direction_row_align_items_center_margin_oneside) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.ROW)) + + val text1 = activity.findViewById(R.id.text1) + assertThat(text1.top, isEqualAllowingError(activity.dpToPixel(30))) + assertThat(flexboxLayout.bottom - text1.bottom, isEqualAllowingError(activity.dpToPixel(50))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexDirection_column_alignItems_center_margin_oneSide() { + val activity = activityRule.activity + val flexboxLayout = createFlexboxLayout( + R.layout.activity_direction_column_align_items_center_margin_oneside) + + assertThat(flexboxLayout.flexDirection, `is`(FlexDirection.COLUMN)) + + val text1 = activity.findViewById(R.id.text1) + assertThat(text1.left, isEqualAllowingError(activity.dpToPixel(30))) + assertThat(flexboxLayout.right - text1.right, isEqualAllowingError(activity.dpToPixel(50))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChildBottomMarginIncluded_flexContainerWrapContent_directionRow_flexGrow() { + // This test is to verify the case where: + // - layout_height is set to wrap_content for the FlexboxLayout + // - Bottom (or top) margin is set to a child + // - The child which the has the bottom (top) margin has the largest height in the + // same flex line (or only a single child exists) + // - The child has a positive layout_flexGrow attribute set + // If these conditions were met, the height of the FlexboxLayout didn't take the bottom + // margin on the child into account + // See https://github.com/google/flexbox-layout/issues/154 + val flexboxLayout = createFlexboxLayout( + R.layout.activity_wrap_content_child_bottom_margin_row_grow) + + // layout_height for text1: 24dp, layout_marginBottom: 12dp + assertThat(flexboxLayout.height, isEqualAllowingError(activityRule.activity.dpToPixel(36))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChildEndMarginIncluded_flexContainerWrapContent_directionColumn_flexGrow() { + // This test is to verify the case where: + // - layout_width is set to wrap_content for the FlexboxLayout + // - End (or start) margin is set to a child + // - The child which the has the end (start) margin has the largest width in the + // same flex line (or only a single child exists) + // - The child has a positive layout_flexGrow attribute set + // If these conditions were met, the width of the FlexboxLayout didn't take the bottom + // margin on the child into account + // See https://github.com/google/flexbox-layout/issues/154 + val flexboxLayout = createFlexboxLayout( + R.layout.activity_wrap_content_child_bottom_margin_column_grow) + + // layout_width for text1: 24dp, layout_marginEnd: 12dp + assertThat(flexboxLayout.width, isEqualAllowingError(activityRule.activity.dpToPixel(36))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChildBottomMarginIncluded_flexContainerWrapContent_directionRow_flexShrink() { + // This test is to verify the case where: + // - layout_height is set to wrap_content for the FlexboxLayout + // - flex_wrap is set to nowrap for the FlexboxLayout + // - Bottom (or top) margin is set to a child + // - The child which the has the bottom (top) margin has the largest height in the + // same flex line + // - The child has a positive layout_flexShrink attribute set + // - The sum of children width overflows parent's width (shrink will happen) + // If these conditions were met, the height of the FlexboxLayout didn't take the bottom + // margin on the child into account + // See https://github.com/google/flexbox-layout/issues/154 + val flexboxLayout = createFlexboxLayout( + R.layout.activity_wrap_content_child_bottom_margin_row_shrink) + + // layout_height for text1: 24dp, layout_marginBottom: 12dp + assertThat(flexboxLayout.height, isEqualAllowingError(activityRule.activity.dpToPixel(36))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChildBottomMarginIncluded_flexContainerWrapContent_directionColumn_flexShrink() { + // This test is to verify the case where: + // - layout_width is set to wrap_content for the FlexboxLayout + // - flex_wrap is set to nowrap for the FlexboxLayout + // - End (or start) margin is set to a child + // - The child which the has the end (start) margin has the largest width in the + // same flex line + // - The child has a positive layout_flexShrink attribute set + // - The sum of children height overflows parent's height (shrink will happen) + // If these conditions were met, the height of the FlexboxLayout didn't take the bottom + // margin on the child into account + // See https://github.com/google/flexbox-layout/issues/154 + val flexboxLayout = createFlexboxLayout( + R.layout.activity_wrap_content_child_bottom_margin_column_shrink) + + // layout_width for text1: 24dp, layout_marginEnd: 12dp + assertThat(flexboxLayout.width, isEqualAllowingError(activityRule.activity.dpToPixel(36))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChildNeedsRemeasure_row() { + createFlexboxLayout(R.layout.activity_child_needs_remeasure_row) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChildNeedsRemeasure_column() { + createFlexboxLayout(R.layout.activity_child_needs_remeasure_column) + onView(withId(R.id.text1)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text1)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstViewGone_firstLineSingleItem_row() { + // This test verifies the case where the first view's visibility is gone and the second + // view is in the next flex line. In that case, the second view's position is misplaced. + // https://github.com/google/flexbox-layout/issues/283 + createFlexboxLayout(R.layout.activity_first_view_gone_first_line_single_item) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstViewGone_firstLineSingleItem_column() { + // This test verifies the case where the first view's visibility is gone and the second + // view is in the next flex line. In that case, the second view's position is misplaced. + // https://github.com/google/flexbox-layout/issues/283 + createFlexboxLayout(R.layout.activity_first_view_gone_first_line_single_item, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstViewGone_flexGrowSetForRestOfItems_row() { + // This test verifies the case where the first view's visibility is gone and the second + // view and third view have the layout_flexGrow attribute set. In that case, the second + // view's position is misplaced and the third view becomes invisible . + // https://github.com/google/flexbox-layout/issues/303 + createFlexboxLayout(R.layout.activity_first_view_gone_layout_grow_set_for_rest) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isRightAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstViewGone_flexGrowSetForRestOfItems_column() { + // This test verifies the case where the first view's visibility is gone and the second + // view and third view have the layout_flexGrow attribute set. In that case, the second + // view's position is misplaced and the third view becomes invisible . + // https://github.com/google/flexbox-layout/issues/303 + createFlexboxLayout(R.layout.activity_first_view_gone_layout_grow_set_for_rest, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isBottomAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstViewGone_flexShrinkSetForRestOfItems_row() { + createFlexboxLayout(R.layout.activity_first_view_gone_layout_shrink_set_for_rest) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstViewGone_flexShrinkSetForRestOfItems_column() { + createFlexboxLayout(R.layout.activity_first_view_gone_layout_shrink_set_for_rest, + object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) { + flexboxLayout.flexDirection = FlexDirection.COLUMN + } + }) + onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text2)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))) + onView(withId(R.id.text3)).check(isTopAlignedWith(withId(R.id.flexbox_layout))) + } + + @Throws(Throwable::class) + private fun createFlexboxLayout(@LayoutRes activityLayoutResId: Int, + configuration: Configuration = Configuration.EMPTY): FlexboxLayout { + val activity = activityRule.activity + activityRule.runOnUiThread { + activity.setContentView(activityLayoutResId) + val flexboxLayout = activity.findViewById(R.id.flexbox_layout) + configuration.apply(flexboxLayout) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + return activity.findViewById(R.id.flexbox_layout) as FlexboxLayout + } + + private fun createTextView(context: Context, text: String, order: Int): TextView { + val textView = TextView(context) + textView.text = text + val lp = FlexboxLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) + lp.order = order + textView.layoutParams = lp + return textView + } + + private fun hasWidth(width: Int): ViewAssertion { + return matches(object : TypeSafeMatcher() { + override fun describeTo(description: Description) { + description.appendText("expected width: " + width) + } + + override fun describeMismatchSafely(item: View, mismatchDescription: Description) { + mismatchDescription.appendText("actual width: " + item.width) + } + + override fun matchesSafely(item: View) = item.width == width + }) + } + + private fun hasHeight(height: Int): ViewAssertion { + return matches(object : TypeSafeMatcher() { + override fun describeTo(description: Description) { + description.appendText("expected height: " + height) + } + + override fun describeMismatchSafely(item: View, mismatchDescription: Description) { + mismatchDescription.appendText("actual height: " + item.height) + } + + override fun matchesSafely(item: View) = item.height == height + }) + } + + private interface Configuration { + + fun apply(flexboxLayout: FlexboxLayout) + + companion object { + val EMPTY: Configuration = object : Configuration { + override fun apply(flexboxLayout: FlexboxLayout) = Unit + } + } + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.java deleted file mode 100644 index 062362f..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright 2017 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - -import static android.support.test.espresso.Espresso.onView; -import static android.support.test.espresso.matcher.ViewMatchers.withId; - -import static org.hamcrest.Matchers.is; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; - -import android.content.Context; -import android.content.pm.ActivityInfo; -import android.content.res.Configuration; -import android.support.test.InstrumentationRegistry; -import android.support.test.espresso.ViewAction; -import android.support.test.espresso.action.CoordinatesProvider; -import android.support.test.espresso.action.GeneralLocation; -import android.support.test.espresso.action.GeneralSwipeAction; -import android.support.test.espresso.action.Press; -import android.support.test.espresso.action.Swipe; -import android.support.test.filters.FlakyTest; -import android.support.test.filters.MediumTest; -import android.support.test.rule.ActivityTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.support.v7.widget.RecyclerView; -import android.view.ViewGroup; - -import com.google.android.flexbox.FlexDirection; -import com.google.android.flexbox.FlexLine; -import com.google.android.flexbox.FlexboxLayoutManager; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** - * Integration tests for {@link FlexboxLayoutManager} with the Activity that handles configration - * changes manually. - */ -@RunWith(AndroidJUnit4.class) -@MediumTest -public class FlexboxLayoutManagerConfigChangeTest { - - @Rule - public ActivityTestRule mActivityRule = - new ActivityTestRule<>(ConfigChangeActivity.class); - - @Test - @FlakyTest - public void testFlexLinesDiscardedOnOrientationChange_direction_row() throws Throwable { - // Verifies the case that the calculated Flex lines are correctly discarded when a - // orientation - // happens with an Activity that handles configuration changes manually - final ConfigChangeActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - // This test assumes that the screen width and the height are different. - recyclerView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; - recyclerView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT; - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 100; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 90, 65); - adapter.addItem(lp); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - FlexLine firstLine = layoutManager.getFlexLines().get(0); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - int orientation = activity.getResources().getConfiguration().orientation; - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - } else { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.scrollToPosition(0); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - FlexLine firstLineAfterRotation = layoutManager.getFlexLines().get(0); - assertThat(firstLine.getMainSize(), is(not(firstLineAfterRotation.getMainSize()))); - } - - @Test - @FlakyTest - public void testFlexLinesDiscardedOnOrientationChange_direction_column() throws Throwable { - // Verifies the case that the calculated Flex lines are correctly discarded when a - // orientation - // happens with an Activity that handles configuration changes manually - final ConfigChangeActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - // This test assumes that the screen width and the height are different. - recyclerView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT; - recyclerView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT; - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 100; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 90, 65); - adapter.addItem(lp); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - FlexLine firstLine = layoutManager.getFlexLines().get(0); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - int orientation = activity.getResources().getConfiguration().orientation; - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - } else { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.scrollToPosition(0); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - FlexLine firstLineAfterRotation = layoutManager.getFlexLines().get(0); - assertThat(firstLine.getMainSize(), is(not(firstLineAfterRotation.getMainSize()))); - } - - /** - * Creates a new flex item. - * - * @param context the context - * @param width in DP - * @param height in DP - * @return the created {@link FlexboxLayoutManager.LayoutParams} instance - */ - private FlexboxLayoutManager.LayoutParams createLayoutParams(Context context, int width, - int height) { - return new FlexboxLayoutManager.LayoutParams( - TestUtil.dpToPixel(context, width), - TestUtil.dpToPixel(context, height)); - } - - private static ViewAction swipe(CoordinatesProvider from, CoordinatesProvider to) { - return new GeneralSwipeAction(Swipe.FAST, from, to, Press.FINGER); - } -} - diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.kt new file mode 100644 index 0000000..bcbeb9f --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerConfigChangeTest.kt @@ -0,0 +1,168 @@ +/* + * Copyright 2017 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import android.content.Context +import android.content.pm.ActivityInfo +import android.content.res.Configuration +import android.support.test.InstrumentationRegistry +import android.support.test.espresso.Espresso.onView +import android.support.test.espresso.ViewAction +import android.support.test.espresso.action.* +import android.support.test.espresso.matcher.ViewMatchers.withId +import android.support.test.filters.FlakyTest +import android.support.test.filters.MediumTest +import android.support.test.rule.ActivityTestRule +import android.support.test.runner.AndroidJUnit4 +import android.support.v7.widget.RecyclerView +import android.view.ViewGroup +import com.google.android.flexbox.FlexDirection +import com.google.android.flexbox.FlexboxLayoutManager +import org.hamcrest.Matchers.`is` +import org.hamcrest.core.IsNot.not +import org.junit.Assert.assertThat +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Integration tests for [FlexboxLayoutManager] with the Activity that handles configuration + * changes manually. + */ +@RunWith(AndroidJUnit4::class) +@MediumTest +class FlexboxLayoutManagerConfigChangeTest { + + @JvmField + @Rule + var activityRule = ActivityTestRule(ConfigChangeActivity::class.java) + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexLinesDiscardedOnOrientationChange_direction_row() { + // Verifies the case that the calculated Flex lines are correctly discarded when a + // orientation happens with an Activity that handles configuration changes manually + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + // This test assumes that the screen width and the height are different. + recyclerView.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT + recyclerView.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..99) { + val lp = createLayoutParams(activity, 90, 65) + adapter.addItem(lp) + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + val firstLine = layoutManager.flexLines[0] + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + + activityRule.runOnUiThread { + val orientation = activity.resources.configuration.orientation + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + } else { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + activityRule.runOnUiThread { layoutManager.scrollToPosition(0) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + val firstLineAfterRotation = layoutManager.flexLines[0] + assertThat(firstLine.mainSize, `is`(not(firstLineAfterRotation.mainSize))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexLinesDiscardedOnOrientationChange_direction_column() { + // Verifies the case that the calculated Flex lines are correctly discarded when a + // orientation happens with an Activity that handles configuration changes manually + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + // This test assumes that the screen width and the height are different. + recyclerView.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT + recyclerView.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..99) { + val lp = createLayoutParams(activity, 90, 65) + adapter.addItem(lp) + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + val firstLine = layoutManager.flexLines[0] + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + + activityRule.runOnUiThread { + val orientation = activity.resources.configuration.orientation + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + } else { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + activityRule.runOnUiThread { layoutManager.scrollToPosition(0) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + val firstLineAfterRotation = layoutManager.flexLines[0] + assertThat(firstLine.mainSize, `is`(not(firstLineAfterRotation.mainSize))) + } + + /** + * Creates a new flex item. + * + * @param context the context + * @param width in DP + * @param height in DP + * @return the created [FlexboxLayoutManager.LayoutParams] instance + */ + private fun createLayoutParams(context: Context, width: Int, + height: Int): FlexboxLayoutManager.LayoutParams { + return FlexboxLayoutManager.LayoutParams(context.dpToPixel(width), context.dpToPixel(height)) + } + + private fun swipe(from: CoordinatesProvider, to: CoordinatesProvider): ViewAction { + return GeneralSwipeAction(Swipe.FAST, from, to, Press.FINGER) + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.java deleted file mode 100644 index 24f0fd2..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.java +++ /dev/null @@ -1,3618 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - -import static android.support.test.espresso.Espresso.onView; -import static android.support.test.espresso.matcher.ViewMatchers.withId; - -import static com.google.android.flexbox.FlexboxItemDecoration.HORIZONTAL; -import static com.google.android.flexbox.FlexboxItemDecoration.VERTICAL; -import static com.google.android.flexbox.test.IsEqualAllowingError.isEqualAllowingError; - -import static junit.framework.Assert.assertTrue; - -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.lessThan; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; - -import android.content.Context; -import android.content.pm.ActivityInfo; -import android.content.res.Configuration; -import android.graphics.drawable.Drawable; -import android.os.Build; -import android.support.annotation.DrawableRes; -import android.support.test.InstrumentationRegistry; -import android.support.test.espresso.ViewAction; -import android.support.test.espresso.action.CoordinatesProvider; -import android.support.test.espresso.action.GeneralLocation; -import android.support.test.espresso.action.GeneralSwipeAction; -import android.support.test.espresso.action.Press; -import android.support.test.espresso.action.Swipe; -import android.support.test.filters.FlakyTest; -import android.support.test.filters.MediumTest; -import android.support.test.rule.ActivityTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.support.v4.content.res.ResourcesCompat; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import com.google.android.flexbox.AlignItems; -import com.google.android.flexbox.AlignSelf; -import com.google.android.flexbox.FlexDirection; -import com.google.android.flexbox.FlexLine; -import com.google.android.flexbox.FlexWrap; -import com.google.android.flexbox.FlexboxItemDecoration; -import com.google.android.flexbox.FlexboxLayoutManager; -import com.google.android.flexbox.JustifyContent; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -/** - * Integration tests for {@link FlexboxLayoutManager}. - */ -@RunWith(AndroidJUnit4.class) -@MediumTest -public class FlexboxLayoutManagerTest { - - @Rule - public ActivityTestRule mActivityRule = - new ActivityTestRule<>(FlexboxTestActivity.class); - - @Test - @FlakyTest - public void testLoadFromXml() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview_reverse); - } - }); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); - assertThat(recyclerView, is(notNullValue())); - assertThat(layoutManager, is(instanceOf(FlexboxLayoutManager.class))); - FlexboxLayoutManager flexboxLayoutManager = (FlexboxLayoutManager) layoutManager; - assertThat(flexboxLayoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(flexboxLayoutManager.getFlexWrap(), is(FlexWrap.WRAP)); - } - - @Test - @FlakyTest - public void testChangeAttributesFromCode() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview_reverse); - } - }); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); - assertThat(recyclerView, is(notNullValue())); - assertThat(layoutManager, is(instanceOf(FlexboxLayoutManager.class))); - FlexboxLayoutManager flexboxLayoutManager = (FlexboxLayoutManager) layoutManager; - assertThat(flexboxLayoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(flexboxLayoutManager.getFlexWrap(), is(FlexWrap.WRAP)); - - flexboxLayoutManager.setFlexDirection(FlexDirection.COLUMN); - flexboxLayoutManager.setFlexWrap(FlexWrap.NOWRAP); - flexboxLayoutManager.setJustifyContent(JustifyContent.CENTER); - flexboxLayoutManager.setAlignItems(AlignItems.FLEX_END); - assertThat(flexboxLayoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(flexboxLayoutManager.getFlexWrap(), is(FlexWrap.NOWRAP)); - assertThat(flexboxLayoutManager.getJustifyContent(), is(JustifyContent.CENTER)); - assertThat(flexboxLayoutManager.getAlignItems(), is(AlignItems.FLEX_END)); - } - - @Test - @FlakyTest - public void testAddViewHolders_direction_row_not_wrapped() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - adapter.addItem(createLayoutParams(activity, 120, 80)); - adapter.addItem(createLayoutParams(activity, 120, 80)); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(layoutManager.getFlexWrap(), is(FlexWrap.WRAP)); - // Only 2 items are added and all items should be attached (visible). So both - // getChildCount and getFlexItemCount(including detached items) should return the same value - assertThat(layoutManager.getFlexItemCount(), is(2)); - assertThat(layoutManager.getChildCount(), is(2)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - } - - @Test - @FlakyTest - public void testAddViewHolders_direction_row_wrapped() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - adapter.addItem(createLayoutParams(activity, 120, 80)); - adapter.addItem(createLayoutParams(activity, 120, 80)); - adapter.addItem(createLayoutParams(activity, 120, 80)); - adapter.addItem(createLayoutParams(activity, 120, 80)); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // 4 items are added and all items should be attached (visible). So both - // getChildCount and getFlexItemCount(including detached items) should return the same value - assertThat(layoutManager.getFlexItemCount(), is(4)); - assertThat(layoutManager.getChildCount(), is(4)); - assertThat(layoutManager.getFlexLines().size(), is(2)); - } - - @Test - @FlakyTest - public void testAddViewHolders_direction_row_partOfItems_detached() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - // RecyclerView width: 320, height: 240. - // Computed FlexContainer width: 320, height: 450 - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // In total 9 items are added but the seventh item and the items follow aren't attached - // (invisible) so are not included in the count of of the getChildCount. - assertThat(layoutManager.getFlexItemCount(), is(9)); - assertThat(layoutManager.getChildCount(), is(6)); - - // At first only the visible area of the flex lines are calculated - assertThat(layoutManager.getFlexLines().size(), is(3)); - } - - @Test - @FlakyTest - public void testAddViewHolders_direction_row_scrollVertically() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - adapter.addItem(createLayoutParams(activity, 150, 90)); - // RecyclerView width: 320, height: 240. - // Computed FlexContainer width: 320, height: 450 - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // In total 9 items are added but the seventh item and the items follow aren't attached - // (invisible) so are not included in the count of of the getChildCount. - assertThat(layoutManager.getFlexItemCount(), is(9)); - assertThat(layoutManager.getChildCount(), is(6)); - // At first only the visible area of the flex lines are calculated - assertThat(layoutManager.getFlexLines().size(), is(3)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - assertThat(layoutManager.getFlexItemCount(), is(9)); - // The RecyclerView is swiped to top until it reaches the bottom of the view. - // The number of the visible views should be 5 - assertThat(layoutManager.getChildCount(), is(5)); - // Since the RecyclerView is swiped to the bottom, all flex lines should be calculated - // by now - assertThat(layoutManager.getFlexLines().size(), is(5)); - } - - @Test - @FlakyTest - public void testFlexGrow() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 150, 130); - lp1.setFlexGrow(1.0f); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 150, 130); - lp2.setFlexGrow(1.0f); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 150, 130); - lp3.setFlexGrow(1.0f); - adapter.addItem(lp3); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // The flexGrow parameters for all LayoutParams are set to 1.0, expecting each child to - // fill the horizontal remaining space - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(2)); - assertThat(layoutManager.getChildAt(0).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 160))); - assertThat(layoutManager.getChildAt(1).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 160))); - assertThat(layoutManager.getChildAt(2).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 320))); - } - - @Test - @FlakyTest - public void testAddViewHolders_direction_column_partOfItems_detached() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - - layoutManager.setFlexDirection(FlexDirection.COLUMN); - // RecyclerView width: 320, height: 240. - // Computed FlexContainer width: 450, height: 240 - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // In total 9 items are added but the seventh item and the items after aren't attached - // (invisible) so are not included in the count of of the getChildCount. - assertThat(layoutManager.getFlexItemCount(), is(9)); - assertThat(layoutManager.getChildCount(), is(6)); - - // At first only the visible area of the flex lines are calculated - assertThat(layoutManager.getFlexLines().size(), is(3)); - } - - @Test - @FlakyTest - public void testAddViewHolders_direction_column_scrollHorizontally() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - adapter.addItem(createLayoutParams(activity, 120, 100)); - - layoutManager.setFlexDirection(FlexDirection.COLUMN); - // RecyclerView width: 320, height: 240. - // Computed FlexContainer width: 500, height: 240 - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // In total 9 items are added but the seventh item and the items follow aren't attached - // (invisible) so are not included in the count of of the getChildCount. - assertThat(layoutManager.getFlexItemCount(), is(9)); - assertThat(layoutManager.getChildCount(), is(6)); - // At first only the visible area of the flex lines are calculated - assertThat(layoutManager.getFlexLines().size(), is(3)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - assertThat(layoutManager.getFlexItemCount(), is(9)); - // The RecyclerView is swiped to top until it reaches the right edge of the view. - // The number of the visible views should be 5 - assertThat(layoutManager.getChildCount(), is(5)); - // Since the RecyclerView is swiped to the bottom, all flex lines should be calculated - // by now - assertThat(layoutManager.getFlexLines().size(), is(5)); - } - - @Test - @FlakyTest - public void testJustifyContent_flexStart_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setJustifyContent(JustifyContent.FLEX_START); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_START)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 150))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexEnd_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setJustifyContent(JustifyContent.FLEX_END); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_END)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 170))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 220))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 220))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 320))); - } - - @Test - @FlakyTest - public void testJustifyContent_center_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setJustifyContent(JustifyContent.CENTER); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.CENTER)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 85))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 235))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setJustifyContent(JustifyContent.SPACE_AROUND); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_AROUND)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 28))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 78))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 242))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 292))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setJustifyContent(JustifyContent.SPACE_BETWEEN); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_BETWEEN)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 320))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexStart_direction_rowReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.ROW_REVERSE); - layoutManager.setJustifyContent(JustifyContent.FLEX_START); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_START)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 320))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 220))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 170))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 220))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexEnd_direction_rowReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.ROW_REVERSE); - layoutManager.setJustifyContent(JustifyContent.FLEX_END); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_END)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 150))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - } - - @Test - @FlakyTest - public void testJustifyContent_center_direction_rowReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.ROW_REVERSE); - layoutManager.setJustifyContent(JustifyContent.CENTER); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.CENTER)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 235))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 85))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_direction_rowReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.ROW_REVERSE); - layoutManager.setJustifyContent(JustifyContent.SPACE_AROUND); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_AROUND)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 242))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 292))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 28))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 78))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_direction_rowReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 50, 100); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.ROW_REVERSE); - layoutManager.setJustifyContent(JustifyContent.SPACE_BETWEEN); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_BETWEEN)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 270))); - assertThat(layoutManager.getChildAt(0).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 320))); - assertThat(layoutManager.getChildAt(1).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 135))); - assertThat(layoutManager.getChildAt(1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 185))); - assertThat(layoutManager.getChildAt(2).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(2).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexStart_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setJustifyContent(JustifyContent.FLEX_START); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_START)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 150))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexEnd_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setJustifyContent(JustifyContent.FLEX_END); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_END)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 90))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 140))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 140))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 190))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 190))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 240))); - } - - @Test - @FlakyTest - public void testJustifyContent_center_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setJustifyContent(JustifyContent.CENTER); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.CENTER)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 45))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 195))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setJustifyContent(JustifyContent.SPACE_AROUND); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_AROUND)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 15))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 65))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 175))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 225))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setJustifyContent(JustifyContent.SPACE_BETWEEN); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_BETWEEN)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 190))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 240))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexStart_direction_columnReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN_REVERSE); - layoutManager.setJustifyContent(JustifyContent.FLEX_START); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_START)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 190))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 240))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 140))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 190))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 90))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 140))); - } - - @Test - @FlakyTest - public void testJustifyContent_flexEnd_direction_columnReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN_REVERSE); - layoutManager.setJustifyContent(JustifyContent.FLEX_END); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.FLEX_END)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 150))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - } - - @Test - @FlakyTest - public void testJustifyContent_center_direction_columnReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN_REVERSE); - layoutManager.setJustifyContent(JustifyContent.CENTER); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.CENTER)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 195))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 45))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceAround_direction_columnReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN_REVERSE); - layoutManager.setJustifyContent(JustifyContent.SPACE_AROUND); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_AROUND)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 175))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 225))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 15))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 65))); - } - - @Test - @FlakyTest - public void testJustifyContent_spaceBetween_direction_columnReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 50); - adapter.addItem(lp3); - layoutManager.setFlexDirection(FlexDirection.COLUMN_REVERSE); - layoutManager.setJustifyContent(JustifyContent.SPACE_BETWEEN); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN_REVERSE)); - assertThat(layoutManager.getJustifyContent(), is(JustifyContent.SPACE_BETWEEN)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 190))); - assertThat(layoutManager.getChildAt(0).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 240))); - assertThat(layoutManager.getChildAt(1).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 95))); - assertThat(layoutManager.getChildAt(1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 145))); - assertThat(layoutManager.getChildAt(2).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 0))); - assertThat(layoutManager.getChildAt(2).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - } - - @Test - @FlakyTest - public void testLargeItem_scrollFast_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 200; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 50); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(layoutManager.getFlexItemCount(), is(200)); - // Only the visible items - assertThat(layoutManager.getChildCount(), is(not(200))); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // Should be scrolled to the bottom by now - assertThat(layoutManager.getFlexItemCount(), is(200)); - // Only the visible items - assertThat(layoutManager.getChildCount(), is(not(200))); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - // Should be scrolled to the top - assertThat(layoutManager.getFlexItemCount(), is(200)); - // Only the visible items - assertThat(layoutManager.getChildCount(), is(not(200))); - } - - @Test - @FlakyTest - public void testLargeItem_scrollFast_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 200; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 80); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getFlexItemCount(), is(200)); - // Only the visible items - assertThat(layoutManager.getChildCount(), is(not(200))); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - - // Should be scrolled to the right edge by now - assertThat(layoutManager.getFlexItemCount(), is(200)); - // Only the visible items - assertThat(layoutManager.getChildCount(), is(not(200))); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - // Should be scrolled to the left edge by now - assertThat(layoutManager.getFlexItemCount(), is(200)); - // Only the visible items - assertThat(layoutManager.getChildCount(), is(not(200))); - } - - @Test - @FlakyTest - public void testAlignItems_stretch_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - layoutManager.setAlignItems(AlignItems.STRETCH); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 70, 80); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 70, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 70, 30); - adapter.addItem(lp3); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(layoutManager.getAlignItems(), is(AlignItems.STRETCH)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - // Verify all items heights are stretched - assertThat(layoutManager.getChildAt(0).getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - assertThat(layoutManager.getChildAt(1).getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - assertThat(layoutManager.getChildAt(2).getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testAlignItems_stretch_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setAlignItems(AlignItems.STRETCH); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 80, 70); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 70); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 30, 70); - adapter.addItem(lp3); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getAlignItems(), is(AlignItems.STRETCH)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - // Verify all items widths are stretched - assertThat(layoutManager.getChildAt(0).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - assertThat(layoutManager.getChildAt(1).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - assertThat(layoutManager.getChildAt(2).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testAlignSelf_stretch_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - layoutManager.setAlignItems(AlignItems.FLEX_START); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 70, 80); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 70, 50); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 70, 30); - lp3.setAlignSelf(AlignSelf.STRETCH); - adapter.addItem(lp3); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(layoutManager.getAlignItems(), is(AlignItems.FLEX_START)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - // Verify the item whose align self is set to stretch is stretched - assertThat(layoutManager.getChildAt(0).getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - assertThat(layoutManager.getChildAt(1).getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(2).getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testAlignSelf_stretch_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setAlignItems(AlignItems.FLEX_START); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 80, 70); - adapter.addItem(lp1); - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 50, 70); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 30, 70); - lp3.setAlignSelf(AlignSelf.STRETCH); - adapter.addItem(lp3); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getAlignItems(), is(AlignItems.FLEX_START)); - assertThat(layoutManager.getFlexItemCount(), is(3)); - assertThat(layoutManager.getFlexLines().size(), is(1)); - // Verify the item whose align self is set to stretch is stretched - assertThat(layoutManager.getChildAt(0).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - assertThat(layoutManager.getChildAt(1).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 50))); - assertThat(layoutManager.getChildAt(2).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testStretchViews_from_middle_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - layoutManager.setAlignItems(AlignItems.STRETCH); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 80); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(layoutManager.getAlignItems(), is(AlignItems.STRETCH)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.setAlignItems(AlignItems.STRETCH); - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 20); - // Add an item whose height is less than the other items. - // But with alignItems set to stretch, the height of the item should be stretched - adapter.addItem(lp); - adapter.notifyDataSetChanged(); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getChildAt(layoutManager.getChildCount() - 1).getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80))); - } - - @Test - @FlakyTest - public void testStretchViews_from_middle_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setAlignItems(AlignItems.STRETCH); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 50); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getAlignItems(), is(AlignItems.STRETCH)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, - GeneralLocation.TOP_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, - GeneralLocation.TOP_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, - GeneralLocation.TOP_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, - GeneralLocation.TOP_LEFT)); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.setAlignItems(AlignItems.STRETCH); - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 20, 50); - // Add an item whose width is less than the other items. - // But with alignItems set to stretch, the width of the item should be stretched - adapter.addItem(lp); - adapter.notifyDataSetChanged(); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getChildAt(layoutManager.getChildCount() - 1).getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 70))); - } - - @Test - @FlakyTest - public void testScrollToTop_middleItem_as_anchorPosition() throws Throwable { - // There was an issue that the anchor position was based on the first item in the first - // visible flex line when scrolling to top. But the anchor position should be based on the - // flex line position (view which has the minimum top position in the same flex line) - // This test verifies the issue is fixed. - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final int positionInSecondLine = 6; - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - layoutManager.setAlignItems(AlignItems.FLEX_START); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 80); - if (i == positionInSecondLine) { - // Change the height from other items in the second line, not the first item - // in the second line - lp = createLayoutParams(activity, 70, 130); - } - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Each line has 4 (320 / 70) flex items and 12 (50 / 4) lines in total - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // By this moment reached to the bottom - - // Now scrolling to the top to see if the views in the first flex line is correctly placed - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - - assertThat(layoutManager.getChildAt(positionInSecondLine).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 210))); // 80 + 130 - // Verify that the view in the same line's cross axis position is correct - assertThat(layoutManager.getChildAt(positionInSecondLine - 1).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 160))); // 80 + 80 - } - - @Test - @FlakyTest - public void testScrollToLeft_middleItem_as_anchorPosition() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final int positionInSecondLine = 6; - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setAlignItems(AlignItems.FLEX_START); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 80, 50); - if (i == positionInSecondLine) { - // Change the width from other items in the second line, not the first item - // in the second line - lp = createLayoutParams(activity, 130, 50); - } - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Each line has 4 (240 / 50) flex items and 12 (50 / 4) lines in total - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - // By this moment reached to the right edge - - // Now scrolling to the left to see if the views in the first flex line is correctly placed - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - - assertThat(layoutManager.getChildAt(positionInSecondLine).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 210))); // 80 + 130 - // Verify that the view in the same line's cross axis position is correct - assertThat(layoutManager.getChildAt(positionInSecondLine - 1).getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 160))); // 80 + 80 - } - - @Test - @FlakyTest - public void testScrollToBottom_middleItem_as_anchorPosition() throws Throwable { - // There was an issue that the anchor position was based on the last item in the last - // visible flex line when scrolling to bottom. But the anchor position should be based on - // the - // flex line position (view which has the maximum bottom position in the same flex line) - // This test verifies the issue is fixed. - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final int positionInSecondBottomLine = 45; - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 80); - if (i == positionInSecondBottomLine) { - // Change the height from other items in the second bottom line - lp = createLayoutParams(activity, 70, 130); - } - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Each line has 4 (320 / 70) flex items and 12 (50 / 4) lines in total - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // By this moment reached to the bottom - - // 4 comes from the number of flex items - positionInSecondBottomLine - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - View anchorView = layoutManager.getChildAt(layoutManager.getChildCount() - 4); - assertThat(recyclerView.getBottom() - anchorView.getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 210))); // 80 + 130 - } - - @Test - @FlakyTest - public void testScrollToTop_direction_rowReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW_REVERSE); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 80); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Each line has 4 (320 / 70) flex items and 12 (50 / 4) lines in total - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // By this moment reached to the bottom - - // Now scrolling to the top to see if the views in the first flex line is correctly placed - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - - assertThat(((TextView) layoutManager.getChildAt(0)).getText().toString(), is("1")); - } - - @Test - @FlakyTest - public void testFlexGrow_only_oneItem_has_positive_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 4; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 80); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Flex line 1: 3 items - // Flex line 2: 1 item - // Give the second item in the first line a positive flex grow - adapter.getItemAt(0).setHeight(TestUtil.dpToPixel(activity, 140)); - adapter.getItemAt(1).setFlexGrow(1.0f); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - // Verify the vertical position (cross size) of the second line is correctly positioned - assertThat(layoutManager.getChildAt(3).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 140))); - } - - @Test - @FlakyTest - public void testFlexGrow_only_oneItem_has_positive_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 4; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 70); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Flex line 1: 3 items - // Flex line 2: 1 item - // Give the second item in the first line a positive flex grow - adapter.getItemAt(0).setWidth(TestUtil.dpToPixel(activity, 120)); - adapter.getItemAt(1).setFlexGrow(1.0f); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - // Verify the horizontal position (cross size) of the second line is correctly positioned - assertThat(layoutManager.getChildAt(3).getLeft(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 120))); - } - - @Test - @FlakyTest - public void testFirstReferenceView_middleOf_line_used_as_anchor() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - layoutManager.setAlignItems(AlignItems.FLEX_END); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 80); - adapter.addItem(lp1); - // The second view in the first line has the maximum height in the same line - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 180); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 80); - adapter.addItem(lp3); - for (int i = 0; i < 30; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 80); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // By this moment reached to the bottom - - // Now scrolling to the top to see if the views in the first flex line is correctly placed - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - - // The top coordinate of the first view should be the height of the second view minus the - // height of the first view (180 - 80) - assertThat(layoutManager.getChildAt(0).getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - } - - @Test - @FlakyTest - public void testLastReferenceView_middleOf_line_used_as_anchor() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - layoutManager.setAlignItems(AlignItems.FLEX_START); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 30; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 80); - adapter.addItem(lp); - } - FlexboxLayoutManager.LayoutParams lp1 = createLayoutParams(activity, 100, 80); - adapter.addItem(lp1); - // The second view in the last line has the maximum height in the same line - FlexboxLayoutManager.LayoutParams lp2 = createLayoutParams(activity, 100, 180); - adapter.addItem(lp2); - FlexboxLayoutManager.LayoutParams lp3 = createLayoutParams(activity, 100, 80); - adapter.addItem(lp3); - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // By this moment reached to the bottom - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - - // The bottom coordinate of the first view in the last line should be the height of the - // second view in the last line minus the height of the first view in the last line - // (180 - 80) - assertThat(layoutManager.getChildAt(layoutManager.getChildCount() - 2).getBottom() - - layoutManager.getChildAt(layoutManager.getChildCount() - 3).getBottom(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - } - - @Test - @FlakyTest - public void testRotateScreen_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - layoutManager.setAlignItems(AlignItems.STRETCH); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 30; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 100); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.CENTER)); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - View anchorView = layoutManager.getChildAt(0); - int offset = anchorView.getTop(); - assertTrue(offset < 0); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - int orientation = activity.getResources().getConfiguration().orientation; - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - } else { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // Verify that offset position is preserved for the first visible view after the rotation - View anchorAfterRotate = layoutManager.getChildAt(0); - assertTrue(anchorAfterRotate.getTop() < 0); - } - - @Test - @FlakyTest - public void testRotateScreen_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - layoutManager.setAlignItems(AlignItems.STRETCH); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 30; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 100); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER)); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - View anchorView = layoutManager.getChildAt(0); - int offset = anchorView.getLeft(); - assertTrue(offset < 0); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - int orientation = activity.getResources().getConfiguration().orientation; - if (orientation == Configuration.ORIENTATION_PORTRAIT) { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); - } else { - activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // Verify that offset position is preserved for the first visible view after the rotation - View anchorAfterRotate = layoutManager.getChildAt(0); - assertTrue(anchorAfterRotate.getLeft() < 0); - } - - @Test - @FlakyTest - public void testDecoration_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final Drawable drawable = ResourcesCompat.getDrawable(activity.getResources(), - R.drawable.divider, null); - final FlexboxItemDecoration itemDecoration = new FlexboxItemDecoration(activity); - itemDecoration.setDrawable(drawable); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.addItemDecoration(itemDecoration); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 10; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 90, 100); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - View view2 = layoutManager.getChildAt(1); - // 90 (view width) + 10 (divider width) - assertThat(view2.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - View view3 = layoutManager.getChildAt(2); - // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) - assertThat(view3.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - View view4 = layoutManager.getChildAt(3); - // 100 (view height) + 15 (divider height) - assertThat(view4.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 115), 2)); - View view7 = layoutManager.getChildAt(6); - // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) - assertThat(view7.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 230), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(HORIZONTAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view2 = layoutManager.getChildAt(1); - // 90 (view width) - assertThat(view2.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 90), 2)); - view3 = layoutManager.getChildAt(2); - // 90 (view width) + 90(view width) - assertThat(view3.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 180), 2)); - view4 = layoutManager.getChildAt(3); - // 100 (view height) + 15 (divider height) - assertThat(view4.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 115), 2)); - view7 = layoutManager.getChildAt(6); - // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) - assertThat(view7.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 230), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(VERTICAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view2 = layoutManager.getChildAt(1); - // 90 (view width) + 10 (divider width) - assertThat(view2.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - view3 = layoutManager.getChildAt(2); - // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) - assertThat(view3.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - view4 = layoutManager.getChildAt(3); - // 100 (view height) - assertThat(view4.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - view7 = layoutManager.getChildAt(6); - // 100 (view height) + 100 (view height) - assertThat(view7.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - } - - @Test - @FlakyTest - public void testDecoration_direction_rowReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final Drawable drawable = ResourcesCompat.getDrawable(activity.getResources(), - R.drawable.divider, null); - final FlexboxItemDecoration itemDecoration = new FlexboxItemDecoration(activity); - itemDecoration.setDrawable(drawable); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW_REVERSE); - recyclerView.setLayoutManager(layoutManager); - recyclerView.addItemDecoration(itemDecoration); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 10; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 90, 100); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW_REVERSE)); - View view1 = layoutManager.getChildAt(0); - View view2 = layoutManager.getChildAt(1); - // 90 (view width) + 10 (divider width) - assertThat(view1.getRight() - view2.getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - View view3 = layoutManager.getChildAt(2); - // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) - assertThat(view1.getRight() - view3.getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - View view4 = layoutManager.getChildAt(3); - // 100 (view height) + 15 (divider height) - assertThat(view4.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 115), 2)); - View view7 = layoutManager.getChildAt(6); - // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) - assertThat(view7.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 230), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(HORIZONTAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view1 = layoutManager.getChildAt(0); - view2 = layoutManager.getChildAt(1); - // 90 (view width) - assertThat(view1.getRight() - view2.getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 90), 2)); - view3 = layoutManager.getChildAt(2); - // 90 (view width) + 90(view width) - assertThat(view1.getRight() - view3.getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 180), 2)); - view4 = layoutManager.getChildAt(3); - // 100 (view height) + 15 (divider height) - assertThat(view4.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 115), 2)); - view7 = layoutManager.getChildAt(6); - // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) - assertThat(view7.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 230), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(VERTICAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view1 = layoutManager.getChildAt(0); - view2 = layoutManager.getChildAt(1); - // 90 (view width) + 10 (divider width) - assertThat(view1.getRight() - view2.getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - view3 = layoutManager.getChildAt(2); - // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) - assertThat(view1.getRight() - view3.getRight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - view4 = layoutManager.getChildAt(3); - // 100 (view height) - assertThat(view4.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - view7 = layoutManager.getChildAt(6); - // 100 (view height) + 100 (view height) - assertThat(view7.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - } - - @Test - @FlakyTest - public void testDecoration_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final Drawable drawable = ResourcesCompat.getDrawable(activity.getResources(), - R.drawable.divider, null); - final FlexboxItemDecoration itemDecoration = new FlexboxItemDecoration(activity); - itemDecoration.setDrawable(drawable); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.addItemDecoration(itemDecoration); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 10; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 90, 65); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - View view2 = layoutManager.getChildAt(1); - // 65 (view height) + 15 (divider height) - assertThat(view2.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80), 2)); - View view3 = layoutManager.getChildAt(2); - // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) - assertThat(view3.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 160), 2)); - View view4 = layoutManager.getChildAt(3); - // 90 (view width) + 10 (divider width) - assertThat(view4.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - View view7 = layoutManager.getChildAt(6); - // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) - assertThat(view7.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(HORIZONTAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view2 = layoutManager.getChildAt(1); - // 65 (view height) + 15 (divider height) - assertThat(view2.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 80), 2)); - view3 = layoutManager.getChildAt(2); - // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) - assertThat(view3.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 160), 2)); - view4 = layoutManager.getChildAt(3); - // 90 (view width) - assertThat(view4.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 90), 2)); - view7 = layoutManager.getChildAt(6); - // 90 (view width) + 90 (view width) - assertThat(view7.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 180), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(VERTICAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view2 = layoutManager.getChildAt(1); - // 65 (view height) - assertThat(view2.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 65), 2)); - view3 = layoutManager.getChildAt(2); - // 65 (view height) + 65 (view height) - assertThat(view3.getTop(), isEqualAllowingError(TestUtil.dpToPixel(activity, 130), 2)); - view4 = layoutManager.getChildAt(3); - // 90 (view width) + 10 (divider width) - assertThat(view4.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - view7 = layoutManager.getChildAt(6); - // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) - assertThat(view7.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - } - - @Test - @FlakyTest - public void testDecoration_direction_columnReverse() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final Drawable drawable = ResourcesCompat.getDrawable(activity.getResources(), - R.drawable.divider, null); - final FlexboxItemDecoration itemDecoration = new FlexboxItemDecoration(activity); - itemDecoration.setDrawable(drawable); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN_REVERSE); - recyclerView.setLayoutManager(layoutManager); - recyclerView.addItemDecoration(itemDecoration); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 10; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 90, 65); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN_REVERSE)); - View view1 = layoutManager.getChildAt(0); - View view2 = layoutManager.getChildAt(1); - // 65 (view height) + 15 (divider height) - assertThat(view1.getTop() - view2.getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80), 2)); - View view3 = layoutManager.getChildAt(2); - // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) - assertThat(view1.getTop() - view3.getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 160), 2)); - View view4 = layoutManager.getChildAt(3); - // 90 (view width) + 10 (divider width) - assertThat(view4.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - View view7 = layoutManager.getChildAt(6); - // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) - assertThat(view7.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(HORIZONTAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view1 = layoutManager.getChildAt(0); - view2 = layoutManager.getChildAt(1); - // 65 (view height) + 15 (divider height) - assertThat(view1.getTop() - view2.getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 80), 2)); - view3 = layoutManager.getChildAt(2); - // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) - assertThat(view1.getTop() - view3.getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 160), 2)); - view4 = layoutManager.getChildAt(3); - // 90 (view width) - assertThat(view4.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 90), 2)); - view7 = layoutManager.getChildAt(6); - // 90 (view width) + 90 (view width) - assertThat(view7.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 180), 2)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.removeItemDecoration(itemDecoration); - itemDecoration.setOrientation(VERTICAL); - recyclerView.addItemDecoration(itemDecoration); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - view1 = layoutManager.getChildAt(0); - view2 = layoutManager.getChildAt(1); - // 65 (view height) - assertThat(view1.getTop() - view2.getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 65), 2)); - view3 = layoutManager.getChildAt(2); - // 65 (view height) + 65 (view height) - assertThat(view1.getTop() - view3.getTop(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 130), 2)); - view4 = layoutManager.getChildAt(3); - // 90 (view width) + 10 (divider width) - assertThat(view4.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100), 2)); - view7 = layoutManager.getChildAt(6); - // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) - assertThat(view7.getLeft(), isEqualAllowingError(TestUtil.dpToPixel(activity, 200), 2)); - } - - @Test - @FlakyTest - public void testScrollToPosition_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 150; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Flex line 1: 3 items - // Flex line 2: 3 items - // .... - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - - final int scrollTo = 42; - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.scrollToPosition(scrollTo); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // Each flex line should have 3 items in this test's configuration. - // After scrolling to the position of 42 (% 3 == 0), the first visible item should - // be the 42'th item - assertThat(((TextView) layoutManager.getChildAt(0)).getText().toString(), - is(String.valueOf(scrollTo + 1))); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // Scroll enough that 42'th item becomes off screen to the top - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.scrollToPosition(scrollTo); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // The 42'th item should be at the bottom of the screen. - // The last visible item should be 42 + 3 since the last visible item is at the last - // of the bottom flex line - assertThat(((TextView) layoutManager.getChildAt( - layoutManager.getChildCount() - 1)).getText().toString(), - is(String.valueOf(scrollTo + 3))); - } - - @Test - @FlakyTest - public void testScrollToPosition_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 150; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // Flex line 1: 3 items - // Flex line 2: 3 items - // .... - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - - final int scrollTo = 42; - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.scrollToPosition(scrollTo); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // Each flex line should have 3 items in this test's configuration. - // After scrolling to the position of 42 (% 3 == 0), the first visible item should - // be the 42'th item - assertThat(((TextView) layoutManager.getChildAt(0)).getText().toString(), - is(String.valueOf(scrollTo + 1))); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - // Scroll enough that 42'th item becomes off screen to the left - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.scrollToPosition(scrollTo); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // The 42'th item should be at the bottom of the screen. - // The last item should be the (42 + 3)'th item since it should be also the last item in the - // bottom flex line - assertThat(((TextView) layoutManager.getChildAt( - layoutManager.getChildCount() - 1)).getText().toString(), - is(String.valueOf(scrollTo + 3))); - } - - @Test - @FlakyTest - public void testScrollToPosition_scrollToNewItem_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 6; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - // There should be 2 lines - // RecyclerView width: 320, height: 240. - // Flex line 1: 3 items - // Flex line 2: 3 items - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - layoutManager.scrollToPosition(adapter.getItemCount() - 1); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // ChildCount (visible views) should be 6 + 1, - // which before fixing https://github.com/google/flexbox-layout/issues/206, only the new - // item was visible - assertThat(layoutManager.getChildCount(), is(7)); - } - - @Test - @FlakyTest - public void testScrollToPosition_scrollToNewItem_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 6; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - // There should be 2 lines - // RecyclerView width: 320, height: 240. - // Flex line 1: 3 items - // Flex line 2: 3 items - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - layoutManager.scrollToPosition(adapter.getItemCount() - 1); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // ChildCount (visible views) should be 6 + 1, - // which before fixing https://github.com/google/flexbox-layout/issues/206, only the new - // item was visible - assertThat(layoutManager.getChildCount(), is(7)); - } - - @Test - @FlakyTest - public void testScrollToStart_secondLineHasMoreItemThanFirst() throws Throwable { - // This test verifies the case that the first line disappears as the user first scrolls to - // the bottom enough that the first line becomes invisible then the user scrolls toward - // start on the condition that the second line has more items than the first line - // https://github.com/google/flexbox-layout/issues/228 - - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams first = new FlexboxLayoutManager.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, TestUtil.dpToPixel(activity, 70)); - adapter.addItem(first); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - // The first line has 1 item, the following lines have more than 1 items - // RecyclerView width: 320, height: 240. - // Flex line 1: 1 items - // Flex line 2: 3 items - // Flex line 3: 3 items - // ... - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - // At this moment, the first item should become invisible - View firstView = layoutManager.getChildAt(0); - assertThat(((TextView) firstView).getText().toString(), is(not("1"))); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - - // The first visible item should be "1", which before fixing the issue - // https://github.com/google/flexbox-layout/issues/228, the first line disappeared. - firstView = layoutManager.getChildAt(0); - assertThat(((TextView) firstView).getText().toString(), is("1")); - } - - @Test - @FlakyTest - public void testNestedRecyclerViews_direction_row() throws Throwable { - // This test verifies the nested RecyclerViews. - // The outer RecyclerView scrolls vertical using LinearLayoutManager. - // The inner RecyclerViews use FlexboxLayoutManager with flexDirection == ROW and - // height of the RecyclerView is set to "wrap_content", which before fixing - // https://github.com/google/flexbox-layout/issues/208, the height of the inner - // RecyclerViews were set to 0. - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final LinearLayoutManager outerLayoutManager = new LinearLayoutManager(activity); - - // Give the inner adapter item count enough so that inner RecyclerView with - // FlexboxLayoutManager wraps its items - int innerAdapterItemCount = 20; - final NestedOuterAdapter adapter = new NestedOuterAdapter(activity,FlexDirection.ROW, - innerAdapterItemCount, R.layout.viewholder_inner_recyclerview); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - outerLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); - recyclerView.setLayoutManager(outerLayoutManager); - recyclerView.setAdapter(adapter); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - NestedOuterAdapter.OuterViewHolder viewHolder = adapter.getViewHolder(0); - RecyclerView innerRecyclerView = viewHolder.mInnerRecyclerView; - assertThat(innerRecyclerView.getHeight(), is(not(0))); - - // This assertion verifies that inner RecylerView displays the entire items including - // wrapped lines to verify the issue that nested RecyclerView with FlexboxLayoutManager - // only displayed one line https://github.com/google/flexbox-layout/issues/290 - assertThat(((FlexboxLayoutManager)innerRecyclerView - .getLayoutManager()).findLastVisibleItemPosition(), is(innerAdapterItemCount - 1)); - } - - @Test - @FlakyTest - public void testNestedRecyclerViews_direction_column() throws Throwable { - // This test verifies the nested RecyclerViews. - // The outer RecyclerView scrolls horizontally using LinearLayoutManager. - // The inner RecyclerViews use FlexboxLayoutManager with flexDirection == COLUMN and - // width of the RecyclerView is set to "wrap_content", which before fixing - // https://github.com/google/flexbox-layout/issues/208, the width of the inner - // RecyclerViews were set to 0. - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final LinearLayoutManager outerLayoutManager = new LinearLayoutManager(activity); - - // Give the inner adapter item count enough so that inner RecyclerView with - // FlexboxLayoutManager wraps its items - int innerAdapterItemCount = 20; - final NestedOuterAdapter adapter = new NestedOuterAdapter(activity, FlexDirection.COLUMN, - innerAdapterItemCount, R.layout.viewholder_inner_recyclerview_wrap_horizontally); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - outerLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); - recyclerView.setLayoutManager(outerLayoutManager); - recyclerView.setAdapter(adapter); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - NestedOuterAdapter.OuterViewHolder viewHolder = adapter.getViewHolder(0); - RecyclerView innerRecyclerView = viewHolder.mInnerRecyclerView; - assertThat(innerRecyclerView.getWidth(), is(not(0))); - - // This assertion verifies that inner RecylerView displays the entire items including - // wrapped lines to verify the issue that nested RecyclerView with FlexboxLayoutManager - // only displayed one line https://github.com/google/flexbox-layout/issues/290 - assertThat(((FlexboxLayoutManager)innerRecyclerView - .getLayoutManager()).findLastVisibleItemPosition(), is(innerAdapterItemCount - 1)); - } - - @Test - @FlakyTest - public void testFindVisibleChild_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 75); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // At first three completely visible lines. - // Flex line 1, item count 3 (0, 1, 2) - // Flex line 2, item count 3 (3, 4, 5) - // Flex line 3, item count 3 (6, 7, 8) - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), is(0)); - assertThat(layoutManager.findFirstVisibleItemPosition(), is(0)); - assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), is(8)); - assertThat(layoutManager.findLastVisibleItemPosition(), is(11)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), is(42)); - assertThat(layoutManager.findFirstVisibleItemPosition(), is(39)); - assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), is(49)); - assertThat(layoutManager.findLastVisibleItemPosition(), is(49)); - } - - @Test - @FlakyTest - public void testFindVisibleChild_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 75); - adapter.addItem(lp); - } - // RecyclerView width: 320, height: 240. - // At first three completely visible lines. - // Flex line 1, item count 3 (0, 1, 2) - // Flex line 2, item count 3 (3, 4, 5) - // Flex line 3, item count 3 (6, 7, 8) - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), is(0)); - assertThat(layoutManager.findFirstVisibleItemPosition(), is(0)); - assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), is(8)); - assertThat(layoutManager.findLastVisibleItemPosition(), is(11)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), is(42)); - assertThat(layoutManager.findFirstVisibleItemPosition(), is(39)); - assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), is(49)); - assertThat(layoutManager.findLastVisibleItemPosition(), is(49)); - } - - @Test - @FlakyTest - public void testDrawDirtyFlexLine_direction_row() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 30; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 75); - adapter.addItem(lp); - } - } - }); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // First scroll to the bottom, then add a new item that isn't visible at this moment. - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 40, 75); - adapter.addItem(0, lp); - } - }); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - View firstVisible = layoutManager.getChildAt(0); - assertThat(firstVisible.getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 40))); - assertThat(firstVisible.getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 75))); - } - - @Test - @FlakyTest - public void testDrawDirtyFlexLine_direction_column() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - - for (int i = 0; i < 30; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 75); - adapter.addItem(lp); - } - } - }); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // First scroll to the bottom, then add a new item that isn't visible at this moment. - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 120); - adapter.addItem(0, lp); - } - }); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - View firstVisible = layoutManager.getChildAt(0); - assertThat(firstVisible.getWidth(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(firstVisible.getHeight(), - isEqualAllowingError(TestUtil.dpToPixel(activity, 120))); - } - - @Test - @FlakyTest - public void testDrawDirtyFlexLine_multi_viewTypes_direction_row() throws Throwable { - // This test verifies https://github.com/google/flexbox-layout/issues/280 - // the position of the view type is shifted if a new item is inserted before the - // view which has the special viewType and that view isn't visible at the time the item - // was inserted. - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapterMultiViewTypes adapter = new TestAdapterMultiViewTypes(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - RecyclerView parent = activity.findViewById(R.id.recyclerview); - TextView matchParentText = (TextView) layoutManager - .getChildAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT); - assertThat(matchParentText.getWidth(), is(parent.getWidth())); - assertThat(matchParentText.getText().toString(), - is(String.valueOf(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT + 1))); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - - final int insertedValue = 10; - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - TestAdapterMultiViewTypes.Item item = new TestAdapterMultiViewTypes.Item(); - item.value = insertedValue; - // Insert an item before the position that has a special viewType - adapter.addItemAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT - 1, item); - } - }); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - // Since a new item is inserted before the position, the index at the view who has the - // special viewType should be shifted. - matchParentText = (TextView) layoutManager - .getChildAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT + 1); - assertThat(matchParentText.getWidth(), is(parent.getWidth())); - assertThat(matchParentText.getText().toString(), - is(String.valueOf(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT + 1))); - - // The position of this view is the old position who had the special viewType, but - // now the viewType should be a normal one - TextView textView = (TextView) layoutManager - .getChildAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT - 1); - assertThat(textView.getWidth(), lessThan(parent.getWidth())); - assertThat(textView.getText().toString(), is(String.valueOf(insertedValue))); - } - - @Test - @FlakyTest - public void testChildrenSizeWithMargin() throws Throwable { - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 30); - lp.setMargins(10, 30, 20, 40); - adapter.addItem(lp); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - TextView text = (TextView) layoutManager.getChildAt(0); - FlexboxLayoutManager.LayoutParams lp = - (FlexboxLayoutManager.LayoutParams) text.getLayoutParams(); - assertThat(text.getHeight(), isEqualAllowingError(TestUtil.dpToPixel(activity, 30))); - assertThat(text.getWidth(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - assertThat(lp.getMarginLeft(), isEqualAllowingError(10)); - assertThat(lp.getMarginRight(), isEqualAllowingError(20)); - assertThat(lp.getMarginTop(), isEqualAllowingError(30)); - assertThat(lp.getMarginBottom(), isEqualAllowingError(40)); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - layoutManager.setFlexDirection(FlexDirection.COLUMN); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - TextView text2 = (TextView) layoutManager.getChildAt(0); - assertThat(text2.getHeight(), isEqualAllowingError(TestUtil.dpToPixel(activity, 30))); - assertThat(text2.getWidth(), isEqualAllowingError(TestUtil.dpToPixel(activity, 100))); - - } - - @Test - @FlakyTest - public void testItemDecoration_withScrolling_direction_row() throws Throwable { - // This test verifies the case that the item decoration set through FlexboxItemDecoration - // is misplaced after the user scrolls the RecyclerView - // https://github.com/google/flexbox-layout/issues/285 - - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final Drawable decorationDrawable = getDrawable(activity, R.drawable.divider); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxItemDecoration decoration = new FlexboxItemDecoration(activity); - decoration.setDrawable(decorationDrawable); - recyclerView.addItemDecoration(decoration); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - // The first line has 1 item, the following lines have more than 1 items - // RecyclerView width: 320, height: 240. - // Flex line 1: 1 items - // Flex line 2: 3 items - // Flex line 3: 3 items - // ... - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.ROW)); - assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(0)), is(0)); - assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(1)), is(0)); - assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(2)), is(0)); - for (FlexLine flexLine : layoutManager.getFlexLines()) { - View firstViewInLine = layoutManager.getChildAt(flexLine.getFirstIndex()); - if (firstViewInLine != null) { - assertThat(layoutManager.getLeftDecorationWidth(firstViewInLine), is(0)); - } - } - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - - // Verify even after the scrolling, decoration values are set correctly - assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(0)), is(0)); - assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(1)), is(0)); - assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(2)), is(0)); - for (FlexLine flexLine : layoutManager.getFlexLines()) { - View firstViewInLine = layoutManager.getChildAt(flexLine.getFirstIndex()); - if (firstViewInLine != null) { - assertThat(layoutManager.getLeftDecorationWidth(firstViewInLine), is(0)); - } - } - } - - @Test - @FlakyTest - public void testItemDecoration_withScrolling_direction_column() throws Throwable { - // This test verifies the case that the item decoration set through FlexboxItemDecoration - // is misplaced after the user scrolls the RecyclerView - // https://github.com/google/flexbox-layout/issues/285 - - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - final Drawable decorationDrawable = getDrawable(activity, R.drawable.divider); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxItemDecoration decoration = new FlexboxItemDecoration(activity); - decoration.setDrawable(decorationDrawable); - recyclerView.addItemDecoration(decoration); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - // The first line has 1 item, the following lines have more than 1 items - // RecyclerView width: 320, height: 240. - // Flex line 1: 1 items - // Flex line 2: 3 items - // Flex line 3: 3 items - // ... - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getFlexDirection(), is(FlexDirection.COLUMN)); - assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(0)), is(0)); - assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(1)), is(0)); - assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(2)), is(0)); - for (FlexLine flexLine : layoutManager.getFlexLines()) { - View firstViewInLine = layoutManager.getChildAt(flexLine.getFirstIndex()); - if (firstViewInLine != null) { - assertThat(layoutManager.getTopDecorationHeight(firstViewInLine), is(0)); - } - } - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - - // Verify even after the scrolling, decoration values are set correctly - assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(0)), is(0)); - assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(1)), is(0)); - assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(2)), is(0)); - for (FlexLine flexLine : layoutManager.getFlexLines()) { - View firstViewInLine = layoutManager.getChildAt(flexLine.getFirstIndex()); - if (firstViewInLine != null) { - assertThat(layoutManager.getTopDecorationHeight(firstViewInLine), is(0)); - } - } - } - - - @Test - @FlakyTest - public void testNotifyItemChange_withPayload() throws Throwable { - // This test verifies the payload is correctly passed to the Adapter in the case - // that notifying an item with payload - // https://github.com/google/flexbox-layout/issues/297 - - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(adapter.getPayloads().size(), is(0)); - - final String payload = "payload"; - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - adapter.changeItemWithPayload(0, payload); - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - - assertThat(adapter.getPayloads().size(), is(1)); - assertThat((String) adapter.getPayloads().get(0), is(payload)); - } - - @Test - @FlakyTest - public void testScrollAlongManAxis_direction_row() throws Throwable { - // This test verifies the scroll along the main axis if the width of the RecyclerView is - // larger than its parent when the main axis direction is horizontal (row) - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.wrapped_recyclerview); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.ROW); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 100, 70); - adapter.addItem(lp); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getChildAt(0).getLeft(), is(0)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_RIGHT, - GeneralLocation.CENTER_LEFT)); - assertThat(layoutManager.getChildAt(0).getLeft(), is(not(0))); - - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, - GeneralLocation.CENTER_RIGHT)); - assertThat(layoutManager.getChildAt(0).getLeft(), is(0)); - } - - @Test - @FlakyTest - public void testScrollAlongManAxis_direction_column() throws Throwable { - // This test verifies the scroll along the main axis if the height of the RecyclerView is - // larger than its parent when the main axis direction is vertical (column). - final FlexboxTestActivity activity = mActivityRule.getActivity(); - final FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(activity); - final TestAdapter adapter = new TestAdapter(); - mActivityRule.runOnUiThread(new Runnable() { - @Override - public void run() { - activity.setContentView(R.layout.wrapped_recyclerview_scroll_vertical); - RecyclerView recyclerView = activity.findViewById(R.id.recyclerview); - layoutManager.setFlexDirection(FlexDirection.COLUMN); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setAdapter(adapter); - for (int i = 0; i < 50; i++) { - FlexboxLayoutManager.LayoutParams lp = createLayoutParams(activity, 70, 100); - adapter.addItem(lp); - } - } - }); - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - assertThat(layoutManager.getChildAt(0).getTop(), is(0)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.BOTTOM_CENTER, - GeneralLocation.TOP_CENTER)); - assertThat(layoutManager.getChildAt(0).getTop(), is(not(0))); - - InstrumentationRegistry.getInstrumentation().waitForIdleSync(); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, - GeneralLocation.BOTTOM_CENTER)); - assertThat(layoutManager.getChildAt(0).getTop(), is(0)); - } - - /** - * Creates a new flex item. - * - * @param context the context - * @param width in DP - * @param height in DP - * @return the created {@link FlexboxLayoutManager.LayoutParams} instance - */ - private FlexboxLayoutManager.LayoutParams createLayoutParams(Context context, int width, - int height) { - return new FlexboxLayoutManager.LayoutParams( - TestUtil.dpToPixel(context, width), - TestUtil.dpToPixel(context, height)); - } - - private static ViewAction swipe(CoordinatesProvider from, CoordinatesProvider to) { - return new GeneralSwipeAction(Swipe.FAST, from, to, Press.FINGER); - } - - @SuppressWarnings("deprecation") - private static Drawable getDrawable(Context context, @DrawableRes int resId) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - return context.getResources().getDrawable(resId, null); - } else { - return context.getResources().getDrawable(resId); - } - } -} - diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.kt new file mode 100644 index 0000000..6af186b --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxLayoutManagerTest.kt @@ -0,0 +1,3186 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import android.content.Context +import android.content.pm.ActivityInfo +import android.content.res.Configuration +import android.support.test.InstrumentationRegistry +import android.support.test.espresso.Espresso.onView +import android.support.test.espresso.ViewAction +import android.support.test.espresso.action.* +import android.support.test.espresso.matcher.ViewMatchers.withId +import android.support.test.filters.FlakyTest +import android.support.test.filters.MediumTest +import android.support.test.rule.ActivityTestRule +import android.support.test.runner.AndroidJUnit4 +import android.support.v4.content.res.ResourcesCompat +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView +import android.view.ViewGroup +import android.widget.TextView +import com.google.android.flexbox.* +import com.google.android.flexbox.FlexboxItemDecoration.HORIZONTAL +import com.google.android.flexbox.FlexboxItemDecoration.VERTICAL +import com.google.android.flexbox.test.IsEqualAllowingError.Companion.isEqualAllowingError +import junit.framework.Assert.assertTrue +import org.hamcrest.Matchers.* +import org.hamcrest.core.IsNot.not +import org.junit.Assert.assertThat +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Integration tests for [FlexboxLayoutManager]. + */ +@RunWith(AndroidJUnit4::class) +@MediumTest +class FlexboxLayoutManagerTest { + + @JvmField + @Rule + var activityRule = ActivityTestRule(FlexboxTestActivity::class.java) + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testLoadFromXml() { + val activity = activityRule.activity + activityRule.runOnUiThread { activity.setContentView(R.layout.recyclerview_reverse) } + val recyclerView = activity.findViewById(R.id.recyclerview) + val layoutManager = recyclerView.layoutManager + assertThat(recyclerView, `is`(notNullValue())) + assertThat(layoutManager, `is`(instanceOf(FlexboxLayoutManager::class.java))) + val flexboxLayoutManager = layoutManager as FlexboxLayoutManager + assertThat(flexboxLayoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(flexboxLayoutManager.flexWrap, `is`(FlexWrap.WRAP)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChangeAttributesFromCode() { + val activity = activityRule.activity + activityRule.runOnUiThread { activity.setContentView(R.layout.recyclerview_reverse) } + val recyclerView = activity.findViewById(R.id.recyclerview) + val layoutManager = recyclerView.layoutManager + assertThat(recyclerView, `is`(notNullValue())) + assertThat(layoutManager, `is`(instanceOf(FlexboxLayoutManager::class.java))) + val flexboxLayoutManager = layoutManager as FlexboxLayoutManager + assertThat(flexboxLayoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(flexboxLayoutManager.flexWrap, `is`(FlexWrap.WRAP)) + + flexboxLayoutManager.flexDirection = FlexDirection.COLUMN + flexboxLayoutManager.flexWrap = FlexWrap.NOWRAP + flexboxLayoutManager.justifyContent = JustifyContent.CENTER + flexboxLayoutManager.alignItems = AlignItems.FLEX_END + assertThat(flexboxLayoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(flexboxLayoutManager.flexWrap, `is`(FlexWrap.NOWRAP)) + assertThat(flexboxLayoutManager.justifyContent, `is`(JustifyContent.CENTER)) + assertThat(flexboxLayoutManager.alignItems, `is`(AlignItems.FLEX_END)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAddViewHolders_direction_row_not_wrapped() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + adapter.addItem(createLayoutParams(activity, 120, 80)) + adapter.addItem(createLayoutParams(activity, 120, 80)) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + assertThat(layoutManager.flexWrap, `is`(FlexWrap.WRAP)) + // Only 2 items are added and all items should be attached (visible). So both + // getChildCount and getFlexItemCount(including detached items) should return the same value + assertThat(layoutManager.flexItemCount, `is`(2)) + assertThat(layoutManager.childCount, `is`(2)) + assertThat(layoutManager.flexLines.size, `is`(1)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAddViewHolders_direction_row_wrapped() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + adapter.addItem(createLayoutParams(activity, 120, 80)) + adapter.addItem(createLayoutParams(activity, 120, 80)) + adapter.addItem(createLayoutParams(activity, 120, 80)) + adapter.addItem(createLayoutParams(activity, 120, 80)) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // 4 items are added and all items should be attached (visible). So both + // getChildCount and getFlexItemCount(including detached items) should return the same value + assertThat(layoutManager.flexItemCount, `is`(4)) + assertThat(layoutManager.childCount, `is`(4)) + assertThat(layoutManager.flexLines.size, `is`(2)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAddViewHolders_direction_row_partOfItems_detached() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + // RecyclerView width: 320, height: 240. + // Computed FlexContainer width: 320, height: 450 + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // In total 9 items are added but the seventh item and the items follow aren't attached + // (invisible) so are not included in the count of of the getChildCount. + assertThat(layoutManager.flexItemCount, `is`(9)) + assertThat(layoutManager.childCount, `is`(6)) + + // At first only the visible area of the flex lines are calculated + assertThat(layoutManager.flexLines.size, `is`(3)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAddViewHolders_direction_row_scrollVertically() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + adapter.addItem(createLayoutParams(activity, 150, 90)) + // RecyclerView width: 320, height: 240. + // Computed FlexContainer width: 320, height: 450 + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // In total 9 items are added but the seventh item and the items follow aren't attached + // (invisible) so are not included in the count of of the getChildCount. + assertThat(layoutManager.flexItemCount, `is`(9)) + assertThat(layoutManager.childCount, `is`(6)) + // At first only the visible area of the flex lines are calculated + assertThat(layoutManager.flexLines.size, `is`(3)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + assertThat(layoutManager.flexItemCount, `is`(9)) + // The RecyclerView is swiped to top until it reaches the bottom of the view. + // The number of the visible views should be 5 + assertThat(layoutManager.childCount, `is`(5)) + // Since the RecyclerView is swiped to the bottom, all flex lines should be calculated + // by now + assertThat(layoutManager.flexLines.size, `is`(5)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexGrow() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 150, 130) + lp1.flexGrow = 1.0f + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 150, 130) + lp2.flexGrow = 1.0f + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 150, 130) + lp3.flexGrow = 1.0f + adapter.addItem(lp3) + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // The flexGrow parameters for all LayoutParams are set to 1.0, expecting each child to + // fill the horizontal remaining space + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(2)) + assertThat(layoutManager.getChildAt(0).width, isEqualAllowingError(activity.dpToPixel(160))) + assertThat(layoutManager.getChildAt(1).width, isEqualAllowingError(activity.dpToPixel(160))) + assertThat(layoutManager.getChildAt(2).width, isEqualAllowingError(activity.dpToPixel(320))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAddViewHolders_direction_column_partOfItems_detached() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + + layoutManager.flexDirection = FlexDirection.COLUMN + // RecyclerView width: 320, height: 240. + // Computed FlexContainer width: 450, height: 240 + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // In total 9 items are added but the seventh item and the items after aren't attached + // (invisible) so are not included in the count of of the getChildCount. + assertThat(layoutManager.flexItemCount, `is`(9)) + assertThat(layoutManager.childCount, `is`(6)) + + // At first only the visible area of the flex lines are calculated + assertThat(layoutManager.flexLines.size, `is`(3)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAddViewHolders_direction_column_scrollHorizontally() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + adapter.addItem(createLayoutParams(activity, 120, 100)) + + layoutManager.flexDirection = FlexDirection.COLUMN + // RecyclerView width: 320, height: 240. + // Computed FlexContainer width: 500, height: 240 + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // In total 9 items are added but the seventh item and the items follow aren't attached + // (invisible) so are not included in the count of of the getChildCount. + assertThat(layoutManager.flexItemCount, `is`(9)) + assertThat(layoutManager.childCount, `is`(6)) + // At first only the visible area of the flex lines are calculated + assertThat(layoutManager.flexLines.size, `is`(3)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + assertThat(layoutManager.flexItemCount, `is`(9)) + // The RecyclerView is swiped to top until it reaches the right edge of the view. + // The number of the visible views should be 5 + assertThat(layoutManager.childCount, `is`(5)) + // Since the RecyclerView is swiped to the bottom, all flex lines should be calculated + // by now + assertThat(layoutManager.flexLines.size, `is`(5)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexStart_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.justifyContent = JustifyContent.FLEX_START + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_START)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(150))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexEnd_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.justifyContent = JustifyContent.FLEX_END + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_END)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(170))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(220))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(220))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(320))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_center_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.justifyContent = JustifyContent.CENTER + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.justifyContent, `is`(JustifyContent.CENTER)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(85))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(135))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(235))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.justifyContent = JustifyContent.SPACE_AROUND + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(28))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(78))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(242))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(292))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.justifyContent = JustifyContent.SPACE_BETWEEN + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(320))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexStart_direction_rowReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.ROW_REVERSE + layoutManager.justifyContent = JustifyContent.FLEX_START + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_START)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(320))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(220))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(170))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(220))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexEnd_direction_rowReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.ROW_REVERSE + layoutManager.justifyContent = JustifyContent.FLEX_END + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_END)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(150))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(50))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_center_direction_rowReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.ROW_REVERSE + layoutManager.justifyContent = JustifyContent.CENTER + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.CENTER)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(235))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(85))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(135))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_direction_rowReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.ROW_REVERSE + layoutManager.justifyContent = JustifyContent.SPACE_AROUND + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(242))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(292))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(28))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(78))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_direction_rowReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 50, 100) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.ROW_REVERSE + layoutManager.justifyContent = JustifyContent.SPACE_BETWEEN + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).left, isEqualAllowingError(activity.dpToPixel(270))) + assertThat(layoutManager.getChildAt(0).right, isEqualAllowingError(activity.dpToPixel(320))) + assertThat(layoutManager.getChildAt(1).left, isEqualAllowingError(activity.dpToPixel(135))) + assertThat(layoutManager.getChildAt(1).right, isEqualAllowingError(activity.dpToPixel(185))) + assertThat(layoutManager.getChildAt(2).left, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(2).right, isEqualAllowingError(activity.dpToPixel(50))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexStart_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.justifyContent = JustifyContent.FLEX_START + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_START)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(150))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexEnd_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.justifyContent = JustifyContent.FLEX_END + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_END)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(90))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(140))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(140))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(190))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(190))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(240))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_center_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.justifyContent = JustifyContent.CENTER + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.CENTER)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(45))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(95))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(195))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.justifyContent = JustifyContent.SPACE_AROUND + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(15))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(65))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(175))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(225))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.justifyContent = JustifyContent.SPACE_BETWEEN + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(190))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(240))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexStart_direction_columnReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN_REVERSE + layoutManager.justifyContent = JustifyContent.FLEX_START + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_START)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(190))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(240))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(140))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(190))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(90))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(140))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_flexEnd_direction_columnReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN_REVERSE + layoutManager.justifyContent = JustifyContent.FLEX_END + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.FLEX_END)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(150))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(50))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_center_direction_columnReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN_REVERSE + layoutManager.justifyContent = JustifyContent.CENTER + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.CENTER)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(195))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(45))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(95))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceAround_direction_columnReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN_REVERSE + layoutManager.justifyContent = JustifyContent.SPACE_AROUND + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_AROUND)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(175))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(225))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(15))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(65))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testJustifyContent_spaceBetween_direction_columnReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 50) + adapter.addItem(lp3) + layoutManager.flexDirection = FlexDirection.COLUMN_REVERSE + layoutManager.justifyContent = JustifyContent.SPACE_BETWEEN + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN_REVERSE)) + assertThat(layoutManager.justifyContent, `is`(JustifyContent.SPACE_BETWEEN)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(190))) + assertThat(layoutManager.getChildAt(0).bottom, isEqualAllowingError(activity.dpToPixel(240))) + assertThat(layoutManager.getChildAt(1).top, isEqualAllowingError(activity.dpToPixel(95))) + assertThat(layoutManager.getChildAt(1).bottom, isEqualAllowingError(activity.dpToPixel(145))) + assertThat(layoutManager.getChildAt(2).top, isEqualAllowingError(activity.dpToPixel(0))) + assertThat(layoutManager.getChildAt(2).bottom, isEqualAllowingError(activity.dpToPixel(50))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testLargeItem_scrollFast_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..199) { + val lp = createLayoutParams(activity, 100, 50) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + assertThat(layoutManager.flexItemCount, `is`(200)) + // Only the visible items + assertThat(layoutManager.childCount, `is`(not(200))) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // Should be scrolled to the bottom by now + assertThat(layoutManager.flexItemCount, `is`(200)) + // Only the visible items + assertThat(layoutManager.childCount, `is`(not(200))) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + // Should be scrolled to the top + assertThat(layoutManager.flexItemCount, `is`(200)) + // Only the visible items + assertThat(layoutManager.childCount, `is`(not(200))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testLargeItem_scrollFast_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..199) { + val lp = createLayoutParams(activity, 70, 80) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.flexItemCount, `is`(200)) + // Only the visible items + assertThat(layoutManager.childCount, `is`(not(200))) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + + // Should be scrolled to the right edge by now + assertThat(layoutManager.flexItemCount, `is`(200)) + // Only the visible items + assertThat(layoutManager.childCount, `is`(not(200))) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + // Should be scrolled to the left edge by now + assertThat(layoutManager.flexItemCount, `is`(200)) + // Only the visible items + assertThat(layoutManager.childCount, `is`(not(200))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_stretch_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + layoutManager.alignItems = AlignItems.STRETCH + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 70, 80) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 70, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 70, 30) + adapter.addItem(lp3) + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + assertThat(layoutManager.alignItems, `is`(AlignItems.STRETCH)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + // Verify all items heights are stretched + assertThat(layoutManager.getChildAt(0).height, isEqualAllowingError(activity.dpToPixel(80))) + assertThat(layoutManager.getChildAt(1).height, isEqualAllowingError(activity.dpToPixel(80))) + assertThat(layoutManager.getChildAt(2).height, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignItems_stretch_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.alignItems = AlignItems.STRETCH + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 80, 70) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 70) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 30, 70) + adapter.addItem(lp3) + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.alignItems, `is`(AlignItems.STRETCH)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + // Verify all items widths are stretched + assertThat(layoutManager.getChildAt(0).width, isEqualAllowingError(activity.dpToPixel(80))) + assertThat(layoutManager.getChildAt(1).width, isEqualAllowingError(activity.dpToPixel(80))) + assertThat(layoutManager.getChildAt(2).width, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignSelf_stretch_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + layoutManager.alignItems = AlignItems.FLEX_START + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 70, 80) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 70, 50) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 70, 30) + lp3.alignSelf = AlignSelf.STRETCH + adapter.addItem(lp3) + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + assertThat(layoutManager.alignItems, `is`(AlignItems.FLEX_START)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + // Verify the item whose align self is set to stretch is stretched + assertThat(layoutManager.getChildAt(0).height, isEqualAllowingError(activity.dpToPixel(80))) + assertThat(layoutManager.getChildAt(1).height, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(2).height, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testAlignSelf_stretch_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.alignItems = AlignItems.FLEX_START + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 80, 70) + adapter.addItem(lp1) + val lp2 = createLayoutParams(activity, 50, 70) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 30, 70) + lp3.alignSelf = AlignSelf.STRETCH + adapter.addItem(lp3) + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.alignItems, `is`(AlignItems.FLEX_START)) + assertThat(layoutManager.flexItemCount, `is`(3)) + assertThat(layoutManager.flexLines.size, `is`(1)) + // Verify the item whose align self is set to stretch is stretched + assertThat(layoutManager.getChildAt(0).width, isEqualAllowingError(activity.dpToPixel(80))) + assertThat(layoutManager.getChildAt(1).width, isEqualAllowingError(activity.dpToPixel(50))) + assertThat(layoutManager.getChildAt(2).width, isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testStretchViews_from_middle_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + layoutManager.alignItems = AlignItems.STRETCH + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + val lp = createLayoutParams(activity, 70, 80) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + assertThat(layoutManager.alignItems, `is`(AlignItems.STRETCH)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + activityRule.runOnUiThread { + layoutManager.alignItems = AlignItems.STRETCH + val lp = createLayoutParams(activity, 70, 20) + // Add an item whose height is less than the other items. + // But with alignItems set to stretch, the height of the item should be stretched + adapter.addItem(lp) + adapter.notifyDataSetChanged() + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.getChildAt(layoutManager.childCount - 1).height, + isEqualAllowingError(activity.dpToPixel(80))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testStretchViews_from_middle_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.alignItems = AlignItems.STRETCH + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + val lp = createLayoutParams(activity, 70, 50) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.alignItems, `is`(AlignItems.STRETCH)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, + GeneralLocation.TOP_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, + GeneralLocation.TOP_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, + GeneralLocation.TOP_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_RIGHT, + GeneralLocation.TOP_LEFT)) + activityRule.runOnUiThread { + layoutManager.alignItems = AlignItems.STRETCH + val lp = createLayoutParams(activity, 20, 50) + // Add an item whose width is less than the other items. + // But with alignItems set to stretch, the width of the item should be stretched + adapter.addItem(lp) + adapter.notifyDataSetChanged() + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.getChildAt(layoutManager.childCount - 1).width, + isEqualAllowingError(activity.dpToPixel(70))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToTop_middleItem_as_anchorPosition() { + // There was an issue that the anchor position was based on the first item in the first + // visible flex line when scrolling to top. But the anchor position should be based on the + // flex line position (view which has the minimum top position in the same flex line) + // This test verifies the issue is fixed. + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val positionInSecondLine = 6 + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + layoutManager.alignItems = AlignItems.FLEX_START + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + var lp = createLayoutParams(activity, 70, 80) + if (i == positionInSecondLine) { + // Change the height from other items in the second line, not the first item + // in the second line + lp = createLayoutParams(activity, 70, 130) + } + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Each line has 4 (320 / 70) flex items and 12 (50 / 4) lines in total + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // By this moment reached to the bottom + + // Now scrolling to the top to see if the views in the first flex line is correctly placed + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + + assertThat(layoutManager.getChildAt(positionInSecondLine).bottom, + isEqualAllowingError(activity.dpToPixel(210))) // 80 + 130 + // Verify that the view in the same line's cross axis position is correct + assertThat(layoutManager.getChildAt(positionInSecondLine - 1).bottom, + isEqualAllowingError(activity.dpToPixel(160))) // 80 + 80 + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToLeft_middleItem_as_anchorPosition() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val positionInSecondLine = 6 + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.alignItems = AlignItems.FLEX_START + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + var lp = createLayoutParams(activity, 80, 50) + if (i == positionInSecondLine) { + // Change the width from other items in the second line, not the first item + // in the second line + lp = createLayoutParams(activity, 130, 50) + } + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Each line has 4 (240 / 50) flex items and 12 (50 / 4) lines in total + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + // By this moment reached to the right edge + + // Now scrolling to the left to see if the views in the first flex line is correctly placed + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + + assertThat(layoutManager.getChildAt(positionInSecondLine).right, + isEqualAllowingError(activity.dpToPixel(210))) // 80 + 130 + // Verify that the view in the same line's cross axis position is correct + assertThat(layoutManager.getChildAt(positionInSecondLine - 1).right, + isEqualAllowingError(activity.dpToPixel(160))) // 80 + 80 + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToBottom_middleItem_as_anchorPosition() { + // There was an issue that the anchor position was based on the last item in the last + // visible flex line when scrolling to bottom. But the anchor position should be based on + // the flex line position (view which has the maximum bottom position in the same flex line) + // This test verifies the issue is fixed. + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val positionInSecondBottomLine = 45 + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + var lp = createLayoutParams(activity, 70, 80) + if (i == positionInSecondBottomLine) { + // Change the height from other items in the second bottom line + lp = createLayoutParams(activity, 70, 130) + } + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Each line has 4 (320 / 70) flex items and 12 (50 / 4) lines in total + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // By this moment reached to the bottom + + // 4 comes from the number of flex items - positionInSecondBottomLine + val recyclerView = activity.findViewById(R.id.recyclerview) + val anchorView = layoutManager.getChildAt(layoutManager.childCount - 4) + assertThat(recyclerView.bottom - anchorView.top, + isEqualAllowingError(activity.dpToPixel(210))) // 80 + 130 + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToTop_direction_rowReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW_REVERSE + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + val lp = createLayoutParams(activity, 70, 80) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Each line has 4 (320 / 70) flex items and 12 (50 / 4) lines in total + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // By this moment reached to the bottom + + // Now scrolling to the top to see if the views in the first flex line is correctly placed + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + + assertThat((layoutManager.getChildAt(0) as TextView).text.toString(), `is`("1")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexGrow_only_oneItem_has_positive_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..3) { + val lp = createLayoutParams(activity, 100, 80) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Flex line 1: 3 items + // Flex line 2: 1 item + // Give the second item in the first line a positive flex grow + adapter.getItemAt(0).setHeight(activity.dpToPixel(140)) + adapter.getItemAt(1).flexGrow = 1.0f + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + // Verify the vertical position (cross size) of the second line is correctly positioned + assertThat(layoutManager.getChildAt(3).top, isEqualAllowingError(activity.dpToPixel(140))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFlexGrow_only_oneItem_has_positive_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..3) { + val lp = createLayoutParams(activity, 70, 70) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Flex line 1: 3 items + // Flex line 2: 1 item + // Give the second item in the first line a positive flex grow + adapter.getItemAt(0).setWidth(activity.dpToPixel(120)) + adapter.getItemAt(1).flexGrow = 1.0f + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + // Verify the horizontal position (cross size) of the second line is correctly positioned + assertThat(layoutManager.getChildAt(3).left, isEqualAllowingError(activity.dpToPixel(120))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFirstReferenceView_middleOf_line_used_as_anchor() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + layoutManager.alignItems = AlignItems.FLEX_END + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp1 = createLayoutParams(activity, 100, 80) + adapter.addItem(lp1) + // The second view in the first line has the maximum height in the same line + val lp2 = createLayoutParams(activity, 100, 180) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 80) + adapter.addItem(lp3) + for (i in 0..29) { + val lp = createLayoutParams(activity, 100, 80) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // By this moment reached to the bottom + + // Now scrolling to the top to see if the views in the first flex line is correctly placed + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + + // The top coordinate of the first view should be the height of the second view minus the + // height of the first view (180 - 80) + assertThat(layoutManager.getChildAt(0).top, isEqualAllowingError(activity.dpToPixel(100))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testLastReferenceView_middleOf_line_used_as_anchor() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + layoutManager.alignItems = AlignItems.FLEX_START + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..29) { + val lp = createLayoutParams(activity, 100, 80) + adapter.addItem(lp) + } + val lp1 = createLayoutParams(activity, 100, 80) + adapter.addItem(lp1) + // The second view in the last line has the maximum height in the same line + val lp2 = createLayoutParams(activity, 100, 180) + adapter.addItem(lp2) + val lp3 = createLayoutParams(activity, 100, 80) + adapter.addItem(lp3) + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // By this moment reached to the bottom + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + + // The bottom coordinate of the first view in the last line should be the height of the + // second view in the last line minus the height of the first view in the last line + // (180 - 80) + assertThat(layoutManager.getChildAt(layoutManager.childCount - 2).bottom - layoutManager.getChildAt(layoutManager.childCount - 3).bottom, + isEqualAllowingError(activity.dpToPixel(100))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testRotateScreen_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + layoutManager.alignItems = AlignItems.STRETCH + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..29) { + val lp = createLayoutParams(activity, 100, 100) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.CENTER)) + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + val anchorView = layoutManager.getChildAt(0) + val offset = anchorView.top + assertTrue(offset < 0) + + activityRule.runOnUiThread { + val orientation = activity.resources.configuration.orientation + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + } else { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // Verify that offset position is preserved for the first visible view after the rotation + val anchorAfterRotate = layoutManager.getChildAt(0) + assertTrue(anchorAfterRotate.top < 0) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testRotateScreen_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + layoutManager.alignItems = AlignItems.STRETCH + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..29) { + val lp = createLayoutParams(activity, 100, 100) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER)) + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + val anchorView = layoutManager.getChildAt(0) + val offset = anchorView.left + assertTrue(offset < 0) + + activityRule.runOnUiThread { + val orientation = activity.resources.configuration.orientation + if (orientation == Configuration.ORIENTATION_PORTRAIT) { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + } else { + activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // Verify that offset position is preserved for the first visible view after the rotation + val anchorAfterRotate = layoutManager.getChildAt(0) + assertTrue(anchorAfterRotate.left < 0) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDecoration_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val drawable = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + val itemDecoration = FlexboxItemDecoration(activity) + itemDecoration.setDrawable(drawable) + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.addItemDecoration(itemDecoration) + recyclerView.adapter = adapter + + for (i in 0..9) { + val lp = createLayoutParams(activity, 90, 100) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + var view2 = layoutManager.getChildAt(1) + // 90 (view width) + 10 (divider width) + assertThat(view2.left, isEqualAllowingError(activity.dpToPixel(100))) + var view3 = layoutManager.getChildAt(2) + // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) + assertThat(view3.left, isEqualAllowingError(activity.dpToPixel(200))) + var view4 = layoutManager.getChildAt(3) + // 100 (view height) + 15 (divider height) + assertThat(view4.top, isEqualAllowingError(activity.dpToPixel(115))) + var view7 = layoutManager.getChildAt(6) + // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) + assertThat(view7.top, isEqualAllowingError(activity.dpToPixel(230))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(HORIZONTAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view2 = layoutManager.getChildAt(1) + // 90 (view width) + assertThat(view2.left, isEqualAllowingError(activity.dpToPixel(90))) + view3 = layoutManager.getChildAt(2) + // 90 (view width) + 90(view width) + assertThat(view3.left, isEqualAllowingError(activity.dpToPixel(180))) + view4 = layoutManager.getChildAt(3) + // 100 (view height) + 15 (divider height) + assertThat(view4.top, isEqualAllowingError(activity.dpToPixel(115))) + view7 = layoutManager.getChildAt(6) + // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) + assertThat(view7.top, isEqualAllowingError(activity.dpToPixel(230))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(VERTICAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view2 = layoutManager.getChildAt(1) + // 90 (view width) + 10 (divider width) + assertThat(view2.left, isEqualAllowingError(activity.dpToPixel(100))) + view3 = layoutManager.getChildAt(2) + // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) + assertThat(view3.left, isEqualAllowingError(activity.dpToPixel(200))) + view4 = layoutManager.getChildAt(3) + // 100 (view height) + assertThat(view4.top, isEqualAllowingError(activity.dpToPixel(100))) + view7 = layoutManager.getChildAt(6) + // 100 (view height) + 100 (view height) + assertThat(view7.top, isEqualAllowingError(activity.dpToPixel(200))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDecoration_direction_rowReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val drawable = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + val itemDecoration = FlexboxItemDecoration(activity) + itemDecoration.setDrawable(drawable) + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW_REVERSE + recyclerView.layoutManager = layoutManager + recyclerView.addItemDecoration(itemDecoration) + recyclerView.adapter = adapter + + for (i in 0..9) { + val lp = createLayoutParams(activity, 90, 100) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW_REVERSE)) + var view1 = layoutManager.getChildAt(0) + var view2 = layoutManager.getChildAt(1) + // 90 (view width) + 10 (divider width) + assertThat(view1.right - view2.right, isEqualAllowingError(activity.dpToPixel(100))) + var view3 = layoutManager.getChildAt(2) + // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) + assertThat(view1.right - view3.right, isEqualAllowingError(activity.dpToPixel(200))) + var view4 = layoutManager.getChildAt(3) + // 100 (view height) + 15 (divider height) + assertThat(view4.top, isEqualAllowingError(activity.dpToPixel(115))) + var view7 = layoutManager.getChildAt(6) + // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) + assertThat(view7.top, isEqualAllowingError(activity.dpToPixel(230))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(HORIZONTAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view1 = layoutManager.getChildAt(0) + view2 = layoutManager.getChildAt(1) + // 90 (view width) + assertThat(view1.right - view2.right, isEqualAllowingError(activity.dpToPixel(90))) + view3 = layoutManager.getChildAt(2) + // 90 (view width) + 90(view width) + assertThat(view1.right - view3.right, isEqualAllowingError(activity.dpToPixel(180))) + view4 = layoutManager.getChildAt(3) + // 100 (view height) + 15 (divider height) + assertThat(view4.top, isEqualAllowingError(activity.dpToPixel(115))) + view7 = layoutManager.getChildAt(6) + // 100 (view height) + 15 (divider height) + 100 (view height) + 15 (divider height) + assertThat(view7.top, isEqualAllowingError(activity.dpToPixel(230))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(VERTICAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view1 = layoutManager.getChildAt(0) + view2 = layoutManager.getChildAt(1) + // 90 (view width) + 10 (divider width) + assertThat(view1.right - view2.right, + isEqualAllowingError(activity.dpToPixel(100))) + view3 = layoutManager.getChildAt(2) + // 90 (view width) + 10 (divider width) + 90(view width) + 10 (divider width) + assertThat(view1.right - view3.right, + isEqualAllowingError(activity.dpToPixel(200))) + view4 = layoutManager.getChildAt(3) + // 100 (view height) + assertThat(view4.top, isEqualAllowingError(activity.dpToPixel(100))) + view7 = layoutManager.getChildAt(6) + // 100 (view height) + 100 (view height) + assertThat(view7.top, isEqualAllowingError(activity.dpToPixel(200))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDecoration_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val drawable = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + val itemDecoration = FlexboxItemDecoration(activity) + itemDecoration.setDrawable(drawable) + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.addItemDecoration(itemDecoration) + recyclerView.adapter = adapter + + for (i in 0..9) { + val lp = createLayoutParams(activity, 90, 65) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + var view2 = layoutManager.getChildAt(1) + // 65 (view height) + 15 (divider height) + assertThat(view2.top, isEqualAllowingError(activity.dpToPixel(80))) + var view3 = layoutManager.getChildAt(2) + // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) + assertThat(view3.top, isEqualAllowingError(activity.dpToPixel(160))) + var view4 = layoutManager.getChildAt(3) + // 90 (view width) + 10 (divider width) + assertThat(view4.left, isEqualAllowingError(activity.dpToPixel(100))) + var view7 = layoutManager.getChildAt(6) + // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) + assertThat(view7.left, isEqualAllowingError(activity.dpToPixel(200))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(HORIZONTAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view2 = layoutManager.getChildAt(1) + // 65 (view height) + 15 (divider height) + assertThat(view2.top, isEqualAllowingError(activity.dpToPixel(80))) + view3 = layoutManager.getChildAt(2) + // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) + assertThat(view3.top, isEqualAllowingError(activity.dpToPixel(160))) + view4 = layoutManager.getChildAt(3) + // 90 (view width) + assertThat(view4.left, isEqualAllowingError(activity.dpToPixel(90))) + view7 = layoutManager.getChildAt(6) + // 90 (view width) + 90 (view width) + assertThat(view7.left, isEqualAllowingError(activity.dpToPixel(180))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(VERTICAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view2 = layoutManager.getChildAt(1) + // 65 (view height) + assertThat(view2.top, isEqualAllowingError(activity.dpToPixel(65))) + view3 = layoutManager.getChildAt(2) + // 65 (view height) + 65 (view height) + assertThat(view3.top, isEqualAllowingError(activity.dpToPixel(130))) + view4 = layoutManager.getChildAt(3) + // 90 (view width) + 10 (divider width) + assertThat(view4.left, isEqualAllowingError(activity.dpToPixel(100))) + view7 = layoutManager.getChildAt(6) + // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) + assertThat(view7.left, isEqualAllowingError(activity.dpToPixel(200))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDecoration_direction_columnReverse() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val drawable = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + val itemDecoration = FlexboxItemDecoration(activity) + itemDecoration.setDrawable(drawable) + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN_REVERSE + recyclerView.layoutManager = layoutManager + recyclerView.addItemDecoration(itemDecoration) + recyclerView.adapter = adapter + + for (i in 0..9) { + val lp = createLayoutParams(activity, 90, 65) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN_REVERSE)) + var view1 = layoutManager.getChildAt(0) + var view2 = layoutManager.getChildAt(1) + // 65 (view height) + 15 (divider height) + assertThat(view1.top - view2.top, isEqualAllowingError(activity.dpToPixel(80))) + var view3 = layoutManager.getChildAt(2) + // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) + assertThat(view1.top - view3.top, isEqualAllowingError(activity.dpToPixel(160))) + var view4 = layoutManager.getChildAt(3) + // 90 (view width) + 10 (divider width) + assertThat(view4.left, isEqualAllowingError(activity.dpToPixel(100))) + var view7 = layoutManager.getChildAt(6) + // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) + assertThat(view7.left, isEqualAllowingError(activity.dpToPixel(200))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(HORIZONTAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view1 = layoutManager.getChildAt(0) + view2 = layoutManager.getChildAt(1) + // 65 (view height) + 15 (divider height) + assertThat(view1.top - view2.top, isEqualAllowingError(activity.dpToPixel(80))) + view3 = layoutManager.getChildAt(2) + // 65 (view height) + 15 (divider height) + 65 (view height) + 15 (divider height) + assertThat(view1.top - view3.top, isEqualAllowingError(activity.dpToPixel(160))) + view4 = layoutManager.getChildAt(3) + // 90 (view width) + assertThat(view4.left, isEqualAllowingError(activity.dpToPixel(90))) + view7 = layoutManager.getChildAt(6) + // 90 (view width) + 90 (view width) + assertThat(view7.left, isEqualAllowingError(activity.dpToPixel(180))) + + activityRule.runOnUiThread { + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.removeItemDecoration(itemDecoration) + itemDecoration.setOrientation(VERTICAL) + recyclerView.addItemDecoration(itemDecoration) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + view1 = layoutManager.getChildAt(0) + view2 = layoutManager.getChildAt(1) + // 65 (view height) + assertThat(view1.top - view2.top, isEqualAllowingError(activity.dpToPixel(65))) + view3 = layoutManager.getChildAt(2) + // 65 (view height) + 65 (view height) + assertThat(view1.top - view3.top, isEqualAllowingError(activity.dpToPixel(130))) + view4 = layoutManager.getChildAt(3) + // 90 (view width) + 10 (divider width) + assertThat(view4.left, isEqualAllowingError(activity.dpToPixel(100))) + view7 = layoutManager.getChildAt(6) + // 90 (view width) + 10 (divider width) + 90 (view width) + 10 (divider width) + assertThat(view7.left, isEqualAllowingError(activity.dpToPixel(200))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToPosition_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..149) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Flex line 1: 3 items + // Flex line 2: 3 items + // .... + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + + val scrollTo = 42 + activityRule.runOnUiThread { layoutManager.scrollToPosition(scrollTo) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // Each flex line should have 3 items in this test's configuration. + // After scrolling to the position of 42 (% 3 == 0), the first visible item should + // be the 42'th item + assertThat((layoutManager.getChildAt(0) as TextView).text.toString(), + `is`((scrollTo + 1).toString())) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // Scroll enough that 42'th item becomes off screen to the top + + activityRule.runOnUiThread { layoutManager.scrollToPosition(scrollTo) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // The 42'th item should be at the bottom of the screen. + // The last visible item should be 42 + 3 since the last visible item is at the last + // of the bottom flex line + assertThat((layoutManager.getChildAt(layoutManager.childCount - 1) as TextView).text.toString(), + `is`((scrollTo + 3).toString())) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToPosition_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..149) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // Flex line 1: 3 items + // Flex line 2: 3 items + // .... + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + + val scrollTo = 42 + activityRule.runOnUiThread { layoutManager.scrollToPosition(scrollTo) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // Each flex line should have 3 items in this test's configuration. + // After scrolling to the position of 42 (% 3 == 0), the first visible item should + // be the 42'th item + assertThat((layoutManager.getChildAt(0) as TextView).text.toString(), + `is`((scrollTo + 1).toString())) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + // Scroll enough that 42'th item becomes off screen to the left + + activityRule.runOnUiThread { layoutManager.scrollToPosition(scrollTo) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // The 42'th item should be at the bottom of the screen. + // The last item should be the (42 + 3)'th item since it should be also the last item in the + // bottom flex line + assertThat((layoutManager.getChildAt(layoutManager.childCount - 1) as TextView).text.toString(), + `is`((scrollTo + 3).toString())) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToPosition_scrollToNewItem_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..5) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + // There should be 2 lines + // RecyclerView width: 320, height: 240. + // Flex line 1: 3 items + // Flex line 2: 3 items + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + + activityRule.runOnUiThread { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + layoutManager.scrollToPosition(adapter.itemCount - 1) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // ChildCount (visible views) should be 6 + 1, + // which before fixing https://github.com/google/flexbox-layout/issues/206, only the new + // item was visible + assertThat(layoutManager.childCount, `is`(7)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToPosition_scrollToNewItem_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..5) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + // There should be 2 lines + // RecyclerView width: 320, height: 240. + // Flex line 1: 3 items + // Flex line 2: 3 items + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + + activityRule.runOnUiThread { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + layoutManager.scrollToPosition(adapter.itemCount - 1) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // ChildCount (visible views) should be 6 + 1, + // which before fixing https://github.com/google/flexbox-layout/issues/206, only the new + // item was visible + assertThat(layoutManager.childCount, `is`(7)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollToStart_secondLineHasMoreItemThanFirst() { + // This test verifies the case that the first line disappears as the user first scrolls to + // the bottom enough that the first line becomes invisible then the user scrolls toward + // start on the condition that the second line has more items than the first line + // https://github.com/google/flexbox-layout/issues/228 + + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val first = FlexboxLayoutManager.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, activity.dpToPixel(70)) + adapter.addItem(first) + for (i in 0..49) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + // The first line has 1 item, the following lines have more than 1 items + // RecyclerView width: 320, height: 240. + // Flex line 1: 1 items + // Flex line 2: 3 items + // Flex line 3: 3 items + // ... + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + // At this moment, the first item should become invisible + var firstView = layoutManager.getChildAt(0) + assertThat((firstView as TextView).text.toString(), `is`(not("1"))) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + + // The first visible item should be "1", which before fixing the issue + // https://github.com/google/flexbox-layout/issues/228, the first line disappeared. + firstView = layoutManager.getChildAt(0) + assertThat((firstView as TextView).text.toString(), `is`("1")) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testNestedRecyclerViews_direction_row() { + // This test verifies the nested RecyclerViews. + // The outer RecyclerView scrolls vertical using LinearLayoutManager. + // The inner RecyclerViews use FlexboxLayoutManager with flexDirection == ROW and + // height of the RecyclerView is set to "wrap_content", which before fixing + // https://github.com/google/flexbox-layout/issues/208, the height of the inner + // RecyclerViews were set to 0. + val activity = activityRule.activity + val outerLayoutManager = LinearLayoutManager(activity) + + // Give the inner adapter item count enough so that inner RecyclerView with + // FlexboxLayoutManager wraps its items + val innerAdapterItemCount = 20 + val adapter = NestedOuterAdapter(FlexDirection.ROW, + innerAdapterItemCount, R.layout.viewholder_inner_recyclerview) + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + outerLayoutManager.orientation = LinearLayoutManager.VERTICAL + recyclerView.layoutManager = outerLayoutManager + recyclerView.adapter = adapter + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + val viewHolder = adapter.getViewHolder(0) + val innerRecyclerView = viewHolder.innerRecyclerView + assertThat(innerRecyclerView.height, `is`(not(0))) + + // This assertion verifies that inner RecyclerView displays the entire items including + // wrapped lines to verify the issue that nested RecyclerView with FlexboxLayoutManager + // only displayed one line https://github.com/google/flexbox-layout/issues/290 + assertThat((innerRecyclerView.layoutManager as FlexboxLayoutManager).findLastVisibleItemPosition(), + `is`(innerAdapterItemCount - 1)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testNestedRecyclerViews_direction_column() { + // This test verifies the nested RecyclerViews. + // The outer RecyclerView scrolls horizontally using LinearLayoutManager. + // The inner RecyclerViews use FlexboxLayoutManager with flexDirection == COLUMN and + // width of the RecyclerView is set to "wrap_content", which before fixing + // https://github.com/google/flexbox-layout/issues/208, the width of the inner + // RecyclerViews were set to 0. + val activity = activityRule.activity + val outerLayoutManager = LinearLayoutManager(activity) + + // Give the inner adapter item count enough so that inner RecyclerView with + // FlexboxLayoutManager wraps its items + val innerAdapterItemCount = 20 + val adapter = NestedOuterAdapter(FlexDirection.COLUMN, + innerAdapterItemCount, R.layout.viewholder_inner_recyclerview_wrap_horizontally) + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + outerLayoutManager.orientation = LinearLayoutManager.HORIZONTAL + recyclerView.layoutManager = outerLayoutManager + recyclerView.adapter = adapter + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + val viewHolder = adapter.getViewHolder(0) + val innerRecyclerView = viewHolder.innerRecyclerView + assertThat(innerRecyclerView.width, `is`(not(0))) + + // This assertion verifies that inner RecyclerView displays the entire items including + // wrapped lines to verify the issue that nested RecyclerView with FlexboxLayoutManager + // only displayed one line https://github.com/google/flexbox-layout/issues/290 + assertThat((innerRecyclerView.layoutManager as FlexboxLayoutManager).findLastVisibleItemPosition(), + `is`(innerAdapterItemCount - 1)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFindVisibleChild_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..49) { + val lp = createLayoutParams(activity, 100, 75) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // At first three completely visible lines. + // Flex line 1, item count 3 (0, 1, 2) + // Flex line 2, item count 3 (3, 4, 5) + // Flex line 3, item count 3 (6, 7, 8) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), `is`(0)) + assertThat(layoutManager.findFirstVisibleItemPosition(), `is`(0)) + assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), `is`(8)) + assertThat(layoutManager.findLastVisibleItemPosition(), `is`(11)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), `is`(42)) + assertThat(layoutManager.findFirstVisibleItemPosition(), `is`(39)) + assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), `is`(49)) + assertThat(layoutManager.findLastVisibleItemPosition(), `is`(49)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testFindVisibleChild_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..49) { + val lp = createLayoutParams(activity, 100, 75) + adapter.addItem(lp) + } + // RecyclerView width: 320, height: 240. + // At first three completely visible lines. + // Flex line 1, item count 3 (0, 1, 2) + // Flex line 2, item count 3 (3, 4, 5) + // Flex line 3, item count 3 (6, 7, 8) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), `is`(0)) + assertThat(layoutManager.findFirstVisibleItemPosition(), `is`(0)) + assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), `is`(8)) + assertThat(layoutManager.findLastVisibleItemPosition(), `is`(11)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(layoutManager.findFirstCompletelyVisibleItemPosition(), `is`(42)) + assertThat(layoutManager.findFirstVisibleItemPosition(), `is`(39)) + assertThat(layoutManager.findLastCompletelyVisibleItemPosition(), `is`(49)) + assertThat(layoutManager.findLastVisibleItemPosition(), `is`(49)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDrawDirtyFlexLine_direction_row() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..29) { + val lp = createLayoutParams(activity, 100, 75) + adapter.addItem(lp) + } + } + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // First scroll to the bottom, then add a new item that isn't visible at this moment. + activityRule.runOnUiThread { + val lp = createLayoutParams(activity, 40, 75) + adapter.addItem(0, lp) + } + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + val firstVisible = layoutManager.getChildAt(0) + assertThat(firstVisible.width, isEqualAllowingError(activity.dpToPixel(40))) + assertThat(firstVisible.height, isEqualAllowingError(activity.dpToPixel(75))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDrawDirtyFlexLine_direction_column() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + + for (i in 0..29) { + val lp = createLayoutParams(activity, 100, 75) + adapter.addItem(lp) + } + } + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // First scroll to the bottom, then add a new item that isn't visible at this moment. + activityRule.runOnUiThread { + val lp = createLayoutParams(activity, 100, 120) + adapter.addItem(0, lp) + } + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + val firstVisible = layoutManager.getChildAt(0) + assertThat(firstVisible.width, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(firstVisible.height, isEqualAllowingError(activity.dpToPixel(120))) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testDrawDirtyFlexLine_multi_viewTypes_direction_row() { + // This test verifies https://github.com/google/flexbox-layout/issues/280 + // the position of the view type is shifted if a new item is inserted before the + // view which has the special viewType and that view isn't visible at the time the item + // was inserted. + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapterMultiViewTypes() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + val parent = activity.findViewById(R.id.recyclerview) + var matchParentText = layoutManager + .getChildAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT) as TextView + assertThat(matchParentText.width, `is`(parent.width)) + assertThat(matchParentText.text.toString(), + `is`((TestAdapterMultiViewTypes.POSITION_MATCH_PARENT + 1).toString())) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + + val insertedValue = 10 + activityRule.runOnUiThread { + val item = TestAdapterMultiViewTypes.Item() + item.value = insertedValue + // Insert an item before the position that has a special viewType + adapter.addItemAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT - 1, item) + } + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + // Since a new item is inserted before the position, the index at the view who has the + // special viewType should be shifted. + matchParentText = layoutManager + .getChildAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT + 1) as TextView + assertThat(matchParentText.width, `is`(parent.width)) + assertThat(matchParentText.text.toString(), + `is`((TestAdapterMultiViewTypes.POSITION_MATCH_PARENT + 1).toString())) + + // The position of this view is the old position who had the special viewType, but + // now the viewType should be a normal one + val textView = layoutManager + .getChildAt(TestAdapterMultiViewTypes.POSITION_MATCH_PARENT - 1) as TextView + assertThat(textView.width, lessThan(parent.width)) + assertThat(textView.text.toString(), `is`(insertedValue.toString())) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testChildrenSizeWithMargin() { + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp = createLayoutParams(activity, 100, 30) + lp.setMargins(10, 30, 20, 40) + adapter.addItem(lp) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + val text = layoutManager.getChildAt(0) as TextView + val lp = text.layoutParams as FlexboxLayoutManager.LayoutParams + assertThat(text.height, isEqualAllowingError(activity.dpToPixel(30))) + assertThat(text.width, isEqualAllowingError(activity.dpToPixel(100))) + assertThat(lp.marginLeft, isEqualAllowingError(10)) + assertThat(lp.marginRight, isEqualAllowingError(20)) + assertThat(lp.marginTop, isEqualAllowingError(30)) + assertThat(lp.marginBottom, isEqualAllowingError(40)) + activityRule.runOnUiThread { layoutManager.flexDirection = FlexDirection.COLUMN } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + val text2 = layoutManager.getChildAt(0) as TextView + assertThat(text2.height, isEqualAllowingError(activity.dpToPixel(30))) + assertThat(text2.width, isEqualAllowingError(activity.dpToPixel(100))) + + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testItemDecoration_withScrolling_direction_row() { + // This test verifies the case that the item decoration set through FlexboxItemDecoration + // is misplaced after the user scrolls the RecyclerView + // https://github.com/google/flexbox-layout/issues/285 + + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val decorationDrawable = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val decoration = FlexboxItemDecoration(activity) + decoration.setDrawable(decorationDrawable) + recyclerView.addItemDecoration(decoration) + for (i in 0..49) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + // The first line has 1 item, the following lines have more than 1 items + // RecyclerView width: 320, height: 240. + // Flex line 1: 1 items + // Flex line 2: 3 items + // Flex line 3: 3 items + // ... + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.ROW)) + assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(0)), `is`(0)) + assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(1)), `is`(0)) + assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(2)), `is`(0)) + layoutManager.flexLines + .mapNotNull { layoutManager.getChildAt(it.firstIndex) } + .forEach { assertThat(layoutManager.getLeftDecorationWidth(it), `is`(0)) } + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + + // Verify even after the scrolling, decoration values are set correctly + assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(0)), `is`(0)) + assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(1)), `is`(0)) + assertThat(layoutManager.getTopDecorationHeight(layoutManager.getChildAt(2)), `is`(0)) + layoutManager.flexLines + .mapNotNull { layoutManager.getChildAt(it.firstIndex) } + .forEach { assertThat(layoutManager.getLeftDecorationWidth(it), `is`(0)) } + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testItemDecoration_withScrolling_direction_column() { + // This test verifies the case that the item decoration set through FlexboxItemDecoration + // is misplaced after the user scrolls the RecyclerView + // https://github.com/google/flexbox-layout/issues/285 + + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + val decorationDrawable = ResourcesCompat.getDrawable(activity.resources, R.drawable.divider, null) + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val decoration = FlexboxItemDecoration(activity) + decoration.setDrawable(decorationDrawable) + recyclerView.addItemDecoration(decoration) + for (i in 0..49) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + // The first line has 1 item, the following lines have more than 1 items + // RecyclerView width: 320, height: 240. + // Flex line 1: 1 items + // Flex line 2: 3 items + // Flex line 3: 3 items + // ... + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.flexDirection, `is`(FlexDirection.COLUMN)) + assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(0)), `is`(0)) + assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(1)), `is`(0)) + assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(2)), `is`(0)) + layoutManager.flexLines + .mapNotNull { layoutManager.getChildAt(it.firstIndex) } + .forEach { assertThat(layoutManager.getTopDecorationHeight(it), `is`(0)) } + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.recyclerview)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + + // Verify even after the scrolling, decoration values are set correctly + assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(0)), `is`(0)) + assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(1)), `is`(0)) + assertThat(layoutManager.getLeftDecorationWidth(layoutManager.getChildAt(2)), `is`(0)) + layoutManager.flexLines + .mapNotNull { layoutManager.getChildAt(it.firstIndex) } + .forEach { assertThat(layoutManager.getTopDecorationHeight(it), `is`(0)) } + } + + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testNotifyItemChange_withPayload() { + // This test verifies the payload is correctly passed to the Adapter in the case + // that notifying an item with payload + // https://github.com/google/flexbox-layout/issues/297 + + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(adapter.payloads.size, `is`(0)) + + val payload = "payload" + activityRule.runOnUiThread { adapter.changeItemWithPayload(0, payload) } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + + assertThat(adapter.payloads.size, `is`(1)) + assertThat(adapter.payloads[0] as String, `is`(payload)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollAlongManAxis_direction_row() { + // This test verifies the scroll along the main axis if the width of the RecyclerView is + // larger than its parent when the main axis direction is horizontal (row) + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.wrapped_recyclerview) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.ROW + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + val lp = createLayoutParams(activity, 100, 70) + adapter.addItem(lp) + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.getChildAt(0).left, `is`(0)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_RIGHT, + GeneralLocation.CENTER_LEFT)) + assertThat(layoutManager.getChildAt(0).left, `is`(not(0))) + + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.CENTER_LEFT, + GeneralLocation.CENTER_RIGHT)) + assertThat(layoutManager.getChildAt(0).left, `is`(0)) + } + + @Test + @FlakyTest + @Throws(Throwable::class) + fun testScrollAlongManAxis_direction_column() { + // This test verifies the scroll along the main axis if the height of the RecyclerView is + // larger than its parent when the main axis direction is vertical (column). + val activity = activityRule.activity + val layoutManager = FlexboxLayoutManager(activity) + val adapter = TestAdapter() + activityRule.runOnUiThread { + activity.setContentView(R.layout.wrapped_recyclerview_scroll_vertical) + val recyclerView = activity.findViewById(R.id.recyclerview) + layoutManager.flexDirection = FlexDirection.COLUMN + recyclerView.layoutManager = layoutManager + recyclerView.adapter = adapter + for (i in 0..49) { + val lp = createLayoutParams(activity, 70, 100) + adapter.addItem(lp) + } + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + assertThat(layoutManager.getChildAt(0).top, `is`(0)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.BOTTOM_CENTER, + GeneralLocation.TOP_CENTER)) + assertThat(layoutManager.getChildAt(0).top, `is`(not(0))) + + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + onView(withId(R.id.container)).perform(swipe(GeneralLocation.TOP_CENTER, + GeneralLocation.BOTTOM_CENTER)) + assertThat(layoutManager.getChildAt(0).top, `is`(0)) + } + + /** + * Creates a new flex item. + * + * @param context the context + * @param width in DP + * @param height in DP + * @return the created [FlexboxLayoutManager.LayoutParams] instance + */ + private fun createLayoutParams(context: Context, width: Int, + height: Int): FlexboxLayoutManager.LayoutParams { + return FlexboxLayoutManager.LayoutParams(context.dpToPixel(width), context.dpToPixel(height)) + } + + private fun swipe(from: CoordinatesProvider, to: CoordinatesProvider): ViewAction { + return GeneralSwipeAction(Swipe.FAST, from, to, Press.FINGER) + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxTestActivity.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxTestActivity.kt similarity index 73% rename from flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxTestActivity.java rename to flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxTestActivity.kt index 001b3df..b7b81f0 100644 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxTestActivity.java +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/FlexboxTestActivity.kt @@ -14,15 +14,12 @@ * limitations under the License. */ -package com.google.android.flexbox.test; +package com.google.android.flexbox.test -import com.google.android.flexbox.FlexboxLayout; - -import android.app.Activity; +import android.app.Activity +import com.google.android.flexbox.FlexboxLayout /** - * Activity for testing the {@link FlexboxLayout}. + * Activity for testing the [FlexboxLayout]. */ -public class FlexboxTestActivity extends Activity { - -} +class FlexboxTestActivity : Activity() diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.java deleted file mode 100644 index 50b90fe..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.hamcrest.Factory; -import org.hamcrest.Matcher; - -import java.util.Locale; - -/** - * Custom {@link BaseMatcher} that expects {@link Number} value allowing some errors to allow - * such as rounding errors. - */ -public class IsEqualAllowingError extends BaseMatcher { - - private Number expected; - - private Integer errorAllowed; - - private IsEqualAllowingError(Number expected) { - this(expected, 2); - } - - private IsEqualAllowingError(Number expected, int errorAllowed) { - this.expected = expected; - this.errorAllowed = errorAllowed; - } - - @Override - public boolean matches(Object item) { - if (!(item instanceof Number)) { - return false; - } - Number other = (Number) item; - return expected.intValue() - errorAllowed <= other.intValue() && - other.intValue() <= expected.intValue() + errorAllowed; - } - - @Override - public void describeTo(Description description) { - description.appendText( - String.format(Locale.US, "expected value is <%s> allowing error of <%s>.", expected, - errorAllowed)); - } - - @Factory - public static Matcher isEqualAllowingError(T expected) { - return new IsEqualAllowingError<>(expected); - } - - @Factory - public static Matcher isEqualAllowingError(T expected, int errorAllowed) { - return new IsEqualAllowingError<>(expected, errorAllowed); - } -} - diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.kt new file mode 100644 index 0000000..68c74e8 --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/IsEqualAllowingError.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import org.hamcrest.BaseMatcher +import org.hamcrest.Description +import org.hamcrest.Factory +import org.hamcrest.Matcher +import java.util.* + +/** + * Custom [BaseMatcher] that expects [Number] value allowing some errors to allow + * such as rounding errors. + */ +class IsEqualAllowingError private constructor(private val expected: Number, private val errorAllowed: Int) : BaseMatcher() { + + override fun matches(item: Any): Boolean { + if (item !is Number) { + return false + } + return expected.toInt() - errorAllowed <= item.toInt() && item.toInt() <= expected.toInt() + errorAllowed + } + + override fun describeTo(description: Description) { + description.appendText( + String.format(Locale.US, "expected value is <%s> allowing error of <%s>.", expected, + errorAllowed)) + } + + companion object { + + @Factory + fun isEqualAllowingError(expected: T): Matcher { + return IsEqualAllowingError(expected, 2) + } + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.java deleted file mode 100644 index 7b642be..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2017 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -/** - * Adapter for the tests for nested RecyclerViews. - * This Adapter is used for the inner RecyclerView. - */ -class NestedInnerAdapter extends RecyclerView.Adapter { - - private int mInnerPosition; - - private int mItemCount; - - NestedInnerAdapter(int innerPosition, int itemCount) { - mInnerPosition = innerPosition; - mItemCount = itemCount; - } - - @Override - public NestedInnerAdapter.InnerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.viewholder_textview, parent, false); - return new InnerViewHolder(view); - } - - @Override - public void onBindViewHolder(InnerViewHolder holder, int position) { - String description = holder.mTextView.getResources().getString(R.string.item_description); - holder.mTextView.setText(String.format(description, mInnerPosition, position)); - } - - @Override - public int getItemCount() { - return mItemCount; - } - - static class InnerViewHolder extends RecyclerView.ViewHolder { - - TextView mTextView; - - InnerViewHolder(View itemView) { - super(itemView); - - mTextView = itemView.findViewById(R.id.textview); - } - } -} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.kt new file mode 100644 index 0000000..09dcdc9 --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedInnerAdapter.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2017 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import android.support.v7.widget.RecyclerView +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView + +/** + * Adapter for the tests for nested RecyclerViews. + * This Adapter is used for the inner RecyclerView. + */ +internal class NestedInnerAdapter(private val innerPosition: Int, private val itemCount: Int) : RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NestedInnerAdapter.InnerViewHolder { + val view = LayoutInflater.from(parent.context).inflate(R.layout.viewholder_textview, parent, false) + return InnerViewHolder(view) + } + + override fun onBindViewHolder(holder: InnerViewHolder, position: Int) { + holder.textView.text = holder.textView.resources.getString(R.string.item_description, innerPosition, position) + } + + override fun getItemCount() = itemCount + + internal class InnerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + + val textView: TextView = itemView.findViewById(R.id.textview) + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.java deleted file mode 100644 index 0f1b63b..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2017 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - -import android.content.Context; -import android.support.annotation.LayoutRes; -import android.support.v7.widget.RecyclerView; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.google.android.flexbox.FlexDirection; -import com.google.android.flexbox.FlexboxLayoutManager; - -import java.util.ArrayList; -import java.util.List; - -/** - * Adapter for the tests for nested RecyclerViews. - * This Adapter is used for the outer RecyclerView. - */ -class NestedOuterAdapter extends RecyclerView.Adapter { - - private static final int ITEM_COUNT = 4; - - private final Context mContext; - - private final List mViewHolderList = new ArrayList<>(); - - private final int mFlexDirection; - - private final int mInnerAdapterItemCount; - - private final int mViewHolderResId; - - NestedOuterAdapter(Context context, @FlexDirection int flexDirection, int innerAdapterItemCount, - @LayoutRes int viewHolderResId) { - mContext = context; - mFlexDirection = flexDirection; - mInnerAdapterItemCount = innerAdapterItemCount; - mViewHolderResId = viewHolderResId; - } - - @Override - public NestedOuterAdapter.OuterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View view = LayoutInflater.from(parent.getContext()) - .inflate(mViewHolderResId, parent, false); - OuterViewHolder holder = new OuterViewHolder(view); - mViewHolderList.add(holder); - return holder; - } - - @Override - public void onBindViewHolder(OuterViewHolder holder, int position) { - FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(mContext); - layoutManager.setFlexDirection(mFlexDirection); - holder.mInnerRecyclerView.setLayoutManager(layoutManager); - holder.mInnerRecyclerView.setAdapter(new NestedInnerAdapter(position, - mInnerAdapterItemCount)); - } - - OuterViewHolder getViewHolder(int position) { - return mViewHolderList.get(position); - } - - @Override - public int getItemCount() { - return ITEM_COUNT; - } - - static class OuterViewHolder extends RecyclerView.ViewHolder { - - RecyclerView mInnerRecyclerView; - - OuterViewHolder(View itemView) { - super(itemView); - - mInnerRecyclerView = itemView.findViewById(R.id.recyclerview_inner); - } - } -} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.kt new file mode 100644 index 0000000..09c80af --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/NestedOuterAdapter.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2017 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import android.support.annotation.LayoutRes +import android.support.v7.widget.RecyclerView +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.android.flexbox.FlexDirection +import com.google.android.flexbox.FlexboxLayoutManager + +/** + * Adapter for the tests for nested RecyclerViews. + * This Adapter is used for the outer RecyclerView. + */ +internal class NestedOuterAdapter( + @param:FlexDirection private val flexDirection: Int, private val innerAdapterItemCount: Int, + @param:LayoutRes private val viewHolderResId: Int +) : RecyclerView.Adapter() { + + private val viewHolderList = mutableListOf() + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OuterViewHolder { + val view = LayoutInflater.from(parent.context).inflate(viewHolderResId, parent, false) + val holder = OuterViewHolder(view) + viewHolderList.add(holder) + return holder + } + + override fun onBindViewHolder(holder: OuterViewHolder, position: Int) { + val layoutManager = FlexboxLayoutManager(holder.itemView.context) + layoutManager.flexDirection = flexDirection + holder.innerRecyclerView.layoutManager = layoutManager + holder.innerRecyclerView.adapter = NestedInnerAdapter(position, innerAdapterItemCount) + } + + fun getViewHolder(position: Int) = viewHolderList[position] + + override fun getItemCount() = ITEM_COUNT + + internal class OuterViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + val innerRecyclerView: RecyclerView = itemView.findViewById(R.id.recyclerview_inner) + } + + companion object { + private const val ITEM_COUNT = 4 + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.java deleted file mode 100644 index 03e53e7..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2016 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - -import android.support.v7.widget.RecyclerView; -import android.view.Gravity; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.google.android.flexbox.FlexboxLayoutManager; - -import java.util.ArrayList; -import java.util.List; - -/** - * {@link RecyclerView.Adapter} implementation for {@link TestViewHolder}. - */ -class TestAdapter extends RecyclerView.Adapter { - - private List mLayoutParams; - - private List mReceivedPayloads = new ArrayList<>(); - - TestAdapter() { - this(new ArrayList()); - } - - private TestAdapter(List flexItems) { - mLayoutParams = flexItems; - } - - @Override - public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.recyclerview_viewholder, parent, false); - return new TestViewHolder(view); - } - - @Override - public void onBindViewHolder(TestViewHolder holder, int position) { - holder.mTextView.setText(String.valueOf(position + 1)); - holder.mTextView.setBackgroundResource(R.drawable.flex_item_background); - holder.mTextView.setGravity(Gravity.CENTER); - holder.mTextView.setLayoutParams(mLayoutParams.get(position)); - } - - @Override - public void onBindViewHolder(TestViewHolder holder, int position, List payloads) { - mReceivedPayloads.addAll(payloads); - onBindViewHolder(holder, position); - } - - void addItem(int position, FlexboxLayoutManager.LayoutParams flexItem) { - mLayoutParams.add(position, flexItem); - notifyItemInserted(position); - } - - void addItem(FlexboxLayoutManager.LayoutParams flexItem) { - mLayoutParams.add(flexItem); - notifyItemInserted(mLayoutParams.size() - 1); - } - - void changeItemWithPayload(int position, Object payload) { - notifyItemChanged(position, payload); - } - - List getPayloads() { - return new ArrayList<>(mReceivedPayloads); - } - - FlexboxLayoutManager.LayoutParams getItemAt(int index) { - return mLayoutParams.get(index); - } - - @Override - public int getItemCount() { - return mLayoutParams.size(); - } -} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.kt new file mode 100644 index 0000000..9b8c444 --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapter.kt @@ -0,0 +1,71 @@ +/* + * Copyright 2016 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import android.support.v7.widget.RecyclerView +import android.view.Gravity +import android.view.LayoutInflater +import android.view.ViewGroup +import com.google.android.flexbox.FlexboxLayoutManager + +/** + * [RecyclerView.Adapter] implementation for [TestViewHolder]. + */ +internal class TestAdapter private constructor(private val layoutParams: MutableList) : RecyclerView.Adapter() { + + private val receivedPayloads = mutableListOf() + + constructor() : this(mutableListOf()) + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder { + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.recyclerview_viewholder, parent, false) + return TestViewHolder(view) + } + + override fun onBindViewHolder(holder: TestViewHolder, position: Int) { + holder.textView.text = (position + 1).toString() + holder.textView.setBackgroundResource(R.drawable.flex_item_background) + holder.textView.gravity = Gravity.CENTER + holder.textView.layoutParams = layoutParams[position] + } + + override fun onBindViewHolder(holder: TestViewHolder, position: Int, payloads: List?) { + payloads?.let(receivedPayloads::addAll) + onBindViewHolder(holder, position) + } + + fun addItem(position: Int, flexItem: FlexboxLayoutManager.LayoutParams) { + layoutParams.add(position, flexItem) + notifyItemInserted(position) + } + + fun addItem(flexItem: FlexboxLayoutManager.LayoutParams) { + layoutParams.add(flexItem) + notifyItemInserted(layoutParams.size - 1) + } + + fun changeItemWithPayload(position: Int, payload: Any) { + notifyItemChanged(position, payload) + } + + val payloads get() = receivedPayloads.toList() + + fun getItemAt(index: Int) = layoutParams[index] + + override fun getItemCount() = layoutParams.size +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.java deleted file mode 100644 index dd26e33..0000000 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2017 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.flexbox.test; - -import android.support.v7.widget.RecyclerView; -import android.view.Gravity; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.google.android.flexbox.FlexboxLayoutManager; - -import java.util.ArrayList; -import java.util.List; - -/** - * {@link RecyclerView.Adapter} implementation for {@link TestViewHolder}, which has multiple - * view types. - */ -class TestAdapterMultiViewTypes extends RecyclerView.Adapter { - - static final int POSITION_MATCH_PARENT = 3; - private static final int ITEMS = 50; - - private static final int VIEW_TYPE_NORMAL = 0; - private static final int VIEW_TYPE_MATCH_PARENT = 1; - - private final List mItems; - - TestAdapterMultiViewTypes() { - mItems = new ArrayList<>(); - for (int i = 0; i < ITEMS; i++) { - Item item = new Item(); - if (i == POSITION_MATCH_PARENT) { - item.viewType = VIEW_TYPE_MATCH_PARENT; - } - item.value = i + 1; - mItems.add(item); - } - } - - @Override - public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - View view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.recyclerview_viewholder, parent, false); - if (viewType == VIEW_TYPE_MATCH_PARENT) { - FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) - view.getLayoutParams(); - flexboxLp.setFlexBasisPercent(90f); - flexboxLp.setFlexGrow(1f); - } - return new TestViewHolder(view); - } - - @Override - public void onBindViewHolder(TestViewHolder holder, int position) { - holder.mTextView.setText(String.valueOf(mItems.get(position).value)); - holder.mTextView.setBackgroundResource(R.drawable.flex_item_background); - holder.mTextView.setGravity(Gravity.CENTER); - } - - @Override - public int getItemViewType(int position) { - return mItems.get(position).viewType; - } - - void addItemAt(int position, Item item) { - mItems.add(position, item); - notifyItemInserted(position); - } - - @Override - public int getItemCount() { - return mItems.size(); - } - - static class Item { - int viewType = VIEW_TYPE_NORMAL; - int value; - } -} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.kt b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.kt new file mode 100644 index 0000000..990254d --- /dev/null +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestAdapterMultiViewTypes.kt @@ -0,0 +1,83 @@ +/* + * Copyright 2017 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.flexbox.test + +import android.support.v7.widget.RecyclerView +import android.view.Gravity +import android.view.LayoutInflater +import android.view.ViewGroup +import com.google.android.flexbox.FlexboxLayoutManager + +/** + * [RecyclerView.Adapter] implementation for [TestViewHolder], which has multiple + * view types. + */ +internal class TestAdapterMultiViewTypes : RecyclerView.Adapter() { + + private val items = mutableListOf() + + init { + for (i in 0 until ITEMS) { + val item = Item() + if (i == POSITION_MATCH_PARENT) { + item.viewType = VIEW_TYPE_MATCH_PARENT + } + item.value = i + 1 + items.add(item) + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder { + val view = LayoutInflater.from(parent.context) + .inflate(R.layout.recyclerview_viewholder, parent, false) + if (viewType == VIEW_TYPE_MATCH_PARENT) { + val flexboxLp = view.layoutParams as FlexboxLayoutManager.LayoutParams + flexboxLp.flexBasisPercent = 90f + flexboxLp.flexGrow = 1f + } + return TestViewHolder(view) + } + + override fun onBindViewHolder(holder: TestViewHolder, position: Int) { + holder.textView.text = items[position].value.toString() + holder.textView.setBackgroundResource(R.drawable.flex_item_background) + holder.textView.gravity = Gravity.CENTER + } + + override fun getItemViewType(position: Int) = items[position].viewType + + fun addItemAt(position: Int, item: Item) { + items.add(position, item) + notifyItemInserted(position) + } + + override fun getItemCount() = items.size + + internal class Item { + internal var viewType = VIEW_TYPE_NORMAL + internal var value = 0 + } + + companion object { + + internal val POSITION_MATCH_PARENT = 3 + private val ITEMS = 50 + + private val VIEW_TYPE_NORMAL = 0 + private val VIEW_TYPE_MATCH_PARENT = 1 + } +} diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestUtil.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestUtil.kt similarity index 62% rename from flexbox/src/androidTest/java/com/google/android/flexbox/test/TestUtil.java rename to flexbox/src/androidTest/java/com/google/android/flexbox/test/TestUtil.kt index 54d7ddf..92c3c43 100644 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestUtil.java +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestUtil.kt @@ -14,18 +14,11 @@ * limitations under the License. */ -package com.google.android.flexbox.test; +package com.google.android.flexbox.test -import android.content.Context; -import android.util.DisplayMetrics; +import android.content.Context -/** - * Utility class for tests. - */ -class TestUtil { - - static int dpToPixel(Context context, int dp) { - DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); - return dp < 0 ? dp : Math.round(dp * displayMetrics.density); - } +internal fun Context.dpToPixel(dp: Int): Int { + val displayMetrics = this.resources.displayMetrics + return if (dp < 0) dp else Math.round(dp * displayMetrics.density) } diff --git a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestViewHolder.java b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestViewHolder.kt similarity index 66% rename from flexbox/src/androidTest/java/com/google/android/flexbox/test/TestViewHolder.java rename to flexbox/src/androidTest/java/com/google/android/flexbox/test/TestViewHolder.kt index e2978c3..a052e23 100644 --- a/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestViewHolder.java +++ b/flexbox/src/androidTest/java/com/google/android/flexbox/test/TestViewHolder.kt @@ -14,22 +14,15 @@ * limitations under the License. */ -package com.google.android.flexbox.test; +package com.google.android.flexbox.test -import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.widget.TextView; +import android.support.v7.widget.RecyclerView +import android.view.View +import android.widget.TextView /** * ViewHolder implementation for a flex item for testing. */ -class TestViewHolder extends RecyclerView.ViewHolder { - - TextView mTextView; - - TestViewHolder(View itemView) { - super(itemView); - - mTextView = itemView.findViewById(R.id.textview); - } +internal class TestViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + val textView: TextView = itemView.findViewById(R.id.textview) } -- GitLab