Skip to content

Commit 232247d

Browse files
author
Yilong Chang
authored
Add unit tests for 4 in-app trigger test cases. (#167)
Add unit tests for 4 in-app trigger test cases.
1 parent 141a374 commit 232247d

File tree

6 files changed

+444
-15
lines changed

6 files changed

+444
-15
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/ActionManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.leanplum.callbacks.ActionCallback;
3232
import com.leanplum.utils.SharedPreferencesUtil;
3333

34-
import java.lang.reflect.Modifier;
3534
import java.util.HashMap;
3635
import java.util.List;
3736
import java.util.Map;
@@ -252,8 +251,8 @@ public MessageMatchResult shouldShowMessage(String messageId, Map<String, Object
252251
// 2. Must match at least one trigger.
253252
result.matchedTrigger = matchedTriggers(messageConfig.get("whenTriggers"), when, eventName,
254253
contextualValues);
255-
result.matchedUnlessTrigger = matchedTriggers(messageConfig.get("unlessTriggers"), when, eventName,
256-
contextualValues);
254+
result.matchedUnlessTrigger =
255+
matchedTriggers(messageConfig.get("unlessTriggers"), when, eventName, contextualValues);
257256
if (!result.matchedTrigger && !result.matchedUnlessTrigger) {
258257
return result;
259258
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright 2018, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum;
23+
24+
import com.leanplum.__setup.AbstractTest;
25+
import com.leanplum._whitebox.utilities.ResponseHelper;
26+
import com.leanplum.internal.ActionManager;
27+
28+
import org.junit.Test;
29+
30+
import java.util.HashMap;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertFalse;
34+
import static org.junit.Assert.assertTrue;
35+
36+
/**
37+
* Tests in-app message triggers.
38+
*
39+
* @author Yilong Chang
40+
*/
41+
public class LeanplumInAppMessageTriggerTest extends AbstractTest {
42+
/**
43+
* Tests message triggers on start and impression is recorded.
44+
*/
45+
@Test
46+
public void testTriggerOnStart() {
47+
final String messageId = "Trigger on start";
48+
ActionManager actionManager = ActionManager.getInstance();
49+
assertEquals(0, actionManager.getMessageTriggerOccurrences(messageId));
50+
assertTrue(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
51+
52+
setupSDK(mContext, "/responses/start_message_response.json");
53+
// Assert the trigger and message impression occurred.
54+
assertEquals(1, actionManager.getMessageTriggerOccurrences(messageId));
55+
assertFalse(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
56+
}
57+
58+
/**
59+
* Tests message triggers on start or resume, and impression is recorded. Starts in background.
60+
*/
61+
@Test
62+
public void testTriggerOnStartOrResumeBackground() throws Exception {
63+
final String messageId = "Trigger on start or resume";
64+
ActionManager actionManager = ActionManager.getInstance();
65+
assertEquals(0, actionManager.getMessageTriggerOccurrences(messageId));
66+
assertTrue(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
67+
68+
// Start in background.
69+
setupSDK(mContext, "/responses/resume_message_response.json");
70+
71+
// Assert the trigger and message impression occurred after start.
72+
assertEquals(1, actionManager.getMessageTriggerOccurrences(messageId));
73+
assertEquals(3, actionManager.getMessageImpressionOccurrences(messageId).size());
74+
assertEquals(0L, actionManager.getMessageImpressionOccurrences(messageId).get("max"));
75+
76+
Leanplum.pause();
77+
Leanplum.resume();
78+
// Assert the trigger and message impression didn't occur after first resume.
79+
assertEquals(1, actionManager.getMessageTriggerOccurrences(messageId));
80+
assertEquals(3, actionManager.getMessageImpressionOccurrences(messageId).size());
81+
assertEquals(0L, actionManager.getMessageImpressionOccurrences(messageId).get("max"));
82+
83+
Leanplum.pause();
84+
Leanplum.resume();
85+
// Assert the trigger and message impression occurred after second resume.
86+
assertEquals(2, actionManager.getMessageTriggerOccurrences(messageId));
87+
assertEquals(4, actionManager.getMessageImpressionOccurrences(messageId).size());
88+
assertEquals(1L, actionManager.getMessageImpressionOccurrences(messageId).get("max"));
89+
}
90+
91+
/**
92+
* Tests message triggers on start or resume, and impression is recorded. Starts in foreground.
93+
*/
94+
@Test
95+
public void testTriggerOnStartOrResumeForeground() throws Exception {
96+
final String messageId = "Trigger on start or resume";
97+
ActionManager actionManager = ActionManager.getInstance();
98+
assertEquals(0, actionManager.getMessageTriggerOccurrences(messageId));
99+
assertTrue(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
100+
101+
ResponseHelper.seedResponse("/responses/resume_message_response.json");
102+
// Start in foreground so that the message can be triggered on resumeSession.
103+
Leanplum.start(mContext, null, null, null, false);
104+
105+
// Assert the trigger and message impression occurred after start.
106+
assertEquals(1, actionManager.getMessageTriggerOccurrences(messageId));
107+
assertEquals(3, actionManager.getMessageImpressionOccurrences(messageId).size());
108+
assertEquals(0L, actionManager.getMessageImpressionOccurrences(messageId).get("max"));
109+
110+
Leanplum.pause();
111+
Leanplum.resume();
112+
// Assert the trigger and message impression occurred after resume after resume.
113+
assertEquals(2, actionManager.getMessageTriggerOccurrences(messageId));
114+
assertEquals(4, actionManager.getMessageImpressionOccurrences(messageId).size());
115+
assertEquals(1L, actionManager.getMessageImpressionOccurrences(messageId).get("max"));
116+
117+
Leanplum.pause();
118+
Leanplum.resume();
119+
// Assert the trigger and message impression occurrence incremented after resume.
120+
assertEquals(3, actionManager.getMessageTriggerOccurrences(messageId));
121+
assertEquals(5, actionManager.getMessageImpressionOccurrences(messageId).size());
122+
assertEquals(2L, actionManager.getMessageImpressionOccurrences(messageId).get("max"));
123+
}
124+
125+
/**
126+
* Tests message triggers on advance to state, and impression is recorded.
127+
*/
128+
@Test
129+
public void testTriggerOnAdvance() throws Exception {
130+
final String messageId = "Trigger on advance";
131+
final String state = "Registered";
132+
ActionManager actionManager = ActionManager.getInstance();
133+
assertEquals(0, actionManager.getMessageTriggerOccurrences(messageId));
134+
assertTrue(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
135+
136+
setupSDK(mContext, "/responses/start_message_response.json");
137+
Leanplum.advanceTo(state);
138+
// Assert the trigger and message impression occurred after advanced to state.
139+
assertEquals(1, actionManager.getMessageTriggerOccurrences(messageId));
140+
assertFalse(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
141+
}
142+
143+
/**
144+
* Tests message triggers on attribute changes, and impression is recorded.
145+
*/
146+
@Test
147+
public void testTriggerOnAttributeChanges() throws Exception {
148+
final String messageId = "Trigger on Attribute changes";
149+
final String attribute = "Nickname";
150+
final String attributeValue = "Android";
151+
ActionManager actionManager = ActionManager.getInstance();
152+
assertEquals(0, actionManager.getMessageTriggerOccurrences(messageId));
153+
assertTrue(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
154+
155+
setupSDK(mContext, "/responses/start_message_response.json");
156+
Leanplum.setUserAttributes(new HashMap<String, Object>(){{
157+
put(attribute, attributeValue);
158+
}});
159+
// Assert the trigger and message impression occurred after attribute changed.
160+
assertEquals(1, actionManager.getMessageTriggerOccurrences(messageId));
161+
assertFalse(actionManager.getMessageImpressionOccurrences(messageId).isEmpty());
162+
}
163+
}

AndroidSDKTests/src/test/java/com/leanplum/__setup/LeanplumTestHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.leanplum.LeanplumInbox;
3030
import com.leanplum.Var;
3131
import com.leanplum._whitebox.utilities.RequestHelper;
32+
import com.leanplum.internal.ActionManager;
3233
import com.leanplum.internal.LeanplumInternal;
3334
import com.leanplum.internal.Request;
3435
import com.leanplum.internal.RequestFactory;
@@ -43,6 +44,7 @@
4344
import java.io.InputStreamReader;
4445
import java.io.Reader;
4546
import java.nio.charset.Charset;
47+
import java.util.HashMap;
4648
import java.util.List;
4749
import java.util.Map;
4850
import java.util.concurrent.CountDownLatch;
@@ -160,6 +162,10 @@ public static void reset() {
160162
TestClassUtil.setField(LeanplumInbox.class, newsfeed, "didLoad", false);
161163

162164
VarCache.reset();
165+
// Reset the map values in ActionManager.
166+
TestClassUtil.setField(ActionManager.getInstance(), "messageImpressionOccurrences", new HashMap<>());
167+
TestClassUtil.setField(ActionManager.getInstance(), "messageTriggerOccurrences", new HashMap<>());
168+
TestClassUtil.setField(ActionManager.getInstance(), "sessionOccurrences", new HashMap<>());
163169
}
164170

165171
/**

AndroidSDKTests/src/test/java/com/leanplum/_whitebox/MessagesTest.java renamed to AndroidSDKTests/src/test/java/com/leanplum/_whitebox/InAppMessagePrioritizationTest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
*
5757
* @author Kyu Hyun Chang
5858
*/
59-
public class MessagesTest extends AbstractTest {
59+
public class InAppMessagePrioritizationTest extends AbstractTest {
6060
private ActionManager.MessageMatchResult mMessageMatchResult;
6161
private ActionContext.ContextualValues mContextualValues;
6262
private ActionManager mMockActionManager;
@@ -89,7 +89,6 @@ public void after() {
8989
*
9090
* @param jsonMessages message configuration in JSON format
9191
* @param expectedMessageIds a set of expected message ids to be triggered
92-
* @throws Exception
9392
*/
9493
private void assertExpectedMessagesAreTriggered(String jsonMessages,
9594
Set<String> expectedMessageIds) throws Exception {
@@ -127,8 +126,6 @@ public Object answer(InvocationOnMock invocation) {
127126

128127
/**
129128
* Testing a single message with priority 1.
130-
*
131-
* @throws Exception
132129
*/
133130
@Test
134131
public void testSingleMessage() throws Exception {
@@ -143,8 +140,6 @@ public void testSingleMessage() throws Exception {
143140

144141
/**
145142
* Testing three messages with no priorities. Only one should be called.
146-
*
147-
* @throws Exception
148143
*/
149144
@Test
150145
public void testNoPriorities() throws Exception {
@@ -156,8 +151,6 @@ public void testNoPriorities() throws Exception {
156151

157152
/**
158153
* Testing messages with different priorities.
159-
*
160-
* @throws Exception
161154
*/
162155
@Test
163156
public void testDifferentPriorities() throws Exception {
@@ -179,8 +172,6 @@ public void testDifferentPriorities() throws Exception {
179172

180173
/**
181174
* Testing messages with tied priorities.
182-
*
183-
* @throws Exception
184175
*/
185176
@Test
186177
public void testTiedPriorities() throws Exception {
@@ -201,8 +192,6 @@ public void testTiedPriorities() throws Exception {
201192
/**
202193
* Testing messages with different priorities along with no priority (10, 30, no value). Only the
203194
* one with priority value of 10 should be called.
204-
*
205-
* @throws Exception
206195
*/
207196
@Test
208197
public void testDifferentPrioritiesWithMissingValues() throws Exception {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"response": [
3+
{
4+
"actionDefinitions": {
5+
"Alert": {
6+
"values": {
7+
"Dismiss action": {
8+
"__name__": ""
9+
},
10+
"Message": "Alert message goes here.",
11+
"Dismiss text": "OK",
12+
"Title": "LeanplumSample"
13+
},
14+
"kinds": {
15+
"Dismiss action": "ACTION",
16+
"Message": "TEXT",
17+
"Dismiss text": "TEXT",
18+
"Title": "TEXT"
19+
},
20+
"kind": 3,
21+
"options": null
22+
}
23+
},
24+
"isRegistered": false,
25+
"regions": {
26+
},
27+
"messages": {
28+
"Trigger on start or resume": {
29+
"countdown": 86400,
30+
"action": "Alert",
31+
"whenTriggers": {
32+
"children": [
33+
{
34+
"subject": "resume",
35+
"objects": [],
36+
"verb": ""
37+
}
38+
],
39+
"verb": "OR"
40+
},
41+
"parentCampaignId": null,
42+
"vars": {
43+
"Dismiss action": {
44+
"__name__": ""
45+
},
46+
"Message": "Alert message goes here.",
47+
"__name__": "Alert",
48+
"Title": "Push",
49+
"Dismiss text": "OK"
50+
},
51+
"hasImpressionCriteria": false,
52+
"priority": 1000,
53+
"whenLimits": {
54+
"children": [],
55+
"subject": null,
56+
"objects": []
57+
}
58+
}
59+
},
60+
"varsFromCode": {
61+
"emptyDictionary": {
62+
"integerValue": 50
63+
},
64+
"dictionaryVariable": {
65+
"unicode": "?.!s\u0153\u2211\u03c0\u00f8\u02c6\u02dc\u00a8&\/\/.sd",
66+
"test_number": 535,
67+
"test_string": "string"
68+
},
69+
"arrayVariable": {
70+
"[0]": 1,
71+
"[2]": 3,
72+
"[1]": 2,
73+
"[3]": 4,
74+
"[4]": 5
75+
},
76+
"intVariable": 100,
77+
"boolVariable": false,
78+
"floatVariable": 5,
79+
"integerVariable": 1,
80+
"fileVariable": "leanplum_watermark.jpg",
81+
"stringVariable": "test_string"
82+
},
83+
"latestVersion": "1.4.0.2",
84+
"vars": {
85+
},
86+
"fileAttributes": {
87+
"PlugIns\/Leanplum-SDK_Tests.xctest\/test.file": {
88+
"_empty_": {
89+
"hash": "754e4eaae3c92be6e5d45c92b385c8fd",
90+
"size": 25
91+
}
92+
}
93+
},
94+
"token": "jRSv9YRFQ9yVPN80JP0quwHmN9rKk0Crm7awgTwZgCs",
95+
"variants": [
96+
],
97+
"syncNewsfeed": false,
98+
"success": true
99+
}
100+
]
101+
}

0 commit comments

Comments
 (0)