| 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/files/file_util.h" | 5 #include "base/files/file_util.h" |
| 6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| 11 #include "content/public/browser/devtools_http_handler.h" | 11 #include "content/public/browser/devtools_http_handler.h" |
| 12 #include "content/public/browser/devtools_http_handler_delegate.h" | 12 #include "content/public/browser/devtools_http_handler_delegate.h" |
| 13 #include "content/public/browser/devtools_target.h" | 13 #include "content/public/browser/devtools_target.h" |
| 14 #include "content/public/test/test_utils.h" |
| 14 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/socket/server_socket.h" | 17 #include "net/socket/server_socket.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 const int kDummyPort = 4321; | 23 const int kDummyPort = 4321; |
| 23 const base::FilePath::CharType kDevToolsActivePortFileName[] = | 24 const base::FilePath::CharType kDevToolsActivePortFileName[] = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 44 *address = net::IPEndPoint(number, kDummyPort); | 45 *address = net::IPEndPoint(number, kDummyPort); |
| 45 return net::OK; | 46 return net::OK; |
| 46 } | 47 } |
| 47 | 48 |
| 48 int Accept(scoped_ptr<net::StreamSocket>* socket, | 49 int Accept(scoped_ptr<net::StreamSocket>* socket, |
| 49 const net::CompletionCallback& callback) override { | 50 const net::CompletionCallback& callback) override { |
| 50 return net::ERR_IO_PENDING; | 51 return net::ERR_IO_PENDING; |
| 51 } | 52 } |
| 52 }; | 53 }; |
| 53 | 54 |
| 55 void QuitFromHandlerThread(const base::Closure& quit_closure) { |
| 56 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); |
| 57 } |
| 58 |
| 54 class DummyServerSocketFactory | 59 class DummyServerSocketFactory |
| 55 : public DevToolsHttpHandler::ServerSocketFactory { | 60 : public DevToolsHttpHandler::ServerSocketFactory { |
| 56 public: | 61 public: |
| 57 DummyServerSocketFactory(base::Closure quit_closure_1, | 62 DummyServerSocketFactory(base::Closure quit_closure_1, |
| 58 base::Closure quit_closure_2) | 63 base::Closure quit_closure_2) |
| 59 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0), | 64 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0), |
| 60 quit_closure_1_(quit_closure_1), | 65 quit_closure_1_(quit_closure_1), |
| 61 quit_closure_2_(quit_closure_2) {} | 66 quit_closure_2_(quit_closure_2) {} |
| 62 | 67 |
| 63 ~DummyServerSocketFactory() override { | 68 ~DummyServerSocketFactory() override { |
| 64 BrowserThread::PostTask( | 69 BrowserThread::PostTask( |
| 65 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 70 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
| 66 } | 71 } |
| 67 | 72 |
| 68 private: | 73 protected: |
| 69 scoped_ptr<net::ServerSocket> Create() const override { | 74 scoped_ptr<net::ServerSocket> Create() const override { |
| 70 BrowserThread::PostTask( | 75 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 71 BrowserThread::UI, FROM_HERE, quit_closure_1_); | 76 base::Bind(&QuitFromHandlerThread, quit_closure_1_)); |
| 72 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); | 77 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); |
| 73 } | 78 } |
| 74 | 79 |
| 75 base::Closure quit_closure_1_; | 80 base::Closure quit_closure_1_; |
| 76 base::Closure quit_closure_2_; | 81 base::Closure quit_closure_2_; |
| 77 }; | 82 }; |
| 78 | 83 |
| 84 class FailingServerSocketFactory : public DummyServerSocketFactory { |
| 85 public: |
| 86 FailingServerSocketFactory(const base::Closure& quit_closure_1, |
| 87 const base::Closure& quit_closure_2) |
| 88 : DummyServerSocketFactory(quit_closure_1, quit_closure_2) { |
| 89 } |
| 90 |
| 91 private: |
| 92 scoped_ptr<net::ServerSocket> Create() const override { |
| 93 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 94 base::Bind(&QuitFromHandlerThread, quit_closure_1_)); |
| 95 return nullptr; |
| 96 } |
| 97 }; |
| 98 |
| 79 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 99 class DummyDelegate : public DevToolsHttpHandlerDelegate { |
| 80 public: | 100 public: |
| 81 std::string GetDiscoveryPageHTML() override { return std::string(); } | 101 std::string GetDiscoveryPageHTML() override { return std::string(); } |
| 82 | 102 |
| 83 bool BundlesFrontendResources() override { return true; } | 103 bool BundlesFrontendResources() override { return true; } |
| 84 | 104 |
| 85 base::FilePath GetDebugFrontendDir() override { return base::FilePath(); } | 105 base::FilePath GetDebugFrontendDir() override { return base::FilePath(); } |
| 86 | 106 |
| 87 scoped_ptr<net::ServerSocket> | 107 scoped_ptr<net::ServerSocket> |
| 88 CreateSocketForTethering(std::string* name) override { | 108 CreateSocketForTethering(std::string* name) override { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 110 base::MessageLoopForIO message_loop_; | 130 base::MessageLoopForIO message_loop_; |
| 111 BrowserThreadImpl ui_thread_; | 131 BrowserThreadImpl ui_thread_; |
| 112 scoped_ptr<BrowserThreadImpl> file_thread_; | 132 scoped_ptr<BrowserThreadImpl> file_thread_; |
| 113 }; | 133 }; |
| 114 | 134 |
| 115 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 135 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
| 116 base::RunLoop run_loop, run_loop_2; | 136 base::RunLoop run_loop, run_loop_2; |
| 117 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 137 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 118 new DummyServerSocketFactory(run_loop.QuitClosure(), | 138 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 119 run_loop_2.QuitClosure())); | 139 run_loop_2.QuitClosure())); |
| 120 content::DevToolsHttpHandler* devtools_http_handler_ = | 140 content::DevToolsHttpHandler* devtools_http_handler = |
| 121 content::DevToolsHttpHandler::Start(factory.Pass(), | 141 content::DevToolsHttpHandler::Start(factory.Pass(), |
| 122 std::string(), | 142 std::string(), |
| 123 new DummyDelegate(), | 143 new DummyDelegate(), |
| 144 base::FilePath()); |
| 145 // Our dummy socket factory will post a quit message once the server will |
| 146 // become ready. |
| 147 run_loop.Run(); |
| 148 devtools_http_handler->Stop(); |
| 149 // Make sure the handler actually stops. |
| 150 run_loop_2.Run(); |
| 151 } |
| 152 |
| 153 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { |
| 154 base::RunLoop run_loop, run_loop_2; |
| 155 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 156 new FailingServerSocketFactory(run_loop.QuitClosure(), |
| 157 run_loop_2.QuitClosure())); |
| 158 content::DevToolsHttpHandler* devtools_http_handler = |
| 159 content::DevToolsHttpHandler::Start(factory.Pass(), |
| 160 std::string(), |
| 161 new DummyDelegate(), |
| 124 base::FilePath()); | 162 base::FilePath()); |
| 125 // Our dummy socket factory will post a quit message once the server will | 163 // Our dummy socket factory will post a quit message once the server will |
| 126 // become ready. | 164 // become ready. |
| 127 run_loop.Run(); | 165 run_loop.Run(); |
| 128 devtools_http_handler_->Stop(); | 166 for (int i = 0; i < 5; i++) { |
| 167 RunAllPendingInMessageLoop(BrowserThread::UI); |
| 168 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 169 } |
| 170 devtools_http_handler->Stop(); |
| 129 // Make sure the handler actually stops. | 171 // Make sure the handler actually stops. |
| 130 run_loop_2.Run(); | 172 run_loop_2.Run(); |
| 131 } | 173 } |
| 132 | 174 |
| 175 |
| 133 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 176 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
| 134 base::RunLoop run_loop, run_loop_2; | 177 base::RunLoop run_loop, run_loop_2; |
| 135 base::ScopedTempDir temp_dir; | 178 base::ScopedTempDir temp_dir; |
| 136 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 179 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 137 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 180 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 138 new DummyServerSocketFactory(run_loop.QuitClosure(), | 181 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 139 run_loop_2.QuitClosure())); | 182 run_loop_2.QuitClosure())); |
| 140 content::DevToolsHttpHandler* devtools_http_handler_ = | 183 content::DevToolsHttpHandler* devtools_http_handler = |
| 141 content::DevToolsHttpHandler::Start(factory.Pass(), | 184 content::DevToolsHttpHandler::Start(factory.Pass(), |
| 142 std::string(), | 185 std::string(), |
| 143 new DummyDelegate(), | 186 new DummyDelegate(), |
| 144 temp_dir.path()); | 187 temp_dir.path()); |
| 145 // Our dummy socket factory will post a quit message once the server will | 188 // Our dummy socket factory will post a quit message once the server will |
| 146 // become ready. | 189 // become ready. |
| 147 run_loop.Run(); | 190 run_loop.Run(); |
| 148 devtools_http_handler_->Stop(); | 191 devtools_http_handler->Stop(); |
| 149 // Make sure the handler actually stops. | 192 // Make sure the handler actually stops. |
| 150 run_loop_2.Run(); | 193 run_loop_2.Run(); |
| 151 | 194 |
| 152 // Now make sure the DevToolsActivePort was written into the | 195 // Now make sure the DevToolsActivePort was written into the |
| 153 // temporary directory and its contents are as expected. | 196 // temporary directory and its contents are as expected. |
| 154 base::FilePath active_port_file = temp_dir.path().Append( | 197 base::FilePath active_port_file = temp_dir.path().Append( |
| 155 kDevToolsActivePortFileName); | 198 kDevToolsActivePortFileName); |
| 156 EXPECT_TRUE(base::PathExists(active_port_file)); | 199 EXPECT_TRUE(base::PathExists(active_port_file)); |
| 157 std::string file_contents; | 200 std::string file_contents; |
| 158 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 201 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
| 159 int port = 0; | 202 int port = 0; |
| 160 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 203 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
| 161 EXPECT_EQ(kDummyPort, port); | 204 EXPECT_EQ(kDummyPort, port); |
| 162 } | 205 } |
| 163 | 206 |
| 164 } // namespace content | 207 } // namespace content |
| OLD | NEW |