PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/StockTraderRI/StockTraderRI.Modules.WatchList.Tests/WatchList/WatchListPresentationModelFixture.cs

#
C# | 191 lines | 131 code | 44 blank | 16 comment | 0 complexity | dc2f9e64cb602fb52c7fd263f896a160 MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation
  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//===============================================================================
 17
 18using System;
 19using System.Collections.Generic;
 20using System.Linq;
 21using Microsoft.Practices.Composite.Events;
 22using Microsoft.Practices.Composite.Wpf.Events;
 23using Microsoft.VisualStudio.TestTools.UnitTesting;
 24using StockTraderRI.Infrastructure;
 25using StockTraderRI.Modules.Watch;
 26using StockTraderRI.Modules.Watch.WatchList;
 27using StockTraderRI.Modules.WatchList.Tests.Mocks;
 28
 29namespace StockTraderRI.Modules.WatchList.Tests.WatchList
 30{
 31    [TestClass]
 32    public class WatchListPresentationModelFixture
 33    {
 34        MockWatchListView view;
 35        MockWatchListService watchListService;
 36        MockMarketFeedService marketFeedService;
 37        MockEventAggregator eventAggregator;
 38
 39        [TestInitialize]
 40        public void SetUp()
 41        {
 42            view = new MockWatchListView();
 43            watchListService = new MockWatchListService();
 44            marketFeedService = new MockMarketFeedService();
 45            eventAggregator = new MockEventAggregator();
 46        }
 47
 48        [TestMethod]
 49        public void CanInitPresenter()
 50        {
 51            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(new MockMarketPricesUpdatedEvent());
 52            WatchListPresentationModel presentationModel = CreatePresenter();
 53
 54            Assert.AreEqual<IWatchListView>(view, presentationModel.View);
 55        }
 56
 57        [TestMethod]
 58        public void ClickingOnTheRemoveMenuItemCallsTheRemoveSelectedItemMethod()
 59        {
 60            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(new MockMarketPricesUpdatedEvent());
 61
 62            watchListService.MockWatchList.Add("TEST");
 63            WatchListPresentationModel presentationModel = CreatePresenter();
 64
 65            Assert.AreEqual(1, watchListService.MockWatchList.Count);
 66
 67            presentationModel.RemoveWatchCommand.Execute("TEST");
 68
 69            Assert.AreEqual(0, watchListService.MockWatchList.Count);
 70        }
 71
 72        [TestMethod]
 73        public void CanGetItemsFromWatchListServiceAndPutInView()
 74        {
 75            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(new MockMarketPricesUpdatedEvent());
 76
 77            watchListService.MockWatchList.Add("TESTFUND0");
 78            watchListService.MockWatchList.Add("TESTFUND1");
 79
 80            WatchListPresentationModel presentationModel = CreatePresenter();
 81
 82            Assert.IsNotNull(view.Model);
 83            Assert.IsNotNull(view.Model.WatchListItems);
 84            Assert.AreEqual(2, view.Model.WatchListItems.Count);
 85            Assert.AreEqual<string>("TESTFUND0", view.Model.WatchListItems[0].TickerSymbol);
 86            Assert.AreEqual<string>("TESTFUND1", view.Model.WatchListItems[1].TickerSymbol);
 87        }
 88
 89        [TestMethod]
 90        public void PresenterObservesWatchListAndUpdatesView()
 91        {
 92            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(new MockMarketPricesUpdatedEvent());
 93
 94            watchListService.MockWatchList.Add("TESTFUND0");
 95
 96            WatchListPresentationModel presentationModel = CreatePresenter();
 97
 98            Assert.AreEqual(1, view.Model.WatchListItems.Count());
 99
100            watchListService.MockWatchList.Add("TESTFUND1");
101
102            Assert.AreEqual(2, view.Model.WatchListItems.Count());
103            Assert.AreEqual<string>("TESTFUND1", view.Model.WatchListItems[1].TickerSymbol);
104        }
105
106
107        [TestMethod]
108        public void ViewGetsCurrentPriceForSymbolRetrievedFromMarketFeedService()
109        {
110            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(new MockMarketPricesUpdatedEvent());
111
112            watchListService.MockWatchList.Add("TESTFUND0");
113            marketFeedService.SetPrice("TESTFUND0", 15.5m);
114
115            WatchListPresentationModel presentationModel = CreatePresenter();
116
117            WatchItem watchItem = view.Model.WatchListItems[0];
118            Assert.AreEqual<string>("TESTFUND0", watchItem.TickerSymbol);
119            Assert.IsNotNull(watchItem.CurrentPrice);
120            Assert.AreEqual<decimal>(15.5m, watchItem.CurrentPrice.Value);
121        }
122
123        [TestMethod]
124        public void IfPriceIsNotAvailableForSymbolSetsNullCurrentPriceInView()
125        {
126            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(new MockMarketPricesUpdatedEvent());
127
128            watchListService.MockWatchList.Add("NONEXISTING");
129            marketFeedService.MockSymbolExists = false;
130
131            WatchListPresentationModel presentationModel = CreatePresenter();
132
133            WatchItem watchItem = view.Model.WatchListItems[0];
134            Assert.AreEqual<string>("NONEXISTING", watchItem.TickerSymbol);
135            Assert.IsNull(watchItem.CurrentPrice);
136        }
137
138        [TestMethod]
139        public void PresenterObservesMarketFeedAndUpdatesView()
140        {
141            var marketPricesUpdatedEvent = new MockMarketPricesUpdatedEvent();
142            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(marketPricesUpdatedEvent);
143
144            marketFeedService.feedData.Add("TESTFUND0", 15.5m);
145            watchListService.MockWatchList.Add("TESTFUND0");
146
147            WatchListPresentationModel presentationModel = CreatePresenter();
148
149            Assert.AreEqual<decimal>(15.5m, view.Model.WatchListItems[0].CurrentPrice.Value);
150
151            Assert.IsNotNull(marketPricesUpdatedEvent.SubscribeArgumentAction);
152            Assert.AreEqual(ThreadOption.UIThread, marketPricesUpdatedEvent.SubscribeArgumentThreadOption);
153
154            marketPricesUpdatedEvent.SubscribeArgumentAction(new Dictionary<string, decimal> { { "TESTFUND0", 25.3m } });
155
156            Assert.AreEqual<decimal>(25.3m, view.Model.WatchListItems[0].CurrentPrice.Value);
157        }
158
159        [TestMethod]
160        public void PresentationModelShouldHaveHeaderInfoSet()
161        {
162            eventAggregator.AddMapping<MarketPricesUpdatedEvent>(new MockMarketPricesUpdatedEvent());
163
164            WatchListPresentationModel presentationModel = CreatePresenter();
165            Assert.AreEqual("Watch List", view.Model.HeaderInfo);
166        }
167
168
169        private WatchListPresentationModel CreatePresenter()
170        {
171            return new WatchListPresentationModel(view, watchListService, marketFeedService, eventAggregator);
172        }
173
174
175    }
176
177    internal class MockMarketPricesUpdatedEvent : MarketPricesUpdatedEvent
178    {
179        public Action<IDictionary<string, decimal>> SubscribeArgumentAction;
180        public Predicate<IDictionary<string, decimal>> SubscribeArgumentFilter;
181        public ThreadOption SubscribeArgumentThreadOption;
182
183        public override SubscriptionToken Subscribe(Action<IDictionary<string, decimal>> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate<IDictionary<string, decimal>> filter)
184        {
185            SubscribeArgumentAction = action;
186            SubscribeArgumentFilter = filter;
187            SubscribeArgumentThreadOption = threadOption;
188            return null;
189        }
190    }
191}