Example #1
0
        /// <summary>
        /// This is the common function that executes the verification that the build
        /// messages are correctly written or omitted from the output according with
        /// the verbosity level.
        /// It takes as argument a function that set the verbosity for the logger. This
        /// delegate is needed because there are at least two different ways to set the
        /// verbosity: one reading it from the registry, the other setting it directly
        /// on the logger.
        /// </summary>
        private static void VerifyLoggingByVerbosity(LoggerVerbositySetter verbositySetter)
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                using (TaskProvider task = new TaskProvider(new ServiceProvider(provider)))
                {
                    IVsHierarchy hierarchy  = MockFactories.HierarchyForLogger(provider);
                    BaseMock     mockOutput = MockFactories.OutputWindowPaneFactory.GetInstance();

                    // Create the logger and make sure that it points to the verbosity
                    // registry key that we have deleted at the beginning of the test.
                    IDELoggerProxy logger = new IDELoggerProxy(mockOutput as IVsOutputWindowPane, task, hierarchy);
                    logger.SetBuildVerbosityRegistryRoot(TestVerbosityRoot);

                    // Create the IEventSource that will be used to send messages to the logger
                    BaseMock mockSource = MockFactories.CreateMSBuildEventSource();

                    // Now initialize the logger.
                    logger.Initialize(mockSource as IEventSource);
                    // Verify that the logger has installed an event handler for messages.
                    Assert.IsNotNull(mockSource["MessageRaised"]);
                    BuildMessageEventHandler messageHandler = (BuildMessageEventHandler)mockSource["MessageRaised"];

                    foreach (VerbosityImportancePair expected in expectedVerbosityImportancePair)
                    {
                        // Because the IDEBuildLogger caches its Verbosity setting after reading it
                        // from the registry, we need to clear the cached value before calling the
                        // verbositySetter, which may set verbosity by changing the registry value.
                        logger.ClearVerbosityCache();

                        // Set the verbosity of the logger.
                        verbositySetter(logger, expected.Verbosity);
                        // Create a message of the expected importance.
                        BuildMessageEventArgs message = new BuildMessageEventArgs("message", "help", "sender", expected.Importance);

                        // Reset the counter of the calls to the output window pane.
                        mockOutput.ResetAllFunctionCalls();

                        // Execute the message event handler
                        messageHandler.Invoke(mockSource, message);
                        System.Windows.Forms.Application.DoEvents();

                        // Verify that the output pane was used only if the expected result is that
                        // something should be printed on the output.
                        if (expected.ShouldPrint)
                        {
                            Assert.IsTrue(mockOutput.TotalCallsAllFunctions() > 0);
                        }
                        else
                        {
                            Assert.AreEqual <int>(mockOutput.TotalCallsAllFunctions(), 0);
                        }
                    }
                }
            }
        }