Skip to content

Commit 04c36b8

Browse files
authored
Add markAsRead method to inbox messages. (#411)
1 parent ab02a6f commit 04c36b8

File tree

2 files changed

+115
-76
lines changed

2 files changed

+115
-76
lines changed

AndroidSDKCore/src/main/java/com/leanplum/LeanplumInboxMessage.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -171,15 +171,15 @@ public boolean isRead() {
171171
}
172172

173173
/**
174-
* Read the inbox message, marking it as read and invoking its open action.
174+
* Mark the inbox message as read without invoking its open action.
175175
*/
176-
public void read() {
176+
public void markAsRead() {
177177
try {
178178
if (Constants.isNoop()) {
179179
return;
180180
}
181181

182-
if (!this.isRead) {
182+
if (!isRead) {
183183
setIsRead(true);
184184

185185
int unreadCount = LeanplumInbox.getInstance().unreadCount() - 1;
@@ -191,7 +191,23 @@ public void read() {
191191
.create();
192192
RequestSender.getInstance().send(req);
193193
}
194-
this.context.runTrackedActionNamed(Constants.Values.DEFAULT_PUSH_ACTION);
194+
} catch (Throwable t) {
195+
Log.exception(t);
196+
}
197+
}
198+
199+
/**
200+
* Read the inbox message, marking it as read and invoking its open action.
201+
*/
202+
public void read() {
203+
try {
204+
if (Constants.isNoop()) {
205+
return;
206+
}
207+
208+
markAsRead();
209+
210+
getContext().runTrackedActionNamed(Constants.Values.DEFAULT_PUSH_ACTION);
195211
} catch (Throwable t) {
196212
Log.exception(t);
197213
}
Lines changed: 94 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018, Leanplum, Inc. All rights reserved.
2+
* Copyright 2020, Leanplum, Inc. All rights reserved.
33
*
44
* Licensed to the Apache Software Foundation (ASF) under one
55
* or more contributor license agreements. See the NOTICE file
@@ -25,9 +25,9 @@
2525

2626
import org.junit.Test;
2727
import org.junit.runner.RunWith;
28+
import org.mockito.Mockito;
2829
import org.robolectric.RobolectricTestRunner;
2930

30-
import java.util.Calendar;
3131
import java.util.Date;
3232
import java.util.HashMap;
3333
import org.robolectric.annotation.Config;
@@ -36,6 +36,9 @@
3636
import static org.junit.Assert.assertFalse;
3737
import static org.junit.Assert.assertNull;
3838
import static org.junit.Assert.assertTrue;
39+
import static org.mockito.Mockito.never;
40+
import static org.mockito.Mockito.times;
41+
import static org.mockito.Mockito.verify;
3942

4043
/**
4144
* Tests covering Inbox Messages.
@@ -45,26 +48,45 @@
4548
@RunWith(RobolectricTestRunner.class)
4649
@Config(sdk = VERSION_CODES.P) // temporarily fix issue with Robolectric and Android SDK 29
4750
public class LeanplumInboxMessageTest {
51+
52+
private LeanplumInboxMessage createMessageTestData(
53+
String messageId,
54+
boolean isRead,
55+
long deliveryMillis,
56+
long expirationMillis) {
57+
58+
HashMap<String, Object> map = new HashMap<>();
59+
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
60+
if (deliveryMillis != 0)
61+
map.put(Constants.Keys.DELIVERY_TIMESTAMP, deliveryMillis);
62+
if (expirationMillis != 0)
63+
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expirationMillis);
64+
map.put(Constants.Keys.IS_READ, isRead);
65+
LeanplumInboxMessage message = LeanplumInboxMessage.createFromJsonMap(messageId, map);
66+
return message;
67+
}
68+
69+
private LeanplumInboxMessage createMessageTestData(String messageId, boolean isRead) {
70+
return createMessageTestData(messageId, isRead, 100, 200);
71+
}
72+
4873
/**
4974
* Test creating a message from json.
5075
*/
5176
@Test
5277
public void testCreateFromJsonMap() {
53-
Date delivery = new Date(100);
54-
Date expiration = new Date(200);
55-
HashMap<String, Object> map = new HashMap<>();
56-
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
57-
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
58-
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
59-
map.put(Constants.Keys.IS_READ, true);
60-
61-
LeanplumInboxMessage message = LeanplumInboxMessage.createFromJsonMap("message##Id", map);
62-
assertEquals("message##Id", message.getMessageId());
63-
assertEquals(delivery, message.getDeliveryTimestamp());
64-
assertEquals(expiration, message.getExpirationTimestamp());
78+
String messageId = "message##Id";
79+
long deliveryMillis = 100;
80+
long expirationMillis = 200;
81+
82+
LeanplumInboxMessage message =
83+
createMessageTestData(messageId, true, deliveryMillis, expirationMillis);
84+
85+
assertEquals(messageId, message.getMessageId());
86+
assertEquals(deliveryMillis, message.getDeliveryTimestamp().getTime());
87+
assertEquals(expirationMillis, message.getExpirationTimestamp().getTime());
6588
assertTrue(message.isRead());
6689
assertNull(message.getData());
67-
6890
assertNull(message.getImageFilePath());
6991
assertNull(message.getImageUrl());
7092
}
@@ -74,15 +96,9 @@ public void testCreateFromJsonMap() {
7496
*/
7597
@Test
7698
public void testCreateFromJsonMapInvalidMessageIdIsRejected() {
77-
Date delivery = new Date(100);
78-
Date expiration = new Date(200);
79-
HashMap<String, Object> map = new HashMap<>();
80-
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
81-
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
82-
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
83-
map.put(Constants.Keys.IS_READ, true);
99+
String invalidMessageId = "messageId";
100+
LeanplumInboxMessage invalidMessage = createMessageTestData(invalidMessageId, true);
84101

85-
LeanplumInboxMessage invalidMessage = LeanplumInboxMessage.createFromJsonMap("messageId", map);
86102
assertNull(invalidMessage);
87103
}
88104

@@ -91,45 +107,29 @@ public void testCreateFromJsonMapInvalidMessageIdIsRejected() {
91107
*/
92108
@Test
93109
public void testReadAndUnreadCount() {
94-
Date delivery = new Date(100);
95-
Date expiration = new Date(200);
96-
HashMap<String, Object> map = new HashMap<>();
97-
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
98-
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
99-
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
100-
map.put(Constants.Keys.IS_READ, false);
101-
LeanplumInboxMessage message = LeanplumInboxMessage
102-
.createFromJsonMap("messageId##00", map);
103-
int intialUnreadCount = LeanplumInbox.getInstance().unreadCount();
110+
LeanplumInboxMessage message = createMessageTestData("messageId##00", false);
111+
int initialUnreadCount = LeanplumInbox.getInstance().unreadCount();
104112

105-
assertEquals(false, message.isRead());
113+
assertFalse(message.isRead());
106114

107115
message.read();
108116

109-
assertEquals(true, message.isRead());
110-
assertEquals(intialUnreadCount - 1, LeanplumInbox.getInstance().unreadCount());
117+
assertTrue(message.isRead());
118+
assertEquals(initialUnreadCount - 1, LeanplumInbox.getInstance().unreadCount());
111119
}
112120

113121
/**
114122
* Tests method isActive happy path.
115123
*/
116124
@Test
117125
public void testIsActive() {
118-
Date delivery = new Date(100);
119-
Calendar date = Calendar.getInstance();
120-
Date expiration = new Date(date.getTimeInMillis() + 100000);
126+
long deliveryMillis = 100;
127+
long expirationMillis = new Date().getTime() + 100000; // current time + 100000
121128

122-
HashMap<String, Object> map = new HashMap<>();
123-
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
124-
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
125-
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
126-
map.put(Constants.Keys.IS_READ, false);
127-
LeanplumInboxMessage message = LeanplumInboxMessage
128-
.createFromJsonMap("messageId##00", map);
129-
130-
Boolean active = message.isActive();
129+
LeanplumInboxMessage message =
130+
createMessageTestData("messageId##00", false, deliveryMillis, expirationMillis);
131131

132-
assertTrue(active);
132+
assertTrue(message.isActive());
133133
}
134134

135135
/**
@@ -138,36 +138,59 @@ public void testIsActive() {
138138
*/
139139
@Test
140140
public void testIsActiveWithExpiredMessage() {
141-
Date delivery = new Date(200);
142-
Date expiration = new Date(200);
143-
HashMap<String, Object> map = new HashMap<>();
144-
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
145-
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
146-
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
147-
map.put(Constants.Keys.IS_READ, false);
148-
LeanplumInboxMessage message = LeanplumInboxMessage
149-
.createFromJsonMap("messageId##00", map);
150-
151-
Boolean isActive = message.isActive();
152-
153-
assertFalse(isActive);
141+
LeanplumInboxMessage message = createMessageTestData("messageId##00", false, 200, 200);
142+
assertFalse(message.isActive());
154143
}
155144

156145
/**
157146
* Test isActive method with null expiration timestamp.
158147
*/
159148
@Test
160149
public void testIsActiveWithNullTimestamp() {
161-
Date delivery = new Date(100);
162-
HashMap<String, Object> map = new HashMap<>();
163-
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
164-
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
165-
map.put(Constants.Keys.IS_READ, false);
166-
LeanplumInboxMessage message = LeanplumInboxMessage
167-
.createFromJsonMap("messageId##00", map);
150+
long deliveryMillis = 100;
151+
long noExpiration = 0;
152+
LeanplumInboxMessage message =
153+
createMessageTestData("messageId##00", false, deliveryMillis, noExpiration);
154+
155+
assertTrue(message.isActive());
156+
}
157+
158+
/**
159+
* Test markAsRead does not execute Open Action.
160+
*/
161+
@Test
162+
public void testMarkAsRead() {
163+
LeanplumInboxMessage realMessage = createMessageTestData("messageId##00", false);
164+
LeanplumInboxMessage message = Mockito.spy(realMessage);
168165

169-
Boolean isActive = message.isActive();
166+
ActionContext context = Mockito.mock(ActionContext.class);
167+
Mockito.when(message.getContext()).thenReturn(context);
170168

171-
assertTrue(isActive);
169+
assertFalse(message.isRead());
170+
171+
message.markAsRead();
172+
173+
assertTrue(message.isRead());
174+
verify(context, never()).runTrackedActionNamed("Open action");
175+
}
176+
177+
/**
178+
* Test read() calls markAsRead and executes Open Action.
179+
*/
180+
@Test
181+
public void testReadCallsMarkAsRead() {
182+
LeanplumInboxMessage realMessage = createMessageTestData("messageId##00", false);
183+
LeanplumInboxMessage message = Mockito.spy(realMessage);
184+
185+
ActionContext context = Mockito.mock(ActionContext.class);
186+
Mockito.when(message.getContext()).thenReturn(context);
187+
188+
assertFalse(message.isRead());
189+
190+
message.read();
191+
192+
assertTrue(message.isRead());
193+
verify(message, times(1)).markAsRead();
194+
verify(context, times(1)).runTrackedActionNamed("Open action");
172195
}
173196
}

0 commit comments

Comments
 (0)