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

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

Issue 10818013: Native Messaging! (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: MultiPlatform and UnitTest! Created 8 years, 4 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/file_path.h"
7 #include "base/file_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/platform_file.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using content::BrowserThread;
22
23 namespace {
24
25 FilePath GetTestDir() {
26 FilePath test_dir;
27 PathService::Get(chrome::DIR_TEST_DATA, &test_dir);
28 test_dir = test_dir.AppendASCII("native_messaging");
29 return test_dir;
30 }
31
32 } // namespace
33
34 namespace extensions {
35
36 class FakeLauncher : public NativeProcessLauncher {
37 public:
38 FakeLauncher(FilePath read_file, FilePath write_file) {
39 read_file_ = base::CreatePlatformFile(
40 read_file,
41 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
42 NULL, NULL);
43 write_file_ = base::CreatePlatformFile(
44 write_file,
45 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
46 NULL, NULL);
47 }
48
49 virtual bool LaunchNativeProcess(
50 const FilePath& path,
51 base::ProcessHandle* native_process_handle,
52 NativeMessageProcessHost::FileHandle* read_file,
53 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE {
54 *native_process_handle = base::kNullProcessHandle;
55 *read_file = read_file_;
56 *write_file = write_file_;
57 return true;
58 }
59
60 private:
61 base::PlatformFile read_file_;
62 base::PlatformFile write_file_;
63 };
64
65 class NativeMessagingTest : public ::testing::Test,
66 public NativeMessageProcessHost::Client,
67 public base::SupportsWeakPtr<NativeMessagingTest> {
68 public:
69 virtual void SetUp() {
70 // Change the user data dir so native apps will be looked for in the test
71 // directory.
72 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_));
73 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir()));
74 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI,
75 &message_loop_));
76 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE,
77 &message_loop_));
78 }
79
80 virtual void TearDown() {
81 // Change the user data dir back for other tests.
82 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_));
83 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
84 native_message_process_host_);
85 message_loop_.RunAllPending();
86 }
87
88 void PostMessageFromNativeProcess(int port_id, const std::string& message) {
89 last_posted_message_ = message;
90 }
91
92 void AcquireProcess(NativeMessageProcessHost* process) {
93 native_message_process_host_ = process;
94 }
95
96 protected:
97 NativeMessageProcessHost* native_message_process_host_;
98 FilePath user_data_dir_;
99 MessageLoopForIO message_loop_;
100 scoped_ptr<content::TestBrowserThread> ui_thread_;
101 scoped_ptr<content::TestBrowserThread> file_thread_;
102 std::string last_posted_message_;
103 };
104
105 TEST_F(NativeMessagingTest, SingleSendMessageRead) {
Matt Perry 2012/08/22 00:02:42 Can you add comments briefly explaining what these
eaugusti 2012/08/31 23:47:13 Done.
106 FilePath temp_file;
107 file_util::CreateTemporaryFile(&temp_file);
108 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"),
109 temp_file);
110 NativeMessageProcessHost::CreateWithLauncher(
111 AsWeakPtr(), "empty_app", "{}", 0,
112 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind(
113 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
114 launcher);
115 message_loop_.RunAllPending();
116 ASSERT_TRUE(native_message_process_host_);
117 native_message_process_host_->ReadNowForTesting();
118 message_loop_.RunAllPending();
119 ASSERT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}");
120 file_util::Delete(temp_file, false /* non-recursive */);
121 }
122
123 TEST_F(NativeMessagingTest, SingleSendMessageWrite) {
124 FilePath temp_file;
125 file_util::CreateTemporaryFile(&temp_file);
126 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"),
127 temp_file);
128 NativeMessageProcessHost::CreateWithLauncher(
129 AsWeakPtr(), "empty_app", "{\"text\": \"Hello.\"}", 0,
130 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind(
131 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
132 launcher);
133 message_loop_.RunAllPending();
134 ASSERT_TRUE(native_message_process_host_);
135
136 ASSERT_TRUE(file_util::ContentsEqual(
137 temp_file, GetTestDir().AppendASCII("single_message_request.msg")));
138
139 file_util::Delete(temp_file, false /* non-recursive */);
140 }
141
142
143 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698