FileDocCategorySizeDatePackage
ViewAsserts.javaAPI DocAndroid 1.5 API12472Wed May 06 22:42:02 BST 2009android.test

ViewAsserts

public class ViewAsserts extends Object
Some useful assertions about views.

Fields Summary
Constructors Summary
private ViewAsserts()

Methods Summary
public static voidassertBaselineAligned(android.view.View first, android.view.View second)
Assert that two views are aligned on their baseline, that is that their baselines are on the same y location.

param
first The first view
param
second The second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstTop = xy[1] + first.getBaseline();

        second.getLocationOnScreen(xy);
        int secondTop = xy[1] + second.getBaseline();

        assertEquals("views are not baseline aligned", firstTop, secondTop);
    
public static voidassertBottomAligned(android.view.View first, android.view.View second)
Assert that two views are bottom aligned, that is that their bottom edges are on the same y location.

param
first The first view
param
second The second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstBottom = xy[1] + first.getMeasuredHeight();

        second.getLocationOnScreen(xy);
        int secondBottom = xy[1] + second.getMeasuredHeight();

        assertEquals("views are not bottom aligned", firstBottom, secondBottom);
    
public static voidassertBottomAligned(android.view.View first, android.view.View second, int margin)
Assert that two views are bottom aligned, that is that their bottom edges are on the same y location, with respect to the specified margin.

param
first The first view
param
second The second view
param
margin The margin between the first view and the second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstBottom = xy[1] + first.getMeasuredHeight();

        second.getLocationOnScreen(xy);
        int secondBottom = xy[1] + second.getMeasuredHeight();

        assertEquals("views are not bottom aligned", Math.abs(firstBottom - secondBottom), margin);
    
public static voidassertGroupContains(android.view.ViewGroup parent, android.view.View child)
Assert that the specified group contains a specific child once and only once.

param
parent The group
param
child The child that should belong to group

        final int count = parent.getChildCount();
        assertTrue("Child count should be >= 0", count >= 0);

        boolean found = false;
        for (int i = 0; i < count; i++) {
            if (parent.getChildAt(i) == child) {
                if (!found) {
                    found = true;
                } else {
                    assertTrue("child " + child + " is duplicated in parent", false);
                }
            }
        }

        assertTrue("group does not contain " + child, found);
    
public static voidassertGroupIntegrity(android.view.ViewGroup parent)
Assert the specified group's integrity. The children count should be >= 0 and each child should be non-null.

param
parent The group whose integrity to check

        final int count = parent.getChildCount();
        assertTrue("child count should be >= 0", count >= 0);

        for (int i = 0; i < count; i++) {
            assertNotNull("group should not contain null children", parent.getChildAt(i));
            assertSame(parent, parent.getChildAt(i).getParent());
        }
    
public static voidassertGroupNotContains(android.view.ViewGroup parent, android.view.View child)
Assert that the specified group does not contain a specific child.

param
parent The group
param
child The child that should not belong to group

        final int count = parent.getChildCount();
        assertTrue("Child count should be >= 0", count >= 0);

        for (int i = 0; i < count; i++) {
            if (parent.getChildAt(i) == child) {
                assertTrue("child " + child + " is found in parent", false);
            }
        }
    
public static voidassertHasScreenCoordinates(android.view.View origin, android.view.View view, int x, int y)
Assert that a view has a particular x and y position on the visible screen.

param
origin The root view of the screen.
param
view The view.
param
x The expected x coordinate.
param
y The expected y coordinate.

        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        assertEquals("x coordinate", x, xy[0] - xyRoot[0]);
        assertEquals("y coordinate", y, xy[1] - xyRoot[1]);
    
public static voidassertHorizontalCenterAligned(android.view.View reference, android.view.View test)
Assert that the test view is horizontally center aligned with respect to the reference view.

param
reference The reference view
param
test The view that should be center aligned with the reference view

        int[] xy = new int[2];
        reference.getLocationOnScreen(xy);
        int referenceLeft = xy[0];

        test.getLocationOnScreen(xy);
        int testLeft = xy[0];

        int center = (reference.getMeasuredWidth() - test.getMeasuredWidth()) / 2;
        int delta = testLeft - referenceLeft;

        assertEquals("views are not horizontally center aligned", center, delta);
    
public static voidassertLeftAligned(android.view.View first, android.view.View second, int margin)
Assert that two views are left aligned, that is that their left edges are on the same x location, with respect to the specified margin.

param
first The first view
param
second The second view
param
margin The margin between the first view and the second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstLeft = xy[0];

        second.getLocationOnScreen(xy);
        int secondLeft = xy[0];

        assertEquals("views are not left aligned", Math.abs(firstLeft - secondLeft), margin);
    
public static voidassertLeftAligned(android.view.View first, android.view.View second)
Assert that two views are left aligned, that is that their left edges are on the same x location.

param
first The first view
param
second The second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstLeft = xy[0];

        second.getLocationOnScreen(xy);
        int secondLeft = xy[0];

        assertEquals("views are not left aligned", firstLeft, secondLeft);
    
public static voidassertOffScreenAbove(android.view.View origin, android.view.View view)
Assert that view is above the visible screen.

param
origin Te root view of the screen.
param
view The view

        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int y = xy[1] - xyRoot[1];

        assertTrue("view should have y location less than that of origin view",
                y < 0);
    
public static voidassertOffScreenBelow(android.view.View origin, android.view.View view)
Assert that view is below the visible screen.

param
origin The root view of the screen.
param
view The view

        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int y = xy[1] - xyRoot[1];

        assertTrue("view should have y location on screen greater than drawing "
                + "height of origen view (" + y + " is not greater than "
                + origin.getHeight() + ")",
                y > origin.getHeight());
    
public static voidassertOnScreen(android.view.View origin, android.view.View view)
Assert that view is on the screen.

param
origin The root view of the screen.
param
view The view.

        int[] xy = new int[2];
        view.getLocationOnScreen(xy);

        int[] xyRoot = new int[2];
        origin.getLocationOnScreen(xyRoot);

        int y = xy[1] - xyRoot[1];

        assertTrue("view should have positive y coordinate on screen",
                y  >= 0);

        assertTrue("view should have y location on screen less than drawing "
                + "height of root view",
                y <= view.getRootView().getHeight());
    
public static voidassertRightAligned(android.view.View first, android.view.View second)
Assert that two views are right aligned, that is that their right edges are on the same x location.

param
first The first view
param
second The second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstRight = xy[0] + first.getMeasuredWidth();

        second.getLocationOnScreen(xy);
        int secondRight = xy[0] + second.getMeasuredWidth();

        assertEquals("views are not right aligned", firstRight, secondRight);
    
public static voidassertRightAligned(android.view.View first, android.view.View second, int margin)
Assert that two views are right aligned, that is that their right edges are on the same x location, with respect to the specified margin.

param
first The first view
param
second The second view
param
margin The margin between the first view and the second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstRight = xy[0] + first.getMeasuredWidth();

        second.getLocationOnScreen(xy);
        int secondRight = xy[0] + second.getMeasuredWidth();

        assertEquals("views are not right aligned", Math.abs(firstRight - secondRight), margin);
    
public static voidassertTopAligned(android.view.View first, android.view.View second)
Assert that two views are top aligned, that is that their top edges are on the same y location.

param
first The first view
param
second The second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstTop = xy[1];

        second.getLocationOnScreen(xy);
        int secondTop = xy[1];

        assertEquals("views are not top aligned", firstTop, secondTop);
    
public static voidassertTopAligned(android.view.View first, android.view.View second, int margin)
Assert that two views are top aligned, that is that their top edges are on the same y location, with respect to the specified margin.

param
first The first view
param
second The second view
param
margin The margin between the first view and the second view

        int[] xy = new int[2];
        first.getLocationOnScreen(xy);
        int firstTop = xy[1];

        second.getLocationOnScreen(xy);
        int secondTop = xy[1];

        assertEquals("views are not top aligned", Math.abs(firstTop - secondTop), margin);
    
public static voidassertVerticalCenterAligned(android.view.View reference, android.view.View test)
Assert that the test view is vertically center aligned with respect to the reference view.

param
reference The reference view
param
test The view that should be center aligned with the reference view

        int[] xy = new int[2];
        reference.getLocationOnScreen(xy);
        int referenceTop = xy[1];

        test.getLocationOnScreen(xy);
        int testTop = xy[1];

        int center = (reference.getMeasuredHeight() - test.getMeasuredHeight()) / 2;
        int delta = testTop - referenceTop;

        assertEquals("views are not vertically center aligned", center, delta);