1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.design.widget; 18 19import static android.support.design.testutils.CollapsingToolbarLayoutActions.setContentScrimColor; 20import static android.support.design.testutils.SwipeUtils.swipeDown; 21import static android.support.design.testutils.SwipeUtils.swipeUp; 22import static android.support.design.testutils.TestUtilsActions.setText; 23import static android.support.design.testutils.TestUtilsActions.setTitle; 24import static android.support.test.espresso.Espresso.onView; 25import static android.support.test.espresso.assertion.ViewAssertions.matches; 26import static android.support.test.espresso.matcher.ViewMatchers.withId; 27 28import static org.junit.Assert.assertEquals; 29 30import android.graphics.Color; 31import android.os.Build; 32import android.os.SystemClock; 33import android.support.annotation.CallSuper; 34import android.support.annotation.IdRes; 35import android.support.annotation.IntRange; 36import android.support.annotation.LayoutRes; 37import android.support.annotation.StringRes; 38import android.support.design.test.R; 39import android.support.design.testutils.Shakespeare; 40import android.support.v4.view.ViewCompat; 41import android.support.v7.app.AppCompatActivity; 42import android.support.v7.widget.Toolbar; 43import android.text.TextUtils; 44import android.widget.TextView; 45 46import org.hamcrest.Description; 47import org.hamcrest.Matcher; 48import org.hamcrest.TypeSafeMatcher; 49 50public abstract class AppBarLayoutBaseTest extends BaseDynamicCoordinatorLayoutTest { 51 52 protected AppBarLayout mAppBar; 53 54 protected CollapsingToolbarLayout mCollapsingToolbar; 55 56 protected Toolbar mToolbar; 57 58 protected TextView mTextView; 59 60 protected float mDefaultElevationValue; 61 62 protected static void performVerticalSwipeUpGesture(@IdRes int containerId, final int swipeX, 63 final int swipeStartY, final int swipeAmountY) { 64 onView(withId(containerId)).perform(swipeUp(swipeX, swipeStartY, swipeAmountY)); 65 } 66 67 protected static void performVerticalSwipeDownGesture(@IdRes int containerId, final int swipeX, 68 final int swipeStartY, final int swipeAmountY) { 69 onView(withId(containerId)).perform(swipeDown(swipeX, swipeStartY, swipeAmountY)); 70 } 71 72 @CallSuper 73 protected void configureContent(@LayoutRes final int layoutResId, 74 @StringRes final int titleResId) throws Throwable { 75 onView(withId(R.id.coordinator_stub)).perform(inflateViewStub(layoutResId)); 76 77 mAppBar = (AppBarLayout) mCoordinatorLayout.findViewById(R.id.app_bar); 78 mCollapsingToolbar = 79 (CollapsingToolbarLayout) mAppBar.findViewById(R.id.collapsing_app_bar); 80 mToolbar = (Toolbar) mAppBar.findViewById(R.id.toolbar); 81 82 final AppCompatActivity activity = mActivityTestRule.getActivity(); 83 mActivityTestRule.runOnUiThread(new Runnable() { 84 @Override 85 public void run() { 86 activity.setSupportActionBar(mToolbar); 87 } 88 }); 89 90 final CharSequence activityTitle = activity.getString(titleResId); 91 mActivityTestRule.runOnUiThread(new Runnable() { 92 @Override 93 public void run() { 94 activity.setTitle(activityTitle); 95 } 96 }); 97 98 if (mCollapsingToolbar != null) { 99 onView(withId(R.id.collapsing_app_bar)) 100 .perform(setTitle(activityTitle)) 101 .perform(setContentScrimColor(Color.MAGENTA)); 102 } 103 104 TextView dialog = (TextView) mCoordinatorLayout.findViewById(R.id.textview_dialogue); 105 if (dialog != null) { 106 onView(withId(R.id.textview_dialogue)) 107 .perform(setText(TextUtils.concat(Shakespeare.DIALOGUE))); 108 } 109 110 mDefaultElevationValue = mAppBar.getResources() 111 .getDimension(R.dimen.design_appbar_elevation); 112 } 113 114 protected void assertAppBarElevation(float expectedValue) { 115 if (Build.VERSION.SDK_INT >= 21) { 116 assertEquals(expectedValue, ViewCompat.getElevation(mAppBar), 0.05f); 117 } 118 } 119 120 protected void assertScrimAlpha(@IntRange(from = 0, to = 255) int alpha) { 121 SystemClock.sleep(300); 122 onView(withId(R.id.collapsing_app_bar)) 123 .check(matches(withScrimAlpha(alpha))); 124 } 125 126 static Matcher withScrimAlpha(final int alpha) { 127 return new TypeSafeMatcher<CollapsingToolbarLayout>() { 128 @Override 129 public void describeTo(Description description) { 130 description.appendText( 131 "CollapsingToolbarLayout has content scrim with alpha: " + alpha); 132 } 133 134 @Override 135 protected boolean matchesSafely(CollapsingToolbarLayout view) { 136 return alpha == view.getScrimAlpha(); 137 } 138 }; 139 } 140} 141