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.
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"
15 // Tests for Hunspell functionality in SpellcheckingProvider
17 using base::ASCIIToUTF16
;
18 using base::WideToUTF16
;
22 TEST_F(SpellCheckProviderTest
, UsingHunspell
) {
23 FakeTextCheckingCompletion completion
;
24 provider_
.RequestTextChecking(blink::WebString("hello"),
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);
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.
35 TEST_F(SpellCheckProviderTest
, MultiLineText
) {
36 FakeTextCheckingCompletion completion
;
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());
44 // Verify that the SpellCheckProvider class does not spellcheck text while we
46 provider_
.ResetResult();
47 provider_
.RequestTextChecking(
48 blink::WebString("First"), &completion
, std::vector
<SpellCheckMarker
>());
49 EXPECT_TRUE(provider_
.text_
.empty());
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 "),
56 std::vector
<SpellCheckMarker
>());
57 EXPECT_EQ(ASCIIToUTF16("First "), provider_
.text_
);
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"),
64 std::vector
<SpellCheckMarker
>());
65 EXPECT_EQ(ASCIIToUTF16("First Second\n"), provider_
.text_
);
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 "),
72 std::vector
<SpellCheckMarker
>());
73 EXPECT_EQ(ASCIIToUTF16("First Second\nThird "), provider_
.text_
);
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 "),
80 std::vector
<SpellCheckMarker
>());
81 EXPECT_TRUE(provider_
.text_
.empty());
83 // Verify that the SpellCheckProvider class spellcheck the lines when we type
85 provider_
.ResetResult();
86 provider_
.RequestTextChecking(
87 blink::WebString("First Second\nThird Fourth."),
89 std::vector
<SpellCheckMarker
>());
90 EXPECT_EQ(ASCIIToUTF16("First Second\nThird Fourth."), provider_
.text_
);
93 // Tests that the SpellCheckProvider class does not send requests to the
94 // spelling service when not necessary.
95 TEST_F(SpellCheckProviderTest
, CancelUnnecessaryRequests
) {
96 FakeTextCheckingCompletion completion
;
97 provider_
.RequestTextChecking(blink::WebString("hello."),
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);
104 // Test that the SpellCheckProvider does not send a request with the same text
106 provider_
.RequestTextChecking(blink::WebString("hello."),
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);
113 // Test that the SpellCheckProvider class cancels an incoming request that
114 // does not include any words.
115 provider_
.RequestTextChecking(blink::WebString(":-)"),
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);
122 // Test that the SpellCheckProvider class sends a request when it receives a
124 const wchar_t kRussianWord
[] = L
"\x0431\x0451\x0434\x0440\x0430";
125 provider_
.RequestTextChecking(blink::WebString(WideToUTF16(kRussianWord
)),
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);
133 // Tests that the SpellCheckProvider calls didFinishCheckingText() when
135 TEST_F(SpellCheckProviderTest
, CompleteNecessaryRequests
) {
136 FakeTextCheckingCompletion completion
;
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 \""
144 const int kSubstringLength
= 18;
145 base::string16 substring
= text
.substr(0, kSubstringLength
);
146 provider_
.RequestTextChecking(blink::WebString(substring
),
148 std::vector
<SpellCheckMarker
>());
149 EXPECT_EQ(0U, completion
.cancellation_count_
) << "Should finish checking \""
150 << substring
<< "\"";
152 provider_
.RequestTextChecking(
153 blink::WebString(text
), &completion
, std::vector
<SpellCheckMarker
>());
154 EXPECT_EQ(0U, completion
.cancellation_count_
) << "Should finish checking \""
158 // Tests that the SpellCheckProvider cancels spelling requests in the middle of
160 TEST_F(SpellCheckProviderTest
, CancelMidWordRequests
) {
161 FakeTextCheckingCompletion completion
;
162 provider_
.RequestTextChecking(blink::WebString("hello "),
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);
169 provider_
.RequestTextChecking(blink::WebString("hello world"),
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);
176 provider_
.RequestTextChecking(blink::WebString("hello world."),
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);