Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
fix: adjust crash when passing color in page indicator (#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
uziasferreirazup authored Mar 23, 2021
1 parent d636aeb commit 5ba8d83
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package br.com.zup.beagle.android.components.page

import android.graphics.Color
import br.com.zup.beagle.android.context.Bind
import br.com.zup.beagle.android.context.valueOf
import br.com.zup.beagle.android.utils.observeBindChanges
import br.com.zup.beagle.android.utils.toAndroidColor
import br.com.zup.beagle.android.view.ViewFactory
import br.com.zup.beagle.android.view.custom.BeaglePageIndicatorView
import br.com.zup.beagle.android.widget.RootView
Expand Down Expand Up @@ -57,8 +57,13 @@ class PageIndicator(

override fun buildView(rootView: RootView) = viewFactory.makePageIndicator(rootView.getContext()).apply {
pageIndicator = this
setSelectedColor(Color.parseColor(selectedColor))
setUnselectedColor(Color.parseColor(unselectedColor))
selectedColor.toAndroidColor()?.let {
setSelectedColor(it)
}
unselectedColor.toAndroidColor()?.let {
setUnselectedColor(it)
}

numberOfPages?.let {
setCount(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import br.com.zup.beagle.android.components.BaseComponentTest
import br.com.zup.beagle.android.context.Bind
import br.com.zup.beagle.android.extensions.once
import br.com.zup.beagle.android.testutil.RandomData
import br.com.zup.beagle.android.utils.ColorUtils
import br.com.zup.beagle.android.utils.Observer
import br.com.zup.beagle.android.utils.observeBindChanges
import br.com.zup.beagle.android.view.ViewFactory
Expand All @@ -29,13 +30,20 @@ import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.slot
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested

private const val SELECTED_COLOR = "#000000"
private const val UNSELECTED_COLOR = "#FF0000"

@DisplayName("Given Page Indicator")
class PageIndicatorTest : BaseComponentTest() {

private val beaglePageIndicatorView: BeaglePageIndicatorView = mockk(relaxed = true, relaxUnitFun = true)
Expand All @@ -49,10 +57,16 @@ class PageIndicatorTest : BaseComponentTest() {
override fun setUp() {
super.setUp()

pageIndicator = PageIndicator(RandomData.string(), RandomData.string(), numberOfPages, currentPage)
pageIndicator = PageIndicator(
selectedColor = SELECTED_COLOR,
unselectedColor = UNSELECTED_COLOR,
numberOfPages = numberOfPages,
currentPage = currentPage,
)

mockkStatic(Color::class)
mockkStatic("br.com.zup.beagle.android.utils.WidgetExtensionsKt")
mockkObject(ColorUtils::class)
every { Color.parseColor(any()) } returns 0
every { anyConstructed<ViewFactory>().makePageIndicator(any()) } returns beaglePageIndicatorView
every {
Expand All @@ -65,62 +79,104 @@ class PageIndicatorTest : BaseComponentTest() {
} just Runs
}

@Test
fun buildView_should_return_BeaglePageIndicatorView_and_set_colors() {
val view = pageIndicator.buildView(rootView)
@DisplayName("When build view")
@Nested
inner class ColorTest {

@DisplayName("Then should call correct color")
@Test
fun testCorrectColorCalled() {
// When
val view = pageIndicator.buildView(rootView)

// Then
assertEquals(beaglePageIndicatorView, view)
verify(exactly = once()) {
ColorUtils.hexColor(SELECTED_COLOR)
ColorUtils.hexColor(UNSELECTED_COLOR)
beaglePageIndicatorView.setSelectedColor(0)
beaglePageIndicatorView.setUnselectedColor(0)
}
}

assertEquals(beaglePageIndicatorView, view)
verify(exactly = once()) { beaglePageIndicatorView.setSelectedColor(0) }
verify(exactly = once()) { beaglePageIndicatorView.setUnselectedColor(0) }
}

@Test
fun buildView_should_call_onItemUpdate_when_currentPage_change() {
//GIVEN
val newPosition = RandomData.int()

//WHEN
pageIndicator.buildView(rootView = rootView)
currentPageSlot.captured.invoke(newPosition)
@DisplayName("When current page change")
@Nested
inner class OnItemUpdatedTest {

@DisplayName("Then should call on item update")
@Test
fun testCallItemUpdate() {
// Given
val newPosition = RandomData.int()

// When
pageIndicator.buildView(rootView = rootView)
currentPageSlot.captured.invoke(newPosition)

// Then
verify(exactly = once()) { pageIndicator.onItemUpdated(newPosition) }
}

//THEN
verify(exactly = once()) { pageIndicator.onItemUpdated(newPosition) }
}

@Test
fun buildView_should_call_setCount_when_numberOfPage_is_not_null() {
//GIVEN

//WHEN
pageIndicator.buildView(rootView = rootView)
@DisplayName("When numbers of page is not null")
@Nested
inner class CountTest {

@DisplayName("Then should call set count")
@Test
fun testCallSetCount() {
// When
pageIndicator.buildView(rootView = rootView)

// Then
verify(exactly = once()) { pageIndicator.setCount(numberOfPages) }
}

//THEN
verify(exactly = once()) { pageIndicator.setCount(numberOfPages) }
}

@Test
fun setCount_should_call_BeaglePageIndicatorView_setCount() {
// Given
val count = RandomData.int()

// When
pageIndicator.buildView(rootView)
pageIndicator.setCount(count)
@DisplayName("When set count")
@Nested
inner class CountCallTest {

@DisplayName("Then should call set count from beagle page view")
@Test
fun testCallSetCount() {
// Given
val count = RandomData.int()

// When
pageIndicator.buildView(rootView)
pageIndicator.setCount(count)

// Then
verify(exactly = once()) { beaglePageIndicatorView.setCount(count) }
}

// Then
verify(exactly = once()) { beaglePageIndicatorView.setCount(count) }
}

@Test
fun onItemUpdated_should_call_BeaglePageIndicatorView_onItemUpdated() {
// Given
val count = RandomData.int()
@DisplayName("When call item updated")
@Nested
inner class ItemUpdatedTest {

@DisplayName("Then should set current index")
@Test
fun testSetCurrentIndex() {
// Given
val count = RandomData.int()

// When
pageIndicator.buildView(rootView)
pageIndicator.onItemUpdated(count)

// When
pageIndicator.buildView(rootView)
pageIndicator.onItemUpdated(count)
// Then
verify(exactly = once()) { beaglePageIndicatorView.setCurrentIndex(count) }
}

// Then
verify(exactly = once()) { beaglePageIndicatorView.setCurrentIndex(count) }
}
}

0 comments on commit 5ba8d83

Please sign in to comment.