Commit 49f0532b authored by Sebastian Kaspari's avatar Sebastian Kaspari
Browse files

Issue #4284: Add DownloadState to browser-state.

parent b98de403
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import mozilla.components.browser.state.state.SecurityInfoState
import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.browser.state.state.TrackingProtectionState
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.lib.state.Action

@@ -166,6 +167,16 @@ sealed class ContentAction : BrowserAction() {
     * Updates the thumbnail of the [ContentState] with the given [sessionId].
     */
    data class UpdateThumbnailAction(val sessionId: String, val thumbnail: Bitmap) : ContentAction()

    /**
     * Updates the [DownloadState] of the [ContentState] with the given [sessionId].
     */
    data class UpdateDownloadAction(val sessionId: String, val download: DownloadState) : ContentAction()

    /**
     * Removes the [DownloadState] of the [ContentState] with the given [sessionId].
     */
    data class ConsumeDownloadAction(val sessionId: String, val download: DownloadState) : ContentAction()
}

/**
+10 −0
Original line number Diff line number Diff line
@@ -45,6 +45,16 @@ internal object ContentStateReducer {
            is ContentAction.UpdateThumbnailAction -> updateContentState(state, action.sessionId) {
                it.copy(thumbnail = action.thumbnail)
            }
            is ContentAction.UpdateDownloadAction -> updateContentState(state, action.sessionId) {
                it.copy(download = action.download)
            }
            is ContentAction.ConsumeDownloadAction -> updateContentState(state, action.sessionId) {
                if (action.download == it.download) {
                    it.copy(download = null)
                } else {
                    it
                }
            }
        }
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
package mozilla.components.browser.state.state

import android.graphics.Bitmap
import mozilla.components.browser.state.state.content.DownloadState

/**
 * Value type that represents the state of the content within a [SessionState].
@@ -21,6 +22,7 @@ import android.graphics.Bitmap
 * @property thumbnail the last generated [Bitmap] of this session's content, to
 * be used as a preview in e.g. a tab switcher.
 * @property icon the icon of the page currently loaded by this session.
 * @property download Last unhandled download request.
 */
data class ContentState(
    val url: String,
@@ -31,5 +33,6 @@ data class ContentState(
    val searchTerms: String = "",
    val securityInfo: SecurityInfoState = SecurityInfoState(),
    val thumbnail: Bitmap? = null,
    val icon: Bitmap? = null
    val icon: Bitmap? = null,
    val download: DownloadState? = null
)
+28 −0
Original line number Diff line number Diff line
/* 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.browser.state.state.content

import android.os.Environment

/**
 * Value type that represents a download request.
 *
 * @property url The full url to the content that should be downloaded.
 * @property fileName A canonical filename for this download.
 * @property contentType Content type (MIME type) to indicate the media type of the download.
 * @property contentLength The file size reported by the server.
 * @property userAgent The user agent to be used for the download.
 * @property destinationDirectory The matching destination directory for this type of download.
 * @property referrerUrl The site that linked to this download.
 */
data class DownloadState(
    val url: String,
    val fileName: String? = null,
    val contentType: String? = null,
    val contentLength: Long? = null,
    val userAgent: String? = null,
    val destinationDirectory: String = Environment.DIRECTORY_DOWNLOADS,
    val referrerUrl: String? = null
)
+40 −0
Original line number Diff line number Diff line
@@ -9,10 +9,12 @@ import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.SecurityInfoState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.browser.state.state.createCustomTab
import mozilla.components.browser.state.state.createTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.mock
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
@@ -251,4 +253,42 @@ class ContentActionTest {
        assertNotEquals("I am a custom tab", tab.content.title)
        assertNotEquals("I am a custom tab", otherTab.content.title)
    }

    @Test
    fun `UpdateDownloadAction updates download`() {
        assertNull(tab.content.download)

        val download1: DownloadState = mock()

        store.dispatch(
            ContentAction.UpdateDownloadAction(tab.id, download1)
        ).joinBlocking()

        assertEquals(download1, tab.content.download)

        val download2: DownloadState = mock()

        store.dispatch(
            ContentAction.UpdateDownloadAction(tab.id, download2)
        ).joinBlocking()

        assertEquals(download2, tab.content.download)
    }

    @Test
    fun `ConsumeDownloadAction removes download`() {
        val download: DownloadState = mock()

        store.dispatch(
            ContentAction.UpdateDownloadAction(tab.id, download)
        ).joinBlocking()

        assertEquals(download, tab.content.download)

        store.dispatch(
            ContentAction.ConsumeDownloadAction(tab.id, download)
        ).joinBlocking()

        assertNull(tab.content.download)
    }
}
 No newline at end of file