Search in sources :

Example 1 with TestDrawable

use of android.support.design.testutils.TestDrawable in project material-components-android by material-components.

the class NavigationViewTest method testIconTinting.

@Test
public void testIconTinting() {
    // Open our drawer
    onView(withId(R.id.drawer_layout)).perform(openDrawer(GravityCompat.START));
    final Resources res = activityTestRule.getActivity().getResources();
    @ColorInt final int redFill = ResourcesCompat.getColor(res, R.color.test_red, null);
    @ColorInt final int greenFill = ResourcesCompat.getColor(res, R.color.test_green, null);
    @ColorInt final int blueFill = ResourcesCompat.getColor(res, R.color.test_blue, null);
    final int iconSize = res.getDimensionPixelSize(R.dimen.drawable_small_size);
    onView(withId(R.id.start_drawer)).perform(setIconForMenuItem(R.id.destination_home, new TestDrawable(redFill, iconSize, iconSize)));
    onView(withId(R.id.start_drawer)).perform(setIconForMenuItem(R.id.destination_profile, new TestDrawable(greenFill, iconSize, iconSize)));
    onView(withId(R.id.start_drawer)).perform(setIconForMenuItem(R.id.destination_people, new TestDrawable(blueFill, iconSize, iconSize)));
    @ColorInt final int defaultTintColor = ResourcesCompat.getColor(res, R.color.emerald_translucent, null);
    // We're allowing a margin of error in checking the color of the items' icons.
    // This is due to the translucent color being used in the icon tinting
    // and off-by-one discrepancies of SRC_IN when it's compositing
    // translucent color. Note that all the checks below are written for the current
    // logic on NavigationView that uses the default SRC_IN tint mode - effectively
    // replacing all non-transparent pixels in the destination (original icon) with
    // our translucent tint color.
    final int allowedComponentVariance = 1;
    // Note that here we're tying ourselves to the implementation details of the
    // internal structure of the NavigationView. Specifically, we're checking the
    // start drawable of the text view with the specific text. If the internal
    // implementation of NavigationView changes, the second Matcher in the lookups
    // below will need to be tweaked.
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_home)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(defaultTintColor, allowedComponentVariance)));
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_profile)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(defaultTintColor, allowedComponentVariance)));
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_people)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(defaultTintColor, allowedComponentVariance)));
    @ColorInt final int newTintColor = ResourcesCompat.getColor(res, R.color.red_translucent, null);
    onView(withId(R.id.start_drawer)).perform(setItemIconTintList(ResourcesCompat.getColorStateList(res, R.color.color_state_list_red_translucent, null)));
    // Check that all menu items with icons now have icons tinted with the newly set color
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_home)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(newTintColor, allowedComponentVariance)));
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_profile)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(newTintColor, allowedComponentVariance)));
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_people)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(newTintColor, allowedComponentVariance)));
    // And now remove all icon tinting
    onView(withId(R.id.start_drawer)).perform(setItemIconTintList(null));
    // And verify that all menu items with icons now have the original colors for their icons.
    // Note that since there is no tinting at this point, we don't allow any color variance
    // in these checks.
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_home)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(redFill, 0)));
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_profile)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(greenFill, 0)));
    onView(allOf(withText(mMenuStringContent.get(R.id.destination_people)), isDescendantOfA(withId(R.id.start_drawer)))).check(matches(withStartDrawableFilledWith(blueFill, 0)));
}
Also used : ColorInt(android.support.annotation.ColorInt) Resources(android.content.res.Resources) TestDrawable(android.support.design.testutils.TestDrawable) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 2 with TestDrawable

use of android.support.design.testutils.TestDrawable in project material-components-android by material-components.

the class BottomNavigationViewTest method testIconTinting.

@Test
@SmallTest
public void testIconTinting() {
    final Resources res = activityTestRule.getActivity().getResources();
    @ColorInt final int redFill = ResourcesCompat.getColor(res, R.color.test_red, null);
    @ColorInt final int greenFill = ResourcesCompat.getColor(res, R.color.test_green, null);
    @ColorInt final int blueFill = ResourcesCompat.getColor(res, R.color.test_blue, null);
    final int iconSize = res.getDimensionPixelSize(R.dimen.drawable_small_size);
    onView(withId(R.id.bottom_navigation)).perform(setIconForMenuItem(R.id.destination_home, new TestDrawable(redFill, iconSize, iconSize)));
    onView(withId(R.id.bottom_navigation)).perform(setIconForMenuItem(R.id.destination_profile, new TestDrawable(greenFill, iconSize, iconSize)));
    onView(withId(R.id.bottom_navigation)).perform(setIconForMenuItem(R.id.destination_people, new TestDrawable(blueFill, iconSize, iconSize)));
    @ColorInt final int defaultTintColor = ResourcesCompat.getColor(res, R.color.emerald_translucent, null);
    // We're allowing a margin of error in checking the color of the items' icons.
    // This is due to the translucent color being used in the icon tinting
    // and off-by-one discrepancies of SRC_IN when it's compositing
    // translucent color. Note that all the checks below are written for the current
    // logic on BottomNavigationView that uses the default SRC_IN tint mode - effectively
    // replacing all non-transparent pixels in the destination (original icon) with
    // our translucent tint color.
    final int allowedComponentVariance = 1;
    // Note that here we're tying ourselves to the implementation details of the internal
    // structure of the BottomNavigationView. Specifically, we're checking the drawable the
    // ImageView with id R.id.icon. If the internal implementation of BottomNavigationView
    // changes, the second Matcher in the lookups below will need to be tweaked.
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_home)))).check(matches(TestUtilsMatchers.drawable(defaultTintColor, allowedComponentVariance)));
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_profile)))).check(matches(TestUtilsMatchers.drawable(defaultTintColor, allowedComponentVariance)));
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_people)))).check(matches(TestUtilsMatchers.drawable(defaultTintColor, allowedComponentVariance)));
    @ColorInt final int newTintColor = ResourcesCompat.getColor(res, R.color.red_translucent, null);
    onView(withId(R.id.bottom_navigation)).perform(setItemIconTintList(ResourcesCompat.getColorStateList(res, R.color.color_state_list_red_translucent, null)));
    // Check that all menu items with icons now have icons tinted with the newly set color
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_home)))).check(matches(TestUtilsMatchers.drawable(newTintColor, allowedComponentVariance)));
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_profile)))).check(matches(TestUtilsMatchers.drawable(newTintColor, allowedComponentVariance)));
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_people)))).check(matches(TestUtilsMatchers.drawable(newTintColor, allowedComponentVariance)));
    // And now remove all icon tinting
    onView(withId(R.id.bottom_navigation)).perform(setItemIconTintList(null));
    // And verify that all menu items with icons now have the original colors for their icons.
    // Note that since there is no tinting at this point, we don't allow any color variance
    // in these checks.
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_home)))).check(matches(TestUtilsMatchers.drawable(redFill, allowedComponentVariance)));
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_profile)))).check(matches(TestUtilsMatchers.drawable(greenFill, allowedComponentVariance)));
    onView(allOf(withId(R.id.icon), isDescendantOfA(withId(R.id.destination_people)))).check(matches(TestUtilsMatchers.drawable(blueFill, allowedComponentVariance)));
}
Also used : ColorInt(android.support.annotation.ColorInt) Resources(android.content.res.Resources) TestDrawable(android.support.design.testutils.TestDrawable) SmallTest(android.support.test.filters.SmallTest) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest)

Aggregations

Resources (android.content.res.Resources)2 ColorInt (android.support.annotation.ColorInt)2 TestDrawable (android.support.design.testutils.TestDrawable)2 Test (org.junit.Test)2 UiThreadTest (android.support.test.annotation.UiThreadTest)1 MediumTest (android.support.test.filters.MediumTest)1 SmallTest (android.support.test.filters.SmallTest)1