diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..00d5622 --- /dev/null +++ b/.github/workflows/build.yml @@ -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/wrapper-validation-action@v1.1.0 + + - 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 \ No newline at end of file diff --git a/app/src/main/java/com/example/composepaybutton/MainActivity.kt b/app/src/main/java/com/example/composepaybutton/MainActivity.kt index 354cb3d..e6faf1d 100644 --- a/app/src/main/java/com/example/composepaybutton/MainActivity.kt +++ b/app/src/main/java/com/example/composepaybutton/MainActivity.kt @@ -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") diff --git a/compose-pay-button/build.gradle b/compose-pay-button/build.gradle index 9e0a175..0295b3b 100644 --- a/compose-pay-button/build.gradle +++ b/compose-pay-button/build.gradle @@ -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") } \ No newline at end of file diff --git a/compose-pay-button/src/androidTest/java/com/google/pay/button/PayButtonTest.kt b/compose-pay-button/src/androidTest/java/com/google/pay/button/PayButtonTest.kt new file mode 100644 index 0000000..fa0a1e5 --- /dev/null +++ b/compose-pay-button/src/androidTest/java/com/google/pay/button/PayButtonTest.kt @@ -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() + + @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() + } + +} \ No newline at end of file