1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <vector>
6
7 #include "base/stl_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/spellcheck_marker.h"
10 #include "chrome/common/spellcheck_messages.h"
11 #include "chrome/renderer/spellchecker/spellcheck_provider_test.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14
15 // Tests for Hunspell functionality in SpellcheckingProvider
16
17 using base::ASCIIToUTF16;
18 using base::WideToUTF16;
19
20 namespace {
21
TEST_F(SpellCheckProviderTest,UsingHunspell)22 TEST_F(SpellCheckProviderTest, UsingHunspell) {
23 FakeTextCheckingCompletion completion;
24 provider_.RequestTextChecking(blink::WebString("hello"),
25 &completion,
26 std::vector<SpellCheckMarker>());
27 EXPECT_EQ(completion.completion_count_, 1U);
28 EXPECT_EQ(provider_.messages_.size(), 0U);
29 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
30 }
31
32 // Tests that the SpellCheckProvider object sends a spellcheck request when a
33 // user finishes typing a word. Also this test verifies that this object checks
34 // only a line being edited by the user.
TEST_F(SpellCheckProviderTest,MultiLineText)35 TEST_F(SpellCheckProviderTest, MultiLineText) {
36 FakeTextCheckingCompletion completion;
37
38 // Verify that the SpellCheckProvider class does not spellcheck empty text.
39 provider_.ResetResult();
40 provider_.RequestTextChecking(
41 blink::WebString(), &completion, std::vector<SpellCheckMarker>());
42 EXPECT_TRUE(provider_.text_.empty());
43
44 // Verify that the SpellCheckProvider class does not spellcheck text while we
45 // are typing a word.
46 provider_.ResetResult();
47 provider_.RequestTextChecking(
48 blink::WebString("First"), &completion, std::vector<SpellCheckMarker>());
49 EXPECT_TRUE(provider_.text_.empty());
50
51 // Verify that the SpellCheckProvider class spellcheck the first word when we
52 // type a space key, i.e. when we finish typing a word.
53 provider_.ResetResult();
54 provider_.RequestTextChecking(blink::WebString("First "),
55 &completion,
56 std::vector<SpellCheckMarker>());
57 EXPECT_EQ(ASCIIToUTF16("First "), provider_.text_);
58
59 // Verify that the SpellCheckProvider class spellcheck the first line when we
60 // type a return key, i.e. when we finish typing a line.
61 provider_.ResetResult();
62 provider_.RequestTextChecking(blink::WebString("First Second\n"),
63 &completion,
64 std::vector<SpellCheckMarker>());
65 EXPECT_EQ(ASCIIToUTF16("First Second\n"), provider_.text_);
66
67 // Verify that the SpellCheckProvider class spellcheck the lines when we
68 // finish typing a word "Third" to the second line.
69 provider_.ResetResult();
70 provider_.RequestTextChecking(blink::WebString("First Second\nThird "),
71 &completion,
72 std::vector<SpellCheckMarker>());
73 EXPECT_EQ(ASCIIToUTF16("First Second\nThird "), provider_.text_);
74
75 // Verify that the SpellCheckProvider class does not send a spellcheck request
76 // when a user inserts whitespace characters.
77 provider_.ResetResult();
78 provider_.RequestTextChecking(blink::WebString("First Second\nThird "),
79 &completion,
80 std::vector<SpellCheckMarker>());
81 EXPECT_TRUE(provider_.text_.empty());
82
83 // Verify that the SpellCheckProvider class spellcheck the lines when we type
84 // a period.
85 provider_.ResetResult();
86 provider_.RequestTextChecking(
87 blink::WebString("First Second\nThird Fourth."),
88 &completion,
89 std::vector<SpellCheckMarker>());
90 EXPECT_EQ(ASCIIToUTF16("First Second\nThird Fourth."), provider_.text_);
91 }
92
93 // Tests that the SpellCheckProvider class does not send requests to the
94 // spelling service when not necessary.
TEST_F(SpellCheckProviderTest,CancelUnnecessaryRequests)95 TEST_F(SpellCheckProviderTest, CancelUnnecessaryRequests) {
96 FakeTextCheckingCompletion completion;
97 provider_.RequestTextChecking(blink::WebString("hello."),
98 &completion,
99 std::vector<SpellCheckMarker>());
100 EXPECT_EQ(completion.completion_count_, 1U);
101 EXPECT_EQ(completion.cancellation_count_, 0U);
102 EXPECT_EQ(provider_.spelling_service_call_count_, 1U);
103
104 // Test that the SpellCheckProvider does not send a request with the same text
105 // as above.
106 provider_.RequestTextChecking(blink::WebString("hello."),
107 &completion,
108 std::vector<SpellCheckMarker>());
109 EXPECT_EQ(completion.completion_count_, 2U);
110 EXPECT_EQ(completion.cancellation_count_, 0U);
111 EXPECT_EQ(provider_.spelling_service_call_count_, 1U);
112
113 // Test that the SpellCheckProvider class cancels an incoming request that
114 // does not include any words.
115 provider_.RequestTextChecking(blink::WebString(":-)"),
116 &completion,
117 std::vector<SpellCheckMarker>());
118 EXPECT_EQ(completion.completion_count_, 3U);
119 EXPECT_EQ(completion.cancellation_count_, 1U);
120 EXPECT_EQ(provider_.spelling_service_call_count_, 1U);
121
122 // Test that the SpellCheckProvider class sends a request when it receives a
123 // Russian word.
124 const wchar_t kRussianWord[] = L"\x0431\x0451\x0434\x0440\x0430";
125 provider_.RequestTextChecking(blink::WebString(WideToUTF16(kRussianWord)),
126 &completion,
127 std::vector<SpellCheckMarker>());
128 EXPECT_EQ(completion.completion_count_, 4U);
129 EXPECT_EQ(completion.cancellation_count_, 1U);
130 EXPECT_EQ(provider_.spelling_service_call_count_, 2U);
131 }
132
133 // Tests that the SpellCheckProvider calls didFinishCheckingText() when
134 // necessary.
TEST_F(SpellCheckProviderTest,CompleteNecessaryRequests)135 TEST_F(SpellCheckProviderTest, CompleteNecessaryRequests) {
136 FakeTextCheckingCompletion completion;
137
138 base::string16 text = ASCIIToUTF16("Icland is an icland ");
139 provider_.RequestTextChecking(
140 blink::WebString(text), &completion, std::vector<SpellCheckMarker>());
141 EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
142 << text << "\"";
143
144 const int kSubstringLength = 18;
145 base::string16 substring = text.substr(0, kSubstringLength);
146 provider_.RequestTextChecking(blink::WebString(substring),
147 &completion,
148 std::vector<SpellCheckMarker>());
149 EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
150 << substring << "\"";
151
152 provider_.RequestTextChecking(
153 blink::WebString(text), &completion, std::vector<SpellCheckMarker>());
154 EXPECT_EQ(0U, completion.cancellation_count_) << "Should finish checking \""
155 << text << "\"";
156 }
157
158 // Tests that the SpellCheckProvider cancels spelling requests in the middle of
159 // a word.
TEST_F(SpellCheckProviderTest,CancelMidWordRequests)160 TEST_F(SpellCheckProviderTest, CancelMidWordRequests) {
161 FakeTextCheckingCompletion completion;
162 provider_.RequestTextChecking(blink::WebString("hello "),
163 &completion,
164 std::vector<SpellCheckMarker>());
165 EXPECT_EQ(completion.completion_count_, 1U);
166 EXPECT_EQ(completion.cancellation_count_, 0U);
167 EXPECT_EQ(provider_.spelling_service_call_count_, 1U);
168
169 provider_.RequestTextChecking(blink::WebString("hello world"),
170 &completion,
171 std::vector<SpellCheckMarker>());
172 EXPECT_EQ(completion.completion_count_, 2U);
173 EXPECT_EQ(completion.cancellation_count_, 1U);
174 EXPECT_EQ(provider_.spelling_service_call_count_, 1U);
175
176 provider_.RequestTextChecking(blink::WebString("hello world."),
177 &completion,
178 std::vector<SpellCheckMarker>());
179 EXPECT_EQ(completion.completion_count_, 3U);
180 EXPECT_EQ(completion.cancellation_count_, 1U);
181 EXPECT_EQ(provider_.spelling_service_call_count_, 2U);
182 }
183
184 } // namespace
185