Skip to content

Commit

Permalink
Migrate Use custom sync server preference
Browse files Browse the repository at this point in the history
Use custom sync server preference was the master switch for both
collection and media custom sync servers. These preferences now have
individual switches; this migration applies the old master switch to
both individual switches, as long as the custom sync URLs are set.
  • Loading branch information
oakkitten committed Sep 10, 2022
1 parent 778a9a2 commit 4709427
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ object PreferenceUpgradeService {
yield(UpgradeGesturesToControls())
yield(UpgradeDayAndNightThemes())
yield(UpgradeCustomCollectionSyncUrl())
yield(UpgradeCustomSyncServerEnabled())
}

/** Returns a list of preference upgrade classes which have not been applied */
Expand Down Expand Up @@ -389,6 +390,26 @@ object PreferenceUpgradeService {
}
}
}

internal class UpgradeCustomSyncServerEnabled : PreferenceUpgrade(8) {
override fun upgrade(preferences: SharedPreferences) {
val customSyncServerEnabled = preferences.getBoolean(RemovedPreferences.PREFERENCE_ENABLE_CUSTOM_SYNC_SERVER, false)
val customCollectionSyncUrl = preferences.getString(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_URL, null)
val customMediaSyncUrl = preferences.getString(CustomSyncServer.PREFERENCE_CUSTOM_MEDIA_SYNC_URL, null)

preferences.edit {
remove(RemovedPreferences.PREFERENCE_ENABLE_CUSTOM_SYNC_SERVER)
putBoolean(
CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_SERVER_ENABLED,
customSyncServerEnabled && !customCollectionSyncUrl.isNullOrEmpty()
)
putBoolean(
CustomSyncServer.PREFERENCE_CUSTOM_MEDIA_SYNC_SERVER_ENABLED,
customSyncServerEnabled && !customMediaSyncUrl.isNullOrEmpty()
)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.ichi2.anki.servicelayer.PreferenceUpgradeService.PreferenceUpgrade
import com.ichi2.anki.servicelayer.RemovedPreferences
import com.ichi2.anki.web.CustomSyncServer
import com.ichi2.libanki.Consts
import com.ichi2.testutils.EmptyApplication
import com.ichi2.utils.HashUtil
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
Expand All @@ -37,10 +36,8 @@ import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config

@RunWith(AndroidJUnit4::class)
@Config(application = EmptyApplication::class) // no point in Application init if we don't use it
class PreferenceUpgradeServiceTest : RobolectricTest() {

private lateinit var mPrefs: SharedPreferences
Expand Down Expand Up @@ -184,4 +181,22 @@ class PreferenceUpgradeServiceTest : RobolectricTest() {
assertThat(mPrefs.contains(RemovedPreferences.PREFERENCE_CUSTOM_SYNC_BASE), equalTo(false))
assertThat(mPrefs.getString(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_URL, ""), equalTo("http://foo/sync/"))
}

@Test
fun `Removed Use custom sync server preference is applied to both sync URL preferences after upgrade`() {
mPrefs.edit {
putString(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_URL, "http://foo/sync/")
putBoolean(RemovedPreferences.PREFERENCE_ENABLE_CUSTOM_SYNC_SERVER, true)
}

assertThat(CustomSyncServer.getCollectionSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo(null))
assertThat(CustomSyncServer.getMediaSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo(null))

PreferenceUpgrade.UpgradeCustomSyncServerEnabled().performUpgrade(mPrefs)

assertThat(mPrefs.getBoolean(CustomSyncServer.PREFERENCE_CUSTOM_COLLECTION_SYNC_SERVER_ENABLED, false), equalTo(true))
assertThat(mPrefs.getBoolean(CustomSyncServer.PREFERENCE_CUSTOM_MEDIA_SYNC_SERVER_ENABLED, false), equalTo(false))
assertThat(CustomSyncServer.getCollectionSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo("http://foo/sync/"))
assertThat(CustomSyncServer.getMediaSyncUrlIfSetAndEnabledOrNull(mPrefs), equalTo(null))
}
}

0 comments on commit 4709427

Please sign in to comment.