/// <summary> /// Add some basic service mock objects to the service provider. /// </summary> /// <param name="serviceProvider">Instance of ServiceProvider which will be /// configured with the mocks.</param> public static void AddBasicSiteSupport(Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider serviceProvider) { if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } // Add solution Support BaseMock solution = MockServicesProvider.GetSolutionFactoryInstance(); serviceProvider.AddService(typeof(IVsSolution), solution, false); //Add site support for ILocalRegistry BaseMock localRegistry = MockServicesProvider.GetLocalRegistryInstance(); serviceProvider.AddService(typeof(SLocalRegistry), (ILocalRegistry)localRegistry, false); // Add site support for RegisterProjectTypes BaseMock mock = MockServicesProvider.GetRegisterProjectInstance(); serviceProvider.AddService(typeof(SVsRegisterProjectTypes), mock, false); // Add site support for VsShell BaseMock vsShell = MockServicesProvider.GetVsShellInstance0(); serviceProvider.AddService(typeof(SVsShell), vsShell, false); // Add site support for SolutionBuildManager service BaseMock solutionBuildManager = MockServicesProvider.GetSolutionBuildManagerInstance0(); serviceProvider.AddService(typeof(SVsSolutionBuildManager), solutionBuildManager, false); }
public void ConsoleTextOfLineNoMarker() { string testString = "Test"; using (OleServiceProvider provider = new OleServiceProvider()) { BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); textLinesMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"), new EventHandler<CallbackArgs>(GetLineTextCallbackForConsoleTextOfLine)); textLinesMock["LineText"] = testString; textLinesMock["ExpectedLine"] = 1; textLinesMock["ExpectedStart"] = 0; textLinesMock["ExpectedEnd"] = 10; // Create a new local registry class. LocalRegistryMock mockRegistry = new LocalRegistryMock(); // Add the text buffer to the list of the classes that local registry can create. mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Add the local registry to the service provider. provider.AddService(typeof(SLocalRegistry), mockRegistry, false); // Create the console. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { IConsoleText consoleText = windowPane as IConsoleText; Assert.IsNull(consoleText.TextOfLine(1, -1, true)); Assert.IsNull(consoleText.TextOfLine(1, -1, false)); string text = consoleText.TextOfLine(1, 10, false); Assert.IsTrue(testString == text); } } }
public PackageTestEnvironment() { // Create the project project = new ProjectTestClass(new ProjectTestPackage()); // Site the project services = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); LocalRegistryMock localRegistry = new LocalRegistryMock(); localRegistry.RegistryRoot = @"Software\Microsoft\VisualStudio\9.0"; services.AddService(typeof(SLocalRegistry), localRegistry, true); BaseMock mockConfiguration = new GenericMockFactory("MockConfiguration", new[] { typeof(Configuration) }).GetInstance(); mockConfiguration.AddMethodReturnValues(string.Format("{0}.{1}", typeof(Configuration).FullName, "ConfigurationName"), new[] { "Debug" }); mockConfiguration.AddMethodReturnValues(string.Format("{0}.{1}", typeof(Configuration).FullName, "PlatformName"), new[] { "AnyCPU" }); BaseMock mockConfigMgr = ConfigurationManagerFactory.GetInstance(); mockConfigMgr.AddMethodReturnValues(string.Format("{0}.{1}", typeof(ConfigurationManager).FullName, ""), new[] { mockConfiguration }); BaseMock extensibility = ExtensibilityFactory.GetInstance(); extensibility.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsExtensibility3).FullName, "GetConfigMgr"), new object[] { 0, null, null, mockConfigMgr }); services.AddService(typeof(IVsExtensibility), extensibility, false); project.SetSite(services); // Init the msbuild engine Microsoft.Build.Evaluation.ProjectCollection engine = VisualStudio.Project.Utilities.InitializeMsBuildEngine(null, services); Assert.IsNotNull(engine, "MSBuild Engine could not be initialized"); // Retrieve the project file content, load it and save it string fullpath = Path.Combine(new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName, "TestProject.proj"); if (string.IsNullOrEmpty(projectXml)) { projectXml = Properties.Resources.TestProject; using (TextWriter writer = new StreamWriter(fullpath)) { writer.Write(projectXml); } } // Init the msbuild project Microsoft.Build.Evaluation.Project buildProject = VisualStudio.Project.Utilities.InitializeMsBuildProject(engine, fullpath); Assert.IsNotNull(buildProject, "MSBuild project not initialized correctly in InitializeMsBuildProject"); //Verify that we can set the build project on the projectnode project.BuildProject = buildProject; // Now the project is opened, so we can update its internal variable. if (null == projectOpened) { projectOpened = typeof(VisualStudio.Project.ProjectNode).GetField("projectOpened", BindingFlags.Instance | BindingFlags.NonPublic); } projectOpened.SetValue(project, true); }
public void TestInitialize() { // Create a basic service provider with basic site support serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddBasicSiteSupport(serviceProvider); // initialize new NestedProjectPackage and SetSite for it. projectPackage = new NestedProjectPackage(); ((IVsPackage)projectPackage).SetSite(serviceProvider); }
public void InitializeTest() { NestedProjectPackage target = new NestedProjectPackage(); // Create a basic service provider serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddBasicSiteSupport(serviceProvider); // SetSite calls Initialize intrinsically ((IVsPackage)target).SetSite(serviceProvider); }
public static void AddBasicSiteSupport(OleServiceProvider serviceProvider) { if (serviceProvider == null) { throw new ArgumentException("serviceProvider"); } // Add solution Support BaseMock solution = MockServiceProvider.GetUserSettingsFactoryInstance(); serviceProvider.AddService(typeof(IVsUserSettings), solution, false); }
/// <summary> /// Use to create an IOleServiceProvider with the basic services required by /// MS.VS.Shell.Package.SetSite() base implementation /// </summary> /// <returns></returns> public static OleServiceProvider CreateOleServiceProviderWithBasicServices() { // Create the service provider OleServiceProvider serviceProvider = new OleServiceProvider(); // Add IProfferService // Create the type only once, then create as many instances as required. if (profferServiceFactory == null) { profferServiceFactory = new GenericMockFactory("MockProfferService", new Type[] { typeof(IProfferService) }); } BaseMock mockObject = profferServiceFactory.GetInstance(); mockObject.AddMethodCallback(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IProfferService).FullName, "ProfferService"), new EventHandler<CallbackArgs>(ProfferServiceCallback)); serviceProvider.AddService(typeof(SProfferService), mockObject, false); // Add IUIHostLocale if (hostLocaleFactory == null) { hostLocaleFactory = new GenericMockFactory("MockUiHostLocale", new Type[] { typeof(IUIHostLocale), typeof(IUIHostLocale2) }); } mockObject = hostLocaleFactory.GetInstance(); // Set the return value to 0 (S_OK) and the out parameter to 1033 (enu). mockObject.AddMethodReturnValues(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IUIHostLocale).FullName, "GetUILocale"), new object[] { 0, (uint)1033 }); serviceProvider.AddService(typeof(SUIHostLocale), mockObject, false); // Add IVsResourceManager if (resourceManagerFactory == null) { resourceManagerFactory = new GenericMockFactory("MockResourceManager", new Type[] { typeof(IVsResourceManager) }); } mockObject = resourceManagerFactory.GetInstance(); mockObject.AddMethodReturnValues(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IVsResourceManager).FullName, "LoadResourceString"), new object[] { 0, Guid.Empty, 0, null, "Mock Localized String" }); serviceProvider.AddService(typeof(SVsResourceManager), mockObject, false); if(activityLogFactory == null) { activityLogFactory = new GenericMockFactory("Microsoft.VisualStudio.Shell.Interop.SVsActivityLog", new Type[] { typeof(SVsActivityLog), typeof(IVsActivityLog) }); } BaseMock fakeActivityLog = activityLogFactory.GetInstance(); serviceProvider.AddService(typeof(SVsActivityLog), fakeActivityLog, false); return serviceProvider; }
private void AddMockActivityLogService(OleServiceProvider provider) { BaseMock activityLogMock = new GenericMockFactory( "MockVsActivityLog", new[] { typeof(Microsoft.VisualStudio.Shell.Interop.IVsActivityLog) } ).GetInstance(); provider.AddService( typeof(Microsoft.VisualStudio.Shell.Interop.SVsActivityLog), activityLogMock, true ); }
//Add some basic service mock objects to the service provider public static void AddBasicSiteSupport(Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider serviceProvider) { if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } // Add solution Support BaseMock solution = MockServicesProvider.GetSolutionFactoryInstance(); serviceProvider.AddService(typeof(SVsSolution), solution, false); //Add site support for ILocalRegistry BaseMock localRegistry = MockServicesProvider.GetLocalRegistryInstance(); serviceProvider.AddService(typeof(SLocalRegistry), (ILocalRegistry)localRegistry, false); // Add site support for UI Shell BaseMock uiShell = MockServicesProvider.GetUiShellInstance0(); serviceProvider.AddService(typeof(SVsUIShell), uiShell, false); serviceProvider.AddService(typeof(SVsUIShellOpenDocument), (IVsUIShellOpenDocument)uiShell, false); //Add site support for Track Selection BaseMock trackSel = MockServicesProvider.GetTrackSelectionInstance(); serviceProvider.AddService(typeof(STrackSelection), trackSel, false); //Add site support for Running Document Table BaseMock runningDoc = MockServicesProvider.GetRunningDocTableInstance(); serviceProvider.AddService(typeof(SVsRunningDocumentTable), runningDoc, false); //Add site support for Window Frame BaseMock windowFrame = MockServicesProvider.GetWindowFrameInstance(); serviceProvider.AddService(typeof(SVsWindowFrame), windowFrame, false); //Add site support for IVsTextManager BaseMock queryEditQuerySave = MockServicesProvider.GetQueryEditQuerySaveInstance(); serviceProvider.AddService(typeof(SVsQueryEditQuerySave), queryEditQuerySave, false); // Add site support for RegisterProjectTypes BaseMock mock = MockServicesProvider.GetRegisterProjectInstance(); serviceProvider.AddService(typeof(SVsRegisterProjectTypes), mock, false); }
public void Dispose() { IVsHierarchy hierarchy = project as IVsHierarchy; if (null != hierarchy) { hierarchy.Close(); } project = null; if (null != services) { services.Dispose(); services = null; } }
/// <summary> /// Use to create an IOleServiceProvider with the basic services required by /// MS.VS.Shell.Package.SetSite() base implementation /// </summary> /// <returns></returns> public static OleServiceProvider CreateOleServiceProviderWithBasicServices() { // Create the service provider OleServiceProvider serviceProvider = new OleServiceProvider(); // Add IProfferService // Create the type only once, then create as many instances as required. if (profferServiceFactory == null) { profferServiceFactory = new GenericMockFactory("MockProfferService", new Type[] { typeof(IProfferService) }); } BaseMock mockObject = profferServiceFactory.GetInstance(); mockObject.AddMethodCallback(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IProfferService).FullName, "ProfferService"), new EventHandler <CallbackArgs>(ProfferServiceCallback)); serviceProvider.AddService(typeof(SProfferService), mockObject, false); // Add IUIHostLocale if (hostLocaleFactory == null) { hostLocaleFactory = new GenericMockFactory("MockUiHostLocale", new Type[] { typeof(IUIHostLocale), typeof(IUIHostLocale2) }); } mockObject = hostLocaleFactory.GetInstance(); // Set the return value to 0 (S_OK) and the out parameter to 1033 (enu). mockObject.AddMethodReturnValues(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IUIHostLocale).FullName, "GetUILocale"), new object[] { 0, (uint)1033 }); serviceProvider.AddService(typeof(SUIHostLocale), mockObject, false); // Add IVsResourceManager if (resourceManagerFactory == null) { resourceManagerFactory = new GenericMockFactory("MockResourceManager", new Type[] { typeof(IVsResourceManager) }); } mockObject = resourceManagerFactory.GetInstance(); mockObject.AddMethodReturnValues(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(IVsResourceManager).FullName, "LoadResourceString"), new object[] { 0, Guid.Empty, 0, null, "Mock Localized String" }); serviceProvider.AddService(typeof(SVsResourceManager), mockObject, false); return(serviceProvider); }
public void ContextMenu() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); // Add the text buffer to the local registry LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view and add it to the local registry. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create a mock UIShell. BaseMock uiShellMock = MockFactories.UIShellFactory.GetInstance(); provider.AddService(typeof(SVsUIShell), uiShellMock, false); // Create the console. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Now we have to set the frame property on the ToolWindowFrame because // this will cause the execution of OnToolWindowCreated and this will add the // command handlers to the console window. windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance(); CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget); // Verify that the "ShowContextMenu" command handler calls the // ShowContextMenu method of IVsUIShell. uiShellMock.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IVsUIShell).FullName, "ShowContextMenu")); helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU); Assert.IsTrue(1 == uiShellMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsUIShell).FullName, "ShowContextMenu"))); } } }
public virtual void Initialize() { serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); // Solution Support serviceProvider.AddService(typeof(SVsSolution), MockIVsSolution.GetInstance(), false); // Project Types Support serviceProvider.AddService(typeof(SVsRegisterProjectTypes), MockIVsRegisterProjectTypes.GetInstance(), false); // UIShell Support BaseMock uiShell = MockIVsUIShell.GetInstance(); serviceProvider.AddService(typeof(SVsUIShell), (IVsUIShell)uiShell, false); serviceProvider.AddService(typeof(SVsUIShellOpenDocument), (IVsUIShellOpenDocument)uiShell, false); // Shell Support serviceProvider.AddService(typeof(SVsShell), MockIVsShell.GetInstance(), false); // Build Manager support serviceProvider.AddService(typeof(SVsSolutionBuildManager), MockIVsSolutionBuildManager.GetInstance(), false); // ILocalRegistry support serviceProvider.AddService(typeof(SLocalRegistry), (ILocalRegistry)MockILocalRegistry.GetInstance(), false); // IVsTaskList support serviceProvider.AddService(typeof(SVsTaskList), MockIVsTaskList.GetInstance(), false); }
public virtual void Initialize() { serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); // Solution Support serviceProvider.AddService(typeof(SVsSolution), MockIVsSolution.GetInstance(), false); // Project Types Support serviceProvider.AddService(typeof(SVsRegisterProjectTypes), MockIVsRegisterProjectTypes.GetInstance(), false); // UIShell Support BaseMock uiShell = MockIVsUIShell.GetInstance(); serviceProvider.AddService(typeof(SVsUIShell), (IVsUIShell)uiShell, false); serviceProvider.AddService(typeof(SVsUIShellOpenDocument), (IVsUIShellOpenDocument)uiShell, false); // Shell Support serviceProvider.AddService(typeof(SVsShell), MockIVsShell.GetInstance(), false); // Build Manager support serviceProvider.AddService(typeof(SVsSolutionBuildManager), MockIVsSolutionBuildManager.GetInstance(), false); // ILocalRegistry support serviceProvider.AddService(typeof(SLocalRegistry), (ILocalRegistry)MockILocalRegistry.GetInstance(), false); // IVsTaskList support serviceProvider.AddService(typeof(SVsTaskList), MockIVsTaskList.GetInstance(), false); }
public void Initialize() { // Create a basic service provider serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddBasicSiteSupport(serviceProvider); // Initialize ProjectPackage context projectPackage = new NestedProjectPackage(); ((IVsPackage)projectPackage).SetSite(serviceProvider); // prepare the factory projectFactory = new NestedProjectFactoryFake(projectPackage); //set the build engine and build project on the factory object FieldInfo buildEngine = typeof(ProjectFactory).GetField("buildEngine", BindingFlags.Instance | BindingFlags.NonPublic); buildEngine.SetValue(projectFactory, MSBuild.Engine.GlobalEngine); MSBuild.Project msbuildproject = MSBuild.Engine.GlobalEngine.CreateNewProject(); FieldInfo buildProject = typeof(ProjectFactory).GetField("buildProject", BindingFlags.Instance | BindingFlags.NonPublic); buildProject.SetValue(projectFactory, msbuildproject); //Create the project object using the projectfactory and load the project int canCreate; if (VSConstants.S_OK == ((IVsProjectFactory)projectFactory).CanCreateProject(fullPathToProjectFile, 2, out canCreate)) { MethodInfo preCreateForOuter = typeof(NestedProjectFactory).GetMethod("PreCreateForOuter", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(preCreateForOuter, "failed to get the PreCreateForOuter method info object from NestedProjectFactory type"); projectNode = (NesteProjectNodeFake)preCreateForOuter.Invoke(projectFactory, new object[] { IntPtr.Zero }); Assert.IsNotNull(projectNode, "Failed to create the projectnode object"); Guid iidProject = new Guid(); int pfCanceled; projectNode.Load(fullPathToProjectFile, "", "", 2, ref iidProject, out pfCanceled); //init the automation objects nestedProject = new OANestedProject(projectNode); projectProperties = (OANestedProjectProperties)nestedProject.Properties; } }
public void WindowPaneImplementation() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the tool window. using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { IVsWindowPane windowPane = disposableObject as IVsWindowPane; Assert.IsNotNull(windowPane); // Now call the IVsWindowPane's methods and check that they are redirect to // the implementation provided by the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "CreatePaneWindow"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.GetDefaultSize(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "GetDefaultSize"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.LoadViewState(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "LoadViewState"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.SaveViewState(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "SaveViewState"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.SetSite(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "SetSite"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.TranslateAccelerator(null))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "TranslateAccelerator"))); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.ClosePane())); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsWindowPane).FullName, "ClosePane"))); } // Verify that the text view is closed after Dispose is called on the window pane. Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextView).FullName, "CloseView"))); } }
public void VerifyCommandFilter() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); textViewMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "AddCommandFilter"), new EventHandler<CallbackArgs>(AddCommandFilterCallback)); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Create a command target that handles some random command OleMenuCommandService commandService = new OleMenuCommandService(provider); Guid newCommandGroup = Guid.NewGuid(); uint newCommandId = 42; CommandID id = new CommandID(newCommandGroup, (int)newCommandId); OleMenuCommand cmd = new OleMenuCommand(null, id); commandService.AddCommand(cmd); textViewMock["OriginalFilter"] = (IOleCommandTarget)commandService; // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the window. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { Assert.IsNotNull(windowPane); // Verify that the command specific to the text view are not handled yet. CommandTargetHelper commandHelper = new CommandTargetHelper((IOleCommandTarget)windowPane); uint flags; Assert.IsFalse(commandHelper.IsCommandSupported( typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN, out flags)); Assert.IsFalse(commandHelper.IsCommandSupported( typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP, out flags)); Assert.IsFalse(commandHelper.IsCommandSupported( typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN, out flags)); Assert.IsFalse(commandHelper.IsCommandSupported( typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU, out flags)); // Verify that also the command that we have defined here is not supported. Assert.IsFalse(commandHelper.IsCommandSupported(newCommandGroup, newCommandId, out flags)); // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Now we have to set the frame property on the ToolWindowFrame because // this will cause the execution of OnToolWindowCreated. windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance(); // Now the command filter should be set for the text view Assert.IsNotNull(textViewMock["CommandFilter"]); // The command target for the window pane should also be able to support // the text view specific command that we have installed. // Verify only two commands that are always supported Assert.IsTrue(commandHelper.IsCommandSupported( typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN, out flags)); Assert.IsTrue(commandHelper.IsCommandSupported( typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL, out flags)); Assert.IsTrue(commandHelper.IsCommandSupported( typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (int)(int)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.SHOWCONTEXTMENU, out flags)); // Verify that also the commands supported by the original command target are // supported by the new one. Assert.IsTrue(commandHelper.IsCommandSupported(newCommandGroup, newCommandId, out flags)); } } }
public void WindowConstructor() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock object for the text buffer. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); // Create a new local registry class. LocalRegistryMock mockRegistry = new LocalRegistryMock(); // Add the text buffer to the list of the classes that local registry can create. mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); provider.AddService(typeof(SLocalRegistry), mockRegistry, false); // Now create the object and verify that the constructor sets the site for the text buffer. using (IDisposable consoleObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { Assert.IsNotNull(consoleObject); Assert.IsTrue(0 < textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite"))); } } }
public void ReadOnlyRegionAfterWrite() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); // Add the buffer to the local registry. LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the console window. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { // Get the stream from the window pane. System.IO.Stream consoleStream = CommandWindowHelper.ConsoleStream(windowPane); Assert.IsNotNull(consoleStream); // Set a return value for GetLastLineIndex textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"), new object[] { 0, 12, 35 }); // Write some text on the stream. System.IO.StreamWriter writer = new System.IO.StreamWriter(consoleStream); writer.Write(""); writer.Flush(); // Verify that the ResetSpan method for the text marker was called and that // the span is set to cover all the current buffer. BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"]; Assert.IsTrue(1 == markerMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLineMarker).FullName, "ResetSpan"))); TextSpan span = (TextSpan)markerMock["Span"]; Assert.IsTrue(0 == span.iStartLine); Assert.IsTrue(0 == span.iStartIndex); Assert.IsTrue(12 == span.iEndLine); Assert.IsTrue(35 == span.iEndIndex); // Change the end point of the buffer and try again. textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"), new object[] { 0, 15, 3 }); writer.Write("abc"); writer.Flush(); Assert.IsTrue(2 == markerMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLineMarker).FullName, "ResetSpan"))); span = (TextSpan)markerMock["Span"]; Assert.IsTrue(0 == span.iStartLine); Assert.IsTrue(0 == span.iStartIndex); Assert.IsTrue(15 == span.iEndLine); Assert.IsTrue(3 == span.iEndIndex); } } }
public void TextViewCreation() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); textViewMock.AddMethodCallback(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite"), new EventHandler<CallbackArgs>(TextViewSetSiteCallback)); textViewMock.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsTextView).FullName, "Initialize"), new EventHandler<CallbackArgs>(TextViewInitializeCallback)); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the tool window. using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { IVsWindowPane windowPane = disposableObject as IVsWindowPane; Assert.IsNotNull(windowPane); // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( windowPane.CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Verify that the text view was used as expected. Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite), "SetSite"))); Assert.IsTrue(1 == textViewMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextView), "Initialize"))); } } }
public void VerifyHistoryOneElement() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); // Add the text buffer to the local registry LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view and add it to the local registry. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the console. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Now we have to set the frame property on the ToolWindowFrame because // this will cause the execution of OnToolWindowCreated and this will add the // commands handling functions. windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance(); // Make sure that the text marker is created. CommandWindowHelper.EnsureConsoleTextMarker(windowPane); // Create the command target helper. CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget); // Add an element to the history executing a command. textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"), new object[] { 0, 3 }); textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 2, 4 }); textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLengthOfLine"), new object[] { 0, 3, 10 }); BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"]; TextSpan span = new TextSpan(); span.iEndLine = 2; span.iEndIndex = 4; markerMock["Span"] = span; textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"), new object[] { 0, 2, 4, 2, 10, "Line 1" }); // Execute the OnReturn handler. helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN); // Now there should be one element in the history buffer. // Verify that DOWN key does nothing. markerMock.ResetAllFunctionCalls(); textLinesMock.ResetAllFunctionCalls(); helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.DOWN); Assert.IsTrue(0 == markerMock.TotalCallsAllFunctions()); Assert.IsTrue(0 == textLinesMock.TotalCallsAllFunctions()); // The UP key should force the "Line 1" text in the last line of the text buffer. textLinesMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"), new EventHandler<CallbackArgs>(ReplaceLinesCallback)); textLinesMock["Text"] = ""; helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.UP); Assert.IsTrue(1 == textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"))); Assert.IsTrue("Line 1" == (string)textLinesMock["Text"]); } } }
public void OptionsPagePackageTestInitialize() { testPackage = new OptionsPagePackageCS(); // Create a basic service provider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddBasicSiteSupport(serviceProvider); ((IVsPackage)testPackage).SetSite(serviceProvider); }
public void Dispose() { IVsHierarchy hierarchy = project as IVsHierarchy; if(null != hierarchy) { hierarchy.Close(); } project = null; if(null != services) { services.Dispose(); services = null; } }
public void Initialize() { // Create a basic service provider serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddBasicSiteSupport(serviceProvider); // Initialize ProjectPackage context projectPackage = new NestedProjectPackage(); ((IVsPackage)projectPackage).SetSite(serviceProvider); // prepare the factory projectFactory = new NestedProjectFactoryFake(projectPackage); //set the build engine and build project on the factory object FieldInfo buildEngine = typeof(ProjectFactory).GetField("buildEngine", BindingFlags.Instance | BindingFlags.NonPublic); buildEngine.SetValue(projectFactory, MSBuild.Engine.GlobalEngine); MSBuild.Project msbuildproject = MSBuild.Engine.GlobalEngine.CreateNewProject(); FieldInfo buildProject = typeof(ProjectFactory).GetField("buildProject", BindingFlags.Instance | BindingFlags.NonPublic); buildProject.SetValue(projectFactory, msbuildproject); //Create the project object using the projectfactory and load the project int canCreate; if(VSConstants.S_OK == ((IVsProjectFactory)projectFactory).CanCreateProject(fullPathToProjectFile, 2, out canCreate)) { MethodInfo preCreateForOuter = typeof(NestedProjectFactory).GetMethod("PreCreateForOuter", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(preCreateForOuter, "failed to get the PreCreateForOuter method info object from NestedProjectFactory type"); projectNode = (NesteProjectNodeFake)preCreateForOuter.Invoke(projectFactory, new object[] { IntPtr.Zero }); Assert.IsNotNull(projectNode, "Failed to create the projectnode object"); Guid iidProject = new Guid(); int pfCanceled; projectNode.Load(fullPathToProjectFile, "", "", 2, ref iidProject, out pfCanceled); //init the automation objects nestedProject = new OANestedProject(projectNode); projectProperties = (OANestedProjectProperties)nestedProject.Properties; } }
public void VerifyOnBeforeMoveLeft() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); // Add the text buffer to the local registry LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view and add it to the local registry. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the console. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Now we have to set the frame property on the ToolWindowFrame because // this will cause the execution of OnToolWindowCreated and this will add the // command handling for the return key. windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance(); // Make sure that the text marker is created. CommandWindowHelper.EnsureConsoleTextMarker(windowPane); BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"]; // Create a OleMenuCommand to use to call OnBeforeHistory. OleMenuCommand cmd = new OleMenuCommand(new EventHandler(EmptyMenuCallback), new CommandID(Guid.Empty, 0)); // Simulate the fact that the cursor is on the last line of the buffer and after the // end of the prompt. cmd.Supported = true; textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"), new object[] { 0, 5 }); textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 4, 7 }); TextSpan span = new TextSpan(); span.iEndIndex = 4; span.iEndLine = 3; markerMock["Span"] = span; CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd); Assert.IsFalse(cmd.Supported); // Simulate the cursor over the prompt. textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 4, 3 }); CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd); Assert.IsTrue(cmd.Supported); // Simulate the cursor right after the prompt. cmd.Supported = false; textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 4, 4 }); CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd); Assert.IsTrue(cmd.Supported); // Simulate the cursor on a line before the last. cmd.Supported = true; textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 3, 7 }); CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd); Assert.IsFalse(cmd.Supported); // Simulate the cursor on a line before the last but over the prompt. cmd.Supported = true; textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 3, 2 }); CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd); Assert.IsFalse(cmd.Supported); // Simulate the cursor on a line after the last. cmd.Supported = true; textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 5, 7 }); CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd); Assert.IsFalse(cmd.Supported); // Simulate the cursor on a line after the last, but over the prompt. cmd.Supported = true; textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 5, 0 }); CommandWindowHelper.ExecuteOnBeforeMoveLeft(windowPane, cmd); Assert.IsFalse(cmd.Supported); } } }
public void InitializeTest() { NestedProjectPackage target = new NestedProjectPackage(); // Create a basic service provider serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddBasicSiteSupport(serviceProvider); // SetSite calls Initialize intrinsically ((IVsPackage)target).SetSite(serviceProvider); }
public void VerifyOnHome() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); // Add the text buffer to the local registry LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view and add it to the local registry. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); textViewMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "SetCaretPos"), new EventHandler<CallbackArgs>(SetCaretPosCallback)); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create the console. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { // Make sure that the text marker is created. CommandWindowHelper.EnsureConsoleTextMarker(windowPane); // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Now we have to set the frame property on the ToolWindowFrame because // this will cause the execution of OnToolWindowCreated and this will add the // command handling for the return key. windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance(); CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget); // Simulate the fact that the cursor is on the last line of the buffer. textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"), new object[] { 0, 6 }); // Simulate a cursor 3 chars long. BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"]; TextSpan span = new TextSpan(); span.iEndLine = 5; span.iEndIndex = 3; markerMock["Span"] = span; textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 5, 7 }); helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL); Assert.IsTrue(5 == (int)textViewMock["CaretLine"]); Assert.IsTrue(3 == (int)textViewMock["CaretColumn"]); // Simulate the fact that the cursor is before last line of the buffer. textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"), new object[] { 0, 6 }); textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 3, 7 }); helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL); Assert.IsTrue(3 == (int)textViewMock["CaretLine"]); Assert.IsTrue(0 == (int)textViewMock["CaretColumn"]); // Simulate the fact that the cursor is after last line of the buffer. textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"), new object[] { 0, 6 }); textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 8, 7 }); helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.BOL); Assert.IsTrue(8 == (int)textViewMock["CaretLine"]); Assert.IsTrue(0 == (int)textViewMock["CaretColumn"]); } } }
public void VerifyOnReturn() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"), new object[] { 0, 0, 4 }); textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLengthOfLine"), new object[] { 0, 0, 13 }); textLinesMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"), new EventHandler<CallbackArgs>(GetLineTextCallback)); textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineCount"), new object[] { 0, 13 }); LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create a mock engine provider. BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance(); // Create a mock engine. BaseMock mockEngine = MockFactories.CreateStandardEngine(); // Set this engine as the one returned from the GetSharedEngine of the engine provider. mockEngineProvider.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"), new object[] { (IEngine)mockEngine }); // Set the callback function for the ExecuteToConsole method of the engine. mockEngine.AddMethodCallback( string.Format("{0}.{1}", typeof(IEngine).FullName, "ExecuteToConsole"), new EventHandler<CallbackArgs>(ExecuteToConsoleCallback)); // Add the engine provider to the list of the services. provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false); // Create the console window. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { // Call the CreatePaneWindow method that will force the creation of the text view. IntPtr newHwnd; Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded( ((IVsWindowPane)windowPane).CreatePaneWindow(IntPtr.Zero, 0, 0, 0, 0, out newHwnd))); // Make sure that the text marker is created. CommandWindowHelper.EnsureConsoleTextMarker(windowPane); // Now we have to set the frame property on the ToolWindowFrame because // this will cause the execution of OnToolWindowCreated and this will add the // command handling for the return key. windowPane.Frame = (IVsWindowFrame)MockFactories.WindowFrameFactory.GetInstance(); // Simulate the cursor on a line different from the last one. textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 5, 8 }); // Execute the command handler for the RETURN key. CommandTargetHelper helper = new CommandTargetHelper(windowPane as IOleCommandTarget); helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN); // In this case nothing should happen because we are not on the input line. Assert.IsTrue(0 == CommandWindowHelper.LinesInInputBuffer(windowPane)); // Now simulate the cursor on the input line. textViewMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextView).FullName, "GetCaretPos"), new object[] { 0, 12, 1 }); // Make sure that the mock engine can execute the command. mockEngine.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput"), new object[] { true }); // Execute the command. helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN); // The input buffer should not contain any text because the engine should have // executed it. Assert.AreEqual<int>(0, CommandWindowHelper.LinesInInputBuffer(windowPane)); Assert.AreEqual<string>("Test Line", (string)mockEngine["ExecutedCommand"]); // Now change the length of the line so that it is shorter than the // console's prompt. textLinesMock.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLengthOfLine"), new object[] { 0, 0, 3 }); // Reset the count of the calls to GetLineText so that we can verify // if it is called. textLinesMock.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText")); // Do the same for the ParseInteractiveInput method of the engine. mockEngine.ResetFunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput")); // Simulate a partial statment. mockEngine.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput"), new object[] { false }); // Execute again the command handler. helper.ExecCommand(typeof(Microsoft.VisualStudio.VSConstants.VSStd2KCmdID).GUID, (uint)Microsoft.VisualStudio.VSConstants.VSStd2KCmdID.RETURN); // Verify that GetLineText was not called. Assert.IsTrue(0 == textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"))); // Verify that the engine was not called to run an interactive command. Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine).FullName, "ParseInteractiveInput"))); // Verify that the console's buffer contains an empty string. Assert.IsTrue(0 == CommandWindowHelper.LinesInInputBuffer(windowPane)); } } }
/// <summary> /// Clean up any resources being used. /// </summary> protected virtual void Dispose(bool disposing) { if (disposing) { if (testPackage != null) { testPackage = null; } if( serviceProvider != null ) { serviceProvider = null; } GC.SuppressFinalize(this); } }
public void Setup() { _package = new TortoiseGitToolbarPackage(); _serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); }
public void EngineInitialization() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create a mock engine provider. BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance(); // Create a mock engine. BaseMock mockEngine = MockFactories.CreateStandardEngine(); // Set this engine as the one returned from the GetSharedEngine of the engine provider. mockEngineProvider.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"), new object[] { (IEngine)mockEngine }); // Add the engine provider to the list of the services. provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false); // Create the console window using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { IVsWindowPane windowPane = disposableObject as IVsWindowPane; Assert.IsNotNull(windowPane); // Verify that the shared engine was get. Assert.IsTrue(1 == mockEngineProvider.FunctionCalls(string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"))); Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine), "set_StdErr"))); Assert.IsTrue(1 == mockEngine.FunctionCalls(string.Format("{0}.{1}", typeof(IEngine), "set_StdOut"))); } } }
static ServiceProviderHelper() { serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddService(typeof(Ankh.UI.IAnkhPackage), AnkhSvn_UnitTestProject.Mocks.PackageMock.EmptyContext(serviceProvider)); }
public void EngineStreams() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); textLinesMock["Text"] = ""; LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); // Create a mock engine provider. BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance(); // Create a mock engine. BaseMock mockEngine = MockFactories.CreateStandardEngine(); // Add the callbacks for the setter methods of stderr and stdout mockEngine.AddMethodCallback( string.Format("{0}.{1}", typeof(IEngine).FullName, "set_StdErr"), new EventHandler<CallbackArgs>(SetEngineStdErr)); mockEngine.AddMethodCallback( string.Format("{0}.{1}", typeof(IEngine).FullName, "set_StdOut"), new EventHandler<CallbackArgs>(SetEngineStdOut)); // Set this engine as the one returned from the GetSharedEngine of the engine provider. mockEngineProvider.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IPythonEngineProvider), "GetSharedEngine"), new object[] { (IEngine)mockEngine }); // Add the engine provider to the list of the services. provider.AddService(typeof(IPythonEngineProvider), mockEngineProvider, false); // Create the console window. using (IDisposable disposableObject = CommandWindowHelper.CreateConsoleWindow(provider) as IDisposable) { IVsWindowPane windowPane = disposableObject as IVsWindowPane; Assert.IsNotNull(windowPane); Assert.IsNotNull(mockEngine["StdErr"]); Assert.IsNotNull(mockEngine["StdOut"]); // Set the callback for the text buffer. textLinesMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"), new EventHandler<CallbackArgs>(ReplaceLinesCallback)); // Verify that the standard error stream is associated with the text buffer. System.IO.Stream stream = (System.IO.Stream)mockEngine["StdErr"]; using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream)) { writer.Write("Test String"); writer.Flush(); Assert.IsTrue((string)textLinesMock["Text"] == "Test String"); textLinesMock["Text"] = ""; } // Verify the standard output. stream = (System.IO.Stream)mockEngine["StdOut"]; using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream)) { writer.Write("Test String"); writer.Flush(); Assert.IsTrue((string)textLinesMock["Text"] == "Test String"); textLinesMock["Text"] = ""; } } } }
public SiteWrappedServiceProvider(OleServiceProvider sp) { _sp = sp; }
public void OptionsPagePackageTestCleanup() { testPackage = null; serviceProvider = null; Dispose(); }
public PackageTestEnvironment() { // Create the project project = new ProjectTestClass(new ProjectTestPackage()); // Site the project services = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); LocalRegistryMock localRegistry = new LocalRegistryMock(); localRegistry.RegistryRoot = @"Software\Microsoft\VisualStudio\9.0"; services.AddService(typeof(SLocalRegistry), localRegistry, true); BaseMock mockConfiguration = new GenericMockFactory("MockConfiguration", new[] { typeof(Configuration) }).GetInstance(); mockConfiguration.AddMethodReturnValues(string.Format("{0}.{1}", typeof(Configuration).FullName, "ConfigurationName"), new[] { "Debug" }); mockConfiguration.AddMethodReturnValues(string.Format("{0}.{1}", typeof(Configuration).FullName, "PlatformName"), new[] { "AnyCPU" }); BaseMock mockConfigMgr = ConfigurationManagerFactory.GetInstance(); mockConfigMgr.AddMethodReturnValues(string.Format("{0}.{1}", typeof(ConfigurationManager).FullName, ""), new[] { mockConfiguration }); BaseMock extensibility = ExtensibilityFactory.GetInstance(); extensibility.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsExtensibility3).FullName, "GetConfigMgr"), new object[] { 0, null, null, mockConfigMgr }); services.AddService(typeof(IVsExtensibility), extensibility, false); project.SetSite(services); // Init the msbuild engine Microsoft.Build.Evaluation.ProjectCollection engine = VisualStudio.Project.Utilities.InitializeMsBuildEngine(null, services); Assert.IsNotNull(engine, "MSBuild Engine could not be initialized"); // Retrieve the project file content, load it and save it string fullpath = Path.Combine(new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName, "TestProject.proj"); if(string.IsNullOrEmpty(projectXml)) { projectXml = Properties.Resources.TestProject; using(TextWriter writer = new StreamWriter(fullpath)) { writer.Write(projectXml); } } // Init the msbuild project Microsoft.Build.Evaluation.Project buildProject = VisualStudio.Project.Utilities.InitializeMsBuildProject(engine, fullpath); Assert.IsNotNull(buildProject, "MSBuild project not initialized correctly in InitializeMsBuildProject"); //Verify that we can set the build project on the projectnode project.BuildProject = buildProject; // Now the project is opened, so we can update its internal variable. if(null == projectOpened) { projectOpened = typeof(VisualStudio.Project.ProjectNode).GetField("projectOpened", BindingFlags.Instance | BindingFlags.NonPublic); } projectOpened.SetValue(project, true); }
public void ConsoleTextOfLineWithMarker() { string testString1 = "Test 1"; string testString2 = "Test 2"; using (OleServiceProvider provider = new OleServiceProvider()) { BaseMock textLinesMock = MockFactories.CreateBufferWithMarker(); textLinesMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLineText"), new EventHandler<CallbackArgs>(GetLineTextCallbackForConsoleTextOfLine)); // Create a new local registry class. LocalRegistryMock mockRegistry = new LocalRegistryMock(); // Add the text buffer to the list of the classes that local registry can create. mockRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Add the local registry to the service provider. provider.AddService(typeof(SLocalRegistry), mockRegistry, false); // Create the console. using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { // Make sure that the text marker is created. CommandWindowHelper.EnsureConsoleTextMarker(windowPane); // Set the span of the marker. TextSpan span = new TextSpan(); span.iStartLine = 0; span.iStartIndex = 0; span.iEndLine = 3; span.iEndIndex = 5; BaseMock markerMock = (BaseMock)textLinesMock["LineMarker"]; markerMock["Span"] = span; IConsoleText consoleText = windowPane as IConsoleText; // Verify the case that the requested line is all inside the // read only region. textLinesMock["LineText"] = testString1; textLinesMock["ExpectedLine"] = 1; textLinesMock["ExpectedStart"] = 0; textLinesMock["ExpectedEnd"] = 10; Assert.IsNull(consoleText.TextOfLine(1, 10, true)); string text = consoleText.TextOfLine(1, 10, false); Assert.IsTrue(text == testString1); // Now ask for some text inside the read-only region, but on its last line. textLinesMock["LineText"] = testString2; textLinesMock["ExpectedLine"] = 3; textLinesMock["ExpectedStart"] = 0; textLinesMock["ExpectedEnd"] = 4; Assert.IsNull(consoleText.TextOfLine(3, 4, true)); text = consoleText.TextOfLine(3, 4, false); Assert.IsTrue(text == testString2); // Now the text is part inside and part outside the read-only region. textLinesMock["LineText"] = testString1; textLinesMock["ExpectedLine"] = 3; textLinesMock["ExpectedStart"] = 5; textLinesMock["ExpectedEnd"] = 10; text = consoleText.TextOfLine(3, 10, true); Assert.IsTrue(testString1 == text); textLinesMock["LineText"] = testString2; textLinesMock["ExpectedLine"] = 3; textLinesMock["ExpectedStart"] = 0; textLinesMock["ExpectedEnd"] = 10; text = consoleText.TextOfLine(3, 10, false); Assert.IsTrue(text == testString2); // Now the line has no intersection with the read-only region. textLinesMock["LineText"] = testString1; textLinesMock["ExpectedLine"] = 4; textLinesMock["ExpectedStart"] = 0; textLinesMock["ExpectedEnd"] = 10; text = consoleText.TextOfLine(4, 10, true); Assert.IsTrue(testString1 == text); textLinesMock["LineText"] = testString2; textLinesMock["ExpectedLine"] = 4; textLinesMock["ExpectedStart"] = 0; textLinesMock["ExpectedEnd"] = 10; text = consoleText.TextOfLine(4, 10, false); Assert.IsTrue(text == testString2); } } }
public void SharedEngineVariables() { using (OleServiceProvider provider = new OleServiceProvider()) { // Create an engine provider with an empty service provider. IPythonEngineProvider engineProvider = CreateEngineProvider(provider); // Get the shared engine. IEngine engine = engineProvider.GetSharedEngine(); // The "engine" variable should always be defined. object engObj = engine.Evaluate("engine"); Assert.IsNotNull(engObj); // The "dte" variable should be set to null. object dte = engine.Evaluate("dte"); Assert.IsNull(dte); // Now add the EnvDTE.DTE service to the service provider. BaseMock dteMock = MockFactories.DTEFactory.GetInstance(); provider.AddService(typeof(EnvDTE.DTE), dteMock, false); // Create a new engine provider. engineProvider = CreateEngineProvider(provider); // Get the shared engine. engine = engineProvider.GetSharedEngine(); // The "engine" variable should always be defined. engObj = engine.Evaluate("engine"); Assert.IsNotNull(engObj); // Now the "dte" variable should be set. dte = engine.Evaluate("dte"); Assert.IsNotNull(dte); } }
public void TestInitialize() { // Create a basic service provider with basic site support serviceProvider = Microsoft.VsSDK.UnitTestLibrary.OleServiceProvider.CreateOleServiceProviderWithBasicServices(); AddBasicSiteSupport(serviceProvider); // initialize new NestedProjectPackage and SetSite for it. projectPackage = new NestedProjectPackage(); ((IVsPackage)projectPackage).SetSite(serviceProvider); }