public void PopulateData_LoadMode_ChainsToAddElementAndChildrenIntoList_CorrectParameters() { using (ShimsContext.Create()) { const DataContextMode expectedDcMode = DataContextMode.Load; const TreeViewMode expectedTreeViewMode = TreeViewMode.Control; A11yElement expectedElement = new A11yElement(); ElementDataContext dataContext = new ElementDataContext(expectedElement, 1); A11yElement actualElement = null; BoundedCounter actualCounter = null; Dictionary <int, A11yElement> actualDictionary = null; ShimCaptureAction.AddElementAndChildrenIntoListA11yElementDictionaryOfInt32A11yElementBoundedCounter = (e, d, c) => { actualElement = e; actualDictionary = d; actualCounter = c; }; CaptureAction.PopulateData(dataContext, expectedDcMode, expectedTreeViewMode); Assert.AreEqual(expectedDcMode, dataContext.Mode); Assert.AreEqual(expectedTreeViewMode, dataContext.TreeMode); Assert.AreSame(expectedElement, actualElement); Assert.IsNotNull(actualDictionary); Assert.IsFalse(actualDictionary.Any()); Assert.AreSame(dataContext.ElementCounter, actualCounter); Assert.AreSame(dataContext.Elements, actualDictionary); } }
public void TryAdd_ValueToAddIsZero_UpperBoundIsNotExceeded_ReturnsTrue() { const int upperBound = 5; BoundedCounter counter = new BoundedCounter(upperBound); Assert.IsTrue(counter.TryAdd(0)); }
public void TryAdd_ValueToAddIsNegative_ThrowsArgumentException() { const int upperBound = 5; BoundedCounter counter = new BoundedCounter(upperBound); counter.TryAdd(-1); }
public void Ctor_UpperBoundIsLargestValidValue_InitialValuesAreCorrect() { const int upperBound = int.MaxValue - 1; BoundedCounter counter = new BoundedCounter(upperBound); Assert.AreEqual(0, counter.Attempts); Assert.AreEqual(0, counter.Count); Assert.AreEqual(upperBound, counter.UpperBound); Assert.IsFalse(counter.UpperBoundExceeded); }
public void AddElementAndChildrenIntoList_CounterIsFull_ReturnsWithoutAdding() { BoundedCounter counter = new BoundedCounter(1); Assert.IsFalse(counter.TryAdd(2)); Assert.AreEqual(2, counter.Attempts); // We intentionally pass in nulls here, since it's an error if they're accessed in any way CaptureAction.AddElementAndChildrenIntoList(null, null, counter); Assert.AreEqual(3, counter.Attempts); }
public void TryAdd_UpperBoundNotExceeded_ReturnsTrue_UpdatesState() { const int upperBound = 5; const int valueToAdd = 3; BoundedCounter counter = new BoundedCounter(upperBound); Assert.IsTrue(counter.TryAdd(valueToAdd)); Assert.AreEqual(valueToAdd, counter.Attempts); Assert.AreEqual(valueToAdd, counter.Count); Assert.AreEqual(upperBound, counter.UpperBound); }
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); }
public void PopulateData_TestMode_ChainsToTreeWalkerRefreshTreeData_CorrectParameters() { using (ShimsContext.Create()) { const DataContextMode expectedDcMode = DataContextMode.Test; const TreeViewMode expectedTreeViewMode = TreeViewMode.Content; A11yElement expectedElement = new A11yElement(); List <A11yElement> expectedElements = null; ElementDataContext dataContext = new ElementDataContext(expectedElement, 1); BoundedCounter actualCounter = null; A11yElement actualElement = null; TreeViewMode? actualTreeMode = null; A11yElement rootElement = null; ShimTreeWalkerForTest.ConstructorA11yElementBoundedCounter = (ktw, e, c) => { actualCounter = c; actualElement = e; new ShimTreeWalkerForTest(ktw) { RefreshTreeDataTreeViewMode = (mode) => { actualTreeMode = mode; expectedElements = new List <A11yElement> { expectedElement }; rootElement = expectedElement; Assert.IsTrue(dataContext.ElementCounter.TryIncrement()); }, ElementsGet = () => expectedElements, TopMostElementGet = () => rootElement, }; }; CaptureAction.PopulateData(dataContext, expectedDcMode, expectedTreeViewMode); Assert.AreSame(dataContext.ElementCounter, actualCounter); Assert.AreSame(expectedElement, actualElement); Assert.AreEqual(expectedDcMode, dataContext.Mode); Assert.AreEqual(expectedTreeViewMode, dataContext.TreeMode); Assert.AreSame(expectedElement, dataContext.Elements.Values.First()); Assert.AreSame(expectedElement, dataContext.RootElment); } }
public void TryAdd_UpperBoundExceeded_ReturnsFalse_UpdatesState() { const int upperBound = 5; const int valueToAdd = 3; BoundedCounter counter = new BoundedCounter(upperBound); counter.TryAdd(valueToAdd); for (int loop = 0; loop < upperBound; loop++) { Assert.IsFalse(counter.TryAdd(valueToAdd)); Assert.AreEqual(valueToAdd * (loop + 2), counter.Attempts); Assert.AreEqual(upperBound, counter.Count); Assert.AreEqual(upperBound, counter.UpperBound); } }
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); } }
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; }
public void AddElementAndChildrenIntoList_GeneralCase_BuildsCorrectDictionary() { using (ShimsContext.Create()) { const int elementCount = 7; BoundedCounter counter = new BoundedCounter(100); Dictionary <int, A11yElement> elementsOut = new Dictionary <int, A11yElement>(); // Build our tree List <ShimA11yElement> elements = new List <ShimA11yElement>(); for (int loop = 0; loop < elementCount; loop++) { int uniqueId = loop; elements.Add(new ShimA11yElement { UniqueIdGet = () => uniqueId, // Don't use loop here, since it will get the final value, not the in-loop value }); } elements[0].ChildrenGet = () => new List <A11yElement> { elements[1], elements[2] }; elements[2].ChildrenGet = () => new List <A11yElement> { elements[3] }; elements[3].ChildrenGet = () => new List <A11yElement> { elements[4], elements[5], elements[6] }; elements[6].ChildrenGet = () => new List <A11yElement>(); CaptureAction.AddElementAndChildrenIntoList(elements[0], elementsOut, counter); Assert.AreEqual(elementCount, elementsOut.Count); for (int loop = 0; loop < elementCount; loop++) { Assert.AreEqual(loop, elementsOut[loop].UniqueId); } } }
public void AddElementAndChildrenIntoList_GeneralCase_BuildsCorrectDictionary() { const int elementCount = 7; BoundedCounter counter = new BoundedCounter(100); Dictionary <int, A11yElement> elementsOut = new Dictionary <int, A11yElement>(); // Build our tree List <A11yElement> elements = new List <A11yElement>(); for (int loop = 0; loop < elementCount; loop++) { A11yElement element = new A11yElement { UniqueId = loop }; elements.Add(element); } elements[0].Children = new List <A11yElement> { elements[1], elements[2] }; elements[2].Children = new List <A11yElement> { elements[3] }; elements[3].Children = new List <A11yElement> { elements[4], elements[5], elements[6] }; elements[6].Children = new List <A11yElement>(); CaptureAction.AddElementAndChildrenIntoList(elements[0], elementsOut, counter); Assert.AreEqual(elementCount, elementsOut.Count); for (int loop = 0; loop < elementCount; loop++) { Assert.AreEqual(loop, elementsOut[loop].UniqueId); } }
/// <summary> /// Constructor /// </summary> internal ElementDataContext(A11yElement e, int maxElements) { this.Element = e; ElementCounter = new BoundedCounter(maxElements); }
/// <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); } } }
/// <summary> /// Constructor /// </summary> /// <param name="element"></param> /// <param name="elementCounter">Provides an upper bound on the number of elements we'll allow to be tested</param> public TreeWalkerForTest(A11yElement element, BoundedCounter elementCounter) { _elementCounter = elementCounter; this.SelectedElement = element; this.Elements = new List <A11yElement>(); }