This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #3424 - Check if session is installable
- Loading branch information
Showing
6 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
components/concept/engine/src/test/resources/manifests/invalid_missing_name.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"start_url": "https://example.com" | ||
} |
4 changes: 4 additions & 0 deletions
4
components/concept/engine/src/test/resources/manifests/minimal_short_name.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"short_name": "Minimal with Short Name", | ||
"start_url": "/" | ||
} |
31 changes: 31 additions & 0 deletions
31
components/feature/pwa/src/main/java/mozilla/components/feature/pwa/ext/Session.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* 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/. */ | ||
|
||
package mozilla.components.feature.pwa.ext | ||
|
||
import mozilla.components.browser.session.Session | ||
import mozilla.components.concept.engine.manifest.WebAppManifest.Icon.Purpose | ||
import kotlin.math.min | ||
|
||
private const val MIN_INSTALLABLE_ICON_SIZE = 192 | ||
|
||
/** | ||
* Checks if the current session represents an installable web app. | ||
* | ||
* Websites are installable if: | ||
* - The site is served over HTTPS | ||
* - The site has a valid manifest with a name or short_name | ||
* - The icons array in the manifest contains an icon of at least 192x192 | ||
*/ | ||
fun Session.isInstallable(): Boolean { | ||
if (!securityInfo.secure) return false | ||
val manifest = webAppManifest ?: return false | ||
|
||
return manifest.icons.any { icon -> | ||
(Purpose.ANY in icon.purpose || Purpose.MASKABLE in icon.purpose) && | ||
icon.sizes.any { size -> | ||
min(size.width, size.height) >= MIN_INSTALLABLE_ICON_SIZE | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
components/feature/pwa/src/test/java/mozilla/components/feature/pwa/ext/SessionKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* 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/. */ | ||
|
||
package mozilla.components.feature.pwa.ext | ||
|
||
import mozilla.components.browser.session.Session | ||
import mozilla.components.concept.engine.manifest.Size | ||
import mozilla.components.concept.engine.manifest.WebAppManifest | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.whenever | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
|
||
class SessionKtTest { | ||
private val demoManifest = WebAppManifest(name = "Demo", startUrl = "https://mozilla.com") | ||
private val demoIcon = WebAppManifest.Icon(src = "https://mozilla.com/example.png") | ||
|
||
@Test | ||
fun `web app must be HTTPS to be installable`() { | ||
val httpSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = false)) | ||
} | ||
assertFalse(httpSession.isInstallable()) | ||
} | ||
|
||
@Test | ||
fun `web app must have manifest to be installable`() { | ||
val noManifestSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn(null) | ||
} | ||
assertFalse(noManifestSession.isInstallable()) | ||
} | ||
|
||
@Test | ||
fun `web app must have an icon to be installable`() { | ||
val noIconSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn(demoManifest) | ||
} | ||
assertFalse(noIconSession.isInstallable()) | ||
|
||
val noSizeIconSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn( | ||
demoManifest.copy(icons = listOf(demoIcon)) | ||
) | ||
} | ||
assertFalse(noSizeIconSession.isInstallable()) | ||
|
||
val onlyBadgeIconSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn( | ||
demoManifest.copy(icons = listOf( | ||
demoIcon.copy( | ||
sizes = listOf(Size(512, 512)), | ||
purpose = setOf(WebAppManifest.Icon.Purpose.BADGE) | ||
) | ||
)) | ||
) | ||
} | ||
assertFalse(onlyBadgeIconSession.isInstallable()) | ||
} | ||
|
||
@Test | ||
fun `web app must have 192x192 icons to be installable`() { | ||
val smallIconSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn( | ||
demoManifest.copy(icons = listOf( | ||
demoIcon.copy(sizes = listOf(Size(32, 32))) | ||
)) | ||
) | ||
} | ||
assertFalse(smallIconSession.isInstallable()) | ||
|
||
val weirdSizeSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn( | ||
demoManifest.copy(icons = listOf( | ||
demoIcon.copy(sizes = listOf(Size(50, 200))) | ||
)) | ||
) | ||
} | ||
assertFalse(weirdSizeSession.isInstallable()) | ||
|
||
val largeIconSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn( | ||
demoManifest.copy(icons = listOf( | ||
demoIcon.copy(sizes = listOf(Size(192, 192))) | ||
)) | ||
) | ||
} | ||
assertTrue(largeIconSession.isInstallable()) | ||
|
||
val multiSizeIconSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn( | ||
demoManifest.copy(icons = listOf( | ||
demoIcon.copy(sizes = listOf(Size(16, 16), Size(512, 512))) | ||
)) | ||
) | ||
} | ||
assertTrue(multiSizeIconSession.isInstallable()) | ||
|
||
val multiIconSession = mock<Session>().also { | ||
whenever(it.securityInfo).thenReturn(Session.SecurityInfo(secure = true)) | ||
whenever(it.webAppManifest).thenReturn( | ||
demoManifest.copy(icons = listOf( | ||
demoIcon.copy(sizes = listOf(Size(191, 193))), | ||
demoIcon.copy(sizes = listOf(Size(512, 512))), | ||
demoIcon.copy( | ||
sizes = listOf(Size(192, 192)), | ||
purpose = setOf(WebAppManifest.Icon.Purpose.BADGE) | ||
) | ||
)) | ||
) | ||
} | ||
assertTrue(multiIconSession.isInstallable()) | ||
} | ||
} |