From 667e20c13b5661e5a1b563d123c2246fb3cf9b30 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Thu, 27 Aug 2020 18:27:34 -0700 Subject: [PATCH] [fenix] Add tests for collection creation (https://github.com/mozilla-mobile/fenix/pull/13909) --- .../collections/CollectionCreationStore.kt | 6 +- .../CollectionCreationStoreTest.kt | 76 +++++++++++++++++++ .../CollectionCreationTabListAdapterTest.kt | 47 ++++++++++-- 3 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 app/src/test/java/org/mozilla/fenix/collections/CollectionCreationStoreTest.kt diff --git a/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationStore.kt b/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationStore.kt index c7d4edffad..f15ccf9637 100644 --- a/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationStore.kt +++ b/app/src/main/java/org/mozilla/fenix/collections/CollectionCreationStore.kt @@ -67,6 +67,8 @@ private fun collectionCreationReducer( is CollectionCreationAction.RemoveAllTabs -> prevState.copy(selectedTabs = emptySet()) is CollectionCreationAction.TabAdded -> prevState.copy(selectedTabs = prevState.selectedTabs + action.tab) is CollectionCreationAction.TabRemoved -> prevState.copy(selectedTabs = prevState.selectedTabs - action.tab) - is CollectionCreationAction.StepChanged -> prevState.copy(saveCollectionStep = action.saveCollectionStep, - defaultCollectionNumber = action.defaultCollectionNumber) + is CollectionCreationAction.StepChanged -> prevState.copy( + saveCollectionStep = action.saveCollectionStep, + defaultCollectionNumber = action.defaultCollectionNumber + ) } diff --git a/app/src/test/java/org/mozilla/fenix/collections/CollectionCreationStoreTest.kt b/app/src/test/java/org/mozilla/fenix/collections/CollectionCreationStoreTest.kt new file mode 100644 index 0000000000..e6d42072d1 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/collections/CollectionCreationStoreTest.kt @@ -0,0 +1,76 @@ +/* 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 io.mockk.mockk +import mozilla.components.support.test.ext.joinBlocking +import org.junit.Assert.assertEquals +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 CollectionCreationStoreTest { + + @Test + fun `select and deselect all tabs`() { + val tabs = listOf(mockk(), mockk()) + val store = CollectionCreationStore( + CollectionCreationState( + tabs = tabs, + selectedTabs = emptySet() + ) + ) + + store.dispatch(CollectionCreationAction.AddAllTabs).joinBlocking() + assertEquals(tabs.toSet(), store.state.selectedTabs) + + store.dispatch(CollectionCreationAction.RemoveAllTabs).joinBlocking() + assertEquals(emptySet(), store.state.selectedTabs) + } + + @Test + fun `select and deselect individual tabs`() { + val tab1 = mockk() + val tab2 = mockk() + val tab3 = mockk() + val store = CollectionCreationStore( + CollectionCreationState( + tabs = listOf(tab1, tab2), + selectedTabs = setOf(tab2) + ) + ) + + store.dispatch(CollectionCreationAction.TabAdded(tab2)).joinBlocking() + assertEquals(setOf(tab2), store.state.selectedTabs) + + store.dispatch(CollectionCreationAction.TabAdded(tab1)).joinBlocking() + assertEquals(setOf(tab1, tab2), store.state.selectedTabs) + + store.dispatch(CollectionCreationAction.TabAdded(tab3)).joinBlocking() + assertEquals(setOf(tab1, tab2, tab3), store.state.selectedTabs) + + store.dispatch(CollectionCreationAction.TabRemoved(tab2)).joinBlocking() + assertEquals(setOf(tab1, tab3), store.state.selectedTabs) + } + + @Test + fun `change the current step`() { + val store = CollectionCreationStore( + CollectionCreationState( + saveCollectionStep = SaveCollectionStep.SelectTabs, + defaultCollectionNumber = 1 + ) + ) + + store.dispatch(CollectionCreationAction.StepChanged( + saveCollectionStep = SaveCollectionStep.RenameCollection, + defaultCollectionNumber = 3 + )).joinBlocking() + assertEquals(SaveCollectionStep.RenameCollection, store.state.saveCollectionStep) + assertEquals(3, store.state.defaultCollectionNumber) + } +} diff --git a/app/src/test/java/org/mozilla/fenix/collections/CollectionCreationTabListAdapterTest.kt b/app/src/test/java/org/mozilla/fenix/collections/CollectionCreationTabListAdapterTest.kt index cc451b1ff9..2d75a55656 100644 --- a/app/src/test/java/org/mozilla/fenix/collections/CollectionCreationTabListAdapterTest.kt +++ b/app/src/test/java/org/mozilla/fenix/collections/CollectionCreationTabListAdapterTest.kt @@ -28,11 +28,18 @@ class CollectionCreationTabListAdapterTest { private lateinit var interactor: CollectionCreationInteractor private lateinit var adapter: CollectionCreationTabListAdapter + private lateinit var mozillaTab: Tab @Before fun setup() { interactor = mockk() adapter = CollectionCreationTabListAdapter(interactor) + mozillaTab = mockk { + every { sessionId } returns "abc" + every { title } returns "Mozilla" + every { hostname } returns "mozilla.org" + every { url } returns "https://mozilla.org" + } every { interactor.selectCollection(any(), any()) } just Runs } @@ -52,15 +59,10 @@ class CollectionCreationTabListAdapterTest { @Test fun `creates and binds viewholder`() { - val tab = mockk { - every { sessionId } returns "abc" - every { title } returns "Mozilla" - every { hostname } returns "mozilla.org" - every { url } returns "https://mozilla.org" - } adapter.updateData( - tabs = listOf(tab), - selectedTabs = emptySet() + tabs = listOf(mozillaTab), + selectedTabs = emptySet(), + hideCheckboxes = false ) val holder = adapter.createViewHolder(FrameLayout(testContext), 0) @@ -70,6 +72,35 @@ class CollectionCreationTabListAdapterTest { assertEquals("mozilla.org", holder.hostname.text) assertFalse(holder.tab_selected_checkbox.isInvisible) assertTrue(holder.itemView.isClickable) + + every { interactor.addTabToSelection(mozillaTab) } just Runs + every { interactor.removeTabFromSelection(mozillaTab) } just Runs + assertFalse(holder.tab_selected_checkbox.isChecked) + + holder.tab_selected_checkbox.isChecked = true + verify { interactor.addTabToSelection(mozillaTab) } + + holder.tab_selected_checkbox.isChecked = false + verify { interactor.removeTabFromSelection(mozillaTab) } + } + + @Test + fun `creates and binds viewholder for selected tab`() { + every { interactor.addTabToSelection(mozillaTab) } just Runs + + adapter.updateData( + tabs = listOf(mozillaTab), + selectedTabs = setOf(mozillaTab), + hideCheckboxes = true + ) + + val holder = adapter.createViewHolder(FrameLayout(testContext), 0) + adapter.bindViewHolder(holder, 0) + + assertEquals("Mozilla", holder.tab_title.text) + assertEquals("mozilla.org", holder.hostname.text) + assertTrue(holder.tab_selected_checkbox.isInvisible) + assertFalse(holder.itemView.isClickable) } @Test