Example #1
0
        public void OfType_Matches_Control_Of_Correct_Type()
        {
            var control = new Control1();
            var target = new Selector().OfType<Control1>();

            Assert.True(target.Match(control).ImmediateResult);
        }
Example #2
0
        public void OfType_Matches_Control_With_TemplatedParent()
        {
            var control = new Control1 { TemplatedParent = new Mock<ITemplatedControl>().Object };
            var target = new Selector().OfType<Control1>();

            Assert.True(target.Match(control).ImmediateResult);
        }
Example #3
0
        public void Child_Doesnt_Match_Control_When_It_Has_No_Parent()
        {
            var control = new TestLogical3();
            var selector = new Selector().OfType<TestLogical1>().Child().OfType<TestLogical3>();

            Assert.False(selector.Match(control).ImmediateResult);
        }
Example #4
0
        public void OfType_Doesnt_Match_Control_Of_Wrong_Type()
        {
            var control = new Control2();
            var target = new Selector().OfType<Control1>();

            Assert.False(target.Match(control).ImmediateResult);
        }
Example #5
0
        public void Name_Matches_Control_With_Correct_Name()
        {
            var control = new Control1 { Name = "foo" };
            var target = new Selector().Name("foo");

            Assert.True(target.Match(control).ImmediateResult);
        }
Example #6
0
        public void Name_Doesnt_Match_Control_Of_Wrong_Name()
        {
            var control = new Control1 { Name = "foo" };
            var target = new Selector().Name("bar");

            Assert.False(target.Match(control).ImmediateResult);
        }
Example #7
0
        public void Name_Doesnt_Match_Control_With_TemplatedParent()
        {
            var control = new Control1 { TemplatedParent = new Mock<ITemplatedControl>().Object };
            var target = new Selector().Name("foo");
            var activator = target.Match(control);

            Assert.False(target.Match(control).ImmediateResult);
        }
Example #8
0
        public void Child_Matches_Control_When_It_Is_Child_OfType()
        {
            var parent = new TestLogical1();
            var child = new TestLogical2();

            child.LogicalParent = parent;

            var selector = new Selector().OfType<TestLogical1>().Child().OfType<TestLogical2>();

            Assert.True(selector.Match(child).ImmediateResult);
        }
Example #9
0
        public async Task Class_Tracks_Additions()
        {
            var control = new Control1();

            var target = new Selector().Class("foo");
            var activator = target.Match(control).ObservableResult;

            Assert.False(await activator.Take(1));
            control.Classes.Add("foo");
            Assert.True(await activator.Take(1));
        }
Example #10
0
        public async Task Class_Doesnt_Match_Control_Without_Class()
        {
            var control = new Control1
            {
                Classes = new Classes { "bar" },
            };

            var target = new Selector().Class("foo");
            var activator = target.Match(control).ObservableResult;

            Assert.False(await activator.Take(1));
        }
        public async Task PropertyEquals_Matches_When_Property_Has_Matching_Value()
        {
            var control = new TextBlock();
            var target = new Selector().PropertyEquals(TextBlock.TextProperty, "foo");
            var activator = target.Match(control).ObservableResult;

            Assert.False(await activator.Take(1));
            control.Text = "foo";
            Assert.True(await activator.Take(1));
            control.Text = null;
            Assert.False(await activator.Take(1));
        }
        public void Descendent_Matches_Control_When_It_Is_Descendent_OfType()
        {
            var grandparent = new TestLogical1();
            var parent = new TestLogical2();
            var child = new TestLogical3();

            parent.LogicalParent = grandparent;
            child.LogicalParent = parent;

            var selector = new Selector().OfType<TestLogical1>().Descendent().OfType<TestLogical3>();

            Assert.True(selector.Match(child).ImmediateResult);
        }
Example #13
0
        public void Child_Doesnt_Match_Control_When_It_Is_Grandchild_OfType()
        {
            var grandparent = new TestLogical1();
            var parent = new TestLogical2();
            var child = new TestLogical3();

            parent.LogicalParent = grandparent;
            child.LogicalParent = parent;

            var selector = new Selector().OfType<TestLogical1>().Child().OfType<TestLogical3>();

            Assert.False(selector.Match(child).ImmediateResult);
        }
Example #14
0
        public async Task Class_Matches_Control_With_TemplatedParent()
        {
            var control = new Control1
            {
                Classes = new Classes { "foo" },
                TemplatedParent = new Mock<ITemplatedControl>().Object,
            };

            var target = new Selector().Class("foo");
            var activator = target.Match(control).ObservableResult;

            Assert.True(await activator.Take(1));
        }
Example #15
0
        public async Task Class_Tracks_Removals()
        {
            var control = new Control1
            {
                Classes = new Classes { "foo" },
            };

            var target = new Selector().Class("foo");
            var activator = target.Match(control).ObservableResult;

            Assert.True(await activator.Take(1));
            control.Classes.Remove("foo");
            Assert.False(await activator.Take(1));
        }
        public async Task Descendent_Matches_Control_When_It_Is_Descendent_OfType_And_Class()
        {
            var grandparent = new TestLogical1();
            var parent = new TestLogical2();
            var child = new TestLogical3();

            grandparent.Classes.Add("foo");
            parent.LogicalParent = grandparent;
            child.LogicalParent = parent;

            var selector = new Selector().OfType<TestLogical1>().Class("foo").Descendent().OfType<TestLogical3>();
            var activator = selector.Match(child).ObservableResult;

            Assert.True(await activator.Take(1));
        }
Example #17
0
        public Selector(
            Selector previous,
            Func<IStyleable, SelectorMatch> evaluate,
            string selectorString,
            bool inTemplate = false,
            bool stopTraversal = false)
            : this()
        {
            Contract.Requires<ArgumentNullException>(previous != null);

            Previous = previous;
            _evaluate = evaluate;
            SelectorString = selectorString;
            _inTemplate = inTemplate || previous._inTemplate;
            _stopTraversal = stopTraversal;
        }
Example #18
0
        public async Task Child_Matches_Control_When_It_Is_Child_OfType_And_Class()
        {
            var parent = new TestLogical1();
            var child = new TestLogical2();

            child.LogicalParent = parent;

            var selector = new Selector().OfType<TestLogical1>().Class("foo").Child().OfType<TestLogical2>();
            var activator = selector.Match(child).ObservableResult;

            Assert.False(await activator.Take(1));
            parent.Classes.Add("foo");
            Assert.True(await activator.Take(1));
            parent.Classes.Remove("foo");
            Assert.False(await activator.Take(1));
        }
Example #19
0
        /// <summary>
        /// Parses a <see cref="Selector"/> from a string.
        /// </summary>
        /// <param name="s">The string.</param>
        /// <returns>The parsed selector.</returns>
        public Selector Parse(string s)
        {
            var syntax = SelectorGrammar.Selector.Parse(s);
            var result = new Selector();

            foreach (var i in syntax)
            {
                var ofType = i as SelectorGrammar.OfTypeSyntax;
                var @class = i as SelectorGrammar.ClassSyntax;
                var name = i as SelectorGrammar.NameSyntax;
                var property = i as SelectorGrammar.PropertySyntax;
                var child = i as SelectorGrammar.ChildSyntax;
                var descendent = i as SelectorGrammar.DescendentSyntax;
                var template = i as SelectorGrammar.TemplateSyntax;

                if (ofType != null)
                {
                    result = result.OfType(_typeResolver(ofType.TypeName, ofType.Xmlns));
                }
                else if (@class != null)
                {
                    result = result.Class(@class.Class);
                }
                else if (name != null)
                {
                    result = result.Name(name.Name);
                }
                else if (property != null)
                {
                    throw new NotImplementedException();
                }
                else if (child != null)
                {
                    result = result.Child();
                }
                else if (descendent != null)
                {
                    result = result.Descendent();
                }
                else if (template != null)
                {
                    result = result.Template();
                }
            }

            return result;
        }
        public void Template_Child_Of_Control_With_Two_Classes()
        {
            var template = new ControlTemplate(parent =>
            {
                return new Border
                {
                    Name = "border",
                };
            });

            var control = new Button
            {
                Template = template,
            };

            control.ApplyTemplate();

            var selector = new Selector()
                .OfType<Button>()
                .Class("foo")
                .Class("bar")
                .Template()
                .Name("border");

            var border = (Border)((IVisual)control).VisualChildren.Single();
            var values = new List<bool>();
            var activator = selector.Match(border).ObservableResult;

            activator.Subscribe(x => values.Add(x));

            Assert.Equal(new[] { false }, values);
            control.Classes.Add("foo", "bar");
            Assert.Equal(new[] { false, true }, values);
            control.Classes.Remove("foo");
            Assert.Equal(new[] { false, true, false }, values);
        }
Example #21
0
        public void PesudoClass_Selector_Should_Have_Correct_String_Representation()
        {
            var target = new Selector().Class(":foo");

            Assert.Equal(":foo", target.ToString());
        }
Example #22
0
        public async Task Multiple_Classes()
        {
            var control = new Control1();
            var target = new Selector().Class("foo").Class("bar");
            var activator = target.Match(control).ObservableResult;

            Assert.False(await activator.Take(1));
            control.Classes.Add("foo");
            Assert.False(await activator.Take(1));
            control.Classes.Add("bar");
            Assert.True(await activator.Take(1));
            control.Classes.Remove("bar");
            Assert.False(await activator.Take(1));
        }
Example #23
0
        /// <summary>
        /// Parses a <see cref="Selector"/> from a string.
        /// </summary>
        /// <param name="s">The string.</param>
        /// <returns>The parsed selector.</returns>
        public Selector Parse(string s)
        {
            var syntax = SelectorGrammar.Selector.Parse(s);
            var result = new Selector();

            foreach (var i in syntax)
            {
                var ofType = i as SelectorGrammar.OfTypeSyntax;
                var @is = i as SelectorGrammar.IsSyntax;
                var @class = i as SelectorGrammar.ClassSyntax;
                var name = i as SelectorGrammar.NameSyntax;
                var property = i as SelectorGrammar.PropertySyntax;
                var child = i as SelectorGrammar.ChildSyntax;
                var descendent = i as SelectorGrammar.DescendentSyntax;
                var template = i as SelectorGrammar.TemplateSyntax;

                if (ofType != null)
                {
                    result = result.OfType(_typeResolver(ofType.TypeName, ofType.Xmlns));
                }
                if (@is != null)
                {
                    result = result.Is(_typeResolver(@is.TypeName, @is.Xmlns));
                }
                else if (@class != null)
                {
                    result = result.Class(@class.Class);
                }
                else if (name != null)
                {
                    result = result.Name(name.Name);
                }
                else if (property != null)
                {
                    var type = result.TargetType;

                    if (type == null)
                    {
                        throw new InvalidOperationException("Property selectors must be applied to a type.");
                    }

                    var targetProperty = PerspexPropertyRegistry.Instance.FindRegistered(type, property.Property);

                    if (targetProperty == null)
                    {
                        throw new InvalidOperationException($"Cannot find '{property.Property}' on '{type}");
                    }

                    object typedValue;

                    if (TypeUtilities.TryConvert(
                            targetProperty.PropertyType, 
                            property.Value, 
                            CultureInfo.InvariantCulture,
                            out typedValue))
                    {
                        result = result.PropertyEquals(targetProperty, typedValue);
                    }
                    else
                    {
                        throw new InvalidOperationException(
                            $"Could not convert '{property.Value}' to '{targetProperty.PropertyType}");
                    }
                }
                else if (child != null)
                {
                    result = result.Child();
                }
                else if (descendent != null)
                {
                    result = result.Descendent();
                }
                else if (template != null)
                {
                    result = result.Template();
                }
            }

            return result;
        }
        public async Task Descendent_Matches_Any_Ancestor()
        {
            var grandparent = new TestLogical1();
            var parent = new TestLogical1();
            var child = new TestLogical3();

            parent.LogicalParent = grandparent;
            child.LogicalParent = parent;

            var selector = new Selector().OfType<TestLogical1>().Class("foo").Descendent().OfType<TestLogical3>();
            var activator = selector.Match(child).ObservableResult;

            Assert.False(await activator.Take(1));
            parent.Classes.Add("foo");
            Assert.True(await activator.Take(1));
            grandparent.Classes.Add("foo");
            Assert.True(await activator.Take(1));
            parent.Classes.Remove("foo");
            Assert.True(await activator.Take(1));
            grandparent.Classes.Remove("foo");
            Assert.False(await activator.Take(1));
        }