private static T Pattern <T>(this AutomationElement node, AutomationPattern pattern) where T : BasePattern
 {
     try
     {
         return((T)node.GetCurrentPattern(pattern));
     }
     catch (InvalidOperationException)
     {
         Console.WriteLine("{0} pattern is not supported by {1}.", pattern.ProgrammaticName, node.Current.Name);
         AutomationWrapper.DumpElement(node);
         throw;
     }
 }
        private AutomationElement FindChildOfProjectHelper(EnvDTE.Project project, string[] path, bool assertOnFailure) {
            var sln = project.DTE.Solution;
            int count = sln.Projects.OfType<EnvDTE.Project>().Count(p => {
                try {
                    return !string.IsNullOrEmpty(p.FullName);
                } catch (Exception) {
                    return false;
                }
            });
            var slnLabel = string.Format(
                "Solution '{0}' ({1} project{2})",
                Path.GetFileNameWithoutExtension(sln.FullName),
                count,
                count == 1 ? "" : "s"
            );

            var slnElements = Element.FindAll(TreeScope.Children, new PropertyCondition(
                AutomationElement.NameProperty, slnLabel
            ));
            int slnCount = slnElements.OfType<AutomationElement>().Count();
            if (assertOnFailure) {
                Assert.AreEqual(1, slnCount, string.Format("Did not find single node <{0}>", slnLabel));
            } else if (slnCount != 1) {
                return null;
            }
            var slnElement = slnElements.Cast<AutomationElement>().Single();

            var projLabel = project.Name;
            var projElements = slnElement.FindAll(TreeScope.Children, new PropertyCondition(
                AutomationElement.NameProperty, projLabel
            ));
            int projCount = projElements.OfType<AutomationElement>().Count();
            if (assertOnFailure) {
                Assert.AreEqual(1, projCount, string.Format("Did not find single node <{0}>", projLabel));
            } else if (projCount != 1) {
                return null;
            }
            var projElement = projElements.Cast<AutomationElement>().Single();

            var itemElement = path.Any() ? FindNode(
                projElement.FindAll(TreeScope.Children, Condition.TrueCondition),
                path,
                0
            ) : projElement;

            if (assertOnFailure) {
                AutomationWrapper.DumpElement(Element);
                Assert.IsNotNull(itemElement, string.Format("Did not find element <{0}\\{1}\\{2}>", slnLabel, projLabel, string.Join("\\", path)));
            }
            return itemElement;
        }
Exemple #3
0
        /// <summary>
        /// Waits for no dialog. If a dialog appears before the timeout expires
        /// then the test fails and the dialog is closed.
        /// </summary>
        public void WaitForNoDialog(TimeSpan timeout) {
            IVsUIShell uiShell = GetService<IVsUIShell>(typeof(IVsUIShell));
            IntPtr hwnd;
            uiShell.GetDialogOwnerHwnd(out hwnd);

            for (int i = 0; i < 100 && hwnd == _mainWindowHandle; i++) {
                System.Threading.Thread.Sleep((int)timeout.TotalMilliseconds / 100);
                uiShell.GetDialogOwnerHwnd(out hwnd);
            }

            if (hwnd != (IntPtr)_mainWindowHandle) {
                AutomationWrapper.DumpElement(AutomationElement.FromHandle(hwnd));
                NativeMethods.EndDialog(hwnd, (IntPtr)(int)MessageBoxButton.Cancel);
                Assert.Fail("Dialog appeared - see output for details");
            }
        }
        /// <summary>
        /// Dumps the current top-level window in VS
        /// </summary>
        public static void DumpVS()
        {
            IVsUIShell uiShell = VSTestContext.ServiceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;
            IntPtr     hwnd;

            uiShell.GetDialogOwnerHwnd(out hwnd);
            AutomationWrapper.DumpElement(AutomationElement.FromHandle(hwnd));

            // if we have a dialog open dump the main VS window too
            var mainHwnd = new IntPtr(VSTestContext.DTE.MainWindow.HWnd);

            if (mainHwnd != hwnd)
            {
                Console.WriteLine("VS: ");
                AutomationWrapper.DumpElement(AutomationElement.FromHandle(mainHwnd));
            }
        }
Exemple #5
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;
            IntPtr hwnd;
            uiShell.GetDialogOwnerHwnd(out 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: ");
            var 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();
                    }
                }
            }
        }
        public AutomationElement this[Guid tabGuid] {
            get {
                var tabItem = FindByAutomationId("PropPage_" + tabGuid.ToString("n").ToLower());
                Assert.IsNotNull(tabItem, "Failed to find page");

                AutomationWrapper.DumpElement(tabItem);
                foreach (var p in tabItem.GetSupportedPatterns())
                {
                    Console.WriteLine("Supports {0}", p.ProgrammaticName);
                }

                try {
                    tabItem.GetInvokePattern().Invoke();
                } catch (InvalidOperationException) {
                    AutomationWrapper.DoDefaultAction(tabItem);
                }

                return(FindByAutomationId("PageHostingPanel"));
            }
        }
Exemple #7
0
        public void DismissAllDialogs()
        {
            int foundWindow = 2;

            while (foundWindow != 0)
            {
                IVsUIShell uiShell = GetService <IVsUIShell>(typeof(IVsUIShell));
                if (uiShell == null)
                {
                    return;
                }

                IntPtr hwnd;
                uiShell.GetDialogOwnerHwnd(out hwnd);

                for (int j = 0; j < 10 && hwnd == _mainWindowHandle; j++)
                {
                    System.Threading.Thread.Sleep(100);
                    uiShell.GetDialogOwnerHwnd(out hwnd);
                }

                //We didn't see any dialogs
                if (hwnd == IntPtr.Zero || hwnd == _mainWindowHandle)
                {
                    foundWindow--;
                    continue;
                }

                //MessageBoxButton.Abort
                //MessageBoxButton.Cancel
                //MessageBoxButton.No
                //MessageBoxButton.Ok
                //MessageBoxButton.Yes
                //The second parameter is going to be the value returned... We always send Ok
                Debug.WriteLine("Dismissing dialog");
                AutomationWrapper.DumpElement(AutomationElement.FromHandle(hwnd));
                NativeMethods.EndDialog(hwnd, new IntPtr(1));
            }
        }
#pragma warning restore 0618
#endif

        public SessionHolder <T> WaitForSession <T>() where T : IIntellisenseSession
        {
            var sessionStack = IntellisenseSessionStack;

            for (int i = 0; i < 40; i++)
            {
                if (sessionStack.TopSession is T)
                {
                    break;
                }
                System.Threading.Thread.Sleep(250);
            }

            if (!(sessionStack.TopSession is T))
            {
                Console.WriteLine("Buffer text:\r\n{0}", Text);
                Console.WriteLine("-----");
                AutomationWrapper.DumpVS();
                Assert.Fail("failed to find session " + typeof(T).FullName);
            }
            return(new SessionHolder <T>((T)sessionStack.TopSession, this));
        }
Exemple #9
0
        /// <summary>
        /// Dumps the current top-level window in VS
        /// </summary>
        public static void DumpVS()
        {
            var sp = ServiceProvider.GlobalProvider;

            if (sp == null)
            {
                return;
            }

            IVsUIShell uiShell = sp.GetService(typeof(IVsUIShell)) as IVsUIShell;
            IntPtr     hwnd;

            uiShell.GetDialogOwnerHwnd(out hwnd);
            AutomationWrapper.DumpElement(AutomationElement.FromHandle(hwnd));

            // if we have a dialog open dump the main VS window too
            var mainHwnd = new IntPtr(((EnvDTE.DTE)sp.GetService(typeof(EnvDTE.DTE))).MainWindow.HWnd);

            if (mainHwnd != hwnd)
            {
                Console.WriteLine("VS: ");
                AutomationWrapper.DumpElement(AutomationElement.FromHandle(mainHwnd));
            }
        }
Exemple #10
0
 public PythonTestExplorer(VisualStudioApp app, AutomationElement element, AutomationWrapper searchBarTextBox)
     : base(element)
 {
     _app       = app;
     _searchBar = searchBarTextBox ?? throw new ArgumentNullException(nameof(searchBarTextBox));
 }
Exemple #11
0
 public static void Collapse(this AutomationElement node)
 {
     AutomationWrapper.Collapse(node);
 }
Exemple #12
0
 public static void EnsureExpanded(this AutomationElement node)
 {
     AutomationWrapper.EnsureExpanded(node);
 }
Exemple #13
0
 public static void Select(this AutomationElement element)
 {
     AutomationWrapper.Select(element);
 }
Exemple #14
0
 void ITreeNode.AddToSelection()
 {
     AutomationWrapper.AddToSelection(Element);
 }