| 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/stream_listen_socket.h" | 16 #include "net/socket/server_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 using net::StreamListenSocket; | 26 class DummyServerSocket : public net::ServerSocket { |
| 27 public: |
| 28 DummyServerSocket() {} |
| 27 | 29 |
| 28 class DummyListenSocket : public StreamListenSocket, | 30 // net::ServerSocket "implementation" |
| 29 public StreamListenSocket::Delegate { | 31 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE { |
| 30 public: | 32 return net::OK; |
| 31 DummyListenSocket() | 33 } |
| 32 : StreamListenSocket(net::kInvalidSocket, this) {} | 34 virtual int ListenWithAddressAndPort(const std::string& ip_address, |
| 33 | 35 int port, |
| 34 // StreamListenSocket::Delegate "implementation" | 36 int backlog) OVERRIDE { |
| 35 virtual void DidAccept(StreamListenSocket* server, | 37 return net::OK; |
| 36 scoped_ptr<StreamListenSocket> connection) OVERRIDE {} | 38 } |
| 37 virtual void DidRead(StreamListenSocket* connection, | 39 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { |
| 38 const char* data, | |
| 39 int len) OVERRIDE {} | |
| 40 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} | |
| 41 protected: | |
| 42 virtual ~DummyListenSocket() {} | |
| 43 virtual void Accept() OVERRIDE {} | |
| 44 virtual int GetLocalAddress(net::IPEndPoint* address) OVERRIDE { | |
| 45 net::IPAddressNumber number; | 40 net::IPAddressNumber number; |
| 46 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &number)); | 41 EXPECT_TRUE(net::ParseIPLiteralToNumber("127.0.0.1", &number)); |
| 47 *address = net::IPEndPoint(number, kDummyPort); | 42 *address = net::IPEndPoint(number, kDummyPort); |
| 48 return net::OK; | 43 return net::OK; |
| 49 } | 44 } |
| 45 virtual int Accept(scoped_ptr<net::StreamSocket>* socket, |
| 46 const net::CompletionCallback& callback) OVERRIDE { |
| 47 return net::ERR_IO_PENDING; |
| 48 } |
| 50 }; | 49 }; |
| 51 | 50 |
| 52 class DummyListenSocketFactory : public net::StreamListenSocketFactory { | 51 class DummyServerSocketFactory |
| 52 : public DevToolsHttpHandler::ServerSocketFactory { |
| 53 public: | 53 public: |
| 54 DummyListenSocketFactory( | 54 DummyServerSocketFactory(base::Closure quit_closure_1, |
| 55 base::Closure quit_closure_1, base::Closure quit_closure_2) | 55 base::Closure quit_closure_2) |
| 56 : quit_closure_1_(quit_closure_1), quit_closure_2_(quit_closure_2) {} | 56 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0), |
| 57 virtual ~DummyListenSocketFactory() { | 57 quit_closure_1_(quit_closure_1), |
| 58 quit_closure_2_(quit_closure_2) {} |
| 59 virtual ~DummyServerSocketFactory() { |
| 58 BrowserThread::PostTask( | 60 BrowserThread::PostTask( |
| 59 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 61 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
| 60 } | 62 } |
| 61 | 63 private: |
| 62 virtual scoped_ptr<StreamListenSocket> CreateAndListen( | 64 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE { |
| 63 StreamListenSocket::Delegate* delegate) const OVERRIDE { | |
| 64 BrowserThread::PostTask( | 65 BrowserThread::PostTask( |
| 65 BrowserThread::UI, FROM_HERE, quit_closure_1_); | 66 BrowserThread::UI, FROM_HERE, quit_closure_1_); |
| 66 return scoped_ptr<net::StreamListenSocket>(new DummyListenSocket()); | 67 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); |
| 67 } | 68 } |
| 68 private: | |
| 69 base::Closure quit_closure_1_; | 69 base::Closure quit_closure_1_; |
| 70 base::Closure quit_closure_2_; | 70 base::Closure quit_closure_2_; |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 73 class DummyDelegate : public DevToolsHttpHandlerDelegate { |
| 74 public: | 74 public: |
| 75 virtual std::string GetDiscoveryPageHTML() OVERRIDE { return std::string(); } | 75 virtual std::string GetDiscoveryPageHTML() OVERRIDE { return std::string(); } |
| 76 virtual bool BundlesFrontendResources() OVERRIDE { return true; } | 76 virtual bool BundlesFrontendResources() OVERRIDE { return true; } |
| 77 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { | 77 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { |
| 78 return base::FilePath(); | 78 return base::FilePath(); |
| 79 } | 79 } |
| 80 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { | 80 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { |
| 81 return std::string(); | 81 return std::string(); |
| 82 } | 82 } |
| 83 virtual scoped_ptr<DevToolsTarget> CreateNewTarget(const GURL& url) OVERRIDE { | 83 virtual scoped_ptr<DevToolsTarget> CreateNewTarget(const GURL& url) OVERRIDE { |
| 84 return scoped_ptr<DevToolsTarget>(); | 84 return scoped_ptr<DevToolsTarget>(); |
| 85 } | 85 } |
| 86 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { | 86 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { |
| 87 callback.Run(TargetList()); | 87 callback.Run(TargetList()); |
| 88 } | 88 } |
| 89 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( | 89 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( |
| 90 net::StreamListenSocket::Delegate* delegate, | 90 net::StreamListenSocket::Delegate* delegate, |
| 91 std::string* name) OVERRIDE { | 91 std::string* name) OVERRIDE { |
| 92 return scoped_ptr<net::StreamListenSocket>(); | 92 return scoped_ptr<net::StreamListenSocket>(); |
| 93 } | 93 } |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 } | 96 } |
| 97 | 97 |
| 98 class DevToolsHttpHandlerTest : public testing::Test { | 98 class DevToolsHttpHandlerTest : public testing::Test { |
| 99 public: | 99 public: |
| 100 DevToolsHttpHandlerTest() | 100 DevToolsHttpHandlerTest() |
| 101 : ui_thread_(BrowserThread::UI, &message_loop_) { | 101 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 102 } | 102 } |
| 103 protected: | 103 protected: |
| 104 virtual void SetUp() { | 104 virtual void SetUp() { |
| 105 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); | 105 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
| 106 file_thread_->Start(); | 106 file_thread_->Start(); |
| 107 } | 107 } |
| 108 virtual void TearDown() { | 108 virtual void TearDown() { |
| 109 file_thread_->Stop(); | 109 file_thread_->Stop(); |
| 110 } | 110 } |
| 111 private: | 111 private: |
| 112 base::MessageLoopForIO message_loop_; | 112 base::MessageLoopForIO message_loop_; |
| 113 BrowserThreadImpl ui_thread_; | 113 BrowserThreadImpl ui_thread_; |
| 114 scoped_ptr<BrowserThreadImpl> file_thread_; | 114 scoped_ptr<BrowserThreadImpl> file_thread_; |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 117 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
| 118 base::RunLoop run_loop, run_loop_2; | 118 base::RunLoop run_loop, run_loop_2; |
| 119 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 120 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 121 run_loop_2.QuitClosure())); |
| 119 content::DevToolsHttpHandler* devtools_http_handler_ = | 122 content::DevToolsHttpHandler* devtools_http_handler_ = |
| 120 content::DevToolsHttpHandler::Start( | 123 content::DevToolsHttpHandler::Start(factory.Pass(), |
| 121 new DummyListenSocketFactory(run_loop.QuitClosure(), | 124 std::string(), |
| 122 run_loop_2.QuitClosure()), | 125 new DummyDelegate(), |
| 123 std::string(), | 126 base::FilePath()); |
| 124 new DummyDelegate(), | |
| 125 base::FilePath()); | |
| 126 // Our dummy socket factory will post a quit message once the server will | 127 // Our dummy socket factory will post a quit message once the server will |
| 127 // become ready. | 128 // become ready. |
| 128 run_loop.Run(); | 129 run_loop.Run(); |
| 129 devtools_http_handler_->Stop(); | 130 devtools_http_handler_->Stop(); |
| 130 // Make sure the handler actually stops. | 131 // Make sure the handler actually stops. |
| 131 run_loop_2.Run(); | 132 run_loop_2.Run(); |
| 132 } | 133 } |
| 133 | 134 |
| 134 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 135 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
| 135 base::RunLoop run_loop, run_loop_2; | 136 base::RunLoop run_loop, run_loop_2; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 155 kDevToolsActivePortFileName); | 156 kDevToolsActivePortFileName); |
| 156 EXPECT_TRUE(base::PathExists(active_port_file)); | 157 EXPECT_TRUE(base::PathExists(active_port_file)); |
| 157 std::string file_contents; | 158 std::string file_contents; |
| 158 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 159 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
| 159 int port = 0; | 160 int port = 0; |
| 160 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 161 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
| 161 EXPECT_EQ(kDummyPort, port); | 162 EXPECT_EQ(kDummyPort, port); |
| 162 } | 163 } |
| 163 | 164 |
| 164 } // namespace content | 165 } // namespace content |
| OLD | NEW |