Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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 "base/command_line.h"
      6 #include "base/message_loop.h"
      7 #include "base/utf_string_conversions.h"
      8 #include "chrome/browser/spellcheck_message_filter.h"
      9 #include "chrome/common/spellcheck_messages.h"
     10 #include "chrome/test/in_process_browser_test.h"
     11 #include "chrome/test/ui_test_utils.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult.h"
     14 
     15 namespace {
     16 
     17 typedef InProcessBrowserTest SpellCheckMessageFilterBrowserTest;
     18 
     19 // Fake filter for testing, which stores sent messages and
     20 // allows verification by the test case.
     21 class TestingSpellCheckMessageFilter : public SpellCheckMessageFilter {
     22  public:
     23   explicit TestingSpellCheckMessageFilter(MessageLoopForUI* loop)
     24       : loop_(loop) { }
     25 
     26   ~TestingSpellCheckMessageFilter() {
     27     for (std::vector<IPC::Message*>::iterator i = sent_messages_.begin();
     28          i != sent_messages_.end();
     29          ++i) {
     30       delete *i;
     31     }
     32   }
     33 
     34   virtual bool Send(IPC::Message* message) {
     35     sent_messages_.push_back(message);
     36     loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
     37     return true;
     38   }
     39 
     40   std::vector<IPC::Message*> sent_messages_;
     41   MessageLoopForUI* loop_;
     42 };
     43 
     44 // Uses browsertest to setup chrome threads.
     45 IN_PROC_BROWSER_TEST_F(SpellCheckMessageFilterBrowserTest,
     46                        SpellCheckReturnMessage) {
     47   scoped_refptr<TestingSpellCheckMessageFilter> target
     48       (new TestingSpellCheckMessageFilter(MessageLoopForUI::current()));
     49 
     50   SpellCheckHostMsg_PlatformRequestTextCheck to_be_received
     51       (123, 456, 789, UTF8ToUTF16("zz."));
     52   bool handled = false;
     53   target->OnMessageReceived(to_be_received, &handled);
     54   EXPECT_TRUE(handled);
     55 
     56   MessageLoopForUI::current()->Run();
     57   EXPECT_EQ(1U, target->sent_messages_.size());
     58 
     59   int sent_identifier;
     60   int sent_tag;
     61   std::vector<WebKit::WebTextCheckingResult> sent_results;
     62   bool ok = SpellCheckMsg_RespondTextCheck::Read(
     63       target->sent_messages_[0], &sent_identifier, &sent_tag, &sent_results);
     64   EXPECT_TRUE(ok);
     65   EXPECT_EQ(1U, sent_results.size());
     66   EXPECT_EQ(sent_results[0].position, 0);
     67   EXPECT_EQ(sent_results[0].length, 2);
     68   EXPECT_EQ(sent_results[0].error,
     69             WebKit::WebTextCheckingResult::ErrorSpelling);
     70 }
     71 
     72 }  // namespace
     73