<commit_before>// Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#library('EventTest');
#import('../../pkg/unittest/unittest.dart');
#import('../../pkg/unittest/html_config.dart');
#import('dart:html');

// TODO(nweiz): Make this private to testEvents when Frog supports closures with
// optional arguments.
eventTest(String name, Event eventFn(), void validate(Event),
    [String type = 'foo']) {
  test(name, () {
    final el = new Element.tag('div');
    var fired = false;
    el.on[type].add((ev) {
      fired = true;
      validate(ev);
    });
    el.on[type].dispatch(eventFn());
    Expect.isTrue(fired, 'Expected event to be dispatched.');
  });
}

main() {
  useHtmlConfiguration();

  // Issue 1005.
  // eventTest('AnimationEvent', () => new AnimationEvent('foo', 'color', 0.5),
  //     (ev) {
  //   Expect.equals('color', ev.animationName);
  //   Expect.equals(0.5, ev.elapsedTime);
  // });

  // Issue 1005.
  // eventTest('BeforeLoadEvent',
  //    () => new BeforeLoadEvent('foo', 'http://example.url'),
  //    (ev) { Expect.equals('http://example.url', ev.url); });

  // Issue 1005.
  // eventTest('CloseEvent',
  //     () => new CloseEvent('foo', 5, 'reason', wasClean: true),
  //     (ev) {
  //   Expect.equals(5, ev.code);
  //   Expect.equals('reason', ev.reason);
  //   Expect.isTrue(ev.wasClean);
  // });

  eventTest('CompositionEvent',
      () => new CompositionEvent('compositionstart', window, 'data'),
      (ev) { Expect.equals('data', ev.data); },
      type: 'compositionstart');

  // initCustomEvent is not yet implemented
  // eventTest('CustomEvent',
  //     () => new CustomEvent('foo', false, false, 'detail'),
  //     (ev) { Expect.equals('detail', ev.detail); });

  // DeviceMotionEvent has no properties to itself, so just test that it doesn't
  // error out on creation and can be dispatched.
  eventTest('DeviceMotionEvent', () => new DeviceMotionEvent('foo'), (ev) {});

  // initDeviceOrientationEvent is not yet implemented
  // eventTest('DeviceOrientationEvent',
  //     () => new DeviceOrientationEvent('foo', 0.1, 0.2, 0.3),
  //     (ev) {
  //   Expect.equals(0.1, ev.alpha);
  //   Expect.equals(0.2, ev.beta);
  //   Expect.equals(0.3, ev.gamma);
  // });

  // Issue 1005.
  // eventTest('ErrorEvent',
  //     () => new ErrorEvent('foo', 'message', 'filename', 10),
  //     (ev) {
  //   Expect.equals('message', ev.message);
  //   Expect.equals('filename', ev.filename);
  //   Expect.equals(10, ev.lineno);
  // });

  eventTest('Event',
      () => new Event('foo', canBubble: false, cancelable: false),
      (ev) {
    Expect.equals('foo', ev.type);
    Expect.isFalse(ev.bubbles);
    Expect.isFalse(ev.cancelable);
  });

  eventTest('HashChangeEvent',
      () => new HashChangeEvent('foo', 'http://old.url', 'http://new.url'),
      (ev) {
    Expect.equals('http://old.url', ev.oldURL);
    Expect.equals('http://new.url', ev.newURL);
  });

  eventTest('KeyboardEvent',
      () => new KeyboardEvent('foo', window, 'key', 10, ctrlKey: true,
          altKey: true, shiftKey: true, metaKey: true, altGraphKey: true),
      (ev) {
    Expect.equals('key', ev.keyIdentifier);
    Expect.equals(10, ev.keyLocation);
    Expect.isTrue(ev.ctrlKey);
    Expect.isTrue(ev.altKey);
    Expect.isTrue(ev.shiftKey);
    Expect.isTrue(ev.metaKey);
    Expect.isTrue(ev.altGraphKey);
  });

  eventTest('MouseEvent',
      // canBubble and cancelable are currently required to avoid dartc
      // complaining about the types of the named arguments.
      () => new MouseEvent('foo', window, 1, 2, 3, 4, 5, 6, canBubble: true,
          cancelable: true, ctrlKey: true, altKey: true, shiftKey: true,
          metaKey: true, relatedTarget: new Element.tag('div')),
      (ev) {
    Expect.equals(1, ev.detail);
    Expect.equals(2, ev.screenX);
    Expect.equals(3, ev.screenY);
    Expect.equals(4, ev.clientX);
    Expect.equals(5, ev.clientY);
    Expect.equals(4, ev.offsetX);  // Same as clientX.
    Expect.equals(5, ev.offsetY);  // Same as clientY.
    Expect.equals(6, ev.button);
    Expect.isTrue(ev.ctrlKey);
    Expect.isTrue(ev.altKey);
    Expect.isTrue(ev.shiftKey);
    Expect.isTrue(ev.metaKey);
    Expect.equals('DIV', ev.relatedTarget.tagName);
  });

  eventTest('MutationEvent',
      () => new MutationEvent('foo', new Element.tag('div'), 'red', 'blue',
          'color', MutationEvent.MODIFICATION),
      (ev) {
    Expect.equals('DIV', ev.relatedNode.tagName);
    Expect.equals('red', ev.prevValue);
    Expect.equals('blue', ev.newValue);
    Expect.equals('color', ev.attrName);
    Expect.equals(MutationEvent.MODIFICATION, ev.attrChange);
  });

  // Issue 1005.
  // eventTest('OverflowEvent',
  //     () => new OverflowEvent(OverflowEvent.BOTH, true, true),
  //     (ev) {
  //   Expect.equals(OverflowEvent.BOTH, ev.orient);
  //   Expect.isTrue(ev.horizontalOverflow);
  //   Expect.isTrue(ev.verticalOverflow);
  // }, type: 'overflowchanged');

  // Issue 1005.
  // eventTest('PageTransitionEvent',
  //     () => new PageTransitionEvent('foo', persisted: true),
  //     (ev) { Expect.isTrue(ev.persisted); });

  // initPopStateEvent is not yet implemented
  // eventTest('PopStateEvent', () => new PopStateEvent('foo', 'state'),
  //     (ev) { Expect.equals('state', ev.state); }

  // Issue 1005.
  // eventTest('ProgressEvent',
  //     // canBubble and cancelable are currently required to avoid dartc
  //     // complaining about the types of the named arguments.<commit_msg>Adding test for DOM Subtree Modified event.<commit_after>// Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#library('EventTest');
#import('../../pkg/unittest/unittest.dart');
#import('../../pkg/unittest/html_config.dart');
#import('dart:html');

// TODO(nweiz): Make this private to testEvents when Frog supports closures with
// optional arguments.
eventTest(String name, Event eventFn(), void validate(Event),
    [String type = 'foo']) {
  test(name, () {
    final el = new Element.tag('div');
    var fired = false;
    el.on[type].add((ev) {
      fired = true;
      validate(ev);
    });
    el.on[type].dispatch(eventFn());
    Expect.isTrue(fired, 'Expected event to be dispatched.');
  });
}

main() {
  useHtmlConfiguration();

  // Issue 1005.
  // eventTest('AnimationEvent', () => new AnimationEvent('foo', 'color', 0.5),
  //     (ev) {
  //   Expect.equals('color', ev.animationName);
  //   Expect.equals(0.5, ev.elapsedTime);
  // });

  // Issue 1005.
  // eventTest('BeforeLoadEvent',
  //    () => new BeforeLoadEvent('foo', 'http://example.url'),
  //    (ev) { Expect.equals('http://example.url', ev.url); });

  // Issue 1005.
  // eventTest('CloseEvent',
  //     () => new CloseEvent('foo', 5, 'reason', wasClean: true),
  //     (ev) {
  //   Expect.equals(5, ev.code);
  //   Expect.equals('reason', ev.reason);
  //   Expect.isTrue(ev.wasClean);
  // });

  eventTest('CompositionEvent',
      () => new CompositionEvent('compositionstart', window, 'data'),
      (ev) { Expect.equals('data', ev.data); },
      type: 'compositionstart');

  // initCustomEvent is not yet implemented
  // eventTest('CustomEvent',
  //     () => new CustomEvent('foo', false, false, 'detail'),
  //     (ev) { Expect.equals('detail', ev.detail); });

  // DeviceMotionEvent has no properties to itself, so just test that it doesn't
  // error out on creation and can be dispatched.
  eventTest('DeviceMotionEvent', () => new DeviceMotionEvent('foo'), (ev) {});

  // initDeviceOrientationEvent is not yet implemented
  // eventTest('DeviceOrientationEvent',
  //     () => new DeviceOrientationEvent('foo', 0.1, 0.2, 0.3),
  //     (ev) {
  //   Expect.equals(0.1, ev.alpha);
  //   Expect.equals(0.2, ev.beta);
  //   Expect.equals(0.3, ev.gamma);
  // });

  // Issue 1005.
  // eventTest('ErrorEvent',
  //     () => new ErrorEvent('foo', 'message', 'filename', 10),
  //     (ev) {
  //   Expect.equals('message', ev.message);
  //   Expect.equals('filename', ev.filename);
  //   Expect.equals(10, ev.lineno);
  // });

  eventTest('Event',
      () => new Event('foo', canBubble: false, cancelable: false),
      (ev) {
    Expect.equals('foo', ev.type);
    Expect.isFalse(ev.bubbles);
    Expect.isFalse(ev.cancelable);
  });

  eventTest('HashChangeEvent',
      () => new HashChangeEvent('foo', 'http://old.url', 'http://new.url'),
      (ev) {
    Expect.equals('http://old.url', ev.oldURL);
    Expect.equals('http://new.url', ev.newURL);
  });

  eventTest('KeyboardEvent',
      () => new KeyboardEvent('foo', window, 'key', 10, ctrlKey: true,
          altKey: true, shiftKey: true, metaKey: true, altGraphKey: true),
      (ev) {
    Expect.equals('key', ev.keyIdentifier);
    Expect.equals(10, ev.keyLocation);
    Expect.isTrue(ev.ctrlKey);
    Expect.isTrue(ev.altKey);
    Expect.isTrue(ev.shiftKey);
    Expect.isTrue(ev.metaKey);
    Expect.isTrue(ev.altGraphKey);
  });

  eventTest('MouseEvent',
      // canBubble and cancelable are currently required to avoid dartc
      // complaining about the types of the named arguments.
      () => new MouseEvent('foo', window, 1, 2, 3, 4, 5, 6, canBubble: true,
          cancelable: true, ctrlKey: true, altKey: true, shiftKey: true,
          metaKey: true, relatedTarget: new Element.tag('div')),
      (ev) {
    Expect.equals(1, ev.detail);
    Expect.equals(2, ev.screenX);
    Expect.equals(3, ev.screenY);
    Expect.equals(4, ev.clientX);
    Expect.equals(5, ev.clientY);
    Expect.equals(4, ev.offsetX);  // Same as clientX.
    Expect.equals(5, ev.offsetY);  // Same as clientY.
    Expect.equals(6, ev.button);
    Expect.isTrue(ev.ctrlKey);
    Expect.isTrue(ev.altKey);
    Expect.isTrue(ev.shiftKey);
    Expect.isTrue(ev.metaKey);
    Expect.equals('DIV', ev.relatedTarget.tagName);
  });

  eventTest('MutationEvent',
      () => new MutationEvent('foo', new Element.tag('div'), 'red', 'blue',
          'color', MutationEvent.MODIFICATION),
      (ev) {
    Expect.equals('DIV', ev.relatedNode.tagName);
    Expect.equals('red', ev.prevValue);
    Expect.equals('blue', ev.newValue);
    Expect.equals('color', ev.attrName);
    Expect.equals(MutationEvent.MODIFICATION, ev.attrChange);
  });

  test('DOMMutationEvent', () {
    var div = new DivElement();
    div.on['DOMSubtreeModified'].add(expectAsync1((DOMMutationEvent e) {}));
    div.nodes.add(new SpanElement());
  });

  // Issue 1005.
  // eventTest('OverflowEvent',
  //     () => new OverflowEvent(OverflowEvent.BOTH, true, true),
  //     (ev) {
  //   Expect.equals(OverflowEvent.BOTH, ev.orient);
  //   Expect.isTrue(ev.horizontalOverflow);
  //   Expect.isTrue(ev.verticalOverflow);
  // }, type: 'overflowchanged');

  // Issue 1005.
  // eventTest('PageTransitionEvent',
  //     () => new PageTransitionEvent('foo', persisted: true),
  //     (ev) { Expect.isTrue(ev.persisted); });

  // initPopStateEvent is not yet implemented
  // eventTest('PopStateEvent', () => new PopStateEvent('foo', 'state'),
  //     (ev) { Expect.equals('state', ev.state); }

  // Issue 1005.
  // eventTest('ProgressEvent',
  //     // canBubble and cancelable are currently required to avoid dartc
  //     // complaining about the types of the named arguments.
{'fraction_non_alphanumeric': 0.18550900277008311, 'fraction_numerical': 0.012551939058171745, 'mean_word_length': 3.029647715381932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6929807', 'n_tokens_mistral': 3891, 'n_tokens_neox': 3726, 'n_words': 804}