1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/on_device_translation/translation_manager_util.h"
#include <string>
#include "base/logging.h"
#include "base/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace on_device_translation {
class TranslationManagerUtilTest : public testing::Test {
public:
TranslationManagerUtilTest() = default;
~TranslationManagerUtilTest() override = default;
};
TEST_F(TranslationManagerUtilTest,
LookupMatchingLocaleByBestFitFindsSupportedLanguageTag) {
std::vector<std::string> en_variations{
"en",
"en-Latn",
"en-Latn-GB",
"en-GB",
"en-fonipa-scouse",
"en-Latn-fonipa-scouse",
"en-Latn-GB-fonipa-scouse",
"en-Latn-x-this-is-a-private-use-extensio-n",
};
for (const auto& variant : en_variations) {
EXPECT_EQ(LookupMatchingLocaleByBestFit(kSupportedLanguageCodes, variant),
"en");
EXPECT_EQ(LookupMatchingLocaleByBestFit(std::set<std::string>(), variant),
std::nullopt);
}
std::vector<std::string> es_variations{
"es",
"es-419",
"es-ES",
"es-ES-1979",
};
for (const auto& variant : es_variations) {
EXPECT_EQ(LookupMatchingLocaleByBestFit(kSupportedLanguageCodes, variant),
"es");
EXPECT_EQ(LookupMatchingLocaleByBestFit(std::set<std::string>(), variant),
std::nullopt);
}
}
TEST_F(TranslationManagerUtilTest,
LookupMatchingLocaleByBestFitReturnsNulloptForUnsupportedLanguageTags) {
std::vector<std::string> language_tags{
"xx",
"xx-Latn",
"xx-Latn-GB",
"xx-GB",
"xx-fonipa-scouse",
"xx-Latn-fonipa-scouse",
"xx-Latn-GB-fonipa-scouse",
"xx-Latn-x-this-is-a-private-use-extensio-n",
"xx-419",
"xx-ES",
"xx-ES-1979",
};
for (const auto& language_tag : language_tags) {
EXPECT_EQ(
LookupMatchingLocaleByBestFit(kSupportedLanguageCodes, language_tag),
std::nullopt);
}
}
TEST_F(TranslationManagerUtilTest,
LookupMatchingLocaleByBestFitReturnsMostSpecificLanguageTag) {
std::vector<std::string> zh_variations{
"zh",
"zh-Latn",
"zh-Latn-GB",
"zh-GB",
"zh-fonipa-scouse",
"zh-Latn-fonipa-scouse",
"zh-Latn-GB-fonipa-scouse",
"zh-Latn-x-this-is-a-private-use-extensio-n",
"zh-419",
"zh-ES",
"zh-ES-1979",
};
for (const auto& variant : zh_variations) {
EXPECT_EQ(LookupMatchingLocaleByBestFit(kSupportedLanguageCodes, variant),
"zh");
}
std::vector<std::string> zh_hant_variations{
"zh-Hant",
"zh-Hant-GB",
"zh-Hant-fonipa-scouse",
"zh-Hant-GB-fonipa-scouse",
"zh-Hant-x-this-is-a-private-use-extensio-n",
};
for (const auto& variant : zh_hant_variations) {
EXPECT_EQ(LookupMatchingLocaleByBestFit(kSupportedLanguageCodes, variant),
"zh-Hant");
}
}
} // namespace on_device_translation
|