events.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Virtual World Framework</title>
  5. <script type="text/javascript" src="qunit.js"></script>
  6. <script type="text/javascript" src="../lib/async.js"></script>
  7. <script type="text/javascript" src="../lib/crypto.js"></script>
  8. <script type="text/javascript" src="../lib/md5.js"></script>
  9. <script type="text/javascript" src="../lib/alea.js"></script>
  10. <script type="text/javascript" src="../lib/mash.js"></script>
  11. <script type="text/javascript" src="../lib/vwf.js"></script>
  12. <script type="text/javascript" src="../lib/require.js"></script>
  13. <script type="text/javascript">
  14. require( {
  15. baseUrl: "../lib",
  16. paths: {
  17. jquery: "jquery-1.10.2.min",
  18. },
  19. }, [
  20. "domReady",
  21. "utility.js",
  22. "vwf/utility",
  23. "jquery",
  24. "vwf/configuration",
  25. "vwf/kernel/model",
  26. "vwf/model/javascript",
  27. "vwf/model/object",
  28. "vwf/model/stage/log",
  29. "vwf/kernel/view",
  30. "vwf/kernel/utility",
  31. "logger",
  32. ], function( ready, testUtility, utility ) {
  33. // Test events.
  34. ready( function() {
  35. vwf.initialize(
  36. /* models */ [ "vwf/model/javascript", "vwf/model/object" ],
  37. /* views */ [ ]
  38. );
  39. // Event firing directly from the kernel
  40. asyncTest( "Event firing kernel", function() {
  41. createFixture( function( nodeID, cleanup ) {
  42. vwf.fireEvent( nodeID, "empty" );
  43. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [] }, "no parameters declared, no parameters passed" );
  44. vwf.fireEvent( nodeID, "empty", [ true, 1 ] );
  45. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [ true, 1 ] }, "no parameters declared, parameters passed" );
  46. vwf.fireEvent( nodeID, "parameters" );
  47. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [] }, "parameters declared, no parameters passed" );
  48. vwf.fireEvent( nodeID, "parameters", [ 'abc', false, 2 ] );
  49. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [ 'abc', false, 2 ] }, "parameters declared, parameters passed" );
  50. cleanup();
  51. start();
  52. } );
  53. } );
  54. // Event firing from JavaScript
  55. asyncTest( "Event firing JavaScript", function() {
  56. createFixture( function( nodeID, cleanup ) {
  57. vwf.execute( nodeID, "this.empty()" );
  58. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [] }, "no parameters declared, no parameters passed" );
  59. vwf.execute( nodeID, "this.empty( true, 1 )" );
  60. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [ true, 1 ] }, "no parameters declared, parameters passed" );
  61. vwf.execute( nodeID, "this.parameters()" );
  62. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [] }, "parameters declared, no parameters passed" );
  63. vwf.execute( nodeID, "this.parameters( 'abc', false, 2 )" );
  64. deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [ 'abc', false, 2 ] }, "parameters declared, parameters passed" );
  65. cleanup();
  66. start();
  67. } );
  68. } );
  69. // JavaScript handler with explicit context
  70. asyncTest( "JavaScript handler explicit context", function() {
  71. createFixtureParentChild( function( parentID, childID, baseID, cleanup ) {
  72. vwf.execute( childID, "this.parent.base = this.events.add( function( result ) { this.result = result }, this )" );
  73. vwf.fireEvent( parentID, "base", [ "explicit context" ] );
  74. equal( vwf.execute( childID, "this.result" ), "explicit context", "invoked on child" );
  75. cleanup();
  76. start();
  77. } );
  78. } );
  79. // JavaScript handler with no context
  80. asyncTest( "JavaScript handler no context", function() {
  81. createFixtureParentChild( function( parentID, childID, baseID, cleanup ) {
  82. vwf.execute( childID, "this.parent.base = this.events.add( function( result ) { this.result = result } )" );
  83. vwf.fireEvent( parentID, "base", [ "no context" ] );
  84. equal( vwf.execute( 0, "this.result" ), "no context", "invoked on global root" );
  85. cleanup();
  86. start();
  87. } );
  88. } );
  89. // JavaScript handler with implicit context
  90. asyncTest( "JavaScript handler implicit context", function() {
  91. createFixtureParentChild( function( parentID, childID, baseID, cleanup ) {
  92. vwf.execute( childID, "this.parent.base = function( result ) { this.result = result }" );
  93. vwf.fireEvent( parentID, "base", [ "implicit context" ] );
  94. equal( vwf.execute( parentID, "this.result" ), "implicit context", "invoked on parent" ); // invoked on target
  95. cleanup();
  96. start();
  97. } );
  98. } );
  99. // Event inheritance directly from the kernel
  100. asyncTest( "Event inheritance kernel", function() {
  101. createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {
  102. vwf.execute( derivedID, "this.derived = this.events.add( function( result ) { this.result = result }, this )" );
  103. vwf.execute( derivedID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );
  104. vwf.execute( baseID, "this.derived = this.events.add( function( result ) { this.result = result }, this )" );
  105. vwf.execute( baseID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );
  106. vwf.fireEvent( derivedID, "derived", [ 'derived-' + derivedID ] );
  107. equal( vwf.execute( derivedID, "this.result" ), "derived-" + derivedID, "derived event from derived" );
  108. vwf.fireEvent( derivedID, "base", [ 'base-' + derivedID ] );
  109. equal( vwf.execute( derivedID, "this.result" ), "base-" + derivedID, "base event from derived" );
  110. vwf.fireEvent( baseID, "derived", [ 'derived-' + baseID ] );
  111. equal( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );
  112. vwf.fireEvent( baseID, "base", [ 'base-' + baseID ] );
  113. equal( vwf.execute( baseID, "this.result" ), "base-" + baseID, "base event from base" );
  114. cleanup();
  115. start();
  116. } );
  117. } );
  118. // Event inheritance from JavaScript direct properties
  119. asyncTest( "Event inheritance JavaScript direct", function() {
  120. createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {
  121. vwf.execute( derivedID, "this.derived = this.events.add( function( result ) { this.result = result }, this )" );
  122. vwf.execute( derivedID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );
  123. vwf.execute( baseID, "this.derived && ( this.derived = this.events.add( function( result ) { this.result = result } ), this )" );
  124. vwf.execute( baseID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );
  125. vwf.execute( derivedID, "this.derived( 'derived-' + this.id )" );
  126. equal( vwf.execute( derivedID, "this.result" ), "derived-" + derivedID, "derived event from derived" );
  127. vwf.execute( derivedID, "this.base( 'base-' + this.id )" );
  128. equal( vwf.execute( derivedID, "this.result" ), "base-" + derivedID, "base event from derived" );
  129. vwf.execute( baseID, "this.derived && this.derived( 'derived-' + this.id )" );
  130. equal( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );
  131. vwf.execute( baseID, "this.base( 'base-' + this.id )" );
  132. equal( vwf.execute( baseID, "this.result" ), "base-" + baseID, "base event from base" );
  133. cleanup();
  134. start();
  135. } );
  136. } );
  137. // Event inheritance from JavaScript collection properties
  138. asyncTest( "Event inheritance JavaScript collection", function() {
  139. createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {
  140. vwf.execute( derivedID, "this.events.derived = this.events.add( function( result ) { this.result = result }, this )" );
  141. vwf.execute( derivedID, "this.events.base = this.events.add( function( result ) { this.result = result }, this )" );
  142. vwf.execute( baseID, "this.events.derived && ( this.events.derived = this.events.add( function( result ) { this.result = result } ), this )" );
  143. vwf.execute( baseID, "this.events.base = this.events.add( function( result ) { this.result = result }, this )" );
  144. vwf.execute( derivedID, "this.events.derived( 'derived-' + this.id )" );
  145. equal( vwf.execute( derivedID, "this.result" ), "derived-" + derivedID, "derived event from derived" );
  146. vwf.execute( derivedID, "this.events.base( 'base-' + this.id )" );
  147. equal( vwf.execute( derivedID, "this.result" ), "base-" + derivedID, "base event from derived" );
  148. vwf.execute( baseID, "this.events.derived && this.events.derived( 'derived-' + this.id )" );
  149. equal( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );
  150. vwf.execute( baseID, "this.events.base( 'base-' + this.id )" );
  151. equal( vwf.execute( baseID, "this.result" ), "base-" + baseID, "base event from base" );
  152. cleanup();
  153. start();
  154. } );
  155. } );
  156. // Event inheritance with self-targeted handlers
  157. asyncTest( "Event inheritance self-targeted handlers", function() {
  158. createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {
  159. vwf.execute( derivedID, "this.derived = function( result ) { this.result = ( this.result || [] ).concat( result + '-derived' ) }" );
  160. vwf.execute( derivedID, "this.base = function( result ) { this.result = ( this.result || [] ).concat( result + '-derived' ) }" );
  161. vwf.execute( baseID, "this.derived = function( result ) { this.result = ( this.result || [] ).concat( result + '-base' ) }" );
  162. vwf.execute( baseID, "this.base = function( result ) { this.result = ( this.result || [] ).concat( result + '-base' ) }" );
  163. vwf.fireEvent( derivedID, "derived", [ 'derived-' + derivedID ] );
  164. deepEqual( vwf.execute( derivedID, "this.result" ), [ "derived-" + derivedID + "-derived" ], "derived event from derived receives handlers from derived" );
  165. vwf.execute( derivedID, "this.result = []" );
  166. vwf.fireEvent( derivedID, "base", [ 'base-' + derivedID ] );
  167. deepEqual( vwf.execute( derivedID, "this.result" ), [ "base-" + derivedID + "-base", "base-" + derivedID + "-derived" ], "base event from derived receives handlers from base and derived" );
  168. vwf.fireEvent( baseID, "derived", [ 'derived-' + baseID ] );
  169. deepEqual( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );
  170. vwf.fireEvent( baseID, "base", [ 'base-' + baseID ] );
  171. deepEqual( vwf.execute( baseID, "this.result" ), [ "base-" + baseID + "-base" ], "base event from base receives handlers from base" );
  172. cleanup();
  173. start();
  174. } );
  175. } );
  176. // Event listeners through JavaScript
  177. asyncTest( "Event listeners JavaScript", function() {
  178. createFixture( function( nodeID, cleanup ) {
  179. // Create two functions on the `result` object to serve as listeners.
  180. vwf.execute( nodeID, "this.result.listener1 = function() { this.result.count1 = ( this.result.count1 || 0 ) + 5 }" );
  181. vwf.execute( nodeID, "this.result.listener2 = function() { this.result.count2 = ( this.result.count2 || 0 ) + 7 }" );
  182. // Add the listeners to the `empty` event and save the listener IDs on the `result` object.
  183. vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener1, this, function( listenerID ) { this.result.listener1ID = listenerID } )" );
  184. vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener2, this, function( listenerID ) { this.result.listener2ID = listenerID } )" );
  185. // Fire the event. Both listeners should execute.
  186. vwf.execute( nodeID, "this.empty()" );
  187. equal( vwf.execute( nodeID, "this.result.count1" ), 5, "" );
  188. equal( vwf.execute( nodeID, "this.result.count2" ), 7, "" );
  189. // Remove the first listener by ID and fire the event. Only the second listener should
  190. // execute. `result.listener1ID` is not valid after the listener has been removed.
  191. vwf.execute( nodeID, "this.empty = this.events.remove( this.result.listener1ID )" );
  192. vwf.execute( nodeID, "this.empty()" );
  193. equal( vwf.execute( nodeID, "this.result.count1" ), 5, "" );
  194. equal( vwf.execute( nodeID, "this.result.count2" ), 14, "" );
  195. // Add the first listener back and fire the event. Both listeners should execute.
  196. vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener1, this )" );
  197. vwf.execute( nodeID, "this.empty()" );
  198. equal( vwf.execute( nodeID, "this.result.count1" ), 10, "" );
  199. equal( vwf.execute( nodeID, "this.result.count2" ), 21, "" );
  200. // Remove the second listener by ID and fire the event. Only the first listener should
  201. // execute. `result.listener2ID` is not valid after the listener has been removed.
  202. vwf.execute( nodeID, "this.empty = this.events.remove( this.result.listener2ID )" );
  203. vwf.execute( nodeID, "this.empty()" );
  204. equal( vwf.execute( nodeID, "this.result.count1" ), 15, "" );
  205. equal( vwf.execute( nodeID, "this.result.count2" ), 21, "" );
  206. // Remove the first listener by function reference and fire the event. Neither
  207. // listener should execute.
  208. vwf.execute( nodeID, "this.empty = this.events.remove( this.result.listener1 )" );
  209. vwf.execute( nodeID, "this.empty()" );
  210. equal( vwf.execute( nodeID, "this.result.count1" ), 15, "" );
  211. equal( vwf.execute( nodeID, "this.result.count2" ), 21, "" );
  212. // Add both listeners back and fire the event. Both listeners should execute.
  213. vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener1, this )" );
  214. vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener2, this )" );
  215. vwf.execute( nodeID, "this.empty()" );
  216. equal( vwf.execute( nodeID, "this.result.count1" ), 20, "" );
  217. equal( vwf.execute( nodeID, "this.result.count2" ), 28, "" );
  218. // Flush all listeners with the context of the target node and fire the event. Both
  219. // listeners should be removed and neither should execute.
  220. vwf.execute( nodeID, "this.empty = this.events.flush( this )" );
  221. vwf.execute( nodeID, "this.empty()" );
  222. equal( vwf.execute( nodeID, "this.result.count1" ), 20, "" );
  223. equal( vwf.execute( nodeID, "this.result.count2" ), 28, "" );
  224. // Do a getEvent to verify that kernel recognizes that event listeners are gone
  225. try {
  226. var testEvent = vwf.getEvent( nodeID, "empty" );
  227. equal( testEvent.listeners, undefined, "" );
  228. } catch( e ) {
  229. equal( e.message, "No error", "" );
  230. }
  231. // Add a listener with no context, which should execute in the context of the global
  232. // node.
  233. vwf.execute( nodeID, "this.empty = function() {}" );
  234. vwf.execute( nodeID, "this.empty()" ); // TODO: how to test?
  235. cleanup();
  236. start();
  237. } );
  238. } );
  239. // Event changes and node state
  240. var changesComponentDescriptor = {
  241. events: {
  242. event: null
  243. },
  244. scripts: [
  245. "this.events.event = this.events.add( function() { return 'original' } )"
  246. ]
  247. };
  248. var changesComponentURI =
  249. testUtility.dataURIFromDescriptor( changesComponentDescriptor );
  250. var changesEventDescriptor = {
  251. listeners: [ {
  252. id: 1, // from the node's sequence counter when the listener was added
  253. body: "return 'original'",
  254. type: "application/javascript",
  255. } ]
  256. };
  257. var changesEventNewBody = "return 'changed'";
  258. var changesEventDescriptorChanged = {
  259. listeners: [
  260. utility.merge( {}, changesEventDescriptor.listeners[0], {
  261. body: changesEventNewBody
  262. } )
  263. ]
  264. };
  265. asyncTest( "Event changes in node state", function() {
  266. vwf.createNode( changesComponentURI, function( nodeID ) {
  267. deepEqual( vwf.getEvent( nodeID, "event" ), changesEventDescriptor, "Unchanged" );
  268. vwf.setEventListener( nodeID, "event", 1, changesEventNewBody );
  269. deepEqual( vwf.getNode( nodeID ), {
  270. patches:
  271. changesComponentURI,
  272. events: {
  273. event: changesEventDescriptorChanged,
  274. }
  275. }, "Event changed" );
  276. vwf.deleteNode( nodeID );
  277. start();
  278. } );
  279. } );
  280. asyncTest( "Event changes to node state", function() {
  281. vwf.createNode( changesComponentURI, function( nodeID ) {
  282. deepEqual( vwf.getEvent( nodeID, "event" ), changesEventDescriptor, "Unchanged" );
  283. vwf.setNode( nodeID, {
  284. patches:
  285. changesComponentURI,
  286. events: {
  287. event: changesEventDescriptorChanged,
  288. }
  289. } );
  290. deepEqual( vwf.getEvent( nodeID, "event" ).listeners[0].body, changesEventNewBody, "Event changed from node state" );
  291. vwf.deleteNode( nodeID );
  292. start();
  293. } );
  294. } );
  295. // == Helper functions =====================================================================
  296. // Create a node with events to fire.
  297. function createFixture( callback ) {
  298. vwf.createChild( 0, testUtility.uniqueName( "node" ), {
  299. extends:
  300. "http://vwf.example.com/node.vwf",
  301. events: {
  302. empty: undefined,
  303. parameters: { parameters: [] },
  304. },
  305. scripts: [
  306. "this.result = {}",
  307. "this.empty = this.events.add( function() { this.result.event = 'empty'; this.result.result = Array.prototype.slice.call( arguments ) }, this )",
  308. "this.parameters = this.events.add( function() { this.result.event = 'parameters'; this.result.result = Array.prototype.slice.call( arguments ) }, this )",
  309. ],
  310. }, undefined, function( nodeID ) {
  311. callback( nodeID, function() {
  312. vwf.deleteNode( nodeID );
  313. } );
  314. } );
  315. }
  316. // Create a parent and two child nodes with events to fire.
  317. function createFixtureParentChild( callback ) {
  318. // Prototype for the nodes.
  319. vwf.createNode( {
  320. extends: "http://vwf.example.com/node.vwf",
  321. events: { base: undefined },
  322. }, function( baseID ) {
  323. // Parent node.
  324. vwf.createChild( 0, testUtility.uniqueName( "parent" ), {
  325. extends: baseID,
  326. }, undefined, function( parentID ) {
  327. // Child node.
  328. vwf.createChild( parentID, testUtility.uniqueName( "child" ), {
  329. extends: baseID,
  330. }, undefined, function( childID ) {
  331. callback( parentID, childID, baseID, function() {
  332. vwf.deleteNode( childID );
  333. vwf.deleteNode( parentID );
  334. vwf.deleteNode( baseID );
  335. } );
  336. } );
  337. } );
  338. } );
  339. }
  340. // Create a node with two levels of inheritance and events to fire.
  341. function createFixtureDerivedBase( callback ) {
  342. vwf.createNode( {
  343. extends: "http://vwf.example.com/node.vwf",
  344. events: { base: undefined },
  345. scripts: [ ],
  346. }, function( baseID ) {
  347. vwf.createNode( {
  348. extends: baseID,
  349. events: { derived: undefined },
  350. scripts: [ ],
  351. }, function( derivedID ) {
  352. callback( derivedID, baseID, function() {
  353. vwf.deleteNode( derivedID );
  354. vwf.deleteNode( baseID );
  355. } );
  356. } );
  357. } );
  358. }
  359. } );
  360. } );
  361. </script>
  362. <link rel="stylesheet" type="text/css" href="qunit.css" />
  363. </head>
  364. <body>
  365. <h1 id="qunit-header">Virtual World Framework</h1>
  366. <h2 id="qunit-banner"></h2>
  367. <div id="qunit-testrunner-toolbar"></div>
  368. <h2 id="qunit-userAgent"></h2>
  369. <ol id="qunit-tests"></ol>
  370. <div id="qunit-fixture">test markup, will be hidden</div>
  371. </body>
  372. </html>