[fenix] Add tests for some of collection creation
parent
7e714c08e8
commit
4e7ae1f1bd
@ -0,0 +1,96 @@
|
|||||||
|
/* 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.collections
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.Button
|
||||||
|
import android.widget.ImageButton
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import mozilla.components.support.ktx.android.content.getDrawableWithTint
|
||||||
|
import org.mozilla.fenix.R
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates views in [R.id.bottom_button_bar_layout] for collection creation.
|
||||||
|
*/
|
||||||
|
class CollectionCreationBottomBarView(
|
||||||
|
private val interactor: CollectionCreationInteractor,
|
||||||
|
private val layout: ViewGroup,
|
||||||
|
private val iconButton: ImageButton,
|
||||||
|
private val textView: TextView,
|
||||||
|
private val saveButton: Button
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun update(step: SaveCollectionStep, state: CollectionCreationState) {
|
||||||
|
when (step) {
|
||||||
|
SaveCollectionStep.SelectTabs -> updateForSelectTabs(state)
|
||||||
|
SaveCollectionStep.SelectCollection -> updateForSelectCollection()
|
||||||
|
else -> { /* noop */ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateForSelectTabs(state: CollectionCreationState) {
|
||||||
|
layout.setOnClickListener(null)
|
||||||
|
layout.isClickable = false
|
||||||
|
|
||||||
|
iconButton.apply {
|
||||||
|
val drawable = context.getDrawableWithTint(
|
||||||
|
R.drawable.ic_close,
|
||||||
|
ContextCompat.getColor(context, R.color.photonWhite)
|
||||||
|
)
|
||||||
|
setImageDrawable(drawable)
|
||||||
|
contentDescription = context.getString(R.string.create_collection_close)
|
||||||
|
importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_YES
|
||||||
|
setOnClickListener { interactor.close() }
|
||||||
|
}
|
||||||
|
|
||||||
|
textView.apply {
|
||||||
|
text = if (state.selectedTabs.isEmpty()) {
|
||||||
|
context.getString(R.string.create_collection_save_to_collection_empty)
|
||||||
|
} else {
|
||||||
|
context.getString(
|
||||||
|
if (state.selectedTabs.size == 1)
|
||||||
|
R.string.create_collection_save_to_collection_tab_selected else
|
||||||
|
R.string.create_collection_save_to_collection_tabs_selected,
|
||||||
|
state.selectedTabs.size
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
saveButton.apply {
|
||||||
|
setOnClickListener {
|
||||||
|
if (state.selectedTabCollection != null) {
|
||||||
|
interactor.selectCollection(
|
||||||
|
collection = state.selectedTabCollection,
|
||||||
|
tabs = state.selectedTabs.toList()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
interactor.saveTabsToCollection(tabs = state.selectedTabs.toList())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
isVisible = state.selectedTabs.isNotEmpty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateForSelectCollection() {
|
||||||
|
saveButton.visibility = View.GONE
|
||||||
|
|
||||||
|
textView.text =
|
||||||
|
textView.context.getString(R.string.create_collection_add_new_collection)
|
||||||
|
|
||||||
|
iconButton.apply {
|
||||||
|
val drawable = context.getDrawableWithTint(
|
||||||
|
R.drawable.ic_new,
|
||||||
|
ContextCompat.getColor(context, R.color.photonWhite)
|
||||||
|
)
|
||||||
|
setImageDrawable(drawable)
|
||||||
|
contentDescription = null
|
||||||
|
importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
|
||||||
|
setOnClickListener { interactor.addNewCollection() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,174 @@
|
|||||||
|
/* 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.collections
|
||||||
|
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.Button
|
||||||
|
import android.widget.ImageButton
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.core.view.isVisible
|
||||||
|
import io.mockk.Called
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import mozilla.components.feature.tab.collections.TabCollection
|
||||||
|
import mozilla.components.support.test.robolectric.testContext
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||||
|
import org.mozilla.fenix.home.Tab
|
||||||
|
|
||||||
|
@RunWith(FenixRobolectricTestRunner::class)
|
||||||
|
class CollectionCreationBottomBarViewTest {
|
||||||
|
|
||||||
|
private lateinit var bottomBarView: CollectionCreationBottomBarView
|
||||||
|
private lateinit var interactor: CollectionCreationInteractor
|
||||||
|
private lateinit var layout: ViewGroup
|
||||||
|
private lateinit var iconButton: ImageButton
|
||||||
|
private lateinit var textView: TextView
|
||||||
|
private lateinit var saveButton: Button
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setup() {
|
||||||
|
interactor = mockk(relaxed = true)
|
||||||
|
layout = mockk(relaxed = true)
|
||||||
|
iconButton = ImageButton(testContext)
|
||||||
|
textView = TextView(testContext)
|
||||||
|
saveButton = Button(testContext)
|
||||||
|
|
||||||
|
bottomBarView = CollectionCreationBottomBarView(
|
||||||
|
interactor,
|
||||||
|
layout,
|
||||||
|
iconButton,
|
||||||
|
textView,
|
||||||
|
saveButton
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testIconButtonUpdateForSelectTabs() {
|
||||||
|
bottomBarView.update(SaveCollectionStep.SelectTabs, CollectionCreationState(0))
|
||||||
|
|
||||||
|
verify { layout.setOnClickListener(null) }
|
||||||
|
verify { layout.isClickable = false }
|
||||||
|
|
||||||
|
assertEquals("Close", iconButton.contentDescription)
|
||||||
|
assertEquals(View.IMPORTANT_FOR_ACCESSIBILITY_YES, iconButton.importantForAccessibility)
|
||||||
|
|
||||||
|
iconButton.performClick()
|
||||||
|
verify { interactor.close() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testIconButtonUpdateForSelectCollection() {
|
||||||
|
bottomBarView.update(SaveCollectionStep.SelectCollection, CollectionCreationState(0))
|
||||||
|
|
||||||
|
verify { layout wasNot Called }
|
||||||
|
|
||||||
|
assertEquals(null, iconButton.contentDescription)
|
||||||
|
assertEquals(View.IMPORTANT_FOR_ACCESSIBILITY_NO, iconButton.importantForAccessibility)
|
||||||
|
|
||||||
|
iconButton.performClick()
|
||||||
|
verify { interactor.addNewCollection() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTextViewUpdateForSelectTabs() {
|
||||||
|
bottomBarView.update(
|
||||||
|
SaveCollectionStep.SelectTabs,
|
||||||
|
CollectionCreationState(
|
||||||
|
0,
|
||||||
|
selectedTabs = emptySet()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertEquals("Select tabs to save", textView.text)
|
||||||
|
|
||||||
|
bottomBarView.update(
|
||||||
|
SaveCollectionStep.SelectTabs,
|
||||||
|
CollectionCreationState(
|
||||||
|
0,
|
||||||
|
selectedTabs = setOf(mockk())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertEquals("1 tab selected", textView.text)
|
||||||
|
|
||||||
|
bottomBarView.update(
|
||||||
|
SaveCollectionStep.SelectTabs,
|
||||||
|
CollectionCreationState(
|
||||||
|
0,
|
||||||
|
selectedTabs = setOf(mockk(), mockk())
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertEquals("2 tabs selected", textView.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTextViewUpdateForSelectCollection() {
|
||||||
|
bottomBarView.update(SaveCollectionStep.SelectCollection, CollectionCreationState(0))
|
||||||
|
|
||||||
|
assertEquals("Add new collection", textView.text)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSaveButtonUpdateForSelectTabs() {
|
||||||
|
val collection = mockk<TabCollection>()
|
||||||
|
val tabs = setOf<Tab>(mockk(), mockk())
|
||||||
|
|
||||||
|
bottomBarView.update(
|
||||||
|
SaveCollectionStep.SelectTabs,
|
||||||
|
CollectionCreationState(
|
||||||
|
0,
|
||||||
|
selectedTabCollection = null,
|
||||||
|
selectedTabs = emptySet()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertFalse(saveButton.isVisible)
|
||||||
|
|
||||||
|
bottomBarView.update(
|
||||||
|
SaveCollectionStep.SelectTabs,
|
||||||
|
CollectionCreationState(
|
||||||
|
0,
|
||||||
|
selectedTabCollection = collection,
|
||||||
|
selectedTabs = emptySet()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertFalse(saveButton.isVisible)
|
||||||
|
|
||||||
|
bottomBarView.update(
|
||||||
|
SaveCollectionStep.SelectTabs,
|
||||||
|
CollectionCreationState(
|
||||||
|
0,
|
||||||
|
selectedTabCollection = null,
|
||||||
|
selectedTabs = tabs
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertTrue(saveButton.isVisible)
|
||||||
|
saveButton.performClick()
|
||||||
|
verify { interactor.saveTabsToCollection(tabs.toList()) }
|
||||||
|
|
||||||
|
bottomBarView.update(
|
||||||
|
SaveCollectionStep.SelectTabs,
|
||||||
|
CollectionCreationState(
|
||||||
|
0,
|
||||||
|
selectedTabCollection = collection,
|
||||||
|
selectedTabs = tabs
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assertTrue(saveButton.isVisible)
|
||||||
|
saveButton.performClick()
|
||||||
|
verify { interactor.selectCollection(collection, tabs.toList()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSaveButtonUpdateForSelectCollection() {
|
||||||
|
bottomBarView.update(SaveCollectionStep.SelectCollection, CollectionCreationState(0))
|
||||||
|
|
||||||
|
assertFalse(saveButton.isVisible)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue