- 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); }
- Mastering Concurrency Programming with Java 8
- LaTeX Cookbook
- PHP 7底層設計與源碼實現
- Java持續交付
- The DevOps 2.4 Toolkit
- MATLAB定量決策五大類問題
- 數據結構(C語言)
- SharePoint Development with the SharePoint Framework
- Building Serverless Applications with Python
- Gradle for Android
- 用案例學Java Web整合開發
- 監控的藝術:云原生時代的監控框架
- 嵌入式Linux C語言程序設計基礎教程
- Python預測分析與機器學習
- Selenium WebDriver Practical Guide