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/strings/utf_string_conversions.h"
      8 #include "chrome/common/spellcheck_marker.h"
      9 #include "chrome/common/spellcheck_messages.h"
     10 #include "chrome/common/spellcheck_result.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 namespace {
     16 
     17 class SpellCheckProviderMacTest : public SpellCheckProviderTest {};
     18 
     19 struct MessageParameters {
     20   MessageParameters()
     21       : router_id(0),
     22         request_id(0) {}
     23 
     24   int router_id;
     25   int request_id;
     26   base::string16 text;
     27   std::vector<SpellCheckMarker> markers;
     28 };
     29 
     30 MessageParameters ReadRequestTextCheck(IPC::Message* message) {
     31   MessageParameters parameters;
     32   bool ok = SpellCheckHostMsg_RequestTextCheck::Read(
     33       message,
     34       &parameters.router_id,
     35       &parameters.request_id,
     36       &parameters.text,
     37       &parameters.markers);
     38   EXPECT_TRUE(ok);
     39   return parameters;
     40 }
     41 
     42 void FakeMessageArrival(SpellCheckProvider* provider,
     43                         const MessageParameters& parameters) {
     44   std::vector<SpellCheckResult> fake_result;
     45   bool handled = provider->OnMessageReceived(
     46       SpellCheckMsg_RespondTextCheck(
     47           0,
     48           parameters.request_id,
     49           fake_result));
     50   EXPECT_TRUE(handled);
     51 }
     52 
     53 TEST_F(SpellCheckProviderMacTest, SingleRoundtripSuccess) {
     54   FakeTextCheckingCompletion completion;
     55 
     56   provider_.RequestTextChecking(blink::WebString("hello "),
     57                                 &completion,
     58                                 std::vector<SpellCheckMarker>());
     59   EXPECT_EQ(completion.completion_count_, 0U);
     60   EXPECT_EQ(provider_.messages_.size(), 1U);
     61   EXPECT_EQ(provider_.pending_text_request_size(), 1U);
     62 
     63   MessageParameters read_parameters =
     64       ReadRequestTextCheck(provider_.messages_[0]);
     65   EXPECT_EQ(read_parameters.text, UTF8ToUTF16("hello "));
     66 
     67   FakeMessageArrival(&provider_, read_parameters);
     68   EXPECT_EQ(completion.completion_count_, 1U);
     69   EXPECT_EQ(provider_.pending_text_request_size(), 0U);
     70 }
     71 
     72 TEST_F(SpellCheckProviderMacTest, TwoRoundtripSuccess) {
     73   FakeTextCheckingCompletion completion1;
     74   provider_.RequestTextChecking(blink::WebString("hello "),
     75                                 &completion1,
     76                                 std::vector<SpellCheckMarker>());
     77   FakeTextCheckingCompletion completion2;
     78   provider_.RequestTextChecking(blink::WebString("bye "),
     79                                 &completion2,
     80                                 std::vector<SpellCheckMarker>());
     81 
     82   EXPECT_EQ(completion1.completion_count_, 0U);
     83   EXPECT_EQ(completion2.completion_count_, 0U);
     84   EXPECT_EQ(provider_.messages_.size(), 2U);
     85   EXPECT_EQ(provider_.pending_text_request_size(), 2U);
     86 
     87   MessageParameters read_parameters1 =
     88       ReadRequestTextCheck(provider_.messages_[0]);
     89   EXPECT_EQ(read_parameters1.text, UTF8ToUTF16("hello "));
     90 
     91   MessageParameters read_parameters2 =
     92       ReadRequestTextCheck(provider_.messages_[1]);
     93   EXPECT_EQ(read_parameters2.text, UTF8ToUTF16("bye "));
     94 
     95   FakeMessageArrival(&provider_, read_parameters1);
     96   EXPECT_EQ(completion1.completion_count_, 1U);
     97   EXPECT_EQ(completion2.completion_count_, 0U);
     98   EXPECT_EQ(provider_.pending_text_request_size(), 1U);
     99 
    100   FakeMessageArrival(&provider_, read_parameters2);
    101   EXPECT_EQ(completion1.completion_count_, 1U);
    102   EXPECT_EQ(completion2.completion_count_, 1U);
    103   EXPECT_EQ(provider_.pending_text_request_size(), 0U);
    104 }
    105 
    106 }  // namespace
    107