Issue
I'm working on a testcase for my application that should verify the correctness of an file import action. To automatically test this, my plan is to copy a file from my test assets directory into the downloads folder of the device under test and perform the import action using an Espresso test case.
Does somebody have experience with this? I'm running into the issue that my test case has no permission to write anything to the device.
So far I have created a dedicated manifest.xml file for my test application containing the required permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Furthermore, I'm performing this action before my test starts to grant the needed permission to the test case:
adb shell pm grant com.my_app_pacakge.test android.permission.WRITE_EXTERNAL_STORAGE
Unfortunately, when I create the file in the downloads directory the following exception is thrown at the moment I try to write contents to the backup file:
Caused by: java.io.FileNotFoundException: /storage/emulated/0/Download/small_backup: open failed: EACCES (Permission denied)
The relevant code is the following:
public void putBackupFile(String name ){
File backupFile = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS ).getPath(), name );
try {
InputStream is = InstrumentationRegistry.getInstrumentation().getContext().getAssets().open( name );
FileOutputStream fileOutputStream = new FileOutputStream(backupFile);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.close();
is.close();
} catch (IOException e1) {
throw new RuntimeException(e1);
}
}
The exception is triggered at: FileOutputStream fileOutputStream = new FileOutputStream(backupFile);
Solution
Answering to the original question: If you grant android.permission.WRITE_EXTERNAL_STORAGE from adb, you have to grant android.permission.READ_EXTERNAL_STORAGE as well:
adb shell pm grant com.my_app_pacakge.test android.permission.READ_EXTERNAL_STORAGE
It seems to be that if one ask for WRITE permission in an app, the READ permission is asked/granted automatically. If one does it from adb, the READ permission have to be granted additionally.
Answered By - alex.dorokhov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.