Skip to content

Commit 366a280

Browse files
committed
Create a simple UI test that waits for the in-app browser to be opened
1 parent a52d80c commit 366a280

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ dependencies {
6969
implementation(libs.androidx.ui.tooling.preview)
7070
implementation(libs.androidx.material3)
7171
implementation(project(":OpacityCore"))
72+
implementation(libs.androidx.espresso.intents)
7273
testImplementation(libs.junit)
7374
androidTestImplementation(libs.androidx.junit)
7475
androidTestImplementation(libs.androidx.espresso.core)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.opacitylabs.opacitycoreexample
2+
3+
import androidx.compose.ui.test.assertIsDisplayed
4+
import androidx.compose.ui.test.junit4.createAndroidComposeRule
5+
import androidx.compose.ui.test.onNodeWithText
6+
import androidx.compose.ui.test.performClick
7+
import androidx.test.espresso.intent.Intents.intended
8+
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
9+
import androidx.test.ext.junit.runners.AndroidJUnit4
10+
import org.hamcrest.Matchers.allOf
11+
import org.junit.After
12+
import org.junit.Before
13+
import org.junit.Rule
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
17+
@RunWith(AndroidJUnit4::class)
18+
class BaseUITest {
19+
@get:Rule
20+
val composeTestRule = createAndroidComposeRule<MainActivity>()
21+
22+
@Before
23+
fun setUp() {
24+
androidx.test.espresso.intent.Intents.init()
25+
}
26+
27+
@After
28+
fun tearDown() {
29+
androidx.test.espresso.intent.Intents.release()
30+
}
31+
32+
@Test
33+
fun testButtonClick() {
34+
// Check if button is displayed
35+
composeTestRule.onNodeWithText("Uber Rider Profile").assertIsDisplayed()
36+
37+
// Perform click
38+
composeTestRule.onNodeWithText("Uber Rider Profile").performClick()
39+
40+
Thread.sleep(2000)
41+
42+
// Check that the intent to open the browser was fired
43+
intended(
44+
allOf(
45+
hasComponent("com.opacitylabs.opacitycore.InAppBrowserActivity"),
46+
// hasExtra("url", "https://uber.opacitylabs.com/rider")
47+
)
48+
)
49+
}
50+
}

app/src/main/java/com/opacitylabs/opacitycoreexample/MainActivity.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ class MainActivity : ComponentActivity() {
5252
Column(modifier = Modifier.padding(innerPadding)) {
5353
val flowInput = remember { mutableStateOf("") }
5454

55+
Button(
56+
onClick = {
57+
lifecycleScope.launch {
58+
try {
59+
val res = OpacityCore.get("uber_rider:profile", null)
60+
Log.d("MainActivity", res["json"].toString())
61+
} catch (e: Exception) {
62+
Log.e("MainActivity", e.toString())
63+
}
64+
}
65+
},
66+
) { Text(text = "Uber Rider Profile") }
67+
5568
TextField(
5669
value = flowInput.value,
5770
onValueChange = { flowInput.value = it },
@@ -91,7 +104,12 @@ class MainActivity : ComponentActivity() {
91104
val opacityApiKey = dotenv["OPACITY_API_KEY"]
92105
requireNotNull(opacityApiKey) { "Opacity API key is null" }
93106

94-
OpacityCore.initialize(opacityApiKey, false, OpacityCore.Environment.PRODUCTION, true)
107+
OpacityCore.initialize(
108+
opacityApiKey,
109+
false,
110+
OpacityCore.Environment.PRODUCTION,
111+
true
112+
)
95113
Log.d("MainActivity", "Opacity SDK re-initialized")
96114
} catch (e: Exception) {
97115
Log.e("MainActivity", e.toString())

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ appcompat = "1.7.0"
1313
material = "1.12.0"
1414
browser = "1.8.0"
1515
securityCrypto = "1.1.0-alpha06"
16+
espressoIntents = "3.6.1"
1617

1718
[libraries]
1819
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -35,6 +36,7 @@ androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version
3536
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
3637
androidx-browser = { group = "androidx.browser", name = "browser", version.ref = "browser" }
3738
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version = "1.6.0" }
39+
androidx-espresso-intents = { group = "androidx.test.espresso", name = "espresso-intents", version.ref = "espressoIntents" }
3840

3941
[plugins]
4042
android-application = { id = "com.android.application", version.ref = "agp" }

0 commit comments

Comments
 (0)