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

/src/AddIns/Misc/PackageManagement/Test/Src/RecentPackagesViewModelTests.cs

https://github.com/ajadex/SharpDevelop
C# | 156 lines | 113 code | 26 blank | 17 comment | 0 complexity | cd9ce6f648626fe02bfcde6ad35ca1b2 MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using ICSharpCode.PackageManagement;
 21using ICSharpCode.PackageManagement.Design;
 22using NUnit.Framework;
 23using PackageManagement.Tests.Helpers;
 24
 25namespace PackageManagement.Tests
 26{
 27	[TestFixture]
 28	public class RecentPackagesViewModelTests
 29	{
 30		RecentPackagesViewModel viewModel;
 31		PackageManagementEvents packageManagementEvents;
 32		FakeRegisteredPackageRepositories registeredPackageRepositories;
 33		FakeTaskFactory taskFactory;
 34		
 35		void CreateViewModel()
 36		{
 37			CreateViewModel(new FakeRegisteredPackageRepositories());
 38		}
 39		
 40		void CreateViewModel(FakeRegisteredPackageRepositories registeredPackageRepositories)
 41		{
 42			this.registeredPackageRepositories = registeredPackageRepositories;
 43			taskFactory = new FakeTaskFactory();
 44			var packageViewModelFactory = new FakePackageViewModelFactory();
 45			packageManagementEvents = new PackageManagementEvents();
 46			viewModel = new RecentPackagesViewModel(
 47				new FakePackageManagementSolution(),
 48				packageManagementEvents,
 49				registeredPackageRepositories,
 50				packageViewModelFactory,
 51				taskFactory);
 52		}
 53		
 54		void CompleteReadPackagesTask()
 55		{
 56			taskFactory.ExecuteAllFakeTasks();
 57		}
 58		
 59		void ClearReadPackagesTasks()
 60		{
 61			taskFactory.ClearAllFakeTasks();
 62		}
 63		
 64		FakePackage AddPackageToRecentPackageRepository()
 65		{
 66			var package = new FakePackage("Test");
 67			FakePackageRepository repository = registeredPackageRepositories.FakeRecentPackageRepository;
 68			repository.FakePackages.Add(package);
 69			return package;
 70		}
 71
 72		[Test]
 73		public void PackageViewModels_PackageIsInstalledAfterRecentPackagesDisplayed_PackagesOnDisplayAreUpdated()
 74		{
 75			CreateViewModel();
 76			viewModel.ReadPackages();
 77			CompleteReadPackagesTask();
 78			var package = AddPackageToRecentPackageRepository();
 79			
 80			ClearReadPackagesTasks();
 81			packageManagementEvents.OnParentPackageInstalled(new FakePackage());
 82			CompleteReadPackagesTask();
 83			
 84			var expectedPackages = new FakePackage[] {
 85				package
 86			};
 87			
 88			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
 89		}
 90		
 91		[Test]
 92		public void PackageViewModels_PackageIsUninstalledAfterRecentPackagesDisplayed_PackagesOnDisplayAreNotUpdated()
 93		{
 94			CreateViewModel();
 95			viewModel.ReadPackages();
 96			CompleteReadPackagesTask();
 97			var package = AddPackageToRecentPackageRepository();
 98
 99			ClearReadPackagesTasks();
100			packageManagementEvents.OnParentPackageUninstalled(new FakePackage());
101			CompleteReadPackagesTask();
102
103			var expectedPackages = new FakePackage[0];
104
105			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
106		}
107		
108		[Test]
109		public void PackageViewModels_PackageIsUninstalledAfterViewModelIsDisposed_PackagesOnDisplayAreNotUpdated()
110		{
111			CreateViewModel();
112			viewModel.ReadPackages();
113			CompleteReadPackagesTask();
114			AddPackageToRecentPackageRepository();
115			
116			ClearReadPackagesTasks();
117			viewModel.Dispose();
118			
119			packageManagementEvents.OnParentPackageUninstalled(new FakePackage());
120			CompleteReadPackagesTask();
121			
122			Assert.AreEqual(0, viewModel.PackageViewModels.Count);
123		}
124		
125		[Test]
126		public void PackageViewModels_PackageIsInstalledAfterViewModelIsDisposed_PackagesOnDisplayAreNotUpdated()
127		{
128			CreateViewModel();
129			viewModel.ReadPackages();
130			CompleteReadPackagesTask();
131			AddPackageToRecentPackageRepository();
132			
133			ClearReadPackagesTasks();
134			
135			viewModel.Dispose();
136			packageManagementEvents.OnParentPackageInstalled(new FakePackage());
137			CompleteReadPackagesTask();
138			
139			Assert.AreEqual(0, viewModel.PackageViewModels.Count);
140		}
141		
142		[Test]
143		public void ReadPackages_ExceptionThrownWhenAccessingActiveRepository_ErrorMessageFromExceptionNotOverriddenByReadPackagesCall()
144		{
145			var registeredRepositories = new ExceptionThrowingRegisteredPackageRepositories();
146			registeredRepositories.ExceptionToThrowWhenRecentPackageRepositoryAccessed = 
147				new Exception("Test");
148			CreateViewModel(registeredRepositories);
149			
150			viewModel.ReadPackages();
151			
152			ApplicationException ex = Assert.Throws<ApplicationException>(CompleteReadPackagesTask);
153			Assert.AreEqual("Test", ex.Message);
154		}
155	}
156}