Bug 1685804 - Use nsIGSettingsService in OSPreferences_gtk; r=zbraniecki

This switches over to using nsIGSettingsService. This makes the gtest results
on gtk based systems depend on the current OS settings, so the test expectations
are adjusted to accept both AM/PM and 24h settings.

Differential Revision: https://phabricator.services.mozilla.com/D101362
This commit is contained in:
Dan Minor 2021-01-12 14:20:07 +00:00
parent e91d8deeea
commit 1465995b22
2 changed files with 34 additions and 51 deletions

View File

@ -6,12 +6,12 @@
#include <locale.h>
#include "OSPreferences.h"
#include "dlfcn.h"
#include "glib.h"
#include "gio/gio.h"
#include "unicode/uloc.h"
#include "nsServiceManagerUtils.h"
#include "nsIGSettingsService.h"
using namespace mozilla::intl;
OSPreferences::OSPreferences() = default;
@ -52,19 +52,13 @@ bool OSPreferences::ReadRegionalPrefsLocales(nsTArray<nsCString>& aLocaleList) {
* We're taking the current 12/24h settings irrelevant of the locale, because
* in the UI user selects this setting for all locales.
*/
typedef GVariant* (*get_value_fn_t)(GSettings*, const gchar*);
static get_value_fn_t FindGetValueFunction() {
get_value_fn_t fn = reinterpret_cast<get_value_fn_t>(
dlsym(RTLD_DEFAULT, "g_settings_get_user_value"));
return fn ? fn : &g_settings_get_value;
}
static int HourCycle() {
int rval = 0;
const char* schema;
const char* key;
// Ubuntu 16.04 and lower report "Unity". Ubuntu 16.04 is supported until
// April 2021. This code can be removed once it hits EOL.
nsAutoCString schema;
nsAutoCString key;
const char* env = getenv("XDG_CURRENT_DESKTOP");
if (env && strcmp(env, "Unity") == 0) {
schema = "com.canonical.indicator.datetime";
@ -74,41 +68,23 @@ static int HourCycle() {
key = "clock-format";
}
// This is a workaround for old GTK versions.
// Once we bump the minimum version to 2.40 we should replace
// this with g_settings_schme_source_lookup.
// See bug 1356718 for details.
const char* const* schemas = g_settings_list_schemas();
GSettings* settings = nullptr;
nsCOMPtr<nsIGSettingsService> gsettings =
do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
nsCOMPtr<nsIGSettingsCollection> desktop_settings;
for (uint32_t i = 0; schemas[i] != nullptr; i++) {
if (strcmp(schemas[i], schema) == 0) {
settings = g_settings_new(schema);
break;
}
}
if (settings) {
// We really want to use g_settings_get_user_value which will
// only want to take it if user manually changed the value.
// But this requires glib 2.40, and we still support older glib versions,
// so we have to check whether it's available and fall back to the older
// g_settings_get_value if not.
static get_value_fn_t sGetValueFunction = FindGetValueFunction();
GVariant* value = sGetValueFunction(settings, key);
if (value) {
if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
const char* strVal = g_variant_get_string(value, nullptr);
if (strncmp("12", strVal, 2) == 0) {
rval = 12;
} else if (strncmp("24", strVal, 2) == 0) {
rval = 24;
}
if (gsettings) {
gsettings->GetCollectionForSchema(schema, getter_AddRefs(desktop_settings));
if (desktop_settings) {
nsAutoCString result;
desktop_settings->GetString(key, result);
if (result == "12h") {
rval = 12;
} else if (result == "24h") {
rval = 24;
}
g_variant_unref(value);
}
g_object_unref(settings);
}
return rval;
}

View File

@ -18,7 +18,8 @@ TEST(DateTimeFormat, FormatPRExplodedTime)
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_TRUE(formattedTime.Find("January") != kNotFound);
ASSERT_TRUE(formattedTime.Find("1970") != kNotFound);
ASSERT_TRUE(formattedTime.Find("12:00:00 AM") != kNotFound);
ASSERT_TRUE(formattedTime.Find("12:00:00 AM") != kNotFound ||
formattedTime.Find("00:00:00") != kNotFound);
prExplodedTime = {0, 0, 19, 0, 1, 0, 1970, 4, 0, {(19 * 60), 0}};
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
@ -26,7 +27,8 @@ TEST(DateTimeFormat, FormatPRExplodedTime)
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_TRUE(formattedTime.Find("January") != kNotFound);
ASSERT_TRUE(formattedTime.Find("1970") != kNotFound);
ASSERT_TRUE(formattedTime.Find("12:19:00 AM") != kNotFound);
ASSERT_TRUE(formattedTime.Find("12:19:00 AM") != kNotFound ||
formattedTime.Find("00:19:00") != kNotFound);
prExplodedTime = {0, 0, 0, 7, 1,
0, 1970, 4, 0, {(6 * 60 * 60), (1 * 60 * 60)}};
@ -35,7 +37,8 @@ TEST(DateTimeFormat, FormatPRExplodedTime)
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_TRUE(formattedTime.Find("January") != kNotFound);
ASSERT_TRUE(formattedTime.Find("1970") != kNotFound);
ASSERT_TRUE(formattedTime.Find("7:00:00 AM") != kNotFound);
ASSERT_TRUE(formattedTime.Find("7:00:00 AM") != kNotFound ||
formattedTime.Find("07:00:00") != kNotFound);
prExplodedTime = {
0, 0, 29, 11, 1,
@ -45,7 +48,8 @@ TEST(DateTimeFormat, FormatPRExplodedTime)
ASSERT_TRUE(NS_SUCCEEDED(rv));
ASSERT_TRUE(formattedTime.Find("January") != kNotFound);
ASSERT_TRUE(formattedTime.Find("1970") != kNotFound);
ASSERT_TRUE(formattedTime.Find("11:29:00 AM") != kNotFound);
ASSERT_TRUE(formattedTime.Find("11:29:00 AM") != kNotFound ||
formattedTime.Find("11:29:00") != kNotFound);
prExplodedTime = {0, 0, 37, 23, 31, 11, 1969, 3, 364, {-(23 * 60), 0}};
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
@ -54,7 +58,8 @@ TEST(DateTimeFormat, FormatPRExplodedTime)
ASSERT_TRUE(formattedTime.Find("December") != kNotFound);
ASSERT_TRUE(formattedTime.Find("31") != kNotFound);
ASSERT_TRUE(formattedTime.Find("1969") != kNotFound);
ASSERT_TRUE(formattedTime.Find("11:37:00 PM") != kNotFound);
ASSERT_TRUE(formattedTime.Find("11:37:00 PM") != kNotFound ||
formattedTime.Find("23:37:00") != kNotFound);
prExplodedTime = {0, 0, 0, 17, 31, 11, 1969, 3, 364, {-(7 * 60 * 60), 0}};
rv = mozilla::DateTimeFormat::FormatPRExplodedTime(
@ -63,7 +68,8 @@ TEST(DateTimeFormat, FormatPRExplodedTime)
ASSERT_TRUE(formattedTime.Find("December") != kNotFound);
ASSERT_TRUE(formattedTime.Find("31") != kNotFound);
ASSERT_TRUE(formattedTime.Find("1969") != kNotFound);
ASSERT_TRUE(formattedTime.Find("5:00:00 PM") != kNotFound);
ASSERT_TRUE(formattedTime.Find("5:00:00 PM") != kNotFound ||
formattedTime.Find("17:00:00") != kNotFound);
prExplodedTime = {
0, 0, 47, 14, 31,
@ -74,7 +80,8 @@ TEST(DateTimeFormat, FormatPRExplodedTime)
ASSERT_TRUE(formattedTime.Find("December") != kNotFound);
ASSERT_TRUE(formattedTime.Find("31") != kNotFound);
ASSERT_TRUE(formattedTime.Find("1969") != kNotFound);
ASSERT_TRUE(formattedTime.Find("2:47:00 PM") != kNotFound);
ASSERT_TRUE(formattedTime.Find("2:47:00 PM") != kNotFound ||
formattedTime.Find("14:47:00") != kNotFound);
}
TEST(DateTimeFormat, DateFormatSelectors)