public void ExtendedObservableCollection_CollectionChangedListenerTest() { using (var verify = new Verify()) { var result = new FooCollection(); var collectionChangedEventQueue = new Queue <Tuple <object, NotifyCollectionChangedEventArgs> >(); var collectionChangedListener = new Listener <NotifyCollectionChangedEventArgs>((s, e) => collectionChangedEventQueue.Enqueue(Tuple.Create(s, e))); result.AddHandler(collectionChangedListener); result.Add(new Foo()); verify.AreEqual(1, collectionChangedEventQueue.Count, "event didn't fire"); var event1 = collectionChangedEventQueue.Dequeue(); verify.AreSame(result, event1.Item1, "event has the wrong item"); verify.AreEqual(NotifyCollectionChangedAction.Add, event1.Item2.Action, "event as the wrong action"); result.RemoveHandler(collectionChangedListener); result.Add(new Foo()); verify.AreEqual(0, collectionChangedEventQueue.Count, "event handler wasn't removed"); } }
public void ExtendedObservableCollection_PropertyChangedListenerTest() { using (var verify = new Verify()) { var result = new FooCollection(); var propertyChangedEventQueue = new Queue <Tuple <object, PropertyChangedEventArgs> >(); var propertyChangedListener = new Listener <PropertyChangedEventArgs>((s, e) => propertyChangedEventQueue.Enqueue(Tuple.Create(s, e))); result.AddHandler(propertyChangedListener); result.Boom += 1; verify.AreEqual(1, propertyChangedEventQueue.Count, "event didn't fire"); var event1 = propertyChangedEventQueue.Dequeue(); verify.AreSame(result, event1.Item1, "event has the wrong item"); verify.AreEqual("Boom", event1.Item2.PropertyName, "event as the wrong parameter name"); result.RemoveHandler(propertyChangedListener); result.Boom += 1; verify.AreEqual(0, propertyChangedEventQueue.Count, "event handler wasn't removed"); } }
public void TestTwoWayExitBehavior() { // Allow the exit to reside in two places, and place it in both room A and room B. this.exit.Behaviors.Add(new MultipleParentsBehavior()); this.roomA.Add(this.exit); this.roomB.Add(this.exit); // Ensure there are no destinations before rigging them up. Verify.AreSame(this.exitBehavior.GetDestination(this.roomA), null); Verify.AreSame(this.exitBehavior.GetDestination(this.roomB), null); // Rig the exits and ensure both got rigged up to the correct destinations. this.exitBehavior.AddDestination("north", this.roomB.ID); this.exitBehavior.AddDestination("south", this.roomA.ID); Verify.AreSame(this.exitBehavior.GetDestination(this.roomA), this.roomB); Verify.AreSame(this.exitBehavior.GetDestination(this.roomB), this.roomA); // Create an unmovable actor, and ensure that said actor cannot move through. this.actor = new Thing() { Name = "Actor", Parent = this.roomA, ID = TestThingID.Generate("testactor") }; this.exitBehavior.MoveThrough(this.actor); Verify.AreSame(this.actor.Parent, this.roomA); // Make the actoc movable, and ensure they end up in the next room when moving through. this.actor.Behaviors.Add(new MovableBehavior()); this.exitBehavior.MoveThrough(this.actor); Verify.AreSame(this.actor.Parent, this.roomB); // Move the actor back through the exit again, and ensure they end up in the starting room. this.exitBehavior.MoveThrough(this.actor); Verify.AreSame(this.actor.Parent, this.roomA); // @@@ TODO: Ensure the actor does not move through when using the wrong context command? // @@@ TODO: Ensure the actor moves through both ways when using correct context commands? }
public void ValidateRecycledElementOwnerAffinity() { RunOnUIThread.Execute(() => { ItemsRepeater repeater1 = null; ItemsRepeater repeater2 = null; const int numItems = 10; var dataCollection = new ObservableCollection <int>(Enumerable.Range(0, numItems)); const string recycleKey = "key"; var dataSource = MockItemsSource.CreateDataSource <int>(dataCollection, true); var layout = new StackLayout(); var recyclePool = new RecyclePool(); var itemTemplate = (DataTemplate)XamlReader.Parse( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='{Binding}' /> </DataTemplate>"); repeater1 = new ItemsRepeater() { ItemsSource = dataSource, Layout = layout, ItemTemplate = new RecyclingElementFactoryDerived() { Templates = { { "key", itemTemplate } }, RecyclePool = recyclePool, SelectTemplateIdFunc = (object data, UIElement owner) => recycleKey } }; repeater2 = new ItemsRepeater() { ItemsSource = dataSource, Layout = layout, ItemTemplate = new RecyclingElementFactoryDerived() { Templates = { { "key", itemTemplate } }, RecyclePool = recyclePool, SelectTemplateIdFunc = (object data, UIElement owner) => recycleKey } }; var root = new StackPanel(); root.Children.Add(repeater1); root.Children.Add(repeater2); Content = new ItemsRepeaterScrollHost() { Width = 400, Height = 400, ScrollViewer = new ScrollViewer() { Content = root } }; Content.UpdateLayout(); Verify.AreEqual(numItems, VisualTreeHelper.GetChildrenCount(repeater1)); Verify.AreEqual(numItems, VisualTreeHelper.GetChildrenCount(repeater2)); // Throw all the elements into the recycle pool dataCollection.Clear(); Content.UpdateLayout(); for (int i = 0; i < numItems; i++) { var element1 = (FrameworkElement)recyclePool.TryGetElement(recycleKey, repeater1); Verify.AreSame(repeater1, element1.Parent); var element2 = (FrameworkElement)recyclePool.TryGetElement(recycleKey, repeater2); Verify.AreSame(repeater2, element2.Parent); } }); }
[TestProperty("TestPass:IncludeOnlyOn", "Desktop")] // TeachingTip doesn't appear to show up correctly in OneCore. public void TeachingTipBackgroundTest() { TeachingTip teachingTip = null, teachingTipLightDismiss = null; SolidColorBrush blueBrush = null; Brush lightDismissBackgroundBrush = null; var loadedEvent = new AutoResetEvent(false); RunOnUIThread.Execute(() => { Grid root = new Grid(); teachingTip = new TeachingTip(); teachingTip.Loaded += (object sender, RoutedEventArgs args) => { loadedEvent.Set(); }; teachingTipLightDismiss = new TeachingTip(); teachingTipLightDismiss.IsLightDismissEnabled = true; // Set LightDismiss background before show... it shouldn't take effect in the tree blueBrush = new SolidColorBrush(Colors.Blue); teachingTipLightDismiss.Background = blueBrush; root.Resources.Add("TeachingTip", teachingTip); root.Resources.Add("TeachingTipLightDismiss", teachingTipLightDismiss); lightDismissBackgroundBrush = MUXControlsTestApp.App.Current.Resources["TeachingTipTransientBackground"] as Brush; Verify.IsNotNull(lightDismissBackgroundBrush, "lightDismissBackgroundBrush"); teachingTip.IsOpen = true; teachingTipLightDismiss.IsOpen = true; MUXControlsTestApp.App.TestContentRoot = root; }); IdleSynchronizer.Wait(); loadedEvent.WaitOne(); IdleSynchronizer.Wait(); RunOnUIThread.Execute(() => { var redBrush = new SolidColorBrush(Colors.Red); teachingTip.SetValue(TeachingTip.BackgroundProperty, redBrush); Verify.AreSame(redBrush, teachingTip.GetValue(TeachingTip.BackgroundProperty) as Brush); Verify.AreSame(redBrush, teachingTip.Background); teachingTip.Background = blueBrush; Verify.AreSame(blueBrush, teachingTip.Background); { var popup = TeachingTipTestHooks.GetPopup(teachingTip); var child = popup.Child; var grandChild = VisualTreeHelper.GetChild(child, 0); Verify.AreSame(blueBrush, ((Grid)grandChild).Background, "Checking TeachingTip.Background TemplateBinding works"); } { var popup = TeachingTipTestHooks.GetPopup(teachingTipLightDismiss); var child = popup.Child; var grandChild = VisualTreeHelper.GetChild(child, 0); var actualBrush = ((Grid)grandChild).Background; Log.Comment("Checking LightDismiss TeachingTip Background is using resource for first invocation"); if (lightDismissBackgroundBrush != actualBrush) { if (actualBrush is SolidColorBrush actualSolidBrush) { string teachingTipMessage = $"LightDismiss TeachingTip Background is SolidColorBrush with color {actualSolidBrush.Color}"; Log.Comment(teachingTipMessage); Verify.Fail(teachingTipMessage); } else { Verify.AreSame(lightDismissBackgroundBrush, actualBrush, "Checking LightDismiss TeachingTip Background is using resource for first invocation"); } } } teachingTip.IsLightDismissEnabled = true; }); IdleSynchronizer.Wait(); RunOnUIThread.Execute(() => { Verify.AreEqual(blueBrush.Color, ((SolidColorBrush)teachingTip.Background).Color); var popup = TeachingTipTestHooks.GetPopup(teachingTip); var child = popup.Child as Grid; var grandChild = VisualTreeHelper.GetChild(child, 0); var grandChildBackgroundBrush = ((Grid)grandChild).Background; //If we can no longer cast the background brush to a solid color brush then changing the //IsLightDismissEnabled has changed the background as we expected it to. if (grandChildBackgroundBrush is SolidColorBrush) { Verify.AreNotEqual(blueBrush.Color, ((SolidColorBrush)grandChildBackgroundBrush).Color); } }); }
public void ValidateRecycling() { RunOnUIThread.Execute(() => { var elementFactory = new RecyclingElementFactory() { RecyclePool = new RecyclePool(), }; elementFactory.Templates["even"] = (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='even' /> </DataTemplate>" ); elementFactory.Templates["odd"] = (DataTemplate)XamlReader.Load( @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <TextBlock Text='odd' /> </DataTemplate>" ); elementFactory.SelectTemplateKey += delegate(RecyclingElementFactory sender, SelectTemplateEventArgs args) { args.TemplateKey = ((int)args.DataContext % 2 == 0) ? "even" : "odd"; }; const int numItems = 10; ItemsRepeater repeater = new ItemsRepeater() { ItemsSource = Enumerable.Range(0, numItems), ItemTemplate = elementFactory, }; var context = (ElementFactoryGetArgs)RepeaterTestHooks.CreateRepeaterElementFactoryGetArgs(); context.Parent = repeater; var clearContext = (ElementFactoryRecycleArgs)RepeaterTestHooks.CreateRepeaterElementFactoryRecycleArgs(); clearContext.Parent = repeater; // Element0 is of type even, a new one should be created context.Data = 0; var element0 = elementFactory.GetElement(context); Verify.IsNotNull(element0); Verify.AreEqual("even", (element0 as TextBlock).Text); clearContext.Element = element0; elementFactory.RecycleElement(clearContext); // Element1 is of type odd, a new one should be created context.Data = 1; var element1 = elementFactory.GetElement(context); Verify.IsNotNull(element1); Verify.AreNotSame(element0, element1); Verify.AreEqual("odd", (element1 as TextBlock).Text); clearContext.Element = element1; elementFactory.RecycleElement(clearContext); // Element0 should be recycled for element2 context.Data = 2; var element2 = elementFactory.GetElement(context); Verify.AreEqual("even", (element2 as TextBlock).Text); Verify.AreSame(element0, element2); // Element1 should be recycled for element3 context.Data = 3; var element3 = elementFactory.GetElement(context); Verify.AreEqual("odd", (element3 as TextBlock).Text); Verify.AreSame(element1, element3); }); }
private static void ConstructorAssertions(Verify verify, ObservableCollectionExtended<object> result, WeakNotifier weakNotifier, Notifier notifier) { var itemAssert = new ItemPropertyChangedEventTest(verify, result); var itemAddedEventQueue = new Queue<Tuple<object, ItemEventArgs<object>>>(); result.ItemAdded += (s, e) => itemAddedEventQueue.Enqueue(Tuple.Create(s, e)); var itemRemovedEventQueue = new Queue<Tuple<object, ItemEventArgs<object>>>(); result.ItemRemoved += (s, e) => itemRemovedEventQueue.Enqueue(Tuple.Create(s, e)); weakNotifier.Age += 1; itemAssert.ExpectEvent(weakNotifier, "Age"); notifier.Age += 1; itemAssert.ExpectEvent(notifier, "Age"); var oldCount = result.Count; result.Clear(); verify.AreEqual(oldCount, itemRemovedEventQueue.Count, "incorrect number of items reported as being removed"); weakNotifier.Age += 1; notifier.Age += 1; verify.AreEqual(0, result.Count, "The collection should be empty"); itemAssert.ExpectCountEquals(0, "ItemPropertyChanged events were fired when the collection was supposed to be empty."); result.Add(weakNotifier); result.Add(notifier); verify.AreEqual(2, result.Count, "The new items were not added"); verify.AreEqual(2, itemAddedEventQueue.Count, "The new item events didn't fire"); CollectionAssert.AreEqual(result, result.ReadOnlyWrapper); itemRemovedEventQueue.Clear(); result.RemoveAt(0); verify.AreEqual(1, itemRemovedEventQueue.Count, "the item wasn't removed"); verify.AreSame(notifier, result[0], "the wrong item was removed"); CollectionAssert.AreEqual(result, result.ReadOnlyWrapper); itemAddedEventQueue.Clear(); itemRemovedEventQueue.Clear(); result[0] = weakNotifier; verify.AreSame(weakNotifier, result[0], "the item wasn't updated"); verify.AreEqual(1, itemAddedEventQueue.Count, "the add event for replacing an item didn't fire"); verify.AreEqual(1, itemRemovedEventQueue.Count, "the remove event for replacing an item didn't fire"); CollectionAssert.AreEqual(result, result.ReadOnlyWrapper); }
public void ExtendedObservableCollection_CollectionChangedListenerTest() { using (var verify = new Verify()) { var result = new FooCollection(); var collectionChangedEventQueue = new Queue<Tuple<object, NotifyCollectionChangedEventArgs>>(); var collectionChangedListener = new Listener<NotifyCollectionChangedEventArgs>((s, e) => collectionChangedEventQueue.Enqueue(Tuple.Create(s, e))); result.AddHandler(collectionChangedListener); result.Add(new Foo()); verify.AreEqual(1, collectionChangedEventQueue.Count, "event didn't fire"); var event1 = collectionChangedEventQueue.Dequeue(); verify.AreSame(result, event1.Item1, "event has the wrong item"); verify.AreEqual(NotifyCollectionChangedAction.Add, event1.Item2.Action, "event as the wrong action"); result.RemoveHandler(collectionChangedListener); result.Add(new Foo()); verify.AreEqual(0, collectionChangedEventQueue.Count, "event handler wasn't removed"); } }
public void ExtendedObservableCollection_ItemPropertyChangedListenerTest2() { using (var verify = new Verify()) { var result = new FooCollection(); var itemPropertyChangedEventQueue = new Queue<Tuple<object, RelayedEventArgs<PropertyChangedEventArgs>>>(); var itemPropertyChangedListener = new Listener<RelayedEventArgs<PropertyChangedEventArgs>>((s, e) => itemPropertyChangedEventQueue.Enqueue(Tuple.Create(s, e))); var mutator = new Foo(); result.Add(mutator); result.AddHandler(itemPropertyChangedListener); mutator.FooBar = "AAA"; verify.AreEqual(1, itemPropertyChangedEventQueue.Count, "event didn't fire"); var event1 = itemPropertyChangedEventQueue.Dequeue(); verify.AreSame(result, event1.Item1, "event has the wrong item"); verify.AreEqual<object>(mutator, event1.Item2.OriginalSender, "event has the wrong original sender"); verify.AreEqual("FooBar", event1.Item2.EventArgs.PropertyName, "event as the wrong parameter name"); result.Remove(mutator); mutator.FooBar = "BBB"; verify.AreEqual(0, itemPropertyChangedEventQueue.Count, "event handler wasn't removed"); } }
public void ExtendedObservableCollection_PropertyChangedListenerTest() { using (var verify = new Verify()) { var result = new FooCollection(); var propertyChangedEventQueue = new Queue<Tuple<object, PropertyChangedEventArgs>>(); var propertyChangedListener = new Listener<PropertyChangedEventArgs>((s, e) => propertyChangedEventQueue.Enqueue(Tuple.Create(s, e))); result.AddHandler(propertyChangedListener); result.Boom += 1; verify.AreEqual(1, propertyChangedEventQueue.Count, "event didn't fire"); var event1 = propertyChangedEventQueue.Dequeue(); verify.AreSame(result, event1.Item1, "event has the wrong item"); verify.AreEqual("Boom", event1.Item2.PropertyName, "event as the wrong parameter name"); result.RemoveHandler(propertyChangedListener); result.Boom += 1; verify.AreEqual(0, propertyChangedEventQueue.Count, "event handler wasn't removed"); } }
public void TestAreSameSame() { string s = "test"; Verify.AreSame(s, s); }
public void MetadataCache_Test14B() { using (var verify = new Verify()) { var result = MetadataCache.GetMetadata(typeof(StringIndexedMock)); var x = result.Properties["Item [System.String]"]; verify.IsNotNull(x, "Item [System.String] property is missing"); var y = result.Properties["Item[]"]; verify.IsNotNull(y, "Item[] property is missing"); verify.AreSame(x, y, "Item[] and Item [System.String] don't match"); } }
public void MetadataCache_Test14() { using (var verify = new Verify()) { var result = MetadataCache.GetMetadata(typeof(List<int>)); foreach (var item in result.Properties) { Debug.WriteLine(item.Name); } var x = result.Properties["Item [Int32]"]; verify.IsNotNull(x, "Item [Int32] property is missing"); var y = result.Properties["Item[]"]; verify.IsNotNull(y, "Item[] property is missing"); verify.AreSame(x, y, "Item[] and Item [Int32] don't match"); } }
public void MetadataCache_ReflexiveTest2() { using (var verify = new Verify()) { var fromTypeInfo = MetadataCache.GetMetadata(typeof(Mock).GetTypeInfo()); var fromType = MetadataCache.GetMetadata(typeof(Mock)); verify.AreSame(fromType, fromTypeInfo, "From TypeInfo was not cached"); } }
public void TestAreSameSame() { var s = "test"; Verify.AreSame(s, s); }