| 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/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "content/browser/browser_thread_impl.h" | 7 #include "content/browser/browser_thread_impl.h" |
| 8 #include "content/public/browser/devtools_http_handler.h" | 8 #include "content/public/browser/devtools_http_handler.h" |
| 9 #include "content/public/browser/devtools_http_handler_delegate.h" | 9 #include "content/public/browser/devtools_http_handler_delegate.h" |
| 10 #include "content/public/browser/devtools_target.h" | 10 #include "content/public/browser/devtools_target.h" |
| 11 #include "net/socket/stream_listen_socket.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/socket/server_socket.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace content { | 15 namespace content { |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 using net::StreamListenSocket; | 18 class DummyServerSocket : public net::ServerSocket { |
| 19 public: |
| 20 DummyServerSocket() {} |
| 18 | 21 |
| 19 class DummyListenSocket : public StreamListenSocket, | 22 // net::ServerSocket "implementation" |
| 20 public StreamListenSocket::Delegate { | 23 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE { |
| 21 public: | 24 return net::OK; |
| 22 DummyListenSocket() | 25 } |
| 23 : StreamListenSocket(net::kInvalidSocket, this) {} | 26 virtual int ListenWithAddressAndPort(const std::string& ip_address, |
| 24 | 27 int port, |
| 25 // StreamListenSocket::Delegate "implementation" | 28 int backlog) OVERRIDE { |
| 26 virtual void DidAccept(StreamListenSocket* server, | 29 return net::OK; |
| 27 scoped_ptr<StreamListenSocket> connection) OVERRIDE {} | 30 } |
| 28 virtual void DidRead(StreamListenSocket* connection, | 31 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { |
| 29 const char* data, | 32 return net::ERR_NOT_IMPLEMENTED; |
| 30 int len) OVERRIDE {} | 33 } |
| 31 virtual void DidClose(StreamListenSocket* sock) OVERRIDE {} | 34 virtual int Accept(scoped_ptr<net::StreamSocket>* socket, |
| 32 protected: | 35 const net::CompletionCallback& callback) OVERRIDE { |
| 33 virtual ~DummyListenSocket() {} | 36 return net::ERR_IO_PENDING; |
| 34 virtual void Accept() OVERRIDE {} | 37 } |
| 35 }; | 38 }; |
| 36 | 39 |
| 37 class DummyListenSocketFactory : public net::StreamListenSocketFactory { | 40 class DummyServerSocketFactory |
| 41 : public DevToolsHttpHandler::ServerSocketFactory { |
| 38 public: | 42 public: |
| 39 DummyListenSocketFactory( | 43 DummyServerSocketFactory(base::Closure quit_closure_1, |
| 40 base::Closure quit_closure_1, base::Closure quit_closure_2) | 44 base::Closure quit_closure_2) |
| 41 : quit_closure_1_(quit_closure_1), quit_closure_2_(quit_closure_2) {} | 45 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0), |
| 42 virtual ~DummyListenSocketFactory() { | 46 quit_closure_1_(quit_closure_1), |
| 47 quit_closure_2_(quit_closure_2) {} |
| 48 virtual ~DummyServerSocketFactory() { |
| 43 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
| 44 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 50 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
| 45 } | 51 } |
| 46 | 52 private: |
| 47 virtual scoped_ptr<StreamListenSocket> CreateAndListen( | 53 virtual scoped_ptr<net::ServerSocket> Create() const OVERRIDE { |
| 48 StreamListenSocket::Delegate* delegate) const OVERRIDE { | |
| 49 BrowserThread::PostTask( | 54 BrowserThread::PostTask( |
| 50 BrowserThread::UI, FROM_HERE, quit_closure_1_); | 55 BrowserThread::UI, FROM_HERE, quit_closure_1_); |
| 51 return scoped_ptr<net::StreamListenSocket>(new DummyListenSocket()); | 56 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); |
| 52 } | 57 } |
| 53 private: | |
| 54 base::Closure quit_closure_1_; | 58 base::Closure quit_closure_1_; |
| 55 base::Closure quit_closure_2_; | 59 base::Closure quit_closure_2_; |
| 56 }; | 60 }; |
| 57 | 61 |
| 58 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 62 class DummyDelegate : public DevToolsHttpHandlerDelegate { |
| 59 public: | 63 public: |
| 60 virtual std::string GetDiscoveryPageHTML() OVERRIDE { return std::string(); } | 64 virtual std::string GetDiscoveryPageHTML() OVERRIDE { return std::string(); } |
| 61 virtual bool BundlesFrontendResources() OVERRIDE { return true; } | 65 virtual bool BundlesFrontendResources() OVERRIDE { return true; } |
| 62 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { | 66 virtual base::FilePath GetDebugFrontendDir() OVERRIDE { |
| 63 return base::FilePath(); | 67 return base::FilePath(); |
| 64 } | 68 } |
| 65 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { | 69 virtual std::string GetPageThumbnailData(const GURL& url) OVERRIDE { |
| 66 return std::string(); | 70 return std::string(); |
| 67 } | 71 } |
| 68 virtual scoped_ptr<DevToolsTarget> CreateNewTarget(const GURL& url) OVERRIDE { | 72 virtual scoped_ptr<DevToolsTarget> CreateNewTarget(const GURL& url) OVERRIDE { |
| 69 return scoped_ptr<DevToolsTarget>(); | 73 return scoped_ptr<DevToolsTarget>(); |
| 70 } | 74 } |
| 71 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { | 75 virtual void EnumerateTargets(TargetCallback callback) OVERRIDE { |
| 72 callback.Run(TargetList()); | 76 callback.Run(TargetList()); |
| 73 } | 77 } |
| 74 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( | 78 virtual scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( |
| 75 net::StreamListenSocket::Delegate* delegate, | 79 net::StreamListenSocket::Delegate* delegate, |
| 76 std::string* name) OVERRIDE { | 80 std::string* name) OVERRIDE { |
| 77 return scoped_ptr<net::StreamListenSocket>(); | 81 return scoped_ptr<net::StreamListenSocket>(); |
| 78 } | 82 } |
| 79 }; | 83 }; |
| 80 | 84 |
| 81 } | 85 } |
| 82 | 86 |
| 83 class DevToolsHttpHandlerTest : public testing::Test { | 87 class DevToolsHttpHandlerTest : public testing::Test { |
| 84 public: | 88 public: |
| 85 DevToolsHttpHandlerTest() | 89 DevToolsHttpHandlerTest() |
| 86 : ui_thread_(BrowserThread::UI, &message_loop_) { | 90 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 87 } | 91 } |
| 88 protected: | 92 protected: |
| 89 virtual void SetUp() { | 93 virtual void SetUp() { |
| 90 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); | 94 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
| 91 file_thread_->Start(); | 95 file_thread_->Start(); |
| 92 } | 96 } |
| 93 virtual void TearDown() { | 97 virtual void TearDown() { |
| 94 file_thread_->Stop(); | 98 file_thread_->Stop(); |
| 95 } | 99 } |
| 96 private: | 100 private: |
| 97 base::MessageLoopForIO message_loop_; | 101 base::MessageLoopForIO message_loop_; |
| 98 BrowserThreadImpl ui_thread_; | 102 BrowserThreadImpl ui_thread_; |
| 99 scoped_ptr<BrowserThreadImpl> file_thread_; | 103 scoped_ptr<BrowserThreadImpl> file_thread_; |
| 100 }; | 104 }; |
| 101 | 105 |
| 102 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 106 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
| 103 base::RunLoop run_loop, run_loop_2; | 107 base::RunLoop run_loop, run_loop_2; |
| 108 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 109 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 110 run_loop_2.QuitClosure())); |
| 104 content::DevToolsHttpHandler* devtools_http_handler_ = | 111 content::DevToolsHttpHandler* devtools_http_handler_ = |
| 105 content::DevToolsHttpHandler::Start( | 112 content::DevToolsHttpHandler::Start(factory.Pass(), |
| 106 new DummyListenSocketFactory(run_loop.QuitClosure(), | 113 std::string(), |
| 107 run_loop_2.QuitClosure()), | 114 new DummyDelegate(), |
| 108 std::string(), | 115 base::FilePath()); |
| 109 new DummyDelegate(), | |
| 110 base::FilePath()); | |
| 111 // Our dummy socket factory will post a quit message once the server will | 116 // Our dummy socket factory will post a quit message once the server will |
| 112 // become ready. | 117 // become ready. |
| 113 run_loop.Run(); | 118 run_loop.Run(); |
| 114 devtools_http_handler_->Stop(); | 119 devtools_http_handler_->Stop(); |
| 115 // Make sure the handler actually stops. | 120 // Make sure the handler actually stops. |
| 116 run_loop_2.Run(); | 121 run_loop_2.Run(); |
| 117 } | 122 } |
| 118 | 123 |
| 119 } // namespace content | 124 } // namespace content |
| OLD | NEW |