public void ValidateToolWindowShown() { IVsPackage package = new TestToolWindowSearchPackage() as IVsPackage; // Create a basic service provider OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); //Add uishell service that knows how to create a toolwindow BaseMock uiShellService = UIShellServiceMock.GetUiShellInstanceCreateToolWin(); serviceProvider.AddService(typeof(SVsUIShell), uiShellService, false); // Site the package Assert.AreEqual(0, package.SetSite(serviceProvider), "SetSite did not return S_OK"); MethodInfo method = typeof(TestToolWindowSearchPackage).GetMethod("ShowToolWindow", BindingFlags.NonPublic | BindingFlags.Instance); object result = method.Invoke(package, new object[] { null, null }); }
public void CheckLogicalView() { VisualStudioHaskellPackage package = new VisualStudioHaskellPackage(); //Create the editor factory EditorFactory editorFactory = new EditorFactory(package); // Create a basic service provider OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); serviceProvider.AddService(typeof(SLocalRegistry), (ILocalRegistry)LocalRegistryServiceMock.GetILocalRegistryInstance(), true); // Site the editor factory Assert.AreEqual(0, editorFactory.SetSite(serviceProvider), "SetSite did not return S_OK"); IntPtr ppunkDocView; IntPtr ppunkDocData; string pbstrEditorCaption = String.Empty; Guid pguidCmdUI = Guid.Empty; int pgrfCDW = 0; Assert.AreEqual(VSConstants.S_OK, editorFactory.CreateEditorInstance(VSConstants.CEF_OPENFILE, null, null, null, 0, IntPtr.Zero, out ppunkDocView, out ppunkDocData, out pbstrEditorCaption, out pguidCmdUI, out pgrfCDW)); //check for successfull creation of editor instance string bstrPhysicalView = string.Empty; Guid refGuidLogicalView = VSConstants.LOGVIEWID_Debugging; Assert.AreEqual(VSConstants.E_NOTIMPL, editorFactory.MapLogicalView(ref refGuidLogicalView, out bstrPhysicalView)); refGuidLogicalView = VSConstants.LOGVIEWID_Code; Assert.AreEqual(VSConstants.E_NOTIMPL, editorFactory.MapLogicalView(ref refGuidLogicalView, out bstrPhysicalView)); refGuidLogicalView = VSConstants.LOGVIEWID_TextView; Assert.AreEqual(VSConstants.S_OK, editorFactory.MapLogicalView(ref refGuidLogicalView, out bstrPhysicalView)); refGuidLogicalView = VSConstants.LOGVIEWID_UserChooseView; Assert.AreEqual(VSConstants.E_NOTIMPL, editorFactory.MapLogicalView(ref refGuidLogicalView, out bstrPhysicalView)); refGuidLogicalView = VSConstants.LOGVIEWID_Primary; Assert.AreEqual(VSConstants.S_OK, editorFactory.MapLogicalView(ref refGuidLogicalView, out bstrPhysicalView)); }
public void TestIndexComboSetCurValWithOverflowInt() { ComboBoxPackage packageObject = new ComboBoxPackage(); Assert.IsNotNull(packageObject, "Failed to create package"); OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); // Add site support to create and enumerate tool windows GenericMockFactory mockFactory = new GenericMockFactory("MockUIShell", new Type[] { typeof(IVsUIShell) }); BaseMock uiShell = mockFactory.GetInstance(); serviceProvider.AddService(typeof(SVsUIShell), uiShell, false); // Site the package Assert.AreEqual(0, ((IVsPackage)packageObject).SetSite(serviceProvider), "SetSite did not return S_OK"); MethodInfo method = typeof(ComboBoxPackage).GetMethod("OnMenuMyIndexCombo", BindingFlags.NonPublic | BindingFlags.Instance); Int64 inChoice = Int64.MaxValue; object inParam1 = inChoice; IntPtr outParam1 = IntPtr.Zero; object inParam2 = null; IntPtr outParam2 = Marshal.AllocCoTaskMem(64); //64 == size of a variant + a little extra padding try { // Set IndexCombo to 2nd choice in list OleMenuCmdEventArgs eventArgs1 = new OleMenuCmdEventArgs(inParam1, outParam1); bool hasTrown = Utilities.HasFunctionThrown <ArgumentException>(delegate { method.Invoke(packageObject, new object[] { null, eventArgs1 }); }); Assert.IsTrue(hasTrown); OleMenuCmdEventArgs eventArgs2 = new OleMenuCmdEventArgs(inParam2, outParam2); object result = method.Invoke(packageObject, new object[] { null, eventArgs2 }); } finally { if (outParam2 != IntPtr.Zero) { Marshal.FreeCoTaskMem(outParam2); } } }
public CommonProjectNode(CommonProjectPackage /*!*/ package, ImageList /*!*/ imageList) { ContractUtils.RequiresNotNull(package, "package"); ContractUtils.RequiresNotNull(imageList, "imageList"); _package = package; CanFileNodesHaveChilds = true; OleServiceProvider.AddService(typeof(VSLangProj.VSProject), new OleServiceProvider.ServiceCreatorCallback(CreateServices), false); SupportsProjectDesigner = true; _imageList = imageList; //Store the number of images in ProjectNode so we know the offset of the language icons. _imageOffset = ImageHandler.ImageList.Images.Count; foreach (Image img in ImageList.Images) { ImageHandler.AddImage(img); } InitializeCATIDs(); }
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 SetSite() { // Create the package IVsPackage package = new VSThrowawayPackagePackage() as IVsPackage; Assert.IsNotNull(package, "The object does not implement IVsPackage"); // Create a basic service provider OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); // Add site support to register editor factory BaseMock registerEditor = VSThrowawayPackage_UnitTests.EditorTests.RegisterEditorsServiceMock.GetRegisterEditorsInstance(); serviceProvider.AddService(typeof(SVsRegisterEditors), registerEditor, false); // Site the package Assert.AreEqual(0, package.SetSite(serviceProvider), "SetSite did not return S_OK"); // Unsite the package Assert.AreEqual(0, package.SetSite(null), "SetSite(null) did not return S_OK"); }
public void ShowToolwindowNegativeTest() { IVsPackage package = new JavaScriptBrowserPackage() as IVsPackage; // Create a basic service provider OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); //Add uishell service that knows how to create a toolwindow BaseMock uiShellService = UIShellServiceMock.GetUiShellInstanceCreateToolWinReturnsNull(); serviceProvider.AddService(typeof(SVsUIShell), uiShellService, false); // Site the package Assert.AreEqual(0, package.SetSite(serviceProvider), "SetSite did not return S_OK"); MethodInfo method = typeof(JavaScriptBrowserPackage).GetMethod("ShowToolWindow", BindingFlags.NonPublic | BindingFlags.Instance); //ShowToolWindow throw NotSupportException but the Exception is converted during the //call of invoke method object result = method.Invoke(package, new object[] { null, null }); }
public void TokensWithSpace() { using (OleServiceProvider provider = new OleServiceProvider()) { ResetScopeState(); // Create a mock engine provider. BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance(); // Create a mock engine. BaseMock mockEngine = MockFactories.EngineFactory.GetInstance(); // 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 scanner for this test. BaseMock scannerMock = MockFactories.ScannerFactory.GetInstance(); scannerMock["Iteration"] = 0; TokenInfo[] tokens = new TokenInfo[2]; tokens[0] = new TokenInfo(); tokens[0].StartIndex = 0; tokens[0].EndIndex = 2; tokens[0].Trigger = TokenTriggers.None; tokens[1] = new TokenInfo(); tokens[1].StartIndex = 4; tokens[1].EndIndex = 4; tokens[1].Trigger = TokenTriggers.MemberSelect; scannerMock["Tokens"] = tokens; scannerMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IScanner).FullName, "ScanTokenAndProvideInfoAboutIt"), new EventHandler <CallbackArgs>(StandardScannerCallback)); Declarations declarations = ExecuteGetDeclarations("dte .", scannerMock as IScanner, provider); Assert.IsTrue(0 == declarations.GetCount()); Assert.IsTrue(0 == mockEngine.TotalCallsAllFunctions()); } }
public void TestMRUComboInParamNumber() { ComboBoxPackage packageObject = new ComboBoxPackage(); Assert.IsNotNull(packageObject, "Failed to create package"); OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); // Add site support to create and enumerate tool windows GenericMockFactory mockFactory = new GenericMockFactory("MockUIShell", new Type[] { typeof(IVsUIShell) }); BaseMock uiShell = mockFactory.GetInstance(); serviceProvider.AddService(typeof(SVsUIShell), uiShell, false); // Site the package Assert.AreEqual(0, ((IVsPackage)packageObject).SetSite(serviceProvider), "SetSite did not return S_OK"); MethodInfo method = typeof(ComboBoxPackage).GetMethod("OnMenuMyMRUCombo", BindingFlags.NonPublic | BindingFlags.Instance); object inParam1 = 42; IntPtr outParam1 = IntPtr.Zero; object inParam2 = null; IntPtr outParam2 = Marshal.AllocCoTaskMem(64); //64 == size of a variant + a little extra padding try { OleMenuCmdEventArgs eventArgs = new OleMenuCmdEventArgs(inParam1, outParam1); object result = method.Invoke(packageObject, new object[] { null, eventArgs }); // Retrieve current value of Zoom and verify OleMenuCmdEventArgs eventArgs2 = new OleMenuCmdEventArgs(inParam2, outParam2); result = method.Invoke(packageObject, new object[] { null, eventArgs2 }); string retrieved = (string)Marshal.GetObjectForNativeVariant(outParam2); Assert.AreEqual <string>(inParam1.ToString(), retrieved); Assert.AreEqual(1, uiShell.FunctionCalls(String.Format("{0}.{1}", typeof(IVsUIShell).FullName, "ShowMessageBox")), "IVsUIShell.ShowMessageBox was not called"); } finally { } }
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 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 OnApplyTest() { SccProviderOptions target = new SccProviderOptions(); // Create a basic service provider using (OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) { // Mock the UIShell service to answer Cancel to the dialog invocation BaseMock mockUIShell = MockUiShellProvider.GetShowMessageBoxCancel(); serviceProvider.AddService(typeof(IVsUIShell), mockUIShell, true); // Create an ISite wrapper over the service provider SiteWrappedServiceProvider wrappedProvider = new SiteWrappedServiceProvider(serviceProvider); target.Site = wrappedProvider; Assembly shell = typeof(Microsoft.VisualStudio.Shell.DialogPage).Assembly; Type argtype = shell.GetType("Microsoft.VisualStudio.Shell.DialogPage+PageApplyEventArgs", true); MethodInfo method = typeof(SccProviderOptions).GetMethod("OnApply", BindingFlags.NonPublic | BindingFlags.Instance); object eventargs = shell.CreateInstance(argtype.FullName); method.Invoke(target, new object[] { eventargs }); } }
public void StandardConstructor() { using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) { IVsPackage package = null; try { // 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 a package object and site it. package = new PythonConsolePackage() as IVsPackage; package.SetSite(provider); // Create a console window using the standard constructor and verify that the // text buffer is created and sited. using (IDisposable consoleObject = CommandWindowHelper.CreateConsoleWindow() as IDisposable) { Assert.IsTrue(0 < textLinesMock.FunctionCalls(string.Format("{0}.{1}", typeof(IObjectWithSite).FullName, "SetSite"))); } } finally { if (null != package) { package.SetSite(null); package.Close(); } } } }
public void SetSite() { // Create the package IVsPackage package = new BasicSccProvider() as IVsPackage; Assert.IsNotNull(package, "The object does not implement IVsPackage"); // Create a basic service provider OleServiceProvider serviceProvider = OleServiceProvider.CreateOleServiceProviderWithBasicServices(); // Need to mock a service implementing IVsRegisterScciProvider, because the scc provider will register with it IVsRegisterScciProvider registerScciProvider = MockRegisterScciProvider.GetBaseRegisterScciProvider(); serviceProvider.AddService(typeof(IVsRegisterScciProvider), registerScciProvider, true); // Site the package Assert.AreEqual(0, package.SetSite(serviceProvider), "SetSite did not return S_OK"); // Unsite the package Assert.AreEqual(0, package.SetSite(null), "SetSite(null) did not return S_OK"); // Remove the mock service serviceProvider.RemoveService(typeof(IVsRegisterScciProvider)); }
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); } } }
private ServiceProviderHelper(Type t, object instance) { type = t; _instance = instance; serviceProvider.AddService(t, instance, false); }
public void ConsoleCreation() { using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) { // In order to create a console window we have to add the text buffer to the // local registry. // 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); // Define the mock object for the text view. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); mockRegistry.AddClass(typeof(VsTextViewClass), textViewMock); // Add the local registry mock to the service provider. provider.AddService(typeof(SLocalRegistry), mockRegistry, false); // Create a mock UIShell to be able to create the tool window. BaseMock uiShell = MockFactories.UIShellFactory.GetInstance(); uiShell["Frame"] = MockFactories.WindowFrameFactory.GetInstance() as IVsWindowFrame; uiShell.AddMethodCallback( string.Format("{0}.{1}", typeof(IVsUIShell), "CreateToolWindow"), new EventHandler <CallbackArgs>(CreateToolwindowCallback)); provider.AddService(typeof(SVsUIShell), uiShell, false); IVsPackage package = null; try { // Create the package. package = new PythonConsolePackage() as IVsPackage; Assert.IsNotNull(package); // Make sure that the static variable about the global service provider is null; FieldInfo globalProvider = typeof(Microsoft.VisualStudio.Shell.Package).GetField("_globalProvider", BindingFlags.Static | BindingFlags.NonPublic); globalProvider.SetValue(null, null); // Site it. int hr = package.SetSite(provider); Assert.IsTrue(Microsoft.VisualStudio.ErrorHandler.Succeeded(hr)); // Get the command target from the package. IOleCommandTarget target = package as IOleCommandTarget; Assert.IsNotNull(target); CommandTargetHelper helper = new CommandTargetHelper(target); helper.ExecCommand(GuidList.guidIronPythonConsoleCmdSet, PkgCmdIDList.cmdidIronPythonConsole); } finally { if (null != package) { package.SetSite(null); package.Close(); } } } }
public void ViewCreationWithLanguage() { using (OleServiceProvider provider = OleServiceProvider.CreateOleServiceProviderWithBasicServices()) { // Create a mock text buffer for the console. BaseMock textLinesMock = MockFactories.TextBufferFactory.GetInstance(); // The buffer have to handle a few of connection points in order to enable the // creation of a Source object from the language service. ConnectionPointHelper.AddConnectionPointsToContainer( textLinesMock, new Type[] { typeof(IVsFinalTextChangeCommitEvents), typeof(IVsTextLinesEvents), typeof(IVsUserDataEvents) }); // Create the local registry mock and add the text buffer to it. LocalRegistryMock mockLocalRegistry = new LocalRegistryMock(); mockLocalRegistry.AddClass(typeof(VsTextBufferClass), textLinesMock); // Define the mock object for the text view. BaseMock textViewMock = MockFactories.TextViewFactory.GetInstance(); // Create the connection point for IVsTextViewEvents (needed for the language service). ConnectionPointHelper.AddConnectionPointsToContainer(textViewMock, new Type[] { typeof(IVsTextViewEvents) }); // Add the text view to the local registry. mockLocalRegistry.AddClass(typeof(VsTextViewClass), textViewMock); MockPackage package = new MockPackage(); ((IVsPackage)package).SetSite(provider); provider.AddService(typeof(Microsoft.VisualStudio.Shell.Package), package, true); // Create the language service and add it to the list of services. PythonLanguage language = new MockLanguage(); provider.AddService(typeof(PythonLanguage), language, true); language.SetSite(provider); // We need to add a method tip window to the local registry in order to create // a Source object. IVsMethodTipWindow methodTip = MockFactories.MethodTipFactory.GetInstance() as IVsMethodTipWindow; mockLocalRegistry.AddClass(typeof(VsMethodTipWindowClass), methodTip); // Create a mock expansion manager that is needed for the language service. BaseMock expansionManager = MockFactories.ExpansionManagerFactory.GetInstance(); ConnectionPointHelper.AddConnectionPointsToContainer(expansionManager, new Type[] { typeof(IVsExpansionEvents) }); Assembly asm = typeof(Microsoft.VisualStudio.Package.LanguageService).Assembly; Type expMgrType = asm.GetType("Microsoft.VisualStudio.Package.SVsExpansionManager"); provider.AddService(expMgrType, expansionManager, false); // Add the local registry to the list of services. provider.AddService(typeof(SLocalRegistry), mockLocalRegistry, false); using (ToolWindowPane windowPane = CommandWindowHelper.CreateConsoleWindow(provider) as ToolWindowPane) { Assert.IsNotNull(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))); // Verify that the language service contains a special view for this text view. FieldInfo specialSourcesField = typeof(PythonLanguage).GetField("specialSources", BindingFlags.Instance | BindingFlags.NonPublic); Assert.IsNotNull(specialSourcesField); Dictionary <IVsTextView, PythonSource> specialSources = (Dictionary <IVsTextView, PythonSource>)specialSourcesField.GetValue(language); PythonSource source; Assert.IsTrue(specialSources.TryGetValue(textViewMock as IVsTextView, out source)); Assert.IsNotNull(source); // Set ColorState to null so that Dispose will not call Marshal.ReleaseComObject on it. source.ColorState = null; } } }
public void TokensWithSpace() { using (OleServiceProvider provider = new OleServiceProvider()) { ResetScopeState(); // Create a mock engine provider. BaseMock mockEngineProvider = MockFactories.EngineProviderFactory.GetInstance(); // Create a mock engine. BaseMock mockEngine = MockFactories.EngineFactory.GetInstance(); // 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 scanner for this test. BaseMock scannerMock = MockFactories.ScannerFactory.GetInstance(); scannerMock["Iteration"] = 0; TokenInfo[] tokens = new TokenInfo[2]; tokens[0] = new TokenInfo(); tokens[0].StartIndex = 0; tokens[0].EndIndex = 2; tokens[0].Trigger = TokenTriggers.None; tokens[1] = new TokenInfo(); tokens[1].StartIndex = 4; tokens[1].EndIndex = 4; tokens[1].Trigger = TokenTriggers.MemberSelect; scannerMock["Tokens"] = tokens; scannerMock.AddMethodCallback( string.Format("{0}.{1}", typeof(IScanner).FullName, "ScanTokenAndProvideInfoAboutIt"), new EventHandler<CallbackArgs>(StandardScannerCallback)); Declarations declarations = ExecuteGetDeclarations("dte .", scannerMock as IScanner, provider); Assert.IsTrue(0 == declarations.GetCount()); Assert.IsTrue(0 == mockEngine.TotalCallsAllFunctions()); } }
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 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)); } } }