Home | History | Annotate | Download | only in events
      1 
      2 /*
      3 Copyright  2001-2004 World Wide Web Consortium, 
      4 (Massachusetts Institute of Technology, European Research Consortium 
      5 for Informatics and Mathematics, Keio University). All 
      6 Rights Reserved. This work is distributed under the W3C Software License [1] in the 
      7 hope that it will be useful, but WITHOUT ANY WARRANTY; without even 
      8 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
      9 
     10 [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
     11 */
     12 
     13 
     14 
     15    /**
     16     *  Gets URI that identifies the test.
     17     *  @return uri identifier of test
     18     */
     19 function getTargetURI() {
     20       return "http://www.w3.org/2001/DOM-Test-Suite/level2/events/dispatchEvent13";
     21    }
     22 
     23 var docsLoaded = -1000000;
     24 var builder = null;
     25 
     26 //
     27 //   This function is called by the testing framework before
     28 //      running the test suite.
     29 //
     30 //   If there are no configuration exceptions, asynchronous
     31 //        document loading is started.  Otherwise, the status
     32 //        is set to complete and the exception is immediately
     33 //        raised when entering the body of the test.
     34 //
     35 function setUpPage() {
     36    setUpPageStatus = 'running';
     37    try {
     38      //
     39      //   creates test document builder, may throw exception
     40      //
     41      builder = createConfiguredBuilder();
     42 
     43       docsLoaded = 0;
     44       
     45       var docRef = null;
     46       if (typeof(this.doc) != 'undefined') {
     47         docRef = this.doc;
     48       }
     49       docsLoaded += preload(docRef, "doc", "hc_staff");
     50         
     51        if (docsLoaded == 1) {
     52           setUpPageStatus = 'complete';
     53        }
     54     } catch(ex) {
     55     	catchInitializationError(builder, ex);
     56         setUpPageStatus = 'complete';
     57     }
     58 }
     59 
     60 
     61 
     62 //
     63 //   This method is called on the completion of 
     64 //      each asychronous load started in setUpTests.
     65 //
     66 //   When every synchronous loaded document has completed,
     67 //      the page status is changed which allows the
     68 //      body of the test to be executed.
     69 function loadComplete() {
     70     if (++docsLoaded == 1) {
     71         setUpPageStatus = 'complete';
     72     }
     73 }
     74 
     75 
     76      /**
     77       *    Inner class implementation for variable listener1 
     78       */
     79 var listener1;
     80 
     81 /**
     82         * Constructor
     83 
     84         * @param events Value from value attribute of nested var element
     85         * @param listeners Value from value attribute of nested var element
     86         */
     87 	      
     88 function EventListenerN1003B(events, listeners) { 
     89            this.events = events;
     90            this.listeners = listeners;
     91            }
     92    
     93         /**
     94          *    
     95 This method is called whenever an event occurs of the type for which theEventListenerinterface was registered.
     96 
     97          * @param evt 
     98 TheEventcontains contextual information about the event. It also contains thestopPropagationand preventDefaultmethods which are used in determining the event's flow and default action.
     99 
    100          */
    101 EventListenerN1003B.prototype.handleEvent = function(evt) {
    102          //
    103          //   bring class variables into function scope
    104          //
    105         var events = listener1.events;
    106            var listeners = listener1.listeners;
    107            var target;
    108       var listener;
    109       events[events.length] = evt;
    110 target = evt.currentTarget;
    111 
    112       for(var indexN10065 = 0;indexN10065 < listeners.length; indexN10065++) {
    113       listener = listeners[indexN10065];
    114       target.removeEventListener("foo", listener.handleEvent, false);
    115 	 
    116 	}
    117    }
    118 
    119      /**
    120       *    Inner class implementation for variable listener2 
    121       */
    122 var listener2;
    123 
    124 /**
    125         * Constructor
    126 
    127         * @param events Value from value attribute of nested var element
    128         * @param listeners Value from value attribute of nested var element
    129         */
    130 	      
    131 function EventListenerN10074(events, listeners) { 
    132            this.events = events;
    133            this.listeners = listeners;
    134            }
    135    
    136         /**
    137          *    
    138 This method is called whenever an event occurs of the type for which theEventListenerinterface was registered.
    139 
    140          * @param evt 
    141 TheEventcontains contextual information about the event. It also contains thestopPropagationand preventDefaultmethods which are used in determining the event's flow and default action.
    142 
    143          */
    144 EventListenerN10074.prototype.handleEvent = function(evt) {
    145          //
    146          //   bring class variables into function scope
    147          //
    148         var events = listener2.events;
    149            var listeners = listener2.listeners;
    150            var target;
    151       var listener;
    152       events[events.length] = evt;
    153 target = evt.currentTarget;
    154 
    155       for(var indexN10098 = 0;indexN10098 < listeners.length; indexN10098++) {
    156       listener = listeners[indexN10098];
    157       target.removeEventListener("foo", listener.handleEvent, false);
    158 	 
    159 	}
    160    }
    161 
    162 /**
    163 * 
    164 Two listeners are registered on the same target, each of which will remove both itself and 
    165 the other on the first event.  Only one should see the event since event listeners
    166 can never be invoked after being removed.
    167 
    168 * @author Curt Arnold
    169 * @see http://www.w3.org/TR/DOM-Level-2-Events/events#Events-EventTarget-dispatchEvent
    170 * @see http://www.w3.org/TR/DOM-Level-2-Events/events#xpointer(id('Events-EventTarget-dispatchEvent')/raises/exception[@name='EventException']/descr/p[substring-before(.,':')='UNSPECIFIED_EVENT_TYPE_ERR'])
    171 */
    172 function dispatchEvent13() {
    173    var success;
    174     if(checkInitialization(builder, "dispatchEvent13") != null) return;
    175     var doc;
    176       var target;
    177       var evt;
    178       var preventDefault;
    179       var listeners = new Array();
    180 
    181       var events = new Array();
    182 
    183       listener1 = new EventListenerN1003B(events, listeners);
    184 	  
    185       listener2 = new EventListenerN10074(events, listeners);
    186 	  
    187       
    188       var docRef = null;
    189       if (typeof(this.doc) != 'undefined') {
    190         docRef = this.doc;
    191       }
    192       doc = load(docRef, "doc", "hc_staff");
    193       listeners[listeners.length] = listener1;
    194 listeners[listeners.length] = listener2;
    195 doc.addEventListener("foo", listener1.handleEvent, false);
    196       doc.addEventListener("foo", listener2.handleEvent, false);
    197       evt = doc.createEvent("Events");
    198       evt.initEvent("foo",true,false);
    199       preventDefault = doc.dispatchEvent(evt);
    200       assertSize("eventCount",1,events);
    201 
    202 }
    203 
    204 
    205 
    206 
    207 function runTest() {
    208    dispatchEvent13();
    209 }
    210