blob: a823b7217c631f20f5f9a3d0a9576a3d443c6c89 [file] [log] [blame]
Simon Hunt584122a2015-01-21 15:32:40 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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/*
18 ONOS GUI -- Remote -- Web Socket Service - Unit Tests
19 */
20describe('factory: fw/remote/websocket.js', function () {
21 var $log, fs, wss;
22
23 beforeEach(module('onosRemote'));
24
25 beforeEach(module(function($provide) {
26 $provide.factory('$location', function (){
27 return {
28 protocol: function () { return 'http'; },
29 host: function () { return 'foo'; },
30 port: function () { return '80'; }
31 };
32 })
33 }));
34
35 beforeEach(inject(function (_$log_, FnService, WebSocketService) {
36 $log = _$log_;
37 fs = FnService;
38 wss = WebSocketService;
39 }));
40
41
42 it('should define WebSocketService', function () {
43 expect(wss).toBeDefined();
44 });
45
46 it('should define api functions', function () {
47 expect(fs.areFunctions(wss, [
48 'createWebSocket'
49 ])).toBeTruthy();
50 });
51
52 it('should use the appropriate URL', function () {
53 var ws = wss.createWebSocket('foo/path');
54 expect(ws.meta.path).toEqual('ws://foo:80/onos/ui/ws/foo/path');
55 });
56
Simon Huntacf410b2015-01-23 10:05:48 -080057 it('should use the appropriate URL with modified port', function () {
58 var ws = wss.createWebSocket('foo/path', { wsport: 1243 });
59 expect(ws.meta.path).toEqual('ws://foo:1243/onos/ui/ws/foo/path');
60 });
61
62 var oCalled, mCalled, cCalled, json, reason;
63
64 function oo() {
65 oCalled++;
66 }
67 function om(j) {
68 mCalled++;
69 json = j;
70 }
71 function oc(r) {
72 cCalled++;
73 reason = r;
74 }
75
76 function resetCounters() {
77 oCalled = mCalled = cCalled = 0;
78 json = reason = null;
79 }
80
81 function validateCallbacks(ws, op, msg, cl) {
82 // we have to cheat a little, by digging into the websocket structure
83 var onO = fs.isF(ws.meta.ws.onopen),
84 onM = fs.isF(ws.meta.ws.onmessage),
85 onC = fs.isF(ws.meta.ws.onclose);
86
87 expect(!!onO).toEqual(op);
88 expect(!!onM).toEqual(msg);
89 expect(!!onC).toEqual(cl);
90
91 onO && onO({});
92 onM && onM({ data: '{ "item": "ivalue" }'});
93 onC && onC({ reason: 'rvalue' });
94
95 expect(oCalled).toEqual(op ? 1 : 0);
96 expect(mCalled).toEqual(msg ? 1 : 0);
97 expect(cCalled).toEqual(cl ? 1 : 0);
98
99 expect(json).toEqual(msg ? { item: 'ivalue' } : null);
100 expect(reason).toEqual(cl ? 'rvalue' : null);
101 }
102
Simon Hunt584122a2015-01-21 15:32:40 -0800103 it('should install the appropriate callbacks', function () {
Simon Huntacf410b2015-01-23 10:05:48 -0800104 resetCounters();
Simon Hunt584122a2015-01-21 15:32:40 -0800105
106 var ws = wss.createWebSocket('foo', {
107 onOpen: oo,
108 onMessage: om,
109 onClose: oc
110 });
111
Simon Huntacf410b2015-01-23 10:05:48 -0800112 validateCallbacks(ws, true, true, true);
Simon Hunt584122a2015-01-21 15:32:40 -0800113 });
114
115 it('should install partial callbacks', function () {
Simon Huntacf410b2015-01-23 10:05:48 -0800116 resetCounters();
Simon Hunt584122a2015-01-21 15:32:40 -0800117
118 var ws = wss.createWebSocket('foo', {
119 onOpen: oo,
120 onMessage: om
121 });
122
Simon Huntacf410b2015-01-23 10:05:48 -0800123 validateCallbacks(ws, true, true, false);
Simon Hunt584122a2015-01-21 15:32:40 -0800124 });
125
Simon Huntacf410b2015-01-23 10:05:48 -0800126 it('should install no callbacks', function () {
127 resetCounters();
Simon Hunt584122a2015-01-21 15:32:40 -0800128
Simon Huntacf410b2015-01-23 10:05:48 -0800129 var ws = wss.createWebSocket('foo');
130
131 validateCallbacks(ws, false, false, false);
132 });
133
134 // can't really test send without faking out the WebSocket.
135/*
136 it('should stringify objects for sending', function () {
137 var ws = wss.createWebSocket('foo');
138 ws.send({ item: 'itemVal' });
139
140 // what to assert?
141 });
142*/
143
144 it('should remove websocket reference on close', function () {
145 var ws = wss.createWebSocket('foo');
146 expect(ws.meta.ws instanceof WebSocket).toBeTruthy();
147
148 ws.close();
149 expect(ws.meta.ws).toBeNull();
150 });
Simon Hunt584122a2015-01-21 15:32:40 -0800151});