1

I have a DocumentProvider backed by files on disk that I want to test with instrumented tests. I can not find a recommended way to do this unfortunately. In the docs there is ProviderTestCase2 which seems like it's going to be deprecated along with IsolatedContext.

The new way of doing it seems to be ProviderTestRule, but I can not get it to work with any DocumentProvider.

My Manifest:

<application>

    <provider
        android:authorities="${documentsAuthority}"
        android:name="com.example.asd.MyProvider"
        android:exported="true"
        android:enabled="true"
        android:grantUriPermissions="true"
        android:permission="android.permission.MANAGE_DOCUMENTS">
        <intent-filter>
            <action android:name="android.content.action.DOCUMENTS_PROVIDER"/>
        </intent-filter>
    </provider>
</application>

And my Instrumented test:

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

    @get:Rule
    public val permission = GrantPermissionRule.grant(Manifest.permission.MANAGE_DOCUMENTS)
    @get:Rule
    public val rule = ProviderTestRule.Builder(VaultDocumentProvider::class.java, BuildConfig.DOCUMENTS_AUTHORITY).build()

    // any test goes here

}

When I run the test, it throws an exception when initializing the ProviderTestRule:

java.lang.SecurityException: Provider must be exported
at android.provider.DocumentsProvider.attachInfo(DocumentsProvider.java:168)
at androidx.test.rule.provider.ProviderTestRule$Builder.createProvider(ProviderTestRule.java:529)
at androidx.test.rule.provider.ProviderTestRule$Builder.build(ProviderTestRule.java:482)
at com.example.asd.ExampleInstrumentedTest.<init>(ExampleInstrumentedTest.kt:22)

I've tried all permutations of exported, permission etc for the Provider, but to no avail. Is ProviderTestRule not the way to go here? Am I using it wrong? Or is there a bug somewhere?

Debrugger
  • 92
  • 1
  • 9
  • `123` probably is not your authority string, and you need to pass your authority to the `Builder` constructor, based on [the docs](https://developer.android.com/reference/androidx/test/rule/provider/ProviderTestRule.Builder#ProviderTestRule.Builder(java.lang.Class%3CT%3E,%20java.lang.String)). – CommonsWare May 02 '20 at 12:24
  • My bad, that was a copy paste mistake. Same behavior with the right authority unfortunately. – Debrugger May 02 '20 at 14:22
  • 1
    It is possible that the `android:permission` is somehow confusing it. Temporarily remove that and see if your test gets farther. You need that permission, but it might indicate whether this is some limitation/bug in `ProviderTestRule`. – CommonsWare May 02 '20 at 14:29
  • If I remove ```android:permission``` I have to also remove ```android:grantUriPermissions```, which requires ```exported``` to be removed as well. Then I'm back at ```Provider must be exported``` but this time from ```at android.app.ActivityThread.installProvider(ActivityThread.java:6288)```. – Debrugger May 02 '20 at 20:58
  • 1
    "If I remove android:permission I have to also remove android:grantUriPermissions" -- for what I was thinking of, I don't believe that is required. After all, `FileProvider` uses `android:grantUriPermissions` without `android:permission`. Again, this change will break your provider, but I was hoping things would survive long enough to see if `ProviderTestRule` cannot work with permission-protected providers or something. – CommonsWare May 02 '20 at 21:08
  • 1
    Otherwise, if you can create a demo project that reproduces the problem, file a bug report. There are a grand total of two `ProviderTestRule` issues, neither of which seem like they are what you are seeing. – CommonsWare May 02 '20 at 21:09

0 Answers0