1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 2 3 using System.Collections.Generic; 4 using System.Globalization; 5 using System.Linq; 6 using System.Threading; 7 using Moq; 8 using Xunit; 9 10 namespace System.Web.WebPages.Test 11 { 12 public class CultureUtilTest 13 { 14 [Fact] SetAutoCultureWithNoUserLanguagesDoesNothing()15 public void SetAutoCultureWithNoUserLanguagesDoesNothing() 16 { 17 // Arrange 18 var context = GetContextForSetCulture(null); 19 Thread thread = GetThread(); 20 CultureInfo culture = thread.CurrentCulture; 21 22 // Act 23 CultureUtil.SetCulture(thread, context, "auto"); 24 25 // Assert 26 Assert.Equal(culture, thread.CurrentCulture); 27 } 28 29 [Fact] SetAutoUICultureWithNoUserLanguagesDoesNothing()30 public void SetAutoUICultureWithNoUserLanguagesDoesNothing() 31 { 32 // Arrange 33 var context = GetContextForSetCulture(null); 34 Thread thread = GetThread(); 35 CultureInfo culture = thread.CurrentUICulture; 36 37 // Act 38 CultureUtil.SetUICulture(thread, context, "auto"); 39 40 // Assert 41 Assert.Equal(culture, thread.CurrentUICulture); 42 } 43 44 [Fact] SetAutoCultureWithEmptyUserLanguagesDoesNothing()45 public void SetAutoCultureWithEmptyUserLanguagesDoesNothing() 46 { 47 // Arrange 48 var context = GetContextForSetCulture(Enumerable.Empty<string>()); 49 Thread thread = GetThread(); 50 CultureInfo culture = thread.CurrentCulture; 51 52 // Act 53 CultureUtil.SetCulture(thread, context, "auto"); 54 55 // Assert 56 Assert.Equal(culture, thread.CurrentCulture); 57 } 58 59 [Fact] SetAutoUICultureWithEmptyUserLanguagesDoesNothing()60 public void SetAutoUICultureWithEmptyUserLanguagesDoesNothing() 61 { 62 // Arrange 63 var context = GetContextForSetCulture(Enumerable.Empty<string>()); 64 Thread thread = GetThread(); 65 CultureInfo culture = thread.CurrentUICulture; 66 67 // Act 68 CultureUtil.SetUICulture(thread, context, "auto"); 69 70 // Assert 71 Assert.Equal(culture, thread.CurrentUICulture); 72 } 73 74 [Fact] SetAutoCultureWithBlankUserLanguagesDoesNothing()75 public void SetAutoCultureWithBlankUserLanguagesDoesNothing() 76 { 77 // Arrange 78 var context = GetContextForSetCulture(new[] { " " }); 79 Thread thread = GetThread(); 80 CultureInfo culture = thread.CurrentCulture; 81 82 // Act 83 CultureUtil.SetCulture(thread, context, "auto"); 84 85 // Assert 86 Assert.Equal(culture, thread.CurrentCulture); 87 } 88 89 [Fact] SetAutoUICultureWithBlankUserLanguagesDoesNothing()90 public void SetAutoUICultureWithBlankUserLanguagesDoesNothing() 91 { 92 // Arrange 93 var context = GetContextForSetCulture(new[] { " " }); 94 Thread thread = GetThread(); 95 CultureInfo culture = thread.CurrentUICulture; 96 97 // Act 98 CultureUtil.SetUICulture(thread, context, "auto"); 99 100 // Assert 101 Assert.Equal(culture, thread.CurrentUICulture); 102 } 103 104 [Fact] SetAutoCultureWithInvalidLanguageDoesNothing()105 public void SetAutoCultureWithInvalidLanguageDoesNothing() 106 { 107 // Arrange 108 var context = GetContextForSetCulture(new[] { "aa-AA", "bb-BB", "cc-CC" }); 109 Thread thread = GetThread(); 110 CultureInfo culture = thread.CurrentCulture; 111 112 // Act 113 CultureUtil.SetCulture(thread, context, "auto"); 114 115 // Assert 116 Assert.Equal(culture, thread.CurrentCulture); 117 } 118 119 [Fact] SetAutoUICultureWithInvalidLanguageDoesNothing()120 public void SetAutoUICultureWithInvalidLanguageDoesNothing() 121 { 122 // Arrange 123 var context = GetContextForSetCulture(new[] { "aa-AA", "bb-BB", "cc-CC" }); 124 Thread thread = GetThread(); 125 CultureInfo culture = thread.CurrentUICulture; 126 127 // Act 128 CultureUtil.SetUICulture(thread, context, "auto"); 129 130 // Assert 131 Assert.Equal(culture, thread.CurrentUICulture); 132 } 133 134 [Fact] SetAutoCultureDetectsUserLanguageCulture()135 public void SetAutoCultureDetectsUserLanguageCulture() 136 { 137 // Arrange 138 var context = GetContextForSetCulture(new[] { "en-GB", "en-US", "ar-eg" }); 139 Thread thread = GetThread(); 140 141 // Act 142 CultureUtil.SetCulture(thread, context, "auto"); 143 144 // Assert 145 Assert.Equal(CultureInfo.GetCultureInfo("en-GB"), thread.CurrentCulture); 146 Assert.Equal("05/01/1979", new DateTime(1979, 1, 5).ToString("d", thread.CurrentCulture)); 147 } 148 149 [Fact] SetAutoUICultureDetectsUserLanguageCulture()150 public void SetAutoUICultureDetectsUserLanguageCulture() 151 { 152 // Arrange 153 var context = GetContextForSetCulture(new[] { "en-GB", "en-US", "ar-eg" }); 154 Thread thread = GetThread(); 155 156 // Act 157 CultureUtil.SetUICulture(thread, context, "auto"); 158 159 // Assert 160 Assert.Equal(CultureInfo.GetCultureInfo("en-GB"), thread.CurrentUICulture); 161 Assert.Equal("05/01/1979", new DateTime(1979, 1, 5).ToString("d", thread.CurrentUICulture)); 162 } 163 164 [Fact] SetAutoCultureUserLanguageWithQParameterCulture()165 public void SetAutoCultureUserLanguageWithQParameterCulture() 166 { 167 // Arrange 168 var context = GetContextForSetCulture(new[] { "en-GB;q=0.3", "en-US", "ar-eg;q=0.5" }); 169 Thread thread = GetThread(); 170 171 // Act 172 CultureUtil.SetCulture(thread, context, "auto"); 173 174 // Assert 175 Assert.Equal(CultureInfo.GetCultureInfo("en-GB"), thread.CurrentCulture); 176 Assert.Equal("05/01/1979", new DateTime(1979, 1, 5).ToString("d", thread.CurrentCulture)); 177 } 178 179 [Fact] SetAutoUICultureDetectsUserLanguageWithQParameterCulture()180 public void SetAutoUICultureDetectsUserLanguageWithQParameterCulture() 181 { 182 // Arrange 183 var context = GetContextForSetCulture(new[] { "en-GB;q=0.3", "en-US", "ar-eg;q=0.5" }); 184 Thread thread = GetThread(); 185 186 // Act 187 CultureUtil.SetUICulture(thread, context, "auto"); 188 189 // Assert 190 Assert.Equal(CultureInfo.GetCultureInfo("en-GB"), thread.CurrentUICulture); 191 Assert.Equal("05/01/1979", new DateTime(1979, 1, 5).ToString("d", thread.CurrentUICulture)); 192 } 193 194 [Fact] SetCultureWithInvalidCultureThrows()195 public void SetCultureWithInvalidCultureThrows() 196 { 197 // Arrange 198 var context = GetContextForSetCulture(); 199 Thread thread = GetThread(); 200 201 // Act and Assert 202 Assert.Throws<CultureNotFoundException>(() => CultureUtil.SetCulture(thread, context, "sans-culture")); 203 } 204 205 [Fact] SetUICultureWithInvalidCultureThrows()206 public void SetUICultureWithInvalidCultureThrows() 207 { 208 // Arrange 209 var context = GetContextForSetCulture(); 210 Thread thread = GetThread(); 211 212 // Act and Assert 213 Assert.Throws<CultureNotFoundException>(() => CultureUtil.SetUICulture(thread, context, "sans-culture")); 214 } 215 216 [Fact] SetCultureWithValidCulture()217 public void SetCultureWithValidCulture() 218 { 219 // Arrange 220 var context = GetContextForSetCulture(); 221 Thread thread = GetThread(); 222 223 // Act 224 CultureUtil.SetCulture(thread, context, "en-GB"); 225 226 // Assert 227 Assert.Equal(CultureInfo.GetCultureInfo("en-GB"), thread.CurrentCulture); 228 Assert.Equal("05/01/1979", new DateTime(1979, 1, 5).ToString("d", thread.CurrentCulture)); 229 } 230 231 [Fact] SetUICultureWithValidCulture()232 public void SetUICultureWithValidCulture() 233 { 234 // Arrange 235 var context = GetContextForSetCulture(); 236 Thread thread = GetThread(); 237 238 // Act 239 CultureUtil.SetUICulture(thread, context, "en-GB"); 240 241 // Assert 242 Assert.Equal(CultureInfo.GetCultureInfo("en-GB"), thread.CurrentUICulture); 243 Assert.Equal("05/01/1979", new DateTime(1979, 1, 5).ToString("d", thread.CurrentUICulture)); 244 } 245 GetThread()246 private static Thread GetThread() 247 { 248 return new Thread(() => { }); 249 } 250 GetContextForSetCulture(IEnumerable<string> userLanguages = null)251 private static HttpContextBase GetContextForSetCulture(IEnumerable<string> userLanguages = null) 252 { 253 Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>(); 254 contextMock.Setup(context => context.Request.UserLanguages).Returns(userLanguages == null ? null : userLanguages.ToArray()); 255 return contextMock.Object; 256 } 257 } 258 } 259