public VisualStudioApp(IServiceProvider site) : this(new IntPtr(GetDTE(site).MainWindow.HWnd)) { // TODO: Make site non-optional ServiceProvider = site ?? Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider; _dte = GetDTE(site); foreach (var p in ((DTE2)_dte).ToolWindows.OutputWindow.OutputWindowPanes.OfType<OutputWindowPane>()) { p.Clear(); } var uiShell = GetService<IVsUIShell>(typeof(IVsUIShell)); IntPtr hwnd; ErrorHandler.ThrowOnFailure(uiShell.GetDialogOwnerHwnd(out hwnd)); if (hwnd != _mainWindowHandle) { using (var dlg = new AutomationDialog(this, AutomationElement.FromHandle(hwnd))) { //startup window popup, press button "Continue without code" var cwc = dlg.FindByName("Continue without code"); if (cwc != null) { Invoke(cwc); WaitForInputIdle(); return; } Console.WriteLine("Unexpected dialog at start of test"); DumpElement(dlg.Element); dlg.WaitForClosed(TimeSpan.FromSeconds(5), dlg.CloseWindow); } } }
/// <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(); } } } }