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