PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Silverlight4/Source/RiaClient.Tests/System.ComponentModel.DataAnnotations/AssociatedMetadataTypeTypeDescriptionProviderTest.cs

https://github.com/kvervo/HorizontalLoopingSelector
C# | 237 lines | 181 code | 39 blank | 17 comment | 0 complexity | fee8bd94fe81fee6af2e5234411dc3ef MD5 | raw file
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. #if !SILVERLIGHT
  6. using System.Linq;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using Moq;
  9. namespace System.ComponentModel.DataAnnotations.Test {
  10. [TestClass]
  11. public class AssociatedMetadataTypeTypeDescriptionProviderTest {
  12. [TestMethod]
  13. public void ConstructorAndGetTypeDescriptor() {
  14. AssociatedMetadataTypeTypeDescriptionProvider provider1 = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(TestTable));
  15. Assert.IsTrue(provider1.GetTypeDescriptor(typeof(TestTable)).GetProperties().Find("Number", true).Attributes.ContainsEquivalent(new RangeAttribute(0, 10)));
  16. AssociatedMetadataTypeTypeDescriptionProvider provider2 = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(TestTable), typeof(TestTable_MetadataAlternative));
  17. Assert.IsTrue(provider2.GetTypeDescriptor(typeof(TestTable)).GetProperties().Find("Number", true).Attributes.ContainsEquivalent(new RequiredAttribute()));
  18. }
  19. // Dev10 Bug 722118: Inheritance is broken with the AssociatedMetadataTypeTypeDescription provider
  20. [TestMethod]
  21. public void AddProviderToBaseType_GetPropertiesOfDerivedType() {
  22. // Before adding TDP
  23. var props = TypeDescriptor.GetProperties(typeof(Base)).Cast<PropertyDescriptor>().
  24. Select(p => p.Name).ToArray();
  25. CollectionAssert.AreEquivalent(new[] { "A", "B" }, props);
  26. props = TypeDescriptor.GetProperties(typeof(Derived)).Cast<PropertyDescriptor>().
  27. Select(p => p.Name).ToArray();
  28. CollectionAssert.AreEquivalent(new[] { "A", "B", "C" }, props);
  29. TypeDescriptionProvider provider = null;
  30. try {
  31. provider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Base));
  32. TypeDescriptor.AddProvider(provider, typeof(Base));
  33. // After adding TDP
  34. props = TypeDescriptor.GetProperties(typeof(Base)).Cast<PropertyDescriptor>().
  35. Select(p => p.Name).ToArray();
  36. CollectionAssert.AreEquivalent(new[] { "A", "B" }, props);
  37. props = TypeDescriptor.GetProperties(typeof(Derived)).Cast<PropertyDescriptor>().
  38. Select(p => p.Name).ToArray();
  39. CollectionAssert.AreEquivalent(new[] { "A", "B", "C" }, props);
  40. }
  41. finally {
  42. // Ensure we remove the provider we attached.
  43. TypeDescriptor.RemoveProvider(provider, typeof(Base));
  44. }
  45. }
  46. [TestMethod]
  47. public void AddProviderToTypeAndInstance() {
  48. var instance = new Base();
  49. // Before adding TDPs
  50. var props = TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>().
  51. Select(p => p.Name).ToArray();
  52. CollectionAssert.AreEquivalent(new[] { "A", "B" }, props);
  53. TypeDescriptionProvider typeProvider = null;
  54. TypeDescriptionProvider instanceProvider = null;
  55. try {
  56. typeProvider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Base));
  57. TypeDescriptor.AddProvider(typeProvider, typeof(Base));
  58. instanceProvider = new TestTypeDescriptionProvider(instance);
  59. TypeDescriptor.AddProvider(instanceProvider, instance);
  60. // After adding TDPs
  61. props = TypeDescriptor.GetProperties(instance).Cast<PropertyDescriptor>().
  62. Select(p => p.Name).ToArray();
  63. CollectionAssert.AreEquivalent(new[] { "A", "B", "CustomProperty1", "CustomProperty2" }, props);
  64. }
  65. finally {
  66. // Ensure we remove the providers we attached.
  67. TypeDescriptor.RemoveProvider(typeProvider, typeof(Base));
  68. TypeDescriptor.RemoveProvider(instanceProvider, instance);
  69. }
  70. }
  71. [TestMethod]
  72. public void AddMultipleProvidersToType() {
  73. // Before adding TDPs
  74. var props = TypeDescriptor.GetProperties(typeof(Base)).Cast<PropertyDescriptor>().
  75. Select(p => p.Name).ToArray();
  76. CollectionAssert.AreEquivalent(new[] { "A", "B" }, props);
  77. TypeDescriptionProvider associatedMetadataProvider = null;
  78. TypeDescriptionProvider testProvider = null;
  79. try {
  80. associatedMetadataProvider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Base));
  81. TypeDescriptor.AddProvider(associatedMetadataProvider, typeof(Base));
  82. testProvider = new TestTypeDescriptionProvider(typeof(Base));
  83. TypeDescriptor.AddProvider(testProvider, typeof(Base));
  84. // After adding TDPs
  85. props = TypeDescriptor.GetProperties(typeof(Base)).Cast<PropertyDescriptor>().
  86. Select(p => p.Name).ToArray();
  87. CollectionAssert.AreEquivalent(new[] { "A", "B", "CustomProperty1", "CustomProperty2" }, props);
  88. }
  89. finally {
  90. // Ensure we remove the providers we attached.
  91. TypeDescriptor.RemoveProvider(associatedMetadataProvider, typeof(Base));
  92. TypeDescriptor.RemoveProvider(testProvider, typeof(Base));
  93. }
  94. // Add providers in reverse order
  95. try {
  96. testProvider = new TestTypeDescriptionProvider(typeof(Base));
  97. TypeDescriptor.AddProvider(testProvider, typeof(Base));
  98. associatedMetadataProvider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Base));
  99. TypeDescriptor.AddProvider(associatedMetadataProvider, typeof(Base));
  100. props = TypeDescriptor.GetProperties(typeof(Base)).Cast<PropertyDescriptor>().
  101. Select(p => p.Name).ToArray();
  102. CollectionAssert.AreEquivalent(new[] { "A", "B", "CustomProperty1", "CustomProperty2" }, props);
  103. }
  104. finally {
  105. // Ensure we remove the providers we attached.
  106. TypeDescriptor.RemoveProvider(associatedMetadataProvider, typeof(Base));
  107. TypeDescriptor.RemoveProvider(testProvider, typeof(Base));
  108. }
  109. }
  110. [TestMethod]
  111. public void MetadataInheritance() {
  112. TypeDescriptionProvider baseProvider = null;
  113. TypeDescriptionProvider derivedProvider = null;
  114. TypeDescriptionProvider derivedNoMetadataProvider = null;
  115. try {
  116. baseProvider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Base));
  117. TypeDescriptor.AddProvider(baseProvider, typeof(Base));
  118. derivedProvider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Derived));
  119. TypeDescriptor.AddProvider(derivedProvider, typeof(Derived));
  120. derivedNoMetadataProvider = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(DerivedNoMetadata));
  121. TypeDescriptor.AddProvider(derivedNoMetadataProvider, typeof(DerivedNoMetadata));
  122. var attrs = TypeDescriptor.GetProperties(typeof(Base))["A"].Attributes.Cast<Attribute>().
  123. Select(a => a.GetType().Name).ToArray();
  124. CollectionAssert.IsSubsetOf(new[] { "DescriptionAttribute" }, attrs);
  125. attrs = TypeDescriptor.GetProperties(typeof(Derived))["A"].Attributes.Cast<Attribute>().
  126. Select(a => a.GetType().Name).ToArray();
  127. CollectionAssert.IsSubsetOf(new[] { "DescriptionAttribute", "CategoryAttribute" }, attrs);
  128. attrs = TypeDescriptor.GetProperties(typeof(DerivedNoMetadata))["A"].Attributes.Cast<Attribute>().
  129. Select(a => a.GetType().Name).ToArray();
  130. CollectionAssert.IsSubsetOf(new[] { "DescriptionAttribute" }, attrs);
  131. }
  132. finally {
  133. // Ensure we remove the providers we attached.
  134. TypeDescriptor.RemoveProvider(baseProvider, typeof(Base));
  135. TypeDescriptor.RemoveProvider(derivedProvider, typeof(Derived));
  136. TypeDescriptor.RemoveProvider(derivedNoMetadataProvider, typeof(DerivedNoMetadata));
  137. }
  138. }
  139. [MetadataType(typeof(BaseMetadata))]
  140. private class Base {
  141. public int A { get; set; }
  142. public int B { get; set; }
  143. }
  144. [MetadataType(typeof(DerivedMetadata))]
  145. private class Derived : Base {
  146. public int C { get; set; }
  147. }
  148. private class DerivedNoMetadata : Base {
  149. public int D { get; set; }
  150. }
  151. private class BaseMetadata {
  152. [Description("TestDescription")]
  153. public virtual int A { get; set; }
  154. }
  155. private class DerivedMetadata : BaseMetadata {
  156. [Category("TestCategory")]
  157. public override int A { get; set; }
  158. }
  159. [MetadataType(typeof(TestTable_Metadata))]
  160. class TestTable {
  161. public int Number { get; set; }
  162. }
  163. class TestTable_Metadata {
  164. [Range(0, 10)]
  165. public object Number { get; set; }
  166. }
  167. class TestTable_MetadataAlternative {
  168. [Required]
  169. public object Number { get; set; }
  170. }
  171. private class TestTypeDescriptionProvider : TypeDescriptionProvider {
  172. private TypeDescriptionProvider _baseProvider;
  173. public TestTypeDescriptionProvider(object instance) {
  174. _baseProvider = TypeDescriptor.GetProvider(instance);
  175. }
  176. public TestTypeDescriptionProvider(Type type) {
  177. _baseProvider = TypeDescriptor.GetProvider(type);
  178. }
  179. public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance) {
  180. return new TestTypeDescriptor(_baseProvider.GetTypeDescriptor(objectType, instance));
  181. }
  182. private class TestTypeDescriptor : CustomTypeDescriptor {
  183. public TestTypeDescriptor(ICustomTypeDescriptor parent)
  184. : base(parent) {
  185. }
  186. public override PropertyDescriptorCollection GetProperties() {
  187. var baseProperties = base.GetProperties().Cast<PropertyDescriptor>();
  188. var customProperties = new[] {
  189. (new Mock<PropertyDescriptor>("CustomProperty1", null) { CallBase = true }).Object,
  190. (new Mock<PropertyDescriptor>("CustomProperty2", null) { CallBase = true }).Object };
  191. var allProperties = baseProperties.Concat(customProperties).ToArray();
  192. return new PropertyDescriptorCollection(allProperties, true);
  193. }
  194. }
  195. }
  196. }
  197. }
  198. #endif