/// <summary>
        /// Populate element with list of properties
        /// </summary>
        /// <param name="element"></param>
        /// <param name="list"></param>
        private static void PopulateWithIndicatedProperties(this A11yElement element, List <int> list)
        {
            element.Clear();
            if (element.IsSafeToRefresh())
            {
                A11yAutomation.UIAutomationObject.PollForPotentialSupportedProperties((IUIAutomationElement)element.PlatformObject, out int[] ppids, out string[] ppns);

                // build a cache based on the lists
                var cache = DesktopElementHelper.BuildCacheRequest(list, null);

                // buildupdate cached element
                var uia = ((IUIAutomationElement)element.PlatformObject).BuildUpdatedCache(cache);

                var properties = RetrievePropertiesFromCache(uia, list);

                element.Properties = properties.Where(p => !string.IsNullOrEmpty(p.TextValue)).ToDictionary(d => d.Id);

                element.UpdateGlimpse();

                // release previous UIAElement
                Marshal.ReleaseComObject(uia);
                // release cache interface.
                Marshal.ReleaseComObject(cache);
            }
        }
        public void CreateScrubbedCopyWithoutRelationships_InputHasGlimpse_ResultHasTruncatedGlimpse()
        {
            A11yElement input = new A11yElement
            {
                Properties = new Dictionary <int, A11yProperty>
                {
                    { PropertyType.UIA_NamePropertyId, new A11yProperty(PropertyType.UIA_NamePropertyId, "abcde") },
                    { PropertyType.UIA_LocalizedControlTypePropertyId, new A11yProperty(PropertyType.UIA_LocalizedControlTypePropertyId, "MyLocalizedControl") },
                },
            };

            input.UpdateGlimpse();

            Assert.AreEqual(@"MyLocalizedControl 'abcde'", input.Glimpse);
            A11yElement copy = input.CreateScrubbedCopyWithoutRelationships();

            Assert.AreEqual(@"MyLocalizedControl 'abc'", copy.Glimpse);
        }
Пример #3
0
        /// <summary>
        /// Given the input element, create a scrubbed copy that has no parent or children
        /// </summary>
        /// <param name="element">The element to clone</param>
        /// <returns>A new element that has been scrubbed of key information and properties</returns>
        internal static A11yElement CreateScrubbedCopyWithoutRelationships(this A11yElement element)
        {
            A11yElement clone = new A11yElement
            {
                BugId          = element.BugId,
                ScanResults    = element.ScanResults,
                TreeWalkerMode = element.TreeWalkerMode,
                UniqueId       = element.UniqueId,
                Children       = element.Children == null ? null : new List <A11yElement>()
            };

            if (element.Properties != null)
            {
                clone.Properties = new Dictionary <int, A11yProperty>();
                foreach (var pair in element.Properties)
                {
                    if (PropertiesToInclude.Contains(pair.Key))
                    {
                        if (pair.Key == PropertyType.UIA_NamePropertyId)
                        {
                            A11yProperty scrubbedProperty = new A11yProperty(pair.Key, pair.Value.ToString().Substring(0, MaxScrubbedNameLength));
                            clone.Properties.Add(pair.Key, scrubbedProperty);
                        }
                        else
                        {
                            clone.Properties.Add(pair.Key, pair.Value);
                        }
                    }
                }
            }

            if (element.Glimpse != null)
            {
                clone.UpdateGlimpse();
            }

            return(clone);
        }
        /// <summary>
        /// Refresh the property and patterns with Live data.
        /// it also set the Glimpse
        /// the update is done via caching to improve performance.
        /// </summary>
        /// <param name="useProperties">default is false to refresh it from UIElement directly</param>
        private static void PopulatePropertiesAndPatternsFromCache(this A11yElement element)
        {
            try
            {
                // Get the list of properties to retrieve
                A11yAutomation.UIAutomationObject.PollForPotentialSupportedProperties((IUIAutomationElement)element.PlatformObject, out int[] ppids, out string[] ppns);

                var ppl = new List <Tuple <int, string> >();

                for (int i = 0; i < ppids.Length; i++)
                {
                    var id   = (int)ppids.GetValue(i);
                    var name = (string)ppns.GetValue(i);

                    if (DesktopElement.IsExcludedProperty(id, name) == false)
                    {
                        ppl.Add(new Tuple <int, string>(id, name));
                    }
                }

                A11yAutomation.UIAutomationObject.PollForPotentialSupportedPatterns((IUIAutomationElement)element.PlatformObject, out int[] ptids, out string[] ptns);
                var ptl = new List <Tuple <int, string> >();

                for (int i = 0; i < ptids.Length; i++)
                {
                    ptl.Add(new Tuple <int, string>((int)ptids.GetValue(i), (string)ptns.GetValue(i)));
                }

                var pplist = (from pp in ppl
                              select pp.Item1).ToList();
                var ptlist = (from pt in ptl
                              select pt.Item1).ToList();

                // build a cache based on the lists
                var cache = DesktopElementHelper.BuildCacheRequest(pplist, ptlist);

                // buildupdate cached element
                var uia = ((IUIAutomationElement)element.PlatformObject).BuildUpdatedCache(cache);

                // retrieve properties from cache
                var ps = (from pp in ppl//.AsParallel()
                          let val = GetPropertyValueFromCache(uia, pp.Item1)
                                    where val != null
                                    select new A11yProperty(pp.Item1, val, pp.Item2));

                element.Properties = ps.Where(p => !string.IsNullOrEmpty(p.TextValue)).ToDictionary(d => d.Id);

                element.UpdateGlimpse();

                element.InitClickablePointProperty(uia);

                // retrieve patterns from cache
                var ptlst = from pt in ptl
                            let pi = A11yPatternFactory.GetPatternInstance(element, uia, pt.Item1, pt.Item2)
                                     where pi != null
                                     select pi;

                element.Patterns = ptlst.ToList();

                // release previous UIAElement
                Marshal.ReleaseComObject(uia);
                // release cache interface.
                Marshal.ReleaseComObject(cache);

                ppl.Clear();
                ptl.Clear();
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception e)
            {
                e.ReportException();
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }