Merge remote-tracking branch 'origin/master' into wheelsource
[profile/ivi/automotive-message-broker.git] / plugins / websocketsink / test / test.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 /* --------------------------- utility code ------------------------------- */
11
12 var PRINT = {
13     logElement : null,
14     init : function(log_id) {
15         this.logElement = document.getElementById(log_id);
16     },
17
18     scrollToBottom : function() {
19         this.logElement.scrollTop = this.logElement.scrollHeight;
20     },
21
22     clear : function() {
23         this.logElement.innerHTML = "";
24     },
25
26     pass : function(msg) {
27         this.logElement.innerHTML += "<div class='PassClass'>PASS: " + msg + "</div>";
28     },
29
30     fail : function(msg) {
31         this.logElement.innerHTML += "<div class='FailClass'>FAIL: " + msg + "</div>";
32         this.scrollToBottom();
33     },
34
35     log : function(msg) {
36         this.logElement.innerHTML += "<div class='LogClass'> " + msg + "</div>";
37     },
38 }
39
40 /* ----------------------------- test code --------------------------------- */
41
42 function getTypes(event)
43 {
44     var types = window.vehicle.getSupportedEventTypes(event, false,
45         function(data) {
46             PRINT.clear();
47             if(data && data.length > 1)
48             {
49                 PRINT.pass(event+" is a set of "+data.length+" events:");
50                 for(i in data)
51                 {
52                     PRINT.log(data[i]);
53                 }
54             }
55             else if(data && data.length > 0)
56             {
57                 PRINT.pass(event+" is a single event:");
58                 for(i in data)
59                 {
60                     PRINT.log(data[i]);
61                 }
62             }
63             else
64             {
65                 PRINT.fail(event+" unexcepted empty data field");
66             }
67         },
68         function(msg) {
69             PRINT.fail(((event === "")?"all events":event)+":<br>"+msg);
70         }
71     );
72 }
73
74 function getValue(event)
75 {
76     var types = window.vehicle.get(event,
77         function(data) {
78             PRINT.clear();
79             if(data && data.length > 0)
80             {
81                 PRINT.pass(event+" values received");
82                 for(i in data)
83                 {
84                     PRINT.log(data[i].name+": "+data[i].value);
85                 }
86             }
87             else
88             {
89                 PRINT.fail("no values retrieved for "+event);
90             }
91         },
92         function(msg) {
93             PRINT.fail(((event === "")?"all events":event)+":<br>"+msg);
94         }
95     );
96 }
97
98 function setValue(event)
99 {
100     var types = window.vehicle.set(event, 23,
101         function(data) {
102             PRINT.clear();
103             if(data && data.property && data.value)
104             {
105                 PRINT.pass(data.property+" value changes to "+data.value);
106             }
107             else
108             {
109                 PRINT.fail("bad response for set "+event);
110             }
111         },
112         function(msg) {
113             PRINT.fail(((event === "")?"all events":event)+":<br>"+msg);
114         }
115     );
116 }
117
118 function start(msg)
119 {
120     if(window.vehicle && window.vehicle.getSupportedEventTypes)
121     {
122         PRINT.pass("vehicle interface online "+msg);
123     }
124     else
125     {
126         PRINT.fail("vehicle interface not found");
127         return;
128     }
129
130     var vehicleEventType = new VehicleEventType();
131     var tester = document.getElementById("tester");
132     var part1 = '<div class="proptest">';
133     var part2 = '<div class="buttons"><div class="testbutton types" onclick=getTypes("';
134     var part3 = '")></div><div class="testbutton get" onclick=getValue("'
135     var part4 = '")></div><div class="testbutton set" onclick=setValue("'
136     var part5 = '")></div></div></div>';
137     var events = vehicleEventType.event;
138     var html = part1 + "all events" + part2 + part3 + part4 + part5;
139     for(i in events)
140     {
141         html += part1 + events[i] + part2 + events[i] + 
142                 part3 + events[i] + part4 + events[i] +
143                 part5;
144     }
145     tester.innerHTML = html;
146 }
147
148 function error(msg)
149 {
150     PRINT.fail(msg);
151 }
152
153 function init(url, protocol) {
154     PRINT.init("result");
155     window.vehicle = new Vehicle(start, error, url, protocol);
156 }