Skip to content

Commit cc6b556

Browse files
author
“Akshay
committed
Optimizations added & removed unnecessary logs
1 parent 95f7d5d commit cc6b556

File tree

2 files changed

+39
-108
lines changed

2 files changed

+39
-108
lines changed

integration-tests/src/androidTest/java/com/iterable/integration/tests/EmbeddedMessageIntegrationTest.kt

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class EmbeddedMessageIntegrationTest : BaseIntegrationTest() {
3535

3636
private lateinit var uiDevice: UiDevice
3737
private lateinit var mainActivityScenario: ActivityScenario<MainActivity>
38-
private lateinit var embeddedActivityScenario: ActivityScenario<EmbeddedMessageTestActivity>
3938

4039
@Before
4140
override fun setUp() {
@@ -111,8 +110,6 @@ class EmbeddedMessageIntegrationTest : BaseIntegrationTest() {
111110

112111
@Test
113112
fun testEmbeddedMessageMVP() {
114-
Log.d(TAG, "🚀 Starting MVP embedded message test")
115-
116113
// Step 1: Ensure user is signed in
117114
Log.d(TAG, "📧 Step 1: Ensuring user is signed in...")
118115
val userSignedIn = testUtils.ensureUserSignedIn(TestConstants.TEST_USER_EMAIL)
@@ -147,14 +144,14 @@ class EmbeddedMessageIntegrationTest : BaseIntegrationTest() {
147144

148145
// Step 4: Wait 5 seconds for backend to process and make user eligible
149146
Log.d(TAG, "⏳ Step 4: Waiting 5 seconds for backend to process user update...")
150-
Thread.sleep(5000)
147+
Thread.sleep(3000)
151148

152149
// Step 5: Manually sync embedded messages
153150
Log.d(TAG, "🔄 Step 5: Syncing embedded messages...")
154151
IterableApi.getInstance().embeddedManager.syncMessages()
155152

156153
// Wait for sync to complete
157-
Thread.sleep(3000)
154+
Thread.sleep(2000)
158155

159156
// Step 6: Get placement IDs and verify expected placement ID exists
160157
Log.d(TAG, "🔍 Step 6: Getting placement IDs...")
@@ -170,7 +167,6 @@ class EmbeddedMessageIntegrationTest : BaseIntegrationTest() {
170167
// Step 7: Get messages for the placement ID
171168
Log.d(TAG, "📨 Step 7: Getting messages for placement ID $TEST_PLACEMENT_ID...")
172169
val messages = IterableApi.getInstance().embeddedManager.getMessages(TEST_PLACEMENT_ID)
173-
Assert.assertNotNull("Messages should not be null", messages)
174170
Assert.assertTrue("Should have at least 1 message for placement $TEST_PLACEMENT_ID", messages!!.isNotEmpty())
175171

176172
val message = messages.first()
@@ -196,7 +192,7 @@ class EmbeddedMessageIntegrationTest : BaseIntegrationTest() {
196192
}
197193

198194
// Wait for fragment to be displayed
199-
Thread.sleep(2000)
195+
Thread.sleep(1000)
200196

201197
// Step 9: Verify display - check fragment exists
202198
Log.d(TAG, "✅ Step 9: Verifying embedded message is displayed...")
@@ -228,49 +224,32 @@ class EmbeddedMessageIntegrationTest : BaseIntegrationTest() {
228224
// Step 10: Interact with button - find and click first button
229225
Log.d(TAG, "🎯 Step 10: Clicking button in the embedded message...")
230226

231-
// Try to find button by resource ID or text
232-
var buttonClicked = false
233-
var attempts = 0
234-
val maxAttempts = 5
227+
// Try to find button by resource ID first
228+
val button = uiDevice.findObject(UiSelector().resourceId("com.iterable.iterableapi.ui:id/embedded_message_first_button"))
235229

236-
while (!buttonClicked && attempts < maxAttempts) {
237-
attempts++
238-
Log.d(TAG, "Attempt $attempts: Looking for button...")
239-
240-
// Try to find button by resource ID
241-
val button = uiDevice.findObject(UiSelector().resourceId("com.iterable.iterableapi.ui:id/embedded_message_first_button"))
242-
243-
if (button.exists()) {
244-
button.click()
245-
buttonClicked = true
246-
Log.d(TAG, "✅ Clicked embedded message button")
247-
} else {
248-
// Try to find by button text if available
249-
val buttonText = message.elements?.buttons?.firstOrNull()?.title
250-
if (buttonText != null) {
251-
val buttonByText = uiDevice.findObject(By.text(buttonText))
252-
if (buttonByText != null) {
253-
buttonByText.click()
254-
buttonClicked = true
255-
Log.d(TAG, "✅ Clicked embedded message button by text: $buttonText")
256-
}
230+
if (button.exists()) {
231+
button.click()
232+
Log.d(TAG, "✅ Clicked embedded message button")
233+
} else {
234+
// Try to find by button text if available
235+
val buttonText = message.elements?.buttons?.firstOrNull()?.title
236+
if (buttonText != null) {
237+
val buttonByText = uiDevice.findObject(By.text(buttonText))
238+
if (buttonByText != null) {
239+
buttonByText.click()
240+
Log.d(TAG, "✅ Clicked embedded message button by text: $buttonText")
241+
} else {
242+
Assert.fail("Button not found in the embedded message (tried resource ID and text: $buttonText)")
257243
}
244+
} else {
245+
Assert.fail("Button not found in the embedded message (tried resource ID, but no button text available)")
258246
}
259-
260-
if (!buttonClicked) {
261-
Log.d(TAG, "Button not found, waiting 1 second before retry...")
262-
Thread.sleep(1000)
263-
}
264-
}
265-
266-
if (!buttonClicked) {
267-
Assert.fail("Button not found in the embedded message after $maxAttempts attempts")
268247
}
269248

270249
// Step 11: Verify URL handler was called
271250
Log.d(TAG, "🎯 Step 11: Verifying URL handler was called after button click...")
272251

273-
val urlHandlerCalled = waitForUrlHandler(timeoutSeconds = 5)
252+
val urlHandlerCalled = waitForUrlHandler(timeoutSeconds = 3)
274253
Assert.assertTrue(
275254
"URL handler should have been called after clicking the button",
276255
urlHandlerCalled
Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
11
package com.iterable.integration.tests.activities
22

33
import android.os.Bundle
4-
import android.util.Log
54
import android.widget.Button
65
import android.widget.Switch
76
import android.widget.TextView
87
import android.widget.Toast
98
import androidx.appcompat.app.AlertDialog
109
import androidx.appcompat.app.AppCompatActivity
1110
import com.iterable.iterableapi.IterableApi
12-
import com.iterable.iterableapi.IterableEmbeddedMessage
1311
import com.iterable.integration.tests.R
1412
import com.iterable.iterableapi.ui.embedded.IterableEmbeddedView
1513
import com.iterable.iterableapi.ui.embedded.IterableEmbeddedViewType
1614
import org.json.JSONObject
1715

1816
class EmbeddedMessageTestActivity : AppCompatActivity() {
1917

20-
companion object {
21-
private const val TAG = "EmbeddedMessageTest"
22-
}
23-
2418
private lateinit var statusTextView: TextView
2519
private lateinit var checkIsPremiumButton: Button
2620
private lateinit var isPremiumSwitch: Switch
2721
private lateinit var syncMessagesButton: Button
2822

29-
// Track current isPremium state locally (since SDK doesn't store it)
30-
private var currentIsPremium = false
31-
3223
override fun onCreate(savedInstanceState: Bundle?) {
3324
super.onCreate(savedInstanceState)
3425
setContentView(R.layout.activity_embedded_message_test)
3526

36-
Log.d(TAG, "Embedded Message Test Activity started")
37-
3827
initializeViews()
3928
setupClickListeners()
40-
updateStatus("Activity initialized - Ready to test embedded messages")
29+
updateStatus("Ready to test embedded messages")
4130
}
4231

4332
private fun initializeViews() {
4433
statusTextView = findViewById(R.id.status_text)
4534
checkIsPremiumButton = findViewById(R.id.btnCheckIsPremium)
4635
isPremiumSwitch = findViewById(R.id.switchIsPremium)
4736
syncMessagesButton = findViewById(R.id.btnSyncMessages)
48-
49-
// Initialize switch to false
50-
isPremiumSwitch.isChecked = currentIsPremium
5137
}
5238

5339
private fun setupClickListeners() {
@@ -65,60 +51,40 @@ class EmbeddedMessageTestActivity : AppCompatActivity() {
6551
}
6652

6753
private fun checkIsPremiumStatus() {
68-
updateStatus("Checking isPremium status...")
69-
Log.d(TAG, "Checking isPremium status")
70-
71-
// Note: SDK doesn't store user data fields locally, so we show an info dialog
7254
AlertDialog.Builder(this)
7355
.setTitle("isPremium Status")
7456
.setMessage("User data fields are stored on the server, not in the SDK.\n\n" +
75-
"To check isPremium status, you can:\n" +
57+
"To check isPremium status:\n" +
7658
"1. Check server logs/dashboard\n" +
7759
"2. Call server API to get user profile\n" +
7860
"3. Check logcat for updateUser calls")
7961
.setPositiveButton("OK", null)
8062
.show()
81-
82-
Toast.makeText(this, "Check logcat or server dashboard for user data fields", Toast.LENGTH_LONG).show()
83-
updateStatus("Status check: See dialog for details")
8463
}
8564

8665
private fun updateUserIsPremium(isPremium: Boolean) {
87-
currentIsPremium = isPremium
8866
val statusText = if (isPremium) "true" else "false"
89-
9067
updateStatus("Updating user (isPremium = $statusText)...")
91-
Log.d(TAG, "Updating user with isPremium = $statusText")
9268

9369
val dataFields = JSONObject().apply {
9470
put("isPremium", isPremium)
9571
}
9672

97-
// Disable switch during update
9873
isPremiumSwitch.isEnabled = false
99-
100-
// Note: updateUser doesn't have callbacks in the current SDK API
101-
// But we can track success/failure by monitoring logs or adding listeners
10274
IterableApi.getInstance().updateUser(dataFields)
10375

104-
// Show toast
105-
Toast.makeText(this, "updateUser called (isPremium = $statusText)\nWait 5 seconds then sync messages to verify", Toast.LENGTH_LONG).show()
106-
Log.d(TAG, "✅ updateUser called with isPremium = $statusText")
76+
Toast.makeText(this, "updateUser called (isPremium = $statusText)\nWait 5 seconds then sync messages", Toast.LENGTH_LONG).show()
10777

108-
// Re-enable switch after delay
10978
isPremiumSwitch.postDelayed({
11079
isPremiumSwitch.isEnabled = true
111-
updateStatus("User update request sent (isPremium = $statusText) - Sync messages to verify")
80+
updateStatus("User updated (isPremium = $statusText) - Sync messages to verify")
11281
}, 1000)
11382
}
11483

11584
private fun syncEmbeddedMessages() {
11685
updateStatus("Syncing embedded messages...")
117-
Log.d(TAG, "Syncing embedded messages")
118-
11986
IterableApi.getInstance().embeddedManager.syncMessages()
12087

121-
// Wait a bit for sync to complete, then show status and display messages
12288
Thread {
12389
Thread.sleep(2000)
12490

@@ -129,62 +95,48 @@ class EmbeddedMessageTestActivity : AppCompatActivity() {
12995
}
13096

13197
val statusMessage = if (messageCount > 0) {
132-
"Sync complete: Found $messageCount message(s) in ${placementIds.size} placement(s)"
98+
"✅ Found $messageCount message(s) in ${placementIds.size} placement(s)"
13399
} else {
134-
"⚠️ Sync complete: No messages found. Check user eligibility and campaign configuration"
100+
"⚠️ No messages found. Check user eligibility and campaign configuration"
135101
}
136102

137103
updateStatus(statusMessage)
138104
Toast.makeText(this, statusMessage, Toast.LENGTH_LONG).show()
139-
Log.d(TAG, statusMessage)
140105

141-
// Display messages if found
142106
if (messageCount > 0) {
143107
displayEmbeddedMessages(placementIds)
144108
} else {
145-
// Clear any existing fragments
146109
clearEmbeddedMessages()
147110
}
148111
}
149112
}.start()
150113
}
151114

152115
private fun displayEmbeddedMessages(placementIds: List<Long>) {
153-
Log.d(TAG, "Displaying embedded messages for ${placementIds.size} placement(s)")
116+
val firstPlacementId = placementIds.firstOrNull() ?: return
117+
val messages = IterableApi.getInstance().embeddedManager.getMessages(firstPlacementId) ?: return
154118

155-
// For now, display the first message from the first placement
156-
// In a real scenario, you might want to display all messages or have a placement selector
157-
val firstPlacementId = placementIds.firstOrNull()
158-
if (firstPlacementId != null) {
159-
val messages = IterableApi.getInstance().embeddedManager.getMessages(firstPlacementId)
160-
if (messages != null && messages.isNotEmpty()) {
161-
val firstMessage = messages.first()
162-
Log.d(TAG, "Displaying message: ${firstMessage.metadata.messageId} from placement: $firstPlacementId")
163-
164-
// Create and add fragment
165-
val fragment = IterableEmbeddedView(IterableEmbeddedViewType.BANNER, firstMessage, null)
166-
supportFragmentManager.beginTransaction()
167-
.replace(R.id.embedded_message_container, fragment)
168-
.commitNowAllowingStateLoss()
169-
170-
updateStatus("✅ Message displayed: ${firstMessage.metadata.messageId}")
171-
Log.d(TAG, "✅ Embedded message fragment added")
172-
}
119+
if (messages.isNotEmpty()) {
120+
val firstMessage = messages.first()
121+
val fragment = IterableEmbeddedView(IterableEmbeddedViewType.BANNER, firstMessage, null)
122+
supportFragmentManager.beginTransaction()
123+
.replace(R.id.embedded_message_container, fragment)
124+
.commitNowAllowingStateLoss()
125+
126+
updateStatus("✅ Message displayed: ${firstMessage.metadata.messageId}")
173127
}
174128
}
175129

176130
private fun clearEmbeddedMessages() {
177131
val fragment = supportFragmentManager.findFragmentById(R.id.embedded_message_container)
178-
if (fragment != null) {
132+
fragment?.let {
179133
supportFragmentManager.beginTransaction()
180-
.remove(fragment)
134+
.remove(it)
181135
.commitNowAllowingStateLoss()
182-
Log.d(TAG, "Cleared embedded message fragment")
183136
}
184137
}
185138

186139
private fun updateStatus(status: String) {
187140
statusTextView.text = "Status: $status"
188-
Log.d(TAG, "Status: $status")
189141
}
190142
}

0 commit comments

Comments
 (0)