diff --git a/app/src/main/java/org/mozilla/fenix/whatsnew/WhatsNew.kt b/app/src/main/java/org/mozilla/fenix/whatsnew/WhatsNew.kt index b13b625e1..aad54640b 100644 --- a/app/src/main/java/org/mozilla/fenix/whatsnew/WhatsNew.kt +++ b/app/src/main/java/org/mozilla/fenix/whatsnew/WhatsNew.kt @@ -26,12 +26,11 @@ class WhatsNew private constructor(private val storage: WhatsNewStorage) { val lastKnownAppVersion = storage.getVersion() // Update the version and date if *just* updated - lastKnownAppVersion?.let { - if (currentVersion.majorVersionNumber > it.majorVersionNumber) { - storage.setVersion(currentVersion) - storage.setDateOfUpdate(System.currentTimeMillis()) - return true - } + if (lastKnownAppVersion == null || + currentVersion.majorVersionNumber > lastKnownAppVersion.majorVersionNumber) { + storage.setVersion(currentVersion) + storage.setDateOfUpdate(System.currentTimeMillis()) + return true } return (!storage.getWhatsNewHasBeenCleared() && storage.getDaysSinceUpdate() < DAYS_PER_UPDATE) diff --git a/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewStorageTest.kt b/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewStorageTest.kt index 5a768eec4..1643d4ad1 100644 --- a/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewStorageTest.kt +++ b/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewStorageTest.kt @@ -1,5 +1,9 @@ package org.mozilla.fenix.whatsnew +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + import androidx.test.ext.junit.runners.AndroidJUnit4 import kotlinx.coroutines.ObsoleteCoroutinesApi import mozilla.components.support.test.robolectric.testContext @@ -59,6 +63,6 @@ class WhatsNewStorageTest { } } -private fun Settings.clear() { +fun Settings.clear() { preferences.clearAndCommit() } diff --git a/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewTest.kt b/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewTest.kt new file mode 100644 index 000000000..74da9d526 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewTest.kt @@ -0,0 +1,94 @@ +package org.mozilla.fenix.whatsnew + +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import kotlinx.coroutines.ObsoleteCoroutinesApi +import mozilla.components.support.test.robolectric.testContext +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.TestApplication +import org.mozilla.fenix.ext.settings +import org.mozilla.fenix.utils.Settings +import org.robolectric.annotation.Config + +@ObsoleteCoroutinesApi +@RunWith(AndroidJUnit4::class) +@Config(application = TestApplication::class) +class WhatsNewTest { + private lateinit var storage: SharedPreferenceWhatsNewStorage + private lateinit var settings: Settings + + @Before + fun setup() { + storage = SharedPreferenceWhatsNewStorage(testContext) + settings = testContext.settings().apply(Settings::clear) + WhatsNew.wasUpdatedRecently = null + } + + @Test + fun `should highlight after fresh install`() { + assertEquals(true, WhatsNew.shouldHighlightWhatsNew(testContext)) + } + + @Test + fun `should highlight if new version has been installed less than 3 days`() { + storage.setVersion(WhatsNewVersion("1.0")) + storage.setDateOfUpdate(System.currentTimeMillis() - WhatsNewStorageTest.DAY_IN_MILLIS) + + assertEquals(true, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("2.0"), storage)) + } + + @Test + fun `should not highlight if new version has been installed more than 3 days`() { + storage.setVersion(WhatsNewVersion("1.0")) + assertEquals(true, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("2.0"), storage)) + + // Simulate an app "restart" by resetting `wasUpdatedRecently` + WhatsNew.wasUpdatedRecently = null + // Simulate time passing + storage.setDateOfUpdate(System.currentTimeMillis() - WhatsNewStorageTest.DAY_IN_MILLIS * 3) + + assertEquals(false, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("2.0"), storage)) + } + + @Test + fun `should not highlight if new version is only a minor update`() { + storage.setVersion(WhatsNewVersion("1.0")) + assertEquals(false, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("1.0.1"), storage)) + + WhatsNew.wasUpdatedRecently = null + + storage.setVersion(WhatsNewVersion("1.0")) + assertEquals(false, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("1.1"), storage)) + } + + @Test + fun `should highlight if new version is a major update`() { + storage.setVersion(WhatsNewVersion("1.0")) + assertEquals(true, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("2.0"), storage)) + + WhatsNew.wasUpdatedRecently = null + + storage.setVersion(WhatsNewVersion("1.2")) + assertEquals(true, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("2.0"), storage)) + + WhatsNew.wasUpdatedRecently = null + + storage.setVersion(WhatsNewVersion("3.2")) + assertEquals(true, WhatsNew.shouldHighlightWhatsNew(WhatsNewVersion("4.2"), storage)) + } + + @Test + fun `should not highlight after user viewed what's new`() { + assertEquals(true, WhatsNew.shouldHighlightWhatsNew(testContext)) + + WhatsNew.userViewedWhatsNew(testContext) + + assertEquals(false, WhatsNew.shouldHighlightWhatsNew(testContext)) + } +} diff --git a/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewVersionTest.kt b/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewVersionTest.kt index 0acedf273..9fc5151fe 100644 --- a/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewVersionTest.kt +++ b/app/src/test/java/org/mozilla/fenix/whatsnew/WhatsNewVersionTest.kt @@ -7,7 +7,6 @@ package org.mozilla.fenix.whatsnew import androidx.test.ext.junit.runners.AndroidJUnit4 import kotlinx.coroutines.ObsoleteCoroutinesApi import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotEquals import org.junit.Test import org.junit.runner.RunWith import org.mozilla.fenix.TestApplication @@ -22,7 +21,7 @@ class WhatsNewVersionTest { val versionOne = WhatsNewVersion("1.2.0") assertEquals(1, versionOne.majorVersionNumber) - val versionTwo = WhatsNewVersion("2.4.0") - assertNotEquals(1, versionTwo.majorVersionNumber) + val versionTwo = WhatsNewVersion("2.4.1") + assertEquals(2, versionTwo.majorVersionNumber) } }