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;
                }
            }
        }
示例#2
0
        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;
                }
            }
        }