StringsTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Microsoft.Internal;
  10. using System.Threading;
  11. using System.Globalization;
  12. namespace System
  13. {
  14. [TestClass]
  15. public class StringsTests
  16. {
  17. [TestMethod]
  18. public void PropertiesAreInsyncWithResources()
  19. {
  20. var properties = GetStringProperties();
  21. Assert.IsTrue(properties.Length > 0, "Expected to find at least one string property in Strings.cs.");
  22. foreach (PropertyInfo property in properties)
  23. {
  24. object value = property.GetValue(null, (object[])null);
  25. Assert.IsNotNull(value, "Property '{0}' does not have an associated string in Strings.resx.", property.Name);
  26. }
  27. }
  28. private static PropertyInfo[] GetStringProperties()
  29. {
  30. PropertyInfo[] properties = typeof(Strings).GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
  31. return properties.Where(property =>
  32. {
  33. return !CanIgnore(property);
  34. }).ToArray();
  35. }
  36. private static bool CanIgnore(PropertyInfo property)
  37. {
  38. switch (property.Name)
  39. {
  40. case "Culture":
  41. case "ResourceManager":
  42. return true;
  43. }
  44. return false;
  45. }
  46. #if !SILVERLIGHT
  47. [TestMethod]
  48. public void LocalizedResourcesArePickedUpBasedOnThreadCulture()
  49. {
  50. // verify each property against an en-US resource manager
  51. bool wasVerified = false;
  52. bool areLocalizedCLRResourcesAvailable = AreLocalizedCLRResourcesAvailable();
  53. foreach (var stringsPropInfo in GetStringProperties())
  54. {
  55. string referenceValue = String.Empty;
  56. PerformOnEnUsThread(() =>
  57. {
  58. referenceValue = (string)stringsPropInfo.GetValue(null, (object[])null);
  59. });
  60. string actualValue = (string)stringsPropInfo.GetValue(null, (object[])null);
  61. // determine if LangPacks are installed for the the CurrentUICulture. If not, default to english
  62. if (areLocalizedCLRResourcesAvailable)
  63. {
  64. Assert.AreNotEqual(referenceValue, actualValue, "Failed to pick up localized resources for UI culture '{0}'", Thread.CurrentThread.CurrentUICulture.Name);
  65. }
  66. else
  67. {
  68. Assert.AreEqual(referenceValue, actualValue);
  69. }
  70. wasVerified = true;
  71. }
  72. Assert.IsTrue(wasVerified);
  73. }
  74. private bool AreLocalizedCLRResourcesAvailable()
  75. {
  76. if (Thread.CurrentThread.CurrentUICulture.LCID == 1033) // en-US
  77. {
  78. return false;
  79. }
  80. object regValue = Microsoft.Win32.Registry.GetValue(
  81. GetRegPath(),
  82. "Install",
  83. 0);
  84. return (regValue != null) && ((int)regValue == 1);
  85. }
  86. private string GetRegPath()
  87. {
  88. var path = String.Format(
  89. @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v{0}\Client\{1}",
  90. typeof(string).Assembly.GetName().Version.Major,
  91. Thread.CurrentThread.CurrentUICulture.LCID);
  92. return path;
  93. }
  94. private static void PerformOnEnUsThread(Action action)
  95. {
  96. Thread enUsThread = new Thread(new ThreadStart(action));
  97. enUsThread.CurrentUICulture = new CultureInfo("en-US");
  98. enUsThread.Start();
  99. enUsThread.Join();
  100. }
  101. #endif
  102. }
  103. }