123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <!DOCTYPE html>
- <html>
- <head>
- <title>Virtual World Framework</title>
- <script type="text/javascript" src="qunit.js"></script>
- <script type="text/javascript" src="../lib/async.js"></script>
- <script type="text/javascript" src="../lib/crypto.js"></script>
- <script type="text/javascript" src="../lib/md5.js"></script>
- <script type="text/javascript" src="../lib/alea.js"></script>
- <script type="text/javascript" src="../lib/mash.js"></script>
- <script type="text/javascript" src="../lib/vwf.js"></script>
- <script type="text/javascript" src="../lib/require.js"></script>
- <script type="text/javascript">
- require( {
- baseUrl: "../lib",
- paths: {
- jquery: "jquery-1.10.2.min",
- },
- }, [
- "domReady",
- "utility.js",
- "jquery",
- "vwf/configuration",
- "vwf/kernel/model",
- "vwf/model/javascript",
- "vwf/model/object",
- "vwf/model/stage/log",
- "vwf/kernel/view",
- "vwf/kernel/utility",
- "vwf/utility",
- "logger",
- ], function( ready, testUtility ) {
- // Test dispatching events.
- ready( function() {
- vwf.initialize(
- /* models */ [ "vwf/model/javascript", "vwf/model/object" ],
- /* views */ [ ]
- );
- var emptyParameters = [ {} ]; // eventData == {}
- var emptyNodeParameters = { "": [ {} ] }; // eventNodeData == {} for all nodes
- // Regular, non-dispatching event to the top.
- asyncTest( "Regular event top", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.fireEvent( topID, "test" );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "top bubble" ], "top fireEvent only visits top" );
- cleanup();
- start();
- } );
- } );
- // Dispatching event to the top.
- asyncTest( "Dispatching event top", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( topID, "test", emptyParameters, emptyNodeParameters );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "top bubble" ], "top dispatchEvent only visits top" );
- cleanup();
- start();
- } );
- } );
- // Regular, non-dispatching event to the bottom.
- asyncTest( "Regular event bottom", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.fireEvent( bottomID, "test" );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "bottom bubble" ], "bottom fireEvent only visits bottom" );
- cleanup();
- start();
- } );
- } );
- // Dispatching event to the bottom.
- asyncTest( "Dispatching event bottom", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( bottomID, "test", emptyParameters, emptyNodeParameters );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "top capture", "middle capture", "bottom bubble", "middle bubble", "top bubble" ],
- "bottom fireEvent captures from and bubbles to top" );
- cleanup();
- start();
- } );
- } );
- // Handled (cancelled) at target.
- asyncTest( "Handle at target", function() {
- createFixture( false, false, { capture: false, bubble: true }, function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( bottomID, "test", emptyParameters, emptyNodeParameters );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "top capture", "middle capture", "bottom bubble" ],
- "handling at target prevents bubbling" );
- cleanup();
- start();
- } );
- } );
- // Handled (cancelled) at middle during capture.
- asyncTest( "Handle at middle capture", function() {
- createFixture( false, { capture: true }, false, function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( bottomID, "test", emptyParameters, emptyNodeParameters );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "top capture", "middle capture" ],
- "handling at middle capture prevents further capture and bubbling" );
- cleanup();
- start();
- } );
- } );
- // Handled (cancelled) at middle during bubbling.
- asyncTest( "Handle at middle bubble", function() {
- createFixture( false, { capture: false, bubble: true }, false, function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( bottomID, "test", emptyParameters, emptyNodeParameters );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "top capture", "middle capture", "bottom bubble", "middle bubble" ],
- "handling at middle bubble prevents further bubbling" );
- cleanup();
- start();
- } );
- } );
- // Implicit "handled" return value from the handlers.
- asyncTest( "Implicit 'handled'", function() {
- createFixture( {}, {}, {}, function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( bottomID, "test", emptyParameters, emptyNodeParameters );
- deepEqual( vwf.execute( baseID, "this.properties.targets" ), [ "top capture" ],
- "implicit undefined return in handler same as explicit true return" );
- cleanup();
- start();
- } );
- } );
- // Dispatching to the top with no arguments.
- asyncTest( "Dispatching with no parameters", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( topID, "test" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[0]" ), [ undefined ],
- "no parameters, no node-parameters, phase" );
- cleanup();
- start();
- } );
- } );
- // Dispatching to the top with parameters.
- asyncTest( "Dispatching with parameters", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( topID, "test", [ { parameters: true } ] );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[0]" ), [ { parameters: true }, undefined ],
- "parameters, no node-parameters, phase" );
- cleanup();
- start();
- } );
- } );
- // Dispatching to the top with parameters and node-parameters.
- asyncTest( "Dispatching with parameters and node-parameters", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- vwf.dispatchEvent( topID, "test", [ { parameters: true } ], { "": [ { nodeParameters: true } ] } );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[0]" ), [ { parameters: true }, { nodeParameters: true }, undefined ],
- "parameters, node-parameters, phase" );
- cleanup();
- start();
- } );
- } );
- // Dispatching to the bottom with default node-parameters.
- asyncTest( "Default node-parameters cascade to every target", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- var nodeParameters = {
- "": [ { nodeParameters: true, default: true } ] // default
- };
- vwf.dispatchEvent( bottomID, "test", [ { parameters: true } ], nodeParameters );
- equal( vwf.execute( baseID, "this.properties.argumentss.length" ), 5, "two captures, three bubbles" ); // top capture, middle capture, bottom bubble, middle bubble, top bubble
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[0][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at top capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[1][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at middle capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[2][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at bottom bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[3][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at middle bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[4][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at top bubble" );
- cleanup();
- start();
- } );
- } );
- // Dispatching to the bottom with default and top node-parameters.
- asyncTest( "Top node-parameters replace default and cascade to targets below", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- var nodeParameters = {
- "": [ { nodeParameters: true, default: true } ] // default
- };
- nodeParameters[topID] = [ { nodeParameters: true, top: true } ]; // for top
- vwf.dispatchEvent( bottomID, "test", [ { parameters: true } ], nodeParameters );
- equal( vwf.execute( baseID, "this.properties.argumentss.length" ), 5, "two captures, three bubbles" ); // top capture, middle capture, bottom bubble, middle bubble, top bubble
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[0][1]" ),
- { nodeParameters: true, top: true }, "top node-parameters at top capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[1][1]" ),
- { nodeParameters: true, top: true }, "top node-parameters at middle capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[2][1]" ),
- { nodeParameters: true, top: true }, "top node-parameters at bottom bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[3][1]" ),
- { nodeParameters: true, top: true }, "top node-parameters at middle bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[4][1]" ),
- { nodeParameters: true, top: true }, "top node-parameters at top bubble" );
- cleanup();
- start();
- } );
- } );
- // Dispatching to the bottom with default and middle node-parameters.
- asyncTest( "Middle node-parameters replace default and cascade to targets below", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- var nodeParameters = {
- "": [ { nodeParameters: true, default: true } ] // default
- };
- nodeParameters[middleID] = [ { nodeParameters: true, middle: true } ]; // for middle
- vwf.dispatchEvent( bottomID, "test", [ { parameters: true } ], nodeParameters );
- equal( vwf.execute( baseID, "this.properties.argumentss.length" ), 5, "two captures, three bubbles" ); // top capture, middle capture, bottom bubble, middle bubble, top bubble
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[0][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at top capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[1][1]" ),
- { nodeParameters: true, middle: true }, "middle node-parameters at middle capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[2][1]" ),
- { nodeParameters: true, middle: true }, "middle node-parameters at bottom bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[3][1]" ),
- { nodeParameters: true, middle: true }, "middle node-parameters at middle bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[4][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at top bubble" );
- cleanup();
- start();
- } );
- } );
- // Dispatching to the bottom with default and bottom node-parameters.
- asyncTest( "Bottom node-parameters replace default", function() {
- createFixture( function( topID, middleID, bottomID, baseID, cleanup ) {
- var nodeParameters = {
- "": [ { nodeParameters: true, default: true } ] // default
- };
- nodeParameters[bottomID] = [ { nodeParameters: true, bottom: true } ]; // for bottom
- vwf.dispatchEvent( bottomID, "test", [ { parameters: true } ], nodeParameters );
- equal( vwf.execute( baseID, "this.properties.argumentss.length" ), 5, "two captures, three bubbles" ); // top capture, middle capture, bottom bubble, middle bubble, top bubble
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[0][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at top capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[1][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at middle capture" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[2][1]" ),
- { nodeParameters: true, bottom: true }, "bottom node-parameters at bottom bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[3][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at middle bubble" );
- deepEqual( vwf.execute( baseID, "this.properties.argumentss[4][1]" ),
- { nodeParameters: true, default: true }, "default node-parameters at top bubble" );
- cleanup();
- start();
- } );
- } );
- // == Helper functions =====================================================================
- // Create parent, child, and grandchild nodes with default and capture- and bubble-phase
- // event handlers.
- function createFixture( /* topResult, middleResult, bottomResult, callback */ ) {
- var args = Array.prototype.slice.call(arguments);
- // Parse the arguments.
- var callback = args.pop(); // callback is the last argument, regardless
- var result = {};
- [ "top", "middle", "bottom" ].forEach( function( which ) {
- result[which] = args.shift();
- if ( result[which] === undefined ) {
- result[which] = false; // explicitly return false ("not handled") by default
- }
- if ( typeof result[which] == "boolean" ) {
- result[which] = { capture: result[which], bubble: result[which] };
- }
-
- } );
- // Prototype for the three nodes.
- vwf.createNode( {
- extends: "http://vwf.example.com/node.vwf",
- properties: {
- targets: [],
- argumentss: [],
- },
- events: {
- test: undefined
- },
- scripts: [
- "var handler = function() { " +
- "return function( eventData, eventNodeData, phase ) { " +
- "var tag = this.name.split( '-' )[0] + ' ' + ( phase || 'bubble' ); " +
- "var result = this.properties.result[ phase || 'bubble' ]; " +
- "Object.getPrototypeOf( this ).properties.targets.push( tag ); " +
- "Object.getPrototypeOf( this ).properties.argumentss.push( Array.prototype.slice.call( arguments ) ); " +
- "if ( result !== undefined ) { " +
- "return result; " + // do an explicit return if a value is provided
- "} " + // otherwise fall out with a default return value of undefined
- "}; " +
- "}; " +
- "this.events.test = this.events.add( handler(), 'capture', this );"
- ],
- }, function( baseID ) {
- // Parent node.
- vwf.createChild( 0, testUtility.uniqueName( "top" ), {
- extends: baseID,
- properties: { result: result.top },
- }, undefined, function( topID ) {
- // Child node.
- vwf.createChild( topID, testUtility.uniqueName( "middle" ), {
- extends: baseID,
- properties: { result: result.middle },
- }, undefined, function( middleID ) {
- // Grandchild node.
- vwf.createChild( middleID, testUtility.uniqueName( "bottom" ), {
- extends: baseID,
- properties: { result: result.bottom },
- }, undefined, function( bottomID ) {
- // Call the test's callback to let it run its assertions. Then clean up after it
- // finishes and calls us back.
- callback( topID, middleID, bottomID, baseID, function() {
- vwf.deleteNode( bottomID );
- vwf.deleteNode( middleID );
- vwf.deleteNode( topID );
- vwf.deleteNode( baseID );
- } );
- } );
- } );
- } );
- } );
- }
- } );
- } );
- </script>
- <link rel="stylesheet" type="text/css" href="qunit.css" />
- </head>
- <body>
- <h1 id="qunit-header">Virtual World Framework</h1>
- <h2 id="qunit-banner"></h2>
- <div id="qunit-testrunner-toolbar"></div>
- <h2 id="qunit-userAgent"></h2>
- <ol id="qunit-tests"></ol>
- <div id="qunit-fixture">test markup, will be hidden</div>
- </body>
- </html>
|