PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/V2/trunk/RI/Desktop/StockTraderRI.Modules.Position.Tests/PositionSummary/PositionSummaryPresentationModelFixture.cs

#
C# | 145 lines | 100 code | 25 blank | 20 comment | 0 complexity | b4271a70a714d0ebbf9d8003e4b38df0 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 System.Collections.Generic;
 19using System.ComponentModel;
 20using System.Linq;
 21using Microsoft.Practices.Composite.Events;
 22using Microsoft.VisualStudio.TestTools.UnitTesting;
 23using StockTraderRI.Infrastructure;
 24using StockTraderRI.Infrastructure.Models;
 25using StockTraderRI.Modules.Position.PositionSummary;
 26using StockTraderRI.Modules.Position.Tests.Mocks;
 27using Microsoft.Practices.Composite.Presentation.Events;
 28using System.Collections.ObjectModel;
 29
 30namespace StockTraderRI.Modules.Position.Tests.PositionSummary
 31{
 32    [TestClass]
 33    public class PositionSummaryPresentationModelFixture
 34    {
 35        MockPositionSummaryView view;
 36        MockOrdersController ordersController;
 37        MockEventAggregator eventAggregator;
 38        MockObservablePosition observablePosition;
 39
 40        [TestInitialize]
 41        public void SetUp()
 42        {
 43            view = new MockPositionSummaryView();
 44            ordersController = new MockOrdersController();
 45            observablePosition = new MockObservablePosition();
 46            this.eventAggregator = new MockEventAggregator();
 47            eventAggregator.AddMapping<TickerSymbolSelectedEvent>(new MockTickerSymbolSelectedEvent());
 48        }
 49
 50        [TestMethod]
 51        public void CanInitPresentationModel()
 52        {
 53            PositionSummaryPresentationModel presentationModel = CreatePresentationModel();
 54
 55            Assert.AreEqual(view, presentationModel.View);
 56
 57        }
 58
 59        [TestMethod]
 60        public void CanSetPresentationModelIntoView()
 61        {
 62            var presentationModel = CreatePresentationModel();
 63
 64            Assert.AreSame(presentationModel, view.Model);
 65        }
 66
 67        [TestMethod]
 68        public void ShouldTriggerPropertyChangedEventOnCurrentPositionSummaryItemChange()
 69        {
 70            PositionSummaryPresentationModel presentationModel = CreatePresentationModel();
 71            string changedPropertyName = string.Empty;
 72
 73            presentationModel.PropertyChanged += (o, e) =>
 74                                                     {
 75                                                         changedPropertyName = e.PropertyName;
 76                                                     };
 77
 78            presentationModel.CurrentPositionSummaryItem = new PositionSummaryItem("NewTickerSymbol", 0, 0, 0);
 79
 80            Assert.AreEqual("CurrentPositionSummaryItem", changedPropertyName);
 81        }
 82
 83        [TestMethod]
 84        public void TickerSymbolSelectedPublishesEvent()
 85        {
 86            var tickerSymbolSelectedEvent = new MockTickerSymbolSelectedEvent();
 87            eventAggregator.AddMapping<TickerSymbolSelectedEvent>(tickerSymbolSelectedEvent);
 88            var presentationModel = CreatePresentationModel();
 89
 90            presentationModel.CurrentPositionSummaryItem = new PositionSummaryItem("FUND0", 0, 0, 0);
 91
 92            Assert.IsTrue(tickerSymbolSelectedEvent.PublishCalled);
 93            Assert.AreEqual("FUND0", tickerSymbolSelectedEvent.PublishArgumentPayload);
 94        }
 95
 96        [TestMethod]
 97        public void ControllerCommandsSetIntoPresentationModel()
 98        {
 99            var presentationModel = CreatePresentationModel();
100
101            Assert.AreSame(presentationModel.BuyCommand, ordersController.BuyCommand);
102            Assert.AreSame(presentationModel.SellCommand, ordersController.SellCommand);
103        }
104
105        private PositionSummaryPresentationModel CreatePresentationModel()
106        {
107            return new PositionSummaryPresentationModel(view 
108                                                , ordersController
109                                                , eventAggregator
110                                                , observablePosition);
111
112        }
113
114        [TestMethod]
115        public void CurrentItemNullDoesNotThrow()
116        {
117            var model = CreatePresentationModel();
118
119            model.CurrentPositionSummaryItem = null;
120        }
121    }
122
123    internal class MockTickerSymbolSelectedEvent : TickerSymbolSelectedEvent
124    {
125        public bool PublishCalled;
126        public string PublishArgumentPayload;
127
128
129        public override void Publish(string payload)
130        {
131            PublishCalled = true;
132            PublishArgumentPayload = payload;
133        }
134    }
135
136    internal class MockObservablePosition : IObservablePosition
137    {
138        public ObservableCollection<PositionSummaryItem> Items { get; set; }
139    }
140}
141
142/*
143 * 
144 * market update with volume change should be reflected in presentation model
145 */