#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Ingenious.Mvc.Util;
namespace Ingenious.Mvc.UnitTest.Util{
[TestFixture]
public sealed class EventHandlerTest : UnitTestBase
{
private object _sender;
private MockEventArgs _e;
private int _handlerCount;
protected override void SetUp()
{
_sender = null;
_e = null;
_handlerCount = 0;
}
[Test]
public void TestDelegateNull()
{
//we can only make sure that no exceptions are thrown
EventHelper.Raise<EventArgs>(null, this, EventArgs.Empty);
EventHelper.Raise<EventArgs>(null, null, EventArgs.Empty);
}
[Test]
public void TestSenderNull()
{
Assert.IsNull(_sender);
Assert.IsNull(_e);
EventHandler<MockEventArgs> del = new EventHandler<MockEventArgs>(Handler);
EventHelper.Raise(del, null, new MockEventArgs("my data"));
Assert.IsNull(_sender);
Assert.IsNotNull(_e);
Assert.AreEqual("my data", _e.Data);
Assert.AreEqual(1, _handlerCount);
}
[Test]
public void TestSingleHandler()
{
Assert.IsNull(_e);
EventHandler<MockEventArgs> del = new EventHandler<MockEventArgs>(Handler);
EventHelper.Raise(del, this, new MockEventArgs("my data"));
Assert.AreEqual(this, _sender);
Assert.IsNotNull(_e);
Assert.AreEqual("my data", _e.Data);
Assert.AreEqual(1, _handlerCount);
}
[Test]
public void TestMultipleHandlers()
{
Assert.IsNull(_e);
EventHandler<MockEventArgs> del = new EventHandler<MockEventArgs>(Handler);
for (int i = 0; i < 50; ++i)
{
del += new EventHandler<MockEventArgs>(Handler);
}
EventHelper.Raise(del, this, new MockEventArgs("my data"));
Assert.AreEqual(this, _sender);
Assert.IsNotNull(_e);
Assert.AreEqual("my data", _e.Data);
Assert.AreEqual(51, _handlerCount);
}
[Test]
public void TestCreateEventArgsCallback()
{
bool callback = false;
EventHandler<MockEventArgs> del = new EventHandler<MockEventArgs>(Handler);
EventHelper.Raise(del, this, delegate
{
callback = true;
return new MockEventArgs("test");
});
Assert.IsTrue(callback);
Assert.AreEqual(this, _sender);
Assert.IsNotNull(_e);
Assert.AreEqual("test", _e.Data);
Assert.AreEqual(1, _handlerCount);
}
private void Handler(object sender, MockEventArgs e)
{
_sender = sender;
_e = e;
++_handlerCount;
}
#region Supporting Types
private sealed class MockEventArgs : EventArgs
{
private string _data;
public string Data
{
get
{
return _data;
}
}
internal MockEventArgs(string data)
{
_data = data;
}
}
#endregion
}
}
|