Skip to content

Commit

Permalink
Merge pull request #26 from tinkoff-mobile-tech/MC-6744
Browse files Browse the repository at this point in the history
MС-6744 improve test coverage
  • Loading branch information
mrkiriss authored May 23, 2023
2 parents 8b96c72 + 61673ec commit 57c806d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package ru.tinkoff.core.app_demo_partner

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withChild
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnitRunner
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import org.junit.Assert.assertTrue
import org.hamcrest.CoreMatchers.allOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -22,7 +28,7 @@ class TinkoffIdSignInButtonTest : AndroidJUnitRunner() {
@get:Rule
val activityRule = ActivityTestRule(PartnerActivity::class.java, false, true)

lateinit var standardButton: TinkoffIdSignInButton
private lateinit var standardButton: TinkoffIdSignInButton

@Before
fun setUp() {
Expand All @@ -32,67 +38,61 @@ class TinkoffIdSignInButtonTest : AndroidJUnitRunner() {
}

@Test
fun testStandardButtonTextPresence() {
checkPresenceTextOnScreen(createFinalTitle(originalTitlePart))
}

@Test
fun testStandardButtonTextUpdate() {
fun testTitleChanging() {
runOnMainThread {
standardButton.title = SOME_TEXT
}

checkPresenceTextOnScreen(createFinalTitle(SOME_TEXT))
}

@Test
fun testStandardButtonTextAbsence() {
runOnMainThread {
standardButton.title = null
}

checkPresenceTextOnScreen(permanentTitlePart)
}

@Test
fun testStandardButtonBadgePresence() {
checkPresenceTextOnScreen(originalBadge)
checkTextOnView(createFinalTitle(SOME_TEXT))
}

@Test
fun testStandardButtonBadgeUpdate() {
fun testBadgeTextChanging() {
runOnMainThread {
standardButton.badgeText = SOME_TEXT
}

checkPresenceTextOnScreen(SOME_TEXT)
checkTextOnView(SOME_TEXT)
}

@Test
fun testStandardButtonBadgeAbsence() {
runOnMainThread {
standardButton.badgeText = null
}

checkAbsenceTextOnScreen(originalBadge)
}

@Test
fun testStandardButtonAbsenceElements() {
fun testTextElementsHidingInCompactMode() {
runOnMainThread {
standardButton.title = SOME_TEXT
standardButton.badgeText = SOME_TEXT
standardButton.isCompact = true
}

checkAbsenceTextOnScreen(createFinalTitle(originalTitlePart))
checkAbsenceTextOnScreen(originalBadge)
onView(
allOf(
withId(standardButtonId),
// title element
withChild(
allOf(
withText(createFinalTitle(SOME_TEXT)),
withEffectiveVisibility(ViewMatchers.Visibility.GONE)
)
),
// badge element
withChild(
allOf(
withText(SOME_TEXT),
withEffectiveVisibility(ViewMatchers.Visibility.GONE)
)
),
)
).check(matches(isDisplayed()))
}

private fun checkPresenceTextOnScreen(text: String) {
assertTrue("Not found text \"$text\" on screen", UiDevice.getInstance(getInstrumentation()).hasObject(By.textContains(text)))
}
private fun checkAbsenceTextOnScreen(text: String) {
assertTrue("Found text \"$text\" on screen", !UiDevice.getInstance(getInstrumentation()).hasObject(By.textContains(text)))
private fun checkTextOnView(text: String) {
onView(
allOf(
withId(standardButtonId),
withChild(withText(text))
)
).check(matches(isDisplayed()))
}

private fun createFinalTitle(titlePart: String) = "$titlePart $permanentTitlePart"
private fun runOnMainThread(block: () -> Unit) {
activityRule.runOnUiThread(block)
Expand All @@ -101,10 +101,8 @@ class TinkoffIdSignInButtonTest : AndroidJUnitRunner() {
private companion object {
private const val SOME_TEXT = "test"

private const val standardButtonId = R.id.standardButtonTinkoffAuth
private const val standardButtonId = R.id.standardSmallBlackButtonTinkoffAuth

private val originalTitlePart = getInstrumentation().targetContext.resources.getString(R.string.partner_auth_sign_in_button_title)
private val permanentTitlePart = getInstrumentation().targetContext.resources.getString(ru.tinkoff.core.tinkoffId.R.string.tinkoff_id_tinkoff_text)
private val originalBadge = getInstrumentation().targetContext.resources.getString(R.string.partner_auth_sign_in_button_badge)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ru.tinkoff.core.tinkoffId

import android.content.Context
import android.os.Build
import android.text.SpannedString
import androidx.core.content.res.ResourcesCompat
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import ru.tinkoff.core.tinkoffId.ui.TinkoffDimen
import ru.tinkoff.core.tinkoffId.ui.TinkoffIdSignInButton

/**
* @author k.voskrebentsev
*/
@Config(sdk = [Build.VERSION_CODES.P])
@RunWith(RobolectricTestRunner::class)
internal class TinkoffIdSignInButtonTest {

private lateinit var button: TinkoffIdSignInButton

@Before
fun setUp() {
button = TinkoffIdSignInButton(context = context)
}

@Test
fun testSetEmptyTitle() {
button.title = ""

val result = button.title as SpannedString

assertEquals(PERMANENT_TITLE_PART, result.toString())
}

@Test
fun testSetTitle() {
button.title = CUSTOM_TITLE_PART

val result = button.title as SpannedString

assertEquals("$CUSTOM_TITLE_PART $PERMANENT_TITLE_PART", result.toString())
}

@Test
fun testDefaultBadgeText() {
assertEquals("", button.badgeText)
}

@Test
fun testDefaultCompact() {
assertEquals(false, button.isCompact)
}

@Test
fun testDefaultStyle() {
assertEquals(TinkoffIdSignInButton.ButtonStyle.YELLOW, button.style)
}

@Test
fun testDefaultCornerSize() {
assertEquals(context.resources.getDimension(TinkoffDimen.tinkoff_id_default_corner_radius).toInt(), button.cornerRadius)
}

@Test
fun testDefaultFont() {
assertEquals(ResourcesCompat.getFont(context, R.font.neue_haas_unica_w1g), button.textFont)
}

companion object {
val context: Context = ApplicationProvider.getApplicationContext()

const val CUSTOM_TITLE_PART = "title"
val PERMANENT_TITLE_PART = context.getString(R.string.tinkoff_id_tinkoff_text)
}
}

0 comments on commit 57c806d

Please sign in to comment.