| 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 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 86 } | 87 } |
| 87 | 88 |
| 88 virtual void SetUp() OVERRIDE { | 89 virtual void SetUp() OVERRIDE { |
| 89 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 90 // Change the user data dir so native apps will be looked for in the test | 91 // Change the user data dir so native apps will be looked for in the test |
| 91 // directory. | 92 // directory. |
| 92 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); | 93 ASSERT_TRUE(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_)); |
| 93 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, GetTestDir())); | 94 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 } | 95 } |
| 99 | 96 |
| 100 virtual void TearDown() OVERRIDE { | 97 virtual void TearDown() OVERRIDE { |
| 101 // Change the user data dir back for other tests. | 98 // Change the user data dir back for other tests. |
| 102 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); | 99 ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir_)); |
| 103 if (native_message_process_host_.get()) { | 100 if (native_message_process_host_.get()) { |
| 104 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | 101 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 105 native_message_process_host_.release()); | 102 native_message_process_host_.release()); |
| 106 } | 103 } |
| 107 message_loop_.RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 108 } | 105 } |
| 109 | 106 |
| 110 virtual void PostMessageFromNativeProcess( | 107 virtual void PostMessageFromNativeProcess( |
| 111 int port_id, | 108 int port_id, |
| 112 scoped_ptr<base::ListValue> message_as_list) OVERRIDE { | 109 scoped_ptr<base::ListValue> message_as_list) OVERRIDE { |
| 113 // |message_as_list| should contain a single DictionaryValue. Extract it | 110 // |message_as_list| should contain a single DictionaryValue. Extract it |
| 114 // into |last_message_|. | 111 // into |last_message_|. |
| 115 ASSERT_EQ(1u, message_as_list->GetSize()); | 112 ASSERT_EQ(1u, message_as_list->GetSize()); |
| 116 base::Value* last_message_value = NULL; | 113 base::Value* last_message_value = NULL; |
| 117 message_as_list->Remove(0, &last_message_value); | 114 message_as_list->Remove(0, &last_message_value); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 142 EXPECT_TRUE(file_util::WriteFile( | 139 EXPECT_TRUE(file_util::WriteFile( |
| 143 filename, message_with_header.data(), message_with_header.size())); | 140 filename, message_with_header.data(), message_with_header.size())); |
| 144 return filename; | 141 return filename; |
| 145 } | 142 } |
| 146 | 143 |
| 147 // Force the channel to be dev. | 144 // Force the channel to be dev. |
| 148 base::ScopedTempDir temp_dir_; | 145 base::ScopedTempDir temp_dir_; |
| 149 Feature::ScopedCurrentChannel current_channel_; | 146 Feature::ScopedCurrentChannel current_channel_; |
| 150 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; | 147 scoped_ptr<NativeMessageProcessHost> native_message_process_host_; |
| 151 base::FilePath user_data_dir_; | 148 base::FilePath user_data_dir_; |
| 152 base::MessageLoopForIO message_loop_; | |
| 153 scoped_ptr<base::RunLoop> read_message_run_loop_; | 149 scoped_ptr<base::RunLoop> read_message_run_loop_; |
| 154 scoped_ptr<content::TestBrowserThread> ui_thread_; | 150 content::TestBrowserThreadBundle thread_bundle_; |
| 155 scoped_ptr<content::TestBrowserThread> io_thread_; | |
| 156 scoped_ptr<DictionaryValue> last_message_; | 151 scoped_ptr<DictionaryValue> last_message_; |
| 157 }; | 152 }; |
| 158 | 153 |
| 159 // Read a single message from a local file. | 154 // Read a single message from a local file. |
| 160 TEST_F(NativeMessagingTest, SingleSendMessageRead) { | 155 TEST_F(NativeMessagingTest, SingleSendMessageRead) { |
| 161 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 156 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 162 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); | 157 base::FilePath temp_input_file = CreateTempFileWithMessage(kTestMessage); |
| 163 | 158 |
| 164 scoped_ptr<NativeProcessLauncher> launcher( | 159 scoped_ptr<NativeProcessLauncher> launcher( |
| 165 new FakeLauncher(temp_input_file, temp_output_file)); | 160 new FakeLauncher(temp_input_file, temp_output_file)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 190 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { | 185 TEST_F(NativeMessagingTest, SingleSendMessageWrite) { |
| 191 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); | 186 base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); |
| 192 base::FilePath temp_input_file = CreateTempFileWithMessage("{}"); | 187 base::FilePath temp_input_file = CreateTempFileWithMessage("{}"); |
| 193 | 188 |
| 194 scoped_ptr<NativeProcessLauncher> launcher( | 189 scoped_ptr<NativeProcessLauncher> launcher( |
| 195 new FakeLauncher(temp_input_file, temp_output_file)); | 190 new FakeLauncher(temp_input_file, temp_output_file)); |
| 196 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( | 191 native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( |
| 197 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", | 192 AsWeakPtr(), kTestNativeMessagingExtensionId, "empty_app.py", |
| 198 0, launcher.Pass()); | 193 0, launcher.Pass()); |
| 199 ASSERT_TRUE(native_message_process_host_.get()); | 194 ASSERT_TRUE(native_message_process_host_.get()); |
| 200 message_loop_.RunUntilIdle(); | 195 base::RunLoop().RunUntilIdle(); |
| 201 | 196 |
| 202 native_message_process_host_->Send(kTestMessage); | 197 native_message_process_host_->Send(kTestMessage); |
| 203 message_loop_.RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
| 204 | 199 |
| 205 std::string output; | 200 std::string output; |
| 206 base::TimeTicks start_time = base::TimeTicks::Now(); | 201 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 207 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { | 202 while (base::TimeTicks::Now() - start_time < TestTimeouts::action_timeout()) { |
| 208 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); | 203 ASSERT_TRUE(file_util::ReadFileToString(temp_output_file, &output)); |
| 209 if (!output.empty()) | 204 if (!output.empty()) |
| 210 break; | 205 break; |
| 211 base::PlatformThread::YieldCurrentThread(); | 206 base::PlatformThread::YieldCurrentThread(); |
| 212 } | 207 } |
| 213 | 208 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 read_message_run_loop_->Run(); | 250 read_message_run_loop_->Run(); |
| 256 EXPECT_TRUE(last_message_->GetInteger("id", &id)); | 251 EXPECT_TRUE(last_message_->GetInteger("id", &id)); |
| 257 EXPECT_EQ(2, id); | 252 EXPECT_EQ(2, id); |
| 258 EXPECT_TRUE(last_message_->GetString("echo.foo", &text)); | 253 EXPECT_TRUE(last_message_->GetString("echo.foo", &text)); |
| 259 EXPECT_EQ("bar", text); | 254 EXPECT_EQ("bar", text); |
| 260 EXPECT_TRUE(last_message_->GetString("caller_url", &url)); | 255 EXPECT_TRUE(last_message_->GetString("caller_url", &url)); |
| 261 EXPECT_EQ(expected_url, url); | 256 EXPECT_EQ(expected_url, url); |
| 262 } | 257 } |
| 263 | 258 |
| 264 } // namespace extensions | 259 } // namespace extensions |
| OLD | NEW |