| 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 "components/devtools_http_handler/devtools_http_handler.h" | 5 #include "components/devtools_http_handler/devtools_http_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 8 #include <utility> | 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/memory/ptr_util.h" |
| 13 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 14 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/values.h" | 20 #include "base/values.h" |
| 18 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | 21 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "content/public/test/test_utils.h" | 23 #include "content/public/test/test_utils.h" |
| 21 #include "net/base/ip_address.h" | 24 #include "net/base/ip_address.h" |
| 22 #include "net/base/ip_endpoint.h" | 25 #include "net/base/ip_endpoint.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 40 // net::ServerSocket "implementation" | 43 // net::ServerSocket "implementation" |
| 41 int Listen(const net::IPEndPoint& address, int backlog) override { | 44 int Listen(const net::IPEndPoint& address, int backlog) override { |
| 42 return net::OK; | 45 return net::OK; |
| 43 } | 46 } |
| 44 | 47 |
| 45 int GetLocalAddress(net::IPEndPoint* address) const override { | 48 int GetLocalAddress(net::IPEndPoint* address) const override { |
| 46 *address = net::IPEndPoint(net::IPAddress::IPv4Localhost(), kDummyPort); | 49 *address = net::IPEndPoint(net::IPAddress::IPv4Localhost(), kDummyPort); |
| 47 return net::OK; | 50 return net::OK; |
| 48 } | 51 } |
| 49 | 52 |
| 50 int Accept(scoped_ptr<net::StreamSocket>* socket, | 53 int Accept(std::unique_ptr<net::StreamSocket>* socket, |
| 51 const net::CompletionCallback& callback) override { | 54 const net::CompletionCallback& callback) override { |
| 52 return net::ERR_IO_PENDING; | 55 return net::ERR_IO_PENDING; |
| 53 } | 56 } |
| 54 }; | 57 }; |
| 55 | 58 |
| 56 void QuitFromHandlerThread(const base::Closure& quit_closure) { | 59 void QuitFromHandlerThread(const base::Closure& quit_closure) { |
| 57 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); | 60 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); |
| 58 } | 61 } |
| 59 | 62 |
| 60 class DummyServerSocketFactory | 63 class DummyServerSocketFactory |
| 61 : public DevToolsHttpHandler::ServerSocketFactory { | 64 : public DevToolsHttpHandler::ServerSocketFactory { |
| 62 public: | 65 public: |
| 63 DummyServerSocketFactory(base::Closure quit_closure_1, | 66 DummyServerSocketFactory(base::Closure quit_closure_1, |
| 64 base::Closure quit_closure_2) | 67 base::Closure quit_closure_2) |
| 65 : quit_closure_1_(quit_closure_1), | 68 : quit_closure_1_(quit_closure_1), |
| 66 quit_closure_2_(quit_closure_2) {} | 69 quit_closure_2_(quit_closure_2) {} |
| 67 | 70 |
| 68 ~DummyServerSocketFactory() override { | 71 ~DummyServerSocketFactory() override { |
| 69 BrowserThread::PostTask( | 72 BrowserThread::PostTask( |
| 70 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 73 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
| 71 } | 74 } |
| 72 | 75 |
| 73 protected: | 76 protected: |
| 74 scoped_ptr<net::ServerSocket> CreateForHttpServer() override { | 77 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 75 base::ThreadTaskRunnerHandle::Get()->PostTask( | 78 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 76 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); | 79 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); |
| 77 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); | 80 return base::WrapUnique(new DummyServerSocket()); |
| 78 } | 81 } |
| 79 | 82 |
| 80 base::Closure quit_closure_1_; | 83 base::Closure quit_closure_1_; |
| 81 base::Closure quit_closure_2_; | 84 base::Closure quit_closure_2_; |
| 82 }; | 85 }; |
| 83 | 86 |
| 84 class FailingServerSocketFactory : public DummyServerSocketFactory { | 87 class FailingServerSocketFactory : public DummyServerSocketFactory { |
| 85 public: | 88 public: |
| 86 FailingServerSocketFactory(const base::Closure& quit_closure_1, | 89 FailingServerSocketFactory(const base::Closure& quit_closure_1, |
| 87 const base::Closure& quit_closure_2) | 90 const base::Closure& quit_closure_2) |
| 88 : DummyServerSocketFactory(quit_closure_1, quit_closure_2) { | 91 : DummyServerSocketFactory(quit_closure_1, quit_closure_2) { |
| 89 } | 92 } |
| 90 | 93 |
| 91 private: | 94 private: |
| 92 scoped_ptr<net::ServerSocket> CreateForHttpServer() override { | 95 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { |
| 93 base::ThreadTaskRunnerHandle::Get()->PostTask( | 96 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 94 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); | 97 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); |
| 95 return scoped_ptr<net::ServerSocket>(); | 98 return nullptr; |
| 96 } | 99 } |
| 97 }; | 100 }; |
| 98 | 101 |
| 99 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 102 class DummyDelegate : public DevToolsHttpHandlerDelegate { |
| 100 public: | 103 public: |
| 101 std::string GetDiscoveryPageHTML() override { return std::string(); } | 104 std::string GetDiscoveryPageHTML() override { return std::string(); } |
| 102 | 105 |
| 103 std::string GetFrontendResource(const std::string& path) override { | 106 std::string GetFrontendResource(const std::string& path) override { |
| 104 return std::string(); | 107 return std::string(); |
| 105 } | 108 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 119 class DevToolsHttpHandlerTest : public testing::Test { | 122 class DevToolsHttpHandlerTest : public testing::Test { |
| 120 public: | 123 public: |
| 121 DevToolsHttpHandlerTest() : testing::Test() { } | 124 DevToolsHttpHandlerTest() : testing::Test() { } |
| 122 | 125 |
| 123 private: | 126 private: |
| 124 content::TestBrowserThreadBundle thread_bundle_; | 127 content::TestBrowserThreadBundle thread_bundle_; |
| 125 }; | 128 }; |
| 126 | 129 |
| 127 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 130 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
| 128 base::RunLoop run_loop, run_loop_2; | 131 base::RunLoop run_loop, run_loop_2; |
| 129 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 132 std::unique_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 130 new DummyServerSocketFactory(run_loop.QuitClosure(), | 133 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 131 run_loop_2.QuitClosure())); | 134 run_loop_2.QuitClosure())); |
| 132 scoped_ptr<DevToolsHttpHandler> devtools_http_handler(new DevToolsHttpHandler( | 135 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( |
| 133 std::move(factory), std::string(), new DummyDelegate(), base::FilePath(), | 136 new DevToolsHttpHandler(std::move(factory), std::string(), |
| 134 base::FilePath(), std::string(), std::string())); | 137 new DummyDelegate(), base::FilePath(), |
| 138 base::FilePath(), std::string(), std::string())); |
| 135 // Our dummy socket factory will post a quit message once the server will | 139 // Our dummy socket factory will post a quit message once the server will |
| 136 // become ready. | 140 // become ready. |
| 137 run_loop.Run(); | 141 run_loop.Run(); |
| 138 devtools_http_handler.reset(); | 142 devtools_http_handler.reset(); |
| 139 // Make sure the handler actually stops. | 143 // Make sure the handler actually stops. |
| 140 run_loop_2.Run(); | 144 run_loop_2.Run(); |
| 141 } | 145 } |
| 142 | 146 |
| 143 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { | 147 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { |
| 144 base::RunLoop run_loop, run_loop_2; | 148 base::RunLoop run_loop, run_loop_2; |
| 145 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 149 std::unique_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 146 new FailingServerSocketFactory(run_loop.QuitClosure(), | 150 new FailingServerSocketFactory(run_loop.QuitClosure(), |
| 147 run_loop_2.QuitClosure())); | 151 run_loop_2.QuitClosure())); |
| 148 scoped_ptr<DevToolsHttpHandler> devtools_http_handler(new DevToolsHttpHandler( | 152 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( |
| 149 std::move(factory), std::string(), new DummyDelegate(), base::FilePath(), | 153 new DevToolsHttpHandler(std::move(factory), std::string(), |
| 150 base::FilePath(), std::string(), std::string())); | 154 new DummyDelegate(), base::FilePath(), |
| 155 base::FilePath(), std::string(), std::string())); |
| 151 // Our dummy socket factory will post a quit message once the server will | 156 // Our dummy socket factory will post a quit message once the server will |
| 152 // become ready. | 157 // become ready. |
| 153 run_loop.Run(); | 158 run_loop.Run(); |
| 154 for (int i = 0; i < 5; i++) { | 159 for (int i = 0; i < 5; i++) { |
| 155 RunAllPendingInMessageLoop(BrowserThread::UI); | 160 RunAllPendingInMessageLoop(BrowserThread::UI); |
| 156 RunAllPendingInMessageLoop(BrowserThread::FILE); | 161 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 157 } | 162 } |
| 158 devtools_http_handler.reset(); | 163 devtools_http_handler.reset(); |
| 159 // Make sure the handler actually stops. | 164 // Make sure the handler actually stops. |
| 160 run_loop_2.Run(); | 165 run_loop_2.Run(); |
| 161 } | 166 } |
| 162 | 167 |
| 163 | 168 |
| 164 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 169 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
| 165 base::RunLoop run_loop, run_loop_2; | 170 base::RunLoop run_loop, run_loop_2; |
| 166 base::ScopedTempDir temp_dir; | 171 base::ScopedTempDir temp_dir; |
| 167 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 172 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 168 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 173 std::unique_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 169 new DummyServerSocketFactory(run_loop.QuitClosure(), | 174 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 170 run_loop_2.QuitClosure())); | 175 run_loop_2.QuitClosure())); |
| 171 scoped_ptr<DevToolsHttpHandler> devtools_http_handler(new DevToolsHttpHandler( | 176 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( |
| 172 std::move(factory), std::string(), new DummyDelegate(), temp_dir.path(), | 177 new DevToolsHttpHandler(std::move(factory), std::string(), |
| 173 base::FilePath(), std::string(), std::string())); | 178 new DummyDelegate(), temp_dir.path(), |
| 179 base::FilePath(), std::string(), std::string())); |
| 174 // Our dummy socket factory will post a quit message once the server will | 180 // Our dummy socket factory will post a quit message once the server will |
| 175 // become ready. | 181 // become ready. |
| 176 run_loop.Run(); | 182 run_loop.Run(); |
| 177 devtools_http_handler.reset(); | 183 devtools_http_handler.reset(); |
| 178 // Make sure the handler actually stops. | 184 // Make sure the handler actually stops. |
| 179 run_loop_2.Run(); | 185 run_loop_2.Run(); |
| 180 | 186 |
| 181 // Now make sure the DevToolsActivePort was written into the | 187 // Now make sure the DevToolsActivePort was written into the |
| 182 // temporary directory and its contents are as expected. | 188 // temporary directory and its contents are as expected. |
| 183 base::FilePath active_port_file = temp_dir.path().Append( | 189 base::FilePath active_port_file = temp_dir.path().Append( |
| 184 kDevToolsActivePortFileName); | 190 kDevToolsActivePortFileName); |
| 185 EXPECT_TRUE(base::PathExists(active_port_file)); | 191 EXPECT_TRUE(base::PathExists(active_port_file)); |
| 186 std::string file_contents; | 192 std::string file_contents; |
| 187 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 193 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
| 188 int port = 0; | 194 int port = 0; |
| 189 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 195 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
| 190 EXPECT_EQ(static_cast<int>(kDummyPort), port); | 196 EXPECT_EQ(static_cast<int>(kDummyPort), port); |
| 191 } | 197 } |
| 192 | 198 |
| 193 } // namespace devtools_http_handler | 199 } // namespace devtools_http_handler |
| OLD | NEW |