@@ -21,47 +21,47 @@ import java.util.concurrent.atomic.AtomicReference
2121
2222@RunWith(AndroidJUnit4 ::class )
2323abstract class BaseIntegrationTest {
24-
24+
2525 companion object {
2626 const val TIMEOUT_SECONDS = TestConstants .TIMEOUT_SECONDS
2727 const val POLL_INTERVAL_SECONDS = TestConstants .POLL_INTERVAL_SECONDS
2828 }
29-
29+
3030 protected lateinit var context: Context
3131 protected lateinit var testUtils: IntegrationTestUtils
32-
32+
3333 // URL handler tracking for tests
3434 private val urlHandlerCalled = AtomicBoolean (false )
3535 private val lastHandledUrl = AtomicReference <String ?>(null )
36-
36+
3737 @Before
3838 open fun setUp () {
3939 context = ApplicationProvider .getApplicationContext()
4040 testUtils = IntegrationTestUtils (context)
41-
41+
4242 // Reset tracking flags
4343 resetUrlHandlerTracking()
44-
44+
4545 // Set test mode flag to prevent MainActivity from initializing SDK
4646 // This ensures our test config (with test handlers) is the one used
4747 System .setProperty(" iterable.test.mode" , " true" )
48-
48+
4949 // Initialize Iterable SDK for testing
5050 initializeIterableSDK()
51-
51+
5252 // Setup test environment
5353 setupTestEnvironment()
5454 }
55-
55+
5656 @After
5757 open fun tearDown () {
5858 // Cleanup test environment
5959 cleanupTestEnvironment()
60-
60+
6161 // Clear test mode flag
6262 System .clearProperty(" iterable.test.mode" )
6363 }
64-
64+
6565 private fun initializeIterableSDK () {
6666 val config = IterableConfig .Builder ()
6767 .setAutoPushRegistration(true )
@@ -87,29 +87,29 @@ abstract class BaseIntegrationTest {
8787 true
8888 }
8989 .build()
90-
90+
9191 IterableApi .initialize(context, BuildConfig .ITERABLE_API_KEY , config)
92-
92+
9393 // Set the user email for integration testing
9494 val userEmail = TestConstants .TEST_USER_EMAIL
9595 IterableApi .getInstance().setEmail(userEmail)
9696 Log .d(" BaseIntegrationTest" , " User email set to: $userEmail " )
9797 Log .d(" BaseIntegrationTest" , " Iterable SDK initialized with email: $userEmail " )
9898 }
99-
99+
100100 private fun setupTestEnvironment () {
101101 // Grant notification permissions
102102 grantNotificationPermissions()
103-
103+
104104 // Setup test data
105105 setupTestData()
106106 }
107-
107+
108108 private fun cleanupTestEnvironment () {
109109 // Clear any test data
110110 clearTestData()
111111 }
112-
112+
113113 private fun grantNotificationPermissions () {
114114 // Grant notification permissions for Android 13+
115115 if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .TIRAMISU ) {
@@ -119,19 +119,22 @@ abstract class BaseIntegrationTest {
119119 )
120120 }
121121 }
122-
122+
123123 private fun setupTestData () {
124124 // Setup any test-specific data
125125 }
126-
126+
127127 private fun clearTestData () {
128128 // Clear any test-specific data
129129 }
130-
130+
131131 /* *
132132 * Wait for a condition to be true with timeout
133133 */
134- protected fun waitForCondition (condition : () -> Boolean , timeoutSeconds : Long = TIMEOUT_SECONDS ): Boolean {
134+ protected fun waitForCondition (
135+ condition : () -> Boolean ,
136+ timeoutSeconds : Long = TIMEOUT_SECONDS
137+ ): Boolean {
135138 return try {
136139 Awaitility .await()
137140 .atMost(timeoutSeconds, TimeUnit .SECONDS )
@@ -142,105 +145,36 @@ abstract class BaseIntegrationTest {
142145 false
143146 }
144147 }
145-
146- /* *
147- * Wait for an in-app message to be displayed
148- */
149- protected fun waitForInAppMessage (timeoutSeconds : Long = TIMEOUT_SECONDS ): Boolean {
150- return waitForCondition({
151- testUtils.hasInAppMessageDisplayed()
152- }, timeoutSeconds)
153- }
154-
148+
149+
155150 /* *
156151 * Trigger a campaign via Iterable API
157152 */
158- protected fun triggerCampaignViaAPI (campaignId : Int , recipientEmail : String = TestConstants .TEST_USER_EMAIL , dataFields : Map <String , Any >? = null, callback : ((Boolean ) -> Unit )? = null) {
153+ protected fun triggerCampaignViaAPI (
154+ campaignId : Int ,
155+ recipientEmail : String = TestConstants .TEST_USER_EMAIL ,
156+ dataFields : Map <String , Any >? = null,
157+ callback : ((Boolean ) -> Unit )? = null
158+ ) {
159159 testUtils.triggerCampaignViaAPI(campaignId, recipientEmail, dataFields, callback)
160160 }
161-
162- /* *
163- * Trigger a push campaign via Iterable API
164- */
165- protected fun triggerPushCampaignViaAPI (campaignId : Int , recipientEmail : String = TestConstants .TEST_USER_EMAIL , dataFields : Map <String , Any >? = null, callback : ((Boolean ) -> Unit )? = null) {
166- testUtils.triggerPushCampaignViaAPI(campaignId, recipientEmail, dataFields, callback)
167- }
168-
169- /* *
170- * Wait for a push notification to be received
171- */
172- protected fun waitForPushNotification (timeoutSeconds : Long = TIMEOUT_SECONDS ): Boolean {
173- return waitForCondition({
174- testUtils.hasReceivedPushNotification()
175- }, timeoutSeconds)
176- }
177-
178- /* *
179- * Send a test push notification
180- */
181- protected fun sendTestPushNotification (campaignId : String = "test_campaign"): Boolean {
182- return testUtils.sendPushNotification(campaignId)
183- }
184-
185- /* *
186- * Trigger an in-app message
187- */
188- protected fun triggerInAppMessage (eventName : String = "test_event"): Boolean {
189- return testUtils.triggerInAppMessage(eventName)
190- }
191-
192- /* *
193- * Wait for a campaign to be triggered and processed
194- */
195- protected fun waitForCampaignTrigger (campaignId : Int , timeoutSeconds : Long = TIMEOUT_SECONDS ): Boolean {
196- // Trigger the campaign with callback
197- var triggered = false
198- val latch = CountDownLatch (1 )
199-
200- triggerCampaignViaAPI(campaignId, TestConstants .TEST_USER_EMAIL , null ) { success ->
201- triggered = success
202- latch.countDown()
203- }
204-
205- // Wait for callback
206- try {
207- latch.await(10 , TimeUnit .SECONDS )
208- } catch (e: InterruptedException ) {
209- return false
210- }
211-
212- if (! triggered) {
213- return false
214- }
215-
216- // Wait for the campaign to be processed (in-app message or push notification)
217- return waitForCondition({
218- testUtils.hasInAppMessageDisplayed() || testUtils.hasReceivedPushNotification()
219- }, timeoutSeconds)
220- }
221-
161+
162+
222163 /* *
223164 * Reset URL handler tracking
224165 */
225166 protected fun resetUrlHandlerTracking () {
226167 urlHandlerCalled.set(false )
227168 lastHandledUrl.set(null )
228169 }
229-
230- /* *
231- * Check if URL handler was called
232- */
233- protected fun wasUrlHandlerCalled (): Boolean {
234- return urlHandlerCalled.get()
235- }
236-
170+
237171 /* *
238172 * Get the last URL handled by the URL handler
239173 */
240174 protected fun getLastHandledUrl (): String? {
241175 return lastHandledUrl.get()
242176 }
243-
177+
244178 /* *
245179 * Wait for URL handler to be called
246180 */
0 commit comments