[fenix] Add updated downloader and file manager
parent
af25894e05
commit
0b1667df4b
@ -0,0 +1,94 @@
|
|||||||
|
/* 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 org.mozilla.fenix.wallpapers
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import mozilla.components.concept.fetch.Client
|
||||||
|
import mozilla.components.concept.fetch.Request
|
||||||
|
import mozilla.components.concept.fetch.isSuccess
|
||||||
|
import mozilla.components.support.base.log.logger.Logger
|
||||||
|
import org.mozilla.fenix.BuildConfig
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can download wallpapers from a remote host.
|
||||||
|
*
|
||||||
|
* @param context Required for writing files to local storage.
|
||||||
|
* @param client Required for fetching files from network.
|
||||||
|
*/
|
||||||
|
class LegacyWallpaperDownloader(
|
||||||
|
private val context: Context,
|
||||||
|
private val client: Client,
|
||||||
|
) {
|
||||||
|
private val logger = Logger("WallpaperDownloader")
|
||||||
|
private val remoteHost = BuildConfig.WALLPAPER_URL
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads a wallpaper from the network. Will try to fetch 4 versions of each wallpaper:
|
||||||
|
* portrait/light - portrait/dark - landscape/light - landscape/dark. These are expected to be
|
||||||
|
* found at a remote path in the form:
|
||||||
|
* <WALLPAPER_URL>/<resolution>/<orientation>/<app theme>/<wallpaper theme>/<wallpaper name>.png
|
||||||
|
*/
|
||||||
|
suspend fun downloadWallpaper(wallpaper: Wallpaper) = withContext(Dispatchers.IO) {
|
||||||
|
if (remoteHost.isNullOrEmpty()) {
|
||||||
|
return@withContext
|
||||||
|
}
|
||||||
|
|
||||||
|
for (metadata in wallpaper.toMetadata(context)) {
|
||||||
|
val localFile = File(context.filesDir.absolutePath, metadata.localPath)
|
||||||
|
if (localFile.exists()) continue
|
||||||
|
val request = Request(
|
||||||
|
url = "$remoteHost/${metadata.remotePath}",
|
||||||
|
method = Request.Method.GET
|
||||||
|
)
|
||||||
|
Result.runCatching {
|
||||||
|
val response = client.fetch(request)
|
||||||
|
if (!response.isSuccess) {
|
||||||
|
logger.error("Download response failure code: ${response.status}")
|
||||||
|
return@withContext
|
||||||
|
}
|
||||||
|
File(localFile.path.substringBeforeLast("/")).mkdirs()
|
||||||
|
response.body.useStream { input ->
|
||||||
|
input.copyTo(localFile.outputStream())
|
||||||
|
}
|
||||||
|
}.onFailure {
|
||||||
|
Result.runCatching {
|
||||||
|
if (localFile.exists()) {
|
||||||
|
localFile.delete()
|
||||||
|
}
|
||||||
|
}.onFailure { e ->
|
||||||
|
logger.error("Failed to delete stale wallpaper bitmaps while downloading", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.error(it.message ?: "Download failed: no throwable message included.", it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private data class WallpaperMetadata(val remotePath: String, val localPath: String)
|
||||||
|
|
||||||
|
private fun Wallpaper.toMetadata(context: Context): List<WallpaperMetadata> =
|
||||||
|
listOf("landscape", "portrait").flatMap { orientation ->
|
||||||
|
listOf("light", "dark").map { theme ->
|
||||||
|
val localPath = "wallpapers/$orientation/$theme/$name.png"
|
||||||
|
val remotePath = "${context.resolutionSegment()}/" +
|
||||||
|
"$orientation/" +
|
||||||
|
"$theme/" +
|
||||||
|
"${collection.name}/" +
|
||||||
|
"$name.png"
|
||||||
|
WallpaperMetadata(remotePath, localPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("MagicNumber")
|
||||||
|
private fun Context.resolutionSegment(): String = when (resources.displayMetrics.densityDpi) {
|
||||||
|
// targeting hdpi and greater density resolutions https://developer.android.com/training/multiscreen/screendensities
|
||||||
|
in 0..240 -> "low"
|
||||||
|
in 240..320 -> "medium"
|
||||||
|
else -> "high"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/* 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 org.mozilla.fenix.wallpapers
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class LegacyWallpaperFileManager(
|
||||||
|
private val rootDirectory: File,
|
||||||
|
coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||||
|
) {
|
||||||
|
private val scope = CoroutineScope(coroutineDispatcher)
|
||||||
|
private val portraitDirectory = File(rootDirectory, "wallpapers/portrait")
|
||||||
|
private val landscapeDirectory = File(rootDirectory, "wallpapers/landscape")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lookup all the files for a wallpaper name. This lookup will fail if there are not
|
||||||
|
* files for each of the following orientation and theme combinations:
|
||||||
|
* light/portrait - light/landscape - dark/portrait - dark/landscape
|
||||||
|
*/
|
||||||
|
suspend fun lookupExpiredWallpaper(name: String): Wallpaper? = withContext(Dispatchers.IO) {
|
||||||
|
if (getAllLocalWallpaperPaths(name).all { File(rootDirectory, it).exists() }) {
|
||||||
|
Wallpaper(
|
||||||
|
name = name,
|
||||||
|
collection = Wallpaper.DefaultCollection,
|
||||||
|
textColor = null,
|
||||||
|
cardColor = null,
|
||||||
|
)
|
||||||
|
} else null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getAllLocalWallpaperPaths(name: String): List<String> =
|
||||||
|
listOf("landscape", "portrait").flatMap { orientation ->
|
||||||
|
listOf("light", "dark").map { theme ->
|
||||||
|
Wallpaper.legacyGetLocalPath(orientation, theme, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all wallpapers that are not the [currentWallpaper] or in [availableWallpapers].
|
||||||
|
*/
|
||||||
|
fun clean(currentWallpaper: Wallpaper, availableWallpapers: List<Wallpaper>) {
|
||||||
|
scope.launch {
|
||||||
|
val wallpapersToKeep = (listOf(currentWallpaper) + availableWallpapers).map { it.name }
|
||||||
|
cleanChildren(portraitDirectory, wallpapersToKeep)
|
||||||
|
cleanChildren(landscapeDirectory, wallpapersToKeep)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cleanChildren(dir: File, wallpapersToKeep: List<String>) {
|
||||||
|
for (file in dir.walkTopDown()) {
|
||||||
|
if (file.isDirectory || file.nameWithoutExtension in wallpapersToKeep) continue
|
||||||
|
file.delete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,111 @@
|
|||||||
|
/* 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 org.mozilla.fenix.wallpapers
|
||||||
|
|
||||||
|
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.rules.TemporaryFolder
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class LegacyWallpaperFileManagerTest {
|
||||||
|
@Rule
|
||||||
|
@JvmField
|
||||||
|
val tempFolder = TemporaryFolder()
|
||||||
|
private lateinit var portraitLightFolder: File
|
||||||
|
private lateinit var portraitDarkFolder: File
|
||||||
|
private lateinit var landscapeLightFolder: File
|
||||||
|
private lateinit var landscapeDarkFolder: File
|
||||||
|
|
||||||
|
private val dispatcher = UnconfinedTestDispatcher()
|
||||||
|
|
||||||
|
private lateinit var fileManager: LegacyWallpaperFileManager
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
portraitLightFolder = tempFolder.newFolder("wallpapers", "portrait", "light")
|
||||||
|
portraitDarkFolder = tempFolder.newFolder("wallpapers", "portrait", "dark")
|
||||||
|
landscapeLightFolder = tempFolder.newFolder("wallpapers", "landscape", "light")
|
||||||
|
landscapeDarkFolder = tempFolder.newFolder("wallpapers", "landscape", "dark")
|
||||||
|
fileManager = LegacyWallpaperFileManager(
|
||||||
|
rootDirectory = tempFolder.root,
|
||||||
|
coroutineDispatcher = dispatcher,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN files exist in all directories WHEN expired wallpaper looked up THEN expired wallpaper returned`() = runTest {
|
||||||
|
val wallpaperName = "name"
|
||||||
|
createAllFiles(wallpaperName)
|
||||||
|
|
||||||
|
val result = fileManager.lookupExpiredWallpaper(wallpaperName)
|
||||||
|
|
||||||
|
val expected = generateWallpaper(name = wallpaperName)
|
||||||
|
assertEquals(expected, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN any missing file in directories WHEN expired wallpaper looked up THEN null returned`() = runTest {
|
||||||
|
val wallpaperName = "name"
|
||||||
|
File(landscapeLightFolder, "$wallpaperName.png").createNewFile()
|
||||||
|
File(landscapeDarkFolder, "$wallpaperName.png").createNewFile()
|
||||||
|
|
||||||
|
val result = fileManager.lookupExpiredWallpaper(wallpaperName)
|
||||||
|
|
||||||
|
assertEquals(null, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN cleaned THEN current wallpaper and available wallpapers kept`() {
|
||||||
|
val currentName = "current"
|
||||||
|
val currentWallpaper = generateWallpaper(name = currentName)
|
||||||
|
val availableName = "available"
|
||||||
|
val available = generateWallpaper(name = availableName)
|
||||||
|
val unavailableName = "unavailable"
|
||||||
|
createAllFiles(currentName)
|
||||||
|
createAllFiles(availableName)
|
||||||
|
createAllFiles(unavailableName)
|
||||||
|
|
||||||
|
fileManager.clean(currentWallpaper, listOf(available))
|
||||||
|
|
||||||
|
assertTrue(getAllFiles(currentName).all { it.exists() })
|
||||||
|
assertTrue(getAllFiles(availableName).all { it.exists() })
|
||||||
|
assertTrue(getAllFiles(unavailableName).none { it.exists() })
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createAllFiles(name: String) {
|
||||||
|
for (file in getAllFiles(name)) {
|
||||||
|
file.createNewFile()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getAllFiles(name: String): List<File> {
|
||||||
|
return listOf(
|
||||||
|
File(portraitLightFolder, "$name.png"),
|
||||||
|
File(portraitDarkFolder, "$name.png"),
|
||||||
|
File(landscapeLightFolder, "$name.png"),
|
||||||
|
File(landscapeDarkFolder, "$name.png"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun generateWallpaper(name: String) = Wallpaper(
|
||||||
|
name = name,
|
||||||
|
textColor = null,
|
||||||
|
cardColor = null,
|
||||||
|
collection = Wallpaper.Collection(
|
||||||
|
name = Wallpaper.defaultName,
|
||||||
|
heading = null,
|
||||||
|
description = null,
|
||||||
|
availableLocales = null,
|
||||||
|
startDate = null,
|
||||||
|
endDate = null,
|
||||||
|
learnMoreUrl = null
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
@ -0,0 +1,137 @@
|
|||||||
|
package org.mozilla.fenix.wallpapers
|
||||||
|
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||||
|
import kotlinx.coroutines.test.TestDispatcher
|
||||||
|
import kotlinx.coroutines.test.TestScope
|
||||||
|
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import mozilla.components.concept.fetch.Client
|
||||||
|
import mozilla.components.concept.fetch.Request
|
||||||
|
import mozilla.components.concept.fetch.Response
|
||||||
|
import mozilla.components.concept.fetch.isSuccess
|
||||||
|
import org.junit.Assert.*
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.rules.TemporaryFolder
|
||||||
|
import org.mozilla.fenix.BuildConfig
|
||||||
|
import java.io.File
|
||||||
|
import java.lang.IllegalStateException
|
||||||
|
|
||||||
|
class WallpaperDownloaderTest {
|
||||||
|
@Rule
|
||||||
|
@JvmField
|
||||||
|
val tempFolder = TemporaryFolder()
|
||||||
|
|
||||||
|
private val remoteHost = BuildConfig.WALLPAPER_URL
|
||||||
|
|
||||||
|
private val wallpaperBytes = "file contents"
|
||||||
|
private val portraitResponseBodySuccess = Response.Body(wallpaperBytes.byteInputStream())
|
||||||
|
private val landscapeResponseBodySuccess = Response.Body(wallpaperBytes.byteInputStream())
|
||||||
|
private val mockPortraitResponse = mockk<Response>()
|
||||||
|
private val mockLandscapeResponse = mockk<Response>()
|
||||||
|
private val mockClient = mockk<Client>()
|
||||||
|
|
||||||
|
private val dispatcher = UnconfinedTestDispatcher()
|
||||||
|
|
||||||
|
private val wallpaperCollection = Wallpaper.Collection(
|
||||||
|
name = "collection",
|
||||||
|
heading = null,
|
||||||
|
description = null,
|
||||||
|
learnMoreUrl = null,
|
||||||
|
availableLocales = null,
|
||||||
|
startDate = null,
|
||||||
|
endDate = null
|
||||||
|
)
|
||||||
|
|
||||||
|
private lateinit var downloader: WallpaperDownloader
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
downloader = WallpaperDownloader(tempFolder.root, mockClient, dispatcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN that request is successful WHEN downloading THEN file is created in expected location`() = runTest {
|
||||||
|
val wallpaper = generateWallpaper()
|
||||||
|
val portraitRequest = Request(
|
||||||
|
url = "$remoteHost/${wallpaper.collection.name}/${wallpaper.name}/portrait.png",
|
||||||
|
method = Request.Method.GET
|
||||||
|
)
|
||||||
|
val landscapeRequest = Request(
|
||||||
|
url = "$remoteHost/${wallpaper.collection.name}/${wallpaper.name}/landscape.png",
|
||||||
|
method = Request.Method.GET
|
||||||
|
)
|
||||||
|
every { mockPortraitResponse.status } returns 200
|
||||||
|
every { mockLandscapeResponse.status } returns 200
|
||||||
|
every { mockPortraitResponse.body } returns portraitResponseBodySuccess
|
||||||
|
every { mockLandscapeResponse.body } returns landscapeResponseBodySuccess
|
||||||
|
every { mockClient.fetch(portraitRequest) } returns mockPortraitResponse
|
||||||
|
every { mockClient.fetch(landscapeRequest) } returns mockLandscapeResponse
|
||||||
|
|
||||||
|
downloader.downloadWallpaper(wallpaper)
|
||||||
|
|
||||||
|
val expectedPortraitFile = File(tempFolder.root, "wallpapers/${wallpaper.name}/portrait.png")
|
||||||
|
val expectedLandscapeFile = File(tempFolder.root, "wallpapers/${wallpaper.name}/landscape.png")
|
||||||
|
assertTrue(expectedPortraitFile.exists() && expectedPortraitFile.readText() == wallpaperBytes)
|
||||||
|
assertTrue(expectedLandscapeFile.exists() && expectedLandscapeFile.readText() == wallpaperBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN that request fails WHEN downloading THEN file is not created`() = runTest {
|
||||||
|
val wallpaper = generateWallpaper()
|
||||||
|
val portraitRequest = Request(
|
||||||
|
url = "$remoteHost/${wallpaper.collection.name}/${wallpaper.name}/portrait.png",
|
||||||
|
method = Request.Method.GET
|
||||||
|
)
|
||||||
|
val landscapeRequest = Request(
|
||||||
|
url = "$remoteHost/${wallpaper.collection.name}/${wallpaper.name}/landscape.png",
|
||||||
|
method = Request.Method.GET
|
||||||
|
)
|
||||||
|
every { mockPortraitResponse.status } returns 400
|
||||||
|
every { mockLandscapeResponse.status } returns 400
|
||||||
|
every { mockClient.fetch(portraitRequest) } returns mockPortraitResponse
|
||||||
|
every { mockClient.fetch(landscapeRequest) } returns mockLandscapeResponse
|
||||||
|
|
||||||
|
downloader.downloadWallpaper(wallpaper)
|
||||||
|
|
||||||
|
val expectedPortraitFile = File(tempFolder.root, "wallpapers/${wallpaper.name}/portrait.png")
|
||||||
|
val expectedLandscapeFile = File(tempFolder.root, "wallpapers/${wallpaper.name}/landscape.png")
|
||||||
|
assertFalse(expectedPortraitFile.exists())
|
||||||
|
assertFalse(expectedLandscapeFile.exists())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `GIVEN that copying the file fails WHEN downloading THEN file is not created`() = runTest {
|
||||||
|
val wallpaper = generateWallpaper()
|
||||||
|
val portraitRequest = Request(
|
||||||
|
url = "$remoteHost/${wallpaper.collection.name}/${wallpaper.name}/portrait.png",
|
||||||
|
method = Request.Method.GET
|
||||||
|
)
|
||||||
|
val landscapeRequest = Request(
|
||||||
|
url = "$remoteHost/${wallpaper.collection.name}/${wallpaper.name}/landscape.png",
|
||||||
|
method = Request.Method.GET
|
||||||
|
)
|
||||||
|
every { mockPortraitResponse.status } returns 200
|
||||||
|
every { mockLandscapeResponse.status } returns 200
|
||||||
|
every { mockPortraitResponse.body } throws IllegalStateException()
|
||||||
|
every { mockClient.fetch(portraitRequest) } throws IllegalStateException()
|
||||||
|
every { mockClient.fetch(landscapeRequest) } returns mockLandscapeResponse
|
||||||
|
|
||||||
|
downloader.downloadWallpaper(wallpaper)
|
||||||
|
|
||||||
|
val expectedPortraitFile = File(tempFolder.root, "wallpapers/${wallpaper.name}/portrait.png")
|
||||||
|
val expectedLandscapeFile = File(tempFolder.root, "wallpapers/${wallpaper.name}/landscape.png")
|
||||||
|
assertFalse(expectedPortraitFile.exists())
|
||||||
|
assertFalse(expectedLandscapeFile.exists())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun generateWallpaper(name: String = "name") = Wallpaper(
|
||||||
|
name = name,
|
||||||
|
collection = wallpaperCollection,
|
||||||
|
textColor = null,
|
||||||
|
cardColor = null
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue