Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
mlcui | a5e41ef | 2021-03-15 23:10:30 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | #include "base/threading/thread_restrictions.h" |
| 8 | #include "build/build_config.h" |
| 9 | #include "chrome/test/base/in_process_browser_test.h" |
| 10 | #include "content/public/test/browser_test.h" |
| 11 | #include "ui/base/l10n/l10n_util.h" |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | class L10nUtilBrowserTest : public InProcessBrowserTest { |
| 16 | public: |
| 17 | L10nUtilBrowserTest() = default; |
| 18 | ~L10nUtilBrowserTest() override = default; |
| 19 | L10nUtilBrowserTest(const L10nUtilBrowserTest&) = delete; |
| 20 | L10nUtilBrowserTest& operator=(const L10nUtilBrowserTest&) = delete; |
| 21 | }; |
| 22 | |
| 23 | } // namespace |
| 24 | |
| 25 | // Tests whether CheckAndResolveLocale returns the same result with and without |
| 26 | // I/O. |
| 27 | IN_PROC_BROWSER_TEST_F(L10nUtilBrowserTest, CheckAndResolveLocaleIO) { |
| 28 | base::ScopedAllowBlockingForTesting allow_io; |
| 29 | std::vector<std::string> accept_languages; |
| 30 | l10n_util::GetAcceptLanguages(&accept_languages); |
| 31 | |
| 32 | for (const std::string& locale : accept_languages) { |
| 33 | std::string resolved_locale; |
| 34 | bool resolved = l10n_util::CheckAndResolveLocale(locale, &resolved_locale, |
| 35 | /*perform_io=*/false); |
| 36 | std::string resolved_locale_with_io; |
| 37 | bool resolved_with_io = l10n_util::CheckAndResolveLocale( |
| 38 | locale, &resolved_locale_with_io, /*perform_io=*/true); |
| 39 | |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 40 | #if BUILDFLAG(IS_ANDROID) |
mlcui | a5e41ef | 2021-03-15 23:10:30 | [diff] [blame] | 41 | // False positives may occur on Android and iOS (and chrome/ isn't used on |
| 42 | // iOS, so we only need to check for Android). |
| 43 | // False negatives should never occur - so if the call without IO returns |
| 44 | // false, the call with I/O must return false too. |
| 45 | if (!resolved) { |
| 46 | EXPECT_FALSE(resolved_with_io) |
| 47 | << "Couldn't resolve " << locale |
| 48 | << " without IO, but resolving with IO successfully returned " |
| 49 | << resolved_locale_with_io; |
| 50 | } |
| 51 | // If CheckAndResolveLocale returns the same locale as the input, that means |
| 52 | // that we have strings for that locale. False negatives should never occur |
| 53 | // like this as well - if the call without I/O returns something different |
| 54 | // to the input, the same should apply to the call with I/O. |
| 55 | if (resolved_locale != locale) { |
| 56 | EXPECT_NE(resolved_locale_with_io, locale) |
| 57 | << "Resolving " << locale |
| 58 | << " without IO returned a different locale (" |
| 59 | << (resolved_locale.empty() ? "an empty string" : resolved_locale) |
| 60 | << "), but resolving with IO returned the same locale"; |
| 61 | } |
| 62 | #else |
| 63 | // On other platforms, the two function calls should be identical. |
| 64 | EXPECT_EQ(resolved, resolved_with_io); |
| 65 | EXPECT_EQ(resolved_locale, resolved_locale_with_io); |
| 66 | #endif |
| 67 | } |
| 68 | } |