-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathIterableActionRunnerTest.java
More file actions
104 lines (87 loc) · 4.18 KB
/
IterableActionRunnerTest.java
File metadata and controls
104 lines (87 loc) · 4.18 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
package com.iterable.iterableapi;
import android.app.Instrumentation;
import android.content.Intent;
import android.net.Uri;
import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.Intents.intending;
import static androidx.test.espresso.intent.matcher.IntentMatchers.anyIntent;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasData;
import static org.hamcrest.CoreMatchers.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@RunWith(AndroidJUnit4.class)
public class IterableActionRunnerTest {
@Before
public void setUp() {
Intents.init();
}
@After
public void tearDown() {
Intents.release();
}
@Test
public void testOpenUrlAction() throws Exception {
IterableTestUtils.initIterableApi(null);
intending(anyIntent()).respondWith(new Instrumentation.ActivityResult(0, null));
JSONObject actionData = new JSONObject();
actionData.put("type", "openUrl");
actionData.put("data", "https://example.com");
IterableAction action = IterableAction.from(actionData);
IterableActionRunner.executeAction(getApplicationContext(), action, IterableActionSource.PUSH);
intended(allOf(hasAction(Intent.ACTION_VIEW), hasData("https://example.com")));
Intents.assertNoUnverifiedIntents();
}
@Test
public void testUrlHandlingOverride() throws Exception {
// Use a simple implementation instead of mock for API 36+ compatibility
IterableUrlHandler urlHandler = new IterableUrlHandler() {
@Override
public boolean handleIterableURL(Uri uri, IterableActionContext context) {
return true;
}
};
IterableTestUtils.initIterableApi(new IterableConfig.Builder().setUrlHandler(urlHandler).build());
JSONObject actionData = new JSONObject();
actionData.put("type", "openUrl");
actionData.put("data", "https://example.com");
IterableAction action = IterableAction.from(actionData);
IterableActionRunner.executeAction(getApplicationContext(), action, IterableActionSource.PUSH);
Intents.assertNoUnverifiedIntents();
IterableTestUtils.initIterableApi(null);
}
@Test
public void testCustomAction() throws Exception {
// Track if the custom action handler was called (for API 36+ compatibility)
final boolean[] handlerCalled = {false};
final IterableAction[] capturedAction = {null};
final IterableActionContext[] capturedContext = {null};
IterableCustomActionHandler customActionHandler = (action, actionContext) -> {
handlerCalled[0] = true;
capturedAction[0] = action;
capturedContext[0] = actionContext;
return false;
};
IterableTestUtils.initIterableApi(new IterableConfig.Builder().setCustomActionHandler(customActionHandler).build());
JSONObject actionData = new JSONObject();
actionData.put("type", "customActionName");
IterableAction action = IterableAction.from(actionData);
IterableActionRunner.executeAction(getApplicationContext(), action, IterableActionSource.PUSH);
// Verify the handler was called with correct parameters
assertTrue("Custom action handler should have been called", handlerCalled[0]);
assertNotNull("Action should not be null", capturedAction[0]);
assertEquals("Action type should match", "customActionName", capturedAction[0].getType());
assertNotNull("Context should not be null", capturedContext[0]);
assertEquals("Source should be PUSH", IterableActionSource.PUSH, capturedContext[0].source);
IterableTestUtils.initIterableApi(null);
}
}