Index: chrome/browser/extensions/api/messaging/native_message_process_host_unittest_posix.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_posix.cc |
index c45fbfff417bce7f9883ade60de69e89aef39f2e..35fbbfa3765672133b4facb5ea99832cf9c85eb7 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_posix.cc |
@@ -26,6 +26,8 @@ using content::BrowserThread; |
namespace { |
+const char kTestMessage[] = "{\"text\": \"Hello.\"}"; |
+ |
FilePath GetTestDir() { |
FilePath test_dir; |
PathService::Get(chrome::DIR_TEST_DATA, &test_dir); |
@@ -70,10 +72,12 @@ 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), |
+ native_message_process_host_(NULL) { |
} |
- virtual void SetUp() { |
+ virtual void SetUp() OVERRIDE { |
CommandLine::ForCurrentProcess()->AppendSwitch( |
switches::kEnableNativeMessaging); |
// Change the user data dir so native apps will be looked for in the test |
@@ -86,19 +90,23 @@ class NativeMessagingTest : public ::testing::Test, |
&message_loop_)); |
} |
- virtual void TearDown() { |
+ virtual void TearDown() OVERRIDE { |
// 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_); |
+ if (native_message_process_host_) { |
+ BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, |
+ native_message_process_host_); |
+ } |
message_loop_.RunUntilIdle(); |
} |
- void PostMessageFromNativeProcess(int port_id, const std::string& message) { |
+ virtual void PostMessageFromNativeProcess( |
+ int port_id, |
+ const std::string& message) OVERRIDE { |
last_posted_message_ = message; |
} |
- void CloseChannel(int port_id, bool error) { |
+ virtual void CloseChannel(int port_id, bool error) OVERRIDE { |
} |
void AcquireProcess(NativeMessageProcessHost::ScopedHost process) { |
@@ -106,6 +114,22 @@ class NativeMessagingTest : public ::testing::Test, |
} |
protected: |
+ std::string FormatMessage(const std::string& message) { |
+ Pickle pickle; |
+ pickle.WriteString(message); |
+ return std::string(const_cast<const Pickle*>(&pickle)->payload(), |
+ pickle.payload_size()); |
+ } |
+ |
+ FilePath CreateTempFileWithMessage(const std::string& message) { |
+ FilePath filename; |
+ file_util::CreateTemporaryFile(&filename); |
+ std::string message_with_header = FormatMessage(message); |
+ EXPECT_TRUE(file_util::WriteFile( |
+ filename, message_with_header.data(), message_with_header.size())); |
+ return filename; |
+ } |
+ |
// Force the channel to be dev. |
Feature::ScopedCurrentChannel current_channel_; |
NativeMessageProcessHost* native_message_process_host_; |
@@ -116,44 +140,50 @@ class NativeMessagingTest : public ::testing::Test, |
std::string last_posted_message_; |
}; |
-// Read a single message from a local file (single_message_response.msg). |
+// Read a single message from a local file. |
TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
- FilePath temp_file; |
- file_util::CreateTemporaryFile(&temp_file); |
- FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), |
- temp_file); |
+ FilePath temp_ouput_file; |
+ file_util::CreateTemporaryFile(&temp_ouput_file); |
+ FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
+ |
+ FakeLauncher launcher(temp_input_file, temp_ouput_file); |
NativeMessageProcessHost::CreateWithLauncher( |
- AsWeakPtr(), "empty_app.py", "{}", 0, |
- NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( |
- &NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
+ AsWeakPtr(), "empty_app.py", 0, |
+ base::Bind(&NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
launcher); |
message_loop_.RunUntilIdle(); |
ASSERT_TRUE(native_message_process_host_); |
native_message_process_host_->ReadNowForTesting(); |
message_loop_.RunUntilIdle(); |
- EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}"); |
- file_util::Delete(temp_file, false /* non-recursive */); |
+ EXPECT_EQ(kTestMessage, last_posted_message_); |
+ |
+ file_util::Delete(temp_ouput_file, false /* non-recursive */); |
+ file_util::Delete(temp_input_file, false /* non-recursive */); |
Matt Perry
2013/01/17 19:26:38
nit: might be better to use a ScopedTempDir and pu
Sergey Ulanov
2013/01/18 02:50:11
Done.
|
} |
// Tests sending a single message. The message should get written to |
// |temp_file| and should match the contents of single_message_request.msg. |
TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
- FilePath temp_file; |
- file_util::CreateTemporaryFile(&temp_file); |
- FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"), |
- temp_file); |
+ FilePath temp_output_file; |
+ file_util::CreateTemporaryFile(&temp_output_file); |
+ FilePath temp_input_file = CreateTempFileWithMessage(std::string()); |
+ |
+ FakeLauncher launcher(temp_input_file, temp_output_file); |
NativeMessageProcessHost::CreateWithLauncher( |
- AsWeakPtr(), "empty_app.py", "{\"text\": \"Hello.\"}", 0, |
- NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( |
- &NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
+ AsWeakPtr(), "empty_app.py", 0, |
+ base::Bind(&NativeMessagingTest::AcquireProcess, AsWeakPtr()), |
launcher); |
message_loop_.RunUntilIdle(); |
+ native_message_process_host_->Send(kTestMessage); |
+ message_loop_.RunUntilIdle(); |
ASSERT_TRUE(native_message_process_host_); |
- EXPECT_TRUE(file_util::ContentsEqual( |
- temp_file, GetTestDir().AppendASCII("single_message_request.msg"))); |
+ std::string output; |
+ ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); |
+ EXPECT_EQ(FormatMessage(kTestMessage), output); |
- file_util::Delete(temp_file, false /* non-recursive */); |
+ file_util::Delete(temp_output_file, false /* non-recursive */); |
+ file_util::Delete(temp_input_file, false /* non-recursive */); |
} |
// Disabled, see http://crbug.com/159754. |
@@ -161,22 +191,23 @@ TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
// it recieved. |
TEST_F(NativeMessagingTest, DISABLED_EchoConnect) { |
NativeMessageProcessHost::Create( |
- AsWeakPtr(), "echo.py", "{\"text\": \"Hello.\"}", 0, |
- NativeMessageProcessHost::TYPE_CONNECT, base::Bind( |
- &NativeMessagingTest::AcquireProcess, AsWeakPtr())); |
+ AsWeakPtr(), "echo.py", 0, |
+ base::Bind(&NativeMessagingTest::AcquireProcess, AsWeakPtr())); |
+ message_loop_.RunUntilIdle(); |
+ native_message_process_host_->Send("{\"text\": \"Hello.\"}"); |
message_loop_.RunUntilIdle(); |
ASSERT_TRUE(native_message_process_host_); |
native_message_process_host_->ReadNowForTesting(); |
message_loop_.RunUntilIdle(); |
- EXPECT_EQ(last_posted_message_, |
- "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}"); |
+ EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}", |
+ last_posted_message_); |
native_message_process_host_->Send("{\"foo\": \"bar\"}"); |
message_loop_.RunUntilIdle(); |
native_message_process_host_->ReadNowForTesting(); |
message_loop_.RunUntilIdle(); |
- EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}"); |
+ EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_); |
} |
} // namespace extensions |