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