mercredi 1 novembre 2017

Android Integration test reading a file from memory

I've a MVP architecture and in one Presenter I want to test a method to retrieve a Bitmap from memory

the method is along those lines

public Bitmap getBitmap(Context context, Uri uri) {
...
try {
    final InputStream input = context.getContentResolver().openInputStream(uri);
    bitmap = BitmapFactory.decodeStream(input, null, options);
    input.close();
    }
...
}

Now I'm not really familiar with the new Android Test Suite, and I don't want to use (if possible) external libraries like Robolectric, so I am a bit at loss on how to proceed...

My thoughts were to go for an instrumentation test since I depend on the Android component Context

@Test
public void getScreenshot() {
    final Uri uriBitmap = Uri.parse("testFile.jpg");
    final Context context = InstrumentationRegistry.getTargetContext();

    final Bitmap retrieved = presenter.getBitmap(context, uriBitmap);
    assertEquals(retrieved, preInjectedTestFile.jpg)
}

but then I am not sure how can I "inject" a testFile.jpg in the file system for the duration of the test... Moreover I'm not sure if the Context I am using is the same one of the hosting app or a test one, which would be better not to mess around with the "official" app memory

Aucun commentaire:

Enregistrer un commentaire