FileDocCategorySizeDatePackage
SizeAdaptiveLayoutTest.javaAPI DocAndroid 5.1 API17127Thu Mar 12 22:22:12 GMT 2015com.android.internal.widget

SizeAdaptiveLayoutTest

public class SizeAdaptiveLayoutTest extends android.test.AndroidTestCase

Fields Summary
private android.view.LayoutInflater
mInflater
private int
mOneU
private int
mFourU
private com.android.internal.widget.SizeAdaptiveLayout
mSizeAdaptiveLayout
private android.view.View
mSmallView
private android.view.View
mMediumView
private android.view.View
mLargeView
Constructors Summary
Methods Summary
private voidinflate(int resource)

        mSizeAdaptiveLayout = (SizeAdaptiveLayout) mInflater.inflate(resource, null);
        mSizeAdaptiveLayout.onAttachedToWindow();

        mSmallView = mSizeAdaptiveLayout.findViewById(R.id.one_u);
        mMediumView = mSizeAdaptiveLayout.findViewById(R.id.two_u);
        mLargeView = mSizeAdaptiveLayout.findViewById(R.id.four_u);
    
private voidmeasureAndLayout(int height)

        // manually measure it, and lay it out
        int measureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
        mSizeAdaptiveLayout.measure(500, measureSpec);
        mSizeAdaptiveLayout.layout(0, 0, 500, mSizeAdaptiveLayout.getMeasuredHeight());
    
protected voidsetUp()

        super.setUp();

        // inflate the layout
        final Context context = getContext();
        mInflater = LayoutInflater.from(context);
        mOneU = 64;
        mFourU = 4 * mOneU;
    
public voidtestModestyPanelChangesColorWhite()

        inflate(R.layout.size_adaptive_color);
        View panel = mSizeAdaptiveLayout.getModestyPanel();
        assertTrue("ModestyPanel should have a ColorDrawable background",
                   panel.getBackground() instanceof ColorDrawable);
        ColorDrawable panelColor = (ColorDrawable) panel.getBackground();
        ColorDrawable salColor = (ColorDrawable) mSizeAdaptiveLayout.getBackground();
        assertEquals("ModestyPanel color should match the SizeAdaptiveLayout",
                     panelColor.getColor(), salColor.getColor());
    
public voidtestModestyPanelTracksStateListColor()

        inflate(R.layout.size_adaptive_color_statelist);
        View panel = mSizeAdaptiveLayout.getModestyPanel();
        assertEquals("ModestyPanel should have a ColorDrawable background" ,
                     panel.getBackground().getClass(), ColorDrawable.class);
        ColorDrawable panelColor = (ColorDrawable) panel.getBackground();
        assertEquals("ModestyPanel color should match the SizeAdaptiveLayout",
                     panelColor.getColor(), Color.RED);
    
public voidtestOpenFourUOnlyJustRight()

        inflate(R.layout.size_adaptive_large_only);
        assertNull("smallView should be NULL in the singleton layout", mSmallView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
    
public voidtestOpenFourUOnlyLarge()

        inflate(R.layout.size_adaptive_large_only);
        assertNull("smallView should be NULL in the singleton layout", mSmallView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int height = (int) lp.maxHeight + 10;

        measureAndLayout(height);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
    
public voidtestOpenFourUOnlySmall()

        inflate(R.layout.size_adaptive_large_only);
        assertNull("smallView should be NULL in the singleton layout", mSmallView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int height = (int) lp.minHeight - 10;

        measureAndLayout(height);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
    
public voidtestOpenIntoAGap()

        inflate(R.layout.size_adaptive_gappy);

        SizeAdaptiveLayout.LayoutParams smallParams =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        SizeAdaptiveLayout.LayoutParams largeParams =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        assertTrue("gappy layout should have a gap",
                smallParams.maxHeight + 10 < largeParams.minHeight);
        int height = (int) smallParams.maxHeight + 10;

        measureAndLayout(height);

        assertTrue("one and only one view should be visible",
                mLargeView.getVisibility() != mSmallView.getVisibility());
        // behavior is undefined in this case.
    
public voidtestOpenIntoAnOverlap()

        inflate(R.layout.size_adaptive_overlapping);

        SizeAdaptiveLayout.LayoutParams smallParams =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        SizeAdaptiveLayout.LayoutParams largeParams =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        assertEquals("overlapping layout should overlap",
                smallParams.minHeight,
                largeParams.minHeight);
        int height = (int) smallParams.maxHeight;

        measureAndLayout(height);

        assertTrue("one and only one view should be visible",
                mLargeView.getVisibility() != mSmallView.getVisibility());
        assertEquals("1U should get priority in an overlap because it is first",
                View.VISIBLE,
                mSmallView.getVisibility());
    
public voidtestOpenLarge()

        inflate(R.layout.size_adaptive);
        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int height = (int) lp.minHeight + 10;

        measureAndLayout(height);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
        assertEquals("1U should be gone",
                View.GONE,
                mSmallView.getVisibility());
    
public voidtestOpenLargeEvenWhenLargeIsActuallySmall()

        inflate(R.layout.size_adaptive_lies);
        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
        assertTrue("4U should also have been measured",
                   mLargeView.getMeasuredHeight() > 0);
    
public voidtestOpenOneUOnlyJustRight()

        inflate(R.layout.size_adaptive_singleton);
        assertNull("largeView should be NULL in the singleton layout", mLargeView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("1U should be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
    
public voidtestOpenOneUOnlyLarge()

        inflate(R.layout.size_adaptive_singleton);
        assertNull("largeView should be NULL in the singleton layout", mLargeView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        int height = (int) lp.maxHeight + 10;

        measureAndLayout(height);

        assertEquals("1U should be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
    
public voidtestOpenOneUOnlySmall()

        inflate(R.layout.size_adaptive_singleton);
        assertNull("largeView should be NULL in the singleton layout", mLargeView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        int height = (int) lp.minHeight - 10;

        measureAndLayout(height);

        assertEquals("1U should be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
    
public voidtestOpenSmall()

        inflate(R.layout.size_adaptive);
        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("1U should be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
        assertEquals("4U should be gone",
                View.GONE,
                mLargeView.getVisibility());
    
public voidtestOpenSmallEvenWhenLargeIsActuallySmall()

        inflate(R.layout.size_adaptive_lies);
        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("1U should be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
        assertTrue("1U should also have been measured",
                   mSmallView.getMeasuredHeight() > 0);
    
public voidtestOpenThreeWayViewLarge()

        inflate(R.layout.size_adaptive_three_way);
        assertNotNull("mMediumView should not be NULL in the three view layout", mMediumView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("1U should be gone",
                View.GONE,
                mSmallView.getVisibility());
        assertEquals("2U should be gone",
                View.GONE,
                mMediumView.getVisibility());
        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
    
public voidtestOpenThreeWayViewMedium()

        inflate(R.layout.size_adaptive_three_way);
        assertNotNull("mMediumView should not be NULL in the three view layout", mMediumView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mMediumView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("1U should be gone",
                View.GONE,
                mSmallView.getVisibility());
        assertEquals("2U should be visible",
                View.VISIBLE,
                mMediumView.getVisibility());
        assertEquals("4U should be gone",
                View.GONE,
                mLargeView.getVisibility());
    
public voidtestOpenThreeWayViewSmall()

        inflate(R.layout.size_adaptive_three_way);
        assertNotNull("mMediumView should not be NULL in the three view layout", mMediumView);

        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        int height = (int) lp.minHeight;

        measureAndLayout(height);

        assertEquals("1U should be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
        assertEquals("2U should be gone",
                View.GONE,
                mMediumView.getVisibility());
        assertEquals("4U should be gone",
                View.GONE,
                mLargeView.getVisibility());
    
public voidtestOpenTooBig()

        inflate(R.layout.size_adaptive);
        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        lp.maxHeight = 500;
        mLargeView.setLayoutParams(lp);
        int height = (int) (lp.minHeight + 10);

        measureAndLayout(height);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
        assertEquals("1U should be gone",
                View.GONE,
                mSmallView.getVisibility());
    
public voidtestOpenTooSmall()

        inflate(R.layout.size_adaptive);
        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        int height = (int) lp.minHeight - 10;

        measureAndLayout(height);

        assertEquals("1U should be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
        assertEquals("4U should be gone",
                View.GONE,
                mLargeView.getVisibility());
    
public voidtestOpenWrapContent()

        inflate(R.layout.size_adaptive_text);
        SizeAdaptiveLayout.LayoutParams lp =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int height = (int) lp.minHeight + 10;

        // manually measure it, and lay it out
        int measureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
        mSizeAdaptiveLayout.measure(500, measureSpec);
        assertTrue("should not be forced to 4U",
                mSizeAdaptiveLayout.getMeasuredHeight() < mFourU);
    
public voidtestPreconditions()
The name 'test preconditions' is a convention to signal that if this test doesn't pass, the test case was not set up properly and it might explain any and all failures in other tests. This is not guaranteed to run before other tests, as junit uses reflection to find the tests.

        assertNotNull(mInflater);

        inflate(R.layout.size_adaptive);
        assertNotNull(mSizeAdaptiveLayout);
        assertNotNull(mSmallView);
        assertNotNull(mLargeView);
    
public voidtestResizeWithAnimation()

        inflate(R.layout.size_adaptive);

        SizeAdaptiveLayout.LayoutParams smallParams =
          (SizeAdaptiveLayout.LayoutParams) mSmallView.getLayoutParams();
        SizeAdaptiveLayout.LayoutParams largeParams =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int startHeight = (int) largeParams.minHeight + 10;
        int endHeight = (int) smallParams.maxHeight;

        measureAndLayout(startHeight);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
        assertFalse("There should be no animation on initial rendering.",
                    mSizeAdaptiveLayout.getTransitionAnimation().isRunning());

        measureAndLayout(endHeight);

        assertEquals("1U should now be visible",
                View.VISIBLE,
                mSmallView.getVisibility());
        assertTrue("There should be an animation on scale between views.",
                   mSizeAdaptiveLayout.getTransitionAnimation().isRunning());
    
public voidtestResizeWithoutAnimation()

        inflate(R.layout.size_adaptive);

        SizeAdaptiveLayout.LayoutParams largeParams =
          (SizeAdaptiveLayout.LayoutParams) mLargeView.getLayoutParams();
        int startHeight = (int) largeParams.minHeight + 10;
        int endHeight = (int) largeParams.minHeight + 10;

        measureAndLayout(startHeight);

        assertEquals("4U should be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
        assertFalse("There should be no animation on initial rendering.",
                    mSizeAdaptiveLayout.getTransitionAnimation().isRunning());

        measureAndLayout(endHeight);

        assertEquals("4U should still be visible",
                View.VISIBLE,
                mLargeView.getVisibility());
        assertFalse("There should be no animation on scale within a view.",
                    mSizeAdaptiveLayout.getTransitionAnimation().isRunning());