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
|
// Copyright 2022 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/spellchecker/spelling_request.h"
#include <memory>
#include <string>
#include <vector>
#include "base/functional/callback_helpers.h"
#include "components/spellcheck/common/spellcheck_result.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
struct RemoteCheckTestCase {
std::string test_name;
std::u16string text;
std::vector<SpellCheckResult> initial_results;
std::vector<SpellCheckResult> expected_converted_results;
};
SpellCheckResult MakeResult(int pos, int length) {
return SpellCheckResult(SpellCheckResult::Decoration::SPELLING, pos, length,
u"");
}
class SpellingRequestRemoteCheckUnitTest
: public testing::TestWithParam<RemoteCheckTestCase> {
public:
SpellingRequestRemoteCheckUnitTest() {
spelling_request_ = SpellingRequest::CreateForTest(
u"", base::DoNothing(), base::DoNothing(), base::DoNothing());
}
~SpellingRequestRemoteCheckUnitTest() override = default;
protected:
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<SpellingRequest> spelling_request_;
};
// Helper for setting the test case's name.
static std::string DescribeParams(
const testing::TestParamInfo<RemoteCheckTestCase>& info) {
return info.param.test_name;
}
std::vector<RemoteCheckTestCase> BuildTestCases() {
std::vector<RemoteCheckTestCase> test_cases = {
RemoteCheckTestCase{"no_results_all_ascii",
u"This has no spelling mistakes 1 2 3",
{},
{}},
RemoteCheckTestCase{
"some_results_all_ascii",
u"Tihs has 3 speling mistkes",
{MakeResult(0, 4), MakeResult(11, 7), MakeResult(19, 7)},
{MakeResult(0, 4), MakeResult(11, 7), MakeResult(19, 7)}},
RemoteCheckTestCase{
"no_results_non_ascii",
u"\u00e9\u00c8\u00e7\u00e2\u062c\u305c\u000a\u0020\u00ef",
{},
{}},
RemoteCheckTestCase{"some_results_non_ascii",
u"\u00e9\u00c8\u00e7 tihs\u00e2\u062c is "
u"\u305c\u000a a \u0020 mistke\u00ef",
{MakeResult(4, 4), MakeResult(21, 6)},
{MakeResult(4, 4), MakeResult(21, 6)}},
RemoteCheckTestCase{
"no_results_some_surrogate_pairs",
u"π¨βπ©βπ¦βπ¦ This π has π§πΏ emojis",
// The code point representation of the emojis in the above string is:
// "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66
// This \ud83d\ude01 has \ud83e\uddd1\ud83c\udfff emojis"
{},
{}},
RemoteCheckTestCase{
"some_results_some_surrogate_pairs",
u"π¨βπ©βπ¦βπ¦ Tihs π has π§πΏ emjis",
// The code point representation of the emojis in the above string is:
// "\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc66\u200d\ud83d\udc66
// Tihs \ud83d\ude01 has \ud83e\uddd1\ud83c\udfff emjis"
{MakeResult(8, 4), MakeResult(22, 5)},
{MakeResult(12, 4), MakeResult(29, 5)}},
RemoteCheckTestCase{
"surrogate_pairs_inside_word",
u"I ufortπ¨βπ©βπ¦βπ¦ππ§πΏunately cant",
// The code point representation of the emojis in the above string is:
// "I ufort\ud83d\udc68\u200d\ud83d\udc69\u200d\ud83d\udc66\u200d
// \ud83d\udc66\ud83d\ude01\ud83e\uddd1\ud83c\udfffunately cant"
{MakeResult(2, 22), MakeResult(25, 4)},
{MakeResult(2, 29), MakeResult(32, 4)}}};
return test_cases;
}
INSTANTIATE_TEST_SUITE_P(/* No prefix */,
SpellingRequestRemoteCheckUnitTest,
testing::ValuesIn(BuildTestCases()),
DescribeParams);
// Tests that remote results have their position value correctly adjusted to
// the corresponding code point position.
TEST_P(SpellingRequestRemoteCheckUnitTest, OnRemoteCheckCompleted) {
const RemoteCheckTestCase& param = GetParam();
spelling_request_->OnRemoteCheckCompleted(true, param.text,
param.initial_results);
const std::vector<SpellCheckResult>& actual_converted_results =
spelling_request_->remote_results_;
EXPECT_EQ(actual_converted_results.size(),
param.expected_converted_results.size());
for (const auto& expected : param.expected_converted_results) {
bool found = false;
for (const auto& actual : actual_converted_results) {
if (expected.decoration == actual.decoration &&
expected.location == actual.location &&
expected.length == actual.length) {
found = true;
break;
}
}
EXPECT_TRUE(found) << "Result with pos = " << expected.location
<< " and length = " << expected.length << " not found.";
}
}
|