public async Task Dispose_UnsubscribtesFromTextChangedEvents()
        {
            var tempFile              = @"C:\Temp\ConsoleApp1.csproj";
            var docData               = IVsPersistDocDataFactory.ImplementAsIVsTextBuffer();
            var textBuffer            = ITextBufferFactory.Create();
            var shellUtilities        = IVsShellUtilitiesHelperFactory.ImplementGetRDTInfo(tempFile, docData);
            var editorAdaptersService = IVsEditorAdaptersFactoryServiceFactory.ImplementGetDocumentBuffer(textBuffer);
            var textDoc               = ITextDocumentFactory.Create();
            var textDocFactoryService = ITextDocumentFactoryServiceFactory.ImplementGetTextDocument(textDoc, true);
            var editorModel           = IProjectFileEditorPresenterFactory.Create();

            var watcher = new TempFileBufferStateListener(editorModel, editorAdaptersService, textDocFactoryService, new IProjectThreadingServiceMock(), shellUtilities,
                                                          IServiceProviderFactory.Create());

            await watcher.InitializeListenerAsync(tempFile);

            Mock.Get(shellUtilities).Verify(u => u.GetRDTDocumentInfoAsync(It.IsAny <IServiceProvider>(), tempFile), Times.Once);
            Mock.Get(editorAdaptersService).Verify(e => e.GetDocumentBuffer((IVsTextBuffer)docData), Times.Once);
            Mock.Get(textDocFactoryService).Verify(t => t.TryGetTextDocument(textBuffer, out textDoc), Times.Once);

            Mock.Get(textDoc).Raise(t => t.FileActionOccurred += null,
                                    new[] { null, new TextDocumentFileActionEventArgs(tempFile, DateTime.Now, FileActionTypes.ContentSavedToDisk) });
            Mock.Get(editorModel).Verify(e => e.SaveProjectFileAsync(), Times.Once);

            await watcher.DisposeAsync();

            Mock.Get(textDoc).Raise(t => t.FileActionOccurred += null,
                                    new[] { null, new TextDocumentFileActionEventArgs(tempFile, DateTime.Now, FileActionTypes.ContentSavedToDisk) });
            Mock.Get(editorModel).Verify(e => e.SaveProjectFileAsync(), Times.Once);
        }
        public async Task NonStandardEncoding_UsesProjectEncoding()
        {
            var projectFilePath = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            // Use some file encoding that's not the default
            var encoding            = Encoding.Default.Equals(Encoding.UTF8) ? Encoding.UTF32 : Encoding.UTF8;
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: encoding);

            var msbuildAccessor = IProjectXmlAccessorFactory.ImplementGetProjectXml("<Project />");

            var fileSystem   = new IFileSystemMock();
            var tempFilePath = @"C:\Temp\asdf.1234";

            fileSystem.SetTempFile(tempFilePath);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  IVsEditorAdaptersFactoryServiceFactory.Create(),
                                                                  ITextDocumentFactoryServiceFactory.Create(),
                                                                  IVsShellUtilitiesHelperFactory.Create(),
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");
            var tempFile        = fileSystem.Files.First(data => StringComparers.Paths.Equals(tempProjectPath, data.Key));

            Assert.Equal(encoding, tempFile.Value.FileEncoding);
        }
        public async Task Initialize_CreatesTempFile()
        {
            var projectFilePath     = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: Encoding.Default);

            var msbuildAccessor = IProjectXmlAccessorFactory.ImplementGetProjectXml("<Project />");

            var fileSystem   = new IFileSystemMock();
            var tempFilePath = @"C:\Temp\asdf.1234";

            fileSystem.SetTempFile(tempFilePath);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  IVsEditorAdaptersFactoryServiceFactory.Create(),
                                                                  ITextDocumentFactoryServiceFactory.Create(),
                                                                  IVsShellUtilitiesHelperFactory.Create(),
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");

            Assert.True(fileSystem.FileExists(tempProjectPath));
            Assert.Equal("<Project />", fileSystem.ReadAllText(tempProjectPath));
        }
 public void NullThreadingService_Throws()
 {
     Assert.Throws <ArgumentNullException>("threadingService", () => new TempFileBufferStateListener(
                                               IProjectFileEditorPresenterFactory.Create(),
                                               IVsEditorAdaptersFactoryServiceFactory.Create(),
                                               ITextDocumentFactoryServiceFactory.Create(),
                                               null,
                                               IVsShellUtilitiesHelperFactory.Create(),
                                               IServiceProviderFactory.Create()));
 }
Example #5
0
 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 void NullFileSystem_Throws()
 {
     Assert.Throws <ArgumentNullException>("fileSystem", () => new TempFileTextBufferManager(
                                               UnconfiguredProjectFactory.Create(),
                                               IProjectXmlAccessorFactory.Create(),
                                               IVsEditorAdaptersFactoryServiceFactory.Create(),
                                               ITextDocumentFactoryServiceFactory.Create(),
                                               IVsShellUtilitiesHelperFactory.Create(),
                                               null,
                                               IProjectThreadingServiceFactory.Create(),
                                               IServiceProviderFactory.Create()));
 }
        public async Task NoChangesFrom_DoesNotOverwriteNewChanges()
        {
            var projectFilePath = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            // Use some file encoding that's not the default
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: Encoding.UTF8);

            var projectXml = "<Project />";
            var bufferXml  = "<Project></Project>";


            var docData      = IVsPersistDocDataFactory.ImplementAsIVsTextBufferIsDocDataDirty(true, VSConstants.S_OK);
            var textBuffer   = ITextBufferFactory.ImplementSnapshot(() => bufferXml);
            var textDocument = ITextDocumentFactory.ImplementTextBuffer(textBuffer);

            var fileSystem      = new IFileSystemMock();
            var tempFilePath    = @"C:\Temp\asdf.1234";
            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");

            fileSystem.SetTempFile(tempFilePath);

            var msbuildAccessor = IProjectXmlAccessorFactory.Implement(() => projectXml, xml => fileSystem.WriteAllText(tempProjectPath, xml));

            var shellUtility         = IVsShellUtilitiesHelperFactory.ImplementGetRDTInfo(Path.Combine(tempFilePath, "ConsoleApp1.csproj"), docData);
            var editorAdapterFactory = IVsEditorAdaptersFactoryServiceFactory.ImplementGetDocumentBuffer(textBuffer);
            var textDocumentService  = ITextDocumentFactoryServiceFactory.ImplementGetTextDocument(textDocument, true);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  editorAdapterFactory,
                                                                  textDocumentService,
                                                                  shellUtility,
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            var tempFile = fileSystem.Files.First(data => StringComparers.Paths.Equals(tempProjectPath, data.Key));

            // First save. File system should be "<Project></Project>", last saved is also "<Project></Project>"
            await textBufferManager.SaveAsync();

            Assert.Equal("<Project></Project>", fileSystem.ReadAllText(tempProjectPath));

            // Now we simulate some changes to the buffer and call Reset. Both the last saved and current project xml should be "<Project></Project>", so
            // the buffer shouldn't be reset
            projectXml = bufferXml;
            bufferXml  = "<Project>asdf</Project>";
            await textBufferManager.ResetBufferAsync();

            Mock.Get(textBuffer).Verify(t => t.Replace(It.IsAny <Span>(), It.IsAny <string>()), Times.Never);
            Assert.Equal("<Project></Project>", fileSystem.ReadAllText(tempProjectPath));
        }
Example #8
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);
        }
Example #9
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);
        }
Example #10
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 ResetBufferDirtyDocData_UpdatesFileNotBuffer()
        {
            var projectFilePath = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            // Use some file encoding that's not the default
            var encoding            = Encoding.Default.Equals(Encoding.UTF8) ? Encoding.UTF32 : Encoding.UTF8;
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: encoding);

            var xml = "<Project />";

            var msbuildAccessor = IProjectXmlAccessorFactory.Implement(() => xml, s => { });

            var docData      = IVsPersistDocDataFactory.ImplementAsIVsTextBufferIsDocDataDirty(true, VSConstants.S_OK);
            var textBuffer   = ITextBufferFactory.ImplementSnapshot("<Project />");
            var textDocument = ITextDocumentFactory.ImplementTextBuffer(textBuffer);

            var fileSystem   = new IFileSystemMock();
            var tempFilePath = @"C:\Temp\asdf.1234";

            fileSystem.SetTempFile(tempFilePath);

            var shellUtility         = IVsShellUtilitiesHelperFactory.ImplementGetRDTInfo(Path.Combine(tempFilePath, "ConsoleApp1.csproj"), docData);
            var editorAdapterFactory = IVsEditorAdaptersFactoryServiceFactory.ImplementGetDocumentBuffer(textBuffer);
            var textDocumentService  = ITextDocumentFactoryServiceFactory.ImplementGetTextDocument(textDocument, true);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  editorAdapterFactory,
                                                                  textDocumentService,
                                                                  shellUtility,
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");
            var tempFile        = fileSystem.Files.First(data => StringComparers.Paths.Equals(tempProjectPath, data.Key));

            Assert.Equal("<Project />", fileSystem.ReadAllText(tempProjectPath));

            xml = "<Project></Project>";
            await textBufferManager.ResetBufferAsync();

            Mock.Get(textBuffer).Verify(t => t.Replace(It.IsAny <Span>(), It.IsAny <string>()), Times.Never);
            Assert.Equal("<Project></Project>", fileSystem.ReadAllText(tempProjectPath));
            Mock.Get(unconfiguredProject).Verify(u => u.SaveAsync(null), Times.Once);
        }
Example #12
0
        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);
        }
Example #13
0
        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());
        }
        private static DotNetCoreProjectCompatibilityDetector CreateCompatibilityDetector(out IDialogServices dialogServices,
                                                                                          string?versionDataString      = null,
                                                                                          Version?vsVersion             = null,
                                                                                          bool isSolutionOpen           = false,
                                                                                          bool hasNewProjects           = false,
                                                                                          bool usingPreviewSDK          = false,
                                                                                          bool isCapabilityMatch        = true,
                                                                                          string targetFrameworkMoniker = ".NETCoreApp,Version=v3.0")
        {
            dialogServices = IDialogServicesFactory.Create();
            var additionalReference = dialogServices;
            var projectProperties   = IProjectPropertiesFactory.CreateWithPropertyAndValue("TargetFrameworkMoniker", targetFrameworkMoniker);
            var propertiesProvider  = IProjectPropertiesProviderFactory.Create(commonProps: projectProperties);
            var project             = ConfiguredProjectFactory.Create(services: ConfiguredProjectServicesFactory.Create(projectPropertiesProvider: propertiesProvider));
            var scope                  = hasNewProjects ? IProjectCapabilitiesScopeFactory.Create(new[] { ProjectCapability.DotNet, ProjectCapability.PackageReferences }) : null;
            var projectAccessor        = new Lazy <IProjectServiceAccessor>(() => IProjectServiceAccessorFactory.Create(scope, project));
            var lazyDialogServices     = new Lazy <IDialogServices>(() => additionalReference);
            var threadHandling         = new Lazy <IProjectThreadingService>(() => IProjectThreadingServiceFactory.Create(verifyOnUIThread: false));
            var vsShellUtilitiesHelper = new Lazy <IVsShellUtilitiesHelper>(() => IVsShellUtilitiesHelperFactory.Create(string.Empty, vsVersion ?? new Version("16.1")));
            var fileSystem             = new Lazy <IFileSystem>(() => IFileSystemFactory.Create(existsFunc: x => true, readAllTextFunc: x => versionDataString ?? defaultVersionDataString));
            var httpClient             = new Lazy <IHttpClient>(() => IHttpClientFactory.Create(versionDataString ?? defaultVersionDataString));
            var vsUIShellService       = IVsServiceFactory.Create <SVsUIShell, IVsUIShell>(Mock.Of <IVsUIShell>());
            var settingsManagerService = IVsServiceFactory.Create <SVsSettingsPersistenceManager, ISettingsManager>(Mock.Of <ISettingsManager>());
            var vsSolutionService      = IVsServiceFactory.Create <SVsSolution, IVsSolution>(IVsSolutionFactory.CreateWithAdviseUnadviseSolutionEvents(1, isFullyLoaded: isSolutionOpen));
            var vsAppIdService         = IVsServiceFactory.Create <SVsAppId, IVsAppId>(Mock.Of <IVsAppId>());
            var vsShellService         = IVsServiceFactory.Create <SVsShell, IVsShell>(Mock.Of <IVsShell>());

            var compatibilityDetector = new TestDotNetCoreProjectCompatibilityDetector(projectAccessor,
                                                                                       lazyDialogServices,
                                                                                       threadHandling,
                                                                                       vsShellUtilitiesHelper,
                                                                                       fileSystem,
                                                                                       httpClient,
                                                                                       vsUIShellService,
                                                                                       settingsManagerService,
                                                                                       vsSolutionService,
                                                                                       vsAppIdService,
                                                                                       vsShellService,
                                                                                       hasNewProjects,
                                                                                       usingPreviewSDK,
                                                                                       isCapabilityMatch);

            return(compatibilityDetector);
        }
        public async Task SetReadonly_SetsOnlyReadonlyFlag()
        {
            var projectFilePath = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            // Use some file encoding that's not the default
            var encoding            = Encoding.Default.Equals(Encoding.UTF8) ? Encoding.UTF32 : Encoding.UTF8;
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: encoding);

            var xml = "<Project />";

            var msbuildAccessor = IProjectXmlAccessorFactory.ImplementGetProjectXml(xml);

            var docData      = IVsPersistDocDataFactory.ImplementAsIVsTextBufferGetStateFlags(~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY);
            var textBuffer   = ITextBufferFactory.ImplementSnapshot("<Project></Project>");
            var textDocument = ITextDocumentFactory.ImplementTextBuffer(textBuffer);

            var fileSystem   = new IFileSystemMock();
            var tempFilePath = @"C:\Temp\asdf.1234";

            fileSystem.SetTempFile(tempFilePath);

            var shellUtility         = IVsShellUtilitiesHelperFactory.ImplementGetRDTInfo(Path.Combine(tempFilePath, "ConsoleApp1.csproj"), docData);
            var editorAdapterFactory = IVsEditorAdaptersFactoryServiceFactory.ImplementGetDocumentBuffer(textBuffer);
            var textDocumentService  = ITextDocumentFactoryServiceFactory.ImplementGetTextDocument(textDocument, true);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  editorAdapterFactory,
                                                                  textDocumentService,
                                                                  shellUtility,
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            await textBufferManager.SetReadOnlyAsync(true);

            Mock.Get(docData).As <IVsTextBuffer>().Verify(t =>
                                                          t.SetStateFlags(~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY));
            await textBufferManager.SetReadOnlyAsync(false);

            Mock.Get(docData).As <IVsTextBuffer>().Verify(t => t.SetStateFlags(~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY));
        }
Example #16
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);
        }
Example #17
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);
        }
Example #18
0
        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);
        }
Example #19
0
        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());
        }