OLD | NEW |
| (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/files/file_path.h" | |
9 #include "base/path_service.h" | |
10 #include "base/strings/string16.h" | |
11 #include "base/strings/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 HyphenatorMessageFilter { | |
25 public: | |
26 explicit TestHyphenatorMessageFilter(RenderProcessHost* host) | |
27 : 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 // 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 delete message; | |
50 return true; | |
51 } | |
52 | |
53 void SetDictionary(base::PlatformFile file) { | |
54 dictionary_file_ = file; | |
55 } | |
56 | |
57 void Reset() { | |
58 if (dictionary_file_ != base::kInvalidPlatformFileValue) { | |
59 base::ClosePlatformFile(dictionary_file_); | |
60 dictionary_file_ = base::kInvalidPlatformFileValue; | |
61 } | |
62 locale_.clear(); | |
63 type_ = 0; | |
64 if (file_ != base::kInvalidPlatformFileValue) { | |
65 base::ClosePlatformFile(file_); | |
66 file_ = base::kInvalidPlatformFileValue; | |
67 } | |
68 } | |
69 | |
70 private: | |
71 virtual ~TestHyphenatorMessageFilter() { | |
72 } | |
73 | |
74 // HyphenatorMessageFilter implementation. This function emulates the | |
75 // original implementation without posting a task. | |
76 virtual void OnOpenDictionary(const string16& locale) OVERRIDE { | |
77 locale_ = locale; | |
78 if (dictionary_file_ == base::kInvalidPlatformFileValue) | |
79 OpenDictionary(locale); | |
80 SendDictionary(); | |
81 } | |
82 | |
83 string16 locale_; | |
84 uint32 type_; | |
85 base::PlatformFile file_; | |
86 }; | |
87 | |
88 class HyphenatorMessageFilterTest : public testing::Test { | |
89 public: | |
90 HyphenatorMessageFilterTest() { | |
91 context_.reset(new TestBrowserContext); | |
92 host_.reset(new MockRenderProcessHost(context_.get())); | |
93 filter_ = new TestHyphenatorMessageFilter(host_.get()); | |
94 } | |
95 | |
96 virtual ~HyphenatorMessageFilterTest() {} | |
97 | |
98 scoped_ptr<TestBrowserContext> context_; | |
99 scoped_ptr<MockRenderProcessHost> host_; | |
100 scoped_refptr<TestHyphenatorMessageFilter> filter_; | |
101 }; | |
102 | |
103 // Verifies IPC messages sent by the HyphenatorMessageFilter class when it | |
104 // receives IPC messages (HyphenatorHostMsg_OpenDictionary). | |
105 TEST_F(HyphenatorMessageFilterTest, OpenDictionary) { | |
106 // Send a HyphenatorHostMsg_OpenDictionary message with an invalid locale and | |
107 // verify it sends a HyphenatorMsg_SetDictionary message with an invalid file. | |
108 string16 invalid_locale(ASCIIToUTF16("xx-xx")); | |
109 IPC::Message invalid_message( | |
110 0, HyphenatorHostMsg_OpenDictionary::ID, IPC::Message::PRIORITY_NORMAL); | |
111 invalid_message.WriteString16(invalid_locale); | |
112 | |
113 bool message_was_ok = false; | |
114 filter_->OnMessageReceived(invalid_message, &message_was_ok); | |
115 EXPECT_TRUE(message_was_ok); | |
116 EXPECT_EQ(invalid_locale, filter_->locale()); | |
117 EXPECT_EQ(HyphenatorMsg_SetDictionary::ID, filter_->type()); | |
118 EXPECT_EQ(base::kInvalidPlatformFileValue, filter_->file()); | |
119 | |
120 filter_->Reset(); | |
121 | |
122 // Open a sample dictionary file and attach it to the | |
123 // HyphenatorMessageFilter class so it can return a valid file. | |
124 base::FilePath path; | |
125 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
126 path = path.Append(FILE_PATH_LITERAL("third_party")); | |
127 path = path.Append(FILE_PATH_LITERAL("hyphen")); | |
128 path = path.Append(FILE_PATH_LITERAL("hyph_en_US.dic")); | |
129 base::PlatformFile file = base::CreatePlatformFile( | |
130 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
131 NULL, NULL); | |
132 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
133 filter_->SetDictionary(file); | |
134 file = base::kInvalidPlatformFileValue; // Ownership has been transferred. | |
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 } | |
153 | |
154 } // namespace content | |
OLD | NEW |