From f7edde8daee44291632c72a60acaaf18f3921bc0 Mon Sep 17 00:00:00 2001 From: mgroves Date: Tue, 22 Mar 2011 00:18:30 -0400 Subject: [PATCH] refactored config view to MVP, added tests --- .../MonoStockPortfolio.Tests.csproj | 1 + .../Given_an_initialized_Config_Presenter.cs | 94 ++++++++++++++++ .../{ => ConfigScreen}/ConfigActivity.cs | 102 ++++++++++-------- .../Activites/ConfigScreen/ConfigPresenter.cs | 36 +++++++ .../ConfigScreen/IConfigPresenter.cs | 11 ++ .../Activites/ConfigScreen/IConfigView.cs | 11 ++ .../Activites/MainScreen/MainActivity.cs | 1 + .../{ => PortfolioScreen}/MenuOption.cs | 2 +- .../Framework/ServiceLocator.cs | 2 + MonoStockPortfolio/MonoStockPortfolio.csproj | 7 +- 10 files changed, 219 insertions(+), 48 deletions(-) create mode 100644 MonoStockPortfolio.Tests/Presenters/Given_an_initialized_Config_Presenter.cs rename MonoStockPortfolio/Activites/{ => ConfigScreen}/ConfigActivity.cs (64%) create mode 100644 MonoStockPortfolio/Activites/ConfigScreen/ConfigPresenter.cs create mode 100644 MonoStockPortfolio/Activites/ConfigScreen/IConfigPresenter.cs create mode 100644 MonoStockPortfolio/Activites/ConfigScreen/IConfigView.cs rename MonoStockPortfolio/Activites/{ => PortfolioScreen}/MenuOption.cs (76%) diff --git a/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj b/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj index 17bf125..8772910 100644 --- a/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj +++ b/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj @@ -63,6 +63,7 @@ + diff --git a/MonoStockPortfolio.Tests/Presenters/Given_an_initialized_Config_Presenter.cs b/MonoStockPortfolio.Tests/Presenters/Given_an_initialized_Config_Presenter.cs new file mode 100644 index 0000000..ce7ac41 --- /dev/null +++ b/MonoStockPortfolio.Tests/Presenters/Given_an_initialized_Config_Presenter.cs @@ -0,0 +1,94 @@ +using System.Collections.Generic; +using System.Linq; +using Machine.Specifications; +using MonoStockPortfolio.Activites.ConfigScreen; +using MonoStockPortfolio.Core.Config; +using MonoStockPortfolio.Entities; +using Telerik.JustMock; +using MonoStockPortfolio.Core; + +namespace MonoStockPortfolio.Tests.Presenters +{ + public class Given_a_Config_Presenter + { + protected static ConfigPresenter _presenter; + protected static IConfigRepository _configRepository; + protected static IConfigView _configView; + + Establish context = () => + { + _configView = Mock.Create(); + + _configRepository = Mock.Create(); + Mock.Arrange(() => _configRepository.GetStockItems()).Returns(new List + { + StockDataItem.GainLoss, + StockDataItem.Change + }); + + _presenter = new ConfigPresenter(_configRepository); + }; + } + + public class When_initialize_the_config_presenter : Given_a_Config_Presenter + { + static List _allStockDataItems; + + Establish context = () => + { + _allStockDataItems = StockDataItem.Volume.GetValues().ToList(); + }; + + Because of = () => + { + _presenter.Initialize(_configView); + }; + + It should_send_two_checked_items = () => + Mock.Assert(() => + _configView.PrepopulateConfiguration( + Arg.IsAny>(), + Arg.Matches>(i => i.Count() == 2)), + Occurs.Exactly(1)); + + It should_send_GainLoss_as_a_checked_item = () => + Mock.Assert(() => + _configView.PrepopulateConfiguration( + Arg.IsAny>(), + Arg.Matches>(i => i.Any(p => p == StockDataItem.GainLoss))), + Occurs.Exactly(1)); + It should_send_Change_as_a_checked_item = () => + Mock.Assert(() => + _configView.PrepopulateConfiguration( + Arg.IsAny>(), + Arg.Matches>(i => i.Any(p => p == StockDataItem.Change))), + Occurs.Exactly(1)); + + It should_send_an_enumerated_list_of_all_stock_items = () => + Mock.Assert(() => + _configView.PrepopulateConfiguration( + Arg.Matches>(i => i.Count == _allStockDataItems.Count), + Arg.IsAny>()), + Occurs.Exactly(1)); + } + + public class When_the_user_wants_to_save_configuration : Given_a_Config_Presenter + { + Establish context = () => + { + _presenter.Initialize(_configView); + }; + + Because of = () => + { + _presenter.SaveConfig(new List {StockDataItem.Ticker, StockDataItem.Time}); + }; + + It should_use_the_repo_to_update_to_2_stock_items = () => + Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches>(i => i.Count == 2))); + It should_use_the_repo_to_update_stock_items_with_Time = () => + Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches>(i => i.Any(s => s == StockDataItem.Time)))); + It should_use_the_repo_to_update_stock_items_with_Ticker = () => + Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches>(i => i.Any(s => s == StockDataItem.Ticker)))); + } +} \ No newline at end of file diff --git a/MonoStockPortfolio/Activites/ConfigActivity.cs b/MonoStockPortfolio/Activites/ConfigScreen/ConfigActivity.cs similarity index 64% rename from MonoStockPortfolio/Activites/ConfigActivity.cs rename to MonoStockPortfolio/Activites/ConfigScreen/ConfigActivity.cs index 1c961a2..808e623 100644 --- a/MonoStockPortfolio/Activites/ConfigActivity.cs +++ b/MonoStockPortfolio/Activites/ConfigScreen/ConfigActivity.cs @@ -4,61 +4,19 @@ using Android.App; using Android.Content; using Android.OS; using Android.Widget; -using MonoStockPortfolio.Core.Config; using MonoStockPortfolio.Entities; using MonoStockPortfolio.Framework; using MonoStockPortfolio.Core; -namespace MonoStockPortfolio.Activites +namespace MonoStockPortfolio.Activites.ConfigScreen { [Activity(Label = "Config", Name = "monostockportfolio.activites.ConfigActivity")] - public class ConfigActivity : Activity + public class ConfigActivity : Activity, IConfigView { - [IoC] private IConfigRepository _repo; - [LazyView(Resource.Id.configList)] private ListView ConfigList; [LazyView(Resource.Id.btnSaveConfig)] private Button SaveConfigButton; - protected override void OnCreate(Bundle bundle) - { - base.OnCreate(bundle); - - SetContentView(Resource.Layout.config); - - var allitems = StockDataItem.Volume.GetValues().ToList(); - var allitemsLabels = allitems.Select(i => i.GetStringValue()).ToList(); - var checkeditems = _repo.GetStockItems(); - - var configAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice, allitemsLabels); - ConfigList.Adapter = configAdapter; - ConfigList.ChoiceMode = ChoiceMode.Multiple; - - for(int i=0;i().ToList(); - var newConfig = new List(); - for (int i = 0; i < ConfigList.Count; i++) - { - if(ConfigList.IsItemChecked(i)) - { - newConfig.Add(allitems[i]); - } - } - _repo.UpdateStockItems(newConfig); - - this.LongToast("Configuration updated!"); - } + [IoC] IConfigPresenter _presenter; public static Intent GotoIntent(Context context) { @@ -66,5 +24,59 @@ namespace MonoStockPortfolio.Activites intent.SetClassName(context, ManifestNames.GetName()); return intent; } + + protected override void OnCreate(Bundle bundle) + { + base.OnCreate(bundle); + + SetContentView(Resource.Layout.config); + + _presenter.Initialize(this); + + WireUpEvents(); + } + + void WireUpEvents() + { + SaveConfigButton.Click += SaveConfigButton_Click; + } + + #region IConfigView members + + public void PrepopulateConfiguration(IList allitems, IEnumerable checkeditems) + { + var allitemsLabels = allitems.Select(i => i.GetStringValue()).ToList(); + var configAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice, allitemsLabels); + ConfigList.Adapter = configAdapter; + ConfigList.ChoiceMode = ChoiceMode.Multiple; + + for (int i = 0; i < ConfigList.Count; i++) + { + if (checkeditems.Contains(allitems[i])) + { + ConfigList.SetItemChecked(i, true); + } + } + } + + public void ShowToastMessage(string message) + { + this.LongToast(message); + } + + #endregion + + void SaveConfigButton_Click(object sender, System.EventArgs e) + { + var checkedItems = new List(); + for(int i =0;i().ToList(); + var checkeditems = _configRepository.GetStockItems(); + + _currentView.PrepopulateConfiguration(allitems, checkeditems); + } + + public void SaveConfig(List checkedItems) + { + _configRepository.UpdateStockItems(checkedItems); + + _currentView.ShowToastMessage("Configuration updated!"); + } + } +} \ No newline at end of file diff --git a/MonoStockPortfolio/Activites/ConfigScreen/IConfigPresenter.cs b/MonoStockPortfolio/Activites/ConfigScreen/IConfigPresenter.cs new file mode 100644 index 0000000..46ae9aa --- /dev/null +++ b/MonoStockPortfolio/Activites/ConfigScreen/IConfigPresenter.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using MonoStockPortfolio.Entities; + +namespace MonoStockPortfolio.Activites.ConfigScreen +{ + public interface IConfigPresenter + { + void Initialize(IConfigView configView); + void SaveConfig(List checkedItems); + } +} \ No newline at end of file diff --git a/MonoStockPortfolio/Activites/ConfigScreen/IConfigView.cs b/MonoStockPortfolio/Activites/ConfigScreen/IConfigView.cs new file mode 100644 index 0000000..5021a6d --- /dev/null +++ b/MonoStockPortfolio/Activites/ConfigScreen/IConfigView.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using MonoStockPortfolio.Entities; + +namespace MonoStockPortfolio.Activites.ConfigScreen +{ + public interface IConfigView + { + void PrepopulateConfiguration(IList allitems, IEnumerable checkeditems); + void ShowToastMessage(string message); + } +} \ No newline at end of file diff --git a/MonoStockPortfolio/Activites/MainScreen/MainActivity.cs b/MonoStockPortfolio/Activites/MainScreen/MainActivity.cs index b808aeb..57d0622 100644 --- a/MonoStockPortfolio/Activites/MainScreen/MainActivity.cs +++ b/MonoStockPortfolio/Activites/MainScreen/MainActivity.cs @@ -5,6 +5,7 @@ using Android.Content; using Android.OS; using Android.Views; using Android.Widget; +using MonoStockPortfolio.Activites.ConfigScreen; using MonoStockPortfolio.Activites.EditPortfolioScreen; using MonoStockPortfolio.Activites.PortfolioScreen; using MonoStockPortfolio.Framework; diff --git a/MonoStockPortfolio/Activites/MenuOption.cs b/MonoStockPortfolio/Activites/PortfolioScreen/MenuOption.cs similarity index 76% rename from MonoStockPortfolio/Activites/MenuOption.cs rename to MonoStockPortfolio/Activites/PortfolioScreen/MenuOption.cs index 0644c6a..b46e874 100644 --- a/MonoStockPortfolio/Activites/MenuOption.cs +++ b/MonoStockPortfolio/Activites/PortfolioScreen/MenuOption.cs @@ -1,4 +1,4 @@ -namespace MonoStockPortfolio.Activites +namespace MonoStockPortfolio.Activites.PortfolioScreen { public class MenuOption { diff --git a/MonoStockPortfolio/Framework/ServiceLocator.cs b/MonoStockPortfolio/Framework/ServiceLocator.cs index 5105c8e..91c9543 100644 --- a/MonoStockPortfolio/Framework/ServiceLocator.cs +++ b/MonoStockPortfolio/Framework/ServiceLocator.cs @@ -1,6 +1,7 @@ using System; using Android.Content; using Android.Util; +using MonoStockPortfolio.Activites.ConfigScreen; using MonoStockPortfolio.Activites.EditPortfolioScreen; using MonoStockPortfolio.Activites.EditPositionScreen; using MonoStockPortfolio.Activites.MainScreen; @@ -30,6 +31,7 @@ namespace MonoStockPortfolio.Framework IttyBittyIoC.Register(); IttyBittyIoC.Register(); IttyBittyIoC.Register(); + IttyBittyIoC.Register(); } public static object Get(Type serviceType) diff --git a/MonoStockPortfolio/MonoStockPortfolio.csproj b/MonoStockPortfolio/MonoStockPortfolio.csproj index bb5496a..d388253 100644 --- a/MonoStockPortfolio/MonoStockPortfolio.csproj +++ b/MonoStockPortfolio/MonoStockPortfolio.csproj @@ -54,7 +54,10 @@ - + + + + @@ -67,7 +70,7 @@ - +