DetailsFragmentTest.java revision 34b46f02a89224481777e7de93d6e22e45e41e96
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.filters.MediumTest; 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.support.v17.leanback.widget.VerticalGridView; 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 Throwable { 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 final VerticalGridView verticalGridView = mActivity.getDetailsFragment() 70 .getRowsFragment().getVerticalGridView(); 71 final int windowHeight = verticalGridView.getHeight(); 72 final int windowWidth = verticalGridView.getWidth(); 73 // make sure background manager attached to window is same size as VerticalGridView 74 // i.e. no status bar. 75 assertEquals(windowHeight, mActivity.getWindow().getDecorView().getHeight()); 76 assertEquals(windowWidth, mActivity.getWindow().getDecorView().getWidth()); 77 78 final View detailsFrame = verticalGridView.findViewById(R.id.details_frame); 79 80 assertEquals(windowWidth, bitmapDrawable.getBounds().width()); 81 82 final Rect detailsFrameRect = new Rect(); 83 detailsFrameRect.set(0, 0, detailsFrame.getWidth(), detailsFrame.getHeight()); 84 verticalGridView.offsetDescendantRectToMyCoords(detailsFrame, detailsFrameRect); 85 86 assertEquals(Math.min(windowHeight, detailsFrameRect.top), 87 bitmapDrawable.getBounds().height()); 88 assertEquals(0, bitmapDrawable.getVerticalOffset()); 89 90 activityTestRule.runOnUiThread(new Runnable() { 91 @Override 92 public void run() { 93 verticalGridView.scrollToPosition(1); 94 } 95 }); 96 97 PollingCheck.waitFor(4000, new PollingCheck.PollingCheckCondition() { 98 @Override 99 public boolean canProceed() { 100 return bitmapDrawable.getVerticalOffset() == mDefaultVerticalOffset; 101 } 102 }); 103 104 detailsFrameRect.set(0, 0, detailsFrame.getWidth(), detailsFrame.getHeight()); 105 verticalGridView.offsetDescendantRectToMyCoords(detailsFrame, detailsFrameRect); 106 107 assertEquals(0, bitmapDrawable.getBounds().top); 108 assertEquals(Math.max(detailsFrameRect.top, 0), bitmapDrawable.getBounds().bottom); 109 assertEquals(windowWidth, bitmapDrawable.getBounds().width()); 110 111 ColorDrawable colorDrawable = (ColorDrawable) (drawable.getChildAt(1).getDrawable()); 112 assertEquals(windowWidth, colorDrawable.getBounds().width()); 113 assertEquals(detailsFrameRect.bottom, colorDrawable.getBounds().top); 114 assertEquals(windowHeight, colorDrawable.getBounds().bottom); 115 } 116} 117