- 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); }
- WebAssembly實戰
- NLTK基礎教程:用NLTK和Python庫構建機器學習應用
- Python爬蟲開發與項目實戰
- Learning ArcGIS Pro
- 零基礎學Java程序設計
- Learning Three.js:The JavaScript 3D Library for WebGL
- Node.js:來一打 C++ 擴展
- Learning Material Design
- Scala編程(第5版)
- 零基礎學HTML+CSS
- C語言程序設計
- Web編程基礎:HTML5、CSS3、JavaScript(第2版)
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Android智能手機APP界面設計實戰教程
- Implementing DevOps with Ansible 2