public void TestOutput() { callbackExecuted = false; // As first create a service provider. using (OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) { // Create a mock object for the output window pane. GenericMockFactory mockWindowPaneFactory = new GenericMockFactory("MockOutputWindowPane", new Type[] { typeof(IVsOutputWindowPane) }); BaseMock mockWindowPane = mockWindowPaneFactory.GetInstance(); mockWindowPane.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsOutputWindowPane).FullName, "OutputString"), new EventHandler <CallbackArgs>(OutputWindowPaneCallback)); // Now create the mock object for the output window. if (null == mockOutputWindowFactory) { mockOutputWindowFactory = new GenericMockFactory("MockOutputWindow1", new Type[] { typeof(IVsOutputWindow) }); } BaseMock mockOutputWindow = mockOutputWindowFactory.GetInstance(); mockOutputWindow.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IVsOutputWindow).FullName, "GetPane"), new object[] { 0, Guid.Empty, (IVsOutputWindowPane)mockWindowPane }); // Add the output window to the services provided by the service provider. serviceProvider.AddService(typeof(SVsOutputWindow), mockOutputWindow, false); // Create an instance of the package and initialize it calling SetSite. ServicesPackage package = new ServicesPackage(); int result = ((IVsPackage)package).SetSite(serviceProvider); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(result), "SetSite failed."); // Now we can create an instance of the service MyGlobalService service = new MyGlobalService(package); service.GlobalServiceFunction(); Assert.IsTrue(callbackExecuted, "OutputText not called."); ((IVsPackage)package).SetSite(null); ((IVsPackage)package).Close(); } }
public void TestFlushFromUTF8() { BaseMock mockBuffer = MockFactories.CreateBufferWithMarker(); mockBuffer.AddMethodReturnValues(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "GetLastLineIndex"), new object[] { 0, 11, 42 }); mockBuffer["Text"] = ""; mockBuffer.AddMethodCallback(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "ReplaceLines"), new EventHandler <CallbackArgs>(ReplaceLinesCallback)); using (Stream stream = CreateTextBufferStream((IVsTextLines)mockBuffer)) { string test = "� Test �"; using (StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.UTF8)) { writer.Write(test); writer.Flush(); Assert.IsTrue((string)mockBuffer["Text"] == test); } } int lockCount = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "LockBuffer")); int unlockCount = mockBuffer.FunctionCalls(string.Format("{0}.{1}", typeof(IVsTextLines).FullName, "UnlockBuffer")); Assert.IsTrue(lockCount == unlockCount); }
public PackageTestEnvironment() { // Create the project project = new ProjectTestClass(); // 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 mockConfigMgr = ConfigurationManagerFactory.GetInstance(); 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 if (null == initEngine) { initEngine = typeof(VisualStudio.Project.Utilities).GetMethod("InitializeMsBuildEngine", BindingFlags.NonPublic | BindingFlags.Static); } Microsoft.Build.Evaluation.ProjectCollection engine = initEngine.Invoke(null, new object[2] { null, services }) as Microsoft.Build.Evaluation.ProjectCollection; 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 if (null == initProject) { initProject = typeof(VisualStudio.Project.Utilities).GetMethod("InitializeMsBuildProject", BindingFlags.NonPublic | BindingFlags.Static); } Microsoft.Build.Evaluation.Project buildProject = initProject.Invoke(null, new object[2] { engine, fullpath }) as Microsoft.Build.Evaluation.Project; Assert.IsNotNull(buildProject, "MSBuild project not initialized correctly in InitializeMsBuildProject"); //Verify that we can set the build project on the projectnode PropertyInfo buildProjectInfo = typeof(VisualStudio.Project.ProjectNode).GetProperty("BuildProject", BindingFlags.Instance | BindingFlags.NonPublic); buildProjectInfo.SetValue(project, buildProject, new object[0]); // 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 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 void CommandBufferOutput() { BaseMock mockEngine = CreateDefaultEngine(); // Make sure that the execute is called. mockEngine.AddMethodReturnValues(parseFunctionName, new object[] { true }); string expected = standardPrompt + System.Environment.NewLine + standardPrompt; // Now set a not empty value for the out and err streams. byte[] streamBuffer = new byte[256]; long usedBytes = 0; using (System.IO.MemoryStream stream = new System.IO.MemoryStream(streamBuffer, true)) { // Set this stream as standard output for the mock engine. mockEngine.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IEngine).FullName, "get_StdOut"), new object[] { (System.IO.Stream)stream }); // Create the command buffer. CommandBuffer buffer = new CommandBuffer(mockEngine as IEngine); buffer.Add("Test"); usedBytes = stream.Position; } // Verify the content of the standard output. using (System.IO.MemoryStream stream = new System.IO.MemoryStream(streamBuffer, 0, (int)usedBytes)) { using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)) { string streamText = reader.ReadToEnd(); Assert.AreEqual <string>(streamText, expected); } } // Redo the same with a multi-line command. usedBytes = 0; expected = standardPrompt + System.Environment.NewLine + multiLinePrompt + System.Environment.NewLine + standardPrompt; using (System.IO.MemoryStream stream = new System.IO.MemoryStream(streamBuffer, true)) { // Set this stream as standard output for the mock engine. mockEngine.AddMethodReturnValues( string.Format("{0}.{1}", typeof(IEngine).FullName, "get_StdOut"), new object[] { (System.IO.Stream)stream }); // Create the command buffer. CommandBuffer buffer = new CommandBuffer(mockEngine as IEngine); // Force a multi-line execution. mockEngine.AddMethodReturnValues(parseFunctionName, new object[] { false }); buffer.Add("Line 1"); // Now execute the command. mockEngine.AddMethodReturnValues(parseFunctionName, new object[] { true }); buffer.Add("Line 2"); usedBytes = stream.Position; } // Verify the content of the standard output. using (System.IO.MemoryStream stream = new System.IO.MemoryStream(streamBuffer, 0, (int)usedBytes)) { using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)) { string streamText = reader.ReadToEnd(); Assert.AreEqual <string>(streamText, expected); } } }