Ejemplo n.º 1
0
        static void TestEffectProperties(TypeInfo effectType, IGraphicsEffect effect)
        {
            var properties = (from property in effectType.DeclaredProperties
                              where property.Name != "Name"
                              where property.Name != "Sources"
                              where property.PropertyType != typeof(IGraphicsEffectSource)
                              select property).ToList();

            IList <object> effectProperties = new ViewIndexerAsList <object>(() => EffectAccessor.GetPropertyCount(effect),
                                                                             i => EffectAccessor.GetProperty(effect, i));

            FilterOutCustomizedEffectProperties(effectType.AsType(), ref properties, ref effectProperties);

            // Should have the same number of strongly typed properties as elements in the properties collection.
            Assert.AreEqual(properties.Count, effectProperties.Count);

            // Store the initial property values.
            var initialValues = effectProperties.ToList();

            var whichIndexIsProperty = new List <int>();

            // Changing strongly typed properties should change the properties collection.
            for (int i = 0; i < effectProperties.Count; i++)
            {
                object testValue1 = GetArbitraryTestValue(properties[i].PropertyType, true);
                object testValue2 = GetArbitraryTestValue(properties[i].PropertyType, false);

                // Change a property value, and see which collection properties match the result.
                properties[i].SetValue(effect, testValue1);

                var matches1 = (from j in Enumerable.Range(0, effectProperties.Count)
                                where BoxedValuesAreEqual(effectProperties[j], Box(testValue1, properties[i]), properties[i])
                                select j).ToList();

                // Change the same property to a different value, and see which collection properties match now.
                properties[i].SetValue(effect, testValue2);

                var matches2 = (from j in Enumerable.Range(0, effectProperties.Count)
                                where BoxedValuesAreEqual(effectProperties[j], Box(testValue2, properties[i]), properties[i])
                                select j).ToList();

                // There should be one and only one property that matched both times. If not,
                // either we don't have 1 <-> 1 mapping between strongly typed properties and
                // collection indices, or something went wrong during the boxing type conversions.
                var intersection = matches1.Intersect(matches2);
                Assert.AreEqual(1, intersection.Count());
                int whichIndexIsThis = intersection.Single();
                whichIndexIsProperty.Add(whichIndexIsThis);

                // Change the property value back to its initial state.
                properties[i].SetValue(effect, Unbox(initialValues[whichIndexIsThis], properties[i]));
                Assert.IsTrue(BoxedValuesAreEqual(initialValues[whichIndexIsThis], effectProperties[whichIndexIsThis], properties[i]));
            }

            // Should not have any duplicate property mappings.
            Assert.AreEqual(whichIndexIsProperty.Count, whichIndexIsProperty.Distinct().Count());
        }
Ejemplo n.º 2
0
        static void TestEffectProperties(TypeInfo effectType, IGraphicsEffect effect)
        {
            var properties = GetEffectProperties(effectType).ToList();

            IList <object> effectProperties = new ViewIndexerAsList <object>(() => EffectAccessor.GetPropertyCount(effect),
                                                                             i => EffectAccessor.GetProperty(effect, i));

            FilterOutCustomizedEffectProperties(effectType.AsType(), ref properties, ref effectProperties);

            // Should have the same number of strongly typed properties as elements in the properties collection.
            Assert.AreEqual(properties.Count, effectProperties.Count);

            // Store the initial property values.
            var initialValues = effectProperties.ToList();

            var whichIndexIsProperty = new List <int>();

            // Changing strongly typed properties should change the properties collection.
            for (int i = 0; i < effectProperties.Count; i++)
            {
                object testValue1 = GetArbitraryTestValue(properties[i].PropertyType, true);
                object testValue2 = GetArbitraryTestValue(properties[i].PropertyType, false);

                // Change a property value, and see which collection properties match the result.
                properties[i].SetValue(effect, testValue1);
                AssertPropertyValuesAreEqual(testValue1, properties[i].GetValue(effect));

                var matches1 = (from j in Enumerable.Range(0, effectProperties.Count)
                                where BoxedValuesAreEqual(effectProperties[j], Box(testValue1, properties[i]), properties[i])
                                select j).ToList();

                // Change the same property to a different value, and see which collection properties match now.
                properties[i].SetValue(effect, testValue2);
                AssertPropertyValuesAreEqual(testValue2, properties[i].GetValue(effect));

                var matches2 = (from j in Enumerable.Range(0, effectProperties.Count)
                                where BoxedValuesAreEqual(effectProperties[j], Box(testValue2, properties[i]), properties[i])
                                select j).ToList();

                // There should be one and only one property that matched both times. If not,
                // either we don't have 1 <-> 1 mapping between strongly typed properties and
                // collection indices, or something went wrong during the boxing type conversions.
                var intersection = matches1.Intersect(matches2);
                Assert.AreEqual(1, intersection.Count());
                int whichIndexIsThis = intersection.Single();
                whichIndexIsProperty.Add(whichIndexIsThis);

                // Change the property value back to its initial state.
                var unboxedValue = Unbox(initialValues[whichIndexIsThis], properties[i]);
                properties[i].SetValue(effect, unboxedValue);
                AssertPropertyValuesAreEqual(unboxedValue, properties[i].GetValue(effect));
                Assert.IsTrue(BoxedValuesAreEqual(initialValues[whichIndexIsThis], effectProperties[whichIndexIsThis], properties[i]));

                // Validate that IGraphicsEffectD2D1Interop agrees with what we think the type and index of this property is.
                int mappingIndex;
                EffectPropertyMapping mapping;

                EffectAccessor.GetNamedPropertyMapping(effect, properties[i].Name, out mappingIndex, out mapping);

                int expectedMappingIndex = whichIndexIsThis;

                if (effectProperties is FilteredViewOfList <object> )
                {
                    expectedMappingIndex = ((FilteredViewOfList <object>)effectProperties).GetOriginalIndex(expectedMappingIndex);
                }

                Assert.AreEqual(expectedMappingIndex, mappingIndex);

                var expectedMapping = GetExpectedPropertyMapping(properties[i], effectProperties[whichIndexIsThis]);
                Assert.AreEqual(expectedMapping, mapping);
            }

            // Should not have any duplicate property mappings.
            Assert.AreEqual(whichIndexIsProperty.Count, whichIndexIsProperty.Distinct().Count());
        }