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 android.os.Build; 20import android.support.annotation.CallSuper; 21import android.support.annotation.IdRes; 22import android.support.annotation.LayoutRes; 23import android.support.annotation.StringRes; 24import android.support.design.test.R; 25import android.support.design.testutils.Shakespeare; 26import android.support.test.InstrumentationRegistry; 27import android.support.test.espresso.action.CoordinatesProvider; 28import android.support.test.espresso.action.GeneralSwipeAction; 29import android.support.test.espresso.action.Press; 30import android.support.test.espresso.action.Swipe; 31import android.support.v4.view.ViewCompat; 32import android.support.v7.app.AppCompatActivity; 33import android.support.v7.widget.Toolbar; 34import android.text.TextUtils; 35import android.view.View; 36import android.widget.TextView; 37 38import static android.support.design.testutils.TestUtilsActions.setText; 39import static android.support.design.testutils.TestUtilsActions.setTitle; 40import static android.support.test.espresso.Espresso.onView; 41import static android.support.test.espresso.matcher.ViewMatchers.withId; 42 43import static org.junit.Assert.assertEquals; 44 45public abstract class AppBarLayoutBaseTest extends BaseDynamicCoordinatorLayoutTest { 46 47 protected AppBarLayout mAppBar; 48 49 protected CollapsingToolbarLayout mCollapsingToolbar; 50 51 protected Toolbar mToolbar; 52 53 protected TextView mTextView; 54 55 protected float mDefaultElevationValue; 56 57 protected static void performVerticalSwipeUpGesture(@IdRes int containerId, final int swipeX, 58 final int swipeStartY, final int swipeAmountY) { 59 onView(withId(containerId)).perform(new GeneralSwipeAction( 60 Swipe.SLOW, 61 new CoordinatesProvider() { 62 @Override 63 public float[] calculateCoordinates(View view) { 64 return new float[] { swipeX, swipeStartY }; 65 } 66 }, 67 new CoordinatesProvider() { 68 @Override 69 public float[] calculateCoordinates(View view) { 70 return new float[] { swipeX, swipeStartY - swipeAmountY }; 71 } 72 }, Press.FINGER)); 73 } 74 75 protected static void performVerticalSwipeDownGesture(@IdRes int containerId, final int swipeX, 76 final int swipeStartY, final int swipeAmountY) { 77 onView(withId(containerId)).perform(new GeneralSwipeAction( 78 Swipe.SLOW, 79 new CoordinatesProvider() { 80 @Override 81 public float[] calculateCoordinates(View view) { 82 return new float[] { swipeX, swipeStartY }; 83 } 84 }, 85 new CoordinatesProvider() { 86 @Override 87 public float[] calculateCoordinates(View view) { 88 return new float[] { swipeX, swipeStartY + swipeAmountY }; 89 } 90 }, Press.FINGER)); 91 } 92 93 @CallSuper 94 protected void configureContent(final @LayoutRes int layoutResId, 95 final @StringRes int titleResId) { 96 onView(withId(R.id.coordinator_stub)).perform(inflateViewStub(layoutResId)); 97 98 mAppBar = (AppBarLayout) mCoordinatorLayout.findViewById(R.id.app_bar); 99 mCollapsingToolbar = 100 (CollapsingToolbarLayout) mAppBar.findViewById(R.id.collapsing_app_bar); 101 mToolbar = (Toolbar) mAppBar.findViewById(R.id.toolbar); 102 103 final AppCompatActivity activity = mActivityTestRule.getActivity(); 104 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 105 @Override 106 public void run() { 107 activity.setSupportActionBar(mToolbar); 108 } 109 }); 110 111 final CharSequence activityTitle = activity.getString(titleResId); 112 activity.setTitle(activityTitle); 113 if (mCollapsingToolbar != null) { 114 onView(withId(R.id.collapsing_app_bar)).perform(setTitle(activityTitle)); 115 } 116 117 TextView dialog = (TextView) mCoordinatorLayout.findViewById(R.id.textview_dialogue); 118 if (dialog != null) { 119 onView(withId(R.id.textview_dialogue)).perform( 120 setText(TextUtils.concat(Shakespeare.DIALOGUE))); 121 } 122 123 mDefaultElevationValue = mAppBar.getResources() 124 .getDimensionPixelSize(R.dimen.design_appbar_elevation); 125 } 126 127 protected void assertAppBarElevation(float expectedValue) { 128 if (Build.VERSION.SDK_INT >= 21) { 129 assertEquals(expectedValue, ViewCompat.getElevation(mAppBar), 0.05f); 130 } 131 } 132} 133