(function() {

  // When testing alternative View implementations, change this varaible.
  var View = Backbone.NativeView;

  var view;

  module("Backbone.NativeView", {

    setup: function() {
      view = new View({el: '#testElement'});
    }

  });

  test("extending", 3, function() {
    var BaseView = Backbone.View.extend();
    var NativeView = Backbone.NativeView;

    var ExtendedView = Backbone.View.extend(Backbone.NativeViewMixin);

    var MixinView = _.extend(BaseView, Backbone.NativeViewMixin);
    MixinView.prototype.initialize = function(options) { this._domEvents = []; }

    ok((new NativeView)._domEvents);
    ok((new ExtendedView)._domEvents);
    ok((new MixinView)._domEvents);
  });

  test("View#$", function() {
    var result = view.$('h1');
    equal(result.length, 1);
    equal(result[0].tagName.toLowerCase(), 'h1');
    equal(result[0].nodeType, 1);
  });

  test("delegate and undelegate", 6, function() {
    var counter1 = 0, counter2 = 0;
    view.delegate('click', function() { counter1++; });
    addEventListener.call(view.el, 'click', function() { counter2++ });

    click(view.el);

    equal(counter1, 1);
    equal(counter2, 1);
    equal(view._domEvents.length, 1);

    view.undelegate('click');

    click(view.el);

    equal(counter1, 1);
    equal(counter2, 2);
    equal(view._domEvents.length, 0);
  });

  test("undelegating only affects matched handlers", 1, function() {
    var counter = 0, handler = function() { counter++ };
    view.delegate('click', 'h1', handler);
    view.delegate('click', 'div', handler);
    view.undelegate('click', 'div');

    _.each(view.$('h1, div'), click);
    equal(counter, 1);
  });


  // Cross-browser helpers
  var addEventListener = typeof Element != 'undefined' && Element.prototype.addEventListener || function(eventName, listener) {
    return this.attachEvent('on' + eventName, listener);
  };

  function click(element) {
    var event;
    if (document.createEvent) {
      event = document.createEvent('MouseEvent');
      var args = [
        'click', true, true,
        // IE 10+ and Firefox require these
        event.view, event.detail, event.screenX, event.screenY, event.clientX,
        event.clientY, event.ctrlKey, event.altKey, event.shiftKey,
        event.metaKey, event.button, event.relatedTarget
      ];
      (event.initMouseEvent || event.initEvent).apply(event, args);
    } else {
      event = document.createEventObject();
      event.type = 'click';
      event.bubbles = true;
      event.cancelable = true;
    }

    if (element.dispatchEvent) {
      return element.dispatchEvent(event);
    }
    element.fireEvent('onclick', event);
  }
})();
@@ -1431,65 +1431,8 @@
 ) %7B%0A
-    var counter = 0, handler = function() %7B counter++ %7D;%0A
     
@@ -1460,23 +1460,40 @@
 , 'h1', 
-handler
+function() %7B ok(true); %7D
 );%0A    v
@@ -1521,23 +1521,41 @@
  'div', 
-handler
+function() %7B ok(false); %7D
 );%0A    v
@@ -1629,31 +1629,8 @@
 k);%0A
-    equal(counter, 1);%0A
   %7D)

clean up matched handlers test. still seems off, will fix