1 52 53 package org.jivesoftware.smackx; 54 55 import java.util.ArrayList ; 56 57 import org.jivesoftware.smack.*; 58 import org.jivesoftware.smack.packet.*; 59 import org.jivesoftware.smack.test.SmackTestCase; 60 61 67 public class MessageEventManagerTest extends SmackTestCase { 68 69 73 public MessageEventManagerTest(String name) { 74 super(name); 75 } 76 77 84 public void testSendMessageEventRequest() { 85 Chat chat1 = getConnection(0).createChat(getBareJID(1)); 87 88 Message msg = chat1.createMessage(); 90 msg.setSubject("Any subject you want"); 91 msg.setBody("An interesting body comes here..."); 92 MessageEventManager.addNotificationsRequests(msg, true, true, true, true); 95 96 try { 98 chat1.sendMessage(msg); 99 } catch (Exception e) { 100 fail("An error occured sending the message"); 101 } 102 } 103 104 113 public void testSendMessageEventRequestAndDisplayNotifications() { 114 Chat chat1 = getConnection(0).createChat(getBareJID(1)); 116 117 MessageEventManager messageEventManager = new MessageEventManager(getConnection(0)); 118 messageEventManager 119 .addMessageEventNotificationListener(new MessageEventNotificationListener() { 120 public void deliveredNotification(String from, String packetID) { 121 System.out.println("From: " + from + " PacketID: " + packetID + "(delivered)"); 122 } 123 124 public void displayedNotification(String from, String packetID) { 125 System.out.println("From: " + from + " PacketID: " + packetID + "(displayed)"); 126 } 127 128 public void composingNotification(String from, String packetID) { 129 System.out.println("From: " + from + " PacketID: " + packetID + "(composing)"); 130 } 131 132 public void offlineNotification(String from, String packetID) { 133 System.out.println("From: " + from + " PacketID: " + packetID + "(offline)"); 134 } 135 136 public void cancelledNotification(String from, String packetID) { 137 System.out.println("From: " + from + " PacketID: " + packetID + "(cancelled)"); 138 } 139 }); 140 141 Message msg = chat1.createMessage(); 143 msg.setSubject("Any subject you want"); 144 msg.setBody("An interesting body comes here..."); 145 MessageEventManager.addNotificationsRequests(msg, true, true, true, true); 148 149 try { 151 chat1.sendMessage(msg); 152 Thread.sleep(200); 154 } catch (Exception e) { 155 fail("An error occured sending the message"); 156 } 157 } 158 159 168 public void testRequestsAndNotifications() { 169 final ArrayList results = new ArrayList (); 170 ArrayList resultsExpected = new ArrayList (); 171 resultsExpected.add("deliveredNotificationRequested"); 172 resultsExpected.add("composingNotificationRequested"); 173 resultsExpected.add("displayedNotificationRequested"); 174 resultsExpected.add("offlineNotificationRequested"); 175 resultsExpected.add("deliveredNotification"); 176 resultsExpected.add("displayedNotification"); 177 resultsExpected.add("composingNotification"); 178 resultsExpected.add("cancelledNotification"); 179 180 Chat chat1 = getConnection(0).createChat(getBareJID(1)); 182 183 MessageEventManager messageEventManager1 = new MessageEventManager(getConnection(0)); 184 messageEventManager1 185 .addMessageEventNotificationListener(new MessageEventNotificationListener() { 186 public void deliveredNotification(String from, String packetID) { 187 results.add("deliveredNotification"); 188 } 189 190 public void displayedNotification(String from, String packetID) { 191 results.add("displayedNotification"); 192 } 193 194 public void composingNotification(String from, String packetID) { 195 results.add("composingNotification"); 196 } 197 198 public void offlineNotification(String from, String packetID) { 199 results.add("offlineNotification"); 200 } 201 202 public void cancelledNotification(String from, String packetID) { 203 results.add("cancelledNotification"); 204 } 205 }); 206 207 MessageEventManager messageEventManager2 = new MessageEventManager(getConnection(1)); 208 messageEventManager2 209 .addMessageEventRequestListener(new DefaultMessageEventRequestListener() { 210 public void deliveredNotificationRequested( 211 String from, 212 String packetID, 213 MessageEventManager messageEventManager) { 214 super.deliveredNotificationRequested(from, packetID, messageEventManager); 215 results.add("deliveredNotificationRequested"); 216 } 217 218 public void displayedNotificationRequested( 219 String from, 220 String packetID, 221 MessageEventManager messageEventManager) { 222 super.displayedNotificationRequested(from, packetID, messageEventManager); 223 results.add("displayedNotificationRequested"); 224 } 225 226 public void composingNotificationRequested( 227 String from, 228 String packetID, 229 MessageEventManager messageEventManager) { 230 super.composingNotificationRequested(from, packetID, messageEventManager); 231 results.add("composingNotificationRequested"); 232 } 233 234 public void offlineNotificationRequested( 235 String from, 236 String packetID, 237 MessageEventManager messageEventManager) { 238 super.offlineNotificationRequested(from, packetID, messageEventManager); 239 results.add("offlineNotificationRequested"); 240 } 241 }); 242 243 Message msg = chat1.createMessage(); 245 msg.setSubject("Any subject you want"); 246 msg.setBody("An interesting body comes here..."); 247 MessageEventManager.addNotificationsRequests(msg, true, true, true, true); 250 251 try { 253 chat1.sendMessage(msg); 254 messageEventManager2.sendDisplayedNotification(getBareJID(0), msg.getPacketID()); 255 messageEventManager2.sendComposingNotification(getBareJID(0), msg.getPacketID()); 256 messageEventManager2.sendCancelledNotification(getBareJID(0), msg.getPacketID()); 257 long initial = System.currentTimeMillis(); 259 while (System.currentTimeMillis() - initial < 2000 && 260 (!results.containsAll(resultsExpected))) { 261 Thread.sleep(100); 262 } 263 assertTrue( 264 "Test failed due to bad results (1)" + resultsExpected, 265 resultsExpected.containsAll(results)); 266 assertTrue( 267 "Test failed due to bad results (2)" + results, 268 results.containsAll(resultsExpected)); 269 270 } catch (Exception e) { 271 fail("An error occured sending the message"); 272 } 273 } 274 275 protected int getMaxConnections() { 276 return 2; 277 } 278 } 279 | Popular Tags |