DetailsFragmentTest.java revision 180dc4030628425a8081fe058bdc1d529c505ae8
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 */ 16package android.support.v17.leanback.app; 17 18import static org.junit.Assert.assertEquals; 19 20import android.content.Intent; 21import android.graphics.Rect; 22import android.graphics.drawable.ColorDrawable; 23import android.support.test.InstrumentationRegistry; 24import android.support.test.rule.ActivityTestRule; 25import android.support.v17.leanback.R; 26import android.support.v17.leanback.graphics.CompositeDrawable; 27import android.support.v17.leanback.graphics.FitWidthBitmapDrawable; 28import android.support.v17.leanback.testutils.PollingCheck; 29import android.test.suitebuilder.annotation.MediumTest; 30import android.view.View; 31 32import org.junit.Rule; 33import org.junit.Test; 34import org.junit.runner.RunWith; 35import org.junit.runners.JUnit4; 36 37/** 38 * Unit tests for {@link DetailsTestFragment}. 39 */ 40@RunWith(JUnit4.class) 41@MediumTest 42public class DetailsFragmentTest { 43 44 @Rule 45 public ActivityTestRule<DetailsFragmentTestActivity> activityTestRule = 46 new ActivityTestRule<>(DetailsFragmentTestActivity.class, false, false); 47 private DetailsFragmentTestActivity mActivity; 48 49 @Test 50 public void parallaxTest() throws InterruptedException { 51 final int mDefaultVerticalOffset = -300; 52 Intent intent = new Intent(); 53 intent.putExtra(DetailsTestFragment.VERTICAL_OFFSET, mDefaultVerticalOffset); 54 mActivity = activityTestRule.launchActivity(intent); 55 56 final DetailsTestFragment detailsFragment = mActivity.getDetailsFragment(); 57 DetailsBackgroundParallaxHelper parallaxHelper = detailsFragment.getParallaxHelper(); 58 final CompositeDrawable drawable = (CompositeDrawable) parallaxHelper.getDrawable(); 59 final FitWidthBitmapDrawable bitmapDrawable = (FitWidthBitmapDrawable) 60 (drawable.getChildAt(0).getDrawable()); 61 62 PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() { 63 @Override 64 public boolean canProceed() { 65 return mActivity.getDetailsFragment().getRowsFragment().getAdapter().size() > 1; 66 } 67 }); 68 69 int windowHeight = mActivity.getWindow().getDecorView().getHeight(); 70 int windowWidth = mActivity.getWindow().getDecorView().getWidth(); 71 72 Rect bounds = drawable.getChildAt(0).getDrawable().getBounds(); 73 assertEquals(windowWidth, bounds.width()); 74 assertEquals(windowHeight / 2, bounds.height()); 75 assertEquals(0, bitmapDrawable.getVerticalOffset()); 76 77 InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() { 78 @Override 79 public void run() { 80 detailsFragment.getRowsFragment().getVerticalGridView().scrollToPosition(1); 81 } 82 }); 83 84 PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() { 85 @Override 86 public boolean canProceed() { 87 return bitmapDrawable.getVerticalOffset() == mDefaultVerticalOffset; 88 } 89 }); 90 91 bounds = drawable.getChildAt(0).getDrawable().getBounds(); 92 assertEquals(0, bounds.height()); 93 assertEquals(windowWidth, bounds.width()); 94 95 View detailsFrame = mActivity.findViewById(R.id.details_frame); 96 int [] loc = new int[2]; 97 detailsFrame.getLocationOnScreen(loc); 98 ColorDrawable colorDrawable = (ColorDrawable) (drawable.getChildAt(1).getDrawable()); 99 100 assertEquals(windowWidth, colorDrawable.getBounds().width()); 101 assertEquals(windowHeight - (loc[1] + detailsFrame.getHeight()), 102 colorDrawable.getBounds().height()); 103 } 104} 105