PageRenderTime 26ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/V2/trunk/RI/Desktop/StockTraderRI.Modules.News.Tests/Controllers/NewsControllerFixture.cs

#
C# | 152 lines | 112 code | 24 blank | 16 comment | 0 complexity | b9c9190f60887aa67b9124192758bc14 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using Microsoft.Practices.Composite.Events;
 19using Microsoft.Practices.Composite.Presentation.Events;
 20using Microsoft.VisualStudio.TestTools.UnitTesting;
 21using StockTraderRI.Infrastructure;
 22using StockTraderRI.Infrastructure.Models;
 23using StockTraderRI.Modules.News.Article;
 24using StockTraderRI.Modules.News.Controllers;
 25using StockTraderRI.Modules.News.Tests.Mocks;
 26using Microsoft.Practices.Composite.Regions;
 27
 28namespace StockTraderRI.Modules.News.Tests.Controllers
 29{
 30    [TestClass]
 31    public class NewsControllerFixture
 32    {
 33        [TestMethod]
 34        public void ShowNewsResolvesPresenterAndCallsSetTickerSymbolOnItAndAddsNamedViewToRegion()
 35        {
 36            var presenter = new MockArticlePresentationModel();
 37            var eventAggregator = new MockEventAggregator();
 38            eventAggregator.AddMapping<TickerSymbolSelectedEvent>(new MockTickerSymbolSelectedEvent());
 39            var controller = new NewsController(CreateRegionManager(), presenter, eventAggregator, new MockNewsReaderPresenter());
 40
 41            controller.ShowNews("Test");
 42
 43            Assert.IsNotNull(presenter.SetTickerSymbolArgumentCompanySymbol);
 44            Assert.AreEqual("Test", presenter.SetTickerSymbolArgumentCompanySymbol);
 45        }
 46
 47        [TestMethod]
 48        public void ControllerShowNewsWhenRasingGlobalEvent()
 49        {
 50            var presenter = new MockArticlePresentationModel();
 51            var eventAggregator = new MockEventAggregator();
 52            var tickerSymbolSelectedEvent = new MockTickerSymbolSelectedEvent();
 53            eventAggregator.AddMapping<TickerSymbolSelectedEvent>(tickerSymbolSelectedEvent);
 54            var controller = new NewsController(CreateRegionManager(), presenter, eventAggregator, new MockNewsReaderPresenter());
 55
 56            controller.Run();
 57
 58            Assert.IsNotNull(tickerSymbolSelectedEvent.SubscribeArgumentAction);
 59
 60            tickerSymbolSelectedEvent.SubscribeArgumentAction("TEST_SYMBOL");
 61            Assert.AreEqual("TEST_SYMBOL", presenter.SetTickerSymbolArgumentCompanySymbol);
 62        }
 63
 64        [TestMethod]
 65        public void ShouldNotifyReaderWhenCurrentNewsArticleChanges()
 66        {
 67            var presenter = new MockArticlePresentationModel();
 68            var eventAggregator = new MockEventAggregator();
 69            eventAggregator.AddMapping<TickerSymbolSelectedEvent>(new MockTickerSymbolSelectedEvent());
 70            var newsReaderPresenter = new MockNewsReaderPresenter();
 71            var controller = new NewsController(CreateRegionManager(), presenter, eventAggregator, newsReaderPresenter);
 72
 73            controller.CurrentNewsArticleChanged(new NewsArticle() { Title = "SomeTitle", Body = "Newsbody" });
 74
 75            Assert.IsTrue(newsReaderPresenter.SetNewsArticleCalled);
 76        }
 77
 78        [TestMethod]
 79        public void ControllerShowNewsViewWhenArticlePresenterReceivesEvent()
 80        {
 81            var articlePresentationModel = new MockArticlePresentationModel();
 82            var eventAggregator = new MockEventAggregator();
 83            eventAggregator.AddMapping<TickerSymbolSelectedEvent>(new MockTickerSymbolSelectedEvent());
 84            var newsReaderPresenter = new MockNewsReaderPresenter();
 85            var regionManager = CreateRegionManager();
 86            var shellRegion = (MockRegion)regionManager.Regions["SecondaryRegion"];
 87
 88            var controller = new NewsController(regionManager, articlePresentationModel, eventAggregator, newsReaderPresenter);
 89
 90            controller.ShowNewsReader();
 91
 92            Assert.IsTrue(shellRegion.ActivateCalled);
 93            Assert.AreEqual(newsReaderPresenter.View, shellRegion.ActivateArg);
 94        }
 95
 96        private MockRegionManager CreateRegionManager()
 97        {
 98            var regionManager = new MockRegionManager();
 99            regionManager.Regions.Add("SecondaryRegion", new MockRegion());
100            regionManager.Regions.Add("NewsRegion", new MockRegion());
101            regionManager.Regions.Add("ResearchRegion", new MockRegion());
102
103            return regionManager;
104        }
105
106        internal class MockArticlePresentationModel : IArticlePresentationModel
107        {
108            public MockArticleView MockArticleView = new MockArticleView();
109            public string SetTickerSymbolArgumentCompanySymbol;
110            public void SetTickerSymbol(string companySymbol)
111            {
112                SetTickerSymbolArgumentCompanySymbol = companySymbol;
113            }
114
115            public IArticleView View
116            {
117                get { return MockArticleView; }
118            }
119
120            public INewsController Controller { get; set; }
121        }
122
123        internal class MockTickerSymbolSelectedEvent : TickerSymbolSelectedEvent
124        {
125            public Action<string> SubscribeArgumentAction;
126            public Predicate<string> SubscribeArgumentFilter;
127            public override SubscriptionToken Subscribe(Action<string> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<string> filter)
128            {
129                SubscribeArgumentAction = action;
130                SubscribeArgumentFilter = filter;
131                return null;
132            }
133        }
134    }
135
136    internal class MockNewsReaderPresenter : INewsReaderPresenter
137    {
138        public bool SetNewsArticleCalled { get; set; }
139
140        public INewsReaderView View { get; set; }
141
142        public MockNewsReaderPresenter()
143        {
144            View = new MockNewsReaderView();
145        }
146
147        public void SetNewsArticle(NewsArticle article)
148        {
149            SetNewsArticleCalled = true;
150        }
151    }
152}