#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.Windows.Forms;
using NUnit.Framework;
using Ingenious.Mvc.Configuration;
using Ingenious.Mvc.Views;
using Ingenious.Mvc.Windows.Forms.Views;
using Ingenious.Mvc.UnitTest.MockTypes;
namespace Ingenious.Mvc.UnitTest.Windows.Forms.Views{
/// <summary>
/// Unit tests the <see cref="UserControlBase"/> class.
/// </summary>
public sealed class UserControlBaseTest : UnitTestBase
{
private ConcreteUserControl _userControl;
protected override void SetUp()
{
_userControl = new ConcreteUserControl();
}
[Test]
public void TestSetIdOnce()
{
Id<IView> id = "v";
Assert.IsTrue(_userControl.Id.IsEmpty);
_userControl.Id = id;
Assert.AreEqual(id, _userControl.Id);
}
[Test]
[ExpectedException(typeof(InvalidOperationException), "The ID for this view has already been set to 'v' and so cannot be set to 'v2'.")]
public void TestSetIdTwice()
{
Id<IView> id = "v";
Assert.IsTrue(_userControl.Id.IsEmpty);
_userControl.Id = id;
Assert.AreEqual(id, _userControl.Id);
_userControl.Id = "v2";
}
[Test]
public void SetTaskOnce()
{
Task task = CreateTask("t1");
Assert.IsNull(_userControl.Task);
_userControl.Task = task;
Assert.AreEqual(task, _userControl.Task);
}
[Test]
[ExpectedException(typeof(InvalidOperationException), "The view with ID 'v' has already had its task set to 't1', so it cannot be set to 't2'.")]
public void TestSetTaskTwice()
{
_userControl.Id = "v";
_userControl.Task = CreateTask("t1");
_userControl.Task = CreateTask("t2");
}
[Test]
[ExpectedException(typeof(InvalidOperationException), "The view with ID 'v' does not have a controller associated with it (ie. HasController is returning false). Ensure your configuration associates a controller with this view.")]
public void TestGetControllerNotSet()
{
_userControl.Id = "v";
Assert.IsFalse(_userControl.HasController);
IController controller = _userControl.Controller;
}
[Test]
public void SetControllerOnce()
{
IController controller = new MockController();
Assert.IsFalse(_userControl.HasController);
_userControl.Controller = controller;
Assert.IsTrue(_userControl.HasController);
Assert.AreEqual(controller, _userControl.Controller);
}
[Test]
[ExpectedException(typeof(InvalidOperationException), "The view with ID 'v' has already had its controller set to 'c1', so it cannot be set to 'c2'.")]
public void TestSetControllerTwice()
{
_userControl.Id = "v";
MockController controller1 = new MockController();
controller1.Id = "c1";
MockController controller2 = new MockController();
controller2.Id = "c2";
_userControl.Controller = controller1;
_userControl.Controller = controller2;
}
[Test]
public void TestSetData()
{
object data = new object();
_userControl.SetData(data);
Assert.AreSame(data, _userControl.Data);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestSetUserControlContainerNameNull()
{
((IUserControlContainer) _userControl).SetUserControl(null, null);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestSetUserControlUserControlNull()
{
((IUserControlContainer) _userControl).SetUserControl("containerName", null);
}
[Test]
[ExpectedException(typeof(ViewException), "An attempt was made to set the user control view (with ID '') for a user control container in the view with ID ''. However, the supplied user control view is not a System.Windows.Forms.Control subclass as required by the UserControlBase class.")]
public void TestSetUserControlNotAControl()
{
((IUserControlContainer) _userControl).SetUserControl("containerName", new MockUserControlView());
}
[Test]
[ExpectedException(typeof(ViewException), "A container called 'containerName' was not found in the view with ID ''.")]
public void TestSetUserControlContainerNotFound()
{
((IUserControlContainer) _userControl).SetUserControl("containerName", new UserControlBaseTest.ConcreteUserControl());
}
[Test]
public void TestSetUserControlValid()
{
Panel panel = new Panel();
panel.Name = "containerName";
_userControl.Controls.Add(panel);
((IUserControlContainer) _userControl).SetUserControl("containerName", new UserControlBaseTest.ConcreteUserControl());
}
#region Supporting Types
public sealed class ConcreteUserControl : UserControlBase
{
public new object Data
{
get
{
return base.Data;
}
}
}
#endregion
}
}
|