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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
|
// Copyright (c) 2013 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.
//
// Unit tests for |FeedbackSender| object.
#include "components/spellcheck/browser/feedback_sender.h"
#include <stddef.h>
#include <stdint.h>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/field_trial.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "components/spellcheck/common/spellcheck_common.h"
#include "components/spellcheck/common/spellcheck_marker.h"
#include "components/spellcheck/common/spellcheck_result.h"
#include "components/spellcheck/common/spellcheck_switches.h"
#include "components/variations/entropy_provider.h"
#include "content/public/test/test_browser_thread.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace spellcheck {
namespace {
const char kCountry[] = "USA";
const char kLanguage[] = "en";
const char kText[] = "Helllo world.";
const int kMisspellingLength = 6;
const int kMisspellingStart = 0;
const int kRendererProcessId = 0;
const int kUrlFetcherId = 0;
// Builds a simple spellcheck result.
SpellCheckResult BuildSpellCheckResult() {
return SpellCheckResult(SpellCheckResult::SPELLING,
kMisspellingStart,
kMisspellingLength,
base::UTF8ToUTF16("Hello"));
}
// Returns the number of times that |needle| appears in |haystack| without
// overlaps. For example, CountOccurences("bananana", "nana") returns 1.
int CountOccurences(const std::string& haystack, const std::string& needle) {
int number_of_occurrences = 0;
for (size_t pos = haystack.find(needle);
pos != std::string::npos;
pos = haystack.find(needle, pos + needle.length())) {
++number_of_occurrences;
}
return number_of_occurrences;
}
class MockFeedbackSender : public spellcheck::FeedbackSender {
public:
MockFeedbackSender(net::URLRequestContextGetter* request_context,
const std::string& language,
const std::string& country)
: FeedbackSender(request_context, language, country), random_(0) {}
void RandBytes(void* p, size_t len) override {
memset(p, 0, len);
if (len >= sizeof(random_))
*(unsigned*)p = ++random_;
}
private:
// For returning a different value from each call to RandUint64().
unsigned random_;
};
std::string GetMisspellingId(const std::string& raw_data) {
std::unique_ptr<base::Value> parsed_data(
base::JSONReader::Read(raw_data).release());
EXPECT_TRUE(parsed_data.get());
base::DictionaryValue* actual_data;
EXPECT_TRUE(parsed_data->GetAsDictionary(&actual_data));
base::ListValue* suggestions = NULL;
EXPECT_TRUE(actual_data->GetList("params.suggestionInfo", &suggestions));
base::DictionaryValue* suggestion = NULL;
EXPECT_TRUE(suggestions->GetDictionary(0, &suggestion));
std::string value;
EXPECT_TRUE(suggestion->GetString("userMisspellingId", &value));
return value;
}
} // namespace
// A test fixture to help keep tests simple.
class FeedbackSenderTest : public testing::Test {
public:
FeedbackSenderTest() : ui_thread_(content::BrowserThread::UI, &loop_) {
feedback_.reset(new MockFeedbackSender(NULL, kLanguage, kCountry));
feedback_->StartFeedbackCollection();
}
~FeedbackSenderTest() override {}
protected:
// Appends the "--enable-spelling-service-feedback" switch to the
// command-line.
void AppendCommandLineSwitch() {
// The command-line switch is temporary.
// TODO(rouslan): Remove the command-line switch. http://crbug.com/247726
base::CommandLine::ForCurrentProcess()->AppendSwitch(
spellcheck::switches::kEnableSpellingFeedbackFieldTrial);
feedback_.reset(new MockFeedbackSender(NULL, kLanguage, kCountry));
feedback_->StartFeedbackCollection();
}
// Enables the "SpellingServiceFeedback.Enabled" field trial.
void EnableFieldTrial() {
// The field trial is temporary.
// TODO(rouslan): Remove the field trial. http://crbug.com/247726
field_trial_list_.reset(
new base::FieldTrialList(
base::MakeUnique<metrics::SHA1EntropyProvider>("foo")));
field_trial_ = base::FieldTrialList::CreateFieldTrial(
kFeedbackFieldTrialName, kFeedbackFieldTrialEnabledGroupName);
field_trial_->group();
feedback_.reset(new MockFeedbackSender(NULL, kLanguage, kCountry));
feedback_->StartFeedbackCollection();
}
uint32_t AddPendingFeedback() {
std::vector<SpellCheckResult> results(1, BuildSpellCheckResult());
feedback_->OnSpellcheckResults(kRendererProcessId,
base::UTF8ToUTF16(kText),
std::vector<SpellCheckMarker>(),
&results);
return results[0].hash;
}
void ExpireSession() {
feedback_->session_start_ =
base::Time::Now() -
base::TimeDelta::FromHours(spellcheck::kSessionHours);
}
bool UploadDataContains(const std::string& data) const {
const net::TestURLFetcher* fetcher =
fetchers_.GetFetcherByID(kUrlFetcherId);
return fetcher && CountOccurences(fetcher->upload_data(), data) > 0;
}
bool UploadDataContains(const std::string& data,
int number_of_occurrences) const {
const net::TestURLFetcher* fetcher =
fetchers_.GetFetcherByID(kUrlFetcherId);
return fetcher && CountOccurences(fetcher->upload_data(), data) ==
number_of_occurrences;
}
// Returns true if the feedback sender would be uploading data now. The test
// does not open network connections.
bool IsUploadingData() const {
return !!fetchers_.GetFetcherByID(kUrlFetcherId);
}
void ClearUploadData() {
fetchers_.RemoveFetcherFromMap(kUrlFetcherId);
}
std::string GetUploadData() const {
const net::TestURLFetcher* fetcher =
fetchers_.GetFetcherByID(kUrlFetcherId);
return fetcher ? fetcher->upload_data() : std::string();
}
void AdjustUpdateTime(base::TimeDelta offset) {
feedback_->last_salt_update_ += offset;
}
std::unique_ptr<MockFeedbackSender> feedback_;
private:
base::MessageLoop loop_;
content::TestBrowserThread ui_thread_;
std::unique_ptr<base::FieldTrialList> field_trial_list_;
scoped_refptr<base::FieldTrial> field_trial_;
net::TestURLFetcherFactory fetchers_;
};
// Do not send data if there's no feedback.
TEST_F(FeedbackSenderTest, NoFeedback) {
EXPECT_FALSE(IsUploadingData());
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_FALSE(IsUploadingData());
}
// Do not send data if not aware of which markers are still in the document.
TEST_F(FeedbackSenderTest, NoDocumentMarkersReceived) {
EXPECT_FALSE(IsUploadingData());
uint32_t hash = AddPendingFeedback();
EXPECT_FALSE(IsUploadingData());
static const int kSuggestionIndex = 1;
feedback_->SelectedSuggestion(hash, kSuggestionIndex);
EXPECT_FALSE(IsUploadingData());
}
// Send PENDING feedback message if the marker is still in the document, and the
// user has not performed any action on it.
TEST_F(FeedbackSenderTest, PendingFeedback) {
uint32_t hash = AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>(1, hash));
EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\""));
}
TEST_F(FeedbackSenderTest, IdenticalFeedback) {
std::vector<uint32_t> hashes;
hashes.push_back(AddPendingFeedback());
hashes.push_back(AddPendingFeedback());
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, hashes);
std::string actual_data = GetUploadData();
std::unique_ptr<base::DictionaryValue> actual(
static_cast<base::DictionaryValue*>(
base::JSONReader::Read(GetUploadData()).release()));
base::ListValue* suggestions = NULL;
ASSERT_TRUE(actual->GetList("params.suggestionInfo", &suggestions));
base::DictionaryValue* suggestion0 = NULL;
ASSERT_TRUE(suggestions->GetDictionary(0, &suggestion0));
base::DictionaryValue* suggestion1 = NULL;
ASSERT_TRUE(suggestions->GetDictionary(0, &suggestion1));
std::string value0, value1;
ASSERT_TRUE(suggestion0->GetString("userMisspellingId", &value0));
ASSERT_TRUE(suggestion1->GetString("userMisspellingId", &value1));
EXPECT_EQ(value0, value1);
base::ListValue* suggestion_ids = NULL;
ASSERT_TRUE(suggestion0->GetList("userSuggestionId", &suggestion_ids));
ASSERT_TRUE(suggestion_ids->GetString(0, &value0));
ASSERT_TRUE(suggestion1->GetList("userSuggestionId", &suggestion_ids));
ASSERT_TRUE(suggestion_ids->GetString(0, &value1));
EXPECT_EQ(value0, value1);
}
TEST_F(FeedbackSenderTest, NonidenticalFeedback) {
std::vector<uint32_t> hashes;
hashes.push_back(AddPendingFeedback());
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, hashes);
std::string raw_data0 = GetUploadData();
hashes.clear();
hashes.push_back(AddPendingFeedback());
AdjustUpdateTime(-base::TimeDelta::FromHours(25));
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, hashes);
std::string raw_data1 = GetUploadData();
std::string value0(GetMisspellingId(raw_data0));
std::string value1(GetMisspellingId(raw_data1));
EXPECT_NE(value0, value1);
}
// Send NO_ACTION feedback message if the marker has been removed from the
// document.
TEST_F(FeedbackSenderTest, NoActionFeedback) {
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\""));
}
// Send SELECT feedback message if the user has selected a spelling suggestion.
TEST_F(FeedbackSenderTest, SelectFeedback) {
uint32_t hash = AddPendingFeedback();
static const int kSuggestionIndex = 0;
feedback_->SelectedSuggestion(hash, kSuggestionIndex);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"SELECT\""));
EXPECT_TRUE(UploadDataContains("\"actionTargetIndex\":" +
base::StringPrintf("%d", kSuggestionIndex)));
}
// Send ADD_TO_DICT feedback message if the user has added the misspelled word
// to the custom dictionary.
TEST_F(FeedbackSenderTest, AddToDictFeedback) {
uint32_t hash = AddPendingFeedback();
feedback_->AddedToDictionary(hash);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"ADD_TO_DICT\""));
}
// Send IN_DICTIONARY feedback message if the user has the misspelled word in
// the custom dictionary.
TEST_F(FeedbackSenderTest, InDictionaryFeedback) {
uint32_t hash = AddPendingFeedback();
feedback_->RecordInDictionary(hash);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"IN_DICTIONARY\""));
}
// Send PENDING feedback message if the user saw the spelling suggestion, but
// decided to not select it, and the marker is still in the document.
TEST_F(FeedbackSenderTest, IgnoreFeedbackMarkerInDocument) {
uint32_t hash = AddPendingFeedback();
feedback_->IgnoredSuggestions(hash);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>(1, hash));
EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\""));
}
// Send IGNORE feedback message if the user saw the spelling suggestion, but
// decided to not select it, and the marker is no longer in the document.
TEST_F(FeedbackSenderTest, IgnoreFeedbackMarkerNotInDocument) {
uint32_t hash = AddPendingFeedback();
feedback_->IgnoredSuggestions(hash);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"IGNORE\""));
}
// Send MANUALLY_CORRECTED feedback message if the user manually corrected the
// misspelled word.
TEST_F(FeedbackSenderTest, ManuallyCorrectedFeedback) {
uint32_t hash = AddPendingFeedback();
static const std::string kManualCorrection = "Howdy";
feedback_->ManuallyCorrected(hash, base::ASCIIToUTF16(kManualCorrection));
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"MANUALLY_CORRECTED\""));
EXPECT_TRUE(UploadDataContains("\"actionTargetValue\":\"" +
kManualCorrection + "\""));
}
// Send feedback messages in batch.
TEST_F(FeedbackSenderTest, BatchFeedback) {
std::vector<SpellCheckResult> results;
results.push_back(SpellCheckResult(SpellCheckResult::SPELLING,
kMisspellingStart,
kMisspellingLength,
base::ASCIIToUTF16("Hello")));
static const int kSecondMisspellingStart = 7;
static const int kSecondMisspellingLength = 5;
results.push_back(SpellCheckResult(SpellCheckResult::SPELLING,
kSecondMisspellingStart,
kSecondMisspellingLength,
base::ASCIIToUTF16("world")));
feedback_->OnSpellcheckResults(kRendererProcessId,
base::UTF8ToUTF16(kText),
std::vector<SpellCheckMarker>(),
&results);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\"", 2));
}
// Send a series of PENDING feedback messages and one final NO_ACTION feedback
// message with the same hash identifier for a single misspelling.
TEST_F(FeedbackSenderTest, SameHashFeedback) {
uint32_t hash = AddPendingFeedback();
std::vector<uint32_t> remaining_markers(1, hash);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers);
EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\""));
std::string hash_string = base::StringPrintf("\"suggestionId\":\"%u\"", hash);
EXPECT_TRUE(UploadDataContains(hash_string));
ClearUploadData();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers);
EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\""));
EXPECT_TRUE(UploadDataContains(hash_string));
ClearUploadData();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\""));
EXPECT_TRUE(UploadDataContains(hash_string));
ClearUploadData();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_FALSE(IsUploadingData());
}
// When a session expires:
// 1) Pending feedback is finalized and sent to the server in the last message
// batch in the session.
// 2) No feedback is sent until a spellcheck request happens.
// 3) Existing markers get new hash identifiers.
TEST_F(FeedbackSenderTest, SessionExpirationFeedback) {
std::vector<SpellCheckResult> results(
1,
SpellCheckResult(SpellCheckResult::SPELLING,
kMisspellingStart,
kMisspellingLength,
base::ASCIIToUTF16("Hello")));
feedback_->OnSpellcheckResults(kRendererProcessId,
base::UTF8ToUTF16(kText),
std::vector<SpellCheckMarker>(),
&results);
uint32_t original_hash = results[0].hash;
std::vector<uint32_t> remaining_markers(1, original_hash);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers);
EXPECT_FALSE(UploadDataContains("\"actionType\":\"NO_ACTION\""));
EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\""));
std::string original_hash_string =
base::StringPrintf("\"suggestionId\":\"%u\"", original_hash);
EXPECT_TRUE(UploadDataContains(original_hash_string));
ClearUploadData();
ExpireSession();
// Last message batch in the current session has only finalized messages.
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers);
EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\""));
EXPECT_FALSE(UploadDataContains("\"actionType\":\"PENDING\""));
EXPECT_TRUE(UploadDataContains(original_hash_string));
ClearUploadData();
// The next session starts on the next spellchecker request. Until then,
// there's no more feedback sent.
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers);
EXPECT_FALSE(IsUploadingData());
// The first spellcheck request after session expiration creates different
// document marker hash identifiers.
std::vector<SpellCheckMarker> original_markers(
1, SpellCheckMarker(results[0].hash, results[0].location));
results[0] = SpellCheckResult(SpellCheckResult::SPELLING,
kMisspellingStart,
kMisspellingLength,
base::ASCIIToUTF16("Hello"));
feedback_->OnSpellcheckResults(
kRendererProcessId, base::UTF8ToUTF16(kText), original_markers, &results);
uint32_t updated_hash = results[0].hash;
EXPECT_NE(updated_hash, original_hash);
remaining_markers[0] = updated_hash;
// The first feedback message batch in session |i + 1| has the new document
// marker hash identifiers.
feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers);
EXPECT_FALSE(UploadDataContains("\"actionType\":\"NO_ACTION\""));
EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\""));
EXPECT_FALSE(UploadDataContains(original_hash_string));
std::string updated_hash_string =
base::StringPrintf("\"suggestionId\":\"%u\"", updated_hash);
EXPECT_TRUE(UploadDataContains(updated_hash_string));
}
// First message in session has an indicator.
TEST_F(FeedbackSenderTest, FirstMessageInSessionIndicator) {
// Session 1, message 1
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"isFirstInSession\":true"));
// Session 1, message 2
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"isFirstInSession\":false"));
ExpireSession();
// Session 1, message 3 (last)
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"isFirstInSession\":false"));
// Session 2, message 1
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"isFirstInSession\":true"));
// Session 2, message 2
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"isFirstInSession\":false"));
}
// Flush all feedback when the spellcheck language and country change.
TEST_F(FeedbackSenderTest, OnLanguageCountryChange) {
AddPendingFeedback();
feedback_->OnLanguageCountryChange("pt", "BR");
EXPECT_TRUE(UploadDataContains("\"language\":\"en\""));
AddPendingFeedback();
feedback_->OnLanguageCountryChange("en", "US");
EXPECT_TRUE(UploadDataContains("\"language\":\"pt\""));
}
// The field names and types should correspond to the API.
TEST_F(FeedbackSenderTest, FeedbackAPI) {
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
std::string actual_data = GetUploadData();
std::unique_ptr<base::DictionaryValue> actual(
static_cast<base::DictionaryValue*>(
base::JSONReader::Read(actual_data).release()));
actual->SetString("params.key", "TestDummyKey");
base::ListValue* suggestions = nullptr;
actual->GetList("params.suggestionInfo", &suggestions);
base::DictionaryValue* suggestion = nullptr;
suggestions->GetDictionary(0, &suggestion);
suggestion->SetString("suggestionId", "42");
suggestion->SetString("timestamp", "9001");
static const std::string expected_data =
"{\"apiVersion\":\"v2\","
"\"method\":\"spelling.feedback\","
"\"params\":"
"{\"clientName\":\"Chrome\","
"\"originCountry\":\"USA\","
"\"key\":\"TestDummyKey\","
"\"language\":\"en\","
"\"suggestionInfo\":[{"
"\"isAutoCorrection\":false,"
"\"isFirstInSession\":true,"
"\"misspelledLength\":6,"
"\"misspelledStart\":0,"
"\"originalText\":\"Helllo world\","
"\"suggestionId\":\"42\","
"\"suggestions\":[\"Hello\"],"
"\"timestamp\":\"9001\","
"\"userActions\":[{\"actionType\":\"NO_ACTION\"}],"
"\"userMisspellingId\":\"14573599553589145012\","
"\"userSuggestionId\":[\"14761077877524043800\"]}]}}";
std::unique_ptr<base::Value> expected = base::JSONReader::Read(expected_data);
EXPECT_TRUE(expected->Equals(actual.get()))
<< "Expected data: " << expected_data
<< "\nActual data: " << actual_data;
}
// The default API version is "v2".
TEST_F(FeedbackSenderTest, DefaultApiVersion) {
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2\""));
EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2-internal\""));
}
// The API version should not change for field-trial participants that do not
// append the command-line switch.
TEST_F(FeedbackSenderTest, FieldTrialAloneHasSameApiVersion) {
EnableFieldTrial();
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2\""));
EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2-internal\""));
}
// The API version should not change if the command-line switch is appended, but
// the user is not participating in the field-trial.
TEST_F(FeedbackSenderTest, CommandLineSwitchAloneHasSameApiVersion) {
AppendCommandLineSwitch();
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2\""));
EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2-internal\""));
}
// The API version should be different for field-trial participants that also
// append the command-line switch.
TEST_F(FeedbackSenderTest, InternalApiVersion) {
AppendCommandLineSwitch();
EnableFieldTrial();
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2\""));
EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2-internal\""));
}
// Duplicate spellcheck results should be matched to the existing markers.
TEST_F(FeedbackSenderTest, MatchDupliateResultsWithExistingMarkers) {
uint32_t hash = AddPendingFeedback();
std::vector<SpellCheckResult> results(
1,
SpellCheckResult(SpellCheckResult::SPELLING,
kMisspellingStart,
kMisspellingLength,
base::ASCIIToUTF16("Hello")));
std::vector<SpellCheckMarker> markers(
1, SpellCheckMarker(hash, results[0].location));
EXPECT_EQ(static_cast<uint32_t>(0), results[0].hash);
feedback_->OnSpellcheckResults(
kRendererProcessId, base::UTF8ToUTF16(kText), markers, &results);
EXPECT_EQ(hash, results[0].hash);
}
// Adding a word to dictionary should trigger ADD_TO_DICT feedback for every
// occurrence of that word.
TEST_F(FeedbackSenderTest, MultipleAddToDictFeedback) {
std::vector<SpellCheckResult> results;
static const int kSentenceLength = 14;
static const int kNumberOfSentences = 2;
static const base::string16 kTextWithDuplicates =
base::ASCIIToUTF16("Helllo world. Helllo world.");
for (int i = 0; i < kNumberOfSentences; ++i) {
results.push_back(SpellCheckResult(SpellCheckResult::SPELLING,
kMisspellingStart + i * kSentenceLength,
kMisspellingLength,
base::ASCIIToUTF16("Hello")));
}
static const int kNumberOfRenderers = 2;
int last_renderer_process_id = -1;
for (int i = 0; i < kNumberOfRenderers; ++i) {
feedback_->OnSpellcheckResults(kRendererProcessId + i,
kTextWithDuplicates,
std::vector<SpellCheckMarker>(),
&results);
last_renderer_process_id = kRendererProcessId + i;
}
std::vector<uint32_t> remaining_markers;
for (size_t i = 0; i < results.size(); ++i)
remaining_markers.push_back(results[i].hash);
feedback_->OnReceiveDocumentMarkers(last_renderer_process_id,
remaining_markers);
EXPECT_TRUE(UploadDataContains("PENDING", 2));
EXPECT_FALSE(UploadDataContains("ADD_TO_DICT"));
feedback_->AddedToDictionary(results[0].hash);
feedback_->OnReceiveDocumentMarkers(last_renderer_process_id,
remaining_markers);
EXPECT_FALSE(UploadDataContains("PENDING"));
EXPECT_TRUE(UploadDataContains("ADD_TO_DICT", 2));
}
// ADD_TO_DICT feedback for multiple occurrences of a word should trigger only
// for pending feedback.
TEST_F(FeedbackSenderTest, AddToDictOnlyPending) {
AddPendingFeedback();
uint32_t add_to_dict_hash = AddPendingFeedback();
uint32_t select_hash = AddPendingFeedback();
feedback_->SelectedSuggestion(select_hash, 0);
feedback_->AddedToDictionary(add_to_dict_hash);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(UploadDataContains("SELECT", 1));
EXPECT_TRUE(UploadDataContains("ADD_TO_DICT", 2));
}
// Spellcheck results that are out-of-bounds are not added to feedback.
TEST_F(FeedbackSenderTest, IgnoreOutOfBounds) {
std::vector<SpellCheckResult> results;
results.push_back(SpellCheckResult(
SpellCheckResult::SPELLING, 0, 100, base::UTF8ToUTF16("Hello")));
results.push_back(SpellCheckResult(
SpellCheckResult::SPELLING, 100, 3, base::UTF8ToUTF16("world")));
results.push_back(SpellCheckResult(
SpellCheckResult::SPELLING, -1, 3, base::UTF8ToUTF16("how")));
results.push_back(SpellCheckResult(
SpellCheckResult::SPELLING, 0, 0, base::UTF8ToUTF16("are")));
results.push_back(SpellCheckResult(
SpellCheckResult::SPELLING, 2, -1, base::UTF8ToUTF16("you")));
feedback_->OnSpellcheckResults(kRendererProcessId,
base::UTF8ToUTF16(kText),
std::vector<SpellCheckMarker>(),
&results);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_FALSE(IsUploadingData());
}
// FeedbackSender does not collect and upload feedback when instructed to stop.
TEST_F(FeedbackSenderTest, CanStopFeedbackCollection) {
feedback_->StopFeedbackCollection();
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_FALSE(IsUploadingData());
}
// FeedbackSender resumes collecting and uploading feedback when instructed to
// start after stopping.
TEST_F(FeedbackSenderTest, CanResumeFeedbackCollection) {
feedback_->StopFeedbackCollection();
feedback_->StartFeedbackCollection();
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(IsUploadingData());
}
// FeedbackSender does not collect data while being stopped and upload it later.
TEST_F(FeedbackSenderTest, NoFeedbackCollectionWhenStopped) {
feedback_->StopFeedbackCollection();
AddPendingFeedback();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
feedback_->StartFeedbackCollection();
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_FALSE(IsUploadingData());
}
// The feedback context is trimmed to 2 words on the left and 2 words on the
// right side of the misspelling.
TEST_F(FeedbackSenderTest, TrimFeedback) {
std::vector<SpellCheckResult> results(
1, SpellCheckResult(SpellCheckResult::SPELLING, 13, 3,
base::UTF8ToUTF16("the")));
feedback_->OnSpellcheckResults(
kRendererProcessId,
base::UTF8ToUTF16("Far and away teh best prize that life has to offer is "
"the chance to work hard at work worth doing."),
std::vector<SpellCheckMarker>(), &results);
feedback_->OnReceiveDocumentMarkers(kRendererProcessId,
std::vector<uint32_t>());
EXPECT_TRUE(
UploadDataContains(",\"originalText\":\"and away teh best prize\","));
EXPECT_TRUE(UploadDataContains(",\"misspelledStart\":9,"));
}
} // namespace spellcheck
|