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

Side by Side Diff: content/browser/hyphenator/hyphenator_message_filter_unittest.cc

Issue 10854245: In-te-grate hy-phen-ator to con-tent. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 3 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "content/browser/hyphenator/hyphenator_message_filter.h"
6
7 #include "base/base_paths.h"
8 #include "base/file_path.h"
9 #include "base/path_service.h"
10 #include "base/string16.h"
11 #include "base/utf_string_conversions.h"
12 #include "content/common/hyphenator_messages.h"
13 #include "content/public/test/mock_render_process_host.h"
14 #include "content/public/test/test_browser_context.h"
15 #include "ipc/ipc_message_utils.h"
16 #include "ipc/ipc_platform_file.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace content {
20
21 // A class derived from the HyphenatorMessageFilter class used in unit tests.
22 // This class overrides some methods so we can test the HyphenatorMessageFilter
23 // class without posting tasks.
24 class TestHyphenatorMessageFilter : public content::HyphenatorMessageFilter {
25 public:
26 explicit TestHyphenatorMessageFilter(content::RenderProcessHost* host)
27 : content::HyphenatorMessageFilter(host),
28 type_(0),
29 file_(base::kInvalidPlatformFileValue) {
30 }
31
32 const string16& locale() const { return locale_; }
33 uint32 type() const { return type_; }
34 base::PlatformFile file() const { return file_; }
35
36 // content::BrowserMessageFilter implementation.
37 virtual bool Send(IPC::Message* message) OVERRIDE {
38 if (message->type() != HyphenatorMsg_SetDictionary::ID)
39 return false;
40
41 // Read the PlatformFileForTransit object and check if its value is
42 // kInvalidPlatformFileValue. Close the incoming file if it is not
43 // kInvalidPlatformFileValue to prevent leaving the dictionary file open.
44 type_ = message->type();
45 PickleIterator iter(*message);
46 IPC::PlatformFileForTransit file;
47 IPC::ParamTraits<IPC::PlatformFileForTransit>::Read(message, &iter, &file);
48 file_ = IPC::PlatformFileForTransitToPlatformFile(file);
49 return true;
50 }
51
52 void SetDictionary(base::PlatformFile file) {
53 dictionary_file_ = file;
54 }
55
56 void Reset() {
57 if (dictionary_file_ != base::kInvalidPlatformFileValue) {
58 base::ClosePlatformFile(dictionary_file_);
59 dictionary_file_ = base::kInvalidPlatformFileValue;
60 }
61 locale_.clear();
62 type_ = 0;
63 if (file_ != base::kInvalidPlatformFileValue) {
64 base::ClosePlatformFile(file_);
65 file_ = base::kInvalidPlatformFileValue;
66 }
67 }
68
69 private:
70 virtual ~TestHyphenatorMessageFilter() {
71 }
72
73 // content::HyphenatorMessageFilter implementation. This function emulates the
74 // original implementation without posting a task.
75 virtual void OnOpenDictionary(const string16& locale) OVERRIDE {
76 locale_ = locale;
77 if (dictionary_file_ == base::kInvalidPlatformFileValue)
78 OpenDictionary(locale);
79 SendDictionary();
80 }
81
82 string16 locale_;
83 uint32 type_;
84 base::PlatformFile file_;
85 };
86
87 } // namespace content
88
89 class HyphenatorMessageFilterTest : public testing::Test {
90 public:
91 HyphenatorMessageFilterTest() {
92 context_.reset(new content::TestBrowserContext);
93 host_.reset(new content::MockRenderProcessHost(context_.get()));
94 filter_ = new content::TestHyphenatorMessageFilter(host_.get());
95 }
96
97 virtual ~HyphenatorMessageFilterTest() {}
98
99 scoped_ptr<content::TestBrowserContext> context_;
100 scoped_ptr<content::MockRenderProcessHost> host_;
101 scoped_refptr<content::TestHyphenatorMessageFilter> filter_;
102 };
103
104 // Verifies IPC messages sent by the HyphenatorMessageFilter class when it
105 // receives IPC messages (HyphenatorHostMsg_OpenDictionary).
106 TEST_F(HyphenatorMessageFilterTest, OpenDictionary) {
107 // Send a HyphenatorHostMsg_OpenDictionary message with an invalid locale and
108 // verify it sends a HyphenatorMsg_SetDictionary message with an invalid file.
109 string16 invalid_locale(ASCIIToUTF16("xx-xx"));
110 IPC::Message invalid_message(
111 0, HyphenatorHostMsg_OpenDictionary::ID, IPC::Message::PRIORITY_NORMAL);
112 invalid_message.WriteString16(invalid_locale);
113
114 bool message_was_ok = false;
115 filter_->OnMessageReceived(invalid_message, &message_was_ok);
116 EXPECT_TRUE(message_was_ok);
117 EXPECT_EQ(invalid_locale, filter_->locale());
118 EXPECT_EQ(HyphenatorMsg_SetDictionary::ID, filter_->type());
119 EXPECT_EQ(base::kInvalidPlatformFileValue, filter_->file());
120
121 filter_->Reset();
122
123 // Open a sample dictionary file and attach it to the
124 // HyphenatorMessageFilter class so it can return a valid file.
125 FilePath path;
126 PathService::Get(base::DIR_SOURCE_ROOT, &path);
127 path = path.Append(FILE_PATH_LITERAL("third_party"));
128 path = path.Append(FILE_PATH_LITERAL("hyphen"));
129 path = path.Append(FILE_PATH_LITERAL("hyph_en_US.dic"));
130 base::PlatformFile file = base::CreatePlatformFile(
131 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
132 NULL, NULL);
133 EXPECT_NE(base::kInvalidPlatformFileValue, file);
134 filter_->SetDictionary(file);
135
136 // Send a HyphenatorHostMsg_OpenDictionary message with an empty locale and
137 // verify it sends a HyphenatorMsg_SetDictionary message with a valid file.
138 string16 empty_locale;
139 IPC::Message valid_message(
140 0, HyphenatorHostMsg_OpenDictionary::ID, IPC::Message::PRIORITY_NORMAL);
141 valid_message.WriteString16(empty_locale);
142
143 message_was_ok = false;
144 filter_->OnMessageReceived(valid_message, &message_was_ok);
145 EXPECT_TRUE(message_was_ok);
146 EXPECT_EQ(empty_locale, filter_->locale());
147 EXPECT_EQ(HyphenatorMsg_SetDictionary::ID, filter_->type());
148 EXPECT_NE(base::kInvalidPlatformFileValue, filter_->file());
149
150 // Delete all resources used by this test.
151 filter_->Reset();
152 if (file != base::kInvalidPlatformFileValue)
153 base::ClosePlatformFile(file);
154 }
OLDNEW
« no previous file with comments | « content/browser/hyphenator/hyphenator_message_filter.cc ('k') | content/browser/renderer_host/render_process_host_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698