Testing a DocumentProvider with ProviderTestRule

456 Views Asked by At

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?

0

There are 0 best solutions below