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

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);
}
主站蜘蛛池模板: 珠海市| 汾西县| 都昌县| 汕尾市| 阿克苏市| 吴川市| 滨海县| 松滋市| 昂仁县| 沂南县| 大名县| 新河县| 保德县| 高尔夫| 和硕县| 华池县| 孟州市| 长子县| 壤塘县| 杭州市| 建阳市| 阿拉善左旗| 寿宁县| 汽车| 冷水江市| 普兰县| 即墨市| 伊吾县| 沙湾县| 八宿县| 林口县| 平安县| 达州市| 泽库县| 鄂托克旗| 陇川县| 兰坪| 慈利县| 台州市| 合肥市| 玉溪市|