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