PageRenderTime 37ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/V4/Quickstarts/EventAggregation/Desktop/EventAggregation.Tests.AcceptanceTests/Helpers/UIItemExtensions.cs

#
C# | 109 lines | 77 code | 13 blank | 19 comment | 12 complexity | 35f40add669083dbc912713d5e1600a2 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.Linq;
 20using System.Text;
 21using Core.UIItems;
 22using System.Windows.Automation;
 23using Core.UIItems.TabItems;
 24
 25namespace EventAggregation.AcceptanceTests.Helpers
 26{
 27    public static class UIItemExtensions
 28    {
 29        public static void Hover(this UIItem uiItem)
 30        {
 31            Core.InputDevices.Mouse.Instance.Location = Core.C.Center(uiItem.Bounds);
 32            System.Threading.Thread.Sleep(1000);
 33        }
 34        private static List<AutomationElement> automationElementList = new List<AutomationElement>();
 35
 36        public static AutomationElement SearchInRawTreeByName(this AutomationElement rootElement, string name)
 37        {
 38            AutomationElement elementNode = TreeWalker.RawViewWalker.GetFirstChild(rootElement);
 39
 40            while (elementNode != null)
 41            {
 42                if (name.Equals(elementNode.Current.Name, StringComparison.OrdinalIgnoreCase)
 43                      || (name.Equals(elementNode.Current.AutomationId, StringComparison.OrdinalIgnoreCase)))
 44                {
 45                    return elementNode;
 46                }
 47                AutomationElement returnedAutomationElement = elementNode.SearchInRawTreeByName(name);
 48                if (returnedAutomationElement != null)
 49                {
 50                    return returnedAutomationElement;
 51                }
 52                elementNode = TreeWalker.RawViewWalker.GetNextSibling(elementNode);
 53            }
 54            return null;
 55        }
 56
 57
 58        public static List<AutomationElement> FindSiblingsInTreeByName(this AutomationElement rootElement, string name)
 59        {
 60            // Clear the automation element list.
 61            automationElementList.Clear();
 62            AutomationElement parentElement = rootElement;
 63
 64            WalkThroughRawTreeAndPopulateAEList(parentElement, name);
 65
 66            return automationElementList;            
 67        }
 68
 69        private static void WalkThroughRawTreeAndPopulateAEList(AutomationElement parentElement, string name)
 70        {
 71            AutomationElement element = SearchInRawTreeByName(parentElement, name);
 72            AutomationElement elementNode = null;
 73            if (element != null)
 74            {
 75                // Add the element to the list;
 76                automationElementList.Add(element);
 77
 78                elementNode = TreeWalker.RawViewWalker.GetNextSibling(element);
 79                while (elementNode != null)
 80                {
 81                    // Add the elementNode to the list
 82                    if (elementNode.Current.AutomationId.Equals(name))
 83                        automationElementList.Add(elementNode);
 84                    else
 85                    {
 86                        WalkThroughRawTreeAndPopulateAEList(elementNode, name);
 87                    }
 88                    elementNode = TreeWalker.RawViewWalker.GetNextSibling(elementNode);
 89                }
 90            }
 91        }
 92
 93
 94        public static void SelectEmployee(this ListView list, int rowNumber)
 95        {
 96            list.Rows[rowNumber].Select();
 97        }
 98
 99        public static void SelectEmployee(this ListView list, string firstName)
100        {
101            list.Rows.Find(r => r.Cells[0].Text.Equals(firstName)).Select();
102        }
103
104        public static void SelectEmployee(this ListView list, string firstName, string lastName)
105        {
106            list.Rows.Find(r => r.Cells[0].Text.Equals(firstName) && r.Cells[1].Text.Equals(lastName)).Select();
107        }
108    }
109}