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