|
| 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 | +} |
0 commit comments