Search in sources :

Example 6 with Behavior

use of android.support.design.widget.CoordinatorLayout.Behavior 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 7 with Behavior

use of android.support.design.widget.CoordinatorLayout.Behavior in project material-components-android by material-components.

the class CoordinatorLayoutTest method testNestedScrollingDispatchesToBehavior.

@Test
public void testNestedScrollingDispatchesToBehavior() throws Throwable {
    final CoordinatorLayoutActivity activity = activityTestRule.getActivity();
    final CoordinatorLayout col = activity.mCoordinatorLayout;
    // Now create a view and add it to the CoordinatorLayout with the spy behavior,
    // along with a NestedScrollView
    final ImageView imageView = new ImageView(activity);
    final CoordinatorLayout.Behavior behavior = spy(new NestedScrollingBehavior());
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            LayoutInflater.from(activity).inflate(R.layout.include_nestedscrollview, col, true);
            CoordinatorLayout.LayoutParams clp = new CoordinatorLayout.LayoutParams(200, 200);
            clp.setBehavior(behavior);
            col.addView(imageView, clp);
        }
    });
    // 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 onStartNestedScroll was called once
    verify(behavior, times(1)).onStartNestedScroll(// parent
    eq(col), // child
    eq(imageView), // target
    any(View.class), // direct child target
    any(View.class), // axes
    any(int.class));
    // Verify that the Behavior's onNestedScrollAccepted was called once
    verify(behavior, times(1)).onNestedScrollAccepted(// parent
    eq(col), // child
    eq(imageView), // target
    any(View.class), // direct child target
    any(View.class), // axes
    any(int.class));
    // Verify that the Behavior's onNestedPreScroll was called at least once
    verify(behavior, atLeastOnce()).onNestedPreScroll(// parent
    eq(col), // child
    eq(imageView), // target
    any(View.class), // dx
    any(int.class), // dy
    any(int.class), // consumed
    any(int[].class));
    // Verify that the Behavior's onNestedScroll was called at least once
    verify(behavior, atLeastOnce()).onNestedScroll(// parent
    eq(col), // child
    eq(imageView), // target
    any(View.class), // dx consumed
    any(int.class), // dy consumed
    any(int.class), // dx unconsumed
    any(int.class), // dy unconsumed
    any(int.class));
    // Verify that the Behavior's onStopNestedScroll was called once
    verify(behavior, times(1)).onStopNestedScroll(// parent
    eq(col), // child
    eq(imageView), // target
    any(View.class));
}
Also used : 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 8 with Behavior

use of android.support.design.widget.CoordinatorLayout.Behavior 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

Behavior (android.support.design.widget.CoordinatorLayout.Behavior)8 Espresso.onView (android.support.test.espresso.Espresso.onView)8 MediumTest (android.support.test.filters.MediumTest)8 View (android.view.View)8 ImageView (android.widget.ImageView)8 Test (org.junit.Test)8 DependentBehavior (android.support.design.testutils.CoordinatorLayoutUtils.DependentBehavior)6 CoordinatorLayoutActivity (android.support.design.testapp.CoordinatorLayoutActivity)3 Rect (android.graphics.Rect)2