Skip to content

Commit 4370cc0

Browse files
authored
Update test "ConvertFrom_ShouldConvertKeys_Localization" to use the SR resource name to retrieve the localized string (#13192)
* Update test "ConvertFrom_ShouldConvertKeys_Localization" to use the SR resource name to retrieve the localized string * Restore SR.Culture to its original value at the end of the test * Add test file KeysConverterTests.LocalizationTests
1 parent 547b34f commit 4370cc0

File tree

2 files changed

+77
-44
lines changed

2 files changed

+77
-44
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

src/test/unit/System.Windows.Forms/System/Windows/Forms/KeysConverterTests.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace System.Windows.Forms.Tests;
1010

11-
public class KeysConverterTests
11+
public partial class KeysConverterTests
1212
{
1313
[Theory]
1414
[UseDefaultXunitCulture]
@@ -25,36 +25,6 @@ public void ConvertFrom_ShouldConvertKeys(string input, Keys keys)
2525
Assert.Equal(keys, result);
2626
}
2727

28-
[Theory]
29-
[InlineData("fr-FR", "(aucune)", Keys.None)]
30-
[InlineData("nb-NO", "None", Keys.None)]
31-
[InlineData("de-DE", "Ende", Keys.End)]
32-
public void ConvertFrom_ShouldConvertKeys_Localization(string cultureName, string localizedKeyName, Keys expectedKey)
33-
{
34-
CultureInfo culture = CultureInfo.GetCultureInfo(cultureName);
35-
KeysConverter converter = new();
36-
37-
// The 'localizedKeyName' is converted into the corresponding key value according to the specified culture.
38-
var resultFromSpecificCulture = (Keys?)converter.ConvertFrom(context: null, culture, localizedKeyName);
39-
Assert.Equal(expectedKey, resultFromSpecificCulture);
40-
41-
// Record original UI culture.
42-
CultureInfo originalUICulture = Thread.CurrentThread.CurrentUICulture;
43-
44-
try
45-
{
46-
Thread.CurrentThread.CurrentUICulture = culture;
47-
48-
// When the culture is empty, the 'localizedKeyName' is converted to the corresponding key value based on CurrentUICulture.
49-
var resultFromUICulture = (Keys?)converter.ConvertFrom(context: null, culture: null, localizedKeyName);
50-
Assert.Equal(expectedKey, resultFromUICulture);
51-
}
52-
finally
53-
{
54-
Thread.CurrentThread.CurrentUICulture = originalUICulture;
55-
}
56-
}
57-
5828
[Theory]
5929
[InlineData(Keys.None, "(none)")]
6030
[InlineData(Keys.S, "S")]
@@ -71,19 +41,6 @@ public void ConvertToString_ShouldConvertKeys(Keys keys, string expectedResult)
7141
Assert.Equal(expectedResult, result);
7242
}
7343

74-
[Theory]
75-
[InlineData("fr-FR", Keys.None, "(aucune)")]
76-
[InlineData("de-DE", Keys.End, "Ende")]
77-
public void ConvertToString_ShouldConvertKeys_Localization(string cultureName, Keys key, string expectedLocalizedKeyName)
78-
{
79-
CultureInfo culture = CultureInfo.GetCultureInfo(cultureName);
80-
81-
KeysConverter converter = new();
82-
string result = converter.ConvertToString(null, culture, key);
83-
84-
Assert.Equal(expectedLocalizedKeyName, result);
85-
}
86-
8744
public static IEnumerable<object[]> ConvertToEnumArray_ShouldConvertKeys_TestData()
8845
{
8946
yield return new object[] { "fr-FR", Keys.None, new Enum[] { Keys.None } };

0 commit comments

Comments
 (0)