Rename BookmarkStore/State/Action to BookmarkFragmentStore/State/Action.
parent
3171f3a1a7
commit
76063e307a
|
@ -25,11 +25,11 @@ class BookmarkAdapter(val emptyView: View, val interactor: BookmarkViewInteracto
|
|||
RecyclerView.Adapter<BookmarkNodeViewHolder>(), SelectionHolder<BookmarkNode> {
|
||||
|
||||
private var tree: List<BookmarkNode> = listOf()
|
||||
private var mode: BookmarkState.Mode = BookmarkState.Mode.Normal
|
||||
private var mode: BookmarkFragmentState.Mode = BookmarkFragmentState.Mode.Normal
|
||||
override val selectedItems: Set<BookmarkNode> get() = mode.selectedItems
|
||||
private var isFirstRun = true
|
||||
|
||||
fun updateData(tree: BookmarkNode?, mode: BookmarkState.Mode) {
|
||||
fun updateData(tree: BookmarkNode?, mode: BookmarkFragmentState.Mode) {
|
||||
val diffUtil = DiffUtil.calculateDiff(
|
||||
BookmarkDiffUtil(
|
||||
this.tree,
|
||||
|
@ -52,8 +52,8 @@ class BookmarkAdapter(val emptyView: View, val interactor: BookmarkViewInteracto
|
|||
private class BookmarkDiffUtil(
|
||||
val old: List<BookmarkNode>,
|
||||
val new: List<BookmarkNode>,
|
||||
val oldMode: BookmarkState.Mode,
|
||||
val newMode: BookmarkState.Mode
|
||||
val oldMode: BookmarkFragmentState.Mode,
|
||||
val newMode: BookmarkFragmentState.Mode
|
||||
) : DiffUtil.Callback() {
|
||||
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
|
||||
old[oldItemPosition].guid == new[newItemPosition].guid
|
||||
|
|
|
@ -54,7 +54,7 @@ import org.mozilla.fenix.utils.allowUndo
|
|||
@SuppressWarnings("TooManyFunctions", "LargeClass")
|
||||
class BookmarkFragment : LibraryPageFragment<BookmarkNode>(), BackHandler, AccountObserver {
|
||||
|
||||
private lateinit var bookmarkStore: BookmarkStore
|
||||
private lateinit var bookmarkStore: BookmarkFragmentStore
|
||||
private lateinit var bookmarkView: BookmarkView
|
||||
private lateinit var signInView: SignInView
|
||||
private lateinit var bookmarkInteractor: BookmarkFragmentInteractor
|
||||
|
@ -86,7 +86,7 @@ class BookmarkFragment : LibraryPageFragment<BookmarkNode>(), BackHandler, Accou
|
|||
val view = inflater.inflate(R.layout.fragment_bookmark, container, false)
|
||||
|
||||
bookmarkStore = StoreProvider.get(this) {
|
||||
BookmarkStore(BookmarkState(null))
|
||||
BookmarkFragmentStore(BookmarkFragmentState(null))
|
||||
}
|
||||
bookmarkInteractor = BookmarkFragmentInteractor(
|
||||
bookmarkStore = bookmarkStore,
|
||||
|
@ -167,10 +167,10 @@ class BookmarkFragment : LibraryPageFragment<BookmarkNode>(), BackHandler, Accou
|
|||
|
||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||
when (val mode = bookmarkStore.state.mode) {
|
||||
BookmarkState.Mode.Normal -> {
|
||||
BookmarkFragmentState.Mode.Normal -> {
|
||||
inflater.inflate(R.menu.bookmarks_menu, menu)
|
||||
}
|
||||
is BookmarkState.Mode.Selecting -> {
|
||||
is BookmarkFragmentState.Mode.Selecting -> {
|
||||
if (mode.selectedItems.any { it.type != BookmarkNodeType.ITEM }) {
|
||||
inflater.inflate(R.menu.bookmarks_select_multi_not_item, menu)
|
||||
} else {
|
||||
|
|
|
@ -22,17 +22,17 @@ import org.mozilla.fenix.lib.Do
|
|||
*/
|
||||
@SuppressWarnings("TooManyFunctions")
|
||||
class BookmarkFragmentInteractor(
|
||||
private val bookmarkStore: BookmarkStore,
|
||||
private val bookmarkStore: BookmarkFragmentStore,
|
||||
private val viewModel: BookmarksSharedViewModel,
|
||||
private val bookmarksController: BookmarkController,
|
||||
private val metrics: MetricController
|
||||
) : BookmarkViewInteractor, SignInInteractor {
|
||||
|
||||
override fun onBookmarksChanged(node: BookmarkNode) {
|
||||
bookmarkStore.dispatch(BookmarkAction.Change(node))
|
||||
bookmarkStore.dispatch(BookmarkFragmentAction.Change(node))
|
||||
}
|
||||
|
||||
override fun onSelectionModeSwitch(mode: BookmarkState.Mode) {
|
||||
override fun onSelectionModeSwitch(mode: BookmarkFragmentState.Mode) {
|
||||
bookmarksController.handleSelectionModeSwitch()
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ class BookmarkFragmentInteractor(
|
|||
}
|
||||
|
||||
override fun onAllBookmarksDeselected() {
|
||||
bookmarkStore.dispatch(BookmarkAction.DeselectAll)
|
||||
bookmarkStore.dispatch(BookmarkFragmentAction.DeselectAll)
|
||||
}
|
||||
|
||||
override fun onCopyPressed(item: BookmarkNode) {
|
||||
|
@ -119,11 +119,11 @@ class BookmarkFragmentInteractor(
|
|||
override fun select(item: BookmarkNode) {
|
||||
when (item.inRoots()) {
|
||||
true -> bookmarksController.handleBookmarkSelected(item)
|
||||
false -> bookmarkStore.dispatch(BookmarkAction.Select(item))
|
||||
false -> bookmarkStore.dispatch(BookmarkFragmentAction.Select(item))
|
||||
}
|
||||
}
|
||||
|
||||
override fun deselect(item: BookmarkNode) {
|
||||
bookmarkStore.dispatch(BookmarkAction.Deselect(item))
|
||||
bookmarkStore.dispatch(BookmarkFragmentAction.Deselect(item))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@ import mozilla.components.lib.state.Action
|
|||
import mozilla.components.lib.state.State
|
||||
import mozilla.components.lib.state.Store
|
||||
|
||||
class BookmarkStore(
|
||||
initalState: BookmarkState
|
||||
) : Store<BookmarkState, BookmarkAction>(
|
||||
initalState, ::bookmarkStateReducer
|
||||
class BookmarkFragmentStore(
|
||||
initalState: BookmarkFragmentState
|
||||
) : Store<BookmarkFragmentState, BookmarkFragmentAction>(
|
||||
initalState, ::bookmarkFragmentStateReducer
|
||||
)
|
||||
|
||||
/**
|
||||
|
@ -20,7 +20,7 @@ class BookmarkStore(
|
|||
* @property tree The current tree of bookmarks, if one is loaded
|
||||
* @property mode The current bookmark multi-selection mode
|
||||
*/
|
||||
data class BookmarkState(val tree: BookmarkNode?, val mode: Mode = Mode.Normal) : State {
|
||||
data class BookmarkFragmentState(val tree: BookmarkNode?, val mode: Mode = Mode.Normal) : State {
|
||||
sealed class Mode {
|
||||
open val selectedItems = emptySet<BookmarkNode>()
|
||||
|
||||
|
@ -32,11 +32,11 @@ data class BookmarkState(val tree: BookmarkNode?, val mode: Mode = Mode.Normal)
|
|||
/**
|
||||
* Actions to dispatch through the `BookmarkStore` to modify `BookmarkState` through the reducer.
|
||||
*/
|
||||
sealed class BookmarkAction : Action {
|
||||
data class Change(val tree: BookmarkNode) : BookmarkAction()
|
||||
data class Select(val item: BookmarkNode) : BookmarkAction()
|
||||
data class Deselect(val item: BookmarkNode) : BookmarkAction()
|
||||
object DeselectAll : BookmarkAction()
|
||||
sealed class BookmarkFragmentAction : Action {
|
||||
data class Change(val tree: BookmarkNode) : BookmarkFragmentAction()
|
||||
data class Select(val item: BookmarkNode) : BookmarkFragmentAction()
|
||||
data class Deselect(val item: BookmarkNode) : BookmarkFragmentAction()
|
||||
object DeselectAll : BookmarkFragmentAction()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -45,25 +45,25 @@ sealed class BookmarkAction : Action {
|
|||
* @param action the action to perform
|
||||
* @return the new bookmarks state
|
||||
*/
|
||||
fun bookmarkStateReducer(state: BookmarkState, action: BookmarkAction): BookmarkState {
|
||||
fun bookmarkFragmentStateReducer(state: BookmarkFragmentState, action: BookmarkFragmentAction): BookmarkFragmentState {
|
||||
return when (action) {
|
||||
is BookmarkAction.Change -> {
|
||||
is BookmarkFragmentAction.Change -> {
|
||||
val items = state.mode.selectedItems.filter { it in action.tree }
|
||||
state.copy(
|
||||
tree = action.tree,
|
||||
mode = if (items.isEmpty()) BookmarkState.Mode.Normal else BookmarkState.Mode.Selecting(items.toSet())
|
||||
mode = if (items.isEmpty()) BookmarkFragmentState.Mode.Normal else BookmarkFragmentState.Mode.Selecting(items.toSet())
|
||||
)
|
||||
}
|
||||
is BookmarkAction.Select ->
|
||||
state.copy(mode = BookmarkState.Mode.Selecting(state.mode.selectedItems + action.item))
|
||||
is BookmarkAction.Deselect -> {
|
||||
is BookmarkFragmentAction.Select ->
|
||||
state.copy(mode = BookmarkFragmentState.Mode.Selecting(state.mode.selectedItems + action.item))
|
||||
is BookmarkFragmentAction.Deselect -> {
|
||||
val items = state.mode.selectedItems - action.item
|
||||
state.copy(
|
||||
mode = if (items.isEmpty()) BookmarkState.Mode.Normal else BookmarkState.Mode.Selecting(items)
|
||||
mode = if (items.isEmpty()) BookmarkFragmentState.Mode.Normal else BookmarkFragmentState.Mode.Selecting(items)
|
||||
)
|
||||
}
|
||||
BookmarkAction.DeselectAll ->
|
||||
state.copy(mode = BookmarkState.Mode.Normal)
|
||||
BookmarkFragmentAction.DeselectAll ->
|
||||
state.copy(mode = BookmarkFragmentState.Mode.Normal)
|
||||
}
|
||||
}
|
||||
|
|
@ -34,7 +34,7 @@ interface BookmarkViewInteractor : SelectionInteractor<BookmarkNode> {
|
|||
*
|
||||
* @param mode the multi-select mode to switch to
|
||||
*/
|
||||
fun onSelectionModeSwitch(mode: BookmarkState.Mode)
|
||||
fun onSelectionModeSwitch(mode: BookmarkFragmentState.Mode)
|
||||
|
||||
/**
|
||||
* Opens up an interface to edit a bookmark node.
|
||||
|
@ -99,7 +99,7 @@ class BookmarkView(
|
|||
val view: View = LayoutInflater.from(container.context)
|
||||
.inflate(R.layout.component_bookmark, container, true)
|
||||
|
||||
private var mode: BookmarkState.Mode = BookmarkState.Mode.Normal
|
||||
private var mode: BookmarkFragmentState.Mode = BookmarkFragmentState.Mode.Normal
|
||||
private var tree: BookmarkNode? = null
|
||||
private var canGoBack = false
|
||||
|
||||
|
@ -112,7 +112,7 @@ class BookmarkView(
|
|||
}
|
||||
}
|
||||
|
||||
fun update(state: BookmarkState) {
|
||||
fun update(state: BookmarkFragmentState) {
|
||||
canGoBack = BookmarkRoot.Root.matches(state.tree)
|
||||
tree = state.tree
|
||||
if (state.mode != mode) {
|
||||
|
@ -122,16 +122,16 @@ class BookmarkView(
|
|||
|
||||
bookmarkAdapter.updateData(state.tree, mode)
|
||||
when (mode) {
|
||||
is BookmarkState.Mode.Normal ->
|
||||
is BookmarkFragmentState.Mode.Normal ->
|
||||
setUiForNormalMode(state.tree)
|
||||
is BookmarkState.Mode.Selecting ->
|
||||
is BookmarkFragmentState.Mode.Selecting ->
|
||||
setUiForSelectingMode(context.getString(R.string.bookmarks_multi_select_title, mode.selectedItems.size))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBackPressed(): Boolean {
|
||||
return when {
|
||||
mode is BookmarkState.Mode.Selecting -> {
|
||||
mode is BookmarkFragmentState.Mode.Selecting -> {
|
||||
interactor.onAllBookmarksDeselected()
|
||||
true
|
||||
}
|
||||
|
|
|
@ -48,12 +48,12 @@ internal class BookmarkAdapterTest {
|
|||
)
|
||||
)
|
||||
)
|
||||
bookmarkAdapter.updateData(tree, BookmarkState.Mode.Normal)
|
||||
bookmarkAdapter.updateData(null, BookmarkState.Mode.Normal)
|
||||
bookmarkAdapter.updateData(tree, BookmarkFragmentState.Mode.Normal)
|
||||
bookmarkAdapter.updateData(null, BookmarkFragmentState.Mode.Normal)
|
||||
verifyOrder {
|
||||
bookmarkAdapter.updateData(tree, BookmarkState.Mode.Normal)
|
||||
bookmarkAdapter.updateData(tree, BookmarkFragmentState.Mode.Normal)
|
||||
bookmarkAdapter.notifyItemRangeInserted(0, 3)
|
||||
bookmarkAdapter.updateData(null, BookmarkState.Mode.Normal)
|
||||
bookmarkAdapter.updateData(null, BookmarkFragmentState.Mode.Normal)
|
||||
bookmarkAdapter.notifyItemRangeRemoved(0, 3)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ class BookmarkFragmentInteractorTest {
|
|||
|
||||
private lateinit var interactor: BookmarkFragmentInteractor
|
||||
|
||||
private val bookmarkStore = spyk(BookmarkStore(BookmarkState(null)))
|
||||
private val bookmarkStore = spyk(BookmarkFragmentStore(BookmarkFragmentState(null)))
|
||||
private val sharedViewModel: BookmarksSharedViewModel = mockk(relaxed = true)
|
||||
private val bookmarkController: DefaultBookmarkController = mockk(relaxed = true)
|
||||
private val metrics: MetricController = mockk(relaxed = true)
|
||||
|
@ -57,7 +57,7 @@ class BookmarkFragmentInteractorTest {
|
|||
interactor.onBookmarksChanged(tree)
|
||||
|
||||
verify {
|
||||
bookmarkStore.dispatch(BookmarkAction.Change(tree))
|
||||
bookmarkStore.dispatch(BookmarkFragmentAction.Change(tree))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,7 +87,7 @@ class BookmarkFragmentInteractorTest {
|
|||
|
||||
@Test
|
||||
fun `switch between bookmark selection modes`() {
|
||||
interactor.onSelectionModeSwitch(BookmarkState.Mode.Normal)
|
||||
interactor.onSelectionModeSwitch(BookmarkFragmentState.Mode.Normal)
|
||||
|
||||
verify {
|
||||
bookmarkController.handleSelectionModeSwitch()
|
||||
|
@ -108,7 +108,7 @@ class BookmarkFragmentInteractorTest {
|
|||
interactor.select(item)
|
||||
|
||||
verify {
|
||||
bookmarkStore.dispatch(BookmarkAction.Select(item))
|
||||
bookmarkStore.dispatch(BookmarkFragmentAction.Select(item))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +117,7 @@ class BookmarkFragmentInteractorTest {
|
|||
interactor.deselect(item)
|
||||
|
||||
verify {
|
||||
bookmarkStore.dispatch(BookmarkAction.Deselect(item))
|
||||
bookmarkStore.dispatch(BookmarkFragmentAction.Deselect(item))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,7 +126,7 @@ class BookmarkFragmentInteractorTest {
|
|||
interactor.onAllBookmarksDeselected()
|
||||
|
||||
verify {
|
||||
bookmarkStore.dispatch(BookmarkAction.DeselectAll)
|
||||
bookmarkStore.dispatch(BookmarkFragmentAction.DeselectAll)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,161 @@
|
|||
/* 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.bookmarks
|
||||
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isEqualTo
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mozilla.components.concept.storage.BookmarkNode
|
||||
import mozilla.components.concept.storage.BookmarkNodeType
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Test
|
||||
|
||||
class BookmarkFragmentStoreTest {
|
||||
|
||||
@Test
|
||||
fun `change the tree of bookmarks starting from an empty tree`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(null)
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
assertThat(BookmarkFragmentState(null, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Change(tree)).join()
|
||||
|
||||
assertThat(initialState.copy(tree = tree)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `change the tree of bookmarks starting from an existing tree`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree)
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Change(newTree)).join()
|
||||
|
||||
assertThat(initialState.copy(tree = newTree)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `change the tree of bookmarks to the same value`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree)
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Change(tree)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ensure selected items remain selected after a tree change`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Selecting(setOf(item, subfolder)))
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Change(newTree)).join()
|
||||
|
||||
assertThat(BookmarkFragmentState(newTree, BookmarkFragmentState.Mode.Selecting(setOf(subfolder)))).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select and deselect bookmarks changes the mode`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree)
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Select(childItem)).join()
|
||||
|
||||
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Selecting(setOf(childItem)))).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Deselect(childItem)).join()
|
||||
|
||||
assertThat(BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selecting the same item twice does nothing`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Selecting(setOf(item, subfolder)))
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Select(item)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselecting an unselected bookmark does nothing`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Selecting(setOf(childItem)))
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Deselect(item)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselecting while not in selecting mode does nothing`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Deselect(item)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselect all bookmarks changes the mode`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Selecting(setOf(item, childItem)))
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.DeselectAll).join()
|
||||
|
||||
assertThat(initialState.copy(mode = BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselect all bookmarks when none are selected`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Normal)
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.DeselectAll)
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deleting bookmarks changes the mode`() = runBlocking {
|
||||
val initialState = BookmarkFragmentState(tree, BookmarkFragmentState.Mode.Selecting(setOf(item, childItem)))
|
||||
val store = BookmarkFragmentStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkFragmentAction.Change(newTree)).join()
|
||||
|
||||
assertThat(initialState.copy(tree = newTree, mode = BookmarkFragmentState.Mode.Normal)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
private val item = BookmarkNode(BookmarkNodeType.ITEM, "456", "123", 0, "Mozilla", "http://mozilla.org", null)
|
||||
private val separator = BookmarkNode(BookmarkNodeType.SEPARATOR, "789", "123", 1, null, null, null)
|
||||
private val subfolder = BookmarkNode(BookmarkNodeType.FOLDER, "987", "123", 0, "Subfolder", null, listOf())
|
||||
private val childItem = BookmarkNode(
|
||||
BookmarkNodeType.ITEM,
|
||||
"987",
|
||||
"123",
|
||||
2,
|
||||
"Firefox",
|
||||
"https://www.mozilla.org/en-US/firefox/",
|
||||
null
|
||||
)
|
||||
private val tree = BookmarkNode(
|
||||
BookmarkNodeType.FOLDER, "123", null, 0, "Mobile", null, listOf(item, separator, childItem, subfolder)
|
||||
)
|
||||
private val newTree = BookmarkNode(
|
||||
BookmarkNodeType.FOLDER,
|
||||
"123",
|
||||
null,
|
||||
0,
|
||||
"Mobile",
|
||||
null,
|
||||
listOf(separator, subfolder)
|
||||
)
|
||||
}
|
|
@ -1,161 +0,0 @@
|
|||
/* 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.bookmarks
|
||||
|
||||
import assertk.assertThat
|
||||
import assertk.assertions.isEqualTo
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import mozilla.components.concept.storage.BookmarkNode
|
||||
import mozilla.components.concept.storage.BookmarkNodeType
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Test
|
||||
|
||||
class BookmarkStoreTest {
|
||||
|
||||
@Test
|
||||
fun `change the tree of bookmarks starting from an empty tree`() = runBlocking {
|
||||
val initialState = BookmarkState(null)
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
assertThat(BookmarkState(null, BookmarkState.Mode.Normal)).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkAction.Change(tree)).join()
|
||||
|
||||
assertThat(initialState.copy(tree = tree)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `change the tree of bookmarks starting from an existing tree`() = runBlocking {
|
||||
val initialState = BookmarkState(tree)
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
assertThat(BookmarkState(tree, BookmarkState.Mode.Normal)).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkAction.Change(newTree)).join()
|
||||
|
||||
assertThat(initialState.copy(tree = newTree)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `change the tree of bookmarks to the same value`() = runBlocking {
|
||||
val initialState = BookmarkState(tree)
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
assertThat(BookmarkState(tree, BookmarkState.Mode.Normal)).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkAction.Change(tree)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ensure selected items remain selected after a tree change`() = runBlocking {
|
||||
val initialState = BookmarkState(tree, BookmarkState.Mode.Selecting(setOf(item, subfolder)))
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.Change(newTree)).join()
|
||||
|
||||
assertThat(BookmarkState(newTree, BookmarkState.Mode.Selecting(setOf(subfolder)))).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select and deselect bookmarks changes the mode`() = runBlocking {
|
||||
val initialState = BookmarkState(tree)
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.Select(childItem)).join()
|
||||
|
||||
assertThat(BookmarkState(tree, BookmarkState.Mode.Selecting(setOf(childItem)))).isEqualTo(store.state)
|
||||
|
||||
store.dispatch(BookmarkAction.Deselect(childItem)).join()
|
||||
|
||||
assertThat(BookmarkState(tree, BookmarkState.Mode.Normal)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selecting the same item twice does nothing`() = runBlocking {
|
||||
val initialState = BookmarkState(tree, BookmarkState.Mode.Selecting(setOf(item, subfolder)))
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.Select(item)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselecting an unselected bookmark does nothing`() = runBlocking {
|
||||
val initialState = BookmarkState(tree, BookmarkState.Mode.Selecting(setOf(childItem)))
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.Deselect(item)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselecting while not in selecting mode does nothing`() = runBlocking {
|
||||
val initialState = BookmarkState(tree, BookmarkState.Mode.Normal)
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.Deselect(item)).join()
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselect all bookmarks changes the mode`() = runBlocking {
|
||||
val initialState = BookmarkState(tree, BookmarkState.Mode.Selecting(setOf(item, childItem)))
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.DeselectAll).join()
|
||||
|
||||
assertThat(initialState.copy(mode = BookmarkState.Mode.Normal)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deselect all bookmarks when none are selected`() = runBlocking {
|
||||
val initialState = BookmarkState(tree, BookmarkState.Mode.Normal)
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.DeselectAll)
|
||||
|
||||
assertSame(initialState, store.state)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deleting bookmarks changes the mode`() = runBlocking {
|
||||
val initialState = BookmarkState(tree, BookmarkState.Mode.Selecting(setOf(item, childItem)))
|
||||
val store = BookmarkStore(initialState)
|
||||
|
||||
store.dispatch(BookmarkAction.Change(newTree)).join()
|
||||
|
||||
assertThat(initialState.copy(tree = newTree, mode = BookmarkState.Mode.Normal)).isEqualTo(store.state)
|
||||
}
|
||||
|
||||
private val item = BookmarkNode(BookmarkNodeType.ITEM, "456", "123", 0, "Mozilla", "http://mozilla.org", null)
|
||||
private val separator = BookmarkNode(BookmarkNodeType.SEPARATOR, "789", "123", 1, null, null, null)
|
||||
private val subfolder = BookmarkNode(BookmarkNodeType.FOLDER, "987", "123", 0, "Subfolder", null, listOf())
|
||||
private val childItem = BookmarkNode(
|
||||
BookmarkNodeType.ITEM,
|
||||
"987",
|
||||
"123",
|
||||
2,
|
||||
"Firefox",
|
||||
"https://www.mozilla.org/en-US/firefox/",
|
||||
null
|
||||
)
|
||||
private val tree = BookmarkNode(
|
||||
BookmarkNodeType.FOLDER, "123", null, 0, "Mobile", null, listOf(item, separator, childItem, subfolder)
|
||||
)
|
||||
private val newTree = BookmarkNode(
|
||||
BookmarkNodeType.FOLDER,
|
||||
"123",
|
||||
null,
|
||||
0,
|
||||
"Mobile",
|
||||
null,
|
||||
listOf(separator, subfolder)
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue