public static void Add <T>(this OrderPrecedenceExportFactoryCollection <T> collection, T value, string appliesTo = null, int orderPrecedence = 0) { var metadata = IOrderPrecedenceMetadataViewFactory.Create(appliesTo, orderPrecedence); var factory = ExportFactoryFactory.ImplementCreateValueWithAutoDispose(() => value, metadata); collection.Add(factory); }
public void ProjectFileEditorPresenter_NullTextBufferListenerFactory_Throws() { Assert.Throws <ArgumentNullException>("textBufferListenerFactory", () => new ProjectFileEditorPresenter( IProjectThreadingServiceFactory.Create(), UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), IVsShellUtilitiesHelperFactory.Create(), ExportFactoryFactory.CreateInstance <IProjectFileModelWatcher>(), null, ExportFactoryFactory.CreateInstance <IFrameOpenCloseListener>(), ExportFactoryFactory.CreateInstance <ITextBufferManager>())); }
public async Task ProjectFileEditorPresenter_SaveProjectFile_OnlySavesInEditorOpen(int state) { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.Create(); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var threadingService = IProjectThreadingServiceFactory.Create(); var editorState = new ProjectFileEditorPresenterTester( threadingService, UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory) { CurrentState = (ProjectFileEditorPresenter.EditorState)state }; // When SaveAsync is called, we should hit an assert in the NoEditor case. bool assertHit = false; try { await editorState.SaveProjectFileAsync(); } catch (InvalidOperationException) { assertHit = true; } Assert.True(state != (int)ProjectFileEditorPresenter.EditorState.NoEditor || assertHit); var textBufferMock = Mock.Get(textBufferManager); textBufferMock.Verify(t => t.SetReadOnlyAsync(It.IsAny <bool>()), Times.Never); textBufferMock.Verify(t => t.SaveAsync(), Times.Never); }
public async Task ProjectFileEditorPresenter_UpdateProjectFile_SchedulesUpdate() { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.Create(); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var threadingService = IProjectThreadingServiceFactory.Create(); var editorState = new ProjectFileEditorPresenterTester( threadingService, UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory); // Implement ResetBuffer so we can verify that the editor is in BufferUpdateScheduled when calling ResetBuffer. While here, // verify that the buffer was set to readonly before reset was called var textBufferMock = Mock.Get(textBufferManager); textBufferMock.Setup(t => t.ResetBufferAsync()).Callback(() => { Assert.Equal(ProjectFileEditorPresenter.EditorState.BufferUpdateScheduled, editorState.CurrentState); textBufferMock.Verify(t => t.SetReadOnlyAsync(true), Times.Once); }).Returns(Task.CompletedTask); await editorState.OpenEditorAsync(); var jt = editorState.ScheduleProjectFileUpdate(); // Ensure the update actually runs await jt.JoinAsync(); textBufferMock.Verify(t => t.SetReadOnlyAsync(true), Times.Once); textBufferMock.Verify(t => t.ResetBufferAsync(), Times.Once); textBufferMock.Verify(t => t.SetReadOnlyAsync(false), Times.Once); }
public async Task ProjectFileEditorPresenter_SaveProjectFile_SavesFile() { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.Create(); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var threadingService = IProjectThreadingServiceFactory.Create(); var editorState = new ProjectFileEditorPresenterTester( threadingService, UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory); var textBufferMock = Mock.Get(textBufferManager); // Implement textBufferManager.SaveAsync to verify the editor is in WritingProjectFile while saving textBufferMock.Setup(t => t.SaveAsync()).Callback(() => { Assert.Equal(ProjectFileEditorPresenter.EditorState.WritingProjectFile, editorState.CurrentState); textBufferMock.Verify(t => t.SetReadOnlyAsync(true), Times.Once); }).Returns(Task.CompletedTask); await editorState.OpenEditorAsync(); await editorState.SaveProjectFileAsync(); textBufferMock.Verify(t => t.SetReadOnlyAsync(true), Times.Once); textBufferMock.Verify(t => t.SaveAsync(), Times.Once); textBufferMock.Verify(t => t.SetReadOnlyAsync(false), Times.Once); }
public void ProjectFileEditorPresenter_UpdateProjectFileIncorrectEditorState_DoesNothing(int state) { var editorState = new ProjectFileEditorPresenterTester( IProjectThreadingServiceFactory.Create(), UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), IVsShellUtilitiesHelperFactory.Create(), ExportFactoryFactory.CreateInstance <IProjectFileModelWatcher>(), ExportFactoryFactory.CreateInstance <ITextBufferStateListener>(), ExportFactoryFactory.CreateInstance <IFrameOpenCloseListener>(), ExportFactoryFactory.CreateInstance <ITextBufferManager>()) { CurrentState = (ProjectFileEditorPresenter.EditorState)state }; Assert.Null(editorState.ScheduleProjectFileUpdate()); }
public async Task ProjectFileEditorPresenter_DisposeWithOpen_DisposesListeners() { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.ImplementCloseFrame(options => { Assert.Equal((uint)__FRAMECLOSE.FRAMECLOSE_PromptSave, options); return(VSConstants.S_OK); }); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var editorState = new ProjectFileEditorPresenterTester( new IProjectThreadingServiceMock(), UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory); // Mock dispose for one of the listeners so it can verify the editor is actually in EditorClosing Mock.Get(textBufferListener).Setup(t => t.Dispose()).Callback(() => Assert.Equal(ProjectFileEditorPresenter.EditorState.EditorClosing, editorState.CurrentState)); await editorState.OpenEditorAsync(); await editorState.CloseCurrentEditorAsync(); Mock.Get(textBufferManager).Verify(t => t.Dispose(), Times.Once); Mock.Get(textBufferListener).Verify(t => t.Dispose(), Times.Once); Mock.Get(frameListener).Verify(f => f.DisposeAsync(), Times.Once); Mock.Get(projectFileWatcher).Verify(p => p.Dispose(), Times.Once); Assert.Equal(ProjectFileEditorPresenter.EditorState.NoEditor, editorState.CurrentState); }
private WorkspaceProjectContextHostInstance CreateInstance(ConfiguredProject project = null, IProjectThreadingService threadingService = null, IUnconfiguredProjectTasksService tasksService = null, IProjectSubscriptionService projectSubscriptionService = null, IActiveEditorContextTracker activeWorkspaceProjectContextTracker = null, IWorkspaceProjectContextProvider workspaceProjectContextProvider = null, IApplyChangesToWorkspaceContext applyChangesToWorkspaceContext = null) { project = project ?? ConfiguredProjectFactory.Create(); threadingService = threadingService ?? IProjectThreadingServiceFactory.Create(); tasksService = tasksService ?? IUnconfiguredProjectTasksServiceFactory.Create(); projectSubscriptionService = projectSubscriptionService ?? IProjectSubscriptionServiceFactory.Create(); activeWorkspaceProjectContextTracker = activeWorkspaceProjectContextTracker ?? IActiveEditorContextTrackerFactory.Create(); workspaceProjectContextProvider = workspaceProjectContextProvider ?? IWorkspaceProjectContextProviderFactory.ImplementCreateProjectContextAsync(IWorkspaceProjectContextAccessorFactory.Create()); applyChangesToWorkspaceContext = applyChangesToWorkspaceContext ?? IApplyChangesToWorkspaceContextFactory.Create(); return(new WorkspaceProjectContextHostInstance(project, threadingService, tasksService, projectSubscriptionService, workspaceProjectContextProvider, activeWorkspaceProjectContextTracker, ExportFactoryFactory.ImplementCreateValueWithAutoDispose(() => applyChangesToWorkspaceContext))); }
public async Task ProjectFileEditorPresenter_MultipleOpen_CallsShowSecondTime() { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.Create(); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var editorState = new ProjectFileEditorPresenterTester( new IProjectThreadingServiceMock(), UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory); await editorState.OpenEditorAsync(); Mock.Get(windowFrame).Verify(w => w.Show(), Times.Never); // On the second call, we should call show on the frame, and none of the listeners should have been set up again await editorState.OpenEditorAsync(); Mock.Get(windowFrame).Verify(w => w.Show(), Times.Once); Mock.Get(textBufferManager).Verify(t => t.InitializeBufferAsync(), Times.Once); Mock.Get(textBufferListener).Verify(t => t.InitializeListenerAsync(filePath), Times.Once); Mock.Get(frameListener).Verify(f => f.InitializeEventsAsync(windowFrame), Times.Once); Mock.Get(projectFileWatcher).Verify(p => p.InitializeModelWatcher(), Times.Once); Assert.Equal(editorState.CurrentState, ProjectFileEditorPresenter.EditorState.EditorOpen); }
private static WorkspaceProjectContextHostInstance CreateInstance(ConfiguredProject?project = null, IProjectThreadingService?threadingService = null, IUnconfiguredProjectTasksService?tasksService = null, IProjectSubscriptionService?projectSubscriptionService = null, IActiveEditorContextTracker?activeWorkspaceProjectContextTracker = null, IWorkspaceProjectContextProvider?workspaceProjectContextProvider = null, IApplyChangesToWorkspaceContext?applyChangesToWorkspaceContext = null) { project ??= ConfiguredProjectFactory.Create(); threadingService ??= IProjectThreadingServiceFactory.Create(); tasksService ??= IUnconfiguredProjectTasksServiceFactory.Create(); projectSubscriptionService ??= IProjectSubscriptionServiceFactory.Create(); activeWorkspaceProjectContextTracker ??= IActiveEditorContextTrackerFactory.Create(); workspaceProjectContextProvider ??= IWorkspaceProjectContextProviderFactory.ImplementCreateProjectContextAsync(IWorkspaceProjectContextAccessorFactory.Create()); applyChangesToWorkspaceContext ??= IApplyChangesToWorkspaceContextFactory.Create(); IDataProgressTrackerService dataProgressTrackerService = IDataProgressTrackerServiceFactory.Create(); return(new WorkspaceProjectContextHostInstance(project, threadingService, tasksService, projectSubscriptionService, workspaceProjectContextProvider, activeWorkspaceProjectContextTracker, ExportFactoryFactory.ImplementCreateValueWithAutoDispose(() => applyChangesToWorkspaceContext), dataProgressTrackerService)); }
private static ApplyChangesToWorkspaceContext CreateInstance(ConfiguredProject?project = null, ICommandLineParserService?commandLineParser = null, IProjectLogger?logger = null, params IWorkspaceContextHandler[] handlers) { if (project == null) { var unconfiguredProject = UnconfiguredProjectFactory.ImplementFullPath(@"C:\Project\Project.csproj"); project = ConfiguredProjectFactory.ImplementUnconfiguredProject(unconfiguredProject); } commandLineParser ??= ICommandLineParserServiceFactory.Create(); logger ??= IProjectLoggerFactory.Create(); var factories = handlers.Select(h => ExportFactoryFactory.ImplementCreateValueWithAutoDispose(() => h)) .ToArray(); var applyChangesToWorkspaceContext = new ApplyChangesToWorkspaceContext(project, logger, factories); applyChangesToWorkspaceContext.CommandLineParsers.Add(commandLineParser); return(applyChangesToWorkspaceContext); }
public async Task ProjectFileEditorPresenter_OpenEditor_SetsUpListeners() { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.Create(); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var editorState = new ProjectFileEditorPresenterTester( new IProjectThreadingServiceMock(), UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory); // Mock textBufferManager.InitializeBufferAsync so it can verify the editor is actually in Initializing Mock.Get(textBufferManager).Setup(t => t.InitializeBufferAsync()).Callback(() => Assert.Equal(ProjectFileEditorPresenter.EditorState.Initializing, editorState.CurrentState)).Returns(Task.CompletedTask); await editorState.OpenEditorAsync(); Mock.Get(textBufferManager).Verify(t => t.InitializeBufferAsync(), Times.Once); Mock.Get(textBufferListener).Verify(t => t.InitializeListenerAsync(filePath), Times.Once); Mock.Get(frameListener).Verify(f => f.InitializeEventsAsync(windowFrame), Times.Once); Mock.Get(projectFileWatcher).Verify(p => p.InitializeModelWatcher(), Times.Once); Assert.Equal(editorState.CurrentState, ProjectFileEditorPresenter.EditorState.EditorOpen); }
public async Task DisposeWithoutOpen_DoesNothing() { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.ImplementCloseFrame(options => { Assert.Equal((uint)__FRAMECLOSE.FRAMECLOSE_PromptSave, options); return(VSConstants.S_OK); }); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var editorState = new ProjectFileEditorPresenterTester( new IProjectThreadingServiceMock(), UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory); await editorState.CloseCurrentEditorAsync(); Mock.Get(textBufferManager).Verify(t => t.Dispose(), Times.Never); Mock.Get(textBufferListener).Verify(t => t.Dispose(), Times.Never); Mock.Get(frameListener).Verify(f => f.DisposeAsync(), Times.Never); Mock.Get(projectFileWatcher).Verify(p => p.Dispose(), Times.Never); }
public async Task ProjectFileEditorPresenter_CloseWindowFail_ReturnsStopClose() { var filePath = @"C:\Temp\ConsoleApp1.csproj"; var textBufferManager = ITextBufferManagerFactory.ImplementFilePath(filePath); var textBufferManagerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferManager); var textBufferListener = ITextBufferStateListenerFactory.Create(); var textBufferListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => textBufferListener); var frameListener = IFrameOpenCloseListenerFactory.Create(); var frameListenerFactory = ExportFactoryFactory.ImplementCreateValue(() => frameListener); var projectFileWatcher = IProjectFileModelWatcherFactory.Create(); var projectFileWatcherFactory = ExportFactoryFactory.ImplementCreateValue(() => projectFileWatcher); var windowFrame = IVsWindowFrameFactory.ImplementCloseFrame(options => { Assert.Equal((uint)__FRAMECLOSE.FRAMECLOSE_PromptSave, options); return(VSConstants.E_FAIL); }); var shellUtilities = IVsShellUtilitiesHelperFactory.ImplementOpenDocument(filePath, XmlFactoryGuid, Guid.Empty, windowFrame); var editorState = new ProjectFileEditorPresenterTester( new IProjectThreadingServiceMock(), UnconfiguredProjectFactory.Create(), IServiceProviderFactory.Create(), shellUtilities, projectFileWatcherFactory, textBufferListenerFactory, frameListenerFactory, textBufferManagerFactory); await editorState.OpenEditorAsync(); Assert.False(await editorState.CanCloseWindowAsync()); }