PageRenderTime 56ms CodeModel.GetById 2ms app.highlight 47ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://github.com/ajadex/SharpDevelop
C# | 653 lines | 514 code | 122 blank | 17 comment | 0 complexity | b2ceef047446b6e1b07971035e7bd5ec 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 System.Collections.Generic;
 21using System.Linq;
 22using ICSharpCode.PackageManagement;
 23using ICSharpCode.PackageManagement.Design;
 24using NuGet;
 25using NUnit.Framework;
 26using PackageManagement.Tests.Helpers;
 27
 28namespace PackageManagement.Tests
 29{
 30	[TestFixture]
 31	public class AvailablePackagesViewModelTests
 32	{
 33		AvailablePackagesViewModel viewModel;
 34		PackageManagementEvents packageManagementEvents;
 35		FakeRegisteredPackageRepositories registeredPackageRepositories;
 36		ExceptionThrowingRegisteredPackageRepositories exceptionThrowingRegisteredPackageRepositories;
 37		FakeTaskFactory taskFactory;
 38		
 39		void CreateViewModel()
 40		{
 41			CreateRegisteredPackageRepositories();
 42			CreateViewModel(registeredPackageRepositories);
 43		}
 44		
 45		void CreateRegisteredPackageRepositories()
 46		{
 47			registeredPackageRepositories = new FakeRegisteredPackageRepositories();
 48		}
 49		
 50		void CreateViewModel(FakeRegisteredPackageRepositories registeredPackageRepositories)
 51		{
 52			taskFactory = new FakeTaskFactory();
 53			var packageViewModelFactory = new FakePackageViewModelFactory();
 54			packageManagementEvents = new PackageManagementEvents();
 55
 56			viewModel = new AvailablePackagesViewModel(
 57				new FakePackageManagementSolution(),
 58				packageManagementEvents,
 59				registeredPackageRepositories, 
 60				packageViewModelFactory, 
 61				taskFactory);
 62		}
 63		
 64		void CreateExceptionThrowingRegisteredPackageRepositories()
 65		{
 66			exceptionThrowingRegisteredPackageRepositories = new ExceptionThrowingRegisteredPackageRepositories();
 67		}
 68		
 69		void CompleteReadPackagesTask()
 70		{
 71			taskFactory.ExecuteAllFakeTasks();
 72		}
 73		
 74		void ClearReadPackagesTasks()
 75		{
 76			taskFactory.ClearAllFakeTasks();
 77		}
 78
 79		void AddOnePackageSourceToRegisteredSources()
 80		{
 81			registeredPackageRepositories.ClearPackageSources();
 82			registeredPackageRepositories.AddOnePackageSource();
 83			registeredPackageRepositories.HasMultiplePackageSources = false;
 84		}
 85		
 86		void AddTwoPackageSourcesToRegisteredSources()
 87		{
 88			var expectedPackageSources = new PackageSource[] {
 89				new PackageSource("http://first.com", "First"),
 90				new PackageSource("http://second.com", "Second")
 91			};
 92			AddPackageSourcesToRegisteredSources(expectedPackageSources);
 93			registeredPackageRepositories.HasMultiplePackageSources = true;
 94		}
 95				
 96		void AddPackageSourcesToRegisteredSources(PackageSource[] sources)
 97		{
 98			registeredPackageRepositories.ClearPackageSources();
 99			registeredPackageRepositories.AddPackageSources(sources);
100		}
101		
102		PackageSource AddTwoPackageSourcesToRegisteredSourcesWithFirstOneDisabled()
103		{
104			var expectedPackageSources = new PackageSource[] {
105				new PackageSource("http://first.com", "First") { IsEnabled = false },
106				new PackageSource("http://second.com", "Second") { IsEnabled = true }
107			};
108			AddPackageSourcesToRegisteredSources(expectedPackageSources);
109			registeredPackageRepositories.HasMultiplePackageSources = true;
110			return expectedPackageSources[0];
111		}
112		
113		void CreateNewActiveRepositoryWithDifferentPackages()
114		{
115			var package = new FakePackage("NewRepositoryPackageId");
116			var newRepository = new FakePackageRepository();
117			newRepository.FakePackages.Add(package);
118			registeredPackageRepositories.FakeActiveRepository = newRepository;
119		}
120		
121		void SetUpTwoPackageSourcesAndViewModelHasReadPackages()
122		{
123			CreateRegisteredPackageRepositories();
124			AddTwoPackageSourcesToRegisteredSources();
125			CreateViewModel(registeredPackageRepositories);
126			registeredPackageRepositories.ActivePackageSource = registeredPackageRepositories.PackageSources[0];
127			viewModel.ReadPackages();
128			CompleteReadPackagesTask();
129			CreateNewActiveRepositoryWithDifferentPackages();
130		}
131		
132		void ChangeSelectedPackageSourceToSecondSource()
133		{
134			var secondPackageSource = registeredPackageRepositories.PackageSources[1];
135			viewModel.SelectedPackageSource = secondPackageSource;
136		}
137		
138		void ChangeSelectedPackageSourceToFirstSource()
139		{
140			var firstPackageSource = registeredPackageRepositories.PackageSources[0];
141			viewModel.SelectedPackageSource = firstPackageSource;
142		}
143		
144		[Test]
145		public void ReadPackages_RepositoryHasThreePackagesWithSameIdButDifferentVersions_HasLatestPackageVersionOnly()
146		{
147			CreateViewModel();
148			
149			var package1 = new FakePackage("Test", "0.1.0.0");
150			var package2 = new FakePackage("Test", "0.2.0.0");
151			var package3 = new FakePackage("Test", "0.3.0.0");
152			
153			var packages = new FakePackage[] {
154				package1, package2, package3
155			};
156			
157			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
158			
159			viewModel.ReadPackages();
160			CompleteReadPackagesTask();
161			
162			var expectedPackages = new FakePackage[] {
163				package3
164			};
165			
166			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
167		}
168		
169		[Test]
170		public void IsSearchable_ByDefault_ReturnsTrue()
171		{
172			CreateViewModel();
173			Assert.IsTrue(viewModel.IsSearchable);
174		}
175	
176		[Test]
177		public void Search_RepositoryHasThreePackagesWithSameIdButSearchTermsMatchNoPackageIds_ReturnsNoPackages()
178		{
179			CreateViewModel();
180			
181			var package1 = new FakePackage("Test", "0.1.0.0");
182			var package2 = new FakePackage("Test", "0.2.0.0");
183			var package3 = new FakePackage("Test", "0.3.0.0");
184			
185			var packages = new FakePackage[] {
186				package1, package2, package3
187			};
188			
189			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
190			
191			viewModel.ReadPackages();
192			CompleteReadPackagesTask();
193			
194			ClearReadPackagesTasks();
195			viewModel.SearchTerms = "NotAMatch";
196			viewModel.Search();
197			CompleteReadPackagesTask();
198			
199			Assert.AreEqual(0, viewModel.PackageViewModels.Count);
200		}
201		
202		[Test]
203		public void ShowNextPage_TwoObjectsWatchingForPagesCollectionChangedEventAndUserMovesToPageTwoAndFilteredPackagesReturnsLessThanExpectedPackagesDueToMatchingVersions_InvalidOperationExceptionNotThrownWhen()
204		{
205			CreateViewModel();
206			viewModel.PageSize = 2;
207			
208			var package1 = new FakePackage("First", "0.1.0.0");
209			var package2 = new FakePackage("Second", "0.2.0.0");
210			var package3 = new FakePackage("Test", "0.3.0.0");
211			var package4 = new FakePackage("Test", "0.4.0.0");
212			
213			var packages = new FakePackage[] {
214				package1, package2, package3, package4
215			};
216			
217			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
218			
219			viewModel.ReadPackages();
220			CompleteReadPackagesTask();
221			
222			ClearReadPackagesTasks();
223			bool collectionChangedEventFired = false;
224			viewModel.Pages.CollectionChanged += (sender, e) => collectionChangedEventFired = true;
225			viewModel.ShowNextPage();
226			CompleteReadPackagesTask();
227			
228			var expectedPackages = new FakePackage[] {
229				package4
230			};
231			
232			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
233			Assert.IsTrue(collectionChangedEventFired);
234		}
235		
236		[Test]
237		public void ShowSources_TwoPackageSources_ReturnsTrue()
238		{
239			CreateRegisteredPackageRepositories();
240			AddTwoPackageSourcesToRegisteredSources();
241			CreateViewModel(registeredPackageRepositories);
242			
243			Assert.IsTrue(viewModel.ShowPackageSources);
244		}
245		
246		[Test]
247		public void ShowPackageSources_OnePackageSources_ReturnsTrue()
248		{
249			CreateRegisteredPackageRepositories();
250			AddOnePackageSourceToRegisteredSources();
251			CreateViewModel(registeredPackageRepositories);
252			
253			Assert.IsTrue(viewModel.ShowPackageSources);
254		}
255		
256		[Test]
257		public void PackageSources_TwoPackageSourcesInOptions_ReturnsTwoPackageSourcesPlusAggregatePackageSource()
258		{
259			CreateRegisteredPackageRepositories();
260			AddTwoPackageSourcesToRegisteredSources();
261			CreateViewModel(registeredPackageRepositories);
262			
263			var expectedPackageSources = new List<PackageSource>(registeredPackageRepositories.PackageSources);
264			expectedPackageSources.Add(RegisteredPackageSourceSettings.AggregatePackageSource);
265			
266			PackageSourceCollectionAssert.AreEqual(expectedPackageSources, viewModel.PackageSources);
267		}
268		
269		[Test]
270		public void PackageSources_OnePackageSourceInOptions_ReturnsOnePackageSource()
271		{
272			CreateRegisteredPackageRepositories();
273			AddOnePackageSourceToRegisteredSources();
274			CreateViewModel(registeredPackageRepositories);
275			
276			var expectedPackageSources = new List<PackageSource>(registeredPackageRepositories.PackageSources);
277			
278			PackageSourceCollectionAssert.AreEqual(expectedPackageSources, viewModel.PackageSources);
279		}
280		
281		[Test]
282		public void SelectedPackageSource_TwoPackageSourcesInOptionsAndActivePackageSourceIsFirstSource_IsFirstPackageSource()
283		{
284			CreateRegisteredPackageRepositories();
285			AddTwoPackageSourcesToRegisteredSources();
286			CreateViewModel(registeredPackageRepositories);
287			
288			var expectedPackageSource = registeredPackageRepositories.PackageSources[0];
289			registeredPackageRepositories.ActivePackageSource = expectedPackageSource;
290			
291			Assert.AreEqual(expectedPackageSource, viewModel.SelectedPackageSource);
292		}
293		
294		[Test]
295		public void SelectedPackageSource_TwoPackageSourcesInOptionsAndActivePackageSourceIsSecondSource_IsSecondPackageSource()
296		{
297			CreateRegisteredPackageRepositories();
298			AddTwoPackageSourcesToRegisteredSources();
299			CreateViewModel(registeredPackageRepositories);
300			
301			var expectedPackageSource = registeredPackageRepositories.PackageSources[1];
302			registeredPackageRepositories.ActivePackageSource = expectedPackageSource;
303			
304			Assert.AreEqual(expectedPackageSource, viewModel.SelectedPackageSource);
305		}
306		
307		[Test]
308		public void SelectedPackageSource_Changed_ActivePackageSourceChanged()
309		{
310			CreateRegisteredPackageRepositories();
311			AddTwoPackageSourcesToRegisteredSources();
312			CreateViewModel(registeredPackageRepositories);
313			
314			registeredPackageRepositories.ActivePackageSource = registeredPackageRepositories.PackageSources[0];
315			var expectedPackageSource = registeredPackageRepositories.PackageSources[1];
316			viewModel.SelectedPackageSource = expectedPackageSource;
317			
318			Assert.AreEqual(expectedPackageSource, registeredPackageRepositories.ActivePackageSource);
319		}
320		
321		[Test]
322		public void SelectedPackageSource_PackageSourceChangedAfterReadingPackages_PackagesReadFromNewPackageSourceAndDisplayed()
323		{
324			SetUpTwoPackageSourcesAndViewModelHasReadPackages();
325			ClearReadPackagesTasks();
326			ChangeSelectedPackageSourceToSecondSource();
327			CompleteReadPackagesTask();
328			
329			var expectedPackages = registeredPackageRepositories.FakeActiveRepository.FakePackages;
330			
331			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
332		}
333		
334		[Test]
335		public void SelectedPackageSource_PackageSourceChangedAfterReadingPackages_PropertyChangedEventFiredAfterPackagesAreRead()
336		{
337			SetUpTwoPackageSourcesAndViewModelHasReadPackages();
338			
339			int packageCountWhenPropertyChangedEventFired = -1;
340			viewModel.PropertyChanged += (sender, e) => packageCountWhenPropertyChangedEventFired = viewModel.PackageViewModels.Count;
341			ClearReadPackagesTasks();
342			ChangeSelectedPackageSourceToSecondSource();
343			CompleteReadPackagesTask();
344			
345			Assert.AreEqual(1, packageCountWhenPropertyChangedEventFired);
346		}
347		
348		[Test]
349		public void SelectedPackageSource_PackageSourceChangedButToSameSelectedPackageSource_PackagesAreNotRead()
350		{
351			SetUpTwoPackageSourcesAndViewModelHasReadPackages();
352			ChangeSelectedPackageSourceToFirstSource();
353			
354			Assert.AreEqual(0, viewModel.PackageViewModels.Count);
355		}
356		
357		[Test]
358		public void SelectedPackageSource_PackageSourceChangedButToSameSelectedPackageSource_PropertyChangedEventNotFired()
359		{
360			SetUpTwoPackageSourcesAndViewModelHasReadPackages();
361			
362			bool fired = false;
363			viewModel.PropertyChanged += (sender, e) => fired = true;
364			ChangeSelectedPackageSourceToFirstSource();
365			
366			Assert.IsFalse(fired);
367		}
368		
369		[Test]
370		public void GetAllPackages_OnePackageInRepository_RepositoryNotCreatedByBackgroundThread()
371		{
372			CreateRegisteredPackageRepositories();
373			AddOnePackageSourceToRegisteredSources();
374			registeredPackageRepositories.FakeActiveRepository.FakePackages.Add(new FakePackage());
375			CreateViewModel(registeredPackageRepositories);
376			viewModel.ReadPackages();
377			
378			registeredPackageRepositories.FakeActiveRepository = null;
379			CompleteReadPackagesTask();
380			
381			Assert.AreEqual(1, viewModel.PackageViewModels.Count);
382		}
383		
384		[Test]
385		public void ReadPackages_ExceptionThrownWhenAccessingActiveRepository_ErrorMessageFromExceptionNotOverriddenByReadPackagesCall()
386		{
387			CreateExceptionThrowingRegisteredPackageRepositories();
388			exceptionThrowingRegisteredPackageRepositories.ExceptionToThrowWhenActiveRepositoryAccessed = 
389				new Exception("Test");
390			CreateViewModel(exceptionThrowingRegisteredPackageRepositories);
391			viewModel.ReadPackages();
392			
393			ApplicationException ex = Assert.Throws<ApplicationException>(CompleteReadPackagesTask);
394			Assert.AreEqual("Test", ex.Message);
395		}
396		
397		[Test]
398		public void ReadPackages_RepositoryHasPrereleaseAndReleasePackage_HasReleasePackageOnly()
399		{
400			CreateViewModel();
401			
402			var releasePackage = new FakePackage("Test", "1.1.0.0");
403			var prereleasePackage = new FakePackage("Test", "1.1.0-alpha");
404			
405			var packages = new FakePackage[] {
406				releasePackage, prereleasePackage
407			};
408			
409			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
410			
411			viewModel.ReadPackages();
412			CompleteReadPackagesTask();
413			
414			var expectedPackages = new FakePackage[] {
415				releasePackage
416			};
417			
418			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
419		}
420		
421		[Test]
422		public void ReadPackages_TwoPackagesWithDifferentDownloadCounts_HighestDownloadCountShownFirst()
423		{
424			CreateViewModel();
425			
426			var package1 = new FakePackage("A", "0.1.0.0") { DownloadCount = 1 };
427			var package2 = new FakePackage("Z", "0.1.0.0") { DownloadCount = 1000 };
428			
429			var packages = new FakePackage[] {
430				package1, package2
431			};
432			
433			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
434			
435			viewModel.ReadPackages();
436			CompleteReadPackagesTask();
437			
438			var expectedPackages = new FakePackage[] {
439				package2, package1
440			};
441			
442			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
443		}
444		
445		[Test]
446		public void GetPackagesFromPackageSource_RepositoryHasThreePackagesWithSameIdButDifferentVersions_LatestPackageVersionOnlyRequestedFromPackageSource()
447		{
448			CreateViewModel();
449			var package1 = new FakePackage("Test", "0.1.0.0") { IsLatestVersion = false };
450			var package2 = new FakePackage("Test", "0.2.0.0") { IsLatestVersion = false };
451			var package3 = new FakePackage("Test", "0.3.0.0") { IsLatestVersion = true };
452			var packages = new FakePackage[] {
453				package1, package2, package3
454			};
455			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
456			viewModel.ReadPackages();
457			
458			IList<IPackage> allPackages = viewModel.GetPackagesFromPackageSource().ToList();
459			
460			var expectedPackages = new FakePackage[] {
461				package3
462			};
463			PackageCollectionAssert.AreEqual(expectedPackages, allPackages);
464		}
465		
466		[Test]
467		public void PackageSources_TwoPackageSourcesButFirstIsDisabled_DoesNotReturnDisabledPackageSource()
468		{
469			CreateRegisteredPackageRepositories();
470			AddTwoPackageSourcesToRegisteredSourcesWithFirstOneDisabled();
471			CreateViewModel(registeredPackageRepositories);
472			
473			IEnumerable<PackageSource> packageSources = viewModel.PackageSources;
474			
475			bool containsDisabledPackageSource = packageSources.Contains(registeredPackageRepositories.PackageSources[0]);
476			bool containsEnabledPackageSource = packageSources.Contains(registeredPackageRepositories.PackageSources[1]);
477			Assert.IsFalse(containsDisabledPackageSource);
478			Assert.IsTrue(containsEnabledPackageSource);
479		}
480		
481		[Test]
482		public void IsInstallAllPackagesEnabled_RepositoryHasTwoPackages_ReturnsFalse()
483		{
484			CreateViewModel();
485			var package1 = new FakePackage("Test", "0.1.0.0");
486			var package2 = new FakePackage("Test", "0.2.0.0");
487			var packages = new FakePackage[] {
488				package1, package2
489			};
490			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
491			viewModel.ReadPackages();
492			CompleteReadPackagesTask();
493			
494			bool enabled = viewModel.IsUpdateAllPackagesEnabled;
495			
496			Assert.IsFalse(enabled);
497		}
498		
499		[Test]
500		public void ShowPrerelease_ByDefault_ReturnsTrue()
501		{
502			CreateViewModel();
503			
504			bool show = viewModel.ShowPrerelease;
505			
506			Assert.IsTrue(show);
507		}
508		
509		[Test]
510		public void ReadPackages_RepositoryHasPrereleasePackageAndIncludePrereleaseIsTrue_HasPrereleasePackageInList()
511		{
512			CreateViewModel();
513			viewModel.IncludePrerelease = true;
514			var prereleasePackage = new FakePackage("Test", "1.1.0-alpha") { IsLatestVersion = false };
515			var expectedPackages = new FakePackage[] { prereleasePackage };
516			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(expectedPackages);
517			
518			viewModel.ReadPackages();
519			CompleteReadPackagesTask();
520			
521			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
522		}
523		
524		[Test]
525		public void ReadPackages_RepositoryHasThreePrereleasePackagesWithSameIdButDifferentVersionsAndIncludePrereleaseIsTrue_HasLatestPreleasePackageVersionOnly()
526		{
527			CreateViewModel();
528			viewModel.IncludePrerelease = true;
529			var package1 = new FakePackage("Test", "0.1.0.0-alpha") { IsLatestVersion = false };
530			var package2 = new FakePackage("Test", "0.2.0.0-alpha") { IsLatestVersion = false };
531			var package3 = new FakePackage("Test", "0.3.0.0-alpha") { IsLatestVersion = false };
532			var packages = new FakePackage[] {
533				package1, package2, package3
534			};
535			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
536			
537			viewModel.ReadPackages();
538			CompleteReadPackagesTask();
539			
540			var expectedPackages = new FakePackage[] {
541				package3
542			};
543			PackageCollectionAssert.AreEqual(expectedPackages, viewModel.PackageViewModels);
544		}
545		
546		[Test]
547		public void PackageViewModels_GetParentOfPackageViewModel_ReturnsAvailablePackagesViewModel()
548		{
549			CreateViewModel();
550			var package1 = new FakePackage("Test", "0.1.0.0");
551			var package2 = new FakePackage("Test", "0.2.0.0");
552			var packages = new FakePackage[] {
553				package1, package2
554			};
555			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
556			viewModel.ReadPackages();
557			CompleteReadPackagesTask();
558			PackageViewModel childViewModel = viewModel.PackageViewModels.First();
559			
560			IPackageViewModelParent parent = childViewModel.GetParent();
561			
562			Assert.AreEqual(viewModel, parent);
563		}
564		
565		[Test]
566		public void GetPackagesFromPackageSource_RepositoryIsServiceBasedRepository_ServiceBasedRepositorySearchUsed()
567		{
568			CreateViewModel();
569			var package = FakePackage.CreatePackageWithVersion("Test", "0.1.0.0");
570			var packages = new FakePackage[] { package };
571			var repository = new FakeServiceBasedRepository();
572			repository.PackagesToReturnForSearch("id:test", false, packages);
573			registeredPackageRepositories.FakeActiveRepository = repository;
574			viewModel.SearchTerms = "id:test";
575			viewModel.IncludePrerelease = false;
576			viewModel.ReadPackages();
577			
578			IList<IPackage> allPackages = viewModel.GetPackagesFromPackageSource().ToList();
579			
580			var expectedPackages = new FakePackage[] { package };
581			PackageCollectionAssert.AreEqual(expectedPackages, allPackages);
582		}
583		
584		[Test]
585		public void GetPackagesFromPackageSource_RepositoryIsServiceBasedRepositoryAndPrereleaseIncluded_ServiceBasedRepositorySearchUsed()
586		{
587			CreateViewModel();
588			var package = FakePackage.CreatePackageWithVersion("Test", "0.1.0.0");
589			var packages = new FakePackage[] { package };
590			var repository = new FakeServiceBasedRepository();
591			repository.PackagesToReturnForSearch("id:test", true, packages);
592			registeredPackageRepositories.FakeActiveRepository = repository;
593			viewModel.SearchTerms = "id:test";
594			viewModel.IncludePrerelease = true;
595			viewModel.ReadPackages();
596			
597			IList<IPackage> allPackages = viewModel.GetPackagesFromPackageSource().ToList();
598			
599			var expectedPackages = new FakePackage[] { package };
600			PackageCollectionAssert.AreEqual(expectedPackages, allPackages);
601		}
602		
603		[Test]
604		public void GetPackagesFromPackageSource_RepositoryHasThreePackagesWithSameIdButDifferentVersionsAndSearchIncludesPrerelease_AbsoluteLatestPackageVersionOnlyRequestedFromPackageSource()
605		{
606			CreateViewModel();
607			var package1 = new FakePackage("Test", "0.1.0.0") { IsAbsoluteLatestVersion = false };
608			var package2 = new FakePackage("Test", "0.2.0.0") { IsAbsoluteLatestVersion = false };
609			var package3 = new FakePackage("Test", "0.3.0.0") { IsAbsoluteLatestVersion = true };
610			var packages = new FakePackage[] {
611				package1, package2, package3
612			};
613			registeredPackageRepositories.FakeActiveRepository.FakePackages.AddRange(packages);
614			viewModel.IncludePrerelease = true;
615			viewModel.ReadPackages();
616			
617			IList<IPackage> allPackages = viewModel.GetPackagesFromPackageSource().ToList();
618			
619			var expectedPackages = new FakePackage[] {
620				package3
621			};
622			PackageCollectionAssert.AreEqual(expectedPackages, allPackages);
623		}
624		
625		[Test]
626		public void ReadPackages_ActiveRepositoryChangedWhichUsesInvalidUrl_InvalidUrlExceptionIsShownAsErrorMessage()
627		{
628			CreateExceptionThrowingRegisteredPackageRepositories();
629			CreateViewModel(exceptionThrowingRegisteredPackageRepositories);
630			var package = new FakePackage("Test", "0.1.0.0");
631			exceptionThrowingRegisteredPackageRepositories
632				.FakeActiveRepository
633				.FakePackages
634				.Add(package);
635			viewModel.ReadPackages();
636			CompleteReadPackagesTask();
637			taskFactory.ClearAllFakeTasks();
638			exceptionThrowingRegisteredPackageRepositories.ExceptionToThrowWhenActiveRepositoryAccessed =
639				new Exception("Invalid url");
640			
641			viewModel.ReadPackages();
642			FakeTask<PackagesForSelectedPageResult> task = taskFactory.FirstFakeTaskCreated;
643			ApplicationException ex = Assert.Throws<ApplicationException>(() => task.ExecuteTaskButNotContinueWith());
644			task.Exception = new AggregateException(ex);
645			task.IsFaulted = true;
646			task.ExecuteContinueWith();
647			
648			Assert.AreEqual("Invalid url", ex.Message);
649			Assert.IsTrue(viewModel.HasError);
650			Assert.AreEqual("Invalid url", viewModel.ErrorMessage);
651		}
652	}
653}