|
| 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 | + |
| 4 | +using System.Globalization; |
| 5 | + |
| 6 | +namespace System.Windows.Forms; |
| 7 | + |
| 8 | +public partial class KeysConverterTests |
| 9 | +{ |
| 10 | + // This test modifies global culture settings (Thread.CurrentUICulture and SR.Culture). |
| 11 | + // As a result, it must run sequentially to avoid conflicts with other tests that may |
| 12 | + // rely on or alter these settings. Running it in a sequential collection ensures consistent results. |
| 13 | + [Collection("Sequential")] |
| 14 | + public class LocalizationTests |
| 15 | + { |
| 16 | + [Theory] |
| 17 | + [InlineData("fr-FR", "toStringNone", Keys.None)] |
| 18 | + [InlineData("zh-CN", "toStringNone", Keys.None)] |
| 19 | + [InlineData("nb-NO", "toStringNone", Keys.None)] |
| 20 | + [InlineData("de-DE", "toStringEnd", Keys.End)] |
| 21 | + public void ConvertFrom_ShouldConvertKeys_Localization(string cultureName, string resourceKey, Keys expectedKey) |
| 22 | + { |
| 23 | + // Record original culture. |
| 24 | + CultureInfo originalUICulture = Thread.CurrentThread.CurrentUICulture; |
| 25 | + CultureInfo originalSRCulture = SR.Culture; |
| 26 | + |
| 27 | + CultureInfo targetCulture = CultureInfo.GetCultureInfo(cultureName); |
| 28 | + |
| 29 | + try |
| 30 | + { |
| 31 | + // Use the SR resource key to retrieve the localized string corresponding to SR.Culture. |
| 32 | + SR.Culture = targetCulture; |
| 33 | + string localizedString = SR.GetResourceString(resourceKey); |
| 34 | + |
| 35 | + KeysConverter converter = new(); |
| 36 | + var resultFromSpecificCulture = (Keys?)converter.ConvertFrom(context: null, targetCulture, localizedString); |
| 37 | + |
| 38 | + Assert.Equal(expectedKey, resultFromSpecificCulture); |
| 39 | + |
| 40 | + Thread.CurrentThread.CurrentUICulture = targetCulture; |
| 41 | + |
| 42 | + // When the culture is empty, the 'localizedString' is converted |
| 43 | + // to the corresponding key value based on CurrentUICulture. |
| 44 | + var resultFromUICulture = (Keys?)converter.ConvertFrom(context: null, culture: null, localizedString); |
| 45 | + Assert.Equal(expectedKey, resultFromUICulture); |
| 46 | + } |
| 47 | + finally |
| 48 | + { |
| 49 | + Thread.CurrentThread.CurrentUICulture = originalUICulture; |
| 50 | + SR.Culture = originalSRCulture; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [Theory] |
| 55 | + [InlineData("fr-FR", Keys.None, "toStringNone")] |
| 56 | + [InlineData("de-DE", Keys.End, "toStringEnd")] |
| 57 | + public void ConvertToString_ShouldConvertKeys_Localization(string cultureName, Keys key, string resourceKey) |
| 58 | + { |
| 59 | + CultureInfo originalSRCulture = SR.Culture; |
| 60 | + try |
| 61 | + { |
| 62 | + CultureInfo targetCulture = CultureInfo.GetCultureInfo(cultureName); |
| 63 | + |
| 64 | + KeysConverter converter = new(); |
| 65 | + string? result = converter.ConvertToString(null, targetCulture, key); |
| 66 | + |
| 67 | + SR.Culture = targetCulture; |
| 68 | + Assert.Equal(SR.GetResourceString(resourceKey), result); |
| 69 | + } |
| 70 | + finally |
| 71 | + { |
| 72 | + SR.Culture = originalSRCulture; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments