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

The AndroidTestCase base class

This class can be used as a base class for general-purpose Android test cases.

Use it when you need access to Android resources, databases, or files in the filesystem. Context is stored as a field in this class, which is conveniently named mContext and can be used inside the tests if needed, or the getContext() method can be used too.

Tests based on this class can start more than one Activity using Context.startActivity().

There are various test cases in Android SDK that extend this base class:

  • ApplicationTestCase<T extends Application>
  • ProviderTestCase2<T extends ContentProvider>
  • ServiceTestCase<T extends Service>

When using the AndroidTestCase Java class, you inherit some base assertion methods that can be used; let's look at these in more detail.

The assertActivityRequiresPermission() method

The signature for this method is as follows:

public void assertActivityRequiresPermission(String packageName, String className, String permission)

Description

This assertion method checks whether the launching of a particular Activity is protected by a specific permission. It takes the following three parameters:

  • packageName: This is a string that indicates the package name of the activity to launch
  • className: This is a string that indicates the class of the activity to launch
  • permission: This is a string with the permission to check

The Activity is launched and then SecurityException is expected, which mentions that the required permission is missing in the error message. The actual instantiation of an activity is not handled by this assertion, and thus, an Instrumentation is not needed.

Example

This test checks the requirement of the android.Manifest.permission.WRITE_EXTERNAL_STORAGE permission, which is needed to write to external storage, in the MyContactsActivity Activity:

public void testActivityPermission() {
  String pkg = "com.blundell.tut";
  String activity =  PKG + ".MyContactsActivity";
  String permission = android.Manifest.permission.CALL_PHONE;
  assertActivityRequiresPermission(pkg, activity, permission);
}

Tip

Always use the constants that describe the permissions from android.Manifest.permission, not the strings, so if the implementation changes, your code will still be valid.

The assertReadingContentUriRequiresPermission method

The signature for this method is as follows:

public void assertReadingContentUriRequiresPermission(Uri uri, String permission)

Description

This assertion method checks whether reading from a specific URI requires the permission provided as a parameter.

It takes the following two parameters:

  • uri: This is the Uri that requires a permission to query
  • permission: This is a string that contains the permission to query

If a SecurityException class is generated, which contains the specified permission, this assertion is validated.

Example

This test tries to read contacts and verifies that the correct SecurityException is generated:

  public void testReadingContacts() {
    Uri URI = ContactsContract.AUTHORITY_URI;
    String PERMISSION = android.Manifest.permission.READ_CONTACTS;
    assertReadingContentUriRequiresPermission(URI, PERMISSION);
  }

The assertWritingContentUriRequiresPermission() method

The signature for this method is as follows:

public void assertWritingContentUriRequiresPermission (Uri uri, String permission)

Description

This assertion method checks whether inserting into a specific Uri requires the permission provided as a parameter.

It takes the following two parameters:

  • uri: This is the Uri that requires a permission to query
  • permission: This is a string that contains the permission to query

If a SecurityException class is generated, which contains the specified permission, this assertion is validated.

Example

This test tries to write to Contacts and verifies that the correct SecurityException is generated:

  public void testWritingContacts() {
  Uri uri = ContactsContract.AUTHORITY_URI;
   String permission = android.Manifest.permission.WRITE_CONTACTS;
  assertWritingContentUriRequiresPermission(uri, permission);
}
主站蜘蛛池模板: 新兴县| 会理县| 安陆市| 西藏| 昭觉县| 吐鲁番市| 山东省| 大新县| 绥宁县| 绥宁县| 房产| 巴中市| 玉山县| 达拉特旗| 育儿| 运城市| 那曲县| 南陵县| 永安市| 井研县| 繁峙县| 清涧县| 仪征市| 玉林市| 左贡县| 海原县| 泾川县| 临夏县| 遵化市| 九台市| 柘荣县| 交口县| 三门县| 海安县| 屯门区| 比如县| 屯昌县| 盐城市| 本溪| 大港区| 岚皋县|