Пример #1
0
        /// <summary>
        /// Checks the text of a dialog and dismisses it.
        ///
        /// dlgField is the field to check the text of.
        /// buttonId is the button to press to dismiss.
        /// </summary>
        private void CheckAndDismissDialog(string[] text, int dlgField, string buttonId, bool assertIfNoDialog)
        {
            var        handle  = new IntPtr(Dte.MainWindow.HWnd);
            IVsUIShell uiShell = ServiceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;

            uiShell.GetDialogOwnerHwnd(out IntPtr hwnd);

            for (int i = 0; i < 20 && hwnd == handle; i++)
            {
                System.Threading.Thread.Sleep(500);
                uiShell.GetDialogOwnerHwnd(out hwnd);
            }

            if (!assertIfNoDialog && (hwnd == IntPtr.Zero || hwnd == handle))
            {
                return;
            }

            Assert.AreNotEqual(IntPtr.Zero, hwnd, "hwnd is null, We failed to get the dialog");
            Assert.AreNotEqual(handle, hwnd, "hwnd is Dte.MainWindow, We failed to get the dialog");
            Console.WriteLine("Ending dialog: ");
            AutomationDialog dlg = new AutomationDialog(this, AutomationElement.FromHandle(hwnd));

            AutomationWrapper.DumpElement(dlg.Element);
            Console.WriteLine("--------");

            bool closed = false;

            try
            {
                string title = dlg.Text;
                if (assertIfNoDialog)
                {
                    AssertUtil.Contains(title, text);
                }
                else if (!text.All(title.Contains))
                {
                    // We do not want to close the dialog now, as it may be expected
                    // by a later part of the test.
                    closed = true;
                }
            }
            finally
            {
                if (!closed)
                {
                    if (buttonId == MessageBoxButton.Close.ToString())
                    {
                        dlg.WaitForClosed(TimeSpan.FromSeconds(10.0), dlg.CloseWindow);
                    }
                    else if (!dlg.ClickButtonAndClose(buttonId))
                    {
                        dlg.CloseWindow();
                    }
                }
            }
        }