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