1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections.Generic; 6 using System.Globalization; 7 using System.Reflection; 8 using Xunit; 9 10 namespace System.ComponentModel.Tests 11 { 12 public class ComponentResourceManagerTests 13 { 14 [Fact] Ctor_Default()15 public void Ctor_Default() 16 { 17 var resourceManager = new ComponentResourceManager(); 18 Assert.Null(resourceManager.BaseName); 19 Assert.False(resourceManager.IgnoreCase); 20 Assert.NotNull(resourceManager.ResourceSetType); 21 } 22 23 [Theory] 24 [InlineData(typeof(int))] Ctor_Type(Type type)25 public void Ctor_Type(Type type) 26 { 27 var resourceManager = new ComponentResourceManager(type); 28 Assert.Equal("Int32", resourceManager.BaseName); 29 Assert.False(resourceManager.IgnoreCase); 30 Assert.NotNull(resourceManager.ResourceSetType); 31 } 32 33 [Theory] 34 [InlineData(true)] 35 [InlineData(false)] ApplyResources_ValueExists_ReturnsExpected(bool ignoreCase)36 public void ApplyResources_ValueExists_ReturnsExpected(bool ignoreCase) 37 { 38 var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) 39 { 40 IgnoreCase = ignoreCase 41 }; 42 43 var value = new TestValue(); 44 resourceManager.ApplyResources(value, "Object"); 45 Assert.Equal("ObjectGetSetProperty", value.GetSetProperty); 46 Assert.Null(value.GetOnlyProperty); 47 Assert.Null(value.GetPrivateProperty()); 48 49 if (!PlatformDetection.IsFullFramework) // https://github.com/dotnet/corefx/issues/22444 needs to be ported to netfx 50 { 51 resourceManager.ApplyResources(value, "Default"); 52 Assert.Equal("DefaultGetSetProperty", value.GetSetProperty); 53 Assert.Null(value.GetOnlyProperty); 54 Assert.Null(value.GetPrivateProperty()); 55 } 56 } 57 58 private class TestValue 59 { 60 public string GetSetProperty { get; set; } 61 public string GetOnlyProperty { get; } 62 private string PrivateProperty { get; set; } 63 GetPrivateProperty()64 public string GetPrivateProperty() => PrivateProperty; 65 } 66 67 [Fact] ApplyResources_AmibguousWithSameDeclaringType_ThrowsAmbiguousMatchException()68 public void ApplyResources_AmibguousWithSameDeclaringType_ThrowsAmbiguousMatchException() 69 { 70 var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) 71 { 72 IgnoreCase = true 73 }; 74 75 var value = new MulticasedClass(); 76 Assert.Throws<AmbiguousMatchException>(() => resourceManager.ApplyResources(value, "Object")); 77 } 78 79 private class MulticasedClass 80 { 81 public string GetSetProperty { get; set; } 82 public string getsetproperty { get; set; } 83 } 84 AmbiguousWithDifferentDeclaringType_TestData()85 public static IEnumerable<object[]> AmbiguousWithDifferentDeclaringType_TestData() 86 { 87 yield return new object[] { new MulticaseSubClass() }; 88 yield return new object[] { new MulticaseSubSubClass() }; 89 } 90 91 [Theory] 92 [MemberData(nameof(AmbiguousWithDifferentDeclaringType_TestData))] 93 public void ApplyResources_AmibguousWithDifferentDeclaringTypeInValueType_UsesMostDeclaredProperty<T>(T value) where T : MulticaseSubClass 94 { 95 var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) 96 { 97 IgnoreCase = true 98 }; 99 100 resourceManager.ApplyResources(value, "Object"); 101 102 Assert.Null(value.GetSetProperty); 103 Assert.Equal("ObjectGetSetProperty", value.getsetproperty); 104 105 if (!PlatformDetection.IsFullFramework) // https://github.com/dotnet/corefx/issues/22444 needs to be ported to netfx 106 { 107 resourceManager.ApplyResources(value, "Default"); 108 109 Assert.Null(value.GetSetProperty); 110 Assert.Equal("DefaultGetSetProperty", value.getsetproperty); 111 } 112 } 113 114 public class MulticaseBaseClass 115 { 116 public string GetSetProperty { get; set; } 117 } 118 119 public class MulticaseSubClass : MulticaseBaseClass 120 { 121 public string getsetproperty { get; set; } 122 } 123 124 public class MulticaseSubSubClass : MulticaseSubClass { } 125 126 [Fact] ApplyResources_IComponentWithNullSite_Success()127 public void ApplyResources_IComponentWithNullSite_Success() 128 { 129 var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) 130 { 131 IgnoreCase = true 132 }; 133 134 var value = new TestComponent(); 135 resourceManager.ApplyResources(value, "Object"); 136 Assert.Equal("ObjectGetSetProperty", value.GetSetProperty); 137 138 if (!PlatformDetection.IsFullFramework) // https://github.com/dotnet/corefx/issues/22444 needs to be ported to netfx 139 { 140 resourceManager.ApplyResources(value, "Default"); 141 Assert.Equal("DefaultGetSetProperty", value.GetSetProperty); 142 } 143 } 144 145 [Fact] ApplyResources_IComponentWithNonDesignModeSite_Success()146 public void ApplyResources_IComponentWithNonDesignModeSite_Success() 147 { 148 var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) 149 { 150 IgnoreCase = true 151 }; 152 153 var value = new TestComponent { Site = new TestSite { DesignMode = false } }; 154 resourceManager.ApplyResources(value, "Object"); 155 Assert.Equal("ObjectGetSetProperty", value.GetSetProperty); 156 157 if (!PlatformDetection.IsFullFramework) // https://github.com/dotnet/corefx/issues/22444 needs to be ported to netfx 158 { 159 resourceManager.ApplyResources(value, "Default"); 160 Assert.Equal("DefaultGetSetProperty", value.GetSetProperty); 161 } 162 } 163 164 [Fact] 165 [ActiveIssue(22145, TargetFrameworkMonikers.NetFramework)] ApplyResources_IComponentWithDesignModeSite_Success()166 public void ApplyResources_IComponentWithDesignModeSite_Success() 167 { 168 var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) 169 { 170 IgnoreCase = true 171 }; 172 173 var value = new TestComponent { Site = new TestSite { DesignMode = true } }; 174 resourceManager.ApplyResources(value, "Object"); 175 Assert.Equal("ObjectGetSetProperty", value.GetSetProperty); 176 177 if (!PlatformDetection.IsFullFramework) // https://github.com/dotnet/corefx/issues/22444 needs to be ported to netfx 178 { 179 resourceManager.ApplyResources(value, "Default"); 180 Assert.Equal("DefaultGetSetProperty", value.GetSetProperty); 181 } 182 } 183 184 private class TestSite : ISite 185 { 186 public bool DesignMode { get; set; } 187 188 public IComponent Component => throw new NotImplementedException(); 189 public IContainer Container => throw new NotImplementedException(); 190 public string Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } 191 public object GetService(Type serviceType) => null; 192 } 193 194 private class TestComponent : IComponent 195 { 196 public ISite Site { get; set; } 197 198 #pragma warning disable 0067 199 public event EventHandler Disposed; 200 #pragma warning restore 0067 201 Dispose()202 public void Dispose() { } 203 204 public string GetSetProperty { get; set; } 205 } 206 207 [Theory] 208 [InlineData(true)] 209 [InlineData(false)] ApplyResources_NoSuchValue_Nop(bool ignoreCase)210 public void ApplyResources_NoSuchValue_Nop(bool ignoreCase) 211 { 212 var resourceManager = new ComponentResourceManager(typeof(global::Resources.TestResx)) 213 { 214 IgnoreCase = ignoreCase 215 }; 216 217 resourceManager.ApplyResources("Value", "ObjectName"); 218 resourceManager.ApplyResources("Value", "ObjectName", CultureInfo.CurrentUICulture); 219 resourceManager.ApplyResources("Value", "ObjectName", CultureInfo.InvariantCulture); 220 } 221 222 [Fact] ApplyResources_NullValue_ThrowsArgumentNullException()223 public void ApplyResources_NullValue_ThrowsArgumentNullException() 224 { 225 var resourceManager = new ComponentResourceManager(); 226 AssertExtensions.Throws<ArgumentNullException>("value", () => resourceManager.ApplyResources(null, "objectName")); 227 AssertExtensions.Throws<ArgumentNullException>("value", () => resourceManager.ApplyResources(null, "objectName", CultureInfo.CurrentCulture)); 228 } 229 230 [Fact] ApplyResources_NullObjectName_ThrowsArgumentNullException()231 public void ApplyResources_NullObjectName_ThrowsArgumentNullException() 232 { 233 var resourceManager = new ComponentResourceManager(); 234 AssertExtensions.Throws<ArgumentNullException>("objectName", () => resourceManager.ApplyResources("value", null)); 235 AssertExtensions.Throws<ArgumentNullException>("objectName", () => resourceManager.ApplyResources("value", null, CultureInfo.CurrentCulture)); 236 } 237 } 238 } 239