FormsViewManagerTest.cs :  » Web-Frameworks » Ingenious-MVC » Ingenious » Mvc » UnitTest » Windows » Forms » ViewManagers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Frameworks » Ingenious MVC 
Ingenious MVC » Ingenious » Mvc » UnitTest » Windows » Forms » ViewManagers » FormsViewManagerTest.cs
#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.Drawing;
using System.Threading;
using System.Windows.Forms;
using NUnit.Framework;
using Ingenious.Mvc.Configuration;
using Ingenious.Mvc.ViewManagers;
using Ingenious.Mvc.Windows.Forms.ViewManagers;
using Ingenious.Mvc.Windows.Forms.Views;
using Ingenious.Mvc.UnitTest.MockTypes;

namespace Ingenious.Mvc.UnitTest.Windows.Forms.ViewManagers{
  /// <summary>
  /// Unit tests the <see cref="FormsViewManager"/> class.
  /// </summary>
  public sealed class FormsViewManagerTest : UnitTestBase
  {
    private bool _display = false;
    private FormsViewManager _viewManager;
    private ViewDisplayer _viewDisplayer;

    protected override void SetUp()
    {
      _viewManager = new FormsViewManager("vm", TaskManager);

      if (_display)
      {
        _viewDisplayer = new ViewDisplayer(_viewManager);
        _viewDisplayer.Show();
      }
    }

    protected override void TearDown()
    {
      if (_display)
      {
        _viewDisplayer.Close();
      }
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void TestActivateTaskNull()
    {
      _viewManager.Activate(null, "v", null);
    }

    [Test]
    [ExpectedException(typeof(IdException), "An empty ID of type Ingenious.Mvc.IView was illegally provided for argument 'viewId'.")]
    public void TestActivateViewIdEmpty()
    {
      _viewManager.Activate(CreateTask(), Id<IView>.Empty, null);
    }

    [Test]
    public void TestActivateValid()
    {
      Configure("TestValid1");

      Task task = TaskManager.StartTask("navigator");
      FormsViewManager viewManager = (FormsViewManager) TaskManager.GetViewManager("viewManager");

      //test without view data
      MockFormView view = (MockFormView) viewManager.Activate(task, "view1", null);
      Assert.IsNotNull(view);
      Assert.AreEqual((Id<IView>) "view1", view.Id);
      //test with view data
      view = (MockFormView) viewManager.Activate(task, "view2", "view data");
      Assert.IsNotNull(view);
      Assert.AreEqual((Id<IView>) "view2", view.Id);
      Assert.AreEqual("view data", view.Data);
    }

    [Test]
    [ExpectedException(typeof(ViewManagerException), "The view with ID 'view1' has type 'Ingenious.Mvc.UnitTest.MockTypes.MockView' as its type, but that type does not implement the required interface 'Ingenious.Mvc.Windows.Forms.Views.IFormView'.")]
    public void TestTypeDoesntImplementIFormView()
    {
      Configure("ViewDoesntImplementRequiredInterface");
      TaskManager.StartTask("navigator");
    }

    [Test]
    [ExpectedException(typeof(ViewManagerException), "The task with ID 't' could not be found in the view manager with ID 'vm'.")]
    public void TestGetActiveViewTaskNotFound()
    {
      _viewManager.GetActiveView(CreateTask("t"));
    }

    [Test]
    public void TestValid1()
    {
      /*
       * view1 ----> view2
       */

      Configure("TestValid1");

      Task task = TaskManager.StartTask("navigator");
      INavigator navigator = TaskManager.GetNavigatorForTask(task);
      FormsViewManager viewManager = (FormsViewManager) TaskManager.GetViewManager("viewManager");

      MockFormView view1 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view1", view1.Id.ToString());
      Assert.IsTrue(view1.WasDisplayed);
      Assert.IsFalse(view1.IsModal);
      Assert.IsTrue(view1.ParentId.IsEmpty);

      //now navigate to view2
      navigator.NavigateTo("view2");
      MockFormView view2 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view2", view2.Id.ToString());
      Assert.IsTrue(view2.WasDisplayed);
      Assert.IsFalse(view2.IsModal);
      Assert.IsTrue(view1.ParentId.IsEmpty);
      Assert.IsTrue(view1.WasClosed);
    }

    [Test]
    public void TestValid2()
    {
      /*
       * view1 ----> view2 (modal)
       */

      Configure("TestValid2");

      Task task = TaskManager.StartTask("navigator");
      INavigator navigator = TaskManager.GetNavigatorForTask(task);
      FormsViewManager viewManager = (FormsViewManager) TaskManager.GetViewManager("viewManager");

      MockFormView view1 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view1", view1.Id.ToString());
      Assert.IsTrue(view1.WasDisplayed);
      Assert.IsFalse(view1.IsModal);
      Assert.IsTrue(view1.ParentId.IsEmpty);

      //now navigate to view2
      navigator.NavigateTo("view2");
      MockFormView view2 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view2", view2.Id.ToString());
      Assert.IsTrue(view2.WasDisplayed);
      Assert.IsTrue(view2.IsModal);
      Assert.IsTrue(view2.ParentId.IsEmpty);
      Assert.IsTrue(view1.WasClosed);
    }

    [Test]
    public void TestValid3()
    {
      /*
       * view1 (leave open) ----> view2 (modal)
       */

      Configure("TestValid3");

      Task task = TaskManager.StartTask("navigator");
      INavigator navigator = TaskManager.GetNavigatorForTask(task);
      FormsViewManager viewManager = (FormsViewManager) TaskManager.GetViewManager("viewManager");

      MockFormView view1 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view1", view1.Id.ToString());
      Assert.IsTrue(view1.WasDisplayed);
      Assert.IsFalse(view1.IsModal);
      Assert.IsTrue(view1.ParentId.IsEmpty);

      //now navigate to view2
      navigator.NavigateTo("view2");
      MockFormView view2 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view2", view2.Id.ToString());
      Assert.IsTrue(view2.WasDisplayed);
      Assert.IsTrue(view2.IsModal);
      Assert.IsTrue(view2.ParentId.IsEmpty);
      Assert.IsFalse(view1.WasClosed);
    }

    [Test]
    public void TestValid4()
    {
      /*
       * view1 ----> view2 (parent=view1)
       */

      Configure("TestValid4");

      Task task = TaskManager.StartTask("navigator");
      INavigator navigator = TaskManager.GetNavigatorForTask(task);
      FormsViewManager viewManager = (FormsViewManager) TaskManager.GetViewManager("viewManager");

      MockFormView view1 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view1", view1.Id.ToString());
      Assert.IsTrue(view1.WasDisplayed);
      Assert.IsFalse(view1.IsModal);
      Assert.IsTrue(view1.ParentId.IsEmpty);
      Assert.IsNull(view1.LastChildOpening);

      //now navigate to view2
      navigator.NavigateTo("view2");
      MockFormView view2 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view2", view2.Id.ToString());
      Assert.IsTrue(view2.WasDisplayed);
      Assert.AreEqual(view1.Id, view2.ParentId);
      Assert.IsFalse(view1.WasClosed);
      Assert.AreEqual(view2, view1.LastChildOpening);

      //close view2 and make sure the parent view is notified
      view2.Close();
      Assert.AreEqual(view2, view1.LastChildClosed);
    } 

    [Test]
    public void TestValid5()
    {
      /*
       * view1 ----> view2 (floating)
       *       ----> view3 (mdi child)
       */

      Configure("TestValid5");

      Task task = TaskManager.StartTask("navigator");
      INavigator navigator = TaskManager.GetNavigatorForTask(task);
      FormsViewManager viewManager = (FormsViewManager) TaskManager.GetViewManager("viewManager");

      MockFormView view1 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view1", view1.Id.ToString());
      Assert.IsTrue(view1.WasDisplayed);
      Assert.IsFalse(view1.IsModal);
      Assert.IsTrue(view1.ParentId.IsEmpty);

      //now navigate to view2
      navigator.NavigateTo("view2");
      MockFormView view2 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view2", view2.Id.ToString());
      Assert.IsTrue(view2.WasDisplayed);
      Assert.IsFalse(view2.IsModal);
      Assert.AreEqual(view1.Id, view2.ParentId);
      Assert.IsFalse(view1.WasClosed);

      //head back to view1
      view1.Activate();

      //now navigate to view3
      navigator.NavigateTo("view3");
      MockFormView view3 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view3", view3.Id.ToString());
      Assert.IsTrue(view3.WasDisplayed);
      Assert.IsFalse(view3.IsModal);
      Assert.AreEqual(view1.Id, view3.ParentId);
      Assert.IsFalse(view1.WasClosed);
      Assert.IsFalse(view2.WasClosed);
    }

    [Test]
    public void TestValid6()
    {
      /*
       * view1 (leave open) ----> view4 (leave open) ----> view7 (modal)
       *  +----> view2             +----> view5
       *  +----> view3             +----> view6
       */

      Configure("TestValid6");

      Task task = TaskManager.StartTask("navigator");
      INavigator navigator = TaskManager.GetNavigatorForTask(task);
      FormsViewManager viewManager = (FormsViewManager) TaskManager.GetViewManager("viewManager");

      MockFormView view1 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view1", view1.Id.ToString());
      Assert.IsTrue(view1.WasDisplayed);
      Assert.IsFalse(view1.IsModal);
      Assert.IsTrue(view1.ParentId.IsEmpty);

      //now navigate to view2
      navigator.NavigateTo("view2");
      MockFormView view2 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view2", view2.Id.ToString());

      //head back to view1
      view1.Activate();

      //now navigate to view3
      navigator.NavigateTo("view3");
      MockFormView view3 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view3", view3.Id.ToString());

      Assert.AreEqual(view1, view2.Parent);
      Assert.AreEqual(view1, view3.Parent);
      Assert.IsTrue(view1.WasDisplayed);
      Assert.IsTrue(view2.WasDisplayed);
      Assert.IsTrue(view3.WasDisplayed);
      Assert.IsFalse(view1.WasClosed);

      //head back to view1
      view1.Activate();

      //now navigate to view4
      navigator.NavigateTo("view4");
      MockFormView view4 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view4", view4.Id.ToString());

      //now navigate to view5
      navigator.NavigateTo("view5");
      MockFormView view5 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view5", view5.Id.ToString());

      //head back to view4
      view4.Activate();

      //now navigate to view6
      navigator.NavigateTo("view6");
      MockFormView view6 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view6", view6.Id.ToString());

      Assert.AreEqual(view4, view5.Parent);
      Assert.AreEqual(view4, view6.Parent);
      Assert.IsTrue(view4.WasDisplayed);
      Assert.IsTrue(view5.WasDisplayed);
      Assert.IsTrue(view6.WasDisplayed);
      Assert.IsFalse(view1.WasClosed);
      Assert.IsFalse(view4.WasClosed);

      //head back to view4
      view4.Activate();

      //now navigate to view7
      navigator.NavigateTo("view7");
      MockFormView view7 = (MockFormView) viewManager.ActiveView;
      Assert.AreEqual("view7", view7.Id.ToString());
      
      Assert.IsTrue(view7.WasDisplayed);
      Assert.IsTrue(view7.WasModal);
      Assert.IsFalse(view4.WasClosed);
    }

    #region Supporting Types

    /// <summary>
    /// Displays a graphical representation of forms and their transitions.
    /// </summary>
    private sealed class ViewDisplayer : Form
    {
      private IList<IView> _views;
      private IDictionary<Id<IView>, ViewInfo> _viewInfos;

      public ViewDisplayer(FormsViewManager viewManager)
      {
        FormBorderStyle = FormBorderStyle.None;
        WindowState = FormWindowState.Maximized;
        _views = new List<IView>();
        _viewInfos = new Dictionary<Id<IView>, ViewInfo>();
        //hook into events so we can display a graph of what the views and their relationships are
        viewManager.ViewCreated += new EventHandler<ViewEventArgs>(viewManager_ViewCreated);
        viewManager.ViewActivated += new EventHandler<ViewEventArgs>(viewManager_ViewActivated);
        viewManager.ViewDisplayed += new EventHandler<ViewEventArgs>(viewManager_ViewDisplayed);
        viewManager.ViewClosed += new EventHandler<ViewEventArgs>(viewManager_ViewClosed);
        this.Closing += new System.ComponentModel.CancelEventHandler(ViewDisplayer_Closing);
      }

      protected override void OnPaint(PaintEventArgs e)
      {
        base.OnPaint(e);

        IList<IView> views = new List<IView>(_views);

        //paint all views
        while (views.Count > 0)
        {
          PaintView((IFormView) views[0], views, e.Graphics);
        }
      }

      private void PaintView(IFormView view, IList<IView> views, Graphics g)
      {
        //paint view
        views.Remove(view);

        //paint children of view
        foreach (IFormView childView in views)
        {
          if (childView.ParentId == view.Id)
          {
            views.Remove(childView);
          }
        }

        //TODO: paint the views
      }

      private void viewManager_ViewCreated(object sender, ViewEventArgs e)
      {
        ViewInfo viewInfo = new ViewInfo();
        viewInfo.View = (IFormView) e.View;
        _viewInfos[e.View.Id] = viewInfo;
        _views.Add(e.View);
        Invalidate();
      }

      private void viewManager_ViewClosed(object sender, ViewEventArgs e)
      {
        _viewInfos[e.View.Id].Closed = true;
        Invalidate();
      }

      private void viewManager_ViewDisplayed(object sender, ViewEventArgs e)
      {
        Invalidate();
      }

      private void viewManager_ViewActivated(object sender, ViewEventArgs e)
      {
        Invalidate();
      }

      private void ViewDisplayer_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
        Thread.Sleep(1000);
      }

      private sealed class ViewInfo
      {
        public IFormView View;
        public bool Closed;
      }
    }

    #endregion
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.