| OLD | NEW |
| 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 11 matching lines...) Expand all Loading... |
| 22 #include "base/time.h" | 22 #include "base/time.h" |
| 23 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 23 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 24 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" | 24 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" |
| 25 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 25 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 26 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
| 27 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/common/chrome_version_info.h" | 28 #include "chrome/common/chrome_version_info.h" |
| 29 #include "chrome/common/extensions/extension.h" | 29 #include "chrome/common/extensions/extension.h" |
| 30 #include "chrome/common/extensions/features/feature.h" | 30 #include "chrome/common/extensions/features/feature.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread_bundle.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 34 |
| 35 using content::BrowserThread; | 35 using content::BrowserThread; |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 const char kTestMessage[] = "{\"text\": \"Hello.\"}"; | 39 const char kTestMessage[] = "{\"text\": \"Hello.\"}"; |
| 40 | 40 |
| 41 base::FilePath GetTestDir() { | 41 base::FilePath GetTestDir() { |
| 42 base::FilePath test_dir; | 42 base::FilePath test_dir; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 72 } | 72 } |
| 73 | 73 |
| 74 private: | 74 private: |
| 75 base::PlatformFile read_file_; | 75 base::PlatformFile read_file_; |
| 76 base::PlatformFile write_file_; | 76 base::PlatformFile write_file_; |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 class NativeMessagingTest : public ::testing::Test, | 79 class NativeMessagingTest : public ::testing::Test, |
| 80 public NativeMessageProcessHost::Client, | 80 public NativeMessageProcessHost::Client, |
| 81 public base::SupportsWeakPtr<NativeMessagingTest> { | 81 public base::SupportsWeakPtr<NativeMessagingTest> { |
| 82 public: | 82 protected: |
| 83 NativeMessagingTest() | 83 NativeMessagingTest() |
| 84 : current_channel_(chrome::VersionInfo::CHANNEL_DEV), | 84 : current_channel_(chrome::VersionInfo::CHANNEL_DEV), |
| 85 native_message_process_host_(NULL) { | 85 native_message_process_host_(NULL), |
| 86 } | 86 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 87 | |
| 88 virtual void SetUp() OVERRIDE { | |
| 89 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 87 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 90 // Change the user data dir so native apps will be looked for in the test | 88 // Change the user data dir so native apps will be looked for in the test |
| 91 // directory. | 89 // directory. |
| 92 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); | 90 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); |
| 93 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); | 91 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); |
| 94 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | |
| 95 &message_loop_)); | |
| 96 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO, | |
| 97 &message_loop_)); | |
| 98 } | 92 } |
| 99 | 93 |
| 100 virtual void TearDown() OVERRIDE { | 94 virtual ~NativeMessagingTest() { |
| 101 // Change the user data dir back for other tests. | 95 // Change the user data dir back for other tests. |
| 102 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); | 96 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); |
| 103 if (native_message_process_host_.get()) { | 97 if (native_message_process_host_.get()) { |
| 104 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | 98 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 105 native_message_process_host_.release()); | 99 native_message_process_host_.release()); |
| 106 } | 100 } |
| 107 message_loop_.RunUntilIdle(); | 101 base::RunLoop().RunUntilIdle(); |
| 108 } | 102 } |
| 109 | 103 |
| 110 virtual void PostMessageFromNativeProcess( | 104 virtual void PostMessageFromNativeProcess( |
| 111 int port_id, | 105 int port_id, |
| 112 const std::string& message) OVERRIDE { | 106 const std::string& message) OVERRIDE { |
| 113 last_message_ = message; | 107 last_message_ = message; |
| 114 | 108 |
| 115 // Parse the message. | 109 // Parse the message. |
| 116 base::Value* parsed = base::JSONReader::Read(message); | 110 base::Value* parsed = base::JSONReader::Read(message); |
| 117 base::DictionaryValue* dict_value; | 111 base::DictionaryValue* dict_value; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 146 EXPECT_TRUE(file_util::WriteFile( | 140 EXPECT_TRUE(file_util::WriteFile( |
| 147 filename, message_with_header.data(), message_with_header.size())); | 141 filename, message_with_header.data(), message_with_header.size())); |
| 148 return filename; | 142 return filename; |
| 149 } | 143 } |
| 150 | 144 |
| 151 // Force the channel to be dev. | 145 // Force the channel to be dev. |
| 152 base::ScopedTempDir temp_dir_; | 146 base::ScopedTempDir temp_dir_; |
| 153 Feature::ScopedCurrentChannel current_channel_; | 147 Feature::ScopedCurrentChannel current_channel_; |
| 154 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; | 148 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; |
| 155 base::FilePath user_data_dir_; | 149 base::FilePath user_data_dir_; |
| 156 base::MessageLoopForIO message_loop_; | |
| 157 scoped_ptr<base::RunLoop> read_message_run_loop_; | 150 scoped_ptr<base::RunLoop> read_message_run_loop_; |
| 158 scoped_ptr<content::TestBrowserThread> ui_thread_; | 151 content::TestBrowserThreadBundle thread_bundle_; |
| 159 scoped_ptr<content::TestBrowserThread> io_thread_; | |
| 160 std::string last_message_; | 152 std::string last_message_; |
| 161 scoped_ptr<base::DictionaryValue> last_message_parsed_; | 153 scoped_ptr<base::DictionaryValue> last_message_parsed_; |
| 162 }; | 154 }; |
| 163 | 155 |
| 164 // Read a single message from a local file. | 156 // Read a single message from a local file. |
| 165 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 157 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| 166 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 158 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 167 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); | 159 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
| 168 | 160 |
| 169 scoped_ptr<NativeProcessLauncher> launcher( | 161 scoped_ptr<NativeProcessLauncher> launcher( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 188 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 180 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| 189 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 181 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 190 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); | 182 base::FilePath temp_input_file = CreateTempFileWithMessage(std::string()); |
| 191 | 183 |
| 192 scoped_ptr<NativeProcessLauncher> launcher( | 184 scoped_ptr<NativeProcessLauncher> launcher( |
| 193 new FakeLauncher(temp_input_file, temp_output_file)); | 185 new FakeLauncher(temp_input_file, temp_output_file)); |
| 194 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( | 186 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
| 195 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", | 187 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", |
| 196 0, launcher.Pass()); | 188 0, launcher.Pass()); |
| 197 ASSERT_TRUE(native_message_process_host_.get()); | 189 ASSERT_TRUE(native_message_process_host_.get()); |
| 198 message_loop_.RunUntilIdle(); | 190 base::RunLoop().RunUntilIdle(); |
| 199 | 191 |
| 200 native_message_process_host_->Send(kTestMessage); | 192 native_message_process_host_->Send(kTestMessage); |
| 201 message_loop_.RunUntilIdle(); | 193 base::RunLoop().RunUntilIdle(); |
| 202 | 194 |
| 203 std::string output; | 195 std::string output; |
| 204 base::TimeTicks start_time = base::TimeTicks::Now(); | 196 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 205 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { | 197 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { |
| 206 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); | 198 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); |
| 207 if (!output.empty()) | 199 if (!output.empty()) |
| 208 break; | 200 break; |
| 209 base::PlatformThread::YieldCurrentThread(); | 201 base::PlatformThread::YieldCurrentThread(); |
| 210 } | 202 } |
| 211 | 203 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 read_message_run_loop_->Run(); | 246 read_message_run_loop_->Run(); |
| 255 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); | 247 EXPECT_TRUE(last_message_parsed_->GetInteger("id", &id)); |
| 256 EXPECT_EQ(2, id); | 248 EXPECT_EQ(2, id); |
| 257 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); | 249 EXPECT_TRUE(last_message_parsed_->GetString("echo.foo", &text)); |
| 258 EXPECT_EQ("bar", text); | 250 EXPECT_EQ("bar", text); |
| 259 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); | 251 EXPECT_TRUE(last_message_parsed_->GetString("caller_url", &url)); |
| 260 EXPECT_EQ(expected_url, url); | 252 EXPECT_EQ(expected_url, url); |
| 261 } | 253 } |
| 262 | 254 |
| 263 } // namespace extensions | 255 } // namespace extensions |
| OLD | NEW |