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