Search in sources :

Example 1 with DependentBehavior

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

the class CoordinatorLayoutTest method testDependentViewChanged.

@Test
public void testDependentViewChanged() throws Throwable {
    final CoordinatorLayout col = activityTestRule.getActivity().mCoordinatorLayout;
    // Add two views, A & B, where B depends on A
    final View viewA = new View(col.getContext());
    final CoordinatorLayout.LayoutParams lpA = col.generateDefaultLayoutParams();
    lpA.width = 100;
    lpA.height = 100;
    final View viewB = new View(col.getContext());
    final CoordinatorLayout.LayoutParams lpB = col.generateDefaultLayoutParams();
    lpB.width = 100;
    lpB.height = 100;
    final CoordinatorLayout.Behavior behavior = spy(new DependentBehavior(viewA));
    lpB.setBehavior(behavior);
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            col.addView(viewA, lpA);
            col.addView(viewB, lpB);
        }
    });
    getInstrumentation().waitForIdleSync();
    // Reset the Behavior since onDependentViewChanged may have already been called as part of
    // any layout/draw passes already
    reset(behavior);
    // Now offset view A
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            ViewCompat.offsetLeftAndRight(viewA, 20);
            ViewCompat.offsetTopAndBottom(viewA, 20);
        }
    });
    getInstrumentation().waitForIdleSync();
    // And assert that view B's Behavior was called appropriately
    verify(behavior, times(1)).onDependentViewChanged(col, viewB, viewA);
}
Also used : DependentBehavior(android.support.design.testutils.CoordinatorLayoutUtils.DependentBehavior) Behavior(android.support.design.widget.CoordinatorLayout.Behavior) ImageView(android.widget.ImageView) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 2 with DependentBehavior

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

the class CoordinatorLayoutTest method testNestedScrollingTriggeringDependentViewChanged.

@Test
public void testNestedScrollingTriggeringDependentViewChanged() throws Throwable {
    final CoordinatorLayoutActivity activity = activityTestRule.getActivity();
    final CoordinatorLayout col = activity.mCoordinatorLayout;
    // First a NestedScrollView to trigger nested scrolling
    final View scrollView = LayoutInflater.from(activity).inflate(R.layout.include_nestedscrollview, col, false);
    // Now create a View and Behavior which depend on the scrollview
    final ImageView dependentView = new ImageView(activity);
    final CoordinatorLayout.Behavior dependentBehavior = spy(new DependentBehavior(scrollView));
    // Finally a view which accepts nested scrolling in the CoordinatorLayout
    final ImageView nestedScrollAwareView = new ImageView(activity);
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // First add the ScrollView
            col.addView(scrollView);
            // Now add the view which depends on the scrollview
            CoordinatorLayout.LayoutParams clp = new CoordinatorLayout.LayoutParams(200, 200);
            clp.setBehavior(dependentBehavior);
            col.addView(dependentView, clp);
            // Now add the nested scrolling aware view
            clp = new CoordinatorLayout.LayoutParams(200, 200);
            clp.setBehavior(new NestedScrollingBehavior());
            col.addView(nestedScrollAwareView, clp);
        }
    });
    // Wait for any layouts, and reset the Behavior so that the call counts are 0
    getInstrumentation().waitForIdleSync();
    reset(dependentBehavior);
    // Now vertically swipe up on the NSV, causing nested scrolling to occur
    onView(withId(R.id.nested_scrollview)).perform(swipeUp());
    // Verify that the Behavior's onDependentViewChanged is not called due to the
    // nested scroll
    verify(dependentBehavior, never()).onDependentViewChanged(// parent
    eq(col), // child
    eq(dependentView), // axes
    eq(scrollView));
}
Also used : DependentBehavior(android.support.design.testutils.CoordinatorLayoutUtils.DependentBehavior) Behavior(android.support.design.widget.CoordinatorLayout.Behavior) CoordinatorLayoutActivity(android.support.design.testapp.CoordinatorLayoutActivity) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 3 with DependentBehavior

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

the class CoordinatorLayoutTest method testDependentViewRemoved.

@Test
public void testDependentViewRemoved() throws Throwable {
    final CoordinatorLayout col = activityTestRule.getActivity().mCoordinatorLayout;
    // Add two views, A & B, where B depends on A
    final View viewA = new View(col.getContext());
    final View viewB = new View(col.getContext());
    final CoordinatorLayout.LayoutParams lpB = col.generateDefaultLayoutParams();
    final CoordinatorLayout.Behavior behavior = spy(new DependentBehavior(viewA));
    lpB.setBehavior(behavior);
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            col.addView(viewA);
            col.addView(viewB, lpB);
        }
    });
    getInstrumentation().waitForIdleSync();
    // Now remove view A
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            col.removeView(viewA);
        }
    });
    // And assert that View B's Behavior was called appropriately
    verify(behavior, times(1)).onDependentViewRemoved(col, viewB, viewA);
}
Also used : DependentBehavior(android.support.design.testutils.CoordinatorLayoutUtils.DependentBehavior) Behavior(android.support.design.widget.CoordinatorLayout.Behavior) ImageView(android.widget.ImageView) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 4 with DependentBehavior

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

the class CoordinatorLayoutTest method testGetDependenciesAfterDependentViewRemoved.

@Test
public void testGetDependenciesAfterDependentViewRemoved() throws Throwable {
    final CoordinatorLayout col = activityTestRule.getActivity().mCoordinatorLayout;
    // Add two views, A & B, where B depends on A
    final View viewA = new View(col.getContext());
    final View viewB = new View(col.getContext());
    final CoordinatorLayout.LayoutParams lpB = col.generateDefaultLayoutParams();
    final CoordinatorLayout.Behavior behavior = new DependentBehavior(viewA) {

        @Override
        public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
            parent.getDependencies(child);
        }
    };
    lpB.setBehavior(behavior);
    // Now add views
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            col.addView(viewA);
            col.addView(viewB, lpB);
        }
    });
    // Wait for a layout
    getInstrumentation().waitForIdleSync();
    // Now remove view A, which will trigger onDependentViewRemoved() on view B's behavior
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            col.removeView(viewA);
        }
    });
}
Also used : DependentBehavior(android.support.design.testutils.CoordinatorLayoutUtils.DependentBehavior) Behavior(android.support.design.widget.CoordinatorLayout.Behavior) ImageView(android.widget.ImageView) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

DependentBehavior (android.support.design.testutils.CoordinatorLayoutUtils.DependentBehavior)4 Behavior (android.support.design.widget.CoordinatorLayout.Behavior)4 Espresso.onView (android.support.test.espresso.Espresso.onView)4 MediumTest (android.support.test.filters.MediumTest)4 View (android.view.View)4 ImageView (android.widget.ImageView)4 Test (org.junit.Test)4 CoordinatorLayoutActivity (android.support.design.testapp.CoordinatorLayoutActivity)1