private static void SetElementDataContext(bool upperBoundExceeded) { BoundedCounter counter = new BoundedCounter(1); counter.TryIncrement(); if (upperBoundExceeded) // if true, we're 1 over capacity. if false, we're exactly at capacity { counter.TryIncrement(); } ShimElementDataContext dc = new ShimElementDataContext { ElementCounterGet = () => counter }; ShimGetDataAction.GetElementDataContextGuid = (_) => dc; }
/// <summary> /// Populate tree by retrieving all children at once. /// </summary> /// <param name="rootNode"></param> /// <param name="parentNode"></param> /// <param name="startChildId"></param> private int PopulateChildrenTreeNode(A11yElement rootNode, A11yElement parentNode, int startChildId) { this.Elements.Add(rootNode); rootNode.Parent = parentNode; rootNode.TreeWalkerMode = this.WalkerMode; // set tree walker mode. IUIAutomationTreeWalker walker = A11yAutomation.GetTreeWalker(this.WalkerMode); IUIAutomationElement child = (IUIAutomationElement)rootNode.PlatformObject; if (child != null) { try { child = walker.GetFirstChildElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types while (child != null && _elementCounter.TryIncrement()) { #pragma warning disable CA2000 // childNode will be disposed by the parent node // Create child without populating basic property. it will be set all at once in parallel. var childNode = new DesktopElement(child, true, false); #pragma warning restore CA2000 // childNode will be disposed by the parent node rootNode.Children.Add(childNode); childNode.Parent = rootNode; childNode.UniqueId = startChildId++; startChildId = PopulateChildrenTreeNode(childNode, rootNode, startChildId); try { child = walker.GetNextSiblingElement(child); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); child = null; System.Diagnostics.Trace.WriteLine("Tree walker exception: " + ex); } #pragma warning restore CA1031 // Do not catch general exception types } } Marshal.ReleaseComObject(walker); return(startChildId); }
public void Reset_UpperBoundExceeded_SetsCorrectValues() { const int upperBound = 1; BoundedCounter counter = new BoundedCounter(upperBound); counter.TryIncrement(); counter.Reset(); Assert.AreEqual(0, counter.Attempts); Assert.AreEqual(0, counter.Count); Assert.AreEqual(upperBound, counter.UpperBound); Assert.IsFalse(counter.UpperBoundExceeded); }
public void TryIncrement_Overflows_ReturnsFalse_Updates_State() { const int upperBound = int.MaxValue - 1; BoundedCounter counter = new BoundedCounter(upperBound); Assert.IsTrue(counter.TryAdd(upperBound)); for (int loop = 0; loop < 5; loop++) { Assert.IsFalse(counter.TryIncrement()); Assert.AreEqual(int.MaxValue, counter.Attempts); Assert.AreEqual(upperBound, counter.Count); } }
public void TryIncrement_UpperBoundNotExceeded_ReturnsTrue_UpdatesState() { const int upperBound = 5; BoundedCounter counter = new BoundedCounter(upperBound); for (int loop = 0; loop < upperBound; loop++) { Assert.AreEqual(upperBound, counter.UpperBound); Assert.AreEqual(loop, counter.Attempts); Assert.AreEqual(loop, counter.Count); Assert.IsFalse(counter.UpperBoundExceeded); Assert.IsTrue(counter.TryIncrement()); } Assert.IsFalse(counter.UpperBoundExceeded); }
/// <summary> /// Add element and children into the list. /// </summary> /// <param name="e"></param> /// <param name="dic"></param> /// <param name="elementCounter">Provides an upper bound on the number of elements we'll allow to be loaded</param> internal static void AddElementAndChildrenIntoList(A11yElement e, Dictionary <int, A11yElement> dic, BoundedCounter elementCounter) { if (!elementCounter.TryIncrement()) { return; } dic.Add(e.UniqueId, e); if (e.Children != null && e.Children.Count != 0) { foreach (var c in e.Children) { AddElementAndChildrenIntoList(c, dic, elementCounter); } } }
public void TryIncrement_UpperBoundExceeded_ReturnsFalse_UpdatesState() { const int upperBound = 5; BoundedCounter counter = new BoundedCounter(upperBound); Assert.IsTrue(counter.TryAdd(upperBound)); Assert.IsFalse(counter.UpperBoundExceeded); // UpperBound has been reached, so all further adds should fail for (int loop = 0; loop < upperBound; loop++) { Assert.AreEqual(upperBound, counter.UpperBound); Assert.AreEqual(upperBound, counter.Count); Assert.AreEqual(upperBound + loop, counter.Attempts); Assert.IsFalse(counter.TryIncrement()); Assert.IsTrue(counter.UpperBoundExceeded); } }