#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="FormBase"/> class.
/// </summary>
public sealed class FormBaseTest : UnitTestBase
{
private ConcreteForm _form;
protected override void SetUp()
{
_form = new ConcreteForm();
}
[Test]
public void TestSetIdOnce()
{
Id<IView> id = "v";
Assert.IsTrue(_form.Id.IsEmpty);
_form.Id = id;
Assert.AreEqual(id, _form.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(_form.Id.IsEmpty);
_form.Id = id;
Assert.AreEqual(id, _form.Id);
_form.Id = "v2";
}
[Test]
public void SetTaskOnce()
{
Task task = CreateTask("t1");
Assert.IsNull(_form.Task);
_form.Task = task;
Assert.AreEqual(task, _form.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()
{
_form.Id = "v";
_form.Task = CreateTask("t1");
_form.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()
{
_form.Id = "v";
Assert.IsFalse(_form.HasController);
IController controller = _form.Controller;
}
[Test]
public void TestSetControllerOnce()
{
IController controller = new MockController();
Assert.IsFalse(_form.HasController);
_form.Controller = controller;
Assert.IsTrue(_form.HasController);
Assert.AreEqual(controller, _form.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()
{
_form.Id = "v";
MockController controller1 = new MockController();
controller1.Id = "c1";
MockController controller2 = new MockController();
controller2.Id = "c2";
_form.Controller = controller1;
_form.Controller = controller2;
}
[Test]
[ExpectedException(typeof(InvalidOperationException), "An attempt was made to access the data for view with ID 'v' before the data was set. The data is not set for a view until after the Initialize() method. You can probably use the Form.Shown event.")]
public void TestGetDataNotSet()
{
_form.Id = "v";
object o = _form.Data;
}
[Test]
public void TestSetData()
{
object data = new object();
((IView) _form).SetData(data);
Assert.AreSame(data, _form.Data);
}
[Test]
public void TestLeaveOpen()
{
//default to not leave open
Assert.AreEqual(false, _form.LeaveOpen);
//if we float then we should leave open by default
_form.IsFloating = true;
Assert.AreEqual(true, _form.LeaveOpen);
_form.IsFloating = false;
Assert.AreEqual(false, _form.LeaveOpen);
//if we are MDI child then we should leave open by default
_form.IsMdiChild = true;
Assert.AreEqual(true, _form.LeaveOpen);
_form.IsMdiChild = false;
Assert.AreEqual(false, _form.LeaveOpen);
//we should be able to explicitly override the default
_form.IsFloating = true;
Assert.AreEqual(true, _form.LeaveOpen);
_form.LeaveOpen = false;
Assert.AreEqual(false, _form.LeaveOpen);
_form.IsMdiChild = true;
Assert.AreEqual(false, _form.LeaveOpen);
}
[Test]
public void TestSetParent()
{
ConcreteForm parentForm = new ConcreteForm();
parentForm.IsMdiContainer = true;
Assert.IsNull(_form.Owner);
Assert.IsNull(_form.MdiParent);
_form.SetParent(parentForm);
//should still be no owner / mdi parent because the child form does not float and is not mdi child
Assert.IsNull(_form.Owner);
Assert.IsNull(_form.MdiParent);
_form.IsFloating = true;
_form.SetParent(parentForm);
//since the child floats, its owner should be set to the parent
Assert.AreEqual(parentForm, _form.Owner);
Assert.IsNull(_form.MdiParent);
_form.IsFloating = false;
_form.IsMdiChild = true;
_form.SetParent(parentForm);
//since the child is an mdi, its mdi parent should be set to the parent
Assert.IsNull(_form.Owner);
Assert.AreEqual(parentForm, _form.MdiParent);
}
[Test]
public void TestChildOpening()
{
bool opening = false;
_form.ChildOpening += delegate
{
opening = true;
};
Assert.IsFalse(opening);
(_form as IFormView).ChildOpening(new ConcreteForm());
Assert.IsTrue(opening);
}
[Test]
public void TestChildClosed()
{
bool closed = false;
_form.ChildClosed += delegate
{
closed = true;
};
Assert.IsFalse(closed);
(_form as IFormView).ChildClosed(new ConcreteForm());
Assert.IsTrue(closed);
}
[Test]
public void TestShow()
{
_form.Show(false);
Assert.IsTrue(_form.Displayed);
Assert.IsFalse(_form.WasModal);
}
[Test]
public void TestShowDialog()
{
_form.Show(true);
Assert.IsTrue(_form.Displayed);
Assert.IsTrue(_form.WasModal);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestSetUserControlContainerNameNull()
{
((IUserControlContainer) _form).SetUserControl(null, null);
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestSetUserControlUserControlNull()
{
((IUserControlContainer) _form).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 FormBase class.")]
public void TestSetUserControlNotAControl()
{
((IUserControlContainer) _form).SetUserControl("containerName", new MockUserControlView());
}
[Test]
[ExpectedException(typeof(ViewException), "A container called 'containerName' was not found in the view with ID ''.")]
public void TestSetUserControlContainerNotFound()
{
((IUserControlContainer) _form).SetUserControl("containerName", new UserControlBaseTest.ConcreteUserControl());
}
[Test]
public void TestSetUserControlValid()
{
Panel panel = new Panel();
panel.Name = "containerName";
_form.Controls.Add(panel);
((IUserControlContainer) _form).SetUserControl("containerName", new UserControlBaseTest.ConcreteUserControl());
}
[Test]
public void TestSetUserControlValid2()
{
Control.ControlCollection controls = _form.Controls;
//create a deep structure of panels with the container at the bottom
for (int i = 0; i < 5; ++i)
{
Panel panel = new Panel();
controls.Add(panel);
controls = panel.Controls;
}
Panel containerPanel = new Panel();
containerPanel.Name = "containerName";
controls.Add(containerPanel);
((IUserControlContainer) _form).SetUserControl("containerName", new UserControlBaseTest.ConcreteUserControl());
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void TestSetCustomConfigurationNull()
{
((ICustomConfigurable) _form).SetCustomConfiguration(null);
}
[Test]
[ExpectedException(typeof(ViewException), "The FormBase class requires an instance of 'Ingenious.Mvc.Windows.Forms.Views.FormBase+CustomConfiguration' for its custom configuration but was given an instance of 'System.String'.")]
public void TestSetCustomConfigurationWrongType()
{
((ICustomConfigurable) _form).SetCustomConfiguration("test");
}
[Test]
public void TestSetCustomConfigurationValid()
{
Ingenious.Mvc.Windows.Forms.Views.FormBase.CustomConfiguration customConfig = new Ingenious.Mvc.Windows.Forms.Views.FormBase.CustomConfiguration();
customConfig.ParentId = "Parent";
customConfig.IsFloating = false;
customConfig.IsMdiChild = true;
customConfig.IsModal = false;
customConfig.LeaveOpen = true;
((ICustomConfigurable) _form).SetCustomConfiguration(customConfig);
Assert.AreEqual("Parent", _form.ParentId.ToString());
Assert.IsFalse(_form.IsFloating);
Assert.IsTrue(_form.IsMdiChild);
Assert.IsFalse(_form.IsModal);
Assert.IsTrue(_form.LeaveOpen);
}
#region Supporting Types
private sealed class ConcreteForm : FormBase
{
private bool _displayed;
private bool _modal;
public new object Data
{
get
{
return base.Data;
}
}
public bool Displayed
{
get
{
return _displayed;
}
}
public bool WasModal
{
get
{
return _modal;
}
}
public ConcreteForm()
{
ShowInTaskbar = false;
Width = 0;
Height = 0;
this.FormBorderStyle = FormBorderStyle.None;
VisibleChanged += new EventHandler(ConcreteForm_VisibleChanged);
}
private void ConcreteForm_VisibleChanged(object sender, EventArgs e)
{
if (Visible)
{
_displayed = true;
_modal = Modal;
Close();
}
}
}
#endregion
}
}
|