using System;
using System.Xml;
using SharpVectors.Dom.Events;
#if TEST
using NUnit.Framework;
#endif
namespace SharpVectors.Dom{
/// <summary>
/// Summary description for Element.
/// </summary>
public class Element
: XmlElement
, INode
, IEventTargetSupport
{
#region Private Fields
private EventTarget eventTarget;
#endregion
#region Constructors
public Element(
string prefix,
string localName,
string namespaceUri,
Document document)
: base(prefix, localName, namespaceUri, document)
{
eventTarget = new EventTarget(this);
}
#endregion
#region IEventTarget interface
#region Methods
#region DOM Level 2
void IEventTarget.AddEventListener(
string type,
EventListener listener,
bool useCapture)
{
eventTarget.AddEventListener(type, listener, useCapture);
}
void IEventTarget.RemoveEventListener(
string type,
EventListener listener,
bool useCapture)
{
eventTarget.RemoveEventListener(type, listener, useCapture);
}
bool IEventTarget.DispatchEvent(
IEvent @event)
{
return eventTarget.DispatchEvent(@event);
}
#endregion
#region DOM Level 3 Experimental
void IEventTarget.AddEventListenerNs(
string namespaceUri,
string type,
EventListener listener,
bool useCapture,
object eventGroup)
{
eventTarget.AddEventListenerNs(namespaceUri, type, listener, useCapture, eventGroup);
}
void IEventTarget.RemoveEventListenerNs(
string namespaceUri,
string type,
EventListener listener,
bool useCapture)
{
eventTarget.RemoveEventListenerNs(namespaceUri, type, listener, useCapture);
}
bool IEventTarget.WillTriggerNs(
string namespaceUri,
string type)
{
return eventTarget.WillTriggerNs(namespaceUri, type);
}
bool IEventTarget.HasEventListenerNs(
string namespaceUri,
string type)
{
return eventTarget.HasEventListenerNs(namespaceUri, type);
}
#endregion
#endregion
#endregion
#region NON-DOM
void IEventTargetSupport.FireEvent(
IEvent @event)
{
eventTarget.FireEvent(@event);
}
#endregion
#region Unit tests
#if TEST
[TestFixture]
public class ElementTests
{
Document doc;
int increment;
[SetUp]
public void SetUp()
{
if (doc == null)
{
doc = new Document();
doc.LoadXml("<?xml version='1.0'?><doc><e1><e1a/><e1b/></e1><e2><e2a/><e2b/></e2></doc>");
}
}
public void OnMouseMove(
IEvent @event)
{
increment++;
}
[Test]
public void TestConstructor()
{
increment = 0;
IEventTarget eventTarget = (IEventTarget)doc.DocumentElement;
// Add first event listener
eventTarget.AddEventListener("mousemove", new EventListener(OnMouseMove), false);
eventTarget.DispatchEvent(new Event("uievent", "mousemove", true, false));
Assert.AreEqual(1, increment);
// Add second event listener
// "If multiple identical EventListeners are registered on the same EventTarget with the same
// parameters the duplicate instances are discarded. They do not cause the EventListener to
// be called twice and since they are discarded they do not need to be removed with the removeEventListener method."
eventTarget.AddEventListener("mousemove", new EventListener(OnMouseMove), false);
eventTarget.DispatchEvent(new Event("uievent", "mousemove", true, false));
Assert.AreEqual(2, increment);
// Remove first event listener
eventTarget.RemoveEventListener("mousemove", new EventListener(OnMouseMove), false);
eventTarget.DispatchEvent(new Event("uievent", "mousemove", true, false));
Assert.AreEqual(2, increment);
// Remove second event listener
// "Calling removeEventListener with arguments which do not identify any currently registered EventListener on the EventTarget has no effect."
eventTarget.RemoveEventListener("mousemove", new EventListener(OnMouseMove), false);
eventTarget.DispatchEvent(new Event("uievent", "mousemove", true, false));
Assert.AreEqual(2, increment);
}
}
#endif//TEST
#endregion Unit tests
}
}
|