예제 #1
0
        private EditProjectFileCommand CreateInstance(
            UnconfiguredProject unconfiguredProject = null,
            bool implementCapabilities       = true,
            IMsBuildAccessor msbuildAccessor = null,
            IFileSystem fileSystem           = null,
            ITextDocumentFactoryService textDocumentService      = null,
            IVsEditorAdaptersFactoryService editorAdapterService = null,
            IProjectThreadingService threadingService            = null,
            IVsShellUtilitiesHelper shellUtilities = null
            )
        {
            UnitTestHelper.IsRunningUnitTests = true;
            var uProj        = unconfiguredProject ?? IUnconfiguredProjectFactory.Create();
            var capabilities = IProjectCapabilitiesServiceFactory.ImplementsContains(CapabilityChecker(implementCapabilities));
            var msbuild      = msbuildAccessor ?? IMsBuildAccessorFactory.Create();
            var fs           = fileSystem ?? new IFileSystemMock();
            var tds          = textDocumentService ?? ITextDocumentFactoryServiceFactory.Create();
            var eas          = editorAdapterService ?? IVsEditorAdaptersFactoryServiceFactory.Create();
            var threadServ   = threadingService ?? IProjectThreadingServiceFactory.Create();
            var shellUt      = shellUtilities ?? new TestShellUtilitiesHelper(
                (sp, path) => Tuple.Create(IVsHierarchyFactory.Create(), (uint)1, IVsPersistDocDataFactory.Create(), (uint)1),
                (sp, path, edType, logView) => IVsWindowFrameFactory.Create());

            return(new EditProjectFileCommand(uProj, capabilities, IServiceProviderFactory.Create(), msbuild, fs, tds, eas, threadServ, shellUt));
        }
예제 #2
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);
        }
예제 #3
0
        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);
        }
예제 #4
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);
        }
        public async Task FrameOpenCloseListener_InitializeEventsAsync_SetsUpWindowFrameEvents()
        {
            var uiShell = IVsUIShell7Factory.ImplementAdviseWindowEvents(l => 1);

            uint eventsCookie = 2;
            var  solution     = IVsSolutionFactory.CreateWithAdviseUnadviseSolutionEvents(eventsCookie);

            var serviceProvider = IServiceProviderFactory.ImplementGetService(ServiceTypeChecker(uiShell, solution));

            var listener = new FrameOpenCloseListener(serviceProvider,
                                                      IProjectFileEditorPresenterFactory.Create(),
                                                      new IProjectThreadingServiceMock(),
                                                      UnconfiguredProjectFactory.Create());

            await listener.InitializeEventsAsync(IVsWindowFrameFactory.Create());

            Mock.Get(uiShell).Verify(u => u.AdviseWindowFrameEvents(listener), Times.Once);
            Mock.Get(solution).Verify(s => s.AdviseSolutionEvents(listener, out eventsCookie), Times.Once);
        }
예제 #6
0
        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);
        }
        public async Task FrameOpenCloseListener_Dispose_UnsubscribesCorrectly()
        {
            var uiShell = IVsUIShell7Factory.ImplementAdviseUnadviseWindowEvents(l => 1234, c => Assert.Equal <uint>(1234, c));

            var solution = IVsSolutionFactory.CreateWithAdviseUnadviseSolutionEvents(4321);

            var serviceProvider = IServiceProviderFactory.ImplementGetService(ServiceTypeChecker(uiShell, solution));

            var listener = new FrameOpenCloseListener(serviceProvider,
                                                      IProjectFileEditorPresenterFactory.Create(),
                                                      new IProjectThreadingServiceMock(),
                                                      UnconfiguredProjectFactory.Create());

            await listener.InitializeEventsAsync(IVsWindowFrameFactory.Create());

            await listener.DisposeAsync();

            Mock.Get(uiShell).Verify(u => u.UnadviseWindowFrameEvents(1234), Times.Once);
            Mock.Get(solution).Verify(s => s.UnadviseSolutionEvents(4321), Times.Once);
        }
예제 #8
0
        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);
        }