Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 246eeaae authored by Stefan Andonian's avatar Stefan Andonian
Browse files

Add unit test and fix bug related to "Migrate IDP_GRID_NAME usage to LauncherPrefs" CL.

The issue was that kotlin initializes class variables before contructor
variables, but allows constructor variables to be referenced by
the class variables before they are initialized. This led to `isBackedUp`
always being false, and then the SharedPreference store always and only
checking if values were in DEVICE_PREFERENCES rather than the standard
backed up preferences.

The fix is to make the class variable sharedPrefFile lazily retrieved.
That way, isBackedUp will have been initialized and behave correctly.

Bug: 269569568
Test: Everything works on device and manual and unit tests
verified original bug is not present.

Change-Id: I8ab4a5752886ce8f4a2988295fa61c8a2c38ec7c
parent 4c9612be
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -274,7 +274,8 @@ abstract class Item {
    abstract val sharedPrefKey: String
    abstract val isBackedUp: Boolean
    abstract val type: Class<*>
    val sharedPrefFile: String = if (isBackedUp) SHARED_PREFERENCES_KEY else DEVICE_PREFERENCES_KEY
    val sharedPrefFile: String
        get() = if (isBackedUp) SHARED_PREFERENCES_KEY else DEVICE_PREFERENCES_KEY

    fun <T> to(value: T): Pair<Item, T> = Pair(this, value)
}
+15 −0
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@ private val TEST_STRING_ITEM = LauncherPrefs.nonRestorableItem("2", "( ͡❛ 
private val TEST_INT_ITEM = LauncherPrefs.nonRestorableItem("3", -1)
private val TEST_CONTEXTUAL_ITEM = ContextualItem("4", true, { true }, Boolean::class.java)

private const val TEST_DEFAULT_VALUE = "default"
private const val TEST_PREF_KEY = "test_pref_key"

@SmallTest
@RunWith(AndroidJUnit4::class)
class LauncherPrefsTest {
@@ -151,4 +154,16 @@ class LauncherPrefsTest {
    fun get_contextualItem_returnsCorrectDefault() {
        assertThat(launcherPrefs.get(TEST_CONTEXTUAL_ITEM)).isTrue()
    }

    @Test
    fun getItemSharedPrefFile_forNonRestorableItem_isCorrect() {
        val nonRestorableItem = LauncherPrefs.nonRestorableItem(TEST_PREF_KEY, TEST_DEFAULT_VALUE)
        assertThat(nonRestorableItem.sharedPrefFile).isEqualTo(LauncherFiles.DEVICE_PREFERENCES_KEY)
    }

    @Test
    fun getItemSharedPrefFile_forBackedUpItem_isCorrect() {
        val backedUpItem = LauncherPrefs.backedUpItem(TEST_PREF_KEY, TEST_DEFAULT_VALUE)
        assertThat(backedUpItem.sharedPrefFile).isEqualTo(LauncherFiles.SHARED_PREFERENCES_KEY)
    }
}