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

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

Issue 10918255: The Windows portion of Native Messagaing (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Windows is Ready Created 8 years, 1 month 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
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_path.h" 7 #include "base/file_path.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/platform_file.h" 13 #include "base/platform_file.h"
14 #include "base/threading/platform_thread.h"
14 #include "base/threading/sequenced_worker_pool.h" 15 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" 16 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
16 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" 17 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
17 #include "chrome/common/chrome_paths.h" 18 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/chrome_version_info.h" 20 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/extensions/features/feature.h" 21 #include "chrome/common/extensions/features/feature.h"
21 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 using content::BrowserThread; 26 using content::BrowserThread;
26 27
27 namespace { 28 namespace {
28 29
30 const int kMaxNapTimes = 3;
31 const int kSleepTimeMS = 50;
32
33 #if defined(OS_WIN)
34 const char kEmptyAppName[] = "empty_app.bat";
35 const char kEchoAppName[] = "echo.bat";
36 #else
37 const char kEmptyAppName[] = "empty_app.py";
38 const char kEchoAppName[] = "echo.py";
39 #endif // defined(OS_WIN)
40
29 FilePath GetTestDir() { 41 FilePath GetTestDir() {
30 FilePath test_dir; 42 FilePath test_dir;
31 PathService::Get(chrome::DIR_TEST_DATA, &test_dir); 43 PathService::Get(chrome::DIR_TEST_DATA, &test_dir);
32 test_dir = test_dir.AppendASCII("native_messaging"); 44 test_dir = test_dir.AppendASCII("native_messaging");
33 return test_dir; 45 return test_dir;
34 } 46 }
35 47
36 } // namespace 48 } // namespace
37 49
38 namespace extensions { 50 namespace extensions {
39 51
40 class FakeLauncher : public NativeProcessLauncher { 52 class FakeLauncher : public NativeProcessLauncher {
41 public: 53 public:
42 FakeLauncher(FilePath read_file, FilePath write_file) { 54 FakeLauncher(FilePath read_file, FilePath write_file) {
55 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_ASYNC;
43 read_file_ = base::CreatePlatformFile( 56 read_file_ = base::CreatePlatformFile(
44 read_file, 57 read_file,
45 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, 58 flags | base::PLATFORM_FILE_READ,
46 NULL, NULL); 59 NULL, NULL);
47 write_file_ = base::CreatePlatformFile( 60 write_file_ = base::CreatePlatformFile(
48 write_file, 61 write_file,
49 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, 62 flags | base::PLATFORM_FILE_WRITE,
50 NULL, NULL); 63 NULL, NULL);
51 } 64 }
52 65
53 virtual bool LaunchNativeProcess( 66 virtual bool LaunchNativeProcess(
54 const FilePath& path, 67 const FilePath& path,
55 base::ProcessHandle* native_process_handle, 68 base::ProcessHandle* native_process_handle,
56 NativeMessageProcessHost::FileHandle* read_file, 69 NativeMessageProcessHost::FileHandle* read_file,
57 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE { 70 NativeMessageProcessHost::FileHandle* write_file) const OVERRIDE {
58 *native_process_handle = base::kNullProcessHandle; 71 *native_process_handle = base::kNullProcessHandle;
59 *read_file = read_file_; 72 *read_file = read_file_;
60 *write_file = write_file_; 73 *write_file = write_file_;
61 return true; 74 return true;
62 } 75 }
63 76
64 private: 77 private:
65 base::PlatformFile read_file_; 78 base::PlatformFile read_file_;
66 base::PlatformFile write_file_; 79 base::PlatformFile write_file_;
67 }; 80 };
68 81
69 class NativeMessagingTest : public ::testing::Test, 82 class NativeMessagingTest : public ::testing::Test,
70 public NativeMessageProcessHost::Client, 83 public NativeMessageProcessHost::Client,
71 public base::SupportsWeakPtr<NativeMessagingTest> { 84 public base::SupportsWeakPtr<NativeMessagingTest> {
72 public: 85 public:
73 NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV) { 86 NativeMessagingTest() : current_channel_(chrome::VersionInfo::CHANNEL_DEV),
87 got_message_(false) {
74 } 88 }
75 89
76 virtual void SetUp() { 90 virtual void SetUp() {
77 CommandLine::ForCurrentProcess()->AppendSwitch( 91 CommandLine::ForCurrentProcess()->AppendSwitch(
78 switches::kEnableNativeMessaging); 92 switches::kEnableNativeMessaging);
79 // Change the user data dir so native apps will be looked for in the test 93 // Change the user data dir so native apps will be looked for in the test
80 // directory. 94 // directory.
81 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); 95 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_));
82 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); 96 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir()));
83 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, 97 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI,
84 &message_loop_)); 98 &message_loop_));
85 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE, 99 file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE,
86 &message_loop_)); 100 &message_loop_));
101 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO,
102 &message_loop_));
87 } 103 }
88 104
89 virtual void TearDown() { 105 virtual void TearDown() {
90 // Change the user data dir back for other tests. 106 // Change the user data dir back for other tests.
91 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); 107 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_));
92 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE,
93 native_message_process_host_);
94 message_loop_.RunAllPending();
95 } 108 }
96 109
97 void PostMessageFromNativeProcess(int port_id, const std::string& message) { 110 void PostMessageFromNativeProcess(int port_id, const std::string& message) {
98 last_posted_message_ = message; 111 last_posted_message_ = message;
112 got_message_ = true;
99 } 113 }
100 114
101 void CloseChannel(int port_id, bool error) { 115 void CloseChannel(int port_id, bool error) {
102 } 116 }
103 117
104 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) { 118 void AcquireProcess(NativeMessageProcessHost::ScopedHost process) {
105 native_message_process_host_ = process.release(); 119 native_message_process_host_.swap(process);
120 }
121
122 void WaitForMessage() {
123 got_message_ = false;
124 native_message_process_host_->ReadNowForTesting();
125 message_loop_.RunAllPending();
126
127 for (int nap_times = 0; nap_times < kMaxNapTimes && !got_message_;
Matt Perry 2012/10/24 23:40:31 Please don't use polling in tests. It results in f
eaugusti 2012/10/30 22:03:12 Done.
128 ++nap_times) {
129 base::PlatformThread::Sleep(
130 base::TimeDelta::FromMilliseconds(kSleepTimeMS));
131 native_message_process_host_->ReadNowForTesting();
132 message_loop_.RunAllPending();
133 }
134
135 ASSERT_TRUE(got_message_);
106 } 136 }
107 137
108 protected: 138 protected:
109 // Force the channel to be dev. 139 // Force the channel to be dev.
110 Feature::ScopedCurrentChannel current_channel_; 140 Feature::ScopedCurrentChannel current_channel_;
111 NativeMessageProcessHost* native_message_process_host_; 141 NativeMessageProcessHost::ScopedHost native_message_process_host_;
112 FilePath user_data_dir_; 142 FilePath user_data_dir_;
113 MessageLoopForIO message_loop_; 143 MessageLoopForIO message_loop_;
114 scoped_ptr<content::TestBrowserThread> ui_thread_; 144 scoped_ptr<content::TestBrowserThread> ui_thread_;
115 scoped_ptr<content::TestBrowserThread> file_thread_; 145 scoped_ptr<content::TestBrowserThread> file_thread_;
146 scoped_ptr<content::TestBrowserThread> io_thread_;
116 std::string last_posted_message_; 147 std::string last_posted_message_;
148 bool got_message_;
117 }; 149 };
118 150
119 // Read a single message from a local file (single_message_response.msg). 151 // Read a single message from a local file (single_message_response.msg).
120 TEST_F(NativeMessagingTest, SingleSendMessageRead) { 152 TEST_F(NativeMessagingTest, SingleSendMessageRead) {
121 FilePath temp_file; 153 FilePath temp_file;
122 file_util::CreateTemporaryFile(&temp_file); 154 file_util::CreateTemporaryFile(&temp_file);
123 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"),
124 temp_file);
125 NativeMessageProcessHost::CreateWithLauncher( 155 NativeMessageProcessHost::CreateWithLauncher(
126 AsWeakPtr(), "empty_app.py", "{}", 0, 156 AsWeakPtr(), kEmptyAppName, "{}", 0,
127 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( 157 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind(
128 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), 158 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
129 launcher); 159 scoped_ptr<NativeProcessLauncher>(new FakeLauncher(
160 GetTestDir().AppendASCII("single_message_response.msg"), temp_file)));
130 message_loop_.RunAllPending(); 161 message_loop_.RunAllPending();
131 ASSERT_TRUE(native_message_process_host_); 162 ASSERT_TRUE(native_message_process_host_.get());
163 // The process host is directly connected to files, there is no need to wait
164 // for the message.
132 native_message_process_host_->ReadNowForTesting(); 165 native_message_process_host_->ReadNowForTesting();
133 message_loop_.RunAllPending(); 166 message_loop_.RunAllPending();
134 EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}"); 167 EXPECT_EQ(last_posted_message_, "{\"text\": \"Hi There!.\"}");
135 file_util::Delete(temp_file, false /* non-recursive */); 168 file_util::Delete(temp_file, false /* non-recursive */);
136 } 169 }
137 170
138 // Tests sending a single message. The message should get written to 171 // Tests sending a single message. The message should get written to
139 // |temp_file| and should match the contents of single_message_request.msg. 172 // |temp_file| and should match the contents of single_message_request.msg.
140 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { 173 TEST_F(NativeMessagingTest, SingleSendMessageWrite) {
141 FilePath temp_file; 174 FilePath temp_file;
142 file_util::CreateTemporaryFile(&temp_file); 175 file_util::CreateTemporaryFile(&temp_file);
143 FakeLauncher launcher(GetTestDir().AppendASCII("single_message_response.msg"),
144 temp_file);
145 NativeMessageProcessHost::CreateWithLauncher( 176 NativeMessageProcessHost::CreateWithLauncher(
146 AsWeakPtr(), "empty_app.py", "{\"text\": \"Hello.\"}", 0, 177 AsWeakPtr(), kEmptyAppName, "{\"text\": \"Hello.\"}", 0,
147 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind( 178 NativeMessageProcessHost::TYPE_SEND_MESSAGE_REQUEST, base::Bind(
148 &NativeMessagingTest::AcquireProcess, AsWeakPtr()), 179 &NativeMessagingTest::AcquireProcess, AsWeakPtr()),
149 launcher); 180 scoped_ptr<NativeProcessLauncher>(new FakeLauncher(
181 GetTestDir().AppendASCII("single_message_response.msg"), temp_file)));
150 message_loop_.RunAllPending(); 182 message_loop_.RunAllPending();
151 ASSERT_TRUE(native_message_process_host_); 183 ASSERT_TRUE(native_message_process_host_.get());
152
153 EXPECT_TRUE(file_util::ContentsEqual( 184 EXPECT_TRUE(file_util::ContentsEqual(
154 temp_file, GetTestDir().AppendASCII("single_message_request.msg"))); 185 temp_file, GetTestDir().AppendASCII("single_message_request.msg")));
155
156 file_util::Delete(temp_file, false /* non-recursive */); 186 file_util::Delete(temp_file, false /* non-recursive */);
157 } 187 }
158 188
159 // Test send message with a real client. The client just echo's back the text 189 // Test send message with a real client. The client just echo's back the text
160 // it recieved. 190 // it recieved.
161 TEST_F(NativeMessagingTest, EchoConnect) { 191 TEST_F(NativeMessagingTest, EchoConnect) {
162 NativeMessageProcessHost::Create( 192 NativeMessageProcessHost::Create(
163 AsWeakPtr(), "echo.py", "{\"text\": \"Hello.\"}", 0, 193 AsWeakPtr(), kEchoAppName, "{\"text\": \"Hello.\"}", 0,
164 NativeMessageProcessHost::TYPE_CONNECT, base::Bind( 194 NativeMessageProcessHost::TYPE_CONNECT, base::Bind(
165 &NativeMessagingTest::AcquireProcess, AsWeakPtr())); 195 &NativeMessagingTest::AcquireProcess, AsWeakPtr()));
166 message_loop_.RunAllPending(); 196 message_loop_.RunAllPending();
167 ASSERT_TRUE(native_message_process_host_); 197 ASSERT_TRUE(native_message_process_host_.get());
168 198 WaitForMessage();
169 native_message_process_host_->ReadNowForTesting();
170 message_loop_.RunAllPending();
171 EXPECT_EQ(last_posted_message_, 199 EXPECT_EQ(last_posted_message_,
172 "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}"); 200 "{\"id\": 1, \"echo\": {\"text\": \"Hello.\"}}");
173
174 native_message_process_host_->Send("{\"foo\": \"bar\"}"); 201 native_message_process_host_->Send("{\"foo\": \"bar\"}");
175 message_loop_.RunAllPending(); 202 WaitForMessage();
176 native_message_process_host_->ReadNowForTesting();
177 message_loop_.RunAllPending();
178 EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}"); 203 EXPECT_EQ(last_posted_message_, "{\"id\": 2, \"echo\": {\"foo\": \"bar\"}}");
179 } 204 }
180 205
181 } // namespace extensions 206 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698