PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/test_framework.js

https://github.com/runeh/dragonfly-stp-1
JavaScript | 448 lines | 365 code | 31 blank | 52 comment | 57 complexity | 76ab860b4e9a6fc0b67b230aaa733c7e MD5 | raw file
  1. /**
  2. * (This file was autogenerated by hob)
  3. *
  4. * @fileoverview
  5. * fixme: add fileoverview
  6. */
  7. window.cls || ( window.cls = {} );
  8. /**
  9. * @constructor
  10. */
  11. window.cls.TestFramework = function()
  12. {
  13. /**
  14. * class to inspect and test the scope interface
  15. *
  16. * the class expects the following markup:
  17. *
  18. * <ul id="window-list"></ul> optional
  19. *
  20. * <ul id="service-list">
  21. * <li>Scope</li>
  22. * <li>WindowManager</li>
  23. * <li>EcmascriptDebugger</li>
  24. * <li>HttpLogger</li>
  25. * etc
  26. * </ul>
  27. *
  28. * <ul id="command-list"></ul>
  29. *
  30. * <ul id="event-list"></ul>
  31. *
  32. * <div id="message-container"></div>
  33. *
  34. * the two methods `get_bound_click_handler`
  35. * and `get_bound_change_handler` must be set to
  36. * an appropriated event listeners
  37. *
  38. */
  39. /* interface */
  40. this.get_bound_click_handler = function(){};
  41. this.get_bound_change_handler = function(){};
  42. /**
  43. * to clear the log entries
  44. */
  45. this.clear_log = function(){};
  46. this.rebuild_last_state = function(){};
  47. this.rebuild_last_state = function(){};
  48. /* privat */
  49. this._selected_service = "";
  50. this._status_map = cls.ServiceBase.get_status_map();
  51. this._event_map = cls.ServiceBase.get_event_map();
  52. this._service_descriptions = null;
  53. const INDENT = " ", RESPONSE = 2;
  54. this._get_indent = function(count)
  55. {
  56. var ret = "";
  57. while(count-- > 0)
  58. {
  59. ret += INDENT;
  60. }
  61. return ret;
  62. }
  63. this._pretty_print_payload_item = function(indent, name, definition, item)
  64. {
  65. return (
  66. this._get_indent(indent) +
  67. name + ': ' + (
  68. item === null && "null" ||
  69. "message" in definition && "\n" +
  70. this._pretty_print_payload(item, definition["message"], indent + 1) ||
  71. "enum" in definition && item in definition["enum"]["numbers"] && (definition["enum"]["numbers"][item] + " (" + item + ")") ||
  72. "type" in definition && definition["type"] == 11 /*bool*/ && (item ? "true" : "false") ||
  73. typeof item == "string" && "\"" + item + "\"" ||
  74. item));
  75. }
  76. this._pretty_print_payload = function(payload, definitions, indent)
  77. {
  78. var
  79. ret = [],
  80. item = null,
  81. i = 0,
  82. definition = null,
  83. j = 0;
  84. //# TODO message: self
  85. if (definitions)
  86. {
  87. for (; i < payload.length; i++)
  88. {
  89. item = payload[i];
  90. if (item === null)
  91. {
  92. item = [];
  93. }
  94. if (definition = definitions[i])
  95. {
  96. if (definition["q"] == "repeated")
  97. {
  98. ret.push(this._get_indent(indent) + definition['name'] + ':');
  99. for( j = 0; j < item.length; j++)
  100. {
  101. ret.push(this._pretty_print_payload_item(
  102. indent + 1,
  103. definition['name'].replace("List", ""),
  104. definition,
  105. item[j]));
  106. }
  107. }
  108. else
  109. {
  110. ret.push(this._pretty_print_payload_item(
  111. indent,
  112. definition['name'],
  113. definition,
  114. item))
  115. }
  116. }
  117. else
  118. {
  119. throw Error("PrettyPrintError. Probably invalid message. " +
  120. "payload: " + JSON.stringify(payload));
  121. }
  122. }
  123. return ret.join("\n")
  124. }
  125. return "";
  126. }
  127. // handle a command response
  128. this._handle_response = function(status, message, service, command)
  129. {
  130. this._last_message = Array.prototype.slice.call(arguments, 0);
  131. var response = document.getElementById('message-response');
  132. if(response)
  133. {
  134. response.parentNode.removeChild(response);
  135. }
  136. response =
  137. document.getElementById('message-container').
  138. appendChild(document.createElement('pre'));
  139. response.id = 'message-response';
  140. service = this._dashed_name(service);
  141. var command_id = this._event_map[service].indexOf('handle' + command);
  142. var definitions =
  143. window.message_maps[service] &&
  144. window.message_maps[service][command_id] &&
  145. window.message_maps[service][command_id][RESPONSE] ||
  146. null;
  147. if (status != 0) // Use the error structure if we received an error response
  148. definitions = window.package_map["com.opera.stp"]["Error"];
  149. var payload = "";
  150. try
  151. {
  152. payload = (cookies.get('pretty-print-message') == 'true' && definitions ?
  153. this._pretty_print_payload(message, definitions, 2) :
  154. JSON.stringify(message));
  155. }
  156. catch (e)
  157. {
  158. payload = JSON.stringify(message);
  159. }
  160. response.textContent =
  161. "response:\n status: " +
  162. this._status_map[status] + "\n" +
  163. " payload: \n" +
  164. payload;
  165. }
  166. // to highlight the selected element in a list
  167. this._update_selected = function(container, target)
  168. {
  169. var selected = container && container.getElementsByClassName('selected')[0];
  170. if(selected)
  171. {
  172. selected.className = selected.className.replace(/ ?selected/, '');
  173. }
  174. if(target)
  175. {
  176. target.className += (target.className ? ' ' : '') + 'selected';
  177. }
  178. }
  179. // to upadte the Command and Evenet List
  180. this._update_list = function(name, list, target)
  181. {
  182. var list_ele = document.getElementById(name.toLowerCase() + '-list-container');
  183. list_ele.innerHTML =
  184. "<h2>" + name + " List</h2><ul id='" + name.toLowerCase() + "-list'>" +
  185. list.map(this._list_item).join("") +
  186. "</ul>";
  187. }
  188. // callback to display a message definition
  189. this._show_def = function(xhr, context)
  190. {
  191. context.pre.innerHTML = xhr.responseText;
  192. }
  193. // to re-select entries of the Service or Command List
  194. this._reselect_element = function(/* list of names */)
  195. {
  196. var
  197. name = '',
  198. i = 0,
  199. j = 0,
  200. last_selected = '',
  201. lis = null,
  202. li = null;
  203. for( ; name = arguments[i]; i++)
  204. {
  205. if(last_selected = cookies.get(name))
  206. {
  207. lis = document.getElementById(name).getElementsByTagName('li');
  208. for( j = 0; ( li = lis[j] ) && li.textContent != last_selected; j++);
  209. if(li)
  210. {
  211. this._click_handler({target: li});
  212. }
  213. }
  214. }
  215. }
  216. // to re-select the last selected service and command or event
  217. // and display the according message definitions
  218. this.rebuild_last_state = function()
  219. {
  220. this._reselect_element('service-list', 'command-list');
  221. }
  222. // basic helpers
  223. // map argument
  224. this._list_item = function(name)
  225. {
  226. return "<li>" + name + "</li>";
  227. }
  228. this._dashed_name = function(name)
  229. {
  230. for ( var cur = '', i = 0, ret = ''; cur = name[i]; i++)
  231. {
  232. ret += /[A-Z]/.test(cur) && ( i ? '-' : '' ) + cur.toLowerCase() || cur;
  233. }
  234. return ret;
  235. }
  236. this._make_service_descriptions = function()
  237. {
  238. var
  239. map = window.message_maps,
  240. service_name = '',
  241. command_id = '',
  242. servive = null,
  243. commands = null,
  244. events = null;
  245. this._service_descriptions = {commands:{}, events: {}};
  246. for (service_name in map)
  247. {
  248. service = map[service_name];
  249. service_name = window.app.helpers.dash_to_class_name(service_name);
  250. commands = this._service_descriptions.commands[service_name] = [];
  251. events = this._service_descriptions.events[service_name] = [];
  252. for (command_id in service)
  253. (1 in service[command_id] && commands || events).push(service[command_id].name);
  254. commands.sort();
  255. events.sort();
  256. }
  257. }
  258. // generic click event handler
  259. this._click_handler = function(event)
  260. {
  261. var target = event.target;
  262. if (!this._service_descriptions)
  263. {
  264. this._make_service_descriptions();
  265. }
  266. if(!this._doc_base_uri)
  267. {
  268. this._doc_base_uri =
  269. document.getElementsByTagName('base')[0] ?
  270. document.baseURI :
  271. './';
  272. }
  273. while (target && !target.id && (target = target.parentNode));
  274. if (target)
  275. {
  276. switch (target.id)
  277. {
  278. case 'window-list':
  279. {
  280. this._update_selected(target.parentNode, event.target);
  281. windows.set_debug_context(parseInt(event.target.getAttribute('data-window-id')));
  282. break;
  283. }
  284. case 'service-list':
  285. {
  286. this._update_selected(target, event.target);
  287. document.getElementById('message-container').innerHTML = "";
  288. this._selected_service = event.target.textContent
  289. this._update_list('Command', this._service_descriptions.commands[this._selected_service], target);
  290. this._update_list('Event', this._service_descriptions.events[this._selected_service], target);
  291. cookies.set('service-list', this._selected_service, 2*60*60*1000);
  292. break;
  293. }
  294. case 'command-list':
  295. {
  296. this._update_selected(target, event.target);
  297. this._update_selected(document.getElementById('event-list'));
  298. var message_container = document.getElementById('message-container');
  299. message_container.innerHTML =
  300. "<h2>Command " + event.target.textContent + "</h2>" +
  301. "<h3>command</h3><pre class='definition'></pre>" +
  302. "<textarea rows='10' id='proto-message'>[]</textarea>" +
  303. "<p class='right-aligned'><input type='button' value='send' data-service='" + this._selected_service + "'" +
  304. " data-command='" + event.target.textContent +
  305. "' id = 'test-send-command' ></p>" +
  306. "<h3>response</h3><pre class='definition'></pre>" +
  307. "<p class='right-aligned'><label>pretty print message " +
  308. "<input type='checkbox'" +
  309. (cookies.get('pretty-print-message') == 'true' ? " checked='checked' " : "" ) +
  310. " id='pretty-print-message'>" +
  311. "</label></p>";
  312. var pres = message_container.getElementsByTagName('pre');
  313. var service = window.services[this._dashed_name(this._selected_service)];
  314. if (service)
  315. {
  316. service = this._selected_service + '.' + service.version;
  317. new XMLHttpRequest().loadResource(
  318. this._doc_base_uri + 'defs/' + service + '.commands.' + event.target.textContent + '.def',
  319. this._show_def,
  320. {'pre': pres[0]}
  321. )
  322. new XMLHttpRequest().loadResource(
  323. this._doc_base_uri + 'defs/' + service + '.responses.' + event.target.textContent + '.def',
  324. this._show_def,
  325. {'pre': pres[1]}
  326. )
  327. }
  328. cookies.set('command-list', event.target.textContent, 2*60*60*1000);
  329. break;
  330. }
  331. case 'event-list':
  332. {
  333. this._update_selected(target, event.target);
  334. this._update_selected(document.getElementById('command-list'));
  335. var message_container = document.getElementById('message-container');
  336. message_container.innerHTML =
  337. "<h2>Event " + event.target.textContent + "</h2>" +
  338. "<pre class='definition'></pre>";
  339. var pres = message_container.getElementsByTagName('pre');
  340. var service = window.services[this._dashed_name(this._selected_service)];
  341. if (service)
  342. {
  343. service = this._selected_service + '.' + service.version;
  344. new XMLHttpRequest().loadResource(
  345. this._doc_base_uri + 'defs/' + service + '.events.' + event.target.textContent + '.def',
  346. this._show_def,
  347. {'pre': pres[0]}
  348. );
  349. }
  350. break;
  351. }
  352. case 'test-send-command':
  353. {
  354. this._send_command(target);
  355. break;
  356. }
  357. case 'clear-log':
  358. {
  359. document.getElementById('log-container').firstElementChild.textContent = "";
  360. break;
  361. }
  362. }
  363. }
  364. }
  365. this._change_handler = function(event)
  366. {
  367. switch(event.target.id)
  368. {
  369. case 'pretty-print-message':
  370. {
  371. cookies.set('pretty-print-message', event.target.checked && 'true' || 'false');
  372. if(this._last_message)
  373. {
  374. this._handle_response.apply(this, this._last_message);
  375. }
  376. break;
  377. }
  378. }
  379. }
  380. /* implementation */
  381. this._send_command = function(target)
  382. {
  383. var service = target.getAttribute('data-service');
  384. var command = target.getAttribute('data-command');
  385. var response = document.getElementById('message-response');
  386. if(response)
  387. {
  388. response.parentNode.removeChild(response);
  389. }
  390. try
  391. {
  392. var message = eval('(' + document.getElementById('proto-message').value + ')');
  393. var tag = tagManager.set_callback(this, this._handle_response, [service, command]);
  394. services[this._dashed_name(service)]['request' + command](tag, message);
  395. }
  396. catch(e)
  397. {
  398. alert('not a valid message');
  399. };
  400. }
  401. this.get_bound_click_handler = function()
  402. {
  403. var self = this;
  404. return function(event)
  405. {
  406. self._click_handler(event);
  407. }
  408. }
  409. this.get_bound_change_handler = function()
  410. {
  411. var self = this;
  412. return function(event)
  413. {
  414. self._change_handler(event);
  415. }
  416. }
  417. }