| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "chrome/common/extensions/extension.h" | 26 #include "chrome/common/extensions/extension.h" |
| 27 #include "chrome/common/extensions/features/feature.h" | 27 #include "chrome/common/extensions/features/feature.h" |
| 28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/test/test_browser_thread.h" | 29 #include "content/public/test/test_browser_thread.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 31 |
| 32 using content::BrowserThread; | 32 using content::BrowserThread; |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 const char kTestHostId[] = "knldjmfmopnpolahpmmgbagdohdnhkik"; |
| 36 const char kTestMessage[] = "{\"text\": \"Hello.\"}"; | 37 const char kTestMessage[] = "{\"text\": \"Hello.\"}"; |
| 37 | 38 |
| 38 base::FilePath GetTestDir() { | 39 base::FilePath GetTestDir() { |
| 39 base::FilePath test_dir; | 40 base::FilePath test_dir; |
| 40 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); | 41 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); |
| 41 test_dir = test_dir.AppendASCII("native_messaging"); | 42 test_dir = test_dir.AppendASCII("native_messaging"); |
| 42 return test_dir; | 43 return test_dir; |
| 43 } | 44 } |
| 44 | 45 |
| 45 } // namespace | 46 } // namespace |
| 46 | 47 |
| 47 namespace extensions { | 48 namespace extensions { |
| 48 | 49 |
| 49 class FakeLauncher : public NativeProcessLauncher { | 50 class FakeLauncher : public NativeProcessLauncher { |
| 50 public: | 51 public: |
| 51 FakeLauncher(base::FilePath read_file, base::FilePath write_file) { | 52 FakeLauncher(base::FilePath read_file, base::FilePath write_file) { |
| 52 read_file_ = base::CreatePlatformFile( | 53 read_file_ = base::CreatePlatformFile( |
| 53 read_file, | 54 read_file, |
| 54 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | | 55 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | |
| 55 base::PLATFORM_FILE_ASYNC, | 56 base::PLATFORM_FILE_ASYNC, |
| 56 NULL, NULL); | 57 NULL, NULL); |
| 57 write_file_ = base::CreatePlatformFile( | 58 write_file_ = base::CreatePlatformFile( |
| 58 write_file, | 59 write_file, |
| 59 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE | | 60 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE | |
| 60 base::PLATFORM_FILE_ASYNC, | 61 base::PLATFORM_FILE_ASYNC, |
| 61 NULL, NULL); | 62 NULL, NULL); |
| 62 } | 63 } |
| 63 | 64 |
| 64 virtual void Launch(const std::string& native_host_name, | 65 virtual void Launch(const GURL& origin, |
| 66 const std::string& native_host_name, |
| 65 LaunchedCallback callback) const OVERRIDE { | 67 LaunchedCallback callback) const OVERRIDE { |
| 66 callback.Run(base::GetCurrentProcessHandle(), read_file_, write_file_); | 68 callback.Run(base::GetCurrentProcessHandle(), read_file_, write_file_); |
| 67 } | 69 } |
| 68 | 70 |
| 69 private: | 71 private: |
| 70 base::PlatformFile read_file_; | 72 base::PlatformFile read_file_; |
| 71 base::PlatformFile write_file_; | 73 base::PlatformFile write_file_; |
| 72 }; | 74 }; |
| 73 | 75 |
| 74 class NativeMessagingTest : public ::testing::Test, | 76 class NativeMessagingTest : public ::testing::Test, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 }; | 146 }; |
| 145 | 147 |
| 146 // Read a single message from a local file. | 148 // Read a single message from a local file. |
| 147 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 149 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| 148 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 150 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 149 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); | 151 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
| 150 | 152 |
| 151 scoped_ptr<NativeProcessLauncher> launcher( | 153 scoped_ptr<NativeProcessLauncher> launcher( |
| 152 new FakeLauncher(temp_input_file, temp_output_file)); | 154 new FakeLauncher(temp_input_file, temp_output_file)); |
| 153 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( | 155 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
| 154 AsWeakPtr(), "empty_app.py", 0, launcher.Pass()); | 156 AsWeakPtr(), kTestHostId, "empty_app.py", 0, launcher.Pass()); |
| 155 ASSERT_TRUE(native_message_process_host_.get()); | 157 ASSERT_TRUE(native_message_process_host_.get()); |
| 156 message_loop_.RunUntilIdle(); | 158 message_loop_.RunUntilIdle(); |
| 157 | 159 |
| 158 native_message_process_host_->ReadNowForTesting(); | 160 native_message_process_host_->ReadNowForTesting(); |
| 159 read_message_run_loop_.Run(); | 161 read_message_run_loop_.Run(); |
| 160 EXPECT_EQ(kTestMessage, last_posted_message_); | 162 EXPECT_EQ(kTestMessage, last_posted_message_); |
| 161 } | 163 } |
| 162 | 164 |
| 163 // Tests sending a single message. The message should get written to | 165 // Tests sending a single message. The message should get written to |
| 164 // |temp_file| and should match the contents of single_message_request.msg. | 166 // |temp_file| and should match the contents of single_message_request.msg. |
| 165 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 167 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| 166 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 168 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 167 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); | 169 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); |
| 168 | 170 |
| 169 scoped_ptr<NativeProcessLauncher> launcher( | 171 scoped_ptr<NativeProcessLauncher> launcher( |
| 170 new FakeLauncher(temp_input_file, temp_output_file)); | 172 new FakeLauncher(temp_input_file, temp_output_file)); |
| 171 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( | 173 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
| 172 AsWeakPtr(), "empty_app.py", 0, launcher.Pass()); | 174 AsWeakPtr(), kTestHostId, "empty_app.py", 0, launcher.Pass()); |
| 173 ASSERT_TRUE(native_message_process_host_.get()); | 175 ASSERT_TRUE(native_message_process_host_.get()); |
| 174 message_loop_.RunUntilIdle(); | 176 message_loop_.RunUntilIdle(); |
| 175 | 177 |
| 176 native_message_process_host_->Send(kTestMessage); | 178 native_message_process_host_->Send(kTestMessage); |
| 177 message_loop_.RunUntilIdle(); | 179 message_loop_.RunUntilIdle(); |
| 178 | 180 |
| 179 std::string output; | 181 std::string output; |
| 180 base::TimeTicks start_time = base::TimeTicks::Now(); | 182 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 181 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { | 183 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { |
| 182 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); | 184 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); |
| 183 if (!output.empty()) | 185 if (!output.empty()) |
| 184 break; | 186 break; |
| 185 base::PlatformThread::YieldCurrentThread(); | 187 base::PlatformThread::YieldCurrentThread(); |
| 186 } | 188 } |
| 187 | 189 |
| 188 EXPECT_EQ(FormatMessage(kTestMessage), output); | 190 EXPECT_EQ(FormatMessage(kTestMessage), output); |
| 189 } | 191 } |
| 190 | 192 |
| 191 // Disabled, see http://crbug.com/159754. | 193 // Disabled, see http://crbug.com/159754. |
| 192 // Test send message with a real client. The client just echo's back the text | 194 // Test send message with a real client. The client just echo's back the text |
| 193 // it recieved. | 195 // it recieved. |
| 194 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) { | 196 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) { |
| 195 native_message_process_host_ = NativeMessageProcessHost::Create( | 197 native_message_process_host_ = NativeMessageProcessHost::Create( |
| 196 AsWeakPtr(), "empty_app.py", 0); | 198 AsWeakPtr(), kTestHostId, "empty_app.py", 0); |
| 197 ASSERT_TRUE(native_message_process_host_.get()); | 199 ASSERT_TRUE(native_message_process_host_.get()); |
| 198 message_loop_.RunUntilIdle(); | 200 message_loop_.RunUntilIdle(); |
| 199 | 201 |
| 200 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); | 202 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); |
| 201 read_message_run_loop_.Run(); | 203 read_message_run_loop_.Run(); |
| 202 EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}", | 204 EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}", |
| 203 last_posted_message_); | 205 last_posted_message_); |
| 204 | 206 |
| 205 native_message_process_host_->Send("{\"foo\": \"bar\"}"); | 207 native_message_process_host_->Send("{\"foo\": \"bar\"}"); |
| 206 read_message_run_loop_.Run(); | 208 read_message_run_loop_.Run(); |
| 207 EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_); | 209 EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_); |
| 208 } | 210 } |
| 209 | 211 |
| 210 } // namespace extensions | 212 } // namespace extensions |
| OLD | NEW |