From 0780d9cf0b8dfb6ac3f2a2b10931eae86b6ec486 Mon Sep 17 00:00:00 2001 From: Kirill Voskrebentsev Date: Tue, 23 May 2023 15:32:51 +0300 Subject: [PATCH 1/2] MC-6754 add button unit tests --- .../tinkoffId/TinkoffIdSignInButtonTest.kt | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tinkoff-id/src/test/java/ru/tinkoff/core/tinkoffId/TinkoffIdSignInButtonTest.kt diff --git a/tinkoff-id/src/test/java/ru/tinkoff/core/tinkoffId/TinkoffIdSignInButtonTest.kt b/tinkoff-id/src/test/java/ru/tinkoff/core/tinkoffId/TinkoffIdSignInButtonTest.kt new file mode 100644 index 0000000..eefe858 --- /dev/null +++ b/tinkoff-id/src/test/java/ru/tinkoff/core/tinkoffId/TinkoffIdSignInButtonTest.kt @@ -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) + } +} From 61673ec261864ba9382c0b531364438e1ca00797 Mon Sep 17 00:00:00 2001 From: Kirill Voskrebentsev Date: Tue, 23 May 2023 15:33:13 +0300 Subject: [PATCH 2/2] MC-6754 refactor button ui tests --- .../TinkoffIdSignInButtonTest.kt | 92 +++++++++---------- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/app-demo/src/androidTest/java/ru/tinkoff/core/app_demo_partner/TinkoffIdSignInButtonTest.kt b/app-demo/src/androidTest/java/ru/tinkoff/core/app_demo_partner/TinkoffIdSignInButtonTest.kt index 07d1702..f4afd16 100644 --- a/app-demo/src/androidTest/java/ru/tinkoff/core/app_demo_partner/TinkoffIdSignInButtonTest.kt +++ b/app-demo/src/androidTest/java/ru/tinkoff/core/app_demo_partner/TinkoffIdSignInButtonTest.kt @@ -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 @@ -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() { @@ -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) @@ -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) } }