Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Building an MVP Framework for .NET. Part 2: Implementing Core Functionality

, 11 Feb 2008
Basing on the concepts introduced in the first part, this article proceeds to implement the core MVP Framework funtionality.
mvcsharp.zip
MVCSharp
API Docs
CleanUpAll.proj
Examples
Basics
ApplicationLogic
Model
Presentation
Web
Global.asax
Win
Properties
Settings.settings
SimpleFormsViewsManager
Properties
TestSimpleFormsViewsManager
ApplicationLogic
Presentation
Properties
Settings.settings
TasksInteraction
ApplicationLogic
Model
Presentation
Web
Global.asax
Win
Properties
Settings.settings
WindowsFormsExample
ApplicationLogic
Presentation
Properties
Settings.settings
MVCSharp
Core
Configuration
Tasks
Views
Tasks
Views
Properties
Webforms
Configuration
Winforms
Configuration
MVCSharp.Tests
Core
Configuration
Tasks
Views
Tasks
MVCSharp.Tests.csproj.user
Properties
Webforms
Configuration
Winforms
Configuration
using System;
using System.Text;
using NUnit.Framework;
using MVCSharp.Core;
using MVCSharp.Core.Tasks;
using MVCSharp.Core.Configuration.Tasks;
using MVCSharp.Core.Configuration.Views;
using MVCSharp.Core.Views;

namespace MVCSharp.Tests.Core
{
    [TestFixture]
    public class TestNavigator
    {
        private Navigator n = new Navigator();
        private StubTask stubTask = new StubTask();
        private TaskInfo ti = new TaskInfo();
        private InteractionPointInfo ipFirstInf = new InteractionPointInfo();
        private InteractionPointInfo ipSecondInf = new InteractionPointInfo();

        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            n.Task = stubTask;
            n.TaskInfo = ti;
            n.ViewsManager = new StubViewsManager();
            ti.InteractionPoints["Some View"] = ipFirstInf;
            ti.InteractionPoints["Some Other View"] = ipSecondInf;
            ipFirstInf.ViewName = "Some View";
            ipSecondInf.ViewName = "Some Other View";
            ipFirstInf.ControllerType = typeof(StubFirstController);
            ipSecondInf.ControllerType = typeof(StubSecondController);
            ipFirstInf.NavTargets["Next"] = ipSecondInf;
            ipFirstInf.NavTargets["Self"] = ipFirstInf;
        }

        [SetUp]
        public void SetUp()
        {
            StubViewsManager.viewActivationCount = 0;
            StubViewsManager.lastViewNameActivated = string.Empty;
            stubTask.viewNameIsFixed = false;
            stubTask.CurrViewName = "Some View";
        }

        [Test]
        public void TestNavigate()
        {
            n.Navigate("Next");

            Assert.AreEqual(1, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some Other View", StubViewsManager.lastViewNameActivated);
            Assert.AreEqual("Some Other View", stubTask.CurrViewName);

            stubTask.CurrViewName = "Some View";
            stubTask.viewNameIsFixed = true;
            n.Navigate("Next");

            Assert.AreEqual(2, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some View", StubViewsManager.lastViewNameActivated);
        }

        [Test]
        public void TestNavigateToSameView()
        {
            n.Navigate("Self");

            Assert.AreEqual(0, StubViewsManager.viewActivationCount);
        }

        [Test]
        public void TestTryNavigateToView()
        {
            n.TryNavigateToView("Some Other View");

            Assert.AreEqual(1, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some Other View", stubTask.CurrViewName);
            Assert.AreEqual("Some Other View", StubViewsManager.lastViewNameActivated);

            n.TryNavigateToView("Some View");

            Assert.AreEqual(2, StubViewsManager.viewActivationCount);
            Assert.AreEqual("Some Other View", stubTask.CurrViewName);
            Assert.AreEqual("Some Other View", StubViewsManager.lastViewNameActivated);
        }

        [Test]
        public void TestNavigateDirectly()
        {
            stubTask.CurrViewName = "Some Other View";
            n.NavigateDirectly("Some View");
            
            Assert.AreEqual(1, StubViewsManager.viewActivationCount);
        }

        [Test]
        public void TestGetController()
        {
            IController firstC = n.GetController("Some View");
            IController secondC = n.GetController("Some Other View");
            
            Assert.IsInstanceOfType(typeof(StubFirstController), firstC);
            Assert.IsInstanceOfType(typeof(StubSecondController), secondC);
            Assert.AreSame(stubTask, firstC.Task);
            Assert.AreSame(firstC, n.GetController("Some View"));
        }

        #region Stubs implementations

        class StubTask : ITask
        {
            private Navigator nav;
            private TasksManager tMan;
            private string currViewName;

            public bool viewNameIsFixed;

            public void OnStart(object param)
            {
            }

            public Navigator Navigator
            {
                get { return nav; }
                set { nav = value; }
            }

            public TasksManager TasksManager
            {
                get { return tMan; }
                set { tMan = value; }
            }

            public string CurrViewName
            {
                get { return currViewName; }
                set { if (!viewNameIsFixed) currViewName = value; }
            }
	
        }

        class StubViewsManager : IViewsManager
        {
            private Navigator navigator;
            public static int viewActivationCount = 0;
            public static string lastViewNameActivated;

            public void ActivateView(string viewName)
            {
                viewActivationCount++;
                lastViewNameActivated = viewName;
            }

            public Navigator Navigator
            {
                get { return navigator; }
                set { navigator = value; }
            }

            public ViewInfoCollection ViewInfos
            {
                get { return null; }
                set { }
            }

            public IView GetView(string viewName)
            { return null; }
        }

        class StubFirstController : StubController
        { }

        class StubSecondController : StubController
        { }

        class StubController : IController
        {
            private ITask task;
            private IView view;

            public ITask Task
            {
                get { return task; }
                set { task = value; }
            }

            public IView View
            {
                get { return view; }
                set { view = value; }
            }
	
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Oleg Zhukov
Team Leader
Russian Federation Russian Federation
Oleg Zhukov, born and living in Russia is Lead Engineer and Project Manager in a company which provides business software solutions. He has graduated from Moscow Institute of Physics and Technology (MIPT) (department of system programming) and has got a M.S. degree in applied physics and mathematics. His research and development work concerns architectural patterns, domain-driven development and systems analysis. Being the adherent of agile methods he applies them extensively in the projects managed by him.

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 11 Feb 2008
Article Copyright 2008 by Oleg Zhukov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid