/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.material.appbar;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static com.google.android.material.testutils.AppBarLayoutMatchers.isCollapsed;
import static com.google.android.material.testutils.SwipeUtils.swipeUp;
import static com.google.android.material.testutils.TestUtilsMatchers.hasZ;

import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.google.android.material.testapp.AppBarLayoutCollapsePinActivity;
import com.google.android.material.testapp.R;
import com.google.android.material.testutils.ActivityUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class AppBarWithCollapsingToolbarStateRestoreTest {
  @Rule
  public final ActivityTestRule<AppBarLayoutCollapsePinActivity> activityTestRule =
      new ActivityTestRule<>(AppBarLayoutCollapsePinActivity.class);

  @Test
  public void testRecreateAndRestore() throws Throwable {
    AppBarLayoutCollapsePinActivity activity = activityTestRule.getActivity();
    AppBarLayout appBar = activity.findViewById(R.id.app_bar);

    // Swipe up and collapse the AppBarLayout
    onView(withId(R.id.coordinator_layout))
        .perform(
            swipeUp(
                appBar.getLeft() + (appBar.getWidth() / 2),
                appBar.getBottom() + 20,
                appBar.getHeight()));
    onView(withId(R.id.app_bar)).check(matches(hasZ())).check(matches(isCollapsed()));

    ActivityUtils.recreateActivity(activityTestRule, activity);
    ActivityUtils.waitForExecution(activityTestRule);

    onView(withId(R.id.app_bar)).check(matches(hasZ())).check(matches(isCollapsed()));
  }
}

@@ -1729,16 +1729,33 @@
 dRestore
+WithSwipeInHeader
 () throw
@@ -1919,19 +1919,145 @@
     
-// Swipe up
+int%5B%5D appBarLocationOnScreenXY = new int%5B2%5D;%0A    appBar.getLocationOnScreen(appBarLocationOnScreenXY);%0A%0A    // Swipe up from AppBarLayout
  and
@@ -2192,18 +2192,100 @@
 pBar
-.getLeft()
+LocationOnScreenXY%5B0%5D + (appBar.getWidth() / 2),%0A                appBarLocationOnScreenXY%5B1%5D
  + (
@@ -2286,37 +2286,38 @@
 1%5D + (appBar.get
-Width
+Height
 () / 2),%0A       
@@ -2339,21 +2339,944 @@
 .get
-Bottom() + 20
+Height()));%0A    onView(withId(R.id.app_bar)).check(matches(hasZ())).check(matches(isCollapsed()));%0A%0A    ActivityUtils.recreateActivity(activityTestRule, activity);%0A    ActivityUtils.waitForExecution(activityTestRule);%0A%0A    onView(withId(R.id.app_bar)).check(matches(hasZ())).check(matches(isCollapsed()));%0A  %7D%0A%0A  @Test%0A  public void testRecreateAndRestoreWithSwipeInContent() throws Throwable %7B%0A    AppBarLayoutCollapsePinActivity activity = activityTestRule.getActivity();%0A    AppBarLayout appBar = activity.findViewById(R.id.app_bar);%0A%0A    int%5B%5D appBarLocationOnScreenXY = new int%5B2%5D;%0A    appBar.getLocationOnScreen(appBarLocationOnScreenXY);%0A%0A    // Swipe up from scrollView and collapse the AppBarLayout%0A    onView(withId(R.id.coordinator_layout))%0A        .perform(%0A            swipeUp(%0A                appBarLocationOnScreenXY%5B0%5D + (appBar.getWidth() / 2),%0A                appBarLocationOnScreenXY%5B1%5D + (3 * appBar.getHeight() / 2)
 ,%0A  

Improve AppBar collapsing tests.