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

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: Addressed the feedback from jam@. 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) {
96 std::vector<WebKit::WebTextCheckingResult> fake_result;
97 bool handled = provider->OnMessageReceived(
98 ViewMsg_SpellChecker_RespondTextCheck
99 (0,
100 parameters.request_id,
101 parameters.document_tag,
102 fake_result));
103 EXPECT_TRUE(handled);
104 }
105
106 TEST_F(SpellCheckProviderTest, SingleRoundtripSuccess) {
107 FakeTextCheckingCompletion completion;
108 int document_tag = 123;
109
110 provider_.RequestTextChecking(WebKit::WebString("hello"),
111 document_tag,
112 &completion);
113 EXPECT_EQ(completion.completion_count_, 0U);
114 EXPECT_EQ(provider_.messages_.size(), 1U);
115 EXPECT_EQ(provider_.pending_text_request_size(), 1U);
116
117 MessageParameters read_parameters =
118 ReadPlatformRequestTextCheck(provider_.messages_[0]);
119 EXPECT_EQ(read_parameters.text, UTF8ToUTF16("hello"));
120
121 FakeMessageArrival(&provider_, read_parameters);
122 EXPECT_EQ(completion.completion_count_, 1U);
123 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
124 }
125
126 TEST_F(SpellCheckProviderTest, TwoRoundtripSuccess) {
127 int document_tag = 123;
128
129 FakeTextCheckingCompletion completion1;
130 provider_.RequestTextChecking(WebKit::WebString("hello"),
131 document_tag,
132 &completion1);
133 FakeTextCheckingCompletion completion2;
134 provider_.RequestTextChecking(WebKit::WebString("bye"),
135 document_tag,
136 &completion2);
137
138 EXPECT_EQ(completion1.completion_count_, 0U);
139 EXPECT_EQ(completion2.completion_count_, 0U);
140 EXPECT_EQ(provider_.messages_.size(), 2U);
141 EXPECT_EQ(provider_.pending_text_request_size(), 2U);
142
143 MessageParameters read_parameters1 =
144 ReadPlatformRequestTextCheck(provider_.messages_[0]);
145 EXPECT_EQ(read_parameters1.text, UTF8ToUTF16("hello"));
146
147 MessageParameters read_parameters2 =
148 ReadPlatformRequestTextCheck(provider_.messages_[1]);
149 EXPECT_EQ(read_parameters2.text, UTF8ToUTF16("bye"));
150
151 FakeMessageArrival(&provider_, read_parameters1);
152 EXPECT_EQ(completion1.completion_count_, 1U);
153 EXPECT_EQ(completion2.completion_count_, 0U);
154 EXPECT_EQ(provider_.pending_text_request_size(), 1U);
155
156 FakeMessageArrival(&provider_, read_parameters2);
157 EXPECT_EQ(completion1.completion_count_, 1U);
158 EXPECT_EQ(completion2.completion_count_, 1U);
159 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
160 }
161
162 TEST_F(SpellCheckProviderTest, PlatformEngineUnavailable) {
163 provider_.is_using_platform_spelling_engine_ = false;
164
165 int document_tag = 123;
166 FakeTextCheckingCompletion completion;
167 provider_.RequestTextChecking(WebKit::WebString("hello"),
168 document_tag,
169 &completion);
170 EXPECT_EQ(completion.completion_count_, 1U);
171 EXPECT_EQ(provider_.messages_.size(), 0U);
172 EXPECT_EQ(provider_.pending_text_request_size(), 0U);
173 }
174
175 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698