-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathEmbeddedMessageIntegrationTest.kt
More file actions
277 lines (230 loc) · 12.3 KB
/
EmbeddedMessageIntegrationTest.kt
File metadata and controls
277 lines (230 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.iterable.integration.tests
import android.content.Intent
import android.util.Log
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
import androidx.test.runner.lifecycle.Stage
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.By
import com.iterable.iterableapi.IterableApi
import com.iterable.iterableapi.IterableEmbeddedMessage
import com.iterable.integration.tests.activities.EmbeddedMessageTestActivity
import com.iterable.iterableapi.ui.embedded.IterableEmbeddedView
import com.iterable.iterableapi.ui.embedded.IterableEmbeddedViewType
import org.awaitility.Awaitility
import org.json.JSONObject
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class EmbeddedMessageIntegrationTest : BaseIntegrationTest() {
companion object {
private const val TAG = "EmbeddedMessageIntegrationTest"
private const val TEST_PLACEMENT_ID = TestConstants.TEST_EMBEDDED_PLACEMENT_ID
}
private lateinit var uiDevice: UiDevice
private lateinit var mainActivityScenario: ActivityScenario<MainActivity>
@Before
override fun setUp() {
Log.d(TAG, "🔧 Test setup starting...")
uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
// Call super.setUp() to initialize SDK with BaseIntegrationTest's config
// This sets test mode flag and initializes SDK with test handlers (including urlHandler)
super.setUp()
Log.d(TAG, "🔧 Base setup complete, SDK initialized with test handlers")
// Disable in-app auto display and clear existing messages BEFORE launching app
// This prevents in-app messages from obscuring the embedded message test screen
Log.d(TAG, "🔧 Disabling in-app auto display and clearing existing messages...")
IterableApi.getInstance().inAppManager.setAutoDisplayPaused(true)
Log.d(TAG, "✅ In-app auto display paused")
// Clear all existing in-app messages
IterableApi.getInstance().inAppManager.messages.forEach {
Log.d(TAG, "Clearing existing in-app message: ${it.messageId}")
IterableApi.getInstance().inAppManager.removeMessage(it)
}
Log.d(TAG, "✅ All in-app messages cleared")
Log.d(TAG, "🔧 MainActivity will skip initialization due to test mode flag")
// Now launch the app flow with custom handlers already configured
launchAppAndNavigateToEmbeddedTesting()
}
@After
override fun tearDown() {
super.tearDown()
}
private fun launchAppAndNavigateToEmbeddedTesting() {
// Step 1: Launch MainActivity (the home page)
Log.d(TAG, "🔧 Step 1: Launching MainActivity...")
val mainIntent = Intent(InstrumentationRegistry.getInstrumentation().targetContext, MainActivity::class.java)
mainActivityScenario = ActivityScenario.launch(mainIntent)
// Wait for MainActivity to be ready
Awaitility.await()
.atMost(5, TimeUnit.SECONDS)
.pollInterval(500, TimeUnit.MILLISECONDS)
.until {
val state = mainActivityScenario.state
Log.d(TAG, "🔧 MainActivity state: $state")
state == Lifecycle.State.RESUMED
}
Log.d(TAG, "🔧 MainActivity is ready!")
// Step 2: Click the "Embedded Messages" button to navigate to EmbeddedMessageTestActivity
Log.d(TAG, "🔧 Step 2: Clicking 'Embedded Messages' button...")
val embeddedButton = uiDevice.findObject(UiSelector().resourceId("com.iterable.integration.tests:id/btnEmbeddedMessages"))
// RESUMED fires before view inflation; waitForExists handles the race.
if (!embeddedButton.waitForExists(5000)) {
Log.e(TAG, "❌ Embedded Messages button not found!")
Assert.fail("Embedded Messages button not found in MainActivity")
}
embeddedButton.click()
Log.d(TAG, "🔧 Clicked Embedded Messages button successfully")
// Step 3: Wait for EmbeddedMessageTestActivity to load
Log.d(TAG, "🔧 Step 3: Waiting for EmbeddedMessageTestActivity to load...")
Thread.sleep(2000) // Give time for navigation
Log.d(TAG, "🔧 App navigation complete: Now on EmbeddedMessageTestActivity!")
}
@Test
fun testEmbeddedMessageMVP() {
// Step 1: Ensure user is signed in
Log.d(TAG, "📧 Step 1: Ensuring user is signed in...")
val userSignedIn = testUtils.ensureUserSignedIn(testUserEmail)
Assert.assertTrue("User should be signed in", userSignedIn)
Log.d(TAG, "✅ User signed in successfully: $testUserEmail")
// Step 2: Preliminary check - verify view is ready with placement ID
Log.d(TAG, "🔍 Step 2: Checking view readiness with placement ID...")
var viewReady = false
InstrumentationRegistry.getInstrumentation().runOnMainSync {
val activity = ActivityLifecycleMonitorRegistry.getInstance()
.getActivitiesInStage(Stage.RESUMED)
.firstOrNull() as? EmbeddedMessageTestActivity
activity?.let {
val fragmentContainer = it.findViewById<androidx.fragment.app.FragmentContainerView>(R.id.embedded_message_container)
viewReady = fragmentContainer != null
if (viewReady) {
Log.d(TAG, "✅ View is ready with placementID - $TEST_PLACEMENT_ID")
}
}
}
Assert.assertTrue("FragmentContainerView should exist in EmbeddedMessageTestActivity", viewReady)
// Drive a clean standard→premium membership transition. Mirrors the iOS BCIT
// embedded test — the BCIT campaign's audience predicate is on
// `membershipLevel == "premium"`.
setMembershipLevel("standard")
syncMessagesAndWait()
Assert.assertFalse(
"User should not be eligible for placement $TEST_PLACEMENT_ID with membershipLevel=standard",
IterableApi.getInstance().embeddedManager.getPlacementIds().contains(TEST_PLACEMENT_ID)
)
setMembershipLevel("premium")
val placementIds = syncAndWaitForPlacement(TEST_PLACEMENT_ID, timeoutSeconds = 30)
Assert.assertTrue(
"Placement ID $TEST_PLACEMENT_ID should exist, but found: $placementIds",
placementIds.contains(TEST_PLACEMENT_ID)
)
// Step 8: Get messages for the placement ID
Log.d(TAG, "📨 Step 8: Getting messages for placement ID $TEST_PLACEMENT_ID...")
val messages = IterableApi.getInstance().embeddedManager.getMessages(TEST_PLACEMENT_ID)
Assert.assertTrue("Should have at least 1 message for placement $TEST_PLACEMENT_ID", messages!!.isNotEmpty())
val message = messages.first()
Log.d(TAG, "✅ Found message: ${message.metadata.messageId}")
// Step 9: Display message using IterableEmbeddedView
Log.d(TAG, "🎨 Step 9: Displaying message using IterableEmbeddedView...")
InstrumentationRegistry.getInstrumentation().runOnMainSync {
val activity = ActivityLifecycleMonitorRegistry.getInstance()
.getActivitiesInStage(Stage.RESUMED)
.firstOrNull() as? EmbeddedMessageTestActivity
if (activity != null) {
val fragment =
IterableEmbeddedView.newInstance(IterableEmbeddedViewType.BANNER, message, null)
activity.supportFragmentManager.beginTransaction()
.replace(R.id.embedded_message_container, fragment)
.commitNow()
Log.d(TAG, "✅ Fragment added to FragmentManager")
} else {
Assert.fail("EmbeddedMessageTestActivity not found in RESUMED stage")
}
}
// Wait for fragment to be displayed
Thread.sleep(1000)
// Step 10: Verify display - check fragment exists
Log.d(TAG, "✅ Step 10: Verifying embedded message is displayed...")
var isEmbeddedFragmentDisplayed = false
InstrumentationRegistry.getInstrumentation().runOnMainSync {
val activity = ActivityLifecycleMonitorRegistry.getInstance()
.getActivitiesInStage(Stage.RESUMED)
.firstOrNull() as? EmbeddedMessageTestActivity
activity?.let { act ->
val fragmentManager = act.supportFragmentManager
fragmentManager.fragments.forEach { fragment ->
if (fragment is IterableEmbeddedView) {
isEmbeddedFragmentDisplayed = true
Log.d(TAG, "✅ Found IterableEmbeddedView fragment")
}
}
}
}
Assert.assertTrue(
"IterableEmbeddedView fragment should be displayed",
isEmbeddedFragmentDisplayed
)
Log.d(TAG, "✅ Embedded message is displayed, now interacting with button...")
// Step 11: Interact with button - find and click first button
Log.d(TAG, "🎯 Step 11: Clicking button in the embedded message...")
// Try to find button by resource ID first
val button = uiDevice.findObject(UiSelector().resourceId("com.iterable.iterableapi.ui:id/embedded_message_first_button"))
if (button.exists()) {
button.click()
Log.d(TAG, "✅ Clicked embedded message button")
} else {
// Try to find by button text if available
val buttonText = message.elements?.buttons?.firstOrNull()?.title
if (buttonText != null) {
val buttonByText = uiDevice.findObject(By.text(buttonText))
if (buttonByText != null) {
buttonByText.click()
Log.d(TAG, "✅ Clicked embedded message button by text: $buttonText")
} else {
Assert.fail("Button not found in the embedded message (tried resource ID and text: $buttonText)")
}
} else {
Assert.fail("Button not found in the embedded message (tried resource ID, but no button text available)")
}
}
// Step 12: Verify URL handler was called
Log.d(TAG, "🎯 Step 12: Verifying URL handler was called after button click...")
val urlHandlerCalled = waitForUrlHandler(timeoutSeconds = 3)
Assert.assertTrue(
"URL handler should have been called after clicking the button",
urlHandlerCalled
)
// Step 13: Verify the correct URL was handled
val handledUrl = getLastHandledUrl()
Log.d(TAG, "🎯 URL handler received: $handledUrl")
Assert.assertNotNull("Handled URL should not be null", handledUrl)
Log.d(TAG, "✅ URL handler was called with URL: $handledUrl")
Log.d(TAG, "✅✅✅ Test completed successfully! All steps passed.")
}
private fun setMembershipLevel(level: String) {
IterableApi.getInstance().updateUser(JSONObject().put("membershipLevel", level))
Thread.sleep(3000)
}
private fun syncMessagesAndWait() {
IterableApi.getInstance().embeddedManager.syncMessages()
Thread.sleep(2000)
}
private fun syncAndWaitForPlacement(placementId: Long, timeoutSeconds: Long = 30): List<Long> {
val deadline = System.currentTimeMillis() + timeoutSeconds * 1000
var placementIds = IterableApi.getInstance().embeddedManager.getPlacementIds()
while (System.currentTimeMillis() < deadline && !placementIds.contains(placementId)) {
IterableApi.getInstance().embeddedManager.syncMessages()
Thread.sleep(2000)
placementIds = IterableApi.getInstance().embeddedManager.getPlacementIds()
}
return placementIds
}
}