|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using GitHub.Models; |
| 5 | +using GitHub.Services; |
| 6 | +using GitHub.StartPage; |
| 7 | +using Microsoft.VisualStudio.Shell.CodeContainerManagement; |
| 8 | +using NSubstitute; |
| 9 | +using NUnit.Framework; |
| 10 | +using Task = System.Threading.Tasks.Task; |
| 11 | +using ServiceProgressData = Microsoft.VisualStudio.Shell.ServiceProgressData; |
| 12 | + |
| 13 | +public class GitHubContainerProviderTests |
| 14 | +{ |
| 15 | + public class TheAcquireCodeContainerAsyncMethod |
| 16 | + { |
| 17 | + [Test] |
| 18 | + public async Task CloneOrOpenRepository_CloneDialogResult_Returned_By_ShowCloneDialog() |
| 19 | + { |
| 20 | + var downloadProgress = Substitute.For<IProgress<ServiceProgressData>>(); |
| 21 | + var cancellationToken = CancellationToken.None; |
| 22 | + var dialogService = Substitute.For<IDialogService>(); |
| 23 | + var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo"); |
| 24 | + dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result); |
| 25 | + var cloneService = Substitute.For<IRepositoryCloneService>(); |
| 26 | + var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService); |
| 27 | + |
| 28 | + await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken); |
| 29 | + |
| 30 | + await cloneService.Received(1).CloneOrOpenRepository(result, downloadProgress, cancellationToken); |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public async Task Pass_DisplayUrl_To_ShowCloneDialog() |
| 35 | + { |
| 36 | + var displayUrl = "https://github.com/owner/displayUrl"; |
| 37 | + var browseOnlineUrl = "https://github.com/owner/browseOnlineUrl"; |
| 38 | + var remoteCodeContainer = new RemoteCodeContainer("Name", Guid.NewGuid(), new Uri(displayUrl), new Uri(browseOnlineUrl), |
| 39 | + DateTimeOffset.Now, new Dictionary<string, string>()); |
| 40 | + var downloadProgress = Substitute.For<IProgress<ServiceProgressData>>(); |
| 41 | + var cancellationToken = CancellationToken.None; |
| 42 | + var dialogService = Substitute.For<IDialogService>(); |
| 43 | + var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo"); |
| 44 | + dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result); |
| 45 | + var cloneService = Substitute.For<IRepositoryCloneService>(); |
| 46 | + var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService); |
| 47 | + |
| 48 | + await target.AcquireCodeContainerAsync(remoteCodeContainer, downloadProgress, cancellationToken); |
| 49 | + |
| 50 | + await dialogService.Received(1).ShowCloneDialog(Arg.Any<IConnection>(), displayUrl); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public async Task Completes_When_Returning_CodeContainer() |
| 55 | + { |
| 56 | + var downloadProgress = Substitute.For<IProgress<ServiceProgressData>>(); |
| 57 | + var cancellationToken = CancellationToken.None; |
| 58 | + var dialogService = Substitute.For<IDialogService>(); |
| 59 | + var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo"); |
| 60 | + dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result); |
| 61 | + var cloneService = Substitute.For<IRepositoryCloneService>(); |
| 62 | + var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService); |
| 63 | + |
| 64 | + var codeContainer = await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken); |
| 65 | + |
| 66 | + Assert.That(codeContainer, Is.Not.Null); |
| 67 | + downloadProgress.Received(1).Report( |
| 68 | + Arg.Is<ServiceProgressData>(x => x.TotalSteps > 0 && x.CurrentStep == x.TotalSteps)); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public async Task Does_Not_Complete_When_CloneDialog_Canceled() |
| 73 | + { |
| 74 | + var downloadProgress = Substitute.For<IProgress<ServiceProgressData>>(); |
| 75 | + var cancellationToken = CancellationToken.None; |
| 76 | + var dialogService = Substitute.For<IDialogService>(); |
| 77 | + var result = (CloneDialogResult)null; |
| 78 | + dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result); |
| 79 | + var cloneService = Substitute.For<IRepositoryCloneService>(); |
| 80 | + var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService); |
| 81 | + |
| 82 | + var codeContainer = await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken); |
| 83 | + |
| 84 | + await cloneService.ReceivedWithAnyArgs(0).CloneOrOpenRepository(null, null, null); |
| 85 | + downloadProgress.ReceivedWithAnyArgs(0).Report(null); |
| 86 | + Assert.That(codeContainer, Is.Null); |
| 87 | + } |
| 88 | + |
| 89 | + static GitHubContainerProvider CreateGitHubContainerProvider(IDialogService dialogService = null, |
| 90 | + IRepositoryCloneService cloneService = null, IUsageTracker usageTracker = null) |
| 91 | + { |
| 92 | + dialogService = dialogService ?? Substitute.For<IDialogService>(); |
| 93 | + cloneService = cloneService ?? Substitute.For<IRepositoryCloneService>(); |
| 94 | + usageTracker = usageTracker ?? Substitute.For<IUsageTracker>(); |
| 95 | + |
| 96 | + var sp = Substitute.For<IGitHubServiceProvider>(); |
| 97 | + sp.GetService<IDialogService>().Returns(dialogService); |
| 98 | + sp.GetService<IRepositoryCloneService>().Returns(cloneService); |
| 99 | + sp.GetService<IUsageTracker>().Returns(usageTracker); |
| 100 | + |
| 101 | + var gitHubServiceProvider = new Lazy<IGitHubServiceProvider>(() => sp); |
| 102 | + return new GitHubContainerProvider(gitHubServiceProvider); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments