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