Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / spellchecker / spellcheck_message_filter_unittest.cc
1 // Copyright (c) 2013 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/strings/utf_string_conversions.h"
6 #include "chrome/browser/spellchecker/spellcheck_factory.h"
7 #include "chrome/browser/spellchecker/spellcheck_message_filter.h"
8 #include "chrome/browser/spellchecker/spellcheck_service.h"
9 #include "chrome/common/spellcheck_marker.h"
10 #include "chrome/common/spellcheck_messages.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "ipc/ipc_message.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 class TestingSpellCheckMessageFilter : public SpellCheckMessageFilter {
17  public:
18   TestingSpellCheckMessageFilter()
19       : SpellCheckMessageFilter(0),
20         spellcheck_(new SpellcheckService(&profile_)) {}
21
22   bool Send(IPC::Message* message) override {
23     sent_messages.push_back(message);
24     return true;
25   }
26
27   SpellcheckService* GetSpellcheckService() const override {
28     return spellcheck_.get();
29   }
30
31 #if !defined(OS_MACOSX)
32   void OnTextCheckComplete(int route_id,
33                            int identifier,
34                            const std::vector<SpellCheckMarker>& markers,
35                            bool success,
36                            const base::string16& text,
37                            const std::vector<SpellCheckResult>& results) {
38     SpellCheckMessageFilter::OnTextCheckComplete(
39         route_id, identifier, markers, success, text, results);
40   }
41 #endif
42
43   ScopedVector<IPC::Message> sent_messages;
44
45  private:
46   ~TestingSpellCheckMessageFilter() override {}
47
48   content::TestBrowserThreadBundle thread_bundle_;
49   TestingProfile profile_;
50   scoped_ptr<SpellcheckService> spellcheck_;
51
52   DISALLOW_COPY_AND_ASSIGN(TestingSpellCheckMessageFilter);
53 };
54
55 TEST(SpellCheckMessageFilterTest, TestOverrideThread) {
56   static const uint32 kSpellcheckMessages[] = {
57     SpellCheckHostMsg_RequestDictionary::ID,
58     SpellCheckHostMsg_NotifyChecked::ID,
59     SpellCheckHostMsg_RespondDocumentMarkers::ID,
60 #if !defined(OS_MACOSX)
61     SpellCheckHostMsg_CallSpellingService::ID,
62 #endif
63   };
64   content::BrowserThread::ID thread;
65   IPC::Message message;
66   scoped_refptr<TestingSpellCheckMessageFilter> filter(
67       new TestingSpellCheckMessageFilter);
68   for (size_t i = 0; i < arraysize(kSpellcheckMessages); ++i) {
69     message.SetHeaderValues(
70         0, kSpellcheckMessages[i], IPC::Message::PRIORITY_NORMAL);
71     thread = content::BrowserThread::IO;
72     filter->OverrideThreadForMessage(message, &thread);
73     EXPECT_EQ(content::BrowserThread::UI, thread);
74   }
75 }
76
77 #if !defined(OS_MACOSX)
78 TEST(SpellCheckMessageFilterTest, OnTextCheckCompleteTestCustomDictionary) {
79   static const std::string kCustomWord = "Helllo";
80   static const int kRouteId = 0;
81   static const int kCallbackId = 0;
82   static const std::vector<SpellCheckMarker> kMarkers;
83   static const base::string16 kText = base::ASCIIToUTF16("Helllo warld.");
84   static const bool kSuccess = true;
85   static const SpellCheckResult::Decoration kDecoration =
86       SpellCheckResult::SPELLING;
87   static const int kLocation = 7;
88   static const int kLength = 5;
89   static const base::string16 kReplacement = base::ASCIIToUTF16("world");
90
91   std::vector<SpellCheckResult> results;
92   results.push_back(SpellCheckResult(
93       SpellCheckResult::SPELLING, 0, 6, base::ASCIIToUTF16("Hello")));
94   results.push_back(
95       SpellCheckResult(kDecoration, kLocation, kLength, kReplacement));
96
97   scoped_refptr<TestingSpellCheckMessageFilter> filter(
98       new TestingSpellCheckMessageFilter);
99   filter->GetSpellcheckService()->GetCustomDictionary()->AddWord(kCustomWord);
100   filter->OnTextCheckComplete(
101       kRouteId, kCallbackId, kMarkers, kSuccess, kText, results);
102   EXPECT_EQ(static_cast<size_t>(1), filter->sent_messages.size());
103
104   SpellCheckMsg_RespondSpellingService::Param params;
105   bool ok = SpellCheckMsg_RespondSpellingService::Read(
106       filter->sent_messages[0], & params);
107   int sent_identifier = params.a;
108   bool sent_success = params.b;
109   base::string16 sent_text = params.c;
110   std::vector<SpellCheckResult> sent_results = params.d;
111   EXPECT_TRUE(ok);
112   EXPECT_EQ(kCallbackId, sent_identifier);
113   EXPECT_EQ(kSuccess, sent_success);
114   EXPECT_EQ(kText, sent_text);
115   EXPECT_EQ(static_cast<size_t>(1), sent_results.size());
116   EXPECT_EQ(kDecoration, sent_results[0].decoration);
117   EXPECT_EQ(kLocation, sent_results[0].location);
118   EXPECT_EQ(kLength, sent_results[0].length);
119   EXPECT_EQ(kReplacement, sent_results[0].replacement);
120 }
121
122 TEST(SpellCheckMessageFilterTest, OnTextCheckCompleteTest) {
123   std::vector<SpellCheckResult> results;
124   results.push_back(SpellCheckResult(
125       SpellCheckResult::SPELLING, 0, 6, base::ASCIIToUTF16("Hello")));
126   results.push_back(SpellCheckResult(
127       SpellCheckResult::SPELLING, 7, 7, base::ASCIIToUTF16("world")));
128
129   scoped_refptr<TestingSpellCheckMessageFilter> filter(
130       new TestingSpellCheckMessageFilter);
131   filter->OnTextCheckComplete(1, 1, std::vector<SpellCheckMarker>(),
132       true,  base::ASCIIToUTF16("Helllo walrd"), results);
133   EXPECT_EQ(static_cast<size_t>(1), filter->sent_messages.size());
134
135   SpellCheckMsg_RespondSpellingService::Param params;
136   bool ok = SpellCheckMsg_RespondSpellingService::Read(
137       filter->sent_messages[0], & params);
138   base::string16 sent_text = params.c;
139   std::vector<SpellCheckResult> sent_results = params.d;
140   EXPECT_TRUE(ok);
141   EXPECT_EQ(static_cast<size_t>(2), sent_results.size());
142 }
143 #endif