public async Task WhenSwitchingToProjectNode_ThenListIsPopulated( [Values( PackageInventoryType.AvailablePackages, PackageInventoryType.InstalledPackages)] PackageInventoryType type) { var node = new Mock <IProjectExplorerProjectNode>(); node.SetupGet(n => n.Project).Returns(new ProjectLocator("project-1")); node.SetupGet(n => n.DisplayName).Returns("project-1"); var viewModel = CreateViewModel(type); await viewModel.SwitchToModelAsync(node.Object); // Switch again. await viewModel.SwitchToModelAsync(node.Object); Assert.AreEqual(CommandState.Enabled, PackageInventoryViewModel.GetCommandState(node.Object)); Assert.IsTrue(viewModel.IsPackageListEnabled); Assert.IsFalse(viewModel.IsInformationBarVisible); StringAssert.Contains("project-1", viewModel.WindowTitle); Assert.AreEqual(4, viewModel.AllPackages.Count); Assert.AreEqual(4, viewModel.FilteredPackages.Count); }
public PackageInventoryWindow( PackageInventoryType inventoryType, IServiceProvider serviceProvider) : base( serviceProvider.GetService <IMainForm>().MainPanel, serviceProvider.GetService <IProjectExplorer>(), serviceProvider.GetService <IEventService>(), serviceProvider.GetService <IExceptionDialog>()) { this.components = new System.ComponentModel.Container(); InitializeComponent(); this.viewModel = new PackageInventoryViewModel( serviceProvider, inventoryType); this.infoLabel.BindProperty( c => c.Text, this.viewModel, m => m.InformationText, this.components); this.components.Add(this.viewModel.OnPropertyChange( m => m.IsInformationBarVisible, visible => { this.splitContainer.Panel1Collapsed = !visible; this.splitContainer.SplitterDistance = this.splitContainer.Panel1MinSize; })); this.components.Add(this.viewModel.OnPropertyChange( m => m.WindowTitle, title => this.TabText = this.Text = title)); // Bind list. this.packageList.BindProperty( c => c.Enabled, this.viewModel, m => m.IsPackageListEnabled, this.components); this.packageList.List.BindCollection(this.viewModel.FilteredPackages); this.packageList.List.AddCopyCommands(); this.packageList.BindProperty( c => c.SearchTerm, this.viewModel, m => m.Filter, this.components); this.packageList.BindProperty( c => c.Loading, this.viewModel, m => m.IsLoading, this.components); var openUrl = new ToolStripMenuItem( "&Additional information...", null, (sender, args) => { using (Process.Start(new ProcessStartInfo() { UseShellExecute = true, Verb = "open", FileName = this.packageList.List.SelectedModelItem?.Package?.Weblink.ToString() })) { }; }); this.packageList.List.ContextMenuStrip.Items.Add(openUrl); this.packageList.List.ContextMenuStrip.Opening += (sender, args) => { openUrl.Enabled = this.packageList.List.SelectedModelItem?.Package?.Weblink != null; }; }
public static async Task <PackageInventoryModel> LoadAsync( IInventoryService inventoryService, PackageInventoryType inventoryType, IProjectExplorerNode node, CancellationToken token) { IEnumerable <GuestOsInfo> inventory; try { if (node is IProjectExplorerVmInstanceNode vmNode) { var info = await inventoryService.GetInstanceInventoryAsync( vmNode.Reference, token) .ConfigureAwait(false); inventory = info != null ? new GuestOsInfo[] { info } : Enumerable.Empty <GuestOsInfo>(); } else if (node is IProjectExplorerZoneNode zoneNode) { inventory = await inventoryService.ListZoneInventoryAsync( new ZoneLocator(zoneNode.ProjectId, zoneNode.ZoneId), OperatingSystems.Windows, token) .ConfigureAwait(false); } else if (node is IProjectExplorerProjectNode projectNode) { inventory = await inventoryService.ListProjectInventoryAsync( projectNode.ProjectId, OperatingSystems.Windows, token) .ConfigureAwait(false); } else { // Unknown/unsupported node. return(null); } } catch (Exception e) when(e.Unwrap() is GoogleApiException apiEx && apiEx.IsConstraintViolation()) { // // Reading OS inventory data can fail because of a // `compute.disableGuestAttributesAccess` constraint. // TraceSources.IapDesktop.TraceWarning( "Failed to load OS inventory data: {0}", e); inventory = Enumerable.Empty <GuestOsInfo>(); } return(PackageInventoryModel.FromInventory( node.DisplayName, inventoryType, inventory)); }
private static PackageInventoryViewModel CreateViewModel(PackageInventoryType type) { var registry = new ServiceRegistry(); registry.AddSingleton <IJobService>(new JobServiceMock()); var inventoryService = new Mock <IInventoryService>(); inventoryService.Setup(s => s.GetInstanceInventoryAsync( It.Is <InstanceLocator>(loc => loc.Name == "instance-1"), It.IsAny <CancellationToken>())) .ReturnsAsync(CreateGuestOsInfo( new InstanceLocator("project-1", "zone-1", "instance-1"), type, new Package[] { new Package("package-1", "arch-1", "ver-1"), new Package("package-2", "arch-1", "ver-2") })); inventoryService.Setup(s => s.GetInstanceInventoryAsync( It.Is <InstanceLocator>(loc => loc.Name == "instance-3"), It.IsAny <CancellationToken>())) .ReturnsAsync((GuestOsInfo)null); inventoryService.Setup(s => s.ListZoneInventoryAsync( It.IsAny <ZoneLocator>(), It.IsAny <OperatingSystems>(), It.IsAny <CancellationToken>())) .ReturnsAsync( new GuestOsInfo[] { CreateGuestOsInfo( new InstanceLocator("project-1", "zone-1", "instance-1"), type, new Package[] { new Package("package-1", "arch-1", "ver-1"), new Package("package-2", "arch-1", "ver-2") }) }); inventoryService.Setup(s => s.ListProjectInventoryAsync( It.IsAny <string>(), It.IsAny <OperatingSystems>(), It.IsAny <CancellationToken>())) .ReturnsAsync( new GuestOsInfo[] { CreateGuestOsInfo( new InstanceLocator("project-1", "zone-1", "instance-1"), type, new Package[] { new Package("package-1", "ARCH-1", "ver-1"), new Package("package-2", "ARCH-1", "ver-2") }), CreateGuestOsInfo( new InstanceLocator("project-1", "zone-2", "instance-2"), type, new Package[] { new Package("package-3", "ARCH-1", "ver-1"), new Package("package-4", "ARCH-2", "ver-3") }), CreateGuestOsInfo( new InstanceLocator("project-1", "zone-2", "instance-3"), type, null) }); registry.AddSingleton <IInventoryService>(inventoryService.Object); return(new PackageInventoryViewModel(registry, type)); }
public PackageInventoryWindow( PackageInventoryType inventoryType, IServiceProvider serviceProvider) : base(serviceProvider, WeifenLuo.WinFormsUI.Docking.DockState.DockBottomAutoHide) { this.components = new System.ComponentModel.Container(); InitializeComponent(); this.viewModel = new PackageInventoryViewModel( serviceProvider, inventoryType); this.infoLabel.BindProperty( c => c.Text, this.viewModel, m => m.InformationText, this.components); this.components.Add(this.viewModel.OnPropertyChange( m => m.IsInformationBarVisible, visible => { this.splitContainer.Panel1Collapsed = !visible; this.splitContainer.SplitterDistance = this.splitContainer.Panel1MinSize; })); this.components.Add(this.viewModel.OnPropertyChange( m => m.WindowTitle, title => { // NB. Update properties separately instead of using multi-assignment, // otherwise the title does not update properly. this.TabText = title; this.Text = title; })); this.viewModel.ResetWindowTitle(); // Fire event to set initial window title. // Bind list. this.packageList.BindProperty( c => c.Enabled, this.viewModel, m => m.IsPackageListEnabled, this.components); this.packageList.List.BindCollection(this.viewModel.FilteredPackages); this.packageList.List.AddCopyCommands(); this.packageList.BindProperty( c => c.SearchTerm, this.viewModel, m => m.Filter, this.components); this.packageList.BindProperty( c => c.Loading, this.viewModel, m => m.IsLoading, this.components); this.packageList.SearchOnKeyDown = true; var openUrl = new ToolStripMenuItem( "&Additional information...", null, (sender, args) => { using (Process.Start(new ProcessStartInfo() { UseShellExecute = true, Verb = "open", FileName = this.packageList.List.SelectedModelItem?.Package?.Weblink.ToString() })) { }; }); this.packageList.List.ContextMenuStrip.Items.Add(openUrl); this.packageList.List.ContextMenuStrip.Opening += (sender, args) => { openUrl.Enabled = this.packageList.List.SelectedModelItem?.Package?.Weblink != null; }; }