| 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/file_util.h" | 5 #include "base/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 "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/socket/server_socket.h" | 16 #include "net/socket/stream_listen_socket.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const int kDummyPort = 4321; | 22 const int kDummyPort = 4321; |
| 23 const base::FilePath::CharType kDevToolsActivePortFileName[] = | 23 const base::FilePath::CharType kDevToolsActivePortFileName[] = |
| 24 FILE_PATH_LITERAL("DevToolsActivePort"); | 24 FILE_PATH_LITERAL("DevToolsActivePort"); |
| 25 | 25 |
| 26 class DummyServerSocket : public net::ServerSocket { | 26 using net::StreamListenSocket; |
| 27 |
| 28 class DummyListenSocket : public StreamListenSocket, |
| 29 public StreamListenSocket::Delegate { |
| 27 public: | 30 public: |
| 28 DummyServerSocket() {} | 31 DummyListenSocket() |
| 32 : StreamListenSocket(net::kInvalidSocket, this) {} |
| 29 | 33 |
| 30 // net::ServerSocket "implementation" | 34 // StreamListenSocket::Delegate "implementation" |
| 31 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE { | 35 virtual void DidAccept(StreamListenSocket* server, |
| 32 return net::OK; | 36 scoped_ptr<StreamListenSocket> connection) OVERRIDE {} |
| 33 } | 37 virtual void DidRead(StreamListenSocket* connection, |
| 34 | 38 const char* data, |
| 35 virtual int ListenWithAddressAndPort(const std::string& ip_address, | 39 int len) OVERRIDE {} |
| 36 int port, | 40 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} |
| 37 int backlog) OVERRIDE { | 41 protected: |
| 38 return net::OK; | 42 virtual ~DummyListenSocket() {} |
| 39 } | 43 virtual void Accept() OVERRIDE {} |
| 40 | 44 virtual int GetLocalAddress(net::IPEndPoint* address) OVERRIDE { |
| 41 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { | |
| 42 net::IPAddressNumber number; | 45 net::IPAddressNumber number; |
| 43 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &number)); | 46 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &number)); |
| 44 *address = net::IPEndPoint(number, kDummyPort); | 47 *address = net::IPEndPoint(number, kDummyPort); |
| 45 return net::OK; | 48 return net::OK; |
| 46 } | 49 } |
| 47 | |
| 48 virtual int Accept(scoped_ptr<net::StreamSocket>* socket, | |
| 49 const net::CompletionCallback& callback) OVERRIDE { | |
| 50 return net::ERR_IO_PENDING; | |
| 51 } | |
| 52 }; | 50 }; |
| 53 | 51 |
| 54 class DummyServerSocketFactory | 52 class DummyListenSocketFactory : public net::StreamListenSocketFactory { |
| 55 : public DevToolsHttpHandler::ServerSocketFactory { | |
| 56 public: | 53 public: |
| 57 DummyServerSocketFactory(base::Closure quit_closure_1, | 54 DummyListenSocketFactory( |
| 58 base::Closure quit_closure_2) | 55 base::Closure quit_closure_1, base::Closure quit_closure_2) |
| 59 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0), | 56 : quit_closure_1_(quit_closure_1), quit_closure_2_(quit_closure_2) {} |
| 60 quit_closure_1_(quit_closure_1), | 57 virtual ~DummyListenSocketFactory() { |
| 61 quit_closure_2_(quit_closure_2) {} | |
| 62 | |
| 63 virtual ~DummyServerSocketFactory() { | |
| 64 BrowserThread::PostTask( | 58 BrowserThread::PostTask( |
| 65 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 59 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
| 66 } | 60 } |
| 67 | 61 |
| 68 private: | 62 virtual scoped_ptr<StreamListenSocket> CreateAndListen( |
| 69 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE { | 63 StreamListenSocket::Delegate* delegate) const OVERRIDE { |
| 70 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
| 71 BrowserThread::UI, FROM_HERE, quit_closure_1_); | 65 BrowserThread::UI, FROM_HERE, quit_closure_1_); |
| 72 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); | 66 return scoped_ptr<net::StreamListenSocket>(new DummyListenSocket()); |
| 73 } | 67 } |
| 74 | 68 private: |
| 75 base::Closure quit_closure_1_; | 69 base::Closure quit_closure_1_; |
| 76 base::Closure quit_closure_2_; | 70 base::Closure quit_closure_2_; |
| 77 }; | 71 }; |
| 78 | 72 |
| 79 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 73 class DummyDelegate : public DevToolsHttpHandlerDelegate { |
| 80 public: | 74 public: |
| 81 virtual std::string GetDiscoveryPageHTML() OVERRIDE { return std::string(); } | 75 virtual std::string GetDiscoveryPageHTML() OVERRIDE { return std::string(); } |
| 82 | |
| 83 virtual bool BundlesFrontendResources() OVERRIDE { return true; } | 76 virtual bool BundlesFrontendResources() OVERRIDE { return true; } |
| 84 | |
| 85 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { | 77 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { |
| 86 return base::FilePath(); | 78 return base::FilePath(); |
| 87 } | 79 } |
| 88 | |
| 89 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { | 80 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { |
| 90 return std::string(); | 81 return std::string(); |
| 91 } | 82 } |
| 92 | |
| 93 virtual scoped_ptr<DevToolsTarget> CreateNewTarget(const GURL& url) OVERRIDE { | 83 virtual scoped_ptr<DevToolsTarget> CreateNewTarget(const GURL& url) OVERRIDE { |
| 94 return scoped_ptr<DevToolsTarget>(); | 84 return scoped_ptr<DevToolsTarget>(); |
| 95 } | 85 } |
| 96 | |
| 97 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { | 86 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { |
| 98 callback.Run(TargetList()); | 87 callback.Run(TargetList()); |
| 99 } | 88 } |
| 100 | |
| 101 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( | 89 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( |
| 102 net::StreamListenSocket::Delegate* delegate, | 90 net::StreamListenSocket::Delegate* delegate, |
| 103 std::string* name) OVERRIDE { | 91 std::string* name) OVERRIDE { |
| 104 return scoped_ptr<net::StreamListenSocket>(); | 92 return scoped_ptr<net::StreamListenSocket>(); |
| 105 } | 93 } |
| 106 }; | 94 }; |
| 107 | 95 |
| 108 } | 96 } |
| 109 | 97 |
| 110 class DevToolsHttpHandlerTest : public testing::Test { | 98 class DevToolsHttpHandlerTest : public testing::Test { |
| 111 public: | 99 public: |
| 112 DevToolsHttpHandlerTest() | 100 DevToolsHttpHandlerTest() |
| 113 : ui_thread_(BrowserThread::UI, &message_loop_) { | 101 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 114 } | 102 } |
| 115 | |
| 116 protected: | 103 protected: |
| 117 virtual void SetUp() { | 104 virtual void SetUp() { |
| 118 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); | 105 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
| 119 file_thread_->Start(); | 106 file_thread_->Start(); |
| 120 } | 107 } |
| 121 | |
| 122 virtual void TearDown() { | 108 virtual void TearDown() { |
| 123 file_thread_->Stop(); | 109 file_thread_->Stop(); |
| 124 } | 110 } |
| 125 | |
| 126 private: | 111 private: |
| 127 base::MessageLoopForIO message_loop_; | 112 base::MessageLoopForIO message_loop_; |
| 128 BrowserThreadImpl ui_thread_; | 113 BrowserThreadImpl ui_thread_; |
| 129 scoped_ptr<BrowserThreadImpl> file_thread_; | 114 scoped_ptr<BrowserThreadImpl> file_thread_; |
| 130 }; | 115 }; |
| 131 | 116 |
| 132 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 117 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
| 133 base::RunLoop run_loop, run_loop_2; | 118 base::RunLoop run_loop, run_loop_2; |
| 134 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | |
| 135 new DummyServerSocketFactory(run_loop.QuitClosure(), | |
| 136 run_loop_2.QuitClosure())); | |
| 137 content::DevToolsHttpHandler* devtools_http_handler_ = | 119 content::DevToolsHttpHandler* devtools_http_handler_ = |
| 138 content::DevToolsHttpHandler::Start(factory.Pass(), | 120 content::DevToolsHttpHandler::Start( |
| 139 std::string(), | 121 new DummyListenSocketFactory(run_loop.QuitClosure(), |
| 140 new DummyDelegate(), | 122 run_loop_2.QuitClosure()), |
| 141 base::FilePath()); | 123 std::string(), |
| 124 new DummyDelegate(), |
| 125 base::FilePath()); |
| 142 // Our dummy socket factory will post a quit message once the server will | 126 // Our dummy socket factory will post a quit message once the server will |
| 143 // become ready. | 127 // become ready. |
| 144 run_loop.Run(); | 128 run_loop.Run(); |
| 145 devtools_http_handler_->Stop(); | 129 devtools_http_handler_->Stop(); |
| 146 // Make sure the handler actually stops. | 130 // Make sure the handler actually stops. |
| 147 run_loop_2.Run(); | 131 run_loop_2.Run(); |
| 148 } | 132 } |
| 149 | 133 |
| 150 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 134 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
| 151 base::RunLoop run_loop, run_loop_2; | 135 base::RunLoop run_loop, run_loop_2; |
| 152 base::ScopedTempDir temp_dir; | 136 base::ScopedTempDir temp_dir; |
| 153 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 137 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 154 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | |
| 155 new DummyServerSocketFactory(run_loop.QuitClosure(), | |
| 156 run_loop_2.QuitClosure())); | |
| 157 content::DevToolsHttpHandler* devtools_http_handler_ = | 138 content::DevToolsHttpHandler* devtools_http_handler_ = |
| 158 content::DevToolsHttpHandler::Start(factory.Pass(), | 139 content::DevToolsHttpHandler::Start( |
| 159 std::string(), | 140 new DummyListenSocketFactory(run_loop.QuitClosure(), |
| 160 new DummyDelegate(), | 141 run_loop_2.QuitClosure()), |
| 161 temp_dir.path()); | 142 std::string(), |
| 143 new DummyDelegate(), |
| 144 temp_dir.path()); |
| 162 // Our dummy socket factory will post a quit message once the server will | 145 // Our dummy socket factory will post a quit message once the server will |
| 163 // become ready. | 146 // become ready. |
| 164 run_loop.Run(); | 147 run_loop.Run(); |
| 165 devtools_http_handler_->Stop(); | 148 devtools_http_handler_->Stop(); |
| 166 // Make sure the handler actually stops. | 149 // Make sure the handler actually stops. |
| 167 run_loop_2.Run(); | 150 run_loop_2.Run(); |
| 168 | 151 |
| 169 // Now make sure the DevToolsActivePort was written into the | 152 // Now make sure the DevToolsActivePort was written into the |
| 170 // temporary directory and its contents are as expected. | 153 // temporary directory and its contents are as expected. |
| 171 base::FilePath active_port_file = temp_dir.path().Append( | 154 base::FilePath active_port_file = temp_dir.path().Append( |
| 172 kDevToolsActivePortFileName); | 155 kDevToolsActivePortFileName); |
| 173 EXPECT_TRUE(base::PathExists(active_port_file)); | 156 EXPECT_TRUE(base::PathExists(active_port_file)); |
| 174 std::string file_contents; | 157 std::string file_contents; |
| 175 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 158 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
| 176 int port = 0; | 159 int port = 0; |
| 177 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 160 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
| 178 EXPECT_EQ(kDummyPort, port); | 161 EXPECT_EQ(kDummyPort, port); |
| 179 } | 162 } |
| 180 | 163 |
| 181 } // namespace content | 164 } // namespace content |
| OLD | NEW |