Bug 1674212 - Add override pref for date time connector pattern; r=zbraniecki

This adds an override pref for the connector pattern used when combining dates
and times. It also fixes a test which was relying upon a hard-coded connector
pattern.

Differential Revision: https://phabricator.services.mozilla.com/D95278
This commit is contained in:
Dan Minor 2020-11-02 12:36:04 +00:00
parent 5bfbc38cf2
commit 492a70088c
2 changed files with 54 additions and 3 deletions

View File

@ -366,6 +366,17 @@ bool OSPreferences::GetPatternForSkeleton(const nsACString& aSkeleton,
bool OSPreferences::GetDateTimeConnectorPattern(const nsACString& aLocale,
nsACString& aRetVal) {
bool result = false;
// Check for a valid override pref and use that if present.
nsAutoCString value;
nsresult nr = Preferences::GetCString(
"intl.date_time.pattern_override.date_time_short", value);
if (NS_SUCCEEDED(nr) && value.Find("{0}") != kNotFound &&
value.Find("{1}") != kNotFound) {
aRetVal = std::move(value);
return true;
}
UErrorCode status = U_ZERO_ERROR;
UDateTimePatternGenerator* pg =
udatpg_open(PromiseFlatCString(aLocale).get(), &status);

View File

@ -134,15 +134,55 @@ TEST(Intl_Locale_OSPreferences, GetDateTimePatternPrefOverrides)
config.DateTimeFormatStyle,
nsDependentCString(""), pattern);
ASSERT_TRUE(NS_SUCCEEDED(nr));
ASSERT_TRUE(pattern.EqualsASCII("yy-MM, HH:mm"));
ASSERT_TRUE(pattern.Find("yy-MM") != kNotFound);
ASSERT_TRUE(pattern.Find("HH:mm") != kNotFound);
// Clear overrides, we should get the default value back.
mozilla::Preferences::SetCString(config.DatePref, "");
mozilla::Preferences::SetCString(config.TimePref, "");
mozilla::Preferences::ClearUser(config.DatePref);
mozilla::Preferences::ClearUser(config.TimePref);
nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle,
mozIOSPreferences::dateTimeFormatStyleNone,
nsDependentCString(""), pattern);
ASSERT_TRUE(NS_SUCCEEDED(nr));
ASSERT_EQ(default_pattern, pattern);
}
// Test overriding connector
nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
mozIOSPreferences::dateTimeFormatStyleShort,
nsDependentCString(""), default_pattern);
mozilla::Preferences::SetCString("intl.date_time.pattern_override.date_short",
"yyyy-MM-dd");
mozilla::Preferences::SetCString("intl.date_time.pattern_override.time_short",
"HH:mm:ss");
mozilla::Preferences::SetCString(
"intl.date_time.pattern_override.date_time_short", "{1} {0}");
nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
mozIOSPreferences::dateTimeFormatStyleShort,
nsDependentCString(""), pattern);
ASSERT_TRUE(NS_SUCCEEDED(nr));
ASSERT_TRUE(pattern.EqualsASCII("yyyy-MM-dd HH:mm:ss"));
// Reset to date and time to defaults
mozilla::Preferences::ClearUser("intl.date_time.pattern_override.date_short");
mozilla::Preferences::ClearUser("intl.date_time.pattern_override.time_short");
// Invalid patterns are ignored
mozilla::Preferences::SetCString(
"intl.date_time.pattern_override.date_time_short", "hello, world!");
nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
mozIOSPreferences::dateTimeFormatStyleShort,
nsDependentCString(""), pattern);
ASSERT_TRUE(NS_SUCCEEDED(nr));
ASSERT_EQ(default_pattern, pattern);
// Clearing the override results in getting the default pattern back.
mozilla::Preferences::ClearUser(
"intl.date_time.pattern_override.date_time_short");
nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
mozIOSPreferences::dateTimeFormatStyleShort,
nsDependentCString(""), pattern);
ASSERT_TRUE(NS_SUCCEEDED(nr));
ASSERT_EQ(default_pattern, pattern);
}