[fenix] For https://github.com/mozilla-mobile/fenix/issues/352: Delete a download
parent
5344dceead
commit
88375a2294
@ -0,0 +1,44 @@
|
||||
/* 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.library.downloads
|
||||
|
||||
import android.content.Context
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import mozilla.components.browser.menu2.BrowserMenuController
|
||||
import mozilla.components.concept.menu.MenuController
|
||||
import mozilla.components.concept.menu.candidate.TextMenuCandidate
|
||||
import mozilla.components.concept.menu.candidate.TextStyle
|
||||
import mozilla.components.support.ktx.android.content.getColorFromAttr
|
||||
import org.mozilla.fenix.R
|
||||
|
||||
class DownloadItemMenu(
|
||||
private val context: Context,
|
||||
private val onItemTapped: (Item) -> Unit
|
||||
) {
|
||||
|
||||
enum class Item {
|
||||
Delete
|
||||
}
|
||||
|
||||
val menuController: MenuController by lazy {
|
||||
BrowserMenuController().apply {
|
||||
submitList(menuItems())
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
internal fun menuItems(): List<TextMenuCandidate> {
|
||||
return listOf(
|
||||
TextMenuCandidate(
|
||||
text = context.getString(R.string.history_delete_item),
|
||||
textStyle = TextStyle(
|
||||
color = context.getColorFromAttr(R.attr.destructive)
|
||||
)
|
||||
) {
|
||||
onItemTapped.invoke(Item.Delete)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- 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/. -->
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/delete_downloads_multi_select"
|
||||
android:title="@string/download_delete_item"
|
||||
app:showAsAction="never" />
|
||||
</menu>
|
@ -0,0 +1,70 @@
|
||||
/* 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.library.downloads
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mozilla.components.browser.state.state.content.DownloadState
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotSame
|
||||
import org.junit.Test
|
||||
|
||||
class DownloadFragmentStoreTest {
|
||||
private val downloadItem = DownloadItem("0", "title", "url", "77", "jpg", DownloadState.Status.COMPLETED)
|
||||
private val newDownloadItem = DownloadItem("1", "title", "url", "77", "jpg", DownloadState.Status.COMPLETED)
|
||||
|
||||
@Test
|
||||
fun exitEditMode() = runBlocking {
|
||||
val initialState = oneItemEditState()
|
||||
val store = DownloadFragmentStore(initialState)
|
||||
|
||||
store.dispatch(DownloadFragmentAction.ExitEditMode).join()
|
||||
assertNotSame(initialState, store.state)
|
||||
assertEquals(store.state.mode, DownloadFragmentState.Mode.Normal)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun itemAddedForRemoval() = runBlocking {
|
||||
val initialState = emptyDefaultState()
|
||||
val store = DownloadFragmentStore(initialState)
|
||||
|
||||
store.dispatch(DownloadFragmentAction.AddItemForRemoval(newDownloadItem)).join()
|
||||
assertNotSame(initialState, store.state)
|
||||
assertEquals(
|
||||
store.state.mode,
|
||||
DownloadFragmentState.Mode.Editing(setOf(newDownloadItem))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun removeItemForRemoval() = runBlocking {
|
||||
val initialState = twoItemEditState()
|
||||
val store = DownloadFragmentStore(initialState)
|
||||
|
||||
store.dispatch(DownloadFragmentAction.RemoveItemForRemoval(newDownloadItem)).join()
|
||||
assertNotSame(initialState, store.state)
|
||||
assertEquals(store.state.mode, DownloadFragmentState.Mode.Editing(setOf(downloadItem)))
|
||||
}
|
||||
|
||||
private fun emptyDefaultState(): DownloadFragmentState = DownloadFragmentState(
|
||||
items = listOf(),
|
||||
mode = DownloadFragmentState.Mode.Normal,
|
||||
pendingDeletionIds = emptySet(),
|
||||
isDeletingItems = false
|
||||
)
|
||||
|
||||
private fun oneItemEditState(): DownloadFragmentState = DownloadFragmentState(
|
||||
items = listOf(),
|
||||
mode = DownloadFragmentState.Mode.Editing(setOf(downloadItem)),
|
||||
pendingDeletionIds = emptySet(),
|
||||
isDeletingItems = false
|
||||
)
|
||||
|
||||
private fun twoItemEditState(): DownloadFragmentState = DownloadFragmentState(
|
||||
items = listOf(),
|
||||
mode = DownloadFragmentState.Mode.Editing(setOf(downloadItem, newDownloadItem)),
|
||||
pendingDeletionIds = emptySet(),
|
||||
isDeletingItems = false
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue