From 317b09183b20c4a7cd75e673af4e11ea2e2e7a63 Mon Sep 17 00:00:00 2001 From: Dominik Mengelt Date: Tue, 12 Dec 2023 17:07:41 +0100 Subject: [PATCH 1/4] added GH action workflow to run unit tests --- .github/workflows/test.yml | 29 +++++++ .../example/composepaybutton/MainActivity.kt | 2 +- compose-pay-button/build.gradle | 6 ++ .../com/google/pay/button/PayButtonTest.kt | 83 +++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yml create mode 100644 compose-pay-button/src/androidTest/java/com/google/pay/button/PayButtonTest.kt diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..c706920 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,29 @@ +name: Run unit tests + +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 tests + run: ./gradlew -p compose-pay-button connectedAndroidTest \ 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 From 3227faab57150f183624ec03c4079d117794f6bc Mon Sep 17 00:00:00 2001 From: Dominik Mengelt Date: Tue, 12 Dec 2023 17:14:33 +0100 Subject: [PATCH 2/4] switched to ReactiveCircus/android-emulator-runner to run the instrumented tests --- .github/workflows/test.yml | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c706920..e2a4251 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,20 +10,13 @@ on: jobs: test: - runs-on: ubuntu-latest - + runs-on: macos-latest steps: - - name: Checkout Repo + - name: checkout 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 + - name: run tests + uses: reactivecircus/android-emulator-runner@v2 with: - java-version: '17' - distribution: 'temurin' - - - name: Run tests - run: ./gradlew -p compose-pay-button connectedAndroidTest \ No newline at end of file + api-level: 29 + script: ./gradlew connectedCheck \ No newline at end of file From 8f1434a5f8bc580dd8ea6b3dcda872f3335180f5 Mon Sep 17 00:00:00 2001 From: Dominik Mengelt Date: Wed, 13 Dec 2023 16:30:48 +0100 Subject: [PATCH 3/4] using ./gradlew build instead of ./gradlew connectedCheck for now --- .github/workflows/build.yml | 29 +++++++++++++++++++++++++++++ .github/workflows/test.yml | 22 ---------------------- 2 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..f8deda9 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,29 @@ +name: Run unit tests + +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 tests + run: ./gradlew -p compose-pay-button build # use connectedCheck in the future \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index e2a4251..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Run unit tests - -on: - push: - branches: - - '*' - pull_request: - branches: - - main - -jobs: - test: - runs-on: macos-latest - steps: - - name: checkout - uses: actions/checkout@v3 - - - name: run tests - uses: reactivecircus/android-emulator-runner@v2 - with: - api-level: 29 - script: ./gradlew connectedCheck \ No newline at end of file From 9ba04384915bc26f83238ae90edfa0fd24d7f291 Mon Sep 17 00:00:00 2001 From: Dominik Mengelt Date: Wed, 13 Dec 2023 16:51:11 +0100 Subject: [PATCH 4/4] updated build name --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f8deda9..00d5622 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: Run unit tests +name: Run build on: push: @@ -25,5 +25,5 @@ jobs: java-version: '17' distribution: 'temurin' - - name: Run tests + - name: Run build run: ./gradlew -p compose-pay-button build # use connectedCheck in the future \ No newline at end of file