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/renderer/spellchecker/spellcheck_provider_test.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
14 // Tests for Hunspell functionality in SpellcheckingProvider
16 using base::ASCIIToUTF16
;
17 using base::WideToUTF16
;
21 TEST_F(SpellCheckProviderTest
, UsingHunspell
) {
22 FakeTextCheckingCompletion completion
;
23 provider_
.RequestTextChecking(blink::WebString("hello"),
25 std::vector
<SpellCheckMarker
>());
26 EXPECT_EQ(completion
.completion_count_
, 1U);
27 EXPECT_EQ(provider_
.messages_
.size(), 0U);
28 EXPECT_EQ(provider_
.pending_text_request_size(), 0U);
31 // Tests that the SpellCheckProvider object sends a spellcheck request when a
32 // user finishes typing a word. Also this test verifies that this object checks
33 // only a line being edited by the user.
34 TEST_F(SpellCheckProviderTest
, MultiLineText
) {
35 FakeTextCheckingCompletion completion
;
37 // Verify that the SpellCheckProvider class does not spellcheck empty text.
38 provider_
.ResetResult();
39 provider_
.RequestTextChecking(
40 blink::WebString(), &completion
, std::vector
<SpellCheckMarker
>());
41 EXPECT_TRUE(provider_
.text_
.empty());
43 // Verify that the SpellCheckProvider class does not spellcheck text while we
45 provider_
.ResetResult();
46 provider_
.RequestTextChecking(
47 blink::WebString("First"), &completion
, std::vector
<SpellCheckMarker
>());
48 EXPECT_TRUE(provider_
.text_
.empty());
50 // Verify that the SpellCheckProvider class spellcheck the first word when we
51 // type a space key, i.e. when we finish typing a word.
52 provider_
.ResetResult();
53 provider_
.RequestTextChecking(blink::WebString("First "),
55 std::vector
<SpellCheckMarker
>());
56 EXPECT_EQ(ASCIIToUTF16("First "), provider_
.text_
);
58 // Verify that the SpellCheckProvider class spellcheck the first line when we
59 // type a return key, i.e. when we finish typing a line.
60 provider_
.ResetResult();
61 provider_
.RequestTextChecking(blink::WebString("First Second\n"),
63 std::vector
<SpellCheckMarker
>());
64 EXPECT_EQ(ASCIIToUTF16("First Second\n"), provider_
.text_
);
66 // Verify that the SpellCheckProvider class spellcheck the lines when we
67 // finish typing a word "Third" to the second line.
68 provider_
.ResetResult();
69 provider_
.RequestTextChecking(blink::WebString("First Second\nThird "),
71 std::vector
<SpellCheckMarker
>());
72 EXPECT_EQ(ASCIIToUTF16("First Second\nThird "), provider_
.text_
);
74 // Verify that the SpellCheckProvider class does not send a spellcheck request
75 // when a user inserts whitespace characters.
76 provider_
.ResetResult();
77 provider_
.RequestTextChecking(blink::WebString("First Second\nThird "),
79 std::vector
<SpellCheckMarker
>());
80 EXPECT_TRUE(provider_
.text_
.empty());
82 // Verify that the SpellCheckProvider class spellcheck the lines when we type
84 provider_
.ResetResult();
85 provider_
.RequestTextChecking(
86 blink::WebString("First Second\nThird Fourth."),
88 std::vector
<SpellCheckMarker
>());
89 EXPECT_EQ(ASCIIToUTF16("First Second\nThird Fourth."), provider_
.text_
);
92 // Tests that the SpellCheckProvider class does not send requests to the
93 // spelling service when not necessary.
94 TEST_F(SpellCheckProviderTest
, CancelUnnecessaryRequests
) {
95 FakeTextCheckingCompletion completion
;
96 provider_
.RequestTextChecking(blink::WebString("hello."),
98 std::vector
<SpellCheckMarker
>());
99 EXPECT_EQ(completion
.completion_count_
, 1U);
100 EXPECT_EQ(completion
.cancellation_count_
, 0U);
101 EXPECT_EQ(provider_
.spelling_service_call_count_
, 1U);
103 // Test that the SpellCheckProvider does not send a request with the same text
105 provider_
.RequestTextChecking(blink::WebString("hello."),
107 std::vector
<SpellCheckMarker
>());
108 EXPECT_EQ(completion
.completion_count_
, 2U);
109 EXPECT_EQ(completion
.cancellation_count_
, 0U);
110 EXPECT_EQ(provider_
.spelling_service_call_count_
, 1U);
112 // Test that the SpellCheckProvider class cancels an incoming request that
113 // does not include any words.
114 provider_
.RequestTextChecking(blink::WebString(":-)"),
116 std::vector
<SpellCheckMarker
>());
117 EXPECT_EQ(completion
.completion_count_
, 3U);
118 EXPECT_EQ(completion
.cancellation_count_
, 1U);
119 EXPECT_EQ(provider_
.spelling_service_call_count_
, 1U);
121 // Test that the SpellCheckProvider class sends a request when it receives a
123 const wchar_t kRussianWord
[] = L
"\x0431\x0451\x0434\x0440\x0430";
124 provider_
.RequestTextChecking(blink::WebString(WideToUTF16(kRussianWord
)),
126 std::vector
<SpellCheckMarker
>());
127 EXPECT_EQ(completion
.completion_count_
, 4U);
128 EXPECT_EQ(completion
.cancellation_count_
, 1U);
129 EXPECT_EQ(provider_
.spelling_service_call_count_
, 2U);
132 // Tests that the SpellCheckProvider calls didFinishCheckingText() when
134 TEST_F(SpellCheckProviderTest
, CompleteNecessaryRequests
) {
135 FakeTextCheckingCompletion completion
;
137 base::string16 text
= ASCIIToUTF16("Icland is an icland ");
138 provider_
.RequestTextChecking(
139 blink::WebString(text
), &completion
, std::vector
<SpellCheckMarker
>());
140 EXPECT_EQ(0U, completion
.cancellation_count_
) << "Should finish checking \""
143 const int kSubstringLength
= 18;
144 base::string16 substring
= text
.substr(0, kSubstringLength
);
145 provider_
.RequestTextChecking(blink::WebString(substring
),
147 std::vector
<SpellCheckMarker
>());
148 EXPECT_EQ(0U, completion
.cancellation_count_
) << "Should finish checking \""
149 << substring
<< "\"";
151 provider_
.RequestTextChecking(
152 blink::WebString(text
), &completion
, std::vector
<SpellCheckMarker
>());
153 EXPECT_EQ(0U, completion
.cancellation_count_
) << "Should finish checking \""
157 // Tests that the SpellCheckProvider cancels spelling requests in the middle of
159 TEST_F(SpellCheckProviderTest
, CancelMidWordRequests
) {
160 FakeTextCheckingCompletion completion
;
161 provider_
.RequestTextChecking(blink::WebString("hello "),
163 std::vector
<SpellCheckMarker
>());
164 EXPECT_EQ(completion
.completion_count_
, 1U);
165 EXPECT_EQ(completion
.cancellation_count_
, 0U);
166 EXPECT_EQ(provider_
.spelling_service_call_count_
, 1U);
168 provider_
.RequestTextChecking(blink::WebString("hello world"),
170 std::vector
<SpellCheckMarker
>());
171 EXPECT_EQ(completion
.completion_count_
, 2U);
172 EXPECT_EQ(completion
.cancellation_count_
, 1U);
173 EXPECT_EQ(provider_
.spelling_service_call_count_
, 1U);
175 provider_
.RequestTextChecking(blink::WebString("hello world."),
177 std::vector
<SpellCheckMarker
>());
178 EXPECT_EQ(completion
.completion_count_
, 3U);
179 EXPECT_EQ(completion
.cancellation_count_
, 1U);
180 EXPECT_EQ(provider_
.spelling_service_call_count_
, 2U);