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

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

Issue 16226004: Replace JSON (de)serialization of extension messages with direct Value pickling. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: disable test on win Created 7 years, 6 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
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/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); 102 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_));
103 if (native_message_process_host_.get()) { 103 if (native_message_process_host_.get()) {
104 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, 104 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
105 native_message_process_host_.release()); 105 native_message_process_host_.release());
106 } 106 }
107 message_loop_.RunUntilIdle(); 107 message_loop_.RunUntilIdle();
108 } 108 }
109 109
110 virtual void PostMessageFromNativeProcess( 110 virtual void PostMessageFromNativeProcess(
111 int port_id, 111 int port_id,
112 const std::string& message) OVERRIDE { 112 scoped_ptr<base::ListValue> message_as_list) OVERRIDE {
113 last_message_ = message; 113 // |message_as_list| should contain a single DictionaryValue. Extract it
114 114 // into |last_message_|.
115 // Parse the message. 115 ASSERT_EQ(1u, message_as_list->GetSize());
116 base::Value* parsed = base::JSONReader::Read(message); 116 base::Value* last_message_value = NULL;
117 base::DictionaryValue* dict_value; 117 message_as_list->Remove(0, &last_message_value);
118 if (parsed && parsed->GetAsDictionary(&dict_value)) { 118 ASSERT_EQ(base::Value::TYPE_DICTIONARY, last_message_value->GetType());
119 last_message_parsed_.reset(dict_value); 119 last_message_.reset(
120 } else { 120 static_cast<base::DictionaryValue*>(last_message_value));
121 LOG(ERROR) << "Failed to parse " << message;
122 last_message_parsed_.reset();
123 delete parsed;
124 }
125 121
126 if (read_message_run_loop_) 122 if (read_message_run_loop_)
127 read_message_run_loop_->Quit(); 123 read_message_run_loop_->Quit();
128 } 124 }
129 125
130 virtual void CloseChannel(int port_id, 126 virtual void CloseChannel(int port_id,
131 const std::string& error_message) OVERRIDE { 127 const std::string& error_message) OVERRIDE {
132 } 128 }
133 129
134 protected: 130 protected:
(...skipping 15 matching lines...) Expand all
150 146
151 // Force the channel to be dev. 147 // Force the channel to be dev.
152 base::ScopedTempDir temp_dir_; 148 base::ScopedTempDir temp_dir_;
153 Feature::ScopedCurrentChannel current_channel_; 149 Feature::ScopedCurrentChannel current_channel_;
154 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; 150 scoped_ptr<NativeMessageProcessHost> native_message_process_host_;
155 base::FilePath user_data_dir_; 151 base::FilePath user_data_dir_;
156 base::MessageLoopForIO message_loop_; 152 base::MessageLoopForIO message_loop_;
157 scoped_ptr<base::RunLoop> read_message_run_loop_; 153 scoped_ptr<base::RunLoop> read_message_run_loop_;
158 scoped_ptr<content::TestBrowserThread> ui_thread_; 154 scoped_ptr<content::TestBrowserThread> ui_thread_;
159 scoped_ptr<content::TestBrowserThread> io_thread_; 155 scoped_ptr<content::TestBrowserThread> io_thread_;
160 std::string last_message_; 156 scoped_ptr<DictionaryValue> last_message_;
161 scoped_ptr<base::DictionaryValue> last_message_parsed_;
162 }; 157 };
163 158
164 // Read a single message from a local file. 159 // Read a single message from a local file.
165 TEST_F(NativeMessagingTest, SingleSendMessageRead) { 160 TEST_F(NativeMessagingTest, SingleSendMessageRead) {
166 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); 161 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output");
167 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); 162 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage);
168 163
169 scoped_ptr<NativeProcessLauncher> launcher( 164 scoped_ptr<NativeProcessLauncher> launcher(
170 new FakeLauncher(temp_input_file, temp_output_file)); 165 new FakeLauncher(temp_input_file, temp_output_file));
171 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( 166 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher(
172 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", 167 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py",
173 0, launcher.Pass()); 168 0, launcher.Pass());
174 ASSERT_TRUE(native_message_process_host_.get()); 169 ASSERT_TRUE(native_message_process_host_.get());
175 read_message_run_loop_.reset(new base::RunLoop()); 170 read_message_run_loop_.reset(new base::RunLoop());
176 read_message_run_loop_->RunUntilIdle(); 171 read_message_run_loop_->RunUntilIdle();
177 172
178 if (last_message_.empty()) { 173 if (!last_message_) {
179 read_message_run_loop_.reset(new base::RunLoop()); 174 read_message_run_loop_.reset(new base::RunLoop());
180 native_message_process_host_->ReadNowForTesting(); 175 native_message_process_host_->ReadNowForTesting();
181 read_message_run_loop_->Run(); 176 read_message_run_loop_->Run();
182 } 177 }
183 EXPECT_EQ(kTestMessage, last_message_); 178 ASSERT_TRUE(last_message_);
179
180 scoped_ptr<base::Value> kTestMessageAsValue(
181 base::JSONReader::Read(kTestMessage));
182 ASSERT_TRUE(kTestMessageAsValue);
183 EXPECT_TRUE(base::Value::Equals(kTestMessageAsValue.get(),
184 last_message_.get()))
185 << "Expected " << *kTestMessageAsValue << " got " << *last_message_;
184 } 186 }
185 187
186 // Tests sending a single message. The message should get written to 188 // Tests sending a single message. The message should get written to
187 // |temp_file| and should match the contents of single_message_request.msg. 189 // |temp_file| and should match the contents of single_message_request.msg.
188 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { 190 //
191 // Disabled on windows, see http://crbug.com/245709.
192 #if defined(OS_WIN)
193 #define MAYBE_SingleSendMessageWrite DISABLED_SingleSendMessageWrite
194 #else
195 #define MAYBE_SingleSendMessageWrite SingleSendMessageWrite
196 #endif
197 TEST_F(NativeMessagingTest, MAYBE_SingleSendMessageWrite) {
189 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); 198 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output");
190 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); 199 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string());
Sergey Ulanov 2013/05/31 19:29:54 This writes a zero-size message to the input file.
not at google - send to devlin 2013/05/31 19:52:00 Done.
191 200
192 scoped_ptr<NativeProcessLauncher> launcher( 201 scoped_ptr<NativeProcessLauncher> launcher(
193 new FakeLauncher(temp_input_file, temp_output_file)); 202 new FakeLauncher(temp_input_file, temp_output_file));
194 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( 203 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher(
195 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", 204 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py",
196 0, launcher.Pass()); 205 0, launcher.Pass());
197 ASSERT_TRUE(native_message_process_host_.get()); 206 ASSERT_TRUE(native_message_process_host_.get());
198 message_loop_.RunUntilIdle(); 207 message_loop_.RunUntilIdle();
199 208
200 native_message_process_host_->Send(kTestMessage); 209 native_message_process_host_->Send(kTestMessage);
(...skipping 25 matching lines...) Expand all
226 switches::kNativeMessagingHosts, hosts_option); 235 switches::kNativeMessagingHosts, hosts_option);
227 236
228 native_message_process_host_ = NativeMessageProcessHost::Create( 237 native_message_process_host_ = NativeMessageProcessHost::Create(
229 AsWeakPtr(), kTestNativeMessagingExtensionId, 238 AsWeakPtr(), kTestNativeMessagingExtensionId,
230 kTestNativeMessagingHostName, 0); 239 kTestNativeMessagingHostName, 0);
231 ASSERT_TRUE(native_message_process_host_.get()); 240 ASSERT_TRUE(native_message_process_host_.get());
232 241
233 native_message_process_host_->Send("{\"text\": \"Hello.\"}"); 242 native_message_process_host_->Send("{\"text\": \"Hello.\"}");
234 read_message_run_loop_.reset(new base::RunLoop()); 243 read_message_run_loop_.reset(new base::RunLoop());
235 read_message_run_loop_->Run(); 244 read_message_run_loop_->Run();
236 ASSERT_FALSE(last_message_.empty()); 245 ASSERT_TRUE(last_message_);
237 ASSERT_TRUE(last_message_parsed_);
238 246
239 std::string expected_url = std::string("chrome-extension://") + 247 std::string expected_url = std::string("chrome-extension://") +
240 kTestNativeMessagingExtensionId + "/"; 248 kTestNativeMessagingExtensionId + "/";
241 int id; 249 int id;
242 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); 250 EXPECT_TRUE(last_message_->GetInteger("id", &id));
243 EXPECT_EQ(1, id); 251 EXPECT_EQ(1, id);
244 std::string text; 252 std::string text;
245 EXPECT_TRUE(last_message_parsed_->GetString("echo.text", &text)); 253 EXPECT_TRUE(last_message_->GetString("echo.text", &text));
246 EXPECT_EQ("Hello.", text); 254 EXPECT_EQ("Hello.", text);
247 std::string url; 255 std::string url;
248 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); 256 EXPECT_TRUE(last_message_->GetString("caller_url", &url));
249 EXPECT_EQ(expected_url, url); 257 EXPECT_EQ(expected_url, url);
250 258
251 259
252 native_message_process_host_->Send("{\"foo\": \"bar\"}"); 260 native_message_process_host_->Send("{\"foo\": \"bar\"}");
253 read_message_run_loop_.reset(new base::RunLoop()); 261 read_message_run_loop_.reset(new base::RunLoop());
254 read_message_run_loop_->Run(); 262 read_message_run_loop_->Run();
255 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); 263 EXPECT_TRUE(last_message_->GetInteger("id", &id));
256 EXPECT_EQ(2, id); 264 EXPECT_EQ(2, id);
257 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); 265 EXPECT_TRUE(last_message_->GetString("echo.foo", &text));
258 EXPECT_EQ("bar", text); 266 EXPECT_EQ("bar", text);
259 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); 267 EXPECT_TRUE(last_message_->GetString("caller_url", &url));
260 EXPECT_EQ(expected_url, url); 268 EXPECT_EQ(expected_url, url);
261 } 269 }
262 270
263 } // namespace extensions 271 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/messaging/native_message_process_host.cc ('k') | chrome/browser/extensions/message_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698