node.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. "jquery",
  23. "vwf/configuration",
  24. "vwf/kernel/model",
  25. "vwf/model/javascript",
  26. "vwf/model/object",
  27. "vwf/model/stage/log",
  28. "vwf/kernel/view",
  29. "vwf/view/document",
  30. "vwf/kernel/utility",
  31. "vwf/utility",
  32. "logger",
  33. ], function( ready, testUtility ) {
  34. // Test the JavaScript node proxy and node initialization.
  35. ready( function() {
  36. vwf.initialize(
  37. /* models */ [ "vwf/model/javascript", "vwf/model/object" ],
  38. /* views */ [ "vwf/view/document" ]
  39. );
  40. // Member name priority on the node
  41. asyncTest( "Member name priority", function() {
  42. createFixture( function( fixtureID ) {
  43. vwf.execute( fixtureID, "this.a" );
  44. equal( vwf.execute( fixtureID, "this.result" ), "a-property", "properties override methods" );
  45. vwf.execute( fixtureID, "this.b()" );
  46. equal( vwf.execute( fixtureID, "this.result" ), "b-method", "methods override events" );
  47. vwf.execute( fixtureID, "this.c()" );
  48. equal( vwf.execute( fixtureID, "this.result" ), "c-event", "events override children" );
  49. vwf.execute( fixtureID, "this.d.p" );
  50. equal( vwf.execute( fixtureID, "this.result" ), "d-child", "children override nothing" );
  51. start();
  52. } );
  53. } );
  54. // Member name priority on the future node
  55. asyncTest( "Future member name priority", function() {
  56. createFixture( function( fixtureID ) {
  57. vwf.execute( fixtureID, "this.future( 0.001 ).a" );
  58. vwf.execute( fixtureID, "this.future( 0.002 ).b()" );
  59. vwf.execute( fixtureID, "this.future( 0.003 ).c()" );
  60. vwf.execute( fixtureID, "this.d.future( 0.004 ).p" );
  61. testUtility.runFutureAssertions( [
  62. { relative: 0.001, assertion: function() {
  63. equal( vwf.execute( fixtureID, "this.result" ), "a-property", "properties override methods" ) } },
  64. { relative: 0.002, assertion: function() {
  65. equal( vwf.execute( fixtureID, "this.result" ), "b-method", "methods override events" ) } },
  66. { relative: 0.003, assertion: function() {
  67. equal( vwf.execute( fixtureID, "this.result" ), "c-event", "events override children" ) } },
  68. { relative: 0.004, assertion: function() {
  69. equal( vwf.execute( fixtureID, "this.result" ), "d-child", "children override nothing" ) } },
  70. ] );
  71. } );
  72. } );
  73. // Completion order when `node.initialize` creates child nodes.
  74. // The child callbacks should arrive after the parent's `initialize` in the order the
  75. // children were created, and all children created by the parent's `initialize` should be
  76. // ready and visible before the first callback runs.
  77. asyncTest( "Initialize creating children", function() {
  78. vwf.createChild( 0, testUtility.uniqueName( "fixture" ), {
  79. // A custom prototype shared by the parent and children to store the log. The dummy
  80. // property is to make this prototype different so that it won't be shared with the
  81. // other tests.
  82. extends: {
  83. properties: {
  84. "initialize-creating-children": true,
  85. log: [],
  86. }
  87. },
  88. scripts: [
  89. 'this.initialize = function() { ' +
  90. 'Object.getPrototypeOf( this ).log.push( "parent before" ); ' +
  91. 'this.children.create( "child1", { ' +
  92. '}, function( child ) { ' +
  93. 'Object.getPrototypeOf( this ).log.push( "child1 callback " + this.childNames() ); ' +
  94. '} ); ' +
  95. 'this.children.create( "child2", { ' +
  96. '}, function( child ) { ' +
  97. 'Object.getPrototypeOf( this ).log.push( "child2 callback " + this.childNames() ); ' +
  98. '} ); ' +
  99. 'Object.getPrototypeOf( this ).log.push( "parent after" ); ' +
  100. '}; ' +
  101. 'this.childNames = function() { ' +
  102. 'return this.children.map( function( child ) { return child.name } ).join( "+" ); ' +
  103. '};'
  104. ]
  105. }, undefined, function( parentID ) {
  106. vwf.execute( parentID, 'Object.getPrototypeOf( this ).log.push( "parent callback" )' );
  107. deepEqual( vwf.execute( parentID, "Object.getPrototypeOf( this ).log" ), [
  108. "parent before", // initialize begins
  109. "parent after", // initialize ends before children complete
  110. "child1 callback child1+child2", // first child callback (both children ready)
  111. "child2 callback child1+child2", // second child callback (both children ready)
  112. "parent callback", // parent completes after children complete
  113. ], "initialize runs, then children complete, then parent completes" );
  114. start();
  115. } );
  116. } );
  117. // Completion order when `node.initialize` creates child nodes with `initialize` functions
  118. // that create child nodes.
  119. asyncTest( "Initialize creating children with initialize that creates children", function() {
  120. vwf.createChild( 0, testUtility.uniqueName( "fixture" ), {
  121. // A custom prototype shared by the parent and children to store the log. The dummy
  122. // property is to make this prototype different so that it won't be shared with the
  123. // other tests.
  124. extends: {
  125. properties: {
  126. "initialize-creating-children-with-initialize-that-creates-children": true,
  127. log: [],
  128. }
  129. },
  130. scripts: [
  131. 'this.initialize = function() { ' +
  132. 'Object.getPrototypeOf( this ).log.push( "parent before" ); ' +
  133. 'this.children.create( "child1", { ' +
  134. 'extends: Object.getPrototypeOf( this ).id, ' +
  135. 'scripts: [ ' +
  136. '\'this.initialize = function() { ' +
  137. 'this.children.create( "child1a", { ' +
  138. '}, function( child ) { ' +
  139. 'Object.getPrototypeOf( this ).log.push( "child1a callback " + this.childNames() ); ' +
  140. '} ); ' +
  141. 'this.children.create( "child1b", { ' +
  142. '}, function( child ) { ' +
  143. 'Object.getPrototypeOf( this ).log.push( "child1b callback " + this.childNames() ); ' +
  144. '} ); ' +
  145. '}; ' +
  146. 'this.childNames = function() { ' +
  147. 'return this.children.map( function( child ) { return child.name } ).join( "+" ); ' +
  148. '}; \'' +
  149. '] ' +
  150. '}, function( child ) { ' +
  151. 'Object.getPrototypeOf( this ).log.push( "child1 callback " + this.childNames() ); ' +
  152. '} ); ' +
  153. 'this.children.create( "child2", { ' +
  154. '}, function( child ) { ' +
  155. 'Object.getPrototypeOf( this ).log.push( "child2 callback " + this.childNames() ); ' +
  156. '} ); ' +
  157. 'Object.getPrototypeOf( this ).log.push( "parent after" ); ' +
  158. '}; ' +
  159. 'this.childNames = function() { ' +
  160. 'return this.children.map( function( child ) { return child.name } ).join( "+" ); ' +
  161. '};'
  162. ]
  163. }, undefined, function( parentID ) {
  164. vwf.execute( parentID, 'Object.getPrototypeOf( this ).log.push( "parent callback" )' );
  165. deepEqual( vwf.execute( parentID, "Object.getPrototypeOf( this ).log" ), [
  166. "parent before", // initialize begins
  167. "parent after", // initialize ends before children complete
  168. "child1a callback child1a+child1b", // child1's first child completes (both grandchildren ready)
  169. "child1b callback child1a+child1b", // child1's second child completes (both grandchildren ready)
  170. "child1 callback child1+child2", // child1 completes after child1 children complete (child1 & child2 ready)
  171. "child2 callback child1+child2", // child2 completes after child2 children complete (child1 & child2 ready)
  172. "parent callback", // parent completes after children complete
  173. ], "initialize runs, then first grandchildren complete, then children complete, then parent completes" );
  174. start();
  175. } );
  176. } );
  177. // Completion order when a non-async action creates nodes.
  178. // The node callbacks should arrive after the action completes but before the next action
  179. // is started.
  180. asyncTest( "Non-async action creating nodes", function() {
  181. vwf.createChild( 0, testUtility.uniqueName( "fixture" ), {
  182. // A custom prototype shared by the parent and children to store the log. The dummy
  183. // property is to make this prototype different so that it won't be shared with the
  184. // other tests.
  185. extends: {
  186. properties: {
  187. "action-creating-nodes": true,
  188. log: [],
  189. }
  190. },
  191. methods: {
  192. m1:
  193. 'Object.getPrototypeOf( this ).log.push( "method1 before" ); ' +
  194. 'this.children.create( "child1", { ' +
  195. '}, function( child ) { ' +
  196. 'Object.getPrototypeOf( this ).log.push( "child1 callback " + this.childNames() ); ' +
  197. '} ); ' +
  198. 'Object.getPrototypeOf( this ).log.push( "method1 after" );',
  199. m2:
  200. 'Object.getPrototypeOf( this ).log.push( "method2 before" ); ' +
  201. 'this.children.create( "child2", { ' +
  202. '}, function( child ) { ' +
  203. 'Object.getPrototypeOf( this ).log.push( "child2 callback " + this.childNames() ); ' +
  204. '} ); ' +
  205. 'Object.getPrototypeOf( this ).log.push( "method2 after" );',
  206. n:
  207. 'Object.getPrototypeOf( this ).log.push( "future method" );',
  208. },
  209. scripts: [
  210. 'this.childNames = function() { ' +
  211. 'return this.children.map( function( child ) { return child.name } ).join( "+" ); ' +
  212. '};'
  213. ]
  214. }, undefined, function( parentID ) {
  215. vwf.execute( parentID, "this.m1()" ); // m1() and m2() should both run to completion
  216. vwf.execute( parentID, "this.m2()" ); // ... before the children they create complete
  217. vwf.execute( parentID, "this.future( 0.001 ).n()" ); // next action starts after the children complete
  218. deepEqual( vwf.execute( parentID, "Object.getPrototypeOf( this ).log" ), [
  219. "method1 before", // m1 begins
  220. "method1 after", // ... and ends without interruption
  221. "method2 before", // m2 begins immedately after
  222. "method2 after", // ... and ends without interruption
  223. ], "action completes before the children" );
  224. testUtility.runFutureAssertions( [
  225. // Called when time leaves t+0, after actions at time t+0 run.
  226. { relative: 0, assertion: function() {
  227. deepEqual( vwf.execute( parentID, "Object.getPrototypeOf( this ).log" ), [
  228. "method1 before",
  229. "method1 after",
  230. "method2 before",
  231. "method2 after",
  232. "child1 callback child1+child2", // m1 and m2 children complete in the order
  233. "child2 callback child1+child2", // ... started, after the action
  234. ], "children complete after the action" ) } },
  235. // Called when time leaves t+0.001.
  236. { relative: 0.001, assertion: function() {
  237. deepEqual( vwf.execute( parentID, "Object.getPrototypeOf( this ).log" ), [
  238. "method1 before",
  239. "method1 after",
  240. "method2 before",
  241. "method2 after",
  242. "child1 callback child1+child2",
  243. "child2 callback child1+child2",
  244. "future method", // children complete before the next action
  245. ], "children complete before the next action" ) } },
  246. ] );
  247. } );
  248. } );
  249. // Completion order when multiple non-async actions create nodes.
  250. // The node callbacks should arrive after each action completes but before the one starts.
  251. asyncTest( "Multiple non-async actions creating nodes", function() {
  252. vwf.createChild( 0, testUtility.uniqueName( "fixture" ), {
  253. // A custom prototype shared by the parent and children to store the log. The dummy
  254. // property is to make this prototype different so that it won't be shared with the
  255. // other tests.
  256. extends: {
  257. properties: {
  258. "multiple-actions-creating-nodes": true,
  259. log: [],
  260. }
  261. },
  262. methods: {
  263. m1:
  264. 'Object.getPrototypeOf( this ).log.push( "method1 before" ); ' +
  265. 'this.children.create( "child1", { ' +
  266. '}, function( child ) { ' +
  267. 'Object.getPrototypeOf( this ).log.push( "child1 callback " + this.childNames() ); ' +
  268. '} ); ' +
  269. 'Object.getPrototypeOf( this ).log.push( "method1 after" );',
  270. m2:
  271. 'Object.getPrototypeOf( this ).log.push( "method2 before" ); ' +
  272. 'this.children.create( "child2", { ' +
  273. '}, function( child ) { ' +
  274. 'Object.getPrototypeOf( this ).log.push( "child2 callback " + this.childNames() ); ' +
  275. '} ); ' +
  276. 'Object.getPrototypeOf( this ).log.push( "method2 after" );',
  277. n:
  278. 'Object.getPrototypeOf( this ).log.push( "future method" );',
  279. },
  280. scripts: [
  281. 'this.childNames = function() { ' +
  282. 'return this.children.map( function( child ) { return child.name } ).join( "+" ); ' +
  283. '};'
  284. ]
  285. }, undefined, function( parentID ) {
  286. vwf.execute( parentID, "this.future( 0 ).m1()" ); // m1's child should complete after m1 and before m2
  287. vwf.execute( parentID, "this.future( 0 ).m2()" ); // m2's child should complete after m2 but still within time t+0
  288. vwf.execute( parentID, "this.future( 0.001 ).n()" ); // next action starts after m1 & m2 children complete
  289. testUtility.runFutureAssertions( [
  290. // Called when time leaves t+0, after actions at time t+0 run.
  291. { relative: 0, assertion: function() {
  292. deepEqual( vwf.execute( parentID, "Object.getPrototypeOf( this ).log" ), [
  293. "method1 before", // m1 begins
  294. "method1 after", // ... and ends without interruption
  295. "child1 callback child1", // m1 child completes after the m1 action before starting m2
  296. "method2 before", // m2 begins
  297. "method2 after", // ... and ends without interruption
  298. "child2 callback child1+child2", // m2 child completes after the m2 action
  299. ], "children complete after each action" ) } },
  300. // Called when time leaves t+0.001.
  301. { relative: 0.001, assertion: function() {
  302. deepEqual( vwf.execute( parentID, "Object.getPrototypeOf( this ).log" ), [
  303. "method1 before",
  304. "method1 after",
  305. "child1 callback child1",
  306. "method2 before",
  307. "method2 after",
  308. "child2 callback child1+child2",
  309. "future method", // children complete before the next action
  310. ], "children complete before the next action" ) } },
  311. ] );
  312. } );
  313. } );
  314. // == Helper functions =====================================================================
  315. // Create a node with conflicting property, method, event and child names.
  316. function createFixture( callback ) {
  317. vwf.createChild( 0, testUtility.uniqueName( "fixture" ), {
  318. extends:
  319. "http://vwf.example.com/node.vwf",
  320. properties: {
  321. a: {
  322. get: "return this.result = 'a-property'",
  323. }
  324. },
  325. methods: {
  326. a: "return this.result = 'a-method'",
  327. b: "return this.result = 'b-method'",
  328. },
  329. events: {
  330. a: undefined,
  331. b: undefined,
  332. c: undefined,
  333. },
  334. children: {
  335. a: { properties: { p: { get: "return this.parent.result = 'a-child'" } } },
  336. b: { properties: { p: { get: "return this.parent.result = 'b-child'" } } },
  337. c: { properties: { p: { get: "return this.parent.result = 'c-child'" } } },
  338. d: { properties: { p: { get: "return this.parent.result = 'd-child'" } } },
  339. },
  340. scripts: [
  341. "this.events.a = this.events.add( function() { this.result = 'a-event' }, this )",
  342. "this.events.b = this.events.add( function() { this.result = 'b-event' }, this )",
  343. "this.events.c = this.events.add( function() { this.result = 'c-event' }, this )",
  344. "this.result = undefined",
  345. ],
  346. }, undefined, function( fixtureID ) {
  347. callback( fixtureID );
  348. } );
  349. }
  350. } );
  351. } );
  352. </script>
  353. <link rel="stylesheet" type="text/css" href="qunit.css" />
  354. </head>
  355. <body>
  356. <h1 id="qunit-header">Virtual World Framework</h1>
  357. <h2 id="qunit-banner"></h2>
  358. <div id="qunit-testrunner-toolbar"></div>
  359. <h2 id="qunit-userAgent"></h2>
  360. <ol id="qunit-tests"></ol>
  361. <div id="qunit-fixture">test markup, will be hidden</div>
  362. </body>
  363. </html>