@Test @FlakyTest(tolerance = TOLERANCE) public void testLoadFromLayoutXml() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_simple); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); assertNotNull(flexboxLayout); assertThat(flexboxLayout.getFlexDirection(), is(FlexboxLayout.FLEX_DIRECTION_ROW_REVERSE)); assertThat(flexboxLayout.getJustifyContent(), is(FlexboxLayout.JUSTIFY_CONTENT_CENTER)); assertThat(flexboxLayout.getAlignContent(), is(FlexboxLayout.ALIGN_CONTENT_CENTER)); assertThat(flexboxLayout.getAlignItems(), is(FlexboxLayout.ALIGN_ITEMS_CENTER)); assertThat(flexboxLayout.getChildCount(), is(1)); View child = flexboxLayout.getChildAt(0); FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) child.getLayoutParams(); assertThat(lp.order, is(2)); assertThat(lp.flexGrow, is(1f)); assertThat(lp.alignSelf, is(FlexboxLayout.LayoutParams.ALIGN_SELF_STRETCH)); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testFlexWrap_wrap() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_flex_wrap_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); assertThat(flexboxLayout.getFlexWrap(), is(FlexboxLayout.FLEX_WRAP_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(isRightOf(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(isBelow(withId(R.id.text1))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text2))); onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testFlexItem_match_parent() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_flex_item_match_parent); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); TextView text1 = (TextView) activity.findViewById(R.id.text1); TextView text2 = (TextView) activity.findViewById(R.id.text2); TextView text3 = (TextView) 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(tolerance = TOLERANCE) public void testFlexboxLayout_wrapContent() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(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(isRightOf(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(isRightOf(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(tolerance = TOLERANCE) public void testJustifyContent_flexStart() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_justify_content_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); assertThat(flexboxLayout.getJustifyContent(), is(FlexboxLayout.JUSTIFY_CONTENT_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(isRightOf(withId(R.id.text1))); onView(withId(R.id.text3)).check(isRightOf(withId(R.id.text2))); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testAlignItems_baseline() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_align_items_baseline_test); } }); InstrumentationRegistry.getInstrumentation().waitForIdleSync(); TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) 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(tolerance = TOLERANCE) public void testAlignItems_baseline_wrapReverse() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_align_items_baseline_test); FlexboxLayout flexboxLayout = (FlexboxLayout) activity .findViewById(R.id.flexbox_layout); flexboxLayout.setFlexWrap(FlexboxLayout.FLEX_WRAP_WRAP_REVERSE); } }); InstrumentationRegistry.getInstrumentation().waitForIdleSync(); TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) 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(tolerance = TOLERANCE) 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()); 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 @FlakyTest(tolerance = TOLERANCE) 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(tolerance = TOLERANCE) 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(tolerance = TOLERANCE) 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); assertThat(first.getWidth(), is(flexboxLayout.getWidth() / 2)); }
@FlakyTest @Test public void postDelayed() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(1); long startTime = SystemClock.elapsedRealtime(); final AtomicBoolean executed = new AtomicBoolean(false); mHandler.postDelayed(new Runnable() { @Override public void run() { executed.set(true); latch.countDown(); } }, 300); latch.await(1, TimeUnit.SECONDS); assertTrue(executed.get()); long elapsedTime = SystemClock.elapsedRealtime() - startTime; assertTrue("Elapsed time should be 300, but was " + elapsedTime, elapsedTime <= 330 && elapsedTime >= 300); }
@Test @FlakyTest(tolerance = 2) public final void test4StateDestroy() { // Destroy the Activity. _mainActivity.finish(); // Restart Activity. _mainActivity = getActivity(); // Get a reference to the list contact fragment. _fragmentContactList = (ContactListFragment) _mainActivity.getFragmentManager().findFragmentByTag(MainActivity.FRAGMENT_CONTACT_LIST_TAG); assertNotNull("ContactListFragment null", _fragmentContactList); assertEquals("Number of contacts", _numberOfContacts, _fragmentContactList.getListAdapter().getCount()); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testOrderAttribute_fromLayoutXml() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_order_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 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(tolerance = TOLERANCE) public void testFlexboxLayout_wrapped_with_ScrollView() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_flexbox_wrapped_with_scrollview); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); 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(isRightOf(withId(R.id.text1))); onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text1))); onView(withId(R.id.text3)).check(isBelow(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 = (TextView) activity.findViewById(R.id.text1); TextView textView3 = (TextView) activity.findViewById(R.id.text3); assertThat(flexboxLayout.getHeight(), is(textView1.getHeight() + textView3.getHeight())); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testFlexboxLayout_wrapped_with_HorizontalScrollView() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView( R.layout.activity_flexbox_wrapped_with_horizontalscrollview); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); 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(isRightOf(withId(R.id.text1))); onView(withId(R.id.text2)).check(isTopAlignedWith(withId(R.id.flexbox_layout))); onView(withId(R.id.text3)).check(isRightOf(withId(R.id.text1))); onView(withId(R.id.text3)).check(isRightOf(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 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) activity.findViewById(R.id.text3); assertThat(flexboxLayout.getWidth(), is(textView1.getWidth() + textView2.getWidth() + textView3.getWidth())); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testFlexGrow_withExactParentLength() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_flex_grow_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); 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(isRightOf(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(isRightOf(withId(R.id.text2))); TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) activity.findViewById(R.id.text3); assertThat(textView3.getWidth(), is(flexboxLayout.getWidth() - textView1.getWidth() - textView2.getWidth())); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testAlignContent_stretch() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_align_content_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); assertThat(flexboxLayout.getAlignContent(), is(FlexboxLayout.ALIGN_CONTENT_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(isRightOf(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(isBelow(withId(R.id.text1))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text2))); TextView textView3 = (TextView) 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(tolerance = TOLERANCE) public void testAlignItems_stretch() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_stretch_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); assertThat(flexboxLayout.getAlignItems(), is(FlexboxLayout.ALIGN_ITEMS_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(isRightOf(withId(R.id.text1))); onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text1))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text2))); // There should be 2 flex lines in the layout with the given layout. int flexLineSize = flexboxLayout.getHeight() / 2; TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) activity.findViewById(R.id.text3); assertTrue(flexLineSize - 1 <= textView1.getHeight() && textView1.getHeight() <= flexLineSize + 1); assertTrue(flexLineSize - 1 <= textView2.getHeight() && flexLineSize <= flexLineSize + 1); assertTrue(flexLineSize - 1 <= textView3.getHeight() && textView3.getHeight() <= flexLineSize + 1); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testAlignSelf_stretch() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_align_self_stretch_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); 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(isRightOf(withId(R.id.text1))); onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text1))); onView(withId(R.id.text3)).check(isBelow(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 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) activity.findViewById(R.id.text3); assertTrue(flexLineSize - 1 <= textView1.getHeight() && textView1.getHeight() <= flexLineSize + 1); assertThat(textView2.getHeight(), not(flexLineSize)); assertThat(textView3.getHeight(), not(flexLineSize)); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testAlignItems_flexStart() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_align_items_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); assertThat(flexboxLayout.getAlignItems(), is(FlexboxLayout.ALIGN_ITEMS_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(isRightOf(withId(R.id.text1))); onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text1))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text2))); // There should be 2 flex lines in the layout with the given layout. int flexLineSize = flexboxLayout.getHeight() / 2; TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) activity.findViewById(R.id.text3); assertThat(textView1.getHeight(), not(flexLineSize)); assertThat(textView2.getHeight(), not(flexLineSize)); assertThat(textView3.getHeight(), not(flexLineSize)); assertTrue(flexLineSize - 1 <= textView3.getTop() && textView3.getTop() <= flexLineSize + 1); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testFlexBasisPercent_wrap() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_flex_basis_percent_test); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); // The text1 length is 50%, the text2 length is 60% and the wrap property is FLEX_WRAP_WRAP, // the text2 should be on the second flex line. assertThat(flexboxLayout.getFlexWrap(), is(FlexboxLayout.FLEX_WRAP_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(isBelow(withId(R.id.text1))); onView(withId(R.id.text3)).check(isRightOf(withId(R.id.text2))); TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) 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.flexBasisPercent))); assertThat(textView2.getWidth(), is(Math.round(flexboxLayout.getWidth() * lp2.flexBasisPercent))); }
@Test @FlakyTest(tolerance = TOLERANCE) public void testView_visibility_gone() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_views_visibility_gone); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); // The text1 and text2's visibility are gone, so the visible view starts from text3 assertThat(flexboxLayout.getFlexWrap(), is(FlexboxLayout.FLEX_WRAP_WRAP)); assertThat(flexboxLayout.getFlexDirection(), is(FlexboxLayout.FLEX_DIRECTION_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(isRightOf(withId(R.id.text3))); onView(withId(R.id.text5)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); onView(withId(R.id.text5)).check(isBelow(withId(R.id.text3))); TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) activity.findViewById(R.id.text3); TextView textView4 = (TextView) activity.findViewById(R.id.text4); TextView textView5 = (TextView) 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(tolerance = TOLERANCE) public void testView_visibility_invisible() throws Throwable { final FlexboxTestActivity activity = mActivityRule.getActivity(); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { activity.setContentView(R.layout.activity_views_visibility_invisible); } }); FlexboxLayout flexboxLayout = (FlexboxLayout) activity.findViewById(R.id.flexbox_layout); // The text1 and text2's visibility are invisible, these views take space like visible views assertThat(flexboxLayout.getFlexWrap(), is(FlexboxLayout.FLEX_WRAP_WRAP)); assertThat(flexboxLayout.getFlexDirection(), is(FlexboxLayout.FLEX_DIRECTION_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(isRightOf(withId(R.id.text1))); onView(withId(R.id.text3)).check(isLeftAlignedWith(withId(R.id.flexbox_layout))); onView(withId(R.id.text3)).check(isBelow(withId(R.id.text1))); TextView textView1 = (TextView) activity.findViewById(R.id.text1); TextView textView2 = (TextView) activity.findViewById(R.id.text2); TextView textView3 = (TextView) 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(tolerance = TOLERANCE) 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(tolerance = TOLERANCE) 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 @SuppressWarnings("unchecked") @FlakyTest(tolerance = TOLERANCE) 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<CharSequence> spinnerAdapter = (ArrayAdapter<CharSequence>) 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(FlexboxLayout.FLEX_DIRECTION_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(FlexboxLayout.FLEX_DIRECTION_ROW_REVERSE)); }
@Test @SuppressWarnings("unchecked") @FlakyTest(tolerance = TOLERANCE) 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<CharSequence> spinnerAdapter = (ArrayAdapter<CharSequence>) 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(FlexboxLayout.FLEX_WRAP_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(FlexboxLayout.FLEX_WRAP_NOWRAP)); }
@Test @SuppressWarnings("unchecked") @FlakyTest(tolerance = TOLERANCE) 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<CharSequence> spinnerAdapter = (ArrayAdapter<CharSequence>) 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(FlexboxLayout.JUSTIFY_CONTENT_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(FlexboxLayout.JUSTIFY_CONTENT_CENTER)); }
@Test @SuppressWarnings("unchecked") @FlakyTest(tolerance = TOLERANCE) 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<CharSequence> spinnerAdapter = (ArrayAdapter<CharSequence>) 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(FlexboxLayout.ALIGN_ITEMS_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(FlexboxLayout.ALIGN_ITEMS_FLEX_END)); }
@Test @SuppressWarnings("unchecked") @FlakyTest(tolerance = TOLERANCE) 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<CharSequence> spinnerAdapter = (ArrayAdapter<CharSequence>) 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(FlexboxLayout.ALIGN_CONTENT_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(FlexboxLayout.ALIGN_CONTENT_STRETCH)); }
/** * TODO For an unknown reason, this test passes when run on its own or when whole class is run, * but it fails when tests are run at package level. */ @Test @FlakyTest public void watchLiveText_IsVisible_Flaky() { onView(withId(R.id.watch)).check(matches(isDisplayed())); onView(withText(R.string.session_watch_live)).check(matches(isDisplayed())); }
@Test @FlakyTest(tolerance = 2) public final void test1ContactList() { // Good testing practice. // Make sure there are rows, etc. assertTrue("At least one Contact is required.", _fragmentContactList.getListAdapter().getCount() > 0); }
@Test @FlakyTest(tolerance = 2) public final void test5PostCondition() { ContactModel _contactModel = ContactModel.getInstance(_mainActivity); _numberOfContacts = _contactModel.deleteAllContacts(); assertTrue("No dummy contacts deleted", _numberOfContacts >= 0); }
@Test @FlakyTest(tolerance = 2) public final void test2ContactDetail() { // NOTE: This method is annotated with @UiThreadTest, so you have to // specifically call runOnUiThread for those methods that require // calling on the main thread. _mainActivity.runOnUiThread(new Runnable() { @Override public void run() { _fragmentContactList.getListView().requestFocus(); _fragmentContactList.getListView().setSelection(0); _fragmentContactList.getListView().performItemClick(_fragmentContactList.getView(), 0, _fragmentContactList.getListView().getSelectedItemId()); assertTrue("Contact not selected", _fragmentContactList.getListView().getSelectedItemId() > 0); // Make sure the detail fragment is available. // Fragments are lazily assigned. _mainActivity.getFragmentManager().executePendingTransactions(); } }); // Need to make sure the Fragment is visible. getInstrumentation().waitForIdleSync(); // Get a reference to the view contact fragment. _fragmentContactDetail = (ContactDetailFragment) _mainActivity.getFragmentManager().findFragmentByTag(MainActivity.FRAGMENT_CONTACT_DETAIL_TAG); assertNotNull("ContactDetailFragment null", _fragmentContactDetail); // Get a Contact that was passed to the fragment. Contact contact = _fragmentContactDetail.getArguments().getParcelable(ContactDetailFragment.BUNDLE_KEY_CONTACT); assertNotNull("Contact null", contact); // Get the contact name displayed in the text box. EditText editText = (EditText) _fragmentContactDetail.getView().findViewById(R.id.editTextName); String contactName = editText.getText().toString(); assertEquals("Contact name is different", ContactModel.getInstance(_mainActivity).getContact(contact.ContactID).Name, contactName); }
@Test @UiThreadTest @FlakyTest(tolerance = 2) public final void test3StatePauseResume() { // NOTE: This test annotated with @UiThreadTest, so all of the calls are executed // on the main thread. Different from the previous test where a Runnable was // used to perform clicks on the UI thread. _fragmentContactList.getListView().requestFocus(); _fragmentContactList.getListView().setSelection(0); _fragmentContactList.getListView().performItemClick(_fragmentContactList.getView(), 0, _fragmentContactList.getListView().getSelectedItemId()); assertTrue("Contact not selected", _fragmentContactList.getListView().getSelectedItemId() > 0); // Make sure the detail fragment is available. // Fragments are lazily assigned. _mainActivity.getFragmentManager().executePendingTransactions(); // Get a reference to the view contact fragment. _fragmentContactDetail = (ContactDetailFragment) _mainActivity.getFragmentManager().findFragmentByTag(MainActivity.FRAGMENT_CONTACT_DETAIL_TAG); assertNotNull("ContactDetailFragment null", _fragmentContactDetail); // Get a Contact that was passed to the fragment. Contact contact = _fragmentContactDetail.getArguments().getParcelable(ContactDetailFragment.BUNDLE_KEY_CONTACT); assertNotNull("Contact null", contact); // Get the contact name displayed in the text box. EditText editText = (EditText) _fragmentContactDetail.getView().findViewById(R.id.editTextName); String contactName = editText.getText().toString(); assertTrue("Contact name | Expected: " + contactName + " Actual: " + ContactModel.getInstance(_mainActivity).getContact(contact.ContactID).Name, contactName.equals(ContactModel.getInstance(_mainActivity).getContact(contact.ContactID).Name)); // ***PAUSE Activity. getInstrumentation().callActivityOnPause(_mainActivity); // Edit the text in the Contact Name field. String newContactName = contactName + _mainActivity.getLocalClassName(); editText.setText(newContactName); editText = null; // *** RESUME Activity. getInstrumentation().callActivityOnResume(_mainActivity); // Just because, find the Fragment again and get the value // stored in the Contact Name field. _fragmentContactDetail = (ContactDetailFragment) _mainActivity.getFragmentManager().findFragmentByTag(MainActivity.FRAGMENT_CONTACT_DETAIL_TAG); editText = (EditText) _fragmentContactDetail.getView().findViewById(R.id.editTextName); contactName = editText.getText().toString(); // Is it the modified version? assertTrue("Contact name | Expected: " + contactName + " Actual: " + newContactName, contactName.equals(newContactName)); }
@LargeTest @FlakyTest(tolerance = TEST_TOLERANCE) public void testUIAutomatorStub() throws InterruptedException { while (server.isAlive()) Thread.sleep(100); }