|
| 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 | + |
| 12 | +public class GitHubContainerProviderTests |
| 13 | +{ |
| 14 | + public class TheAcquireCodeContainerAsyncMethod |
| 15 | + { |
| 16 | + [Test] |
| 17 | + public async Task CloneOrOpenRepository_CloneDialogResult_Returned_By_ShowCloneDialog() |
| 18 | + { |
| 19 | + var downloadProgress = Substitute.For<IProgress<Microsoft.VisualStudio.Shell.ServiceProgressData>>(); |
| 20 | + var cancellationToken = CancellationToken.None; |
| 21 | + var dialogService = Substitute.For<IDialogService>(); |
| 22 | + var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo"); |
| 23 | + dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result); |
| 24 | + var cloneService = Substitute.For<IRepositoryCloneService>(); |
| 25 | + var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService); |
| 26 | + |
| 27 | + await target.AcquireCodeContainerAsync(downloadProgress, cancellationToken); |
| 28 | + |
| 29 | + await cloneService.Received(1).CloneOrOpenRepository(result, downloadProgress, cancellationToken); |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + public async Task Pass_DisplayUrl_To_ShowCloneDialog() |
| 34 | + { |
| 35 | + var displayUrl = "https://github.com/owner/displayUrl"; |
| 36 | + var browseOnlineUrl = "https://github.com/owner/browseOnlineUrl"; |
| 37 | + var remoteCodeContainer = new RemoteCodeContainer("Name", Guid.NewGuid(), new Uri(displayUrl), new Uri(browseOnlineUrl), |
| 38 | + DateTimeOffset.Now, new Dictionary<string, string>()); |
| 39 | + var downloadProgress = Substitute.For<IProgress<Microsoft.VisualStudio.Shell.ServiceProgressData>>(); |
| 40 | + var cancellationToken = CancellationToken.None; |
| 41 | + var dialogService = Substitute.For<IDialogService>(); |
| 42 | + var result = new CloneDialogResult(@"x:\repo", "https://github.com/owner/repo"); |
| 43 | + dialogService.ShowCloneDialog(null).ReturnsForAnyArgs(result); |
| 44 | + var cloneService = Substitute.For<IRepositoryCloneService>(); |
| 45 | + var target = CreateGitHubContainerProvider(dialogService: dialogService, cloneService: cloneService); |
| 46 | + |
| 47 | + await target.AcquireCodeContainerAsync(remoteCodeContainer, downloadProgress, cancellationToken); |
| 48 | + |
| 49 | + await dialogService.Received(1).ShowCloneDialog(Arg.Any<IConnection>(), displayUrl); |
| 50 | + } |
| 51 | + |
| 52 | + static GitHubContainerProvider CreateGitHubContainerProvider(IDialogService dialogService = null, |
| 53 | + IRepositoryCloneService cloneService = null, IUsageTracker usageTracker = null) |
| 54 | + { |
| 55 | + dialogService = dialogService ?? Substitute.For<IDialogService>(); |
| 56 | + cloneService = cloneService ?? Substitute.For<IRepositoryCloneService>(); |
| 57 | + usageTracker = usageTracker ?? Substitute.For<IUsageTracker>(); |
| 58 | + |
| 59 | + var sp = Substitute.For<IGitHubServiceProvider>(); |
| 60 | + sp.GetService<IDialogService>().Returns(dialogService); |
| 61 | + sp.GetService<IRepositoryCloneService>().Returns(cloneService); |
| 62 | + sp.GetService<IUsageTracker>().Returns(usageTracker); |
| 63 | + |
| 64 | + var gitHubServiceProvider = new Lazy<IGitHubServiceProvider>(() => sp); |
| 65 | + return new GitHubContainerProvider(gitHubServiceProvider); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments