Skip to content

Commit

Permalink
For mozilla-mobile/fenix#15279: ManifestUpdateFeature takes LazyCompo…
Browse files Browse the repository at this point in the history
…nent WebAppShortcutManager.

This is commit mozilla-mobile#2 of making WebAppShortcutManager a Lazy Component.
  • Loading branch information
mcomella committed Nov 6, 2020
1 parent 74149f6 commit fe3839a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import mozilla.components.concept.engine.manifest.WebAppManifest
import mozilla.components.feature.pwa.ManifestStorage
import mozilla.components.feature.pwa.WebAppShortcutManager
import mozilla.components.support.base.feature.LifecycleAwareFeature
import mozilla.components.support.base.utils.LazyComponent

/**
* Feature used to update the existing web app manifest and web app shortcut.
Expand All @@ -26,10 +27,11 @@ import mozilla.components.support.base.feature.LifecycleAwareFeature
* @param sessionId ID of the web app session to observe.
* @param initialManifest Loaded manifest for the current web app.
*/
@Suppress("LongParameterList")
class ManifestUpdateFeature(
private val applicationContext: Context,
private val sessionManager: SessionManager,
private val shortcutManager: WebAppShortcutManager,
private val shortcutManager: LazyComponent<WebAppShortcutManager>,
private val storage: ManifestStorage,
private val sessionId: String,
private var initialManifest: WebAppManifest
Expand Down Expand Up @@ -59,7 +61,7 @@ class ManifestUpdateFeature(
@VisibleForTesting
internal suspend fun updateStoredManifest(manifest: WebAppManifest) {
storage.updateManifest(manifest)
shortcutManager.updateShortcuts(applicationContext, listOf(manifest))
shortcutManager.get().updateShortcuts(applicationContext, listOf(manifest))
initialManifest = manifest
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.engine.manifest.WebAppManifest
import mozilla.components.feature.pwa.ManifestStorage
import mozilla.components.feature.pwa.WebAppShortcutManager
import mozilla.components.support.base.utils.LazyComponent
import mozilla.components.support.test.any
import mozilla.components.support.test.robolectric.testContext
import org.junit.Before
Expand All @@ -32,6 +33,7 @@ class ManifestUpdateFeatureTest {
@Mock private lateinit var sessionManager: SessionManager
@Mock private lateinit var session: Session
@Mock private lateinit var shortcutManager: WebAppShortcutManager
private lateinit var lazyShortcutManager: LazyComponent<WebAppShortcutManager>
@Mock private lateinit var storage: ManifestStorage
private val sessionId = "external-app-session-id"
private val baseManifest = WebAppManifest(
Expand All @@ -43,12 +45,14 @@ class ManifestUpdateFeatureTest {
@Before
fun setup() {
initMocks(this)
lazyShortcutManager = LazyComponent { shortcutManager }

`when`(sessionManager.findSessionById(sessionId)).thenReturn(session)
}

@Test
fun `start and stop controls session observer`() {
val feature = ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest)
val feature = ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest)

feature.start()
verify(session).register(feature)
Expand All @@ -60,7 +64,7 @@ class ManifestUpdateFeatureTest {
@Test
fun `start and stop handle null session`() {
`when`(sessionManager.findSessionById(sessionId)).thenReturn(null)
val feature = ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest)
val feature = ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest)

feature.start()
verify(session, never()).register(feature)
Expand All @@ -71,7 +75,7 @@ class ManifestUpdateFeatureTest {

@Test
fun `updateStoredManifest is called when the manifest changes`() = runBlockingTest {
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest))
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest))
doReturn(Unit).`when`(feature).updateStoredManifest(any())
feature.start()

Expand All @@ -83,7 +87,7 @@ class ManifestUpdateFeatureTest {

@Test
fun `updateStoredManifest is not called when the manifest is the same`() = runBlockingTest {
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest))
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest))
feature.start()

feature.onWebAppManifestChanged(session, baseManifest)
Expand All @@ -93,7 +97,7 @@ class ManifestUpdateFeatureTest {

@Test
fun `updateStoredManifest is not called when the manifest is null`() = runBlockingTest {
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest))
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest))
feature.start()

feature.onWebAppManifestChanged(session, null)
Expand All @@ -103,7 +107,7 @@ class ManifestUpdateFeatureTest {

@Test
fun `updateStoredManifest is not called when the manifest has a different start URL`() = runBlockingTest {
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest))
val feature = spy(ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest))
feature.start()

val manifest = WebAppManifest(name = "Mozilla", startUrl = "https://netscape.com")
Expand All @@ -114,7 +118,7 @@ class ManifestUpdateFeatureTest {

@Test
fun `updateStoredManifest updates storage and shortcut`() = runBlockingTest {
val feature = ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest)
val feature = ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest)

val manifest = baseManifest.copy(shortName = "Moz")
feature.updateStoredManifest(manifest)
Expand All @@ -125,7 +129,7 @@ class ManifestUpdateFeatureTest {

@Test
fun `start updates last web app usage`() = runBlockingTest {
val feature = ManifestUpdateFeature(testContext, sessionManager, shortcutManager, storage, sessionId, baseManifest)
val feature = ManifestUpdateFeature(testContext, sessionManager, lazyShortcutManager, storage, sessionId, baseManifest)

feature.start()

Expand Down
1 change: 1 addition & 0 deletions docs/tmp-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ have to stop on every commit for a merge conflict. This file will be removed at

* **feature-pwa**
* ⚠️ **This is a breaking change**: `WebAppUseCases`'s initializer argument, `WebAppShortcutManager`, is wrapped in a `LazyComponent`. The same applies to the classes defined inside `WebAppUseCases`.
* ⚠️ **This is a breaking change**: `ManifestUpdateFeature`'s initializer argument, `WebAppShortcutManager`, is wrapped in a `LazyComponent`.

0 comments on commit fe3839a

Please sign in to comment.