1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/locale_change_guard.h"
9 #include "base/macros.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/base/l10n/l10n_util.h"
15 // These languages require user notification when locale is automatically
16 // switched between different regions within the same language.
17 const char* const kShowNotificationLanguages
[] = {
79 "nb", // Norwegian (Bokmal)
82 "nn", // Norwegian (Nynorsk)
96 "sh", // Serbo-Croatian
130 } // anonymous namespace
134 TEST(LocaleChangeGuardTest
, ShowNotificationLocaleChanged
) {
135 // "en" is used as "global default" in many places.
137 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en", "it"));
139 LocaleChangeGuard::ShouldShowLocaleChangeNotification("it", "en"));
141 // Between two latin locales.
143 LocaleChangeGuard::ShouldShowLocaleChangeNotification("fr", "it"));
145 LocaleChangeGuard::ShouldShowLocaleChangeNotification("it", "fr"));
147 // en <-> non-latin locale
149 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en", "zh"));
151 LocaleChangeGuard::ShouldShowLocaleChangeNotification("zh", "en"));
153 // latin <-> non-latin locale
155 LocaleChangeGuard::ShouldShowLocaleChangeNotification("fr", "zh"));
157 LocaleChangeGuard::ShouldShowLocaleChangeNotification("zh", "fr"));
161 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en", "en"));
163 LocaleChangeGuard::ShouldShowLocaleChangeNotification("fr", "fr"));
165 LocaleChangeGuard::ShouldShowLocaleChangeNotification("zh", "zh"));
167 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en", "en-US"));
169 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en-GB", "en-US"));
171 // Different regions within the same language
173 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en", "en-au"));
175 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en-AU", "en"));
177 LocaleChangeGuard::ShouldShowLocaleChangeNotification("en-AU", "en-GB"));
180 LocaleChangeGuard::ShouldShowLocaleChangeNotification("zh", "zh-CN"));
182 LocaleChangeGuard::ShouldShowLocaleChangeNotification("zh-CN", "zh-TW"));
184 LocaleChangeGuard::ShouldShowLocaleChangeNotification("es", "es-419"));
186 LocaleChangeGuard::ShouldShowLocaleChangeNotification("es", "es-ES"));
189 TEST(LocaleChangeGuardTest
, ShowNotificationLocaleChangedList
) {
190 for (size_t i
= 0; i
< l10n_util::GetAcceptLanguageListSizeForTesting();
192 const char* const locale
= l10n_util::GetAcceptLanguageListForTesting()[i
];
193 const char* const dash
= strchr(locale
, '-');
194 const std::string language
=
195 (dash
? std::string(locale
, dash
- locale
) : std::string(locale
));
197 const char* const* allowed_begin
= kShowNotificationLanguages
;
198 const char* const* allowed_end
=
199 kShowNotificationLanguages
+ arraysize(kShowNotificationLanguages
);
200 const bool notification_allowed
=
201 (std::find(allowed_begin
, allowed_end
, language
) != allowed_end
);
203 const char* const* skipped_begin
=
204 LocaleChangeGuard::GetSkipShowNotificationLanguagesForTesting();
205 const char* const* skipped_end
=
207 LocaleChangeGuard::GetSkipShowNotificationLanguagesSizeForTesting();
208 const bool notification_skipped
=
209 (std::find(skipped_begin
, skipped_end
, language
) != skipped_end
);
211 EXPECT_TRUE(notification_allowed
^ notification_skipped
)
212 << "Language '" << language
<< "' (from locale '" << locale
213 << "') must be in exactly one list: either "
214 "kSkipShowNotificationLanguages (found=" << notification_skipped
215 << ") or kShowNotificationLanguages (found=" << notification_allowed
220 } // namespace chromeos