Example #1
1
        public void StoryboardTargetTest()
        {
            XamlNamespaces namespaces = new XamlNamespaces("http://schemas.microsoft.com/winfx/2006/xaml/presentation");

            ColorAnimation colorAnimation = new ColorAnimation { From = Colors.Green, To = Colors.Blue };
            Storyboard.SetTargetProperty(colorAnimation, PropertyPath.Parse("(Control.Background).(SolidColorBrush.Color)", namespaces));

            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(colorAnimation);

            TestRootClock rootClock = new TestRootClock();

            Control control = new Control();
            control.SetAnimatableRootClock(new AnimatableRootClock(rootClock, true));
            control.Background = new SolidColorBrush(Colors.Red);

            storyboard.Begin(control);

            rootClock.Tick(TimeSpan.FromSeconds(0));
            Assert.AreEqual(Colors.Green, ((SolidColorBrush)control.Background).Color);

            rootClock.Tick(TimeSpan.FromSeconds(0.5));
            Assert.IsTrue(Color.FromArgb(255, 0, (byte)(Colors.Green.G / 2), (byte)(Colors.Blue.B / 2)).IsClose(((SolidColorBrush)control.Background).Color));

            rootClock.Tick(TimeSpan.FromSeconds(1));
            Assert.AreEqual(Colors.Blue, ((SolidColorBrush)control.Background).Color);
        }
Example #2
0
        public void PropertyPathBasicTest()
        {
            XamlNamespaces namespaces = new XamlNamespaces(new[]
            {
                new NamespaceDeclaration("clr-namespace:Granular.Presentation.Tests;assembly=Granular.Presentation.Tests"),
                new NamespaceDeclaration("test", "clr-namespace:Granular.Presentation.Tests;assembly=Granular.Presentation.Tests")
            });

            PropertyPath path1 = PropertyPath.Parse("(test:PropertyPathTestElement.Child).PropertyName", namespaces);
            Assert.AreEqual(2, path1.Elements.Count());
            Assert.IsTrue(path1.Elements.ElementAt(0) is PropertyPathElement);
            Assert.AreEqual(new XamlName("PropertyPathTestElement.Child", "clr-namespace:Granular.Presentation.Tests;assembly=Granular.Presentation.Tests"), ((PropertyPathElement)path1.Elements.ElementAt(0)).PropertyName);
            Assert.IsTrue(path1.Elements.ElementAt(1) is PropertyPathElement);
            Assert.AreEqual(new XamlName("PropertyName", String.Empty), ((PropertyPathElement)path1.Elements.ElementAt(1)).PropertyName);

            PropertyPath path2 = PropertyPath.Parse("PropertyName[indexValue1, indexValue2]");
            Assert.IsTrue(path2.Elements.ElementAt(0) is IndexPropertyPathElement);
            Assert.AreEqual(new XamlName("PropertyName", String.Empty), ((IndexPropertyPathElement)path2.Elements.ElementAt(0)).PropertyName);
            CollectionAssert.AreEqual(new string[] { "indexValue1", "indexValue2" }, ((IndexPropertyPathElement)path2.Elements.ElementAt(0)).IndexRawValues.ToArray());

            PropertyPath path3 = PropertyPath.Parse("[indexValue1, indexValue2]");
            Assert.IsTrue(path3.Elements.ElementAt(0) is IndexPropertyPathElement);
            Assert.IsTrue(((IndexPropertyPathElement)path3.Elements.ElementAt(0)).PropertyName.IsEmpty);
            CollectionAssert.AreEqual(new string[] { "indexValue1", "indexValue2" }, ((IndexPropertyPathElement)path3.Elements.ElementAt(0)).IndexRawValues.ToArray());
        }
Example #3
0
 public XamlElement(XamlName name, XamlNamespaces namespaces, IEnumerable<XamlMember> members = null, IEnumerable<object> values = null, IEnumerable<XamlMember> directives = null)
     : base(name, namespaces)
 {
     this.Members = members ?? EmptyMembers;
     this.Values = values ?? EmptyValues;
     this.Directives = directives ?? EmptyDirectives;
 }
Example #4
0
        public void PropertyPathGetValueTest()
        {
            PropertyPathTestElement child2 = new PropertyPathTestElement { Value = 1 };
            PropertyPathTestElement child1 = new PropertyPathTestElement { Child = child2, Children = new TestCollection<PropertyPathTestElement> { child2 } };
            PropertyPathTestElement root = new PropertyPathTestElement { Child = child1, Children = new TestCollection<PropertyPathTestElement> { child1, child2 } };

            XamlNamespaces namespaces = new XamlNamespaces("clr-namespace:Granular.Presentation.Tests;assembly=Granular.Presentation.Tests");

            object value;

            Assert.IsTrue(PropertyPath.Parse("Value").TryGetValue(child2, out value));
            Assert.AreEqual(child2.Value, value);

            Assert.IsTrue(PropertyPath.Parse("[2, 3]").TryGetValue(child2, out value));
            Assert.AreEqual(child2.Value * 2 * 3, value);

            Assert.IsTrue(PropertyPath.Parse("Child.Child.Value").TryGetValue(root, out value));
            Assert.AreEqual(child2.Value, value);

            Assert.IsTrue(PropertyPath.Parse("Child.(PropertyPathTestElement.Child).Value", namespaces).TryGetValue(root, out value));
            Assert.AreEqual(child2.Value, value);

            Assert.IsTrue(PropertyPath.Parse("Children[1].Value").TryGetValue(root, out value));
            Assert.AreEqual(child2.Value, value);

            Assert.IsTrue(PropertyPath.Parse("(PropertyPathTestElement.Children)[1].Value", namespaces).TryGetValue(root, out value));
            Assert.AreEqual(child2.Value, value);
        }
Example #5
0
 public XamlElement(XamlName name, XamlNamespaces namespaces, IEnumerable<XamlAttribute> attributes = null, IEnumerable<XamlElement> children = null, string textValue = null)
     : base(name, namespaces)
 {
     this.Attributes = attributes ?? new XamlAttribute[0];
     this.Children = children ?? new XamlElement[0];
     this.TextValue = textValue ?? String.Empty;
 }
Example #6
0
        private static IEnumerable<XamlMember> CreateXamlMembers(XElement element, XamlNamespaces namespaces)
        {
            IEnumerable<XamlMember> attributeMembers = element.Attributes().Where(attribute => !IsDirective(attribute.Name) && !attribute.IsNamespaceDeclaration).Select(attribute => CreateXamlMember(attribute, namespaces));
            IEnumerable<XamlMember> elementMembers = element.Elements().Where(child => IsMemberName(child.Name)).Select(child => CreateXamlMember(child, namespaces));

            return attributeMembers.Concat(elementMembers).ToArray();
        }
Example #7
0
        private static XamlMember CreateXamlMember(XAttribute attribute, XamlNamespaces namespaces)
        {
            XamlName name = new XamlName(attribute.Name.LocalName, attribute.Name.NamespaceName.IsNullOrEmpty() ? namespaces.Get(String.Empty) : attribute.Name.NamespaceName);
            object value = (object)MarkupExtensionParser.Parse(attribute.Value, namespaces);

            return new XamlMember(name, namespaces, value);
        }
Example #8
0
        private static XamlElement CreateXamlElement(XElement element, XamlNamespaces namespaces)
        {
            IEnumerable<NamespaceDeclaration> elementNamespaces = element.Attributes().Where(attribute => attribute.IsNamespaceDeclaration).Select(attribute => new NamespaceDeclaration(GetNamespaceDeclarationPrefix(attribute), attribute.Value)).ToArray();
            if (elementNamespaces.Any())
            {
                namespaces = namespaces.Merge(elementNamespaces);
            }

            return new XamlElement(new XamlName(element.Name.LocalName, element.Name.NamespaceName), namespaces, CreateXamlMembers(element, namespaces), CreateValues(element, namespaces), CreateDirectives(element, namespaces));
        }
Example #9
0
        public void ParseEnumTest()
        {
            XamlNamespaces defaultNamespace = new XamlNamespaces("Granular.Presentation.Tests.Markup");
            EnumParseTestType value1 = (EnumParseTestType)TypeConverter.ConvertValue("Value1", typeof(EnumParseTestType), defaultNamespace);
            EnumParseTestType value2 = (EnumParseTestType)TypeConverter.ConvertValue("Value2", typeof(EnumParseTestType), defaultNamespace);
            EnumParseTestType value3 = (EnumParseTestType)TypeConverter.ConvertValue("Value3", typeof(EnumParseTestType), defaultNamespace);

            Assert.AreEqual(EnumParseTestType.Value1, value1);
            Assert.AreEqual(EnumParseTestType.Value2, value2);
            Assert.AreEqual(EnumParseTestType.Value3, value3);
        }
 public void ParseExplicitPropertiesTest()
 {
     XamlNamespaces namespaces = new XamlNamespaces(String.Empty, "default-namespace");
     XamlElement root1 = (XamlElement)MarkupExtensionParser.Parse("{root1 property1=value1, property2=value2}", namespaces);
     Assert.AreEqual("root1", root1.Name.LocalName);
     Assert.AreEqual(2, root1.Members.Count());
     Assert.AreEqual("property1", root1.Members.ElementAt(0).Name.LocalName);
     Assert.AreEqual("value1", root1.Members.ElementAt(0).Values.Single());
     Assert.AreEqual("property2", root1.Members.ElementAt(1).Name.LocalName);
     Assert.AreEqual("value2", root1.Members.ElementAt(1).Values.Single());
 }
 public void ParseImplicitPropertiesTest()
 {
     XamlNamespaces namespaces = new XamlNamespaces(String.Empty, "default-namespace");
     XamlElement root1 = (XamlElement)MarkupExtensionParser.Parse("{root1 value1, value2}", namespaces);
     Assert.AreEqual("root1", root1.Name.LocalName);
     Assert.AreEqual(2, root1.Attributes.Count());
     Assert.IsTrue(root1.Attributes.ElementAt(0).Name.IsEmpty);
     Assert.IsTrue(root1.Attributes.ElementAt(1).Name.IsEmpty);
     Assert.AreEqual("value1", root1.Attributes.ElementAt(0).Value);
     Assert.AreEqual("value2", root1.Attributes.ElementAt(1).Value);
 }
Example #12
0
        public static object ConvertValue(object value, Type type, XamlNamespaces namespaces)
        {
            object result;

            if (!TryConvertValue(value, type, namespaces, out result))
            {
                throw new Granular.Exception("Can't convert \"{0}\" to {1}", value, type.Name);
            }

            return result;
        }
        public static object Parse(string text, XamlNamespaces namespaces)
        {
            if (IsEscaped(text))
            {
                return GetEscapedText(text);
            }

            if (IsMarkupExtension(text))
            {
                return new MarkupExtensionParser(text, namespaces).Parse();
            }

            return text;
        }
Example #14
0
        private static object CreateValue(XNode node, XamlNamespaces namespaces)
        {
            if (node is XText)
            {
                return ((XText)node).Value.Trim();
            }

            if (node is XElement)
            {
                return CreateXamlElement((XElement)node, namespaces);
            }

            throw new Granular.Exception("Node \"{0}\" doesn't contain a value", node);
        }
Example #15
0
        private IPropertyPathElement MatchElement(XamlNamespaces namespaces)
        {
            VerifyTokensExists();

            XamlName propertyName = TryMatchPropertyName(namespaces);
            IEnumerable<string> indexRawValues = TryMatchIndexRawValues();

            if (propertyName.IsEmpty && !indexRawValues.Any())
            {
                throw new Granular.Exception("Can't parse \"{0}\", Property name or Index parameters were expected, \"{1}\" was found at index {2}", text, tokens.Peek().Value, tokens.Peek().Start);
            }

            return indexRawValues.Any() ? (IPropertyPathElement) new IndexPropertyPathElement(propertyName, indexRawValues, namespaces) : new PropertyPathElement(propertyName);
        }
Example #16
0
        private static XamlMember CreateXamlMember(XElement element, XamlNamespaces namespaces)
        {
            XamlName name = new XamlName(element.Name.LocalName, element.Name.NamespaceName.IsNullOrEmpty() ? namespaces.Get(String.Empty) : element.Name.NamespaceName);

            if (element.Attributes().Any())
            {
                throw new Granular.Exception("Member \"{0}\" cannot contain attributes", element.Name);
            }

            if (element.Elements().Any(child => IsMemberName(child.Name)))
            {
                throw new Granular.Exception("Member \"{0}\" cannot contain member elements", element.Name);
            }

            return new XamlMember(name, namespaces, CreateValues(element, namespaces));
        }
        public void ParseEscapedTextTest()
        {
            XamlNamespaces namespaces = new XamlNamespaces(String.Empty, "default-namespace");

            string value = (string)MarkupExtensionParser.Parse("{}{0}{1}{2}", namespaces);
            Assert.AreEqual("{0}{1}{2}", value);

            try
            {
                MarkupExtensionParser.Parse("{0}{1}{2}", namespaces);
                Assert.Fail();
            }
            catch
            {
                //
            }
        }
Example #18
0
        public void ParseTypeTest()
        {
            XamlNamespaces namespaces = new XamlNamespaces(new[]
            {
                new NamespaceDeclaration("clr-namespace:System"),
                new NamespaceDeclaration("s", "clr-namespace:System"),
            });

            Type type1 = (Type)TypeConverter.ConvertValue("Double", typeof(Type), namespaces);
            Type type2 = (Type)TypeConverter.ConvertValue("s:Double", typeof(Type), namespaces);
            Type type3 = TypeParser.ParseType("Double", namespaces);
            Type type4 = TypeParser.ParseType("s:Double", namespaces);

            Assert.AreEqual(typeof(Double), type1);
            Assert.AreEqual(typeof(Double), type2);
            Assert.AreEqual(typeof(Double), type3);
            Assert.AreEqual(typeof(Double), type4);
        }
Example #19
0
        public static bool TryConvertValue(object value, Type type, XamlNamespaces namespaces, out object result)
        {
            if (type.IsInstanceOfType(value))
            {
                result = value;
                return true;
            }

            ITypeConverter typeConverter = KnownTypes.GetTypeConverter(type);

            if (typeConverter != null)
            {
                result = typeConverter.ConvertFrom(namespaces, value);
                return true;
            }

            result = null;
            return false;
        }
Example #20
0
        private static XamlElement CreateXamlElement(XElement root, XamlNamespaces namespaces)
        {
            if (root.Nodes().OfType<XText>().Count() > 1)
            {
                throw new Granular.Exception("Xml cannot contain more than one text node");
            }

            IEnumerable<NamespaceDeclaration> elementNamespaces = root.Attributes().Where(attribute => attribute.IsNamespaceDeclaration).Select(attribute => new NamespaceDeclaration(GetNamespaceDeclarationPrefix(attribute), attribute.Value)).ToArray();
            if (elementNamespaces.Any())
            {
                namespaces = namespaces.Merge(elementNamespaces);
            }

            IEnumerable<XamlAttribute> attributes = root.Attributes().Where(attribute => !attribute.IsNamespaceDeclaration).Select(attribute => CreateXamlAttribute(attribute, namespaces)).ToArray();
            IEnumerable<XamlElement> elements = root.Elements().Select(element => CreateXamlElement(element, namespaces)).ToArray();

            string textValue = root.Nodes().OfType<XText>().Select(text => text.Value.Trim()).DefaultIfEmpty(String.Empty).First();

            return new XamlElement(new XamlName(root.Name.LocalName, root.Name.NamespaceName), namespaces, attributes, elements, textValue);
        }
        public void ParseChildrenTest()
        {
            XamlNamespaces namespaces = new XamlNamespaces(String.Empty, "default-namespace");
            XamlElement root1 = (XamlElement)MarkupExtensionParser.Parse("{root1 property1={child1 value1, property2=value2}, property3={child3 property4=value4}}", namespaces);

            Assert.AreEqual("root1", root1.Name.LocalName);
            Assert.AreEqual(2, root1.Attributes.Count());
            Assert.AreEqual("property1", root1.Attributes.ElementAt(0).Name.LocalName);

            Assert.IsTrue(root1.Attributes.ElementAt(0).Value is XamlElement);
            Assert.AreEqual("child1", (root1.Attributes.ElementAt(0).Value as XamlElement).Name.LocalName);
            Assert.IsTrue((root1.Attributes.ElementAt(0).Value as XamlElement).Attributes.Count() == 2);
            Assert.AreEqual("value1", (root1.Attributes.ElementAt(0).Value as XamlElement).Attributes.ElementAt(0).Value);
            Assert.AreEqual("value2", (root1.Attributes.ElementAt(0).Value as XamlElement).Attributes.ElementAt(1).Value);

            Assert.IsTrue(root1.Attributes.ElementAt(1).Value is XamlElement);
            Assert.AreEqual("child3", (root1.Attributes.ElementAt(1).Value as XamlElement).Name.LocalName);
            Assert.IsTrue((root1.Attributes.ElementAt(1).Value as XamlElement).Attributes.Count() == 1);
            Assert.AreEqual("property4", (root1.Attributes.ElementAt(1).Value as XamlElement).Attributes.ElementAt(0).Name.LocalName);
            Assert.AreEqual("value4", (root1.Attributes.ElementAt(1).Value as XamlElement).Attributes.ElementAt(0).Value);
        }
Example #22
0
        public static IElementInitializer Create(IPropertyAdapter propertyAdapter, IEnumerable<object> values, XamlNamespaces namespaces)
        {
            if (!values.Any())
            {
                return ElementInitializer.Empty;
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(propertyAdapter.PropertyType) &&
                !(values.Count() == 1 && values.First() is XamlElement && propertyAdapter.PropertyType.IsAssignableFrom(((XamlElement)values.First()).GetElementType())))
            {
                IElementInitializer propertyContentInitializer = ElementCollectionContentInitailizer.Create(values, propertyAdapter.PropertyType);

                // wrap with a factory that creates the collection (when it's null) before adding its values
                return new ElementPropertyMemberFactoryInitializer(propertyAdapter, propertyContentInitializer);
            }

            if (values.Count() == 1)
            {
                if (propertyAdapter.PropertyType == typeof(IFrameworkElementFactory))
                {
                    return new FrameworkElementFactoryInitializer(propertyAdapter, ElementFactory.FromValue(values.First(), null, namespaces));
                }

                IElementFactory contentFactory = ElementFactory.FromValue(values.First(), propertyAdapter.PropertyType, namespaces);
                return new ElementPropertyMemberInitializer(propertyAdapter, contentFactory);
            }

            throw new Granular.Exception("Member of type \"{0}\" cannot have more than one child", propertyAdapter.PropertyType.Name);
        }
Example #23
0
        public static IElementInitializer Create(XamlName memberName, Type containingType, IEnumerable<object> values, XamlNamespaces namespaces)
        {
            IEventAdapter eventAdapter = EventAdapter.CreateAdapter(containingType, memberName);
            if (eventAdapter != null)
            {
                return new ElementEventMemberInitializer(eventAdapter, GetEventHandlerName(memberName, values));
            }

            IPropertyAdapter propertyAdapter = PropertyAdapter.CreateAdapter(containingType, memberName);
            if (propertyAdapter != null)
            {
                return ElementPropertyMemberInitializer.Create(propertyAdapter, values, namespaces);
            }

            throw new Granular.Exception("Type \"{0}\" does not contain a member named \"{1}\"", containingType.Name, memberName);
        }
Example #24
0
        public ElementInitializer(XamlElement element)
        {
            elementType = element.GetElementType();
            namespaces = element.Namespaces;

            memberInitializers = CreateMemberInitializers(element);
            contentInitializer = CreateContentInitializer(element);

            nameDirectiveValue = GetNameDirectiveValue(element);
            nameProperty = GetNameProperty(element.GetElementType());
        }
Example #25
0
 public static bool TryParseType(string prefixedTypeName, XamlNamespaces namespaces, out Type type)
 {
     return TryParseType(XamlName.FromPrefixedName(prefixedTypeName, namespaces), out type);
 }
Example #26
0
 public static Type ParseType(string prefixedTypeName, XamlNamespaces namespaces)
 {
     return ParseType(XamlName.FromPrefixedName(prefixedTypeName, namespaces));
 }
Example #27
0
 public object ConvertFrom(XamlNamespaces namespaces, object value)
 {
     return PropertyPath.Parse((string)value, namespaces);
 }
Example #28
0
 public object ConvertFrom(XamlNamespaces namespaces, object value)
 {
     return new PropertyPathElement(XamlName.FromPrefixedName((string)value, namespaces));
 }
Example #29
0
 public static PropertyPath Parse(string value, XamlNamespaces namespaces = null)
 {
     PropertyPathParser parser = new PropertyPathParser(value, namespaces ?? XamlNamespaces.Empty);
     return new PropertyPath(parser.Parse());
 }
Example #30
0
 public IndexPropertyPathElement(XamlName propertyName, IEnumerable<string> indexRawValues, XamlNamespaces namespaces)
 {
     this.PropertyName = propertyName;
     this.IndexRawValues = indexRawValues;
     this.namespaces = namespaces;
 }
Example #31
0
        internal static XamlType LookupXamlType(string typeNamespace, string typeName)
        {
            if (XamlNamespaces.Contains(typeNamespace))
            {
                switch (typeName)
                {
                case "Array":
                case "ArrayExtension":
                    return(Array);

                case "Member":
                    return(Member);

                case "Null":
                case "NullExtension":
                    return(Null);

                case "Property":
                    return(Property);

                case "Reference":
                case "ReferenceExtension":
                    return(Reference);

                case "Static":
                case "StaticExtension":
                    return(Static);

                case "Type":
                case "TypeExtension":
                    return(Type);

                case "String":
                    return(String);

                case "Double":
                    return(Double);

                case "Int16":
                    return(Int16);

                case "Int32":
                    return(Int32);

                case "Int64":
                    return(Int64);

                case "Boolean":
                    return(Boolean);

                case "XData":
                    return(XData);

                case "Object":
                    return(Object);

                case "Char":
                    return(Char);

                case "Single":
                    return(Single);

                case "Byte":
                    return(Byte);

                case "Decimal":
                    return(Decimal);

                case "Uri":
                    return(Uri);

                case "TimeSpan":
                    return(TimeSpan);

                default:
                    return(null);
                }
            }
            return(null);
        }
Example #32
0
 public static bool ContainsDefault(this XamlNamespaces @this)
 {
     return(@this.Contains(String.Empty));
 }
Example #33
0
 public static string GetDefault(this XamlNamespaces @this)
 {
     return(@this.Get(String.Empty));
 }