0

I'm seeing behavior I am not expecting during development of android library (AAR), was wondering if someone can explain it. my library structure is :

src/
    androidTest/
        assets/
            my_assets/
        java/
            com/
                ...
    main/
        java/
            com/
                ...

my test is doing the following:

    final String assetsSubfolder = "my_assets";
    Context targetContext = InstrumentationRegistry.getTargetContext();
    String[] targetAssets = targetContext.getResources().getAssets().list("");
    Context testContext = InstrumentationRegistry.getContext();
    String[] testAssets = testContext.getResources().getAssets().list("");
    Assert.assertTrue("target should not have assets now", !Arrays.asList(targetAssets).contains(assetsSubfolder));
    Assert.assertTrue("test should have assets now", Arrays.asList(testAssets).contains(assetsSubfolder));

The test fails due to the target, which is the library production code, containing my testing assets folder. I thought that using getTargetContext I will see no assets folder, as there is no assets folder in my production code. Can someone explain this?

Eldar
  • 149
  • 2
  • 12
  • Create an app that uses the library, write a test case for the app, and see how your contexts behave. There, you will have a different context for the app under test from the context for the test code. With a library, I am not surprised that you are seeing the behavior that you are. – CommonsWare Oct 08 '17 at 11:31
  • I'm not expecting that the app test context will see the assets directory as its not under the in the app's testing assets directory, its under the library assets directory. why libraries behave differently in that manner? – Eldar Oct 08 '17 at 11:47
  • 1
    Well, at least in earlier versions of Android, it's because there is only one app: the one that contains your test code and the library. When testing apps, traditionally there were two apps: your app, and a separate APK with the test code. Two apps = two contexts. I have not investigated how all this is handled under the current instrumentation test system, as I haven't had the need to know. However, what you describe does not surprise me, because that's how it always used to work, particularly in pre-Android Studio days. – CommonsWare Oct 08 '17 at 12:00
  • sounds like you're right regarding one test app generated for library testing using the test context and the target context, though it still seems like a bug in the instrumentation testing system. Thanks! @CommonsWare – Eldar Oct 08 '17 at 12:13

0 Answers0