Chromium Code Reviews| Index: chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc |
| diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest_posix.cc b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc |
| similarity index 72% |
| rename from chrome/browser/extensions/api/messaging/native_message_process_host_unittest_posix.cc |
| rename to chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc |
| index dfddf853ea910c97a3f18faf99d4fdbedb7f8033..03953b2baa003c6690ad71e3dd7f396dfbf97a3b 100644 |
| --- a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest_posix.cc |
| +++ b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc |
| @@ -11,6 +11,7 @@ |
| #include "base/message_loop.h" |
| #include "base/path_service.h" |
| #include "base/platform_file.h" |
| +#include "base/threading/platform_thread.h" |
| #include "base/threading/sequenced_worker_pool.h" |
| #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| @@ -26,6 +27,17 @@ using content::BrowserThread; |
| namespace { |
| +const int kMaxNapTimes = 3; |
| +const int kSleepTimeMS = 50; |
| + |
| +#if defined(OS_WIN) |
| +const char kEmptyAppName[] = "empty_app.bat"; |
| +const char kEchoAppName[] = "echo.bat"; |
| +#else |
| +const char kEmptyAppName[] = "empty_app.py"; |
| +const char kEchoAppName[] = "echo.py"; |
| +#endif // defined(OS_WIN) |
| + |
| FilePath GetTestDir() { |
| FilePath test_dir; |
| PathService::Get(chrome::DIR_TEST_DATA, &test_dir); |
| @@ -40,13 +52,14 @@ namespace extensions { |
| class FakeLauncher : public NativeProcessLauncher { |
| public: |
| FakeLauncher(FilePath read_file, FilePath write_file) { |
| + int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_ASYNC; |
| read_file_ = base::CreatePlatformFile( |
| read_file, |
| - base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| + flags | base::PLATFORM_FILE_READ, |
| NULL, NULL); |
| write_file_ = base::CreatePlatformFile( |
| write_file, |
| - base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| + flags | base::PLATFORM_FILE_WRITE, |
| NULL, NULL); |
| } |
| @@ -70,7 +83,8 @@ class NativeMessagingTest : public ::testing::Test, |
| public NativeMessageProcessHost::Client, |
| public base::SupportsWeakPtr<NativeMessagingTest> { |
| public: |
| - NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV) { |
| + NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV), |
| + got_message_(false) { |
| } |
| virtual void SetUp() { |
| @@ -84,51 +98,70 @@ class NativeMessagingTest : public ::testing::Test, |
| &message_loop_)); |
| file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, |
| &message_loop_)); |
| + io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO, |
| + &message_loop_)); |
| } |
| virtual void TearDown() { |
| // Change the user data dir back for other tests. |
| ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); |
| - BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, |
| - native_message_process_host_); |
| - message_loop_.RunAllPending(); |
| } |
| void PostMessageFromNativeProcess(int port_id, const std::string& message) { |
| last_posted_message_ = message; |
| + got_message_ = true; |
| } |
| void CloseChannel(int port_id, bool error) { |
| } |
| void AcquireProcess(NativeMessageProcessHost::ScopedHost process) { |
| - native_message_process_host_ = process.release(); |
| + native_message_process_host_.swap(process); |
| + } |
| + |
| + void WaitForMessage() { |
| + got_message_ = false; |
| + native_message_process_host_->ReadNowForTesting(); |
| + message_loop_.RunAllPending(); |
| + |
| + for (int nap_times = 0; nap_times < kMaxNapTimes && !got_message_; |
|
Matt Perry
2012/10/24 23:40:31
Please don't use polling in tests. It results in f
eaugusti
2012/10/30 22:03:12
Done.
|
| + ++nap_times) { |
| + base::PlatformThread::Sleep( |
| + base::TimeDelta::FromMilliseconds(kSleepTimeMS)); |
| + native_message_process_host_->ReadNowForTesting(); |
| + message_loop_.RunAllPending(); |
| + } |
| + |
| + ASSERT_TRUE(got_message_); |
| } |
| protected: |
| // Force the channel to be dev. |
| Feature::ScopedCurrentChannel current_channel_; |
| - NativeMessageProcessHost* native_message_process_host_; |
| + NativeMessageProcessHost::ScopedHost native_message_process_host_; |
| FilePath user_data_dir_; |
| MessageLoopForIO message_loop_; |
| scoped_ptr<content::TestBrowserThread> ui_thread_; |
| scoped_ptr<content::TestBrowserThread> file_thread_; |
| + scoped_ptr<content::TestBrowserThread> io_thread_; |
| std::string last_posted_message_; |
| + bool got_message_; |
| }; |
| // Read a single message from a local file (single_message_response.msg). |
| TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| FilePath temp_file; |
| file_util::CreateTemporaryFile(&temp_file); |
| - FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), |
| - temp_file); |
| NativeMessageProcessHost::CreateWithLauncher( |
| - AsWeakPtr(), "empty_app.py", "{}", 0, |
| + AsWeakPtr(), kEmptyAppName, "{}", 0, |
| NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( |
| &NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
| - launcher); |
| + scoped_ptr<NativeProcessLauncher>(new FakeLauncher( |
| + GetTestDir().AppendASCII("single_message_response.msg"), temp_file))); |
| message_loop_.RunAllPending(); |
| - ASSERT_TRUE(native_message_process_host_); |
| + ASSERT_TRUE(native_message_process_host_.get()); |
| + // The process host is directly connected to files, there is no need to wait |
| + // for the message. |
| native_message_process_host_->ReadNowForTesting(); |
| message_loop_.RunAllPending(); |
| EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}"); |
| @@ -140,19 +173,16 @@ TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| FilePath temp_file; |
| file_util::CreateTemporaryFile(&temp_file); |
| - FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), |
| - temp_file); |
| NativeMessageProcessHost::CreateWithLauncher( |
| - AsWeakPtr(), "empty_app.py", "{\"text\": \"Hello.\"}", 0, |
| + AsWeakPtr(), kEmptyAppName, "{\"text\": \"Hello.\"}", 0, |
| NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( |
| &NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
| - launcher); |
| + scoped_ptr<NativeProcessLauncher>(new FakeLauncher( |
| + GetTestDir().AppendASCII("single_message_response.msg"), temp_file))); |
| message_loop_.RunAllPending(); |
| - ASSERT_TRUE(native_message_process_host_); |
| - |
| + ASSERT_TRUE(native_message_process_host_.get()); |
| EXPECT_TRUE(file_util::ContentsEqual( |
| temp_file, GetTestDir().AppendASCII("single_message_request.msg"))); |
| - |
| file_util::Delete(temp_file, false /* non-recursive */); |
| } |
| @@ -160,21 +190,16 @@ TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| // it recieved. |
| TEST_F(NativeMessagingTest, EchoConnect) { |
| NativeMessageProcessHost::Create( |
| - AsWeakPtr(), "echo.py", "{\"text\": \"Hello.\"}", 0, |
| + AsWeakPtr(), kEchoAppName, "{\"text\": \"Hello.\"}", 0, |
| NativeMessageProcessHost::TYPE_CONNECT, base::Bind( |
| &NativeMessagingTest::AcquireProcess, AsWeakPtr())); |
| message_loop_.RunAllPending(); |
| - ASSERT_TRUE(native_message_process_host_); |
| - |
| - native_message_process_host_->ReadNowForTesting(); |
| - message_loop_.RunAllPending(); |
| + ASSERT_TRUE(native_message_process_host_.get()); |
| + WaitForMessage(); |
| EXPECT_EQ(last_posted_message_, |
| "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}"); |
| - |
| native_message_process_host_->Send("{\"foo\": \"bar\"}"); |
| - message_loop_.RunAllPending(); |
| - native_message_process_host_->ReadNowForTesting(); |
| - message_loop_.RunAllPending(); |
| + WaitForMessage(); |
| EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}"); |
| } |