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