1 52 53 package org.jivesoftware.smackx; 54 55 import org.jivesoftware.smack.Chat; 56 import org.jivesoftware.smack.XMPPException; 57 import org.jivesoftware.smack.packet.Message; 58 import org.jivesoftware.smack.test.SmackTestCase; 59 60 65 public class FormTest extends SmackTestCase { 66 67 71 public FormTest(String arg0) { 72 super(arg0); 73 } 74 75 80 public void testFilloutForm() { 81 Form formToSend = new Form(Form.TYPE_FORM); 82 formToSend.setInstructions( 83 "Fill out this form to report your case.\nThe case will be created automatically."); 84 formToSend.setTitle("Case configurations"); 85 FormField field = new FormField("hidden_var"); 87 field.setType(FormField.TYPE_HIDDEN); 88 field.addValue("Some value for the hidden variable"); 89 formToSend.addField(field); 90 field = new FormField(); 92 field.addValue("Section 1: Case description"); 93 formToSend.addField(field); 94 field = new FormField("name"); 96 field.setLabel("Enter a name for the case"); 97 field.setType(FormField.TYPE_TEXT_SINGLE); 98 formToSend.addField(field); 99 field = new FormField("description"); 101 field.setLabel("Enter a description"); 102 field.setType(FormField.TYPE_TEXT_MULTI); 103 formToSend.addField(field); 104 field = new FormField("time"); 106 field.setLabel("Is this your first case?"); 107 field.setType(FormField.TYPE_BOOLEAN); 108 formToSend.addField(field); 109 field = new FormField("age"); 111 field.setLabel("How old are you?"); 112 field.setType(FormField.TYPE_TEXT_SINGLE); 113 formToSend.addField(field); 114 115 Chat chat = getConnection(0).createChat(getBareJID(1)); 117 Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat.getThreadID()); 118 119 Message msg = chat.createMessage(); 120 msg.setBody("To enter a case please fill out this form and send it back to me"); 121 msg.addExtension(formToSend.getDataFormToSend()); 122 123 try { 124 chat.sendMessage(msg); 126 127 Message msg2 = chat2.nextMessage(2000); 129 Form formToRespond = Form.getFormFrom(msg2); 131 assertNotNull(formToRespond); 132 assertNotNull(formToRespond.getField("name")); 133 assertNotNull(formToRespond.getField("description")); 134 Form completedForm = formToRespond.createAnswerForm(); 136 assertNotNull(completedForm.getField("hidden_var")); 137 try { 139 completedForm.setAnswer("name", true); 140 fail("A boolean value was set to a field of type String"); 141 } 142 catch (IllegalArgumentException e) { 143 } 144 completedForm.setAnswer("name", "Credit card number invalid"); 145 completedForm.setAnswer( 146 "description", 147 "The ATM says that my credit card number is invalid. What's going on?"); 148 completedForm.setAnswer("time", true); 149 completedForm.setAnswer("age", 20); 150 msg2 = chat2.createMessage(); 152 msg2.setBody("To enter a case please fill out this form and send it back to me"); 153 msg2.addExtension(completedForm.getDataFormToSend()); 155 chat2.sendMessage(msg2); 157 158 Message msg3 = chat.nextMessage(2000); 160 completedForm = Form.getFormFrom(msg3); 162 assertNotNull(completedForm); 163 assertNotNull(completedForm.getField("name")); 164 assertNotNull(completedForm.getField("description")); 165 assertEquals( 166 completedForm.getField("name").getValues().next(), 167 "Credit card number invalid"); 168 assertNotNull(completedForm.getField("time")); 169 assertNotNull(completedForm.getField("age")); 170 assertEquals("The age is bad", "20", completedForm.getField("age").getValues().next()); 171 172 } 173 catch (XMPPException ex) { 174 fail(ex.getMessage()); 175 } 176 } 177 178 protected int getMaxConnections() { 179 return 2; 180 } 181 182 } 183 | Popular Tags |