File: spellcheck_provider_hunspell_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (162 lines) | stat: -rw-r--r-- 7,083 bytes parent folder | download | duplicates (9)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/feature_list.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/spellcheck/common/spellcheck_features.h"
#include "components/spellcheck/renderer/spellcheck_provider_test.h"
#include "testing/gtest/include/gtest/gtest.h"

// Tests for Hunspell functionality in SpellcheckingProvider

using base::ASCIIToUTF16;
using base::WideToUTF16;

namespace {

void CheckSpellingServiceCallCount(size_t actual, size_t expected) {
  // On Windows, if the native spell checker integration is enabled,
  // CallSpellingService() is not used, so the fake provider's |text_| is never
  // assigned. Don't assert the text in that case.
#if !BUILDFLAG(IS_WIN)
  EXPECT_EQ(actual, expected);
#endif  // !BUILDFLAG(IS_WIN)
}

void CheckProviderText(std::u16string expected, std::u16string actual) {
  // On Windows, if the native spell checker integration is enabled,
  // CallSpellingService() is not used, so the fake provider's |text_| is never
  // assigned. Don't assert the text in that case.
#if !BUILDFLAG(IS_WIN)
  EXPECT_EQ(actual, expected);
#endif  // !BUILDFLAG(IS_WIN)
}

// Tests that the SpellCheckProvider object sends a spellcheck request when a
// user finishes typing a word. Also this test verifies that this object checks
// only a line being edited by the user.
TEST_F(SpellCheckProviderTest, MultiLineText) {
  FakeTextCheckingResult completion;

  // Verify that the SpellCheckProvider class does not spellcheck empty text.
  provider_.ResetResult();
  provider_.RequestTextChecking(
      std::u16string(),
      std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 1U);
  EXPECT_TRUE(provider_.text_.empty());
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 0U);

  // Verify that the SpellCheckProvider class spellcheck the first word when we
  // stop typing after finishing the first word.
  provider_.ResetResult();
  provider_.RequestTextChecking(
      u"First", std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 2U);
  CheckProviderText(u"First", provider_.text_);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);

  // Verify that the SpellCheckProvider class spellcheck the first line when we
  // type a return key, i.e. when we finish typing a line.
  provider_.ResetResult();
  provider_.RequestTextChecking(
      u"First Second\n",
      std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 3U);
  CheckProviderText(u"First Second\n", provider_.text_);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 2U);

  // Verify that the SpellCheckProvider class spellcheck the lines when we
  // finish typing a word "Third" to the second line.
  provider_.ResetResult();
  provider_.RequestTextChecking(
      u"First Second\nThird ",
      std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 4U);
  CheckProviderText(u"First Second\nThird ", provider_.text_);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 3U);

  // Verify that the SpellCheckProvider class does not send a spellcheck request
  // when a user inserts whitespace characters.
  provider_.ResetResult();
  provider_.RequestTextChecking(
      u"First Second\nThird   ",
      std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 5U);
  EXPECT_TRUE(provider_.text_.empty());
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 3U);

  // Verify that the SpellCheckProvider class spellcheck the lines when we type
  // a period.
  provider_.ResetResult();
  provider_.RequestTextChecking(
      u"First Second\nThird   Fourth.",
      std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 6U);
  CheckProviderText(u"First Second\nThird   Fourth.", provider_.text_);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 4U);
}

// Tests that the SpellCheckProvider class does not send requests to the
// spelling service when not necessary.
TEST_F(SpellCheckProviderTest, CancelUnnecessaryRequests) {
  FakeTextCheckingResult completion;
  provider_.RequestTextChecking(
      u"hello.", std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 1U);
  EXPECT_EQ(completion.cancellation_count_, 0U);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);

  // Test that the SpellCheckProvider does not send a request with the same text
  // as above.
  provider_.RequestTextChecking(
      u"hello.", std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 2U);
  EXPECT_EQ(completion.cancellation_count_, 0U);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);

  // Test that the SpellCheckProvider class cancels an incoming request that
  // does not include any words.
  provider_.RequestTextChecking(
      u":-)", std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 3U);
  EXPECT_EQ(completion.cancellation_count_, 1U);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 1U);

  // Test that the SpellCheckProvider class sends a request when it receives a
  // Russian word.
  const char16_t kRussianWord[] = u"\x0431\x0451\x0434\x0440\x0430";
  provider_.RequestTextChecking(
      kRussianWord, std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(completion.completion_count_, 4U);
  EXPECT_EQ(completion.cancellation_count_, 1U);
  CheckSpellingServiceCallCount(provider_.spelling_service_call_count_, 2U);
}

// Tests that the SpellCheckProvider calls didFinishCheckingText() when
// necessary.
TEST_F(SpellCheckProviderTest, CompleteNecessaryRequests) {
  FakeTextCheckingResult completion;

  std::u16string text = u"Icland is an icland ";
  provider_.RequestTextChecking(
      text, std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
                                                << text << "\"";

  const int kSubstringLength = 18;
  std::u16string substring = text.substr(0, kSubstringLength);
  provider_.RequestTextChecking(
      substring, std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
                                                << substring << "\"";

  provider_.RequestTextChecking(
      text, std::make_unique<FakeTextCheckingCompletion>(&completion));
  EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
                                                << text << "\"";
}

}  // namespace