Android: Provider Test Case 2 and getFilesDir()
Asked Answered
A

1

3

Part of my application involves saving a png file to my local files directory, and then shared via a content provider.

I write the file via getContext().openFileOutput

However, in my content provider, ParcelFileDescriptor will only open actual File objects. So trying to do this via a mocked out content provider using ProviderTestCase2, the following code doesn't work:

return ParcelFileDescriptor.open(new File(getContext().getFilesDir(), filename), ParcelFileDescriptor.MODE_READ_ONLY)

This is because context.getFilesDir() points to /dev/null in the mock context given to the provider via the ProviderTestCase2 code. The code above results in an exception because /dev/null doesn't count as a directory. Is this as intended?

Archbishopric answered 10/12, 2012 at 7:14 Comment(0)
S
0

This feels like a joke... The docs say:

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

But the IsolatedContext source is this:

@Override
public File getFilesDir() {
    return new File("/dev/null");
}

So we have better ignoring that method and try with

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

in out tests.

Saki answered 14/3, 2016 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.