Skip to content

Commit 70209fa

Browse files
author
Sayaan Saha
authored
tests(inbox): inbox test refactor and improve code coverage (#164)
1 parent 232247d commit 70209fa

File tree

5 files changed

+501
-190
lines changed

5 files changed

+501
-190
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
package com.leanplum;
22+
23+
import com.leanplum.internal.Constants;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.robolectric.RobolectricTestRunner;
28+
29+
import java.util.Calendar;
30+
import java.util.Date;
31+
import java.util.HashMap;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertFalse;
35+
import static org.junit.Assert.assertNull;
36+
import static org.junit.Assert.assertTrue;
37+
38+
/**
39+
* Tests covering Inbox Messages.
40+
*
41+
* @author Sayaan Saha
42+
*/
43+
@RunWith(RobolectricTestRunner.class)
44+
public class LeanplumInboxMessageTest {
45+
/**
46+
* Test creating a message from json.
47+
*/
48+
@Test
49+
public void testCreateFromJsonMap() {
50+
Date delivery = new Date(100);
51+
Date expiration = new Date(200);
52+
HashMap<String, Object> map = new HashMap<>();
53+
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
54+
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
55+
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
56+
map.put(Constants.Keys.IS_READ, true);
57+
58+
LeanplumInboxMessage message = LeanplumInboxMessage.createFromJsonMap("message##Id", map);
59+
assertEquals("message##Id", message.getMessageId());
60+
assertEquals(delivery, message.getDeliveryTimestamp());
61+
assertEquals(expiration, message.getExpirationTimestamp());
62+
assertTrue(message.isRead());
63+
assertNull(message.getData());
64+
65+
assertNull(message.getImageFilePath());
66+
assertNull(message.getImageUrl());
67+
}
68+
69+
/**
70+
* Test that message without messageId is rejected.
71+
*/
72+
@Test
73+
public void testCreateFromJsonMapInvalidMessageIdIsRejected() {
74+
Date delivery = new Date(100);
75+
Date expiration = new Date(200);
76+
HashMap<String, Object> map = new HashMap<>();
77+
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
78+
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
79+
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
80+
map.put(Constants.Keys.IS_READ, true);
81+
82+
LeanplumInboxMessage invalidMessage = LeanplumInboxMessage.createFromJsonMap("messageId", map);
83+
assertNull(invalidMessage);
84+
}
85+
86+
/**
87+
* Test unread count is updated after reading a message.
88+
*/
89+
@Test
90+
public void testReadAndUnreadCount() {
91+
Date delivery = new Date(100);
92+
Date expiration = new Date(200);
93+
HashMap<String, Object> map = new HashMap<>();
94+
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
95+
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
96+
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
97+
map.put(Constants.Keys.IS_READ, false);
98+
LeanplumInboxMessage message = LeanplumInboxMessage
99+
.createFromJsonMap("messageId##00", map);
100+
int intialUnreadCount = LeanplumInbox.getInstance().unreadCount();
101+
102+
assertEquals(false, message.isRead());
103+
104+
message.read();
105+
106+
assertEquals(true, message.isRead());
107+
assertEquals(intialUnreadCount - 1, LeanplumInbox.getInstance().unreadCount());
108+
}
109+
110+
/**
111+
* Tests method isActive happy path.
112+
*/
113+
@Test
114+
public void testIsActive() {
115+
Date delivery = new Date(100);
116+
Calendar date = Calendar.getInstance();
117+
Date expiration = new Date(date.getTimeInMillis() + 100000);
118+
119+
HashMap<String, Object> map = new HashMap<>();
120+
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
121+
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
122+
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
123+
map.put(Constants.Keys.IS_READ, false);
124+
LeanplumInboxMessage message = LeanplumInboxMessage
125+
.createFromJsonMap("messageId##00", map);
126+
127+
Boolean active = message.isActive();
128+
129+
assertTrue(active);
130+
}
131+
132+
/**
133+
* Test isActive method with a message
134+
* after the expiration timestamp.
135+
*/
136+
@Test
137+
public void testIsActiveWithExpiredMessage() {
138+
Date delivery = new Date(200);
139+
Date expiration = new Date(200);
140+
HashMap<String, Object> map = new HashMap<>();
141+
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
142+
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
143+
map.put(Constants.Keys.EXPIRATION_TIMESTAMP, expiration.getTime());
144+
map.put(Constants.Keys.IS_READ, false);
145+
LeanplumInboxMessage message = LeanplumInboxMessage
146+
.createFromJsonMap("messageId##00", map);
147+
148+
Boolean isActive = message.isActive();
149+
150+
assertFalse(isActive);
151+
}
152+
153+
/**
154+
* Test isActive method with null expiration timestamp.
155+
*/
156+
@Test
157+
public void testIsActiveWithNullTimestamp() {
158+
Date delivery = new Date(100);
159+
HashMap<String, Object> map = new HashMap<>();
160+
map.put(Constants.Keys.MESSAGE_DATA, new HashMap<String, Object>());
161+
map.put(Constants.Keys.DELIVERY_TIMESTAMP, delivery.getTime());
162+
map.put(Constants.Keys.IS_READ, false);
163+
LeanplumInboxMessage message = LeanplumInboxMessage
164+
.createFromJsonMap("messageId##00", map);
165+
166+
Boolean isActive = message.isActive();
167+
168+
assertTrue(isActive);
169+
}
170+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
package com.leanplum;
22+
23+
24+
import com.leanplum.__setup.AbstractTest;
25+
import com.leanplum._whitebox.utilities.ResponseHelper;
26+
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import java.lang.reflect.Constructor;
31+
import java.util.List;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertNotNull;
35+
import static org.mockito.Mockito.mock;
36+
import static org.mockito.Mockito.times;
37+
import static org.mockito.Mockito.verify;
38+
39+
/**
40+
* Whitebox tests covering Inbox Messages.
41+
*
42+
* @author Sayaan Saha
43+
*/
44+
public class LeanplumInboxMessageWhiteboxTest extends AbstractTest {
45+
@Before
46+
public void setUp() {
47+
setupSDK(mContext, "/responses/simple_start_response.json");
48+
}
49+
50+
/**
51+
* Tests getting local file path for prefetched image assests.
52+
*/
53+
@Test
54+
public void testImageFilepathIsReturnedIfPrefetchingEnabled() {
55+
ResponseHelper.seedResponse("/responses/newsfeed_response.json");
56+
LeanplumInbox.getInstance().downloadMessages();
57+
LeanplumInboxMessage imageMessage = LeanplumInbox.getInstance().allMessages().get(0);
58+
59+
String imageFilePath = imageMessage.getImageFilePath();
60+
61+
assertNotNull(imageFilePath);
62+
}
63+
64+
/**
65+
* Tests getting image url from Inbox message object.
66+
*/
67+
@Test
68+
public void testImageURL() {
69+
LeanplumInbox.disableImagePrefetching();
70+
ResponseHelper.seedResponse("/responses/newsfeed_response.json");
71+
LeanplumInbox.getInstance().downloadMessages();
72+
List<LeanplumInboxMessage> messagesList = LeanplumInbox.getInstance().allMessages();
73+
LeanplumInboxMessage imageMessage = messagesList.get(0);
74+
75+
String actualUrl = imageMessage.getImageUrl().toString();
76+
77+
assertEquals("http://bit.ly/2GzJxxx", actualUrl);
78+
}
79+
80+
/**
81+
* Tests method remove, to remove a message from inbox.
82+
*/
83+
@Test
84+
public void testRemove() {
85+
ResponseHelper.seedResponse("/responses/newsfeed_response.json");
86+
LeanplumInbox.getInstance().downloadMessages();
87+
LeanplumInboxMessage message = LeanplumInbox.getInstance().allMessages().get(1);
88+
int intialCount = LeanplumInbox.getInstance().count();
89+
90+
message.remove();
91+
92+
assertEquals(intialCount - 1, LeanplumInbox.getInstance().count());
93+
}
94+
95+
/**
96+
* Tests leanplum inbox message calls runTrackedActionName to execute open action.
97+
*/
98+
@Test
99+
public void testOpenAction() throws Exception {
100+
ActionContext mock = mock(ActionContext.class);
101+
Constructor<LeanplumInboxMessage> constructor = (Constructor<LeanplumInboxMessage>) LeanplumInboxMessage.class.getDeclaredConstructors()[0];
102+
constructor.setAccessible(true);
103+
104+
LeanplumInboxMessage imageMessage = constructor.newInstance("mesageId##11", 100L,
105+
200L, false, mock);
106+
imageMessage.read();
107+
108+
verify(mock, times(1)).runTrackedActionNamed("Open action");
109+
110+
// Verify that runTrackedActionNamed was called if the message was read before.
111+
imageMessage.read();
112+
verify(mock, times(2)).runTrackedActionNamed("Open action");
113+
}
114+
}

0 commit comments

Comments
 (0)