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

Unified Diff: chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc

Issue 12406002: Pass ID of the calling extension to the native messaging host. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/extensions/api/messaging/native_process_launcher.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
index cc8192bd7dda3b4bf20306ea0e41d97b9d382f63..d95ba5d3855332cab5f8dbc6729af2ed39340d93 100644
--- a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
+++ b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc
@@ -7,6 +7,7 @@
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
+#include "base/json/json_reader.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
@@ -110,7 +111,19 @@ class NativeMessagingTest : public ::testing::Test,
virtual void PostMessageFromNativeProcess(
int port_id,
const std::string& message) OVERRIDE {
- last_posted_message_ = message;
+ last_message_ = message;
+
+ // Parse the message.
+ base::Value* parsed = base::JSONReader::Read(message);
+ base::DictionaryValue* dict_value;
+ if (parsed && parsed->GetAsDictionary(&dict_value)) {
+ last_message_parsed_.reset(dict_value);
+ } else {
+ LOG(ERROR) << "Failed to parse " << message;
+ last_message_parsed_.reset();
+ delete parsed;
+ }
+
if (read_message_run_loop_)
read_message_run_loop_->Quit();
}
@@ -144,7 +157,8 @@ class NativeMessagingTest : public ::testing::Test,
scoped_ptr<base::RunLoop> read_message_run_loop_;
scoped_ptr<content::TestBrowserThread> ui_thread_;
scoped_ptr<content::TestBrowserThread> io_thread_;
- std::string last_posted_message_;
+ std::string last_message_;
+ scoped_ptr<base::DictionaryValue> last_message_parsed_;
};
// Read a single message from a local file.
@@ -161,12 +175,12 @@ TEST_F(NativeMessagingTest, SingleSendMessageRead) {
read_message_run_loop_.reset(new base::RunLoop());
read_message_run_loop_->RunUntilIdle();
- if (last_posted_message_.empty()) {
+ if (last_message_.empty()) {
read_message_run_loop_.reset(new base::RunLoop());
native_message_process_host_->ReadNowForTesting();
read_message_run_loop_->Run();
}
- EXPECT_EQ(kTestMessage, last_posted_message_);
+ EXPECT_EQ(kTestMessage, last_message_);
}
// Tests sending a single message. The message should get written to
@@ -199,7 +213,7 @@ TEST_F(NativeMessagingTest, SingleSendMessageWrite) {
}
// Test send message with a real client. The client just echo's back the text
-// it recieved.
+// it received.
TEST_F(NativeMessagingTest, EchoConnect) {
base::ScopedTempDir temp_dir;
base::FilePath manifest_path = temp_dir.path().AppendASCII(
@@ -220,13 +234,31 @@ TEST_F(NativeMessagingTest, EchoConnect) {
native_message_process_host_->Send("{\"text\": \"Hello.\"}");
read_message_run_loop_.reset(new base::RunLoop());
read_message_run_loop_->Run();
- EXPECT_EQ("{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}",
- last_posted_message_);
+ ASSERT_FALSE(last_message_.empty());
+ ASSERT_TRUE(last_message_parsed_);
+
+ std::string expected_url = std::string("chrome-extension://") +
+ kTestNativeMessagingExtensionId + "/";
+ int id;
+ EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id));
+ EXPECT_EQ(1, id);
+ std::string text;
+ EXPECT_TRUE(last_message_parsed_->GetString("echo.text", &text));
+ EXPECT_EQ("Hello.", text);
+ std::string url;
+ EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url));
+ EXPECT_EQ(expected_url, url);
+
native_message_process_host_->Send("{\"foo\": \"bar\"}");
read_message_run_loop_.reset(new base::RunLoop());
read_message_run_loop_->Run();
- EXPECT_EQ("{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}", last_posted_message_);
+ EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id));
+ EXPECT_EQ(2, id);
+ EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text));
+ EXPECT_EQ("bar", text);
+ EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url));
+ EXPECT_EQ(expected_url, url);
}
} // namespace extensions
« no previous file with comments | « no previous file | chrome/browser/extensions/api/messaging/native_process_launcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698