2003-08-13 14:59:24 +02:00
|
|
|
/**
|
|
|
|
*
|
2013-02-07 15:19:47 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smackx;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.*;
|
|
|
|
import org.jivesoftware.smack.packet.*;
|
2004-07-12 15:36:13 +02:00
|
|
|
import org.jivesoftware.smack.test.SmackTestCase;
|
2003-08-13 14:59:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Test the MessageEvent extension using the high level API.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2004-07-12 15:36:13 +02:00
|
|
|
public class MessageEventManagerTest extends SmackTestCase {
|
2003-10-15 16:55:19 +02:00
|
|
|
|
2003-08-13 14:59:24 +02:00
|
|
|
public MessageEventManagerTest(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* High level API test.
|
2018-05-09 23:06:12 +02:00
|
|
|
* This is a simple test to use with an XMPP client and check if the client receives the
|
2003-08-13 14:59:24 +02:00
|
|
|
* message
|
|
|
|
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
|
2018-05-09 23:06:12 +02:00
|
|
|
* occurs: offline, composing, displayed or delivered
|
2003-08-13 14:59:24 +02:00
|
|
|
*/
|
|
|
|
public void testSendMessageEventRequest() {
|
2003-10-15 16:55:19 +02:00
|
|
|
// Create a chat for each connection
|
2006-11-22 23:55:37 +01:00
|
|
|
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
|
2003-10-15 16:55:19 +02:00
|
|
|
|
|
|
|
// Create the message to send with the roster
|
2006-11-22 23:55:37 +01:00
|
|
|
Message msg = new Message();
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.setSubject("Any subject you want");
|
|
|
|
msg.setBody("An interesting body comes here...");
|
|
|
|
// Add to the message all the notifications requests (offline, delivered, displayed,
|
|
|
|
// composing)
|
|
|
|
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
|
|
|
|
|
|
|
|
// Send the message that contains the notifications request
|
|
|
|
try {
|
|
|
|
chat1.sendMessage(msg);
|
|
|
|
} catch (Exception e) {
|
2019-10-30 12:02:36 +01:00
|
|
|
fail("An error occurred sending the message");
|
2003-10-15 16:55:19 +02:00
|
|
|
}
|
|
|
|
}
|
2003-08-13 14:59:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* High level API test.
|
2018-05-09 23:06:12 +02:00
|
|
|
* This is a simple test to use with an XMPP client, check if the client receives the
|
2003-08-13 14:59:24 +02:00
|
|
|
* message and display in the console any notification
|
|
|
|
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
|
2018-05-09 23:06:12 +02:00
|
|
|
* occurs: offline, composing, displayed or delivered
|
2015-02-12 12:13:19 +01:00
|
|
|
* 2. User_2 will use an XMPP client (like Exodus) to display the message and compose a reply
|
2003-08-13 14:59:24 +02:00
|
|
|
* 3. User_1 will display any notification that receives
|
|
|
|
*/
|
|
|
|
public void testSendMessageEventRequestAndDisplayNotifications() {
|
2003-10-15 16:55:19 +02:00
|
|
|
// Create a chat for each connection
|
2006-11-22 23:55:37 +01:00
|
|
|
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
|
2003-10-15 16:55:19 +02:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
MessageEventManager messageEventManager = new MessageEventManager(getConnection(0));
|
2003-10-15 16:55:19 +02:00
|
|
|
messageEventManager
|
|
|
|
.addMessageEventNotificationListener(new MessageEventNotificationListener() {
|
|
|
|
public void deliveredNotification(String from, String packetID) {
|
|
|
|
System.out.println("From: " + from + " PacketID: " + packetID + "(delivered)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void displayedNotification(String from, String packetID) {
|
|
|
|
System.out.println("From: " + from + " PacketID: " + packetID + "(displayed)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void composingNotification(String from, String packetID) {
|
|
|
|
System.out.println("From: " + from + " PacketID: " + packetID + "(composing)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void offlineNotification(String from, String packetID) {
|
|
|
|
System.out.println("From: " + from + " PacketID: " + packetID + "(offline)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelledNotification(String from, String packetID) {
|
|
|
|
System.out.println("From: " + from + " PacketID: " + packetID + "(cancelled)");
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
2003-10-15 16:55:19 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Create the message to send with the roster
|
2006-11-22 23:55:37 +01:00
|
|
|
Message msg = new Message();
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.setSubject("Any subject you want");
|
|
|
|
msg.setBody("An interesting body comes here...");
|
|
|
|
// Add to the message all the notifications requests (offline, delivered, displayed,
|
|
|
|
// composing)
|
|
|
|
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
|
|
|
|
|
|
|
|
// Send the message that contains the notifications request
|
|
|
|
try {
|
|
|
|
chat1.sendMessage(msg);
|
|
|
|
// Wait a few seconds so that the XMPP client can send any event
|
|
|
|
Thread.sleep(200);
|
2003-08-13 14:59:24 +02:00
|
|
|
} catch (Exception e) {
|
2019-10-30 12:02:36 +01:00
|
|
|
fail("An error occurred sending the message");
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* High level API test.
|
|
|
|
* 1. User_1 will send a message to user_2 requesting to be notified when any of these events
|
2018-05-09 23:06:12 +02:00
|
|
|
* occurs: offline, composing, displayed or delivered
|
2003-08-13 14:59:24 +02:00
|
|
|
* 2. User_2 will receive the message
|
2018-05-09 23:06:12 +02:00
|
|
|
* 3. User_2 will simulate that the message was displayed
|
2003-08-13 14:59:24 +02:00
|
|
|
* 4. User_2 will simulate that he/she is composing a reply
|
|
|
|
* 5. User_2 will simulate that he/she has cancelled the reply
|
|
|
|
*/
|
|
|
|
public void testRequestsAndNotifications() {
|
2006-11-22 23:55:37 +01:00
|
|
|
final ArrayList<String> results = new ArrayList<String>();
|
|
|
|
ArrayList<String> resultsExpected = new ArrayList<String>();
|
2003-08-13 14:59:24 +02:00
|
|
|
resultsExpected.add("deliveredNotificationRequested");
|
|
|
|
resultsExpected.add("composingNotificationRequested");
|
|
|
|
resultsExpected.add("displayedNotificationRequested");
|
|
|
|
resultsExpected.add("offlineNotificationRequested");
|
|
|
|
resultsExpected.add("deliveredNotification");
|
|
|
|
resultsExpected.add("displayedNotification");
|
|
|
|
resultsExpected.add("composingNotification");
|
|
|
|
resultsExpected.add("cancelledNotification");
|
|
|
|
|
2003-10-15 16:55:19 +02:00
|
|
|
// Create a chat for each connection
|
2006-11-22 23:55:37 +01:00
|
|
|
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
|
2003-10-15 16:55:19 +02:00
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
MessageEventManager messageEventManager1 = new MessageEventManager(getConnection(0));
|
2003-10-15 16:55:19 +02:00
|
|
|
messageEventManager1
|
|
|
|
.addMessageEventNotificationListener(new MessageEventNotificationListener() {
|
|
|
|
public void deliveredNotification(String from, String packetID) {
|
|
|
|
results.add("deliveredNotification");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void displayedNotification(String from, String packetID) {
|
|
|
|
results.add("displayedNotification");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void composingNotification(String from, String packetID) {
|
|
|
|
results.add("composingNotification");
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|
2003-10-15 16:55:19 +02:00
|
|
|
|
|
|
|
public void offlineNotification(String from, String packetID) {
|
|
|
|
results.add("offlineNotification");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelledNotification(String from, String packetID) {
|
|
|
|
results.add("cancelledNotification");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
MessageEventManager messageEventManager2 = new MessageEventManager(getConnection(1));
|
2003-10-15 16:55:19 +02:00
|
|
|
messageEventManager2
|
|
|
|
.addMessageEventRequestListener(new DefaultMessageEventRequestListener() {
|
|
|
|
public void deliveredNotificationRequested(
|
|
|
|
String from,
|
|
|
|
String packetID,
|
|
|
|
MessageEventManager messageEventManager) {
|
|
|
|
super.deliveredNotificationRequested(from, packetID, messageEventManager);
|
|
|
|
results.add("deliveredNotificationRequested");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void displayedNotificationRequested(
|
|
|
|
String from,
|
|
|
|
String packetID,
|
|
|
|
MessageEventManager messageEventManager) {
|
|
|
|
super.displayedNotificationRequested(from, packetID, messageEventManager);
|
|
|
|
results.add("displayedNotificationRequested");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void composingNotificationRequested(
|
|
|
|
String from,
|
|
|
|
String packetID,
|
|
|
|
MessageEventManager messageEventManager) {
|
|
|
|
super.composingNotificationRequested(from, packetID, messageEventManager);
|
|
|
|
results.add("composingNotificationRequested");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void offlineNotificationRequested(
|
|
|
|
String from,
|
|
|
|
String packetID,
|
|
|
|
MessageEventManager messageEventManager) {
|
|
|
|
super.offlineNotificationRequested(from, packetID, messageEventManager);
|
|
|
|
results.add("offlineNotificationRequested");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create the message to send with the roster
|
2006-11-22 23:55:37 +01:00
|
|
|
Message msg = new Message();
|
2003-10-15 16:55:19 +02:00
|
|
|
msg.setSubject("Any subject you want");
|
|
|
|
msg.setBody("An interesting body comes here...");
|
|
|
|
// Add to the message all the notifications requests (offline, delivered, displayed,
|
|
|
|
// composing)
|
|
|
|
MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
|
|
|
|
|
|
|
|
// Send the message that contains the notifications request
|
|
|
|
try {
|
|
|
|
chat1.sendMessage(msg);
|
2015-02-05 11:17:27 +01:00
|
|
|
messageEventManager2.sendDisplayedNotification(getBareJID(0), msg.getStanzaId());
|
|
|
|
messageEventManager2.sendComposingNotification(getBareJID(0), msg.getStanzaId());
|
|
|
|
messageEventManager2.sendCancelledNotification(getBareJID(0), msg.getStanzaId());
|
2005-06-18 00:24:33 +02:00
|
|
|
// Wait up to 2 seconds
|
|
|
|
long initial = System.currentTimeMillis();
|
|
|
|
while (System.currentTimeMillis() - initial < 2000 &&
|
|
|
|
(!results.containsAll(resultsExpected))) {
|
|
|
|
Thread.sleep(100);
|
|
|
|
}
|
2003-10-15 16:55:19 +02:00
|
|
|
assertTrue(
|
|
|
|
"Test failed due to bad results (1)" + resultsExpected,
|
|
|
|
resultsExpected.containsAll(results));
|
|
|
|
assertTrue(
|
|
|
|
"Test failed due to bad results (2)" + results,
|
|
|
|
results.containsAll(resultsExpected));
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2019-10-30 12:02:36 +01:00
|
|
|
fail("An error occurred sending the message");
|
2003-10-15 16:55:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-12 15:36:13 +02:00
|
|
|
protected int getMaxConnections() {
|
|
|
|
return 2;
|
2003-10-15 16:55:19 +02:00
|
|
|
}
|
2003-08-13 14:59:24 +02:00
|
|
|
}
|