Commit aedef32f authored by Yue Li's avatar Yue Li Committed by Commit Bot

Quick Answers: Add unit tests for Translation response parser

Bug: b/169453041
Test: Run TranslationResponseParserTest.* in chromeos_components_unittests
Change-Id: I08315718f29b8785150fddc765552d73c88bab96
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2493434Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Yue Li <updowndota@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821003}
parent 8a2d5959
...@@ -72,6 +72,7 @@ source_set("unit_tests") { ...@@ -72,6 +72,7 @@ source_set("unit_tests") {
"search_result_parsers/unit_conversion_result_parser_unittest.cc", "search_result_parsers/unit_conversion_result_parser_unittest.cc",
"test/test_helpers.cc", "test/test_helpers.cc",
"test/test_helpers.h", "test/test_helpers.h",
"translation_response_parser_unittest.cc",
"translation_result_loader_unittest.cc", "translation_result_loader_unittest.cc",
"understanding/intent_generator_unittest.cc", "understanding/intent_generator_unittest.cc",
] ]
......
// 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/quick_answers/translation_response_parser.h"
#include <memory>
#include <string>
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "chromeos/components/quick_answers/quick_answers_model.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace quick_answers {
class TranslationResponseParserTest : public testing::Test {
public:
TranslationResponseParserTest() = default;
TranslationResponseParserTest(const TranslationResponseParserTest&) = delete;
TranslationResponseParserTest& operator=(
const TranslationResponseParserTest&) = delete;
// testing::Test:
void SetUp() override {
translation_response_parser_ =
std::make_unique<TranslationResponseParser>(base::BindOnce(
&TranslationResponseParserTest::TranslationResponseParserCallback,
base::Unretained(this)));
run_loop_ = std::make_unique<base::RunLoop>();
}
void TranslationResponseParserCallback(
std::unique_ptr<QuickAnswer> quick_answer) {
quick_answer_ = std::move(quick_answer);
run_loop_->Quit();
}
void WaitForResponse() {
run_loop_->Run();
run_loop_ = std::make_unique<base::RunLoop>();
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_;
std::unique_ptr<TranslationResponseParser> translation_response_parser_;
std::unique_ptr<QuickAnswer> quick_answer_;
data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
std::unique_ptr<base::RunLoop> run_loop_;
};
TEST_F(TranslationResponseParserTest, ProcessResponseSuccess) {
constexpr char kTranslationResponse[] = R"(
{
"data": {
"translations": [
{
"translatedText": "translated text"
}
]
}
}
)";
translation_response_parser_->ProcessResponse(
std::make_unique<std::string>(kTranslationResponse));
WaitForResponse();
EXPECT_TRUE(quick_answer_);
EXPECT_EQ("translated text", quick_answer_->primary_answer);
EXPECT_EQ(ResultType::kTranslationResult, quick_answer_->result_type);
}
TEST_F(TranslationResponseParserTest, ProcessResponseNoResults) {
constexpr char kTranslationResponse[] = R"(
{}
)";
translation_response_parser_->ProcessResponse(
std::make_unique<std::string>(kTranslationResponse));
WaitForResponse();
EXPECT_FALSE(quick_answer_);
}
TEST_F(TranslationResponseParserTest, ProcessResponseInvalidResponse) {
translation_response_parser_->ProcessResponse(
std::make_unique<std::string>("results {}"));
WaitForResponse();
EXPECT_FALSE(quick_answer_);
}
} // namespace quick_answers
} // namespace chromeos
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