source: sandbox/jabberit_messenger/trophy_expresso/strophejs/tests/pubsub.js @ 2397

Revision 2397, 9.4 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Importacao do modulo trophy integrado ao expresso.

  • Property svn:executable set to *
Line 
1
2
3Strophe.Test = {
4    BOSH_URL: "/xmpp-httpbind",
5    XMPP_DOMAIN: 'localhost',
6    PUBSUB_COMPONENT: "pubsub.localhost",
7    _node_name: "", //node name created in connectCallback function
8    connection: null, //connection object created in run function
9   
10    run: function() {
11        $(document).ready(function(){
12            //Connect strophe, uses localhost to test
13            Strophe.Test.connection = new Strophe.Connection(Strophe.Test.BOSH_URL);
14           
15            //connect anonymously to run most tests
16            Strophe.Test.connection.connect(Strophe.Test.XMPP_DOMAIN,
17                                            null,
18                                            Strophe.Test.connectCallback);
19
20            //set up the test client UI
21            $("#disconnect").click(function() {
22                Strophe.Test.connection.disconnect();
23            });
24            $("#run_tests").click(function() {     
25                test("Anonymous connection test.", function() {
26                    if(Strophe.Test.connection.connected)
27                    {
28                        ok( true, "all good");
29                    }
30                    else
31                    {
32                        ok( false, "not connected anonymously");
33                    }
34                });
35
36                test("Create default node test.",function(){
37                    var iqid =  Strophe.Test.connection.pubsub
38                        .createNode(Strophe.Test.connection.jid,
39                                    Strophe.Test.PUBSUB_COMPONENT,
40                                    Strophe.Test._node_name, {},
41                                    function(stanza) {
42                                        test("handled create node.",
43                                             function() {
44                                                 var error = $(stanza).find("error");
45                                                 
46                                                 if(error.length == 0)
47                                                 {
48                                                     ok(true, "returned");
49                                                 }
50                                                 else
51                                                 {
52                                                     ok(false,"error creating node.");
53                                                 }
54                                             });
55                                    });
56                    ok(true,"sent create request. "+ iqid);
57                });
58
59                test("subscribe to a node",function() {
60                    var iqid = Strophe.Test.connection.pubsub
61                        .subscribe(Strophe.Test.connection.jid,
62                                   Strophe.Test.PUBSUB_COMPONENT,
63                                   Strophe.Test._node_name,
64                                   [],
65                                   function(stanza) {
66                                       test("items received", function() {
67                                           console.log(stanza);
68                                           if($(stanza).length > 0)
69                                           {
70                                               ok(true,"item received.");
71                                           }
72                                           else
73                                           {
74                                               ok(false,"no items.");
75                                           }
76                                       });
77                                   },
78                                   function(stanza) {
79                                       var error = $(stanza).find("error");
80                                       
81                                       test("handled subscribe",
82                                            function() {
83                                                if(error.length == 0)
84                                                {
85                                                    ok(true,"subscribed");
86                                                }
87                                                else
88                                                {
89                                                    console.log(error.get(0));
90                                                    ok(false,
91                                                       "not subscribed");
92                                                }
93                                               
94                                            });
95                                   });
96                   
97                    if(iqid)
98                        ok(true,
99                           "subscribed to " + Strophe.Test._node_name);
100                });
101
102                test("publish to a node",function() {
103                    var iqid = Strophe.Test.connection.pubsub
104                        .publish(Strophe.Test.connection.jid,
105                                 Strophe.Test.PUBSUB_COMPONENT,
106                                 Strophe.Test._node_name,
107                                 {test:'test'},
108                                 function(stanza) {
109                                     var error = $(stanza).find("error");
110                                     
111                                     test("handled published item",
112                                          function() {
113                                              if(error.length == 0)
114                                              {
115                                                  ok(true,"got item");
116                                              }
117                                              else
118                                              {
119                                                  ok(false,
120                                                     "no item");
121                                              }
122                                             
123                                          });
124                                 });
125                   
126                    if(iqid)
127                        ok(true,
128                           "published to " + Strophe.Test._node_name);
129                   
130                });             
131
132                test("subscribe to a node with options",function() {
133                    var keyword_elem = Strophe.xmlElement("field",
134                                                          [["var",
135                                                            "http://stanziq.com/search#keyword"],
136                                                           ["type",
137                                                            'text-single'],
138                                                           ["label",
139                                                            "keyword to match"]]);
140                    var value = Strophe.xmlElement("value",[]);
141                    var text = Strophe.xmlTextNode("crazy");
142                    value.appendChild(text);
143                    keyword_elem.appendChild(value);
144                   
145                    var iqid = Strophe.Test.connection.pubsub
146                        .subscribe(Strophe.Test.connection.jid,
147                                   Strophe.Test.PUBSUB_COMPONENT,
148                                   Strophe.Test._node_name,
149                                   [keyword_elem],
150                                   function(stanza) {console.log(stanza);},
151                                   function(stanza) {
152                                       
153                                       var error = $(stanza).find("error");
154                                       
155                                       test("handled subscribe with options",
156                                            function() {
157                                                if(error.length == 0)
158                                                {
159                                                    ok(true,"search subscribed");
160                                                }
161                                                else
162                                                {
163                                                    console.log(error.get(0));
164                                                    ok(false,
165                                                       "search not subscribed");
166                                                }
167                                               
168                                            });
169                                   });
170                   
171                    if(iqid)
172                        ok(true,
173                           "subscribed to search");
174                });
175
176                test("unsubscribe to a node",function() {
177                    var iqid = Strophe.Test.connection.pubsub
178                        .unsubscribe(Strophe.Test.connection.jid,
179                                     Strophe.Test.PUBSUB_COMPONENT,
180                                     Strophe.Test._node_name,
181                                     function(stanza) {
182                                         var error = $(stanza).find("error");
183                                         
184                                         test("handled unsubscribe",
185                                              function() {
186                                                  if(error.length == 0)
187                                                  {
188                                                      ok(true,"unsubscribed");
189                                                  }
190                                                  else
191                                                  {
192                                                      console.log(error.get(0));
193                                                      ok(false,
194                                                         "unable to unsubscribed");
195                                                  }
196                                              });
197                                     });
198                   
199                    if(iqid)
200                        ok(true,
201                           "unsubscribed from search with no options.");
202                });
203
204                test("test items retrieval",function(){
205                    var itemid = Strophe.Test.connection.pubsub
206                        .items(Strophe.Test.connection.jid,
207                               Strophe.Test.PUBSUB_COMPONENT,
208                               Strophe.Test._node_name,
209                               function(stanza) {
210                                   ok(true,"item request successful.");
211                               },
212                               function(stanza) {
213                                   ok(false,"failed to send request.");
214                               });
215                   
216                    if(itemid)
217                    {
218                        ok(true,"item request sent.");
219                    }
220                });
221
222                test("test sendIQ interface.",function(){
223                    var sendiq_good = false;
224                    //setup timeout for sendIQ for 3 seconds
225                    setTimeout(function() {
226                        test("Timeout check", function () {
227                            ok(sendiq_good, "The iq didn't timeout.");
228                        });
229                    }, 3000);
230                   
231                    //send a pubsub subscribe stanza
232                   
233                    var sub = $iq({from:Strophe.Test.connection.jid,
234                                   to:Strophe.Test.PUBSUB_COMPONENT,
235                                   type:'set'})
236                        .c('pubsub', { xmlns:Strophe.NS.PUBSUB })
237                        .c('subscribe',
238                           {node:Strophe.Test._node_name,
239                            jid:Strophe.Test.connection.jid});
240                    var stanza=sub.tree();
241                    //call sendIQ with several call backs
242                    Strophe.Test.connection
243                        .sendIQ(stanza,
244                                function(stanza) {
245                                    test("iq sent",function() {
246                                        sendiq_good = true;
247                                        ok(true,"iq sent succesfully.");
248                                    });
249                                },
250                                function(stz) {
251                                    test("iq fail",function() {
252                                        if (stz)
253                                            sendiq_good = true;
254                                        console.log(stanza);
255                                        ok(true,"failed to send iq.");
256                                    });
257                                });
258                });
259
260                test("test sendIQ failed.",function(){
261                    var sub = $iq({from:Strophe.Test.connection.jid,
262                                   to:Strophe.Test.PUBSUB_COMPONENT,
263                                   type:'get'});
264
265                    //call sendIQ with several call backs
266                    Strophe.Test.connection
267                        .sendIQ(sub.tree(),
268                                function(stanza) {
269                                    console.log(stanza);
270                                    test("iq sent",function() {
271                                        ok(false,
272                                           "iq sent succesfully when should have failed.");
273                                    });
274                                },
275                                function(stanza) {
276                                    test("iq fail",function() {
277                                        ok(true,
278                                           "success on failure test: failed to send iq.");
279                                    });
280                                });                 
281                });
282            });
283        });
284    },
285
286    connectCallback: function(status,cond) {
287        var error_message = null;
288        if(status == Strophe.Status.CONNECTED)
289        {
290            $('#run_tests').show();
291            $('#disconnect').show();
292            var bare_jid = Strophe.getBareJidFromJid(Strophe.Test.connection.jid).split("@")[0];
293            Strophe.Test._node_name = "/home/"+Strophe.Test.XMPP_DOMAIN+"/"+bare_jid;
294        }
295        else if (status == Strophe.Status.DISCONNECTED || status == Strophe.Status.DICONNECTING)
296        {
297            $('#run_tests').hide();
298            $('#disconnect').hide();
299        }       
300        else if ((status == 0) || (status == Strophe.Status.CONNFAIL))
301        {
302            error_message = "Failed to connect to xmpp server.";
303        }
304        else if (status == Strophe.Status.AUTHFAIL)
305        {
306            error_message = "Failed to authenticate to xmpp server.";
307        }
308        if(error_message)
309        {
310            $('published_item').text(error_message);
311           
312        }
313    }
314};
Note: See TracBrowser for help on using the repository browser.