官术网_书友最值得收藏!

View assertions

The assertions introduced earlier handle a variety of types as parameters, but they are only intended to test simple conditions or simple objects.

For example, we have asertEquals(short expected, short actual) to test short values, assertEquals(int expected, int actual) to test integer values, assertEquals(Object expected, Object expected) to test any Object instance, and so on.

Usually, while testing user interfaces in Android, you will face the problem of more sophisticated methods, which are mainly related with Views. In this respect, Android provides a class with plenty of assertions in android.test.ViewAsserts (see http://developer.android.com/reference/android/test/ViewAsserts.html for more details), which test relationships between Views and their absolute and relative positions on the screen.

These methods are also overloaded to provide different conditions. Among the assertions, we can find the following:

  • assertBaselineAligned: This asserts that two Views are aligned on their baseline; that is, their baselines are on the same y location.
  • assertBottomAligned: This asserts that two views are bottom aligned; that is, their bottom edges are on the same y location.
  • assertGroupContains: This asserts that the specified group contains a specific child once and only once.
  • assertGroupIntegrity: This asserts the specified group's integrity. The child count should be >= 0 and each child should be non-null.
  • assertGroupNotContains: This asserts that the specified group does not contain a specific child.
  • assertHasScreenCoordinates: This asserts that a View has a particular x and y position on the visible screen.
  • assertHorizontalCenterAligned: This asserts that the test View is horizontally center aligned with respect to the reference view.
  • assertLeftAligned: This asserts that two Views are left aligned; that is, their left edges are on the same x location. An optional margin can also be provided.
  • assertOffScreenAbove: This asserts that the specified view is above the visible screen.
  • assertOffScreenBelow: This asserts that the specified view is below the visible screen.
  • assertOnScreen: This asserts that a View is on the screen.
  • assertRightAligned: This asserts that two Views are right-aligned; that is, their right edges are on the same x location. An optional margin can also be specified.
  • assertTopAligned: This asserts that two Views are top aligned; that is, their top edges are on the same y location. An optional margin can also be specified.
  • assertVerticalCenterAligned: This asserts that the test View is vertically center-aligned with respect to the reference View.

The following example shows how you can use ViewAssertions to test the user interface layout:

  public void testUserInterfaceLayout() {
    int margin = 0;
    View origin = mActivity.getWindow().getDecorView();
    assertOnScreen(origin, editText);
    assertOnScreen(origin, button);
    assertRightAligned(editText, button, margin);
  }

The assertOnScreen method uses an origin to start looking for the requested Views. In this case, we are using the top-level window decor View. If, for some reason, you don't need to go that high in the hierarchy, or if this approach is not suitable for your test, you may use another root View in the hierarchy, for example View.getRootView(), which, in our concrete example, would be editText.getRootView().

主站蜘蛛池模板: 普兰店市| 陆河县| 手游| 龙里县| 闻喜县| 洛宁县| 碌曲县| 太原市| 曲周县| 星子县| 洪雅县| 吉木萨尔县| 克东县| 凤阳县| 徐州市| 巴彦淖尔市| 宿迁市| 雅安市| 淮滨县| 定南县| 阿荣旗| 体育| 调兵山市| 北辰区| 紫云| 佛教| 腾冲县| 蒲江县| 射阳县| 皮山县| 荆门市| 安新县| 巨鹿县| 桑植县| 南宫市| 天台县| 聂荣县| 阜宁县| 荔波县| 四子王旗| 大渡口区|