コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }