androidunit-testingkotlinsharedpreferencesrobolectric

Getting "System.logW: No Compatibility callbacks set! Querying change 119147584" Warning when Unit-Testing Kotlin App


I wrote a Unit-test with RobolectricsTestRunner included.

The unit test passed, but with the following warning:

System.logW: No Compatibility callbacks set! Querying change 119147584

All I did in the unit-test, I saved a variable in Shared Preferences. This warning doesn't destroy anything. I was just wondering what it means. Unit-Test:

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [30])
class SharedPreferencesProviderTest {

    private lateinit var sharedPreferencesProvider: SharedPreferencesProviderInterface

    @Before
    fun setup() {
        // setup Shared preferences
        ...

        // Shared Preferences Access
        ...

        // clearing Shared Preferences
       ...
    }

    @Test
    fun firstStart_shouldBeTrueDefaultAndCheckChange() {
        // Default check
        var firstStart =
            sharedPreferencesProvider.getIsFirstStart()
        var expected = true
        assertTrue(firstStart == expected)

        // Check whether it handles changes
        sharedPreferencesProvider.setIsFirstStart(false)
        firstStart =
            sharedPreferencesProvider.getIsFirstStart()
        expected = false
        assertTrue(firstStart == expected)
    }}

I seems like it doesn't trigger OnSharedPreferenceChangeListener anymore, but I'm not sure. source


Solution

  • tl;dr: You can ignore that.

    The log comes from android.compat.Compatibility.BehaviorChangeDelegate. This is an interface class for behaviour change listener.

    The default action of the isChangeEnabled function call, as stated below, is to print the log and return true.

            @SystemApi(client = MODULE_LIBRARIES)
            default boolean isChangeEnabled(long changeId) {
                // Do not use String.format here (b/160912695)
                System.logW("No Compatibility callbacks set! Querying change " + changeId);
                return true;
            }
    

    What I assume is that during the Robolectric test runs, an implementation of this class that does not override the isChangeEnabled is some how attached and invoked. It's unclear where it's happening (I searched through the robolectric repo but I assume the repo isn't the culprit), and how we can fix this - or even if we need to fix this.

    If you are just curious if you've done something wrong, no it's not you. You can just ignore this log.