tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestOSPreferences.cpp (8680B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "gtest/gtest.h"
      7 #include "mozilla/gtest/MozAssertions.h"
      8 #include "mozilla/Preferences.h"
      9 #include "mozilla/intl/LocaleService.h"
     10 #include "mozilla/intl/OSPreferences.h"
     11 
     12 using namespace mozilla::intl;
     13 
     14 /**
     15 * We test that on all platforms we test against (irrelevant of the tier),
     16 * we will be able to retrieve at least a single locale out of the system.
     17 *
     18 * In theory, that may not be true, but if we encounter such platform we should
     19 * decide how to handle this and special case and this test should make
     20 * it not happen without us noticing.
     21 */
     22 TEST(Intl_Locale_OSPreferences, GetSystemLocales)
     23 {
     24  nsTArray<nsCString> systemLocales;
     25  ASSERT_TRUE(NS_SUCCEEDED(
     26      OSPreferences::GetInstance()->GetSystemLocales(systemLocales)));
     27 
     28  ASSERT_FALSE(systemLocales.IsEmpty());
     29 }
     30 
     31 /**
     32 * We test that on all platforms we test against (irrelevant of the tier),
     33 * we will be able to retrieve at least a single locale out of the system.
     34 *
     35 * In theory, that may not be true, but if we encounter such platform we should
     36 * decide how to handle this and special case and this test should make
     37 * it not happen without us noticing.
     38 */
     39 TEST(Intl_Locale_OSPreferences, GetRegionalPrefsLocales)
     40 {
     41  nsTArray<nsCString> rgLocales;
     42  ASSERT_TRUE(NS_SUCCEEDED(
     43      OSPreferences::GetInstance()->GetRegionalPrefsLocales(rgLocales)));
     44 
     45  ASSERT_FALSE(rgLocales.IsEmpty());
     46 }
     47 
     48 /**
     49 * We test that on all platforms we test against,
     50 * we will be able to retrieve a date and time pattern.
     51 *
     52 * This may come back empty on platforms where we don't have platforms
     53 * bindings for, so effectively, we're testing for crashes. We should
     54 * never crash.
     55 */
     56 TEST(Intl_Locale_OSPreferences, GetDateTimePattern)
     57 {
     58  nsAutoCString pattern;
     59  OSPreferences* osprefs = OSPreferences::GetInstance();
     60 
     61  struct Test {
     62    int dateStyle;
     63    int timeStyle;
     64    const char* locale;
     65  };
     66  Test tests[] = {{0, 0, ""},   {1, 0, "pl"}, {2, 0, "de-DE"}, {3, 0, "fr"},
     67                  {4, 0, "ar"},
     68 
     69                  {0, 1, ""},   {0, 2, "it"}, {0, 3, ""},      {0, 4, "ru"},
     70 
     71                  {4, 1, ""},   {3, 2, "cs"}, {2, 3, ""},      {1, 4, "ja"}};
     72 
     73  for (unsigned i = 0; i < std::size(tests); i++) {
     74    const Test& t = tests[i];
     75    if (NS_SUCCEEDED(osprefs->GetDateTimePattern(
     76            t.dateStyle, t.timeStyle, nsDependentCString(t.locale), pattern))) {
     77      ASSERT_TRUE((t.dateStyle == 0 && t.timeStyle == 0) || !pattern.IsEmpty());
     78    }
     79  }
     80 
     81  // If the locale is not specified, we should get the pattern corresponding to
     82  // the first regional prefs locale.
     83  AutoTArray<nsCString, 10> rpLocales;
     84  LocaleService::GetInstance()->GetRegionalPrefsLocales(rpLocales);
     85  ASSERT_TRUE(rpLocales.Length() > 0);
     86 
     87  nsAutoCString rpLocalePattern;
     88  ASSERT_TRUE(NS_SUCCEEDED(
     89      osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleLong,
     90                                  mozIOSPreferences::dateTimeFormatStyleLong,
     91                                  rpLocales[0], rpLocalePattern)));
     92  ASSERT_TRUE(NS_SUCCEEDED(
     93      osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleLong,
     94                                  mozIOSPreferences::dateTimeFormatStyleLong,
     95                                  nsDependentCString(""), pattern)));
     96  ASSERT_EQ(rpLocalePattern, pattern);
     97 }
     98 
     99 /**
    100 * Test that is possible to override the OS defaults through a pref.
    101 */
    102 TEST(Intl_Locale_OSPreferences, GetDateTimePatternPrefOverrides)
    103 {
    104  nsresult nr;
    105  nsAutoCString default_pattern, pattern;
    106  OSPreferences* osprefs = OSPreferences::GetInstance();
    107 
    108  struct {
    109    const char* DatePref;
    110    const char* TimePref;
    111    int32_t DateTimeFormatStyle;
    112  } configs[] = {{"intl.date_time.pattern_override.date_short",
    113                  "intl.date_time.pattern_override.time_short",
    114                  mozIOSPreferences::dateTimeFormatStyleShort},
    115                 {"intl.date_time.pattern_override.date_medium",
    116                  "intl.date_time.pattern_override.time_medium",
    117                  mozIOSPreferences::dateTimeFormatStyleMedium},
    118                 {"intl.date_time.pattern_override.date_long",
    119                  "intl.date_time.pattern_override.time_long",
    120                  mozIOSPreferences::dateTimeFormatStyleLong},
    121                 {"intl.date_time.pattern_override.date_full",
    122                  "intl.date_time.pattern_override.time_full",
    123                  mozIOSPreferences::dateTimeFormatStyleFull}};
    124 
    125  for (const auto& config : configs) {
    126    // Get default value for the OS
    127    nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle,
    128                                     mozIOSPreferences::dateTimeFormatStyleNone,
    129                                     nsDependentCString(""), default_pattern);
    130    ASSERT_NS_SUCCEEDED(nr);
    131 
    132    // Override date format
    133    mozilla::Preferences::SetCString(config.DatePref, "yy-MM");
    134    nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle,
    135                                     mozIOSPreferences::dateTimeFormatStyleNone,
    136                                     nsDependentCString(""), pattern);
    137    ASSERT_NS_SUCCEEDED(nr);
    138    ASSERT_TRUE(pattern.EqualsASCII("yy-MM"));
    139 
    140    // Override time format
    141    mozilla::Preferences::SetCString(config.TimePref, "HH:mm");
    142    nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleNone,
    143                                     config.DateTimeFormatStyle,
    144                                     nsDependentCString(""), pattern);
    145    ASSERT_NS_SUCCEEDED(nr);
    146    ASSERT_TRUE(pattern.EqualsASCII("HH:mm"));
    147 
    148    // Override both
    149    nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle,
    150                                     config.DateTimeFormatStyle,
    151                                     nsDependentCString(""), pattern);
    152    ASSERT_NS_SUCCEEDED(nr);
    153    ASSERT_TRUE(pattern.Find("yy-MM") != kNotFound);
    154    ASSERT_TRUE(pattern.Find("HH:mm") != kNotFound);
    155 
    156    // Clear overrides, we should get the default value back.
    157    mozilla::Preferences::ClearUser(config.DatePref);
    158    mozilla::Preferences::ClearUser(config.TimePref);
    159    nr = osprefs->GetDateTimePattern(config.DateTimeFormatStyle,
    160                                     mozIOSPreferences::dateTimeFormatStyleNone,
    161                                     nsDependentCString(""), pattern);
    162    ASSERT_NS_SUCCEEDED(nr);
    163    ASSERT_EQ(default_pattern, pattern);
    164  }
    165 
    166  // Test overriding connector
    167  nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
    168                                   mozIOSPreferences::dateTimeFormatStyleShort,
    169                                   nsDependentCString(""), default_pattern);
    170 
    171  mozilla::Preferences::SetCString("intl.date_time.pattern_override.date_short",
    172                                   "yyyy-MM-dd");
    173  mozilla::Preferences::SetCString("intl.date_time.pattern_override.time_short",
    174                                   "HH:mm:ss");
    175  mozilla::Preferences::SetCString(
    176      "intl.date_time.pattern_override.connector_short", "{1} {0}");
    177  nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
    178                                   mozIOSPreferences::dateTimeFormatStyleShort,
    179                                   nsDependentCString(""), pattern);
    180  ASSERT_NS_SUCCEEDED(nr);
    181  ASSERT_TRUE(pattern.EqualsASCII("yyyy-MM-dd HH:mm:ss"));
    182 
    183  // Reset to date and time to defaults
    184  mozilla::Preferences::ClearUser("intl.date_time.pattern_override.date_short");
    185  mozilla::Preferences::ClearUser("intl.date_time.pattern_override.time_short");
    186 
    187  // Invalid patterns are ignored
    188  mozilla::Preferences::SetCString(
    189      "intl.date_time.pattern_override.connector_short", "hello, world!");
    190  nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
    191                                   mozIOSPreferences::dateTimeFormatStyleShort,
    192                                   nsDependentCString(""), pattern);
    193  ASSERT_NS_SUCCEEDED(nr);
    194  ASSERT_EQ(default_pattern, pattern);
    195 
    196  // Clearing the override results in getting the default pattern back.
    197  mozilla::Preferences::ClearUser(
    198      "intl.date_time.pattern_override.connector_short");
    199  nr = osprefs->GetDateTimePattern(mozIOSPreferences::dateTimeFormatStyleShort,
    200                                   mozIOSPreferences::dateTimeFormatStyleShort,
    201                                   nsDependentCString(""), pattern);
    202  ASSERT_NS_SUCCEEDED(nr);
    203  ASSERT_EQ(default_pattern, pattern);
    204 }