public void VectorChanged_AddWhileAttached_AllAttached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new Button()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); foreach (StubBehavior stub in behaviorCollection) { TestUtilities.AssertAttached(stub, behaviorCollection.AssociatedObject); } }
public void VectorChanged_ReplaceWhileAttached_OldDetachedNewAttached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new Button()); StubBehavior first = new StubBehavior(); behaviorCollection.Add(first); StubBehavior second = new StubBehavior(); behaviorCollection[0] = second; TestUtilities.AssertDetached(first); TestUtilities.AssertAttached(second, behaviorCollection.AssociatedObject); }
private static void OnBehaviorsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { BehaviorCollection oldCollection = (BehaviorCollection)args.OldValue; BehaviorCollection newCollection = (BehaviorCollection)args.NewValue; if (oldCollection == newCollection) { return; } if (oldCollection != null && oldCollection.AssociatedObject != null) { oldCollection.Detach(); } if (newCollection != null && sender != null) { newCollection.Attach(sender); } }
public void Detach_Attached_AllItemsDetached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Attach(new Button()); behaviorCollection.Detach(); Assert.IsNull(behaviorCollection.AssociatedObject, "The AssociatedObject should be null after Detach."); foreach (StubBehavior behavior in behaviorCollection) { TestUtilities.AssertDetached(behavior); } }
public void Attach_NonNullThenNull_ExceptionThrown() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Attach(new Button()); TestUtilities.AssertThrowsException(() => behaviorCollection.Attach(null)); }
public void Attach_MultipleTimeSameObject_AttachCalledOnce() { BehaviorCollection behaviorCollection = new BehaviorCollection() { new StubBehavior() }; Button button = new Button(); behaviorCollection.Attach(button); behaviorCollection.Attach(button); // This method hard codes AttachCount == 1. TestUtilities.AssertAttached((StubBehavior)behaviorCollection[0], button); }
public void Attach_MultipleObjects_ExceptionThrown() { BehaviorCollection behaviorCollection = new BehaviorCollection(); StubBehavior stub = new StubBehavior(); behaviorCollection.Attach(new Button()); TestUtilities.AssertThrowsException(() => behaviorCollection.Attach(new StackPanel())); }
public void Attach_Null_AttachNotCalledOnItems() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Attach(null); foreach (StubBehavior stub in behaviorCollection) { TestUtilities.AssertNotAttached(stub); } }
public void Attach_MultipleBehaviors_AllAttached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); Button button = new Button(); behaviorCollection.Attach(button); Assert.AreEqual(button, behaviorCollection.AssociatedObject, "Attach should set the AssociatedObject to the given parameter."); foreach (StubBehavior stub in behaviorCollection) { TestUtilities.AssertAttached(stub, button); } }
public void VectorChanged_ResetWhileAttached_AllDetached() { StubBehavior[] behaviorArray = { new StubBehavior(), new StubBehavior(), new StubBehavior() }; BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new Button()); foreach (StubBehavior behavior in behaviorArray) { behaviorCollection.Add(behavior); } behaviorCollection.Clear(); foreach (StubBehavior behavior in behaviorArray) { TestUtilities.AssertDetached(behavior); } }
public void VectorChanged_RemoveWhileAttached_Detached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new ToggleSwitch()); StubBehavior behavior = new StubBehavior(); behaviorCollection.Add(behavior); behaviorCollection.Remove(behavior); TestUtilities.AssertDetached(behavior); }