Commit 0e83b4fb authored by Yue Li's avatar Yue Li Committed by Commit Bot

Add localized strings support for chromeos_components_tests

Bug: b/169453041
Test: Run existing tests
Change-Id: I85a2c447098602ddb6b5734af5b1bb05c343d844
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2504596Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Yue Li <updowndota@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822024}
parent b1800f23
......@@ -30,4 +30,8 @@ specific_include_rules = {
".*test\.cc": [
"+services/network/test",
],
".*test_suite\.cc": [
"+ui/base",
],
}
......@@ -9,7 +9,11 @@ assert(is_chromeos, "Non-ChromeOS builds cannot depend on //chromeos")
# To add a unit test to this target, make a "unit_test" source_set in your
# component and add a reference here.
test("chromeos_components_unittests") {
sources = [ "run_all_unittests.cc" ]
sources = [
"chromeos_components_test_suite.cc",
"chromeos_components_test_suite.h",
"run_all_unittests.cc",
]
deps = [
"//base",
......@@ -37,11 +41,17 @@ test("chromeos_components_unittests") {
"//chromeos/components/tether:unit_tests",
"//chromeos/components/trial_group:unit_tests",
"//mojo/core/embedder",
"//ui/base:base",
]
if (!is_official_build) {
deps += [ "//chromeos/components/telemetry_extension_ui:unit_tests" ]
}
data_deps = [
"//chromeos/strings:chromeos_test_strings",
"//ui/resources:ui_test_pak_data",
]
}
group("closure_compile") {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/chromeos_components_test_suite.h"
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/path_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
namespace {
// Initializes the i18n stack and loads the necessary strings. Uses a specific
// locale, so that the tests can compare against golden strings without
// depending on the environment.
void InitI18n() {
base::i18n::SetICUDefaultLocale("en_US");
ui::RegisterPathProvider();
base::FilePath ui_test_pak_path;
ASSERT_TRUE(base::PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
base::FilePath dir_module_path;
ASSERT_TRUE(base::PathService::Get(base::DIR_MODULE, &dir_module_path));
base::FilePath chromeos_test_strings_path =
dir_module_path.Append(FILE_PATH_LITERAL("chromeos_test_strings.pak"));
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
chromeos_test_strings_path, ui::SCALE_FACTOR_NONE);
}
} // namespace
ChromeosComponentsTestSuite::ChromeosComponentsTestSuite(int argc, char** argv)
: TestSuite(argc, argv) {}
ChromeosComponentsTestSuite::~ChromeosComponentsTestSuite() = default;
void ChromeosComponentsTestSuite::Initialize() {
base::TestSuite::Initialize();
InitI18n();
}
void ChromeosComponentsTestSuite::Shutdown() {
ui::ResourceBundle::CleanupSharedInstance();
base::TestSuite::Shutdown();
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_CHROMEOS_COMPONENTS_TEST_SUITE_H_
#define CHROMEOS_COMPONENTS_CHROMEOS_COMPONENTS_TEST_SUITE_H_
#include "base/test/test_suite.h"
class ChromeosComponentsTestSuite : public base::TestSuite {
public:
ChromeosComponentsTestSuite(int argc, char** argv);
ChromeosComponentsTestSuite(const ChromeosComponentsTestSuite&) = delete;
ChromeosComponentsTestSuite& operator=(const ChromeosComponentsTestSuite&) =
delete;
~ChromeosComponentsTestSuite() override;
protected:
// base::TestSuite:
void Initialize() override;
void Shutdown() override;
};
#endif // CHROMEOS_COMPONENTS_CHROMEOS_COMPONENTS_TEST_SUITE_H_
......@@ -43,7 +43,8 @@ class MockResultLoaderDelegate : public ResultLoader::ResultLoaderDelegate {
};
MATCHER_P(QuickAnswerEqual, quick_answer, "") {
return (arg->primary_answer == quick_answer->primary_answer);
return (arg->primary_answer == quick_answer->primary_answer &&
arg->secondary_answer == quick_answer->secondary_answer);
}
MATCHER_P(QuickAnswersRequestEqual, quick_answers_request, "") {
......
......@@ -44,10 +44,6 @@ constexpr base::StringPiece kSourceLanguageKey = "source";
constexpr base::StringPiece kTargetLanguageKey = "target";
std::string BuildTitleText(const IntentInfo& intent_info) {
// TODO(b/169453041): Add test support for localized strings.
if (!ui::ResourceBundle::HasSharedInstance())
return std::string();
auto locale_name = l10n_util::GetDisplayNameForLocale(
intent_info.source_language, intent_info.target_language, true);
return l10n_util::GetStringFUTF8(IDS_QUICK_ANSWERS_TRANSLATION_TITLE_TEXT,
......
......@@ -37,6 +37,7 @@ constexpr char kValidResponse[] = R"(
}
)";
constexpr char kTestTranslationTitle[] = "test · inglés";
constexpr char kTestTranslationResult[] = "prueba";
const auto kTestTranslationIntent =
......@@ -87,6 +88,7 @@ TEST_F(TranslationResultLoaderTest, Success) {
std::unique_ptr<QuickAnswer> expected_quick_answer =
std::make_unique<QuickAnswer>();
expected_quick_answer->primary_answer = kTestTranslationResult;
expected_quick_answer->secondary_answer = kTestTranslationTitle;
test_url_loader_factory_.AddResponse(kCloudTranslationApiRequest,
kValidResponse);
EXPECT_CALL(
......
......@@ -6,6 +6,7 @@
#include "base/test/launcher/unit_test_launcher.h"
#include "base/test/test_suite.h"
#include "build/chromeos_buildflags.h"
#include "chromeos/components/chromeos_components_test_suite.h"
#include "mojo/core/embedder/embedder.h"
#if BUILDFLAG(IS_CHROMEOS_DEVICE)
......@@ -17,8 +18,8 @@ int main(int argc, char** argv) {
// Some unit tests make Mojo calls.
mojo::core::Init();
base::TestSuite test_suite(argc, argv);
return base::LaunchUnitTests(
argc, argv,
base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite)));
ChromeosComponentsTestSuite test_suite(argc, argv);
return base::LaunchUnitTests(argc, argv,
base::BindOnce(&ChromeosComponentsTestSuite::Run,
base::Unretained(&test_suite)));
}
......@@ -32,8 +32,4 @@ source_set("unit_tests") {
"//testing/gtest",
"//ui/base",
]
data_deps = [
"//chromeos/strings:chromeos_test_strings",
"//ui/resources:ui_test_pak_data",
]
}
......@@ -4,50 +4,22 @@
#include "chromeos/components/security_token_pin/error_generator.h"
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/path_service.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/security_token_pin/constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/scale_factor.h"
#include "ui/base/ui_base_paths.h"
namespace chromeos {
namespace security_token_pin {
class SecurityTokenPinErrorGeneratorTest : public testing::Test {
protected:
SecurityTokenPinErrorGeneratorTest() { InitI18n(); }
~SecurityTokenPinErrorGeneratorTest() override {
ui::ResourceBundle::CleanupSharedInstance();
}
private:
// Initializes the i18n stack and loads the necessary strings. Uses a specific
// locale, so that the tests can compare against golden strings without
// depending on the environment.
void InitI18n() {
base::i18n::SetICUDefaultLocale("en_US");
ui::RegisterPathProvider();
base::FilePath ui_test_pak_path;
ASSERT_TRUE(base::PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
base::FilePath dir_module_path;
ASSERT_TRUE(base::PathService::Get(base::DIR_MODULE, &dir_module_path));
base::FilePath chromeos_test_strings_path =
dir_module_path.Append(FILE_PATH_LITERAL("chromeos_test_strings.pak"));
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
chromeos_test_strings_path, ui::SCALE_FACTOR_NONE);
}
SecurityTokenPinErrorGeneratorTest() = default;
SecurityTokenPinErrorGeneratorTest(
const SecurityTokenPinErrorGeneratorTest&) = delete;
SecurityTokenPinErrorGeneratorTest& operator=(
const SecurityTokenPinErrorGeneratorTest&) = delete;
};
// Tests that an empty message is returned when there's neither an error nor the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment