Commit 22c50755 authored by Maurya Talisetti's avatar Maurya Talisetti Committed by Maurya
Browse files

Issue#3664 Configure Icon loader for private sessions.

Icons being loaded in private sessions will not be cached on the disk.
parent 47e9ef8e
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -20,7 +20,8 @@ data class IconRequest(
    val url: String,
    val size: Size = Size.DEFAULT,
    val resources: List<Resource> = emptyList(),
    @ColorInt val color: Int? = null
    @ColorInt val color: Int? = null,
    val isPrivate: Boolean = false
) {

    /**
+2 −2
Original line number Diff line number Diff line
@@ -62,11 +62,11 @@ internal fun List<IconRequest.Resource>.toJSON(): JSONArray {
    }.toJSONArray()
}

internal fun JSONObject.toIconRequest(): IconRequest? {
internal fun JSONObject.toIconRequest(isPrivate: Boolean): IconRequest? {
    return try {
        val url = getString("url")

        IconRequest(url, resources = getJSONArray("icons").toIconResources())
        IconRequest(url, isPrivate = isPrivate, resources = getJSONArray("icons").toIconResources())
    } catch (e: JSONException) {
        Logger.warn("Could not parse message from icons extensions", e)
        null
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ internal class IconMessageHandler(

    override fun onMessage(message: Any, source: EngineSession?): Any {
        if (message is JSONObject) {
            message.toIconRequest()?.let { loadRequest(it) }
            message.toIconRequest(session.private)?.let { loadRequest(it) }
        } else {
            throw IllegalStateException("Received unexpected message: $message")
        }
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ class DiskIconProcessor(
        icon: Icon,
        desiredSize: DesiredSize
    ): Icon {
        if (resource != null && icon.shouldCacheOnDisk) {
        if (resource != null && icon.shouldCacheOnDisk && !request.isPrivate) {
            cache.put(context, request, resource, icon)
        }

+31 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import mozilla.components.support.test.mock
import org.junit.Test
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`

class DiskIconProcessorTest {
    @Test
@@ -83,4 +84,34 @@ class DiskIconProcessorTest {

        verify(cache, never()).put(any(), any(), any(), eq(icon))
    }

    @Test
    fun `Icon loaded in private mode is not saved in cache`() {
        /* Can be Source.INLINE as well. To ensure that the icon is eligible for caching on the disk. */
        val icon = Icon(mock(), source = Icon.Source.DOWNLOAD)
        val cache: DiskIconProcessor.ProcessorDiskCache = mock()

        val processor = DiskIconProcessor(cache)
        val request: IconRequest = mock()
        `when`(request.isPrivate).thenReturn(true)
        val resource: IconRequest.Resource = mock()
        processor.process(context = mock(), request = request, resource = resource, icon = icon, desiredSize = mock())

        verify(cache, never()).put(any(), any(), any(), eq(icon))
    }

    @Test
    fun `Icon loaded in non-private mode is saved in cache`() {
        /* Can be Source.INLINE as well. To ensure that the icon is eligible for caching on the disk. */
        val icon = Icon(mock(), source = Icon.Source.DOWNLOAD)
        val cache: DiskIconProcessor.ProcessorDiskCache = mock()

        val processor = DiskIconProcessor(cache)
        val request: IconRequest = mock()
        `when`(request.isPrivate).thenReturn(false)
        val resource: IconRequest.Resource = mock()
        processor.process(context = mock(), request = request, resource = resource, icon = icon, desiredSize = mock())

        verify(cache).put(any(), eq(request), eq(resource), eq(icon))
    }
}
Loading