Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc

Issue 12374054: Update and enable NativeMessagingTest.EchoConnect. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/extensions/api/messaging/native_messaging_apitest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/platform_file.h" 14 #include "base/platform_file.h"
15 #include "base/process_util.h" 15 #include "base/process_util.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/stringprintf.h"
17 #include "base/test/test_timeouts.h" 18 #include "base/test/test_timeouts.h"
18 #include "base/threading/platform_thread.h" 19 #include "base/threading/platform_thread.h"
19 #include "base/threading/sequenced_worker_pool.h" 20 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/time.h" 21 #include "base/time.h"
21 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" 22 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
23 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h"
22 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" 24 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
23 #include "chrome/common/chrome_paths.h" 25 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/chrome_version_info.h" 27 #include "chrome/common/chrome_version_info.h"
26 #include "chrome/common/extensions/extension.h" 28 #include "chrome/common/extensions/extension.h"
27 #include "chrome/common/extensions/features/feature.h" 29 #include "chrome/common/extensions/features/feature.h"
28 #include "content/public/browser/browser_thread.h" 30 #include "content/public/browser/browser_thread.h"
29 #include "content/public/test/test_browser_thread.h" 31 #include "content/public/test/test_browser_thread.h"
30 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
31 33
32 using content::BrowserThread; 34 using content::BrowserThread;
33 35
34 namespace { 36 namespace {
35 37
36 const char kTestHostId[] = "knldjmfmopnpolahpmmgbagdohdnhkik";
37 const char kTestMessage[] = "{\"text\": \"Hello.\"}"; 38 const char kTestMessage[] = "{\"text\": \"Hello.\"}";
38 39
39 base::FilePath GetTestDir() { 40 base::FilePath GetTestDir() {
40 base::FilePath test_dir; 41 base::FilePath test_dir;
41 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); 42 PathService::Get(chrome::DIR_TEST_DATA, &test_dir);
42 test_dir = test_dir.AppendASCII("native_messaging"); 43 test_dir = test_dir.AppendASCII("native_messaging");
43 return test_dir; 44 return test_dir;
44 } 45 }
45 46
46 } // namespace 47 } // namespace
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, 104 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
104 native_message_process_host_.release()); 105 native_message_process_host_.release());
105 } 106 }
106 message_loop_.RunUntilIdle(); 107 message_loop_.RunUntilIdle();
107 } 108 }
108 109
109 virtual void PostMessageFromNativeProcess( 110 virtual void PostMessageFromNativeProcess(
110 int port_id, 111 int port_id,
111 const std::string& message) OVERRIDE { 112 const std::string& message) OVERRIDE {
112 last_posted_message_ = message; 113 last_posted_message_ = message;
113 read_message_run_loop_.Quit(); 114 if (read_message_run_loop_)
115 read_message_run_loop_->Quit();
114 } 116 }
115 117
116 virtual void CloseChannel(int port_id, bool error) OVERRIDE { 118 virtual void CloseChannel(int port_id, bool error) OVERRIDE {
117 } 119 }
118 120
119 protected: 121 protected:
120 std::string FormatMessage(const std::string& message) { 122 std::string FormatMessage(const std::string& message) {
121 Pickle pickle; 123 Pickle pickle;
122 pickle.WriteString(message); 124 pickle.WriteString(message);
123 return std::string(const_cast<const Pickle*>(&pickle)->payload(), 125 return std::string(const_cast<const Pickle*>(&pickle)->payload(),
124 pickle.payload_size()); 126 pickle.payload_size());
125 } 127 }
126 128
127 base::FilePath CreateTempFileWithMessage(const std::string& message) { 129 base::FilePath CreateTempFileWithMessage(const std::string& message) {
128 base::FilePath filename = temp_dir_.path().AppendASCII("input"); 130 base::FilePath filename = temp_dir_.path().AppendASCII("input");
129 file_util::CreateTemporaryFile(&filename); 131 file_util::CreateTemporaryFile(&filename);
130 std::string message_with_header = FormatMessage(message); 132 std::string message_with_header = FormatMessage(message);
131 EXPECT_TRUE(file_util::WriteFile( 133 EXPECT_TRUE(file_util::WriteFile(
132 filename, message_with_header.data(), message_with_header.size())); 134 filename, message_with_header.data(), message_with_header.size()));
133 return filename; 135 return filename;
134 } 136 }
135 137
136 // Force the channel to be dev. 138 // Force the channel to be dev.
137 base::ScopedTempDir temp_dir_; 139 base::ScopedTempDir temp_dir_;
138 Feature::ScopedCurrentChannel current_channel_; 140 Feature::ScopedCurrentChannel current_channel_;
139 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; 141 scoped_ptr<NativeMessageProcessHost> native_message_process_host_;
140 base::FilePath user_data_dir_; 142 base::FilePath user_data_dir_;
141 MessageLoopForIO message_loop_; 143 MessageLoopForIO message_loop_;
142 base::RunLoop read_message_run_loop_; 144 scoped_ptr<base::RunLoop> read_message_run_loop_;
143 scoped_ptr<content::TestBrowserThread> ui_thread_; 145 scoped_ptr<content::TestBrowserThread> ui_thread_;
144 scoped_ptr<content::TestBrowserThread> io_thread_; 146 scoped_ptr<content::TestBrowserThread> io_thread_;
145 std::string last_posted_message_; 147 std::string last_posted_message_;
146 }; 148 };
147 149
148 // Read a single message from a local file. 150 // Read a single message from a local file.
149 TEST_F(NativeMessagingTest, SingleSendMessageRead) { 151 TEST_F(NativeMessagingTest, SingleSendMessageRead) {
150 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); 152 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output");
151 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); 153 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage);
152 154
153 scoped_ptr<NativeProcessLauncher> launcher( 155 scoped_ptr<NativeProcessLauncher> launcher(
154 new FakeLauncher(temp_input_file, temp_output_file)); 156 new FakeLauncher(temp_input_file, temp_output_file));
155 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( 157 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher(
156 AsWeakPtr(), kTestHostId, "empty_app.py", 0, launcher.Pass()); 158 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py",
159 0, launcher.Pass());
157 ASSERT_TRUE(native_message_process_host_.get()); 160 ASSERT_TRUE(native_message_process_host_.get());
158 message_loop_.RunUntilIdle(); 161 read_message_run_loop_.reset(new base::RunLoop());
162 read_message_run_loop_->RunUntilIdle();
159 163
160 native_message_process_host_->ReadNowForTesting(); 164 if (last_posted_message_.empty()) {
161 read_message_run_loop_.Run(); 165 read_message_run_loop_.reset(new base::RunLoop());
166 native_message_process_host_->ReadNowForTesting();
167 read_message_run_loop_->Run();
168 }
162 EXPECT_EQ(kTestMessage, last_posted_message_); 169 EXPECT_EQ(kTestMessage, last_posted_message_);
163 } 170 }
164 171
165 // Tests sending a single message. The message should get written to 172 // Tests sending a single message. The message should get written to
166 // |temp_file| and should match the contents of single_message_request.msg. 173 // |temp_file| and should match the contents of single_message_request.msg.
167 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { 174 TEST_F(NativeMessagingTest, SingleSendMessageWrite) {
168 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); 175 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output");
169 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); 176 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string());
170 177
171 scoped_ptr<NativeProcessLauncher> launcher( 178 scoped_ptr<NativeProcessLauncher> launcher(
172 new FakeLauncher(temp_input_file, temp_output_file)); 179 new FakeLauncher(temp_input_file, temp_output_file));
173 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( 180 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher(
174 AsWeakPtr(), kTestHostId, "empty_app.py", 0, launcher.Pass()); 181 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py",
182 0, launcher.Pass());
175 ASSERT_TRUE(native_message_process_host_.get()); 183 ASSERT_TRUE(native_message_process_host_.get());
176 message_loop_.RunUntilIdle(); 184 message_loop_.RunUntilIdle();
177 185
178 native_message_process_host_->Send(kTestMessage); 186 native_message_process_host_->Send(kTestMessage);
179 message_loop_.RunUntilIdle(); 187 message_loop_.RunUntilIdle();
180 188
181 std::string output; 189 std::string output;
182 base::TimeTicks start_time = base::TimeTicks::Now(); 190 base::TimeTicks start_time = base::TimeTicks::Now();
183 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { 191 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) {
184 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); 192 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output));
185 if (!output.empty()) 193 if (!output.empty())
186 break; 194 break;
187 base::PlatformThread::YieldCurrentThread(); 195 base::PlatformThread::YieldCurrentThread();
188 } 196 }
189 197
190 EXPECT_EQ(FormatMessage(kTestMessage), output); 198 EXPECT_EQ(FormatMessage(kTestMessage), output);
191 } 199 }
192 200
193 // Disabled, see http://crbug.com/159754.
194 // Test send message with a real client. The client just echo's back the text 201 // Test send message with a real client. The client just echo's back the text
195 // it recieved. 202 // it recieved.
196 TEST_F(NativeMessagingTest, DISABLED_EchoConnect) { 203 TEST_F(NativeMessagingTest, EchoConnect) {
204 base::ScopedTempDir temp_dir;
205 base::FilePath manifest_path = temp_dir.path().AppendASCII(
206 std::string(kTestNativeMessagingHostName) + ".json");
207 ASSERT_NO_FATAL_FAILURE(CreateTestNativeHostManifest(manifest_path));
208
209 std::string hosts_option = base::StringPrintf(
210 "%s=%s", extensions::kTestNativeMessagingHostName,
211 manifest_path.AsUTF8Unsafe().c_str());
212 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
213 switches::kNativeMessagingHosts, hosts_option);
214
197 native_message_process_host_ = NativeMessageProcessHost::Create( 215 native_message_process_host_ = NativeMessageProcessHost::Create(
198 AsWeakPtr(), kTestHostId, "empty_app.py", 0); 216 AsWeakPtr(), kTestNativeMessagingExtensionId,
217 kTestNativeMessagingHostName, 0);
199 ASSERT_TRUE(native_message_process_host_.get()); 218 ASSERT_TRUE(native_message_process_host_.get());
200 message_loop_.RunUntilIdle();
201 219
202 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); 220 native_message_process_host_->Send("{\"text\": \"Hello.\"}");
203 read_message_run_loop_.Run(); 221 read_message_run_loop_.reset(new base::RunLoop());
222 read_message_run_loop_->Run();
204 EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}", 223 EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}",
205 last_posted_message_); 224 last_posted_message_);
206 225
207 native_message_process_host_->Send("{\"foo\": \"bar\"}"); 226 native_message_process_host_->Send("{\"foo\": \"bar\"}");
208 read_message_run_loop_.Run(); 227 read_message_run_loop_.reset(new base::RunLoop());
228 read_message_run_loop_->Run();
209 EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_); 229 EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_);
210 } 230 }
211 231
212 } // namespace extensions 232 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/messaging/native_messaging_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698