[fenix] For https://github.com/mozilla-mobile/fenix/issues/4127 - Converts Exceptions to LibState and adds tests
parent
ba9cf5973e
commit
b3c118d7ba
@ -1,62 +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.exceptions
|
||||
|
||||
import android.view.ViewGroup
|
||||
import org.mozilla.fenix.mvi.Action
|
||||
import org.mozilla.fenix.mvi.ActionBusFactory
|
||||
import org.mozilla.fenix.mvi.Change
|
||||
import org.mozilla.fenix.mvi.UIComponent
|
||||
import org.mozilla.fenix.mvi.UIComponentViewModelBase
|
||||
import org.mozilla.fenix.mvi.UIComponentViewModelProvider
|
||||
import org.mozilla.fenix.mvi.ViewState
|
||||
import org.mozilla.fenix.test.Mockable
|
||||
|
||||
data class ExceptionsItem(val url: String)
|
||||
|
||||
@Mockable
|
||||
class ExceptionsComponent(
|
||||
private val container: ViewGroup,
|
||||
bus: ActionBusFactory,
|
||||
viewModelProvider: UIComponentViewModelProvider<ExceptionsState, ExceptionsChange>
|
||||
) :
|
||||
UIComponent<ExceptionsState, ExceptionsAction, ExceptionsChange>(
|
||||
bus.getManagedEmitter(ExceptionsAction::class.java),
|
||||
bus.getSafeManagedObservable(ExceptionsChange::class.java),
|
||||
viewModelProvider
|
||||
) {
|
||||
|
||||
override fun initView() = ExceptionsUIView(container, actionEmitter, changesObservable)
|
||||
|
||||
init {
|
||||
bind()
|
||||
}
|
||||
}
|
||||
|
||||
data class ExceptionsState(val items: List<ExceptionsItem>) : ViewState
|
||||
|
||||
sealed class ExceptionsAction : Action {
|
||||
object LearnMore : ExceptionsAction()
|
||||
sealed class Delete : ExceptionsAction() {
|
||||
object All : Delete()
|
||||
data class One(val item: ExceptionsItem) : Delete()
|
||||
}
|
||||
}
|
||||
|
||||
sealed class ExceptionsChange : Change {
|
||||
data class Change(val list: List<ExceptionsItem>) : ExceptionsChange()
|
||||
}
|
||||
|
||||
class ExceptionsViewModel(
|
||||
initialState: ExceptionsState
|
||||
) : UIComponentViewModelBase<ExceptionsState, ExceptionsChange>(initialState, reducer) {
|
||||
companion object {
|
||||
val reducer: (ExceptionsState, ExceptionsChange) -> ExceptionsState = { state, change ->
|
||||
when (change) {
|
||||
is ExceptionsChange.Change -> state.copy(items = change.list)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/* 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.exceptions
|
||||
|
||||
/**
|
||||
* Interactor for the exceptions screen
|
||||
* Provides implementations for the ExceptionsViewInteractor
|
||||
*/
|
||||
class ExceptionsInteractor(
|
||||
private val learnMore: () -> Unit,
|
||||
private val deleteOne: (ExceptionsItem) -> Unit,
|
||||
private val deleteAll: () -> Unit
|
||||
) : ExceptionsViewInteractor {
|
||||
override fun onLearnMore() {
|
||||
learnMore.invoke()
|
||||
}
|
||||
|
||||
override fun onDeleteAll() {
|
||||
deleteAll.invoke()
|
||||
}
|
||||
|
||||
override fun onDeleteOne(item: ExceptionsItem) {
|
||||
deleteOne.invoke(item)
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/* 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.exceptions
|
||||
|
||||
import mozilla.components.lib.state.Action
|
||||
import mozilla.components.lib.state.State
|
||||
import mozilla.components.lib.state.Store
|
||||
|
||||
/**
|
||||
* Class representing an exception item
|
||||
* @property url Host of the exception
|
||||
*/
|
||||
data class ExceptionsItem(val url: String)
|
||||
|
||||
/**
|
||||
* The [Store] for holding the [ExceptionsState] and applying [ExceptionsAction]s.
|
||||
*/
|
||||
class ExceptionsStore(initialState: ExceptionsState) :
|
||||
Store<ExceptionsState, ExceptionsAction>(initialState, ::exceptionsStateReducer)
|
||||
|
||||
/**
|
||||
* Actions to dispatch through the `ExceptionsStore` to modify `ExceptionsState` through the reducer.
|
||||
*/
|
||||
sealed class ExceptionsAction : Action {
|
||||
data class Change(val list: List<ExceptionsItem>) : ExceptionsAction()
|
||||
}
|
||||
|
||||
/**
|
||||
* The state for the Exceptions Screen
|
||||
* @property items List of exceptions to display
|
||||
*/
|
||||
data class ExceptionsState(val items: List<ExceptionsItem>) : State
|
||||
|
||||
/**
|
||||
* The ExceptionsState Reducer.
|
||||
*/
|
||||
fun exceptionsStateReducer(state: ExceptionsState, action: ExceptionsAction): ExceptionsState {
|
||||
return when (action) {
|
||||
is ExceptionsAction.Change -> state.copy(items = action.list)
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/* 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.exceptions
|
||||
|
||||
import io.mockk.mockk
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class ExceptionsInteractorTest {
|
||||
|
||||
@Test
|
||||
fun onLearnMore() {
|
||||
var learnMoreClicked = false
|
||||
val interactor = ExceptionsInteractor(
|
||||
{ learnMoreClicked = true },
|
||||
mockk(),
|
||||
mockk()
|
||||
)
|
||||
interactor.onLearnMore()
|
||||
assertEquals(true, learnMoreClicked)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onDeleteAll() {
|
||||
var onDeleteAll = false
|
||||
val interactor = ExceptionsInteractor(
|
||||
mockk(),
|
||||
mockk(),
|
||||
{ onDeleteAll = true }
|
||||
)
|
||||
interactor.onDeleteAll()
|
||||
assertEquals(true, onDeleteAll)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onDeleteOne() {
|
||||
var exceptionsItemReceived: ExceptionsItem? = null
|
||||
val exceptionsItem = ExceptionsItem("url")
|
||||
val interactor = ExceptionsInteractor(
|
||||
mockk(),
|
||||
{ exceptionsItemReceived = exceptionsItem },
|
||||
mockk()
|
||||
)
|
||||
interactor.onDeleteOne(exceptionsItem)
|
||||
assertEquals(exceptionsItemReceived, exceptionsItem)
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/* 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.exceptions
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotSame
|
||||
import org.junit.Test
|
||||
|
||||
class ExceptionsStoreTest {
|
||||
@Test
|
||||
fun onChange() = runBlocking {
|
||||
val initialState = emptyDefaultState()
|
||||
val store = ExceptionsStore(initialState)
|
||||
val newExceptionsItem = ExceptionsItem("URL")
|
||||
|
||||
store.dispatch(ExceptionsAction.Change(listOf(newExceptionsItem))).join()
|
||||
assertNotSame(initialState, store.state)
|
||||
assertEquals(
|
||||
store.state.items,
|
||||
listOf(newExceptionsItem)
|
||||
)
|
||||
}
|
||||
|
||||
private fun emptyDefaultState(): ExceptionsState = ExceptionsState(
|
||||
items = listOf()
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue