1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cellbroadcastreceiver.unit; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 22 import android.telephony.SmsCbMessage; 23 24 import com.android.cellbroadcastreceiver.CellBroadcastReceiverApp; 25 26 import org.junit.After; 27 import org.junit.Before; 28 import org.junit.Test; 29 30 import java.lang.reflect.Method; 31 import java.util.ArrayList; 32 33 public class CellBroadcastReceiverAppTest extends CellBroadcastTest { 34 35 ArrayList<SmsCbMessage> mCachedMessages; 36 37 @Before setUp()38 public void setUp() throws Exception { 39 super.setUp(getClass().getSimpleName()); 40 mCachedMessages = new ArrayList<>(getNewMessageList()); 41 clearNewMessageList(); 42 } 43 44 @After tearDown()45 public void tearDown() throws Exception { 46 super.tearDown(); 47 clearNewMessageList(); 48 mCachedMessages.forEach(m -> addNewMessageToList(m)); 49 } 50 51 @Test testAddNewAndGetLatestMessage()52 public void testAddNewAndGetLatestMessage() throws Exception { 53 SmsCbMessage fm = getFakeMessage(1); 54 ArrayList<SmsCbMessage> ml = addNewMessageToList(fm); 55 56 assertEquals(1, ml.size()); 57 assertEquals(fm, ml.get(0)); 58 assertEquals(fm, getLatestMessage()); 59 } 60 61 @Test testAddNewAndGetNewMessageList()62 public void testAddNewAndGetNewMessageList() throws Exception { 63 ArrayList<SmsCbMessage> ml1 = new ArrayList<>(); 64 for (int i = 0; i < 3; i++) { 65 ml1.add(getFakeMessage(i)); 66 } 67 ml1.forEach(m -> addNewMessageToList(m)); 68 69 ArrayList<SmsCbMessage> ml2 = getNewMessageList(); 70 71 assertEquals(ml1.size(), ml2.size()); 72 assertTrue(ml1.containsAll(ml2)); 73 } 74 75 @Test testAddAndRemoveReadMessage()76 public void testAddAndRemoveReadMessage() throws Exception { 77 ArrayList<SmsCbMessage> ml1 = new ArrayList<>(); 78 for (int i = 0; i < 3; i++) { 79 ml1.add(getFakeMessage(i)); 80 } 81 ml1.forEach(m -> addNewMessageToList(m)); 82 83 for (int i = 0; i < 3; i++) { 84 ArrayList<SmsCbMessage> ml2 = removeReadMessage(ml1.get(i)); 85 assertTrue(!ml2.contains(ml1.get(i))); 86 } 87 } 88 getFakeMessage(int seq)89 private SmsCbMessage getFakeMessage(int seq) { 90 return new SmsCbMessage(1, 1, seq, null, 4379, "en", 0, "AMBER Alert: " + seq, 3, 91 null, null, 0, null, System.currentTimeMillis(), 1, 0); 92 } 93 clearNewMessageList()94 void clearNewMessageList() throws Exception { 95 Method method = CellBroadcastReceiverApp.class.getDeclaredMethod("clearNewMessageList"); 96 method.setAccessible(true); 97 method.invoke(null); 98 } 99 addNewMessageToList(SmsCbMessage message)100 ArrayList<SmsCbMessage> addNewMessageToList(SmsCbMessage message) { 101 Class[] args = new Class[1]; 102 args[0] = SmsCbMessage.class; 103 try { 104 Method method = CellBroadcastReceiverApp.class.getDeclaredMethod( 105 "addNewMessageToList", args); 106 method.setAccessible(true); 107 return (ArrayList<SmsCbMessage>) method.invoke(null, message); 108 } catch (Exception e) { 109 return null; 110 } 111 } 112 removeReadMessage(SmsCbMessage message)113 ArrayList<SmsCbMessage> removeReadMessage(SmsCbMessage message) throws Exception { 114 Class[] args = new Class[1]; 115 args[0] = SmsCbMessage.class; 116 Method method = CellBroadcastReceiverApp.class.getDeclaredMethod( 117 "removeReadMessage", args); 118 method.setAccessible(true); 119 return (ArrayList<SmsCbMessage>) method.invoke(null, message); 120 } 121 getLatestMessage()122 SmsCbMessage getLatestMessage() throws Exception { 123 Method method = CellBroadcastReceiverApp.class.getDeclaredMethod("getLatestMessage"); 124 method.setAccessible(true); 125 return (SmsCbMessage) method.invoke(null); 126 } 127 getNewMessageList()128 ArrayList<SmsCbMessage> getNewMessageList() throws Exception { 129 Method method = CellBroadcastReceiverApp.class.getDeclaredMethod("getNewMessageList"); 130 method.setAccessible(true); 131 return (ArrayList<SmsCbMessage>) method.invoke(null); 132 } 133 } 134