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

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: Adopted Carnitas approach, added tests. 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
OLDNEW
(Empty)
1 // Copyright (c) 2006-2008 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 class TestingSpellCheckProvider : public SpellCheckProvider {
18 public:
19 TestingSpellCheckProvider()
20 : SpellCheckProvider(NULL, NULL),
21 is_using_platform_spelling_engine_(true) {
22 }
23
24 virtual ~TestingSpellCheckProvider() {
25 for (std::vector<IPC::Message*>::iterator i = messages_.begin();
26 i != messages_.end();
27 ++i) {
28 delete *i;
29 }
30 }
31
32 virtual bool Send(IPC::Message* message) {
33 messages_.push_back(message);
34 return true;
35 }
36
37 virtual bool is_using_platform_spelling_engine() const {
38 return is_using_platform_spelling_engine_;
39 }
40
41 std::vector<IPC::Message*> messages_;
42 bool is_using_platform_spelling_engine_;
43 };
44
45 class FakeTextCheckingCompletion : public WebKit::WebTextCheckingCompletion {
46 public:
47 FakeTextCheckingCompletion()
48 : completion_count_(0) {
49 }
50
51 virtual void didFinishCheckingText(
52 const WebKit::WebVector<WebKit::WebTextCheckingResult>& results) {
53 completion_count_++;
54 last_results_ = results;
55 }
56
57 size_t completion_count_;
58 WebKit::WebVector<WebKit::WebTextCheckingResult> last_results_;
59 };
60
61 class SpellCheckProviderTest : public testing::Test {
62 public:
63 SpellCheckProviderTest() { }
64 virtual ~SpellCheckProviderTest() { }
65
66 protected:
67 TestingSpellCheckProvider provider_;
68 };
69
70 struct MessageParameters {
71 MessageParameters()
72 : router_id(0),
73 request_id(0),
74 document_tag(0) { }
75
76 int router_id;
77 int request_id;
78 int document_tag;
79 string16 text;
80 };
81
82 MessageParameters ReadPlatformRequestTextCheck(IPC::Message* message) {
83 MessageParameters parameters;
84 bool ok = ViewHostMsg_SpellChecker_PlatformRequestTextCheck::Read(
85 message,
86 &parameters.router_id,
87 &parameters.request_id,
88 &parameters.document_tag,
89 &parameters.text);
90 EXPECT_TRUE(ok);
91 return parameters;
92 }
93
94 void FakeMessageArrival(SpellCheckProvider* provider,
95 const MessageParameters& parameters)
jam 2011/02/17 18:36:59 nit: spacing is off. also brace bracket belongs o
gmorrita 2011/02/18 01:08:01 Done.
96 {
97 std::vector<WebKit::WebTextCheckingResult> fake_result;
98 bool handled = provider->OnMessageReceived(
99 ViewMsg_SpellChecker_RespondTextCheck
100 (0,
101 parameters.request_id,
102 parameters.document_tag,
103 fake_result));
104 EXPECT_TRUE(handled);
105 }
106
107 TEST_F(SpellCheckProviderTest, SingleRoundtripSuccess) {
108 FakeTextCheckingCompletion completion;
109 int document_tag = 123;
110
111 provider_.RequestTextChecking(WebKit::WebString("hello"),
112 document_tag,
113 &completion);
114 EXPECT_EQ(completion.completion_count_, 0U);
115 EXPECT_EQ(provider_.messages_.size(), 1U);
116 EXPECT_EQ(provider_.pending_text_request_size(), 1U);
117
118 MessageParameters read_parameters =
119 ReadPlatformRequestTextCheck(provider_.messages_[0]);
120 EXPECT_EQ(read_parameters.text, UTF8ToUTF16("hello"));
121
122 FakeMessageArrival(&provider_, read_parameters);
123 EXPECT_EQ(completion.completion_count_, 1U);
124 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
125 }
126
127 TEST_F(SpellCheckProviderTest, TwoRoundtripSuccess) {
128 int document_tag = 123;
129
130 FakeTextCheckingCompletion completion1;
131 provider_.RequestTextChecking(WebKit::WebString("hello"),
132 document_tag,
133 &completion1);
134 FakeTextCheckingCompletion completion2;
135 provider_.RequestTextChecking(WebKit::WebString("bye"),
136 document_tag,
137 &completion2);
138
139 EXPECT_EQ(completion1.completion_count_, 0U);
140 EXPECT_EQ(completion2.completion_count_, 0U);
141 EXPECT_EQ(provider_.messages_.size(), 2U);
142 EXPECT_EQ(provider_.pending_text_request_size(), 2U);
143
144 MessageParameters read_parameters1 =
145 ReadPlatformRequestTextCheck(provider_.messages_[0]);
146 EXPECT_EQ(read_parameters1.text, UTF8ToUTF16("hello"));
147
148 MessageParameters read_parameters2 =
149 ReadPlatformRequestTextCheck(provider_.messages_[1]);
150 EXPECT_EQ(read_parameters2.text, UTF8ToUTF16("bye"));
151
152 FakeMessageArrival(&provider_, read_parameters1);
153 EXPECT_EQ(completion1.completion_count_, 1U);
154 EXPECT_EQ(completion2.completion_count_, 0U);
155 EXPECT_EQ(provider_.pending_text_request_size(), 1U);
156
157 FakeMessageArrival(&provider_, read_parameters2);
158 EXPECT_EQ(completion1.completion_count_, 1U);
159 EXPECT_EQ(completion2.completion_count_, 1U);
160 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
161 }
162
163 TEST_F(SpellCheckProviderTest, PlatformEngineUnavailable) {
164 provider_.is_using_platform_spelling_engine_ = false;
165
166 int document_tag = 123;
167 FakeTextCheckingCompletion completion;
168 provider_.RequestTextChecking(WebKit::WebString("hello"),
169 document_tag,
170 &completion);
171 EXPECT_EQ(completion.completion_count_, 1U);
172 EXPECT_EQ(provider_.messages_.size(), 0U);
173 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
174 }
175
176 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698