Chromium Code Reviews| Index: content/renderer/hyphenator/hyphenator_unittest.cc |
| =================================================================== |
| --- content/renderer/hyphenator/hyphenator_unittest.cc (revision 152650) |
| +++ content/renderer/hyphenator/hyphenator_unittest.cc (working copy) |
| @@ -7,15 +7,69 @@ |
| #include "base/path_service.h" |
| #include "base/platform_file.h" |
| #include "base/utf_string_conversions.h" |
| +#include "content/common/hyphenator_messages.h" |
| +#include "content/public/test/mock_render_thread.h" |
| +#include "ipc/ipc_listener.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/hyphen/hyphen.h" |
| +namespace { |
| + |
| +// A mock message listener that listens HyphenatorHost messages. This class |
|
tony
2012/08/22 19:05:50
Nit: that listens _for_ HyphenatorHost messages
Hironori Bono
2012/08/27 06:57:28
Done. Thanks for fixing my typo.
|
| +// intercepts a HyphenatorHostMsg_OpenDictionary message sent to an |
| +// IPC::TestSink object and emulates a browser process. |
| +class MockListener : public IPC::Listener { |
| + public: |
| + MockListener(content::Hyphenator* hyphenator, const string16& locale) |
| + : hyphenator_(hyphenator), |
| + locale_(locale) { |
| + } |
| + virtual ~MockListener() { |
| + } |
| + |
| + // IPC::ChannelProxy::MessageFilter implementation. |
| + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| + if (message.type() != HyphenatorHostMsg_OpenDictionary::ID) |
| + return false; |
| + |
| + // Retrieve the locale parameter directly because HyphenatorHost messages |
| + // are internal messages and unit tests cannot access its member functions, |
| + // i.e. unit tests cannot call the HyphenatorHostMsg_OpenDictionary::Read |
| + // function. |
| + PickleIterator iter(message); |
| + string16 locale; |
| + EXPECT_TRUE(message.ReadString16(&iter, &locale)); |
| + EXPECT_EQ(locale_, locale); |
| + |
| + // Open the default dictionary and call the Hyphenator::SetDictionary |
| + // function instead of sending a HyphenatorMsg_SetDictionary message. (The |
| + // Hyphenator class posts a task when it receives the message, i.e. sending |
| + // the message makes this unit test flaky.) |
| + FilePath dictionary_path; |
| + if (!PathService::Get(base::DIR_SOURCE_ROOT, &dictionary_path)) |
| + return false; |
| + dictionary_path = dictionary_path.AppendASCII("third_party"); |
| + dictionary_path = dictionary_path.AppendASCII("hyphen"); |
| + dictionary_path = dictionary_path.AppendASCII("hyph_en_US.dic"); |
| + base::PlatformFile file = base::CreatePlatformFile( |
| + dictionary_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| + NULL, NULL); |
| + hyphenator_->SetDictionary(file); |
| + return true; |
| + } |
| + |
| + private: |
| + content::Hyphenator* hyphenator_; |
| + string16 locale_; |
| +}; |
| + |
| +} // namespace |
| + |
| // A unit test for our hyphenator. This class loads a sample hyphenation |
| // dictionary and hyphenates words. |
| class HyphenatorTest : public testing::Test { |
| public: |
| HyphenatorTest() { |
| - Initialize(); |
| } |
| bool Initialize() { |
| @@ -49,8 +103,22 @@ |
| return hyphenated_word; |
| } |
| + bool OpenDictionary(const string16& locale) { |
| + hyphenator_.reset(new content::Hyphenator(base::kInvalidPlatformFileValue)); |
| + thread_.reset(new content::MockRenderThread()); |
| + listener_.reset(new MockListener(hyphenator_.get(), locale)); |
| + thread_->sink().AddFilter(listener_.get()); |
| + return hyphenator_->Attach(thread_.get(), locale); |
| + } |
| + |
| + size_t GetMessageCount() const { |
| + return thread_->sink().message_count(); |
| + } |
| + |
| private: |
| scoped_ptr<content::Hyphenator> hyphenator_; |
| + scoped_ptr<content::MockRenderThread> thread_; |
| + scoped_ptr<MockListener> listener_; |
| }; |
| // Verifies that our hyphenator yields the same hyphenated words as the original |
| @@ -82,9 +150,29 @@ |
| { "undone.", "un-done." }, |
| { "unnecessary", "un-nec-es-sary" }, |
| }; |
| + Initialize(); |
| for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| string16 input = ASCIIToUTF16(kTestCases[i].input); |
| string16 expected = ASCIIToUTF16(kTestCases[i].expected); |
| EXPECT_EQ(expected, Hyphenate(input)); |
| } |
| } |
| + |
| +// Verifies that our hyphenator sends a HyphenatorHostMsg_OpenDictionary |
| +// message to ask a browser to open a dictionary. Also, this test verifies that |
| +// our hyphenator can hyphnate words when the Hyphenator::SetDictionary function |
| +// is called. |
| +TEST_F(HyphenatorTest, openDictionary) { |
| + // Send a HyphenatorHostMsg_OpenDictionary message and verify it is handled by |
| + // our MockListner class. |
| + EXPECT_TRUE(OpenDictionary(string16())); |
| + EXPECT_EQ(0U, GetMessageCount()); |
| + |
| + // Verify that we can now hyphenate words. When the MockListener class |
| + // receives a HyphenatorHostMsg_OpenDictionary message, it calls the |
| + // Hyphenator::SetDictionary function to attach the hyphenation dictionary. |
| + // So, the Hyphenate function should hyphenate words now. |
| + string16 input = ASCIIToUTF16("hyphenation"); |
| + string16 expected = ASCIIToUTF16("hy-phen-ation"); |
| + EXPECT_EQ(expected, Hyphenate(input)); |
| +} |