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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
// Copyright 2019 The Chromium Authors
// 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/search_result_parsers/definition_result_parser.h"
#include <memory>
#include <string>
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chromeos/components/quick_answers/quick_answers_model.h"
#include "chromeos/components/quick_answers/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/color/color_id.h"
namespace quick_answers {
namespace {
constexpr char kPhoneticsAudioUrlWithoutProtocol[] = "//example.com/audio";
constexpr char kPhoneticsAudioUrlWithProtocol[] = "https://example.com/audio";
using base::Value;
} // namespace
class DefinitionResultParserTest : public testing::Test {
public:
DefinitionResultParserTest()
: parser_(std::make_unique<DefinitionResultParser>()) {}
DefinitionResultParserTest(const DefinitionResultParserTest&) = delete;
DefinitionResultParserTest& operator=(const DefinitionResultParserTest&) =
delete;
protected:
Value::Dict BuildDictionaryResult(
const std::string& query_term,
const std::string& phonetic_str,
const std::string& definition,
const std::string& word_class,
const std::string& sample_sentence = std::string(),
const std::vector<std::string>& synonyms_list = {},
const std::vector<std::string>& subsenses_list = {}) {
Value::Dict result;
if (!query_term.empty()) {
result.SetByDottedPath("dictionaryResult.queryTerm", query_term);
}
// Build definition entry.
Value::List entries;
Value::Dict entry;
entry.Set("locale", "en");
// Build phonetics.
if (!phonetic_str.empty()) {
Value::List phonetics;
Value::Dict phonetic;
phonetic.Set("text", phonetic_str);
phonetic.Set("oxfordAudio", kPhoneticsAudioUrlWithoutProtocol);
phonetics.Append(std::move(phonetic));
entry.Set("phonetics", std::move(phonetics));
}
// Build senses.
if (!definition.empty()) {
Value::List sense_families;
Value::Dict sense_family;
Value::List senses;
Value::Dict sense;
sense.SetByDottedPath("definition.text", definition);
if (!sample_sentence.empty()) {
Value::List example_groups;
Value::Dict example_group;
Value::List examples;
examples.Append(sample_sentence);
example_group.Set("examples", std::move(examples));
example_groups.Append(std::move(example_group));
sense.Set("exampleGroups", std::move(example_groups));
}
if (!synonyms_list.empty()) {
Value::List thesaurus_entries;
Value::Dict thesaurus_entry;
Value::List synonyms;
Value::Dict synonym;
Value::List nyms;
for (std::string synonym_term : synonyms_list) {
Value::Dict nym;
nym.Set("nym", synonym_term);
nyms.Append(std::move(nym));
}
synonym.Set("nyms", std::move(nyms));
synonyms.Append(std::move(synonym));
thesaurus_entry.Set("synonyms", std::move(synonyms));
thesaurus_entries.Append(std::move(thesaurus_entry));
sense.Set("thesaurusEntries", std::move(thesaurus_entries));
}
if (!subsenses_list.empty()) {
Value::List subsenses;
for (std::string subsense_term : subsenses_list) {
Value::Dict subsense;
subsense.SetByDottedPath("definition.text", subsense_term);
subsenses.Append(std::move(subsense));
}
sense.Set("subsenses", std::move(subsenses));
}
senses.Append(std::move(sense));
sense_family.Set("senses", std::move(senses));
if (!word_class.empty()) {
Value::List parts_of_speech;
Value::Dict part_of_speech;
part_of_speech.Set("value", word_class);
parts_of_speech.Append(std::move(part_of_speech));
sense_family.Set("partsOfSpeech", std::move(parts_of_speech));
}
sense_families.Append(std::move(sense_family));
entry.Set("senseFamilies", std::move(sense_families));
}
entries.Append(std::move(entry));
result.SetByDottedPath("dictionaryResult.entries", std::move(entries));
return result;
}
void SetHeadWord(Value::Dict& result, const std::string& headword) {
(*result.FindListByDottedPath("dictionaryResult.entries"))[0].GetDict().Set(
"headword", headword);
}
std::unique_ptr<DefinitionResultParser> parser_;
};
TEST_F(DefinitionResultParserTest, Success) {
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"unfathomable", /*phonetic_str=*/"ˌənˈfaT͟Həməb(ə)",
/*definition=*/"incapable of being fully explored or understood.",
/*word_class=*/"adjective");
QuickAnswer quick_answer;
EXPECT_TRUE(parser_->Parse(result, &quick_answer));
const auto& expected_title = "unfathomable · /ˌənˈfaT͟Həməb(ə)/";
const auto& expected_answer =
"incapable of being fully explored or understood.";
EXPECT_EQ(ResultType::kDefinitionResult, quick_answer.result_type);
EXPECT_EQ(1u, quick_answer.title.size());
EXPECT_EQ(1u, quick_answer.first_answer_row.size());
auto* answer =
static_cast<QuickAnswerText*>(quick_answer.first_answer_row[0].get());
EXPECT_EQ(expected_answer,
GetQuickAnswerTextForTesting(quick_answer.first_answer_row));
EXPECT_EQ(ui::kColorLabelForegroundSecondary, answer->color_id);
auto* title = static_cast<QuickAnswerText*>(quick_answer.title[0].get());
EXPECT_EQ(expected_title, GetQuickAnswerTextForTesting(quick_answer.title));
EXPECT_EQ(ui::kColorLabelForeground, title->color_id);
// Expectations for `StructuredResult`.
std::unique_ptr<StructuredResult> structured_result =
parser_->ParseInStructuredResult(result);
ASSERT_TRUE(structured_result);
ASSERT_TRUE(structured_result->definition_result);
DefinitionResult* definition_result =
structured_result->definition_result.get();
EXPECT_EQ(definition_result->word, "unfathomable");
EXPECT_EQ(definition_result->word_class, "adjective");
// `PhoneticsInfo::query_text` is headword. It's a query text for TTS. We
// should not expect this test case response from the server as either
// `query_text` or `phonetics_audio` should be filled.
EXPECT_TRUE(definition_result->phonetics_info.query_text.empty());
EXPECT_EQ(definition_result->phonetics_info.text, "ˌənˈfaT͟Həməb(ə)");
EXPECT_EQ(definition_result->phonetics_info.locale, "en");
EXPECT_EQ(definition_result->phonetics_info.phonetics_audio,
GURL(kPhoneticsAudioUrlWithProtocol));
EXPECT_EQ(definition_result->sense.definition, expected_answer);
}
TEST_F(DefinitionResultParserTest, SuccessWithRichCardInfo) {
const std::vector<std::string> synonyms_list = {"fine", "nice", "minute",
"precise"};
const std::vector<std::string> subsenses_list = {
"(of a mixture or effect) delicately complex and understated.",
"making use of clever and indirect methods to achieve something.",
"capable of making fine distinctions.",
"arranged in an ingenious and elaborate way."};
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"subtle", /*phonetic_str=*/"ˈsəd(ə)l",
/*definition=*/
"(especially of a change or distinction) so delicate or precise as to be "
"difficult to analyze or describe.",
/*word_class=*/"adjective",
/*sample_sentence=*/"his language expresses rich and subtle meanings",
synonyms_list, subsenses_list);
QuickAnswer quick_answer;
EXPECT_TRUE(parser_->Parse(result, &quick_answer));
EXPECT_EQ(ResultType::kDefinitionResult, quick_answer.result_type);
// Expectations for `StructuredResult`.
std::unique_ptr<StructuredResult> structured_result =
parser_->ParseInStructuredResult(result);
ASSERT_TRUE(structured_result);
ASSERT_TRUE(structured_result->definition_result);
DefinitionResult* definition_result =
structured_result->definition_result.get();
EXPECT_EQ(definition_result->word, "subtle");
EXPECT_EQ(definition_result->word_class, "adjective");
// `PhoneticsInfo::query_text` is headword. It's a query text for TTS. We
// should not expect this test case response from the server as either
// `query_text` or `phonetics_audio` should be filled.
EXPECT_TRUE(definition_result->phonetics_info.query_text.empty());
EXPECT_EQ(definition_result->phonetics_info.text, "ˈsəd(ə)l");
EXPECT_EQ(definition_result->phonetics_info.locale, "en");
EXPECT_EQ(definition_result->phonetics_info.phonetics_audio,
GURL(kPhoneticsAudioUrlWithProtocol));
EXPECT_EQ(
definition_result->sense.definition,
"(especially of a change or distinction) so delicate or precise as to be "
"difficult to analyze or describe.");
// Rich card specific `StructuredResult` fields.
EXPECT_TRUE(definition_result->sense.sample_sentence.has_value());
EXPECT_EQ(definition_result->sense.sample_sentence,
"his language expresses rich and subtle meanings");
EXPECT_TRUE(definition_result->sense.synonyms_list.has_value());
const std::vector<std::string> expected_synonyms_list = {"fine", "nice",
"minute"};
EXPECT_EQ(definition_result->sense.synonyms_list, expected_synonyms_list);
EXPECT_TRUE(definition_result->subsenses_list.has_value());
const std::vector<std::string> expected_subsenses_list = {
"(of a mixture or effect) delicately complex and understated.",
"making use of clever and indirect methods to achieve something.",
"capable of making fine distinctions."};
std::vector<std::string> actual_subsenses_list = {};
for (quick_answers::Sense subsense :
definition_result->subsenses_list.value()) {
actual_subsenses_list.push_back(subsense.definition);
}
EXPECT_EQ(actual_subsenses_list, expected_subsenses_list);
}
TEST_F(DefinitionResultParserTest, EmptyValue) {
Value::Dict result;
QuickAnswer quick_answer;
EXPECT_FALSE(parser_->Parse(result, &quick_answer));
}
TEST_F(DefinitionResultParserTest, NoQueryTerm) {
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"", /*phonetic_str=*/"ˌənˈfaT͟Həməb(ə)",
/*definition=*/"incapable of being fully explored or understood.",
/*word_class=*/"adjective");
QuickAnswer quick_answer;
EXPECT_FALSE(parser_->Parse(result, &quick_answer));
}
TEST_F(DefinitionResultParserTest, NoQueryTermShouldFallbackToHeadword) {
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"", /*phonetic_str=*/"ˌənˈfaT͟Həməb(ə)",
/*definition=*/"incapable of being fully explored or understood.",
/*word_class=*/"adjective");
SetHeadWord(result, "unfathomable");
QuickAnswer quick_answer;
EXPECT_TRUE(parser_->Parse(result, &quick_answer));
const auto& expected_title = "unfathomable · /ˌənˈfaT͟Həməb(ə)/";
EXPECT_EQ(expected_title, GetQuickAnswerTextForTesting(quick_answer.title));
}
TEST_F(DefinitionResultParserTest, ShouldPrioritizeQueryTerm) {
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"Unfathomable", /*phonetic_str=*/"ˌənˈfaT͟Həməb(ə)",
/*definition=*/"incapable of being fully explored or understood.",
/*word_class=*/"adjective");
SetHeadWord(result, "Unfathomable");
QuickAnswer quick_answer;
EXPECT_TRUE(parser_->Parse(result, &quick_answer));
const auto& expected_title = "Unfathomable · /ˌənˈfaT͟Həməb(ə)/";
EXPECT_EQ(expected_title, GetQuickAnswerTextForTesting(quick_answer.title));
}
TEST_F(DefinitionResultParserTest, NoPhonetic) {
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"unfathomable", /*phonetic_str=*/"",
/*definition=*/"incapable of being fully explored or understood.",
/*word_class=*/"adjective");
QuickAnswer quick_answer;
EXPECT_TRUE(parser_->Parse(result, &quick_answer));
const auto& expected_title = "unfathomable";
const auto& expected_answer =
"incapable of being fully explored or understood.";
EXPECT_EQ(ResultType::kDefinitionResult, quick_answer.result_type);
EXPECT_EQ(expected_answer,
GetQuickAnswerTextForTesting(quick_answer.first_answer_row));
EXPECT_EQ(expected_title, GetQuickAnswerTextForTesting(quick_answer.title));
}
TEST_F(DefinitionResultParserTest, NoDefinition) {
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"unfathomable", /*phonetic_str=*/"ˌənˈfaT͟Həməb(ə)l",
/*definition=*/"", /*word_class=*/"adjective");
QuickAnswer quick_answer;
EXPECT_FALSE(parser_->Parse(result, &quick_answer));
}
TEST_F(DefinitionResultParserTest, NoWordClass) {
Value::Dict result = BuildDictionaryResult(
/*query_term=*/"unfathomable", /*phonetic_str=*/"ˌənˈfaT͟Həməb(ə)l",
/*definition=*/"incapable of being fully explored or understood.",
/*word_class=*/"");
QuickAnswer quick_answer;
EXPECT_FALSE(parser_->Parse(result, &quick_answer));
}
} // namespace quick_answers
|