Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: chrome/renderer/spellchecker/spellcheck_provider_unittest.cc

Issue 6392045: Integrating Mac OS Grammar checker into Chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated to ToT Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/spellchecker/spellcheck_provider.cc ('k') | webkit/glue/webpreferences.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 <vector>
6
7 #include "base/utf_string_conversions.h"
8 #include "chrome/common/render_messages.h"
9 #include "chrome/renderer/spellchecker/spellcheck_provider.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple tion.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult .h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h"
14
15 namespace {
16
17 // Faked test target, which stores sent message for verification,
18 // and allows manipulate |is_using_platform_spelling_engine| parameter.
19 class TestingSpellCheckProvider : public SpellCheckProvider {
20 public:
21 TestingSpellCheckProvider()
22 : SpellCheckProvider(NULL, NULL),
23 is_using_platform_spelling_engine_(true) {
24 }
25
26 virtual ~TestingSpellCheckProvider() {
27 for (std::vector<IPC::Message*>::iterator i = messages_.begin();
28 i != messages_.end();
29 ++i) {
30 delete *i;
31 }
32 }
33
34 virtual bool Send(IPC::Message* message) {
35 messages_.push_back(message);
36 return true;
37 }
38
39 virtual bool is_using_platform_spelling_engine() const {
40 return is_using_platform_spelling_engine_;
41 }
42
43 std::vector<IPC::Message*> messages_;
44 bool is_using_platform_spelling_engine_;
45 };
46
47 // A fake completion object for verification.
48 class FakeTextCheckingCompletion : public WebKit::WebTextCheckingCompletion {
49 public:
50 FakeTextCheckingCompletion()
51 : completion_count_(0) {
52 }
53
54 virtual void didFinishCheckingText(
55 const WebKit::WebVector<WebKit::WebTextCheckingResult>& results) {
56 completion_count_++;
57 last_results_ = results;
58 }
59
60 size_t completion_count_;
61 WebKit::WebVector<WebKit::WebTextCheckingResult> last_results_;
62 };
63
64 class SpellCheckProviderTest : public testing::Test {
65 public:
66 SpellCheckProviderTest() { }
67 virtual ~SpellCheckProviderTest() { }
68
69 protected:
70 TestingSpellCheckProvider provider_;
71 };
72
73 struct MessageParameters {
74 MessageParameters()
75 : router_id(0),
76 request_id(0),
77 document_tag(0) { }
78
79 int router_id;
80 int request_id;
81 int document_tag;
82 string16 text;
83 };
84
85 MessageParameters ReadPlatformRequestTextCheck(IPC::Message* message) {
86 MessageParameters parameters;
87 bool ok = ViewHostMsg_SpellChecker_PlatformRequestTextCheck::Read(
88 message,
89 &parameters.router_id,
90 &parameters.request_id,
91 &parameters.document_tag,
92 &parameters.text);
93 EXPECT_TRUE(ok);
94 return parameters;
95 }
96
97 void FakeMessageArrival(SpellCheckProvider* provider,
98 const MessageParameters& parameters) {
99 std::vector<WebKit::WebTextCheckingResult> fake_result;
100 bool handled = provider->OnMessageReceived(
101 ViewMsg_SpellChecker_RespondTextCheck
102 (0,
103 parameters.request_id,
104 parameters.document_tag,
105 fake_result));
106 EXPECT_TRUE(handled);
107 }
108
109 TEST_F(SpellCheckProviderTest, SingleRoundtripSuccess) {
110 FakeTextCheckingCompletion completion;
111 int document_tag = 123;
112
113 provider_.RequestTextChecking(WebKit::WebString("hello"),
114 document_tag,
115 &completion);
116 EXPECT_EQ(completion.completion_count_, 0U);
117 EXPECT_EQ(provider_.messages_.size(), 1U);
118 EXPECT_EQ(provider_.pending_text_request_size(), 1U);
119
120 MessageParameters read_parameters =
121 ReadPlatformRequestTextCheck(provider_.messages_[0]);
122 EXPECT_EQ(read_parameters.text, UTF8ToUTF16("hello"));
123
124 FakeMessageArrival(&provider_, read_parameters);
125 EXPECT_EQ(completion.completion_count_, 1U);
126 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
127 }
128
129 TEST_F(SpellCheckProviderTest, TwoRoundtripSuccess) {
130 int document_tag = 123;
131
132 FakeTextCheckingCompletion completion1;
133 provider_.RequestTextChecking(WebKit::WebString("hello"),
134 document_tag,
135 &completion1);
136 FakeTextCheckingCompletion completion2;
137 provider_.RequestTextChecking(WebKit::WebString("bye"),
138 document_tag,
139 &completion2);
140
141 EXPECT_EQ(completion1.completion_count_, 0U);
142 EXPECT_EQ(completion2.completion_count_, 0U);
143 EXPECT_EQ(provider_.messages_.size(), 2U);
144 EXPECT_EQ(provider_.pending_text_request_size(), 2U);
145
146 MessageParameters read_parameters1 =
147 ReadPlatformRequestTextCheck(provider_.messages_[0]);
148 EXPECT_EQ(read_parameters1.text, UTF8ToUTF16("hello"));
149
150 MessageParameters read_parameters2 =
151 ReadPlatformRequestTextCheck(provider_.messages_[1]);
152 EXPECT_EQ(read_parameters2.text, UTF8ToUTF16("bye"));
153
154 FakeMessageArrival(&provider_, read_parameters1);
155 EXPECT_EQ(completion1.completion_count_, 1U);
156 EXPECT_EQ(completion2.completion_count_, 0U);
157 EXPECT_EQ(provider_.pending_text_request_size(), 1U);
158
159 FakeMessageArrival(&provider_, read_parameters2);
160 EXPECT_EQ(completion1.completion_count_, 1U);
161 EXPECT_EQ(completion2.completion_count_, 1U);
162 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
163 }
164
165 TEST_F(SpellCheckProviderTest, PlatformEngineUnavailable) {
166 provider_.is_using_platform_spelling_engine_ = false;
167
168 int document_tag = 123;
169 FakeTextCheckingCompletion completion;
170 provider_.RequestTextChecking(WebKit::WebString("hello"),
171 document_tag,
172 &completion);
173 EXPECT_EQ(completion.completion_count_, 1U);
174 EXPECT_EQ(provider_.messages_.size(), 0U);
175 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
176 }
177
178 } // namespace
OLDNEW
« no previous file with comments | « chrome/renderer/spellchecker/spellcheck_provider.cc ('k') | webkit/glue/webpreferences.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698