Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added GH action workflow to run unit tests #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Run build

on:
push:
branches:
- '*'
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Gradle Wrapper Validation
uses: gradle/[email protected]

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Run build
run: ./gradlew -p compose-pay-button build # use connectedCheck in the future
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class MainActivity : ComponentActivity() {
Text("default")

// Default
PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods)
PayButton(onClick = onClick, allowedPaymentMethods = "")

Divider(thickness = 1.dp, color = Color.LightGray)
Text("customized look")
Expand Down
6 changes: 6 additions & 0 deletions compose-pay-button/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ dependencies {
implementation "androidx.compose.ui:ui:$compose_ui_version"
implementation 'androidx.compose.material:material:1.5.4'
implementation 'androidx.core:core-ktx:1.12.0'

androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_ui_version")
androidTestImplementation('androidx.test.espresso:espresso-core:3.5.1')
androidTestImplementation('androidx.test:runner:1.5.2')
androidTestImplementation('androidx.test:rules:1.5.0')
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_ui_version")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.google.pay.button

import androidx.activity.ComponentActivity
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.test.espresso.Espresso
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

const val GOOGLE_PAY_BUTTON_TEST_TAG = "google-pay-button"

@RunWith(AndroidJUnit4::class)
class GooglePayButtonTest {

private var allowedPaymentMethods = "";

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Before
fun setup() {
allowedPaymentMethods = """
[
{
"type": "CARD",
"parameters": {
"allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
"allowedCardNetworks": ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"]
},
"tokenizationSpecification": {
"type": "PAYMENT_GATEWAY",
"parameters": {
"gateway": "example",
"gatewayMerchantId": "exampleGatewayMerchantId"
}
}
}
]
""".trimIndent()
}

@Test
fun testButtonNotDisplayedForEmptyAllowedPaymentMethods() {
composeTestRule.setContent {
PayButton(
onClick = {},
allowedPaymentMethods = "",
modifier = Modifier.testTag(GOOGLE_PAY_BUTTON_TEST_TAG))
}
composeTestRule.onNodeWithTag(GOOGLE_PAY_BUTTON_TEST_TAG).assertIsNotDisplayed()
}

@Test
fun testButtonDisplayedAndEnabled() {
composeTestRule.setContent {
PayButton(
onClick = { println("Button clicked") },
allowedPaymentMethods = allowedPaymentMethods,
type = ButtonType.Book,
enabled = true,
modifier = Modifier.testTag(GOOGLE_PAY_BUTTON_TEST_TAG))
}

composeTestRule.onNodeWithTag(GOOGLE_PAY_BUTTON_TEST_TAG).assertIsDisplayed()
Espresso.onView(withText("Book with ")).check(matches(isDisplayed()))

// TODO - the following assertions do not work for some reason
//composeTestRule.onNodeWithTag(GOOGLE_PAY_BUTTON_TEST_TAG).assertHasClickAction()
// if we would set enabled = false
//composeTestRule.onNodeWithTag(GOOGLE_PAY_BUTTON_TEST_TAG).assertIsNotEnabled()
}

}