| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 | <!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",        "vwf/utility",        "jquery",        "vwf/configuration",        "vwf/kernel/model",        "vwf/model/javascript",        "vwf/model/object",        "vwf/model/stage/log",        "vwf/kernel/view",        "vwf/kernel/utility",        "logger",      ], function( ready, testUtility, utility ) {        // Test events.                  ready( function() {          vwf.initialize(            /* models */ [ "vwf/model/javascript", "vwf/model/object" ],            /*  views */ [ ]          );          // Event firing directly from the kernel          asyncTest( "Event firing kernel", function() {            createFixture( function( nodeID, cleanup ) {              vwf.fireEvent( nodeID, "empty" );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [] }, "no parameters declared, no parameters passed" );              vwf.fireEvent( nodeID, "empty", [ true, 1 ] );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [ true, 1 ] }, "no parameters declared, parameters passed" );              vwf.fireEvent( nodeID, "parameters" );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [] }, "parameters declared, no parameters passed" );              vwf.fireEvent( nodeID, "parameters", [ 'abc', false, 2 ] );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [ 'abc', false, 2 ] }, "parameters declared, parameters passed" );              cleanup();              start();            } );          } );          // Event firing from JavaScript          asyncTest( "Event firing JavaScript", function() {            createFixture( function( nodeID, cleanup ) {              vwf.execute( nodeID, "this.empty()" );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [] }, "no parameters declared, no parameters passed" );              vwf.execute( nodeID, "this.empty( true, 1 )" );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "empty", result: [ true, 1 ] }, "no parameters declared, parameters passed" );              vwf.execute( nodeID, "this.parameters()" );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [] }, "parameters declared, no parameters passed" );              vwf.execute( nodeID, "this.parameters( 'abc', false, 2 )" );              deepEqual( vwf.execute( nodeID, "this.result" ), { event: "parameters", result: [ 'abc', false, 2 ] }, "parameters declared, parameters passed" );              cleanup();              start();            } );          } );          // JavaScript handler with explicit context          asyncTest( "JavaScript handler explicit context", function() {            createFixtureParentChild( function( parentID, childID, baseID, cleanup ) {              vwf.execute( childID, "this.parent.base = this.events.add( function( result ) { this.result = result }, this )" );              vwf.fireEvent( parentID, "base", [ "explicit context" ] );              equal( vwf.execute( childID, "this.result" ), "explicit context", "invoked on child" );              cleanup();              start();            } );          } );          // JavaScript handler with no context          asyncTest( "JavaScript handler no context", function() {            createFixtureParentChild( function( parentID, childID, baseID, cleanup ) {              vwf.execute( childID, "this.parent.base = this.events.add( function( result ) { this.result = result } )" );              vwf.fireEvent( parentID, "base", [ "no context" ] );              equal( vwf.execute( 0, "this.result" ), "no context", "invoked on global root" );              cleanup();              start();            } );          } );          // JavaScript handler with implicit context          asyncTest( "JavaScript handler implicit context", function() {            createFixtureParentChild( function( parentID, childID, baseID, cleanup ) {              vwf.execute( childID, "this.parent.base = function( result ) { this.result = result }" );              vwf.fireEvent( parentID, "base", [ "implicit context" ] );              equal( vwf.execute( parentID, "this.result" ), "implicit context", "invoked on parent" ); // invoked on target              cleanup();              start();            } );          } );          // Event inheritance directly from the kernel          asyncTest( "Event inheritance kernel", function() {            createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {              vwf.execute( derivedID, "this.derived = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( derivedID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( baseID, "this.derived = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( baseID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );              vwf.fireEvent( derivedID, "derived", [ 'derived-' + derivedID ] );              equal( vwf.execute( derivedID, "this.result" ), "derived-" + derivedID, "derived event from derived" );              vwf.fireEvent( derivedID, "base", [ 'base-' + derivedID ] );              equal( vwf.execute( derivedID, "this.result" ), "base-" + derivedID, "base event from derived" );              vwf.fireEvent( baseID, "derived", [ 'derived-' + baseID ] );              equal( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );              vwf.fireEvent( baseID, "base", [ 'base-' + baseID ] );              equal( vwf.execute( baseID, "this.result" ), "base-" + baseID, "base event from base" );                            cleanup();              start();            } );          } );          // Event inheritance from JavaScript direct properties          asyncTest( "Event inheritance JavaScript direct", function() {            createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {              vwf.execute( derivedID, "this.derived = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( derivedID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( baseID, "this.derived && ( this.derived = this.events.add( function( result ) { this.result = result } ), this )" );              vwf.execute( baseID, "this.base = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( derivedID, "this.derived( 'derived-' + this.id )" );              equal( vwf.execute( derivedID, "this.result" ), "derived-" + derivedID, "derived event from derived" );              vwf.execute( derivedID, "this.base( 'base-' + this.id )" );              equal( vwf.execute( derivedID, "this.result" ), "base-" + derivedID, "base event from derived" );              vwf.execute( baseID, "this.derived && this.derived( 'derived-' + this.id )" );              equal( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );              vwf.execute( baseID, "this.base( 'base-' + this.id )" );              equal( vwf.execute( baseID, "this.result" ), "base-" + baseID, "base event from base" );                            cleanup();              start();            } );          } );          // Event inheritance from JavaScript collection properties          asyncTest( "Event inheritance JavaScript collection", function() {            createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {              vwf.execute( derivedID, "this.events.derived = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( derivedID, "this.events.base = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( baseID, "this.events.derived && ( this.events.derived = this.events.add( function( result ) { this.result = result } ), this )" );              vwf.execute( baseID, "this.events.base = this.events.add( function( result ) { this.result = result }, this )" );              vwf.execute( derivedID, "this.events.derived( 'derived-' + this.id )" );              equal( vwf.execute( derivedID, "this.result" ), "derived-" + derivedID, "derived event from derived" );              vwf.execute( derivedID, "this.events.base( 'base-' + this.id )" );              equal( vwf.execute( derivedID, "this.result" ), "base-" + derivedID, "base event from derived" );              vwf.execute( baseID, "this.events.derived && this.events.derived( 'derived-' + this.id )" );              equal( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );              vwf.execute( baseID, "this.events.base( 'base-' + this.id )" );              equal( vwf.execute( baseID, "this.result" ), "base-" + baseID, "base event from base" );                            cleanup();              start();            } );          } );          // Event inheritance with self-targeted handlers          asyncTest( "Event inheritance self-targeted handlers", function() {            createFixtureDerivedBase( function( derivedID, baseID, cleanup ) {              vwf.execute( derivedID, "this.derived = function( result ) { this.result = ( this.result || [] ).concat( result + '-derived' ) }" );              vwf.execute( derivedID, "this.base = function( result ) { this.result = ( this.result || [] ).concat( result + '-derived' ) }" );              vwf.execute( baseID, "this.derived = function( result ) { this.result = ( this.result || [] ).concat( result + '-base' ) }" );              vwf.execute( baseID, "this.base = function( result ) { this.result = ( this.result || [] ).concat( result + '-base' ) }" );              vwf.fireEvent( derivedID, "derived", [ 'derived-' + derivedID ] );              deepEqual( vwf.execute( derivedID, "this.result" ), [ "derived-" + derivedID + "-derived" ], "derived event from derived receives handlers from derived" );              vwf.execute( derivedID, "this.result = []" );              vwf.fireEvent( derivedID, "base", [ 'base-' + derivedID ] );              deepEqual( vwf.execute( derivedID, "this.result" ), [ "base-" + derivedID + "-base", "base-" + derivedID + "-derived" ], "base event from derived receives handlers from base and derived" );              vwf.fireEvent( baseID, "derived", [ 'derived-' + baseID ] );              deepEqual( vwf.execute( baseID, "this.result" ), undefined, "derived event not visible in base" );              vwf.fireEvent( baseID, "base", [ 'base-' + baseID ] );              deepEqual( vwf.execute( baseID, "this.result" ), [ "base-" + baseID + "-base" ], "base event from base receives handlers from base" );                            cleanup();              start();            } );          } );          // Event listeners through JavaScript          asyncTest( "Event listeners JavaScript", function() {            createFixture( function( nodeID, cleanup ) {              // Create two functions on the `result` object to serve as listeners.              vwf.execute( nodeID, "this.result.listener1 = function() { this.result.count1 = ( this.result.count1 || 0 ) + 5 }" );              vwf.execute( nodeID, "this.result.listener2 = function() { this.result.count2 = ( this.result.count2 || 0 ) + 7 }" );              // Add the listeners to the `empty` event and save the listener IDs on the `result` object.              vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener1, this, function( listenerID ) { this.result.listener1ID = listenerID } )" );              vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener2, this, function( listenerID ) { this.result.listener2ID = listenerID } )" );              // Fire the event. Both listeners should execute.              vwf.execute( nodeID, "this.empty()" );              equal( vwf.execute( nodeID, "this.result.count1" ), 5, "" );              equal( vwf.execute( nodeID, "this.result.count2" ), 7, "" );              // Remove the first listener by ID and fire the event. Only the second listener should              // execute. `result.listener1ID` is not valid after the listener has been removed.              vwf.execute( nodeID, "this.empty = this.events.remove( this.result.listener1ID )" );              vwf.execute( nodeID, "this.empty()" );              equal( vwf.execute( nodeID, "this.result.count1" ), 5, "" );              equal( vwf.execute( nodeID, "this.result.count2" ), 14, "" );              // Add the first listener back and fire the event. Both listeners should execute.              vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener1, this )" );              vwf.execute( nodeID, "this.empty()" );              equal( vwf.execute( nodeID, "this.result.count1" ), 10, "" );              equal( vwf.execute( nodeID, "this.result.count2" ), 21, "" );              // Remove the second listener by ID and fire the event. Only the first listener should              // execute. `result.listener2ID` is not valid after the listener has been removed.              vwf.execute( nodeID, "this.empty = this.events.remove( this.result.listener2ID )" );              vwf.execute( nodeID, "this.empty()" );              equal( vwf.execute( nodeID, "this.result.count1" ), 15, "" );              equal( vwf.execute( nodeID, "this.result.count2" ), 21, "" );              // Remove the first listener by function reference and fire the event. Neither              // listener should execute.              vwf.execute( nodeID, "this.empty = this.events.remove( this.result.listener1 )" );              vwf.execute( nodeID, "this.empty()" );              equal( vwf.execute( nodeID, "this.result.count1" ), 15, "" );              equal( vwf.execute( nodeID, "this.result.count2" ), 21, "" );              // Add both listeners back and fire the event. Both listeners should execute.              vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener1, this )" );              vwf.execute( nodeID, "this.empty = this.events.add( this.result.listener2, this )" );              vwf.execute( nodeID, "this.empty()" );              equal( vwf.execute( nodeID, "this.result.count1" ), 20, "" );              equal( vwf.execute( nodeID, "this.result.count2" ), 28, "" );              // Flush all listeners with the context of the target node and fire the event. Both              // listeners should be removed and neither should execute.              vwf.execute( nodeID, "this.empty = this.events.flush( this )" );              vwf.execute( nodeID, "this.empty()" );              equal( vwf.execute( nodeID, "this.result.count1" ), 20, "" );              equal( vwf.execute( nodeID, "this.result.count2" ), 28, "" );              // Do a getEvent to verify that kernel recognizes that event listeners are gone              try {                var testEvent = vwf.getEvent( nodeID, "empty" );                equal( testEvent.listeners, undefined, "" );              } catch( e ) {                equal( e.message, "No error", "" );              }              // Add a listener with no context, which should execute in the context of the global              // node.              vwf.execute( nodeID, "this.empty = function() {}" );              vwf.execute( nodeID, "this.empty()" );  // TODO: how to test?              cleanup();              start();            } );          } );          // Event changes and node state          var changesComponentDescriptor = {            events: {              event: null            },            scripts: [              "this.events.event = this.events.add( function() { return 'original' } )"            ]          };          var changesComponentURI =            testUtility.dataURIFromDescriptor( changesComponentDescriptor );          var changesEventDescriptor = {            listeners: [ {              id: 1,  // from the node's sequence counter when the listener was added              body: "return 'original'",              type: "application/javascript",            } ]          };          var changesEventNewBody = "return 'changed'";          var changesEventDescriptorChanged = {            listeners: [              utility.merge( {}, changesEventDescriptor.listeners[0], {                body: changesEventNewBody              } )            ]          };          asyncTest( "Event changes in node state", function() {            vwf.createNode( changesComponentURI, function( nodeID ) {              deepEqual( vwf.getEvent( nodeID, "event" ), changesEventDescriptor, "Unchanged" );              vwf.setEventListener( nodeID, "event", 1, changesEventNewBody );              deepEqual( vwf.getNode( nodeID ), {                patches:                  changesComponentURI,                events: {                  event: changesEventDescriptorChanged,                }              }, "Event changed" );              vwf.deleteNode( nodeID );              start();            } );          } );          asyncTest( "Event changes to node state", function() {            vwf.createNode( changesComponentURI, function( nodeID ) {              deepEqual( vwf.getEvent( nodeID, "event" ), changesEventDescriptor, "Unchanged" );              vwf.setNode( nodeID, {                patches:                  changesComponentURI,                events: {                  event: changesEventDescriptorChanged,                }              } );              deepEqual( vwf.getEvent( nodeID, "event" ).listeners[0].body, changesEventNewBody, "Event changed from node state" );              vwf.deleteNode( nodeID );              start();            } );          } );          // == Helper functions =====================================================================          // Create a node with events to fire.          function createFixture( callback ) {            vwf.createChild( 0, testUtility.uniqueName( "node" ), {              extends:                "http://vwf.example.com/node.vwf",              events: {                empty: undefined,                parameters: { parameters: [] },              },              scripts: [                "this.result = {}",                "this.empty = this.events.add( function() { this.result.event = 'empty'; this.result.result = Array.prototype.slice.call( arguments ) }, this )",                "this.parameters = this.events.add( function() { this.result.event = 'parameters'; this.result.result = Array.prototype.slice.call( arguments ) }, this )",              ],            }, undefined, function( nodeID ) {                callback( nodeID, function() {                  vwf.deleteNode( nodeID );                } );            } );          }          // Create a parent and two child nodes with events to fire.          function createFixtureParentChild( callback ) {            // Prototype for the nodes.            vwf.createNode( {              extends: "http://vwf.example.com/node.vwf",              events: { base: undefined },            }, function( baseID ) {              // Parent node.              vwf.createChild( 0, testUtility.uniqueName( "parent" ), {                extends: baseID,              }, undefined, function( parentID ) {                // Child node.                vwf.createChild( parentID, testUtility.uniqueName( "child" ), {                  extends: baseID,                }, undefined, function( childID ) {                  callback( parentID, childID, baseID, function() {                    vwf.deleteNode( childID );                    vwf.deleteNode( parentID );                    vwf.deleteNode( baseID );                  } );                } );              } );            } );          }                   // Create a node with two levels of inheritance and events to fire.          function createFixtureDerivedBase( callback ) {            vwf.createNode( {              extends: "http://vwf.example.com/node.vwf",              events: { base: undefined },              scripts: [ ],            }, function( baseID ) {              vwf.createNode( {                extends: baseID,                events: { derived: undefined },                scripts: [ ],              }, function( derivedID ) {                callback( derivedID, baseID, function() {                  vwf.deleteNode( derivedID );                  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>
 |