-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compose): add UI tests and move common plugins to gradle/shared/…
…code-quality.gradle
- Loading branch information
Showing
12 changed files
with
250 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,6 @@ public void onLoaded() { | |
latch.countDown(); | ||
} | ||
}, | ||
new TestHCaptchaStateListener(), | ||
webView | ||
); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<application> | ||
<activity android:name="androidx.activity.ComponentActivity" /> | ||
</application> | ||
</manifest> |
24 changes: 0 additions & 24 deletions
24
compose/src/androidTest/java/com/hcaptcha/sdk/compose/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
compose/src/androidTest/java/com/hcaptcha/sdk/compose/HCaptchaComposeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.hcaptcha.sdk.compose | ||
|
||
import androidx.compose.ui.test.* | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.semantics.contentDescription | ||
import androidx.compose.ui.semantics.semantics | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.hcaptcha.sdk.HCaptchaCompose | ||
import com.hcaptcha.sdk.HCaptchaConfig | ||
import com.hcaptcha.sdk.HCaptchaError | ||
import com.hcaptcha.sdk.HCaptchaResponse | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.util.concurrent.TimeUnit | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class HCaptchaComposeTest { | ||
private val resultContentDescription = "HCaptchaResultString" | ||
private val timeout = TimeUnit.SECONDS.toMillis(4) | ||
|
||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
fun setContent(token: String = "10000000-ffff-ffff-ffff-000000000001") { | ||
composeTestRule.setContent { | ||
var text by remember { mutableStateOf("<init>") } | ||
Column { | ||
Text(text = text, modifier = Modifier.semantics { contentDescription = resultContentDescription }) | ||
|
||
HCaptchaCompose(HCaptchaConfig | ||
.builder() | ||
.siteKey(token) | ||
.build()) { result -> | ||
when (result) { | ||
is HCaptchaResponse.Success -> { | ||
text = result.token | ||
} | ||
is HCaptchaResponse.Failure -> { | ||
text = result.error.name | ||
} | ||
else -> {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun validToken() { | ||
setContent() | ||
|
||
runBlocking { delay(timeout) } | ||
|
||
composeTestRule.onNodeWithContentDescription(resultContentDescription) | ||
.assertTextEquals("10000000-aaaa-bbbb-cccc-000000000001") | ||
} | ||
|
||
@Test | ||
fun invalidToken() { | ||
setContent("bad-baad-token") | ||
|
||
runBlocking { delay(timeout) } | ||
|
||
composeTestRule.onNodeWithContentDescription(resultContentDescription) | ||
.assertTextEquals(HCaptchaError.INVALID_DATA.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
checkstyle { | ||
toolVersion = '8.45.1' | ||
} | ||
|
||
task checkstyle(type: Checkstyle) { | ||
description 'Check code standard' | ||
group 'verification' | ||
configFile file("${rootDir}/gradle/config/checkstyle.xml") | ||
source 'src' | ||
include '**/*.java' | ||
exclude '**/gen/**' | ||
classpath = files() | ||
ignoreFailures = false | ||
maxWarnings = 0 | ||
} | ||
|
||
pmd { | ||
consoleOutput = true | ||
toolVersion = "6.51.0" | ||
} | ||
|
||
task pmd(type: Pmd) { | ||
ruleSetFiles = files("${project.rootDir}/gradle/config/pmd.xml") | ||
ignoreFailures = false | ||
ruleSets = [] | ||
source 'src' | ||
include '**/*.java' | ||
exclude '**/gen/**' | ||
reports { | ||
xml.required = false | ||
xml.outputLocation = file("${project.buildDir}/reports/pmd/pmd.xml") | ||
html.required = true | ||
html.outputLocation = file("$project.buildDir/outputs/pmd/pmd.html") | ||
} | ||
} | ||
|
||
spotbugs { | ||
ignoreFailures = false | ||
showStackTraces = true | ||
showProgress = false | ||
reportLevel = 'high' | ||
excludeFilter = file("${project.rootDir}/gradle/config/findbugs-exclude.xml") | ||
onlyAnalyze = ['com.hcaptcha.sdk.*'] | ||
projectName = name | ||
release = version | ||
} | ||
|
||
// enable html report | ||
gradle.taskGraph.beforeTask { task -> | ||
if (task.name.toLowerCase().contains('spotbugs')) { | ||
task.reports { | ||
html.enabled true | ||
xml.enabled true | ||
} | ||
} | ||
} | ||
|
||
// https://www.rallyhealth.com/coding/code-coverage-for-android-testing | ||
task jacocoUnitTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) { | ||
def coverageSourceDirs = [ | ||
"src/main/java" | ||
] | ||
def javaClasses = fileTree( | ||
dir: "${project.buildDir}/intermediates/javac/debug/classes", | ||
excludes: [ | ||
'**/R.class', | ||
'**/R$*.class', | ||
'**/BuildConfig.*', | ||
'**/Manifest*.*' | ||
] | ||
) | ||
|
||
classDirectories.from files([javaClasses]) | ||
additionalSourceDirs.from files(coverageSourceDirs) | ||
sourceDirectories.from files(coverageSourceDirs) | ||
executionData.from = "${project.buildDir}/jacoco/testDebugUnitTest.exec" | ||
|
||
reports { | ||
xml.required = true | ||
html.required = true | ||
} | ||
} | ||
|
||
check.dependsOn('checkstyle', 'pmd', 'jacocoUnitTestReport') | ||
|
||
sonarqube { | ||
properties { | ||
property "sonar.projectKey", "hCaptcha_hcaptcha-android-sdk" | ||
property "sonar.organization", "hcaptcha" | ||
property "sonar.host.url", "https://sonarcloud.io" | ||
|
||
property "sonar.language", "java" | ||
property "sonar.sourceEncoding", "utf-8" | ||
|
||
property "sonar.sources", "src/main" | ||
property "sonar.java.binaries", "${project.buildDir}/intermediates/javac/debug/classes" | ||
property "sonar.tests", ["src/test/", "../test/src/androidTest/"] | ||
|
||
property "sonar.android.lint.report", "${project.buildDir}/outputs/lint-results.xml" | ||
property "sonar.java.spotbugs.reportPaths", ["${project.buildDir}/reports/spotbugs/debug.xml", "${project.buildDir}/reports/spotbugs/release.xml"] | ||
property "sonar.java.pmd.reportPaths", "${project.buildDir}/reports/pmd/pmd.xml" | ||
property "sonar.java.checkstyle.reportPaths", "${project.buildDir}/reports/checkstyle/checkstyle.xml" | ||
property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/jacoco/jacocoUnitTestReport.xml" | ||
} | ||
} | ||
|
||
project.tasks["sonarqube"].dependsOn "check" | ||
|
Oops, something went wrong.