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