Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@

import androidx.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

public class OSInAppMessage {

public static final String IAM_ID = "messageId";

/**
* The unique identifier for this in-app message
*/
Expand All @@ -46,4 +51,16 @@ public String getMessageId() {
return messageId;
}

public JSONObject toJSONObject() {
JSONObject mainObj = new JSONObject();
try {
mainObj.put(IAM_ID, messageId);
}
catch(JSONException e) {
e.printStackTrace();
}

return mainObj;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

class OSInAppMessageInternal extends OSInAppMessage {

private static final String IAM_ID = "id";
// "id" is expected instead of "messageId" when parsing JSON from the backend
private static final String ID = "id";
private static final String IAM_ID = "messageId";
private static final String IAM_VARIANTS = "variants";
private static final String IAM_TRIGGERS = "triggers";
private static final String IAM_REDISPLAY_STATS = "redisplay";
Expand Down Expand Up @@ -74,7 +76,8 @@ class OSInAppMessageInternal extends OSInAppMessage {

OSInAppMessageInternal(JSONObject json) throws JSONException {
// initialize simple root properties
super(json.getString(IAM_ID));
// "id" is expected instead of "messageId" when parsing JSON from the backend
super(json.getString(ID));
this.variants = parseVariants(json.getJSONObject(IAM_VARIANTS));
this.triggers = parseTriggerJson(json.getJSONArray(IAM_TRIGGERS));
this.clickedClickIds = new HashSet<>();
Expand Down Expand Up @@ -145,7 +148,8 @@ protected ArrayList<ArrayList<OSTrigger>> parseTriggerJson(JSONArray triggersJso
return parsedTriggers;
}

JSONObject toJSONObject() {
@Override
public JSONObject toJSONObject() {
JSONObject json = new JSONObject();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public class InAppMessagingHelpers {
public static final String IAM_PAGE_ID = "12345678-1234-ABCD-1234-123456789012";
public static final String IAM_HAS_LIQUID = "has_liquid";

// unit tests will create an IAM based off JSON of another IAM
// toJSONObject uses key of "messageId" so we need to replace that with "id" for creating IAM
public static JSONObject convertIAMtoJSONObject(OSInAppMessageInternal inAppMessage) {
JSONObject json = inAppMessage.toJSONObject();
try {
json.put("id", json.get("messageId"));
json.remove("messageId");
} catch (JSONException e) {
e.printStackTrace();
}

return json;
}

public static boolean evaluateMessage(OSInAppMessageInternal message) {
return OneSignal.getInAppMessageController().triggerController.evaluateMessageTriggers(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,6 @@ public OSTestInAppMessageInternal(@NonNull String messageId, int displaysQuantit
super(json);
}

OSTestInAppMessageInternal(OSInAppMessageInternal inAppMessage) throws JSONException {
super(inAppMessage.toJSONObject());
}

public void setMessageId(String messageId) {
this.messageId = messageId;
}
Expand Down Expand Up @@ -586,7 +582,8 @@ public static List<OSTestInAppMessageInternal> getRedisplayInAppMessages() {

for (OSInAppMessageInternal message : messages) {
try {
OSTestInAppMessageInternal testInAppMessage = new OSTestInAppMessageInternal(message);
JSONObject json = InAppMessagingHelpers.convertIAMtoJSONObject(message);
OSTestInAppMessageInternal testInAppMessage = new OSTestInAppMessageInternal(json);
testInAppMessage.getRedisplayStats().setDisplayStats(message.getRedisplayStats());
testMessages.add(testInAppMessage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1907,11 +1907,32 @@ public void testLiquidIAMDisplayWaitsForGetTags() throws Exception {
assertEquals("in_app_messages/" + message.getMessageId() + "/impression", lastRequest.url);
}

// Test toJSONObject() method currently only checks JSON for "messageId"
@Test
public void testInAppMessageInternalToJSONObject_messageId() throws Exception {
// 1. Create a basic test IAM
OSTestInAppMessageInternal iam = InAppMessagingHelpers.buildTestMessage(null);

// 2. Set a message ID for the IAM
String messageId = new String(new char[64]).replace('\0', '0');
iam.setMessageId(messageId);

// 3. Init
OneSignalInit();
threadAndTaskWait();

// 4. call toJSONObject() on IAM
JSONObject testJsonObj = iam.toJSONObject();

// 5. Check "messageId" in JSON Object
assertEquals(messageId, testJsonObj.optString("messageId"));
}

private void setMockRegistrationResponseWithMessages(ArrayList<OSTestInAppMessageInternal> messages) throws JSONException {
final JSONArray jsonMessages = new JSONArray();

for (OSTestInAppMessageInternal message : messages)
jsonMessages.put(message.toJSONObject());
jsonMessages.put(InAppMessagingHelpers.convertIAMtoJSONObject(message));

ShadowOneSignalRestClient.setNextSuccessfulRegistrationResponse(new JSONObject() {{
put("id", "df8f05be55ba-b2f7f966-d8cc-11e4-bed1");
Expand Down