AppBarLayout + CollapsingToolbarLayout tests fixes

- Fixed flakiness due to waiting for snapping to
  happen
- Tweaked the layouts so that text and content is
  visible on screen (not light-on-light)
- Make tests on API 21+ use translucent status bar,
  and updated tests to suit
- Fixed issues caugh while testing

BUG: 30271522
BUG: 30225781

Change-Id: I9f368cb8f059e9d3224473bf6c2ddfd2ce92cbdf
diff --git a/design/src/android/support/design/widget/AppBarLayout.java b/design/src/android/support/design/widget/AppBarLayout.java
index 53ff559..c253029 100644
--- a/design/src/android/support/design/widget/AppBarLayout.java
+++ b/design/src/android/support/design/widget/AppBarLayout.java
@@ -1117,7 +1117,7 @@
 
         @Override
         int setHeaderTopBottomOffset(CoordinatorLayout coordinatorLayout,
-                AppBarLayout header, int newOffset, int minOffset, int maxOffset) {
+                AppBarLayout appBarLayout, int newOffset, int minOffset, int maxOffset) {
             final int curOffset = getTopBottomOffsetForScrollingSibling();
             int consumed = 0;
 
@@ -1125,7 +1125,6 @@
                 // If we have some scrolling range, and we're currently within the min and max
                 // offsets, calculate a new offset
                 newOffset = MathUtils.constrain(newOffset, minOffset, maxOffset);
-                AppBarLayout appBarLayout = (AppBarLayout) header;
                 if (curOffset != newOffset) {
                     final int interpolatedOffset = appBarLayout.hasChildWithInterpolator()
                             ? interpolateOffset(appBarLayout, newOffset)
@@ -1161,6 +1160,11 @@
             return consumed;
         }
 
+        @VisibleForTesting
+        boolean isOffsetAnimatorRunning() {
+            return mOffsetAnimator != null && mOffsetAnimator.isRunning();
+        }
+
         private int interpolateOffset(AppBarLayout layout, final int offset) {
             final int absOffset = Math.abs(offset);
 
@@ -1410,9 +1414,8 @@
                     ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior();
             if (behavior instanceof Behavior) {
                 // Offset the child, pinning it to the bottom the header-dependency, maintaining
-                // any vertical gap, and overlap
+                // any vertical gap and overlap
                 final Behavior ablBehavior = (Behavior) behavior;
-                final int offset = ablBehavior.getTopBottomOffsetForScrollingSibling();
                 ViewCompat.offsetTopAndBottom(child, (dependency.getBottom() - child.getTop())
                         + ablBehavior.mOffsetDelta
                         + getVerticalLayoutGap()
diff --git a/design/src/android/support/design/widget/CollapsingToolbarLayout.java b/design/src/android/support/design/widget/CollapsingToolbarLayout.java
index 6b2595c..0ae7f7c 100644
--- a/design/src/android/support/design/widget/CollapsingToolbarLayout.java
+++ b/design/src/android/support/design/widget/CollapsingToolbarLayout.java
@@ -409,8 +409,23 @@
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         super.onLayout(changed, left, top, right, bottom);
 
-        // Update the collapsed bounds by getting it's transformed bounds. This needs to be done
-        // before the children are offset below
+        // Update our child view offset helpers
+        for (int i = 0, z = getChildCount(); i < z; i++) {
+            final View child = getChildAt(i);
+
+            if (mLastInsets != null && !ViewCompat.getFitsSystemWindows(child)) {
+                final int insetTop = mLastInsets.getSystemWindowInsetTop();
+                if (child.getTop() < insetTop) {
+                    // If the child isn't set to fit system windows but is drawing within the inset
+                    // offset it down
+                    ViewCompat.offsetTopAndBottom(child, insetTop);
+                }
+            }
+
+            getViewOffsetHelper(child).onViewLayout();
+        }
+
+        // Update the collapsed bounds by getting it's transformed bounds
         if (mCollapsingTitleEnabled && mDummyView != null) {
             // We only draw the title if the dummy view is being displayed (Toolbar removes
             // views if there is no space)
@@ -446,23 +461,6 @@
             }
         }
 
-        // Update our child view offset helpers
-        for (int i = 0, z = getChildCount(); i < z; i++) {
-            final View child = getChildAt(i);
-
-            if (mLastInsets != null && !ViewCompat.getFitsSystemWindows(child)) {
-                final int insetTop = mLastInsets.getSystemWindowInsetTop();
-                if (child.getTop() < insetTop) {
-                    // If the child isn't set to fit system windows but is drawing within the inset
-                    // offset it down
-                    ViewCompat.offsetTopAndBottom(child, insetTop);
-                }
-            }
-
-            getViewOffsetHelper(child).onViewLayout();
-        }
-
-        ensureToolbar();
         // Finally, set our minimum height to enable proper AppBarLayout collapsing
         if (mToolbar != null) {
             if (mCollapsingTitleEnabled && TextUtils.isEmpty(mCollapsingTextHelper.getText())) {
@@ -1237,7 +1235,6 @@
         final ViewOffsetHelper offsetHelper = getViewOffsetHelper(child);
         final LayoutParams lp = (LayoutParams) child.getLayoutParams();
         return getHeight()
-                + (mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0)
                 - offsetHelper.getLayoutTop()
                 - child.getHeight()
                 - lp.bottomMargin;
diff --git a/design/src/android/support/design/widget/HeaderScrollingViewBehavior.java b/design/src/android/support/design/widget/HeaderScrollingViewBehavior.java
index 0ba0bf0..d1bec2d 100644
--- a/design/src/android/support/design/widget/HeaderScrollingViewBehavior.java
+++ b/design/src/android/support/design/widget/HeaderScrollingViewBehavior.java
@@ -142,11 +142,8 @@
     }
 
     final int getOverlapPixelsForOffset(final View header) {
-        return mOverlayTop == 0
-                ? 0
-                : MathUtils.constrain(Math.round(getOverlapRatioForOffset(header) * mOverlayTop),
-                        0, mOverlayTop);
-
+        return mOverlayTop == 0 ? 0 : MathUtils.constrain(
+                (int) (getOverlapRatioForOffset(header) * mOverlayTop), 0, mOverlayTop);
     }
 
     private static int resolveGravity(int gravity) {
diff --git a/design/tests/res/layout/design_appbar_anchored_fab_margin_bottom.xml b/design/tests/res/layout/design_appbar_anchored_fab_margin_bottom.xml
index 151e213..aaed2f9 100644
--- a/design/tests/res/layout/design_appbar_anchored_fab_margin_bottom.xml
+++ b/design/tests/res/layout/design_appbar_anchored_fab_margin_bottom.xml
@@ -19,7 +19,8 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <include layout="@layout/design_content_appbar_toolbar_collapse_pin" />
 
diff --git a/design/tests/res/layout/design_appbar_anchored_fab_margin_left.xml b/design/tests/res/layout/design_appbar_anchored_fab_margin_left.xml
index bb139d6..9fabb7b 100644
--- a/design/tests/res/layout/design_appbar_anchored_fab_margin_left.xml
+++ b/design/tests/res/layout/design_appbar_anchored_fab_margin_left.xml
@@ -19,7 +19,8 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <include layout="@layout/design_content_appbar_toolbar_collapse_pin" />
 
diff --git a/design/tests/res/layout/design_appbar_anchored_fab_margin_right.xml b/design/tests/res/layout/design_appbar_anchored_fab_margin_right.xml
index b67dbd6..ea2e1c0 100644
--- a/design/tests/res/layout/design_appbar_anchored_fab_margin_right.xml
+++ b/design/tests/res/layout/design_appbar_anchored_fab_margin_right.xml
@@ -19,7 +19,8 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <include layout="@layout/design_content_appbar_toolbar_collapse_pin" />
 
diff --git a/design/tests/res/layout/design_appbar_anchored_fab_margin_top.xml b/design/tests/res/layout/design_appbar_anchored_fab_margin_top.xml
index ce20235..763d013 100644
--- a/design/tests/res/layout/design_appbar_anchored_fab_margin_top.xml
+++ b/design/tests/res/layout/design_appbar_anchored_fab_margin_top.xml
@@ -19,7 +19,8 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <include layout="@layout/design_content_appbar_toolbar_collapse_pin" />
 
diff --git a/design/tests/res/layout/design_appbar_toolbar_collapse_pin.xml b/design/tests/res/layout/design_appbar_toolbar_collapse_pin.xml
index 6c8b4b0..e6cfb96 100644
--- a/design/tests/res/layout/design_appbar_toolbar_collapse_pin.xml
+++ b/design/tests/res/layout/design_appbar_toolbar_collapse_pin.xml
@@ -18,7 +18,8 @@
 <android.support.design.widget.CoordinatorLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <include layout="@layout/design_content_appbar_toolbar_collapse_pin" />
 
diff --git a/design/tests/res/layout/design_appbar_toolbar_collapse_pin_margins.xml b/design/tests/res/layout/design_appbar_toolbar_collapse_pin_margins.xml
index 92a7268..4eed2e2 100644
--- a/design/tests/res/layout/design_appbar_toolbar_collapse_pin_margins.xml
+++ b/design/tests/res/layout/design_appbar_toolbar_collapse_pin_margins.xml
@@ -18,9 +18,9 @@
 <android.support.design.widget.CoordinatorLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
-    android:id="@+id/col"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <android.support.design.widget.AppBarLayout
         android:id="@+id/app_bar"
diff --git a/design/tests/res/layout/design_appbar_toolbar_collapse_pin_with_fab.xml b/design/tests/res/layout/design_appbar_toolbar_collapse_pin_with_fab.xml
index 9c5797b..f5a2557 100644
--- a/design/tests/res/layout/design_appbar_toolbar_collapse_pin_with_fab.xml
+++ b/design/tests/res/layout/design_appbar_toolbar_collapse_pin_with_fab.xml
@@ -19,7 +19,8 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <include layout="@layout/design_content_appbar_toolbar_collapse_pin" />
 
@@ -29,8 +30,8 @@
         android:layout_width="wrap_content"
         app:layout_anchor="@+id/app_bar"
         app:layout_anchorGravity="bottom|right|end"
-        android:src="@drawable/ic_add"
         android:layout_marginRight="16dp"
-        android:clickable="true" />
+        android:clickable="true"
+        app:backgroundTint="#FF0000"/>
 
 </android.support.design.widget.CoordinatorLayout>
diff --git a/design/tests/res/layout/design_appbar_toolbar_collapse_scroll.xml b/design/tests/res/layout/design_appbar_toolbar_collapse_scroll.xml
index c409792..8c68021 100644
--- a/design/tests/res/layout/design_appbar_toolbar_collapse_scroll.xml
+++ b/design/tests/res/layout/design_appbar_toolbar_collapse_scroll.xml
@@ -32,8 +32,7 @@
             android:id="@+id/collapsing_app_bar"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
-            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
-            android:fitsSystemWindows="true">
+            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
 
             <android.support.v7.widget.Toolbar
                 android:id="@+id/toolbar"
diff --git a/design/tests/res/layout/design_appbar_toolbar_collapse_with_image.xml b/design/tests/res/layout/design_appbar_toolbar_collapse_with_image.xml
index f55f470..a4d667f 100644
--- a/design/tests/res/layout/design_appbar_toolbar_collapse_with_image.xml
+++ b/design/tests/res/layout/design_appbar_toolbar_collapse_with_image.xml
@@ -19,13 +19,15 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true">
 
     <android.support.design.widget.AppBarLayout
         android:id="@+id/app_bar"
         android:layout_width="match_parent"
         android:layout_height="@dimen/appbar_height"
-        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
+        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
+        android:fitsSystemWindows="true">
 
         <android.support.design.widget.CollapsingToolbarLayout
             android:id="@+id/collapsing_app_bar"
@@ -39,7 +41,8 @@
                 android:layout_height="match_parent"
                 app:layout_collapseMode="parallax"
                 android:src="@drawable/photo"
-                android:scaleType="centerCrop"/>
+                android:scaleType="centerCrop"
+                android:fitsSystemWindows="true"/>
 
             <android.support.v7.widget.Toolbar
                 android:id="@+id/toolbar"
diff --git a/design/tests/res/layout/design_content_appbar_toolbar_collapse_pin.xml b/design/tests/res/layout/design_content_appbar_toolbar_collapse_pin.xml
index fed016f..961ce41 100644
--- a/design/tests/res/layout/design_content_appbar_toolbar_collapse_pin.xml
+++ b/design/tests/res/layout/design_content_appbar_toolbar_collapse_pin.xml
@@ -21,7 +21,8 @@
     <android.support.design.widget.AppBarLayout
         android:id="@+id/app_bar"
         android:layout_width="match_parent"
-        android:layout_height="@dimen/appbar_height">
+        android:layout_height="@dimen/appbar_height"
+        android:fitsSystemWindows="true">
 
         <android.support.design.widget.CollapsingToolbarLayout
             android:id="@+id/collapsing_app_bar"
diff --git a/design/tests/res/layout/design_tabs_viewpager.xml b/design/tests/res/layout/design_tabs_viewpager.xml
index 877c690..9cc7bcb 100644
--- a/design/tests/res/layout/design_tabs_viewpager.xml
+++ b/design/tests/res/layout/design_tabs_viewpager.xml
@@ -28,9 +28,7 @@
         android:layout_width="match_parent"
         android:layout_height="?attr/actionBarSize"
         android:background="?attr/colorPrimary"
-        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
-        app:contentInsetStart="72dp"
-        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
+        app:contentInsetStart="72dp"/>
 
     <include layout="@layout/tab_layout_unbound" />
 
diff --git a/design/tests/res/layout/tab_layout_bound_max.xml b/design/tests/res/layout/tab_layout_bound_max.xml
index 90a0943..9958edf 100644
--- a/design/tests/res/layout/tab_layout_bound_max.xml
+++ b/design/tests/res/layout/tab_layout_bound_max.xml
@@ -21,8 +21,6 @@
         android:id="@+id/tabs"
         android:layout_height="wrap_content"
         android:layout_width="match_parent"
-        android:background="?attr/colorPrimary"
-        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
         app:tabMode="scrollable"
         app:tabContentStart="72dp"
         app:tabMaxWidth="@dimen/tab_width_limit_medium" />
diff --git a/design/tests/res/layout/tab_layout_bound_min.xml b/design/tests/res/layout/tab_layout_bound_min.xml
index c8c8554..a05bd64 100644
--- a/design/tests/res/layout/tab_layout_bound_min.xml
+++ b/design/tests/res/layout/tab_layout_bound_min.xml
@@ -21,8 +21,6 @@
         android:id="@+id/tabs"
         android:layout_height="wrap_content"
         android:layout_width="match_parent"
-        android:background="?attr/colorPrimary"
-        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
         app:tabMode="scrollable"
         app:tabContentStart="72dp"
         app:tabMinWidth="@dimen/tab_width_limit_medium" />
diff --git a/design/tests/res/layout/tab_layout_bound_minmax.xml b/design/tests/res/layout/tab_layout_bound_minmax.xml
index 5f76659..0e34286 100644
--- a/design/tests/res/layout/tab_layout_bound_minmax.xml
+++ b/design/tests/res/layout/tab_layout_bound_minmax.xml
@@ -21,8 +21,6 @@
         android:id="@+id/tabs"
         android:layout_height="wrap_content"
         android:layout_width="match_parent"
-        android:background="?attr/colorPrimary"
-        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
         app:tabMode="scrollable"
         app:tabContentStart="72dp"
         app:tabMinWidth="@dimen/tab_width_limit_small"
diff --git a/design/tests/res/layout/tab_layout_unbound.xml b/design/tests/res/layout/tab_layout_unbound.xml
index 4a49916..fb51d4d 100644
--- a/design/tests/res/layout/tab_layout_unbound.xml
+++ b/design/tests/res/layout/tab_layout_unbound.xml
@@ -21,7 +21,5 @@
         android:id="@+id/tabs"
         android:layout_height="wrap_content"
         android:layout_width="match_parent"
-        android:background="?attr/colorPrimary"
-        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
         app:tabMode="scrollable"
         app:tabContentStart="72dp"/>
diff --git a/design/tests/res/values/dimens.xml b/design/tests/res/values/dimens.xml
index 134463c..2451b42 100644
--- a/design/tests/res/values/dimens.xml
+++ b/design/tests/res/values/dimens.xml
@@ -26,7 +26,7 @@
     <dimen name="drawable_medium_size">16dip</dimen>
     <dimen name="drawable_large_size">20dip</dimen>
 
-    <dimen name="appbar_height">128dip</dimen>
+    <dimen name="appbar_height">192dip</dimen>
     <dimen name="fab_margin">32dip</dimen>
 
     <dimen name="fab_mini_height">40dp</dimen>
diff --git a/design/tests/src/android/support/design/widget/AppBarWithCollapsingToolbarTest.java b/design/tests/src/android/support/design/widget/AppBarWithCollapsingToolbarTest.java
index a17ed77..9754f55 100644
--- a/design/tests/src/android/support/design/widget/AppBarWithCollapsingToolbarTest.java
+++ b/design/tests/src/android/support/design/widget/AppBarWithCollapsingToolbarTest.java
@@ -22,6 +22,7 @@
 import android.os.SystemClock;
 import android.support.design.test.R;
 import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SdkSuppress;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.widget.ImageView;
 
@@ -51,6 +52,7 @@
         final int toolbarHeight = mToolbar.getHeight();
         final int appbarHeight = mAppBar.getHeight();
         final int longSwipeAmount = 3 * appbarHeight / 2;
+        final int reallyLongSwipeAmount = 2 * appbarHeight;
         final int shortSwipeAmount = toolbarHeight;
 
         assertAppBarElevation(0f);
@@ -65,19 +67,21 @@
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should be visually snapped below the system status bar.
         // Allow for off-by-a-pixel margin of error.
-        assertEquals(originalAppbarTop + toolbarHeight, appbarOnScreenXY[1] + appbarHeight, 1);
+        assertEquals(originalAppbarTop + toolbarHeight + mAppBar.getTopInset(),
+                appbarOnScreenXY[1] + appbarHeight, 1);
 
         // Perform another swipe-up gesture
         performVerticalSwipeUpGesture(
                 R.id.coordinator_layout,
                 centerX,
-                originalAppbarBottom,
+                appbarOnScreenXY[1] + appbarHeight + 5,
                 shortSwipeAmount);
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should still be visually snapped below the system status bar
         // as it is in the pinned mode. Allow for off-by-a-pixel margin of error.
-        assertEquals(originalAppbarTop + toolbarHeight, appbarOnScreenXY[1] + appbarHeight, 1);
+        assertEquals(originalAppbarTop + toolbarHeight + mAppBar.getTopInset(),
+                appbarOnScreenXY[1] + appbarHeight, 1);
         assertAppBarElevation(mDefaultElevationValue);
 
         // Perform a short swipe-down gesture across the horizontal center of the screen.
@@ -93,7 +97,8 @@
         // At this point the app bar should still be visually snapped below the system status bar
         // as it is in the pinned mode and we haven't fully swiped down the content below the
         // app bar. Allow for off-by-a-pixel margin of error.
-        assertEquals(originalAppbarTop + toolbarHeight, appbarOnScreenXY[1] + appbarHeight, 1);
+        assertEquals(originalAppbarTop + toolbarHeight + mAppBar.getTopInset(),
+                appbarOnScreenXY[1] + appbarHeight, 1);
         assertAppBarElevation(mDefaultElevationValue);
 
         // Perform another swipe-down gesture across the horizontal center of the screen.
@@ -101,7 +106,7 @@
                 R.id.coordinator_layout,
                 centerX,
                 originalAppbarBottom,
-                longSwipeAmount);
+                reallyLongSwipeAmount);
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should be in its original position.
@@ -149,6 +154,7 @@
         final int toolbarHeight = mToolbar.getHeight();
         final int appbarHeight = mAppBar.getHeight();
         final int longSwipeAmount = 3 * appbarHeight / 2;
+        final int reallyLongSwipeAmount = 2 * appbarHeight;
         final int shortSwipeAmount = toolbarHeight;
 
         assertAppBarElevation(0f);
@@ -203,7 +209,7 @@
                 R.id.coordinator_layout,
                 centerX,
                 originalAppbarBottom,
-                longSwipeAmount);
+                reallyLongSwipeAmount);
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should be in its original position.
@@ -384,8 +390,14 @@
 
     }
 
+    /**
+     * This test only runs on API 11+ since FrameLayout (which CollapsingToolbarLayout
+     * inherits from) has an issue with measuring children with margins when run on earlier
+     * versions of the platform.
+     */
     @Test
-    public void testPinnedToolbarWithMargins() throws Throwable {
+    @SdkSuppress(minSdkVersion = 11)
+    public void testPinnedToolbarWithMargins() {
         configureContent(R.layout.design_appbar_toolbar_collapse_pin_margins,
                 R.string.design_appbar_collapsing_toolbar_pin_margins);
 
@@ -418,9 +430,10 @@
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         mToolbar.getLocationOnScreen(toolbarOnScreenXY);
         // At this point the toolbar should be visually pinned to the bottom of the appbar layout,
-        // observing it's margins
+        // observing it's margins and top inset
         // The toolbar should still be visually pinned to the bottom of the appbar layout
-        assertEquals(originalAppbarTop, toolbarOnScreenXY[1] - toolbarLp.topMargin, 1);
+        assertEquals(originalAppbarTop + mAppBar.getTopInset(),
+                toolbarOnScreenXY[1] - toolbarLp.topMargin, 1);
 
         // Swipe up again, this time just 50% of the margin size
         swipeAmount = toolbarVerticalMargins / 2;
diff --git a/design/tests/src/android/support/design/widget/AppBarWithToolbarAndTabsTest.java b/design/tests/src/android/support/design/widget/AppBarWithToolbarAndTabsTest.java
index 0c715de..a4f4a89 100644
--- a/design/tests/src/android/support/design/widget/AppBarWithToolbarAndTabsTest.java
+++ b/design/tests/src/android/support/design/widget/AppBarWithToolbarAndTabsTest.java
@@ -16,6 +16,7 @@
 
 package android.support.design.widget;
 
+import android.os.SystemClock;
 import android.support.annotation.LayoutRes;
 import android.support.annotation.StringRes;
 import android.support.design.test.R;
@@ -25,9 +26,7 @@
 import org.junit.Test;
 
 import static android.support.design.testutils.TestUtilsActions.addTabs;
-import static android.support.design.testutils.TestUtilsActions.waitUntilIdle;
 import static android.support.test.espresso.Espresso.onView;
-import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
 import static android.support.test.espresso.matcher.ViewMatchers.withId;
 import static org.junit.Assert.assertEquals;
 
@@ -266,7 +265,7 @@
                 toolbarHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should be in its original position as it
@@ -286,7 +285,7 @@
                 3 * toolbarHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should "snap" the toolbar away and align the tab layout below
@@ -304,7 +303,7 @@
                 tabsHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should "snap" back to align the tab layout below
@@ -321,7 +320,7 @@
                 3 * tabsHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should not be visually "present" on the screen, with its bottom
@@ -338,7 +337,7 @@
                 tabsHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should still not be visually "present" on the screen, with
@@ -357,7 +356,7 @@
                 3 * tabsHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should "snap" the toolbar away and align the tab layout below
@@ -374,7 +373,7 @@
                 toolbarHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should still align the tab layout below
@@ -391,7 +390,7 @@
                 3 * tabsHeight / 4);
 
         // Wait for the snap animation to be done
-        onView(isRoot()).perform(waitUntilIdle());
+        waitForSnapAnimationToFinish();
 
         mAppBar.getLocationOnScreen(appbarOnScreenXY);
         // At this point the app bar should be in its original position.
@@ -400,4 +399,12 @@
         assertEquals(originalAppbarBottom, appbarOnScreenXY[1] + appbarHeight, 1);
         assertAppBarElevation(mDefaultElevationValue);
     }
+
+    private void waitForSnapAnimationToFinish() {
+        final AppBarLayout.Behavior behavior = (AppBarLayout.Behavior)
+                ((CoordinatorLayout.LayoutParams) mAppBar.getLayoutParams()).getBehavior();
+        while (behavior.isOffsetAnimatorRunning()) {
+            SystemClock.sleep(16);
+        }
+    }
 }