- Learning Android Application Testing
- Paul Blundell Diego Torres Milano
- 525字
- 2021-07-23 19:58:55
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)
This assertion method checks whether the launching of a particular Activity is protected by a specific permission. It takes the following three parameters:
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.
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); }
The assertReadingContentUriRequiresPermission method
The signature for this method is as follows:
public void assertReadingContentUriRequiresPermission(Uri uri, String permission)
This assertion method checks whether reading from a specific URI requires the permission provided as a parameter.
It takes the following two parameters:
If a SecurityException
class is generated, which contains the specified permission, this assertion is validated.
The assertWritingContentUriRequiresPermission() method
The signature for this method is as follows:
public void assertWritingContentUriRequiresPermission (Uri uri, String permission)
This assertion method checks whether inserting into a specific Uri
requires the permission provided as a parameter.
It takes the following two parameters:
If a SecurityException
class is generated, which contains the specified permission, this assertion is validated.
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); }
- Mobile Application Development:JavaScript Frameworks
- Windows系統管理與服務配置
- Hands-On Data Structures and Algorithms with JavaScript
- JIRA 7 Administration Cookbook(Second Edition)
- Python Game Programming By Example
- 精通網絡視頻核心開發技術
- 低代碼平臺開發實踐:基于React
- C#程序設計教程(第3版)
- Visual Studio Code 權威指南
- 深入實踐Kotlin元編程
- 深度探索Go語言:對象模型與runtime的原理特性及應用
- Qt 4開發實踐
- Drupal Search Engine Optimization
- 寫給青少年的人工智能(Python版·微課視頻版)
- 零基礎PHP從入門到精通