Exemple #1
0
        public void RaisesPropertyChangedOnParentValueSet()
        {
            using (new DefaultImplementationFixture())
            {
                var raised = 0;

                var parent = new DependencyObjectFake();
                var child  = new DependencyObjectFake();
                var childInheritanceSource = child.Component.GetValueSource <InheritanceValueSource>();

                var prop = Dependency.Property.RegisterAttached("AttachedProperty", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default", isInherited: true, propertyChangedCallback: (s, e) =>
                {
                    if (object.ReferenceEquals(s, child))
                    {
                        raised++;
                    }
                }));

                childInheritanceSource.ParentComponent = parent.Component;

                Assert.Equal("default", child.GetValue(prop));

                parent.SetValue(prop, "inherited");

                Assert.Equal("inherited", child.GetValue(prop));
                Assert.Equal(1, raised);
            }
        }
 public void InstansiateEmptyObject()
 {
     using (new DefaultImplementationFixture())
     {
         var obj = new DependencyObjectFake();
     }
 }
Exemple #3
0
        public void CanOverrideMetadataPropertyChangedCallback()
        {
            using (new DefaultImplementationFixture())
            {
                var raisedParent = new List <IDependencyObject>();
                var raisedChild  = new List <IDependencyObject>();

                var parent = new DependencyObjectFake();
                var child  = new SecondDependencyObjectFake();

                var prop = Dependency.Property.RegisterAttached("AttachedProperty", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default", isInherited: true, propertyChangedCallback: (s, e) =>
                {
                    raisedParent.Add(s);
                }));

                prop.OverrideMetadata(typeof(SecondDependencyObjectFake), new PropertyMetadata("overriden", propertyChangedCallback: (s, e) =>
                {
                    raisedChild.Add(s);
                }));

                var childInheritanceSource = child.Component.GetValueSource <InheritanceValueSource>();
                childInheritanceSource.ParentComponent = parent.Component;

                parent.SetValue(prop, "inherited");

                Assert.Collection(raisedParent,
                                  x => Assert.Same(parent, x),
                                  x => Assert.Same(child, x));

                Assert.Collection(raisedChild,
                                  x => Assert.Same(child, x));
            }
        }
        public void GetMemberPropertyDefaultValue()
        {
            using (new DefaultImplementationFixture())
            {
                var prop  = Dependency.Property.Register("IsEnabled", typeof(bool), typeof(DependencyObjectFake), new PropertyMetadata(true));
                var owner = new DependencyObjectFake();

                Assert.True((bool)owner.GetValue(prop));
            }
        }
        public void SetMemberReadOnlyPropertyValueViaDP()
        {
            using (new DefaultImplementationFixture())
            {
                var propKey = Dependency.Property.RegisterReadOnly("IsEnabled", typeof(bool), typeof(DependencyObjectFake), new PropertyMetadata(true));
                var prop    = propKey.DependencyProperty;
                var owner   = new DependencyObjectFake();

                Assert.Throws <InvalidOperationException>(() => owner.SetValue(prop, false));
            }
        }
        public void SetMemberReadOnlyPropertyValueViaDPKey()
        {
            using (new DefaultImplementationFixture())
            {
                var propKey = Dependency.Property.RegisterReadOnly("IsEnabled", typeof(bool), typeof(DependencyObjectFake), new PropertyMetadata(true));
                var owner   = new DependencyObjectFake();

                owner.SetValue(propKey, false);

                Assert.False((bool)owner.GetValue(propKey.DependencyProperty));
            }
        }
Exemple #7
0
        public void CanOverrideMetadataDefaultValue()
        {
            using (new DefaultImplementationFixture())
            {
                var parent = new DependencyObjectFake();
                var child  = new SecondDependencyObjectFake();

                var prop = Dependency.Property.RegisterAttached("AttachedProperty", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default", isInherited: true));
                prop.OverrideMetadata(typeof(SecondDependencyObjectFake), new PropertyMetadata("overriden"));

                var childInheritanceSource = child.Component.GetValueSource <InheritanceValueSource>();
                childInheritanceSource.ParentComponent = parent.Component;

                Assert.Equal("default", parent.GetValue(prop));
                Assert.Equal("overriden", child.GetValue(prop));
            }
        }
Exemple #8
0
        public void SetPropertyValue_SetTopSecond()
        {
            using (new DefaultImplementationFixture())
            {
                var member   = Dependency.Property.Register("Value", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default"));
                var attached = Dependency.Property.RegisterAttached("AttachedValue", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default"));
                var owner    = new DependencyObjectFake();
                var top      = owner.Component.GetValueSource <ValueStoreFake>();

                owner.SetValue(member, "local");
                top.SetValue(member, "top");

                owner.SetValue(attached, "local");
                top.SetValue(attached, "top");

                Assert.Equal("top", owner.GetValue(member));
                Assert.Equal("top", owner.GetValue(attached));
            }
        }
Exemple #9
0
        public void WontInheritNonInheritingPropertyValues()
        {
            using (new DefaultImplementationFixture())
            {
                var parent = new DependencyObjectFake();

                var child = new DependencyObjectFake();
                var childInheritanceSource = child.Component.GetValueSource <InheritanceValueSource>();

                var inheritingProp        = Dependency.Property.RegisterAttached("AttachedProperty", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default", isInherited: true));
                var nonInheritingAttached = Dependency.Property.RegisterAttached("NonInheritingAttachedProperty", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default"));
                var nonInheritingMember   = Dependency.Property.Register("NonInheritingMemberProperty", typeof(string), typeof(DependencyObjectFake), new PropertyMetadata("default"));

                parent.SetValue(inheritingProp, "inherited");
                parent.SetValue(nonInheritingAttached, "inherited");
                parent.SetValue(nonInheritingMember, "inherited");

                childInheritanceSource.ParentComponent = parent.Component;

                Assert.Equal("inherited", child.GetValue(inheritingProp));
                Assert.Equal("default", child.GetValue(nonInheritingAttached));
                Assert.Equal("default", child.GetValue(nonInheritingAttached));
            }
        }
Exemple #10
0
        public void PropertyChangedEventTestCases()
        {
            // https://docs.google.com/spreadsheets/d/1ubr8rybfcFehGWplfxLCxmEHj0n3dDhRVjOBtvGK-GI

            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = "Honeycomb.Xaml.Tests.Data.PropertyChangedEventTestCases.csv";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    var csv   = new CsvReader(reader);
                    var cases = csv.GetRecords <TestCase>();

                    foreach (var test in cases)
                    {
                        var expected = Convert.ToBoolean(test.Raise) ? 1 : 0;

                        using (new DefaultImplementationFixture())
                        {
                            var raised = 0;

                            Action <IDependencyObject, DependencyPropertyChangedEventArgs> handler = null;

                            var owner = new DependencyObjectFake();
                            var high  = owner.Component.GetValueSource <ValueStoreFake>();
                            var low   = owner.Component.GetValueSource <LocalValueStore>();

                            var prop = Dependency.Property.Register("Value", typeof(string), typeof(DependencyObjectFake),
                                                                    new PropertyMetadata(test.DEFAULT, (d, e) => handler?.Invoke(d, e)));

                            IValueStore changingStore;
                            IValueStore constantStore;
                            string      changeOperation;
                            string      constValue;

                            if (test.HIGH.Contains("->"))
                            {
                                changingStore   = high;
                                changeOperation = test.HIGH;
                                constantStore   = low;
                                constValue      = test.LOW;
                            }
                            else
                            {
                                changingStore   = low;
                                changeOperation = test.LOW;
                                constantStore   = high;
                                constValue      = test.HIGH;
                            }

                            var beforeAndAfter = changeOperation.Split("->").Select(x => x.Trim()).ToArray();

                            if (!string.IsNullOrEmpty(constValue))
                            {
                                constantStore.SetValue(prop, constValue);
                            }

                            if (!string.IsNullOrEmpty(beforeAndAfter[0]))
                            {
                                changingStore.SetValue(prop, beforeAndAfter[0]);
                            }

                            Assert.Equal(test.OldValue, owner.GetValue(prop));

                            handler = (d, e) =>
                            {
                                raised++;

                                Assert.Equal(test.OldValue, e.OldValue);
                                Assert.Equal(test.NewValue, e.NewValue);
                            };

                            if (string.IsNullOrEmpty(beforeAndAfter[1]))
                            {
                                changingStore.ClearValue(prop);
                            }
                            else
                            {
                                changingStore.SetValue(prop, beforeAndAfter[1]);
                            }

                            Assert.Equal(test.NewValue, owner.GetValue(prop));
                            Assert.Equal(expected, raised);
                        }
                    }
                }
        }