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

Getting different file path when testing FileProvider #8773

Open
ArpitPatel009 opened this issue Jan 25, 2024 · 1 comment
Open

Getting different file path when testing FileProvider #8773

ArpitPatel009 opened this issue Jan 25, 2024 · 1 comment

Comments

@ArpitPatel009
Copy link

ArpitPatel009 commented Jan 25, 2024

Description

I have two test. when i run that individual it passes but it is failing when i run with test suite.

Test 1 (In Fragment)

This test opens the camera take picture using intent and passing result in onResultActivity and verifying that data is displayed in Image.
In intent we are using external storage to save data and retrieve URI using FileProvider.

@Test
    fun clickingTakeAPhoto_loadsImageInTheForm() {
        // arrange
        val fragment = rule.launch { Fragment() }

        // act
        rule.scenario.onActivity {
            fragment.startCamera()
        }

        val request = rule.scenario.withFragment<Fragment, ShadowActivity.IntentForResult> {
            shadowOf(fragment.requireActivity()).nextStartedActivityForResult
        }

        val uri = request.intent.getParcelableExtra<Uri>(MediaStore.EXTRA_OUTPUT)
        val source = javaClass.getResourceAsStream("test.jpg")

        rule.scenario.recreate()

        rule.scenario.onActivity { activity ->
            with(shadowOf(activity)) {
                shadowOf(activity.contentResolver).registerInputStream(uri, source)
                receiveResult(request.intent, Activity.RESULT_OK, Intent())
            }
        }

        // assert
        with (LinkPage()) {
            onView(this.uploadedImage).check(matches(DrawableMatchers.isDrawableDisplayed()))
        }
    }

Test 2 (In compose)

This test is verifying Intent(ACTION_VIEW) that on button click
it also uses FileProvider to get URI and storing data in Internal app storage (Cache).

 @Test
    fun openPDFViewer_Verify_ViewIntent() {

        val repo = Repository(mock(), context)

        Intents.init()
        val expected = hasAction(Intent.ACTION_VIEW)
        val file = repo.createUserFile(context, "test", javaClass.getResourceAsStream("/dummy.pdf")!!.readBytes())
        rule.setContent {
            OpenPDFViewer(file)
        }

        intended(allOf(hasAction(equalTo(Intent.ACTION_VIEW)), hasType("application/pdf")))
        intended(expected)
        Intents.release()
    }

Steps to Reproduce

It is failing on this line when run test suite FileProvider.getUriForFile(context, package, file)

Did bit of the investigation inside FileProvide class and it is failing when getting multiple root paths and not able to verify path of the Test 2 file and throw Failed to find configured root that contains.

  // Find the most-specific root path
            Map.Entry<String, File> mostSpecific = null;
            for (Map.Entry<String, File> root : mRoots.entrySet()) {
                final String rootPath = root.getValue().getPath();
                if (path.startsWith(rootPath) && (mostSpecific == null
                        || rootPath.length() > mostSpecific.getValue().getPath().length())) {
                    mostSpecific = root;
                }
            }

            if (mostSpecific == null) {
                throw new IllegalArgumentException(
                        "Failed to find configured root that contains " + path);
            }

Robolectric & Android Version

roboelectric = 4.9.2
compileSdkVersion = 34
minSdkVersion = 26
targetSdkVersion = 33

@MGaetan89
Copy link
Contributor

Just out of curiosity, did you try using the latest version of Robolectric (4.11.1) to see if the issue still exists @ArpitPatel009?

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

2 participants