3

I'm new to Android. I wrote my activity in Kotlin and my tests in Java. I'm trying to run an espresso test on an android app and I have the following error message when trying to load a file from within my main activity. Can anyone help?

Here is the code I'm using for the function that loads the data from the file inside my Activity class:

fun loadImage(uri: Uri){
    //get the input stream of the image selected using its uri
    val inputStream = contentResolver.openInputStream(uri)
}

To run the test, I manually added a file to the emulator to get its uri. This next code is of the test:

@RunWith(AndroidJUnit4.class)
public class testGallery {
    @Rule
    public IntentsTestRule<MainActivity> mActivity = new IntentsTestRule<> (MainActivity.class);

  @Rule
  public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);

    @Test
        public void test_uploadImage() {
            Uri uri = Uri.parse("content://com.android.providers.downloads.documents/document/32");
            Intent resultData = new Intent();
            resultData.setAction(Intent.ACTION_OPEN_DOCUMENT);
            resultData.putExtra("Uri", uri);
            Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);

            this.mActivity.getActivity().grantUriPermission("com.intern_firstapp_juan", Uri.parse("content://com.android.providers.downloads.documents/document/32"), Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

            mActivity.getActivity().uploadImage(uri);
        }
}

Finally, this is the stack trace that appears when running the test.

java.lang.SecurityException: Permission Denial: opening provider com.android.providers.downloads.DownloadStorageProvider from ProcessRecord{54c088b 24178:com.example.hdhanushkodi.myapplication/u0a88} (pid=24178, uid=10088) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4191)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5963)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2576)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1819)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1433)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1286)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1006)
at android.client.mobile.com.intern_firstapp_juan.MainActivity.uploadImage(MainActivity.kt:105)
at android.client.mobile.com.intern_firstapp_juan.testGallery.test_uploadImage(testGallery.java:82)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:129)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2136)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:12088)
at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:12485)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:357)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3268)
at android.os.Binder.execTransact(Binder.java:731)

Here is my AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.client.mobile.com.intern_firstapp_juan">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ShowImageActivity">
        </activity>

    </application>

</manifest>
  • Unit test are not able to access the Android SDK because they run on the local JVM. You need to mock that object of substitute it's functionality somehow – Chisko Jun 08 '18 at 19:56
  • 2
    This is an instrumented test that runs on an emulator with AndroidJUnitRunner. It should have access to the android sdk. – Juan Ortiz Couder Jun 08 '18 at 21:40
  • Hi! If you are asking how to grant permissions for an instrumneted unit test, there are some answers [here](https://stackoverflow.com/questions/33929937/android-marshmallow-test-permissions-with-espresso) – David Rawson Jun 09 '18 at 23:43
  • Did you find a solution? – MaxGyver Feb 22 '19 at 19:49
  • No @MaxGyver . However, I was able to run other testing that ended up working. – Juan Ortiz Couder Feb 25 '19 at 14:22
  • The one linked by @DavidRawson is not working and is not appropriate solution for this so a -1 for that. This is related to Android SAF, and I have part of the app that I cannot test via instrumentation testing because I can't get pass this issue. I think, eversince Google introduce SAF, they forgot to mention how we will test this. Imagine testing a file explorer app or a media player app and you have some component to test say, a scanner. – Neon Warge Mar 17 '21 at 17:03

0 Answers0