Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception with using TestStorage #1141

Closed
yogurtearl opened this issue Sep 30, 2021 · 10 comments
Closed

Exception with using TestStorage #1141

yogurtearl opened this issue Sep 30, 2021 · 10 comments

Comments

@yogurtearl
Copy link
Contributor

yogurtearl commented Sep 30, 2021

I have test-services-1.4.1-alpha02.apk installed for all the experiments below,
and from adb shell pm dump androidx.test.services looks like the TestOutputFilesContentProvider is installed with authority androidx.test.services.storage.outputfiles.

But I get the errors listed below.
(I copied the impl of writeToTestStorage into my test to work around #1139)

I think you need to add this to the manifest of the TestStorage artifact, to support targetSdk 30, 31

    <queries>
        <package android:name="androidx.test.services"/>
    </queries>

On API 26 emulator with I get No parent directory (Same on API 31 device with targetSdk 28) ( or if I add the <queries> block with targetSdk 30, 31)

java.io.FileNotFoundException: Could not access file: content://androidx.test.services.storage.outputfiles/foo.png 
Exception: No parent directory for 'content://androidx.test.services.storage.outputfiles/foo.png'
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
at android.content.ContentProviderProxy.openFile(ContentProviderNative.java:598)
at android.content.ContentProviderClient.openFile(ContentProviderClient.java:369)
at android.content.ContentProviderClient.openFile(ContentProviderClient.java:346)
at androidx.test.services.storage.internal.TestStorageUtil.getOutputStream(TestStorageUtil.java:90)
at androidx.test.services.storage.TestStorage.openOutputFile(TestStorage.java:221)
at androidx.test.services.storage.TestStorage.openOutputFile(TestStorage.java:201)
at com.example.androidcomposescratch.ExampleInstrumentedTestKt.writeToTestStorage(ExampleInstrumentedTest.kt:81)
at com.example.androidcomposescratch.ExampleInstrumentedTestKt.writeToTestStorage(ExampleInstrumentedTest.kt:69)
at com.example.androidcomposescratch.ExampleInstrumentedTest.testCompose(ExampleInstrumentedTest.kt:59)

On API 31 device with targetSdk 31, I get this different error: (this is solved by adding the <queries> block)

androidx.test.services.storage.TestStorageException: No content provider registered for: content://androidx.test.services.storage.outputfiles/foo.png. Are all test services apks installed?
at androidx.test.services.storage.internal.TestStorageUtil.makeContentProviderClient(TestStorageUtil.java:108)
at androidx.test.services.storage.internal.TestStorageUtil.getOutputStream(TestStorageUtil.java:88)
at androidx.test.services.storage.TestStorage.openOutputFile(TestStorage.java:221)
at androidx.test.services.storage.TestStorage.openOutputFile(TestStorage.java:201)
at com.example.androidcomposescratch.ExampleInstrumentedTestKt.writeToTestStorage(ExampleInstrumentedTest.kt:81)
at com.example.androidcomposescratch.ExampleInstrumentedTestKt.writeToTestStorage(ExampleInstrumentedTest.kt:69)
at com.example.androidcomposescratch.ExampleInstrumentedTest.testCompose(ExampleInstrumentedTest.kt:59)
@brettchabot
Copy link
Collaborator

Are you installing the apks with --force-queryable ?

@yogurtearl
Copy link
Contributor Author

ah! No, was installing via shell scripts that didn't include that, but just found your comment about that.

but even with adb install -g -r --force-queryable fix the No content provider registered issue on API 31/targetSdk 31, but I still hit the No parent directory error on all emulators.

@yogurtearl
Copy link
Contributor Author

Compose has a debugImplementation 'androidx.compose.ui:ui-test-manifest:...' artifact that allows them to add things to the manifest of the apk under test via debugImplementation instead of androidTestInplementation

Could be useful if androidx.test had a similar artifact, would help with this (could include the <queries>) and could help with #832

@brettchabot
Copy link
Collaborator

My understanding is should not be necessary if the services apk is installed with --force-queryable. 'No parent directory' might be a separate problem? @adazh

@adazh
Copy link
Collaborator

adazh commented Sep 30, 2021

The "No parent directory" error looks like the test storage failed to create the folder on the device, likely due to permission issues.

Can you try granting the "MANAGE_EXTERNAL_STORAGE" permission to the androidx.test services via:
adb shell appops set androidx.test.services MANAGE_EXTERNAL_STORAGE allow?

@yuuki3655
Copy link
Collaborator

AGP runs adb shell appops set androidx.test.services MANAGE_EXTERNAL_STORAGE allow command when useTestStorageService is enabled. Could you enable it via instrumentation runner arguments like the example below?

android {
    defaultConfig {
        testInstrumentationRunnerArguments useTestStorageService: 'true'
    }
}
dependencies {
    androidTestUtil 'androidx.test.services:test-services:1.4.1-alpha02'
}

@brettchabot
Copy link
Collaborator

Ada, Yuki and I chatted offline. I was able to repro this problem, but resolved it via the config Yuki gives above and using Studio bumblebee canary 13 + com.android.tools.build:gradle:7.1.0-alpha13

@yogurtearl
Copy link
Contributor Author

yogurtearl commented Sep 30, 2021

Thanks for all the info. :)

This worked on API 30, 31 emulators:

adb shell appops set androidx.test.services MANAGE_EXTERNAL_STORAGE allow

but on API 26, I got this:

$ adb shell appops set androidx.test.services MANAGE_EXTERNAL_STORAGE allow
Error: Unknown operation string: MANAGE_EXTERNAL_STORAGE

but useTestStorageService: 'true' fixes No parent directory on API < 30.

My understanding is should not be necessary if the services apk is installed with --force-queryable.

--force-queryable gives an error on API < 30, which makes it harder to script things for people not using AGP or Studio. :/
Adding the query in the manifest would make it work across all APIs.

Closing this, but opened this to add docs: https://issuetracker.google.com/issues/201652738

@ZSmallX
Copy link

ZSmallX commented Jun 13, 2023

I'm facing the same problem on API 29 and is really confused.
I think docs is outdated and optimization needed for using AndroidX Test Service - Test Storage. No idea why this issue was closed without any docs optimization? https://issuetracker.google.com/issues/201652738

And I solved it by this command adb install -r -g test-services-1.4.2.apk to grant all the permissions on API 29.

@PaulKlauser
Copy link

@ZSmallX it should also work if you have the androidx.test.services:test-services:1.4.2 dependency declared, no need for the adb command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants