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
3 // found in the LICENSE file.
5 #include "base/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "content/browser/browser_thread_impl.h"
8 #include "content/public/browser/devtools_http_handler.h"
9 #include "content/public/browser/devtools_http_handler_delegate.h"
10 #include "net/socket/stream_listen_socket.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 using net::StreamListenSocket
;
18 class DummyListenSocket
: public StreamListenSocket
,
19 public StreamListenSocket::Delegate
{
22 : StreamListenSocket(0, this) {}
24 // StreamListenSocket::Delegate "implementation"
25 virtual void DidAccept(StreamListenSocket
* server
,
26 scoped_ptr
<StreamListenSocket
> connection
) OVERRIDE
{}
27 virtual void DidRead(StreamListenSocket
* connection
,
30 virtual void DidClose(StreamListenSocket
* sock
) OVERRIDE
{}
32 virtual ~DummyListenSocket() {}
33 virtual void Accept() OVERRIDE
{}
36 class DummyListenSocketFactory
: public net::StreamListenSocketFactory
{
38 DummyListenSocketFactory(
39 base::Closure quit_closure_1
, base::Closure quit_closure_2
)
40 : quit_closure_1_(quit_closure_1
), quit_closure_2_(quit_closure_2
) {}
41 virtual ~DummyListenSocketFactory() {
42 BrowserThread::PostTask(
43 BrowserThread::UI
, FROM_HERE
, quit_closure_2_
);
46 virtual scoped_ptr
<StreamListenSocket
> CreateAndListen(
47 StreamListenSocket::Delegate
* delegate
) const OVERRIDE
{
48 BrowserThread::PostTask(
49 BrowserThread::UI
, FROM_HERE
, quit_closure_1_
);
50 return scoped_ptr
<net::StreamListenSocket
>(new DummyListenSocket());
53 base::Closure quit_closure_1_
;
54 base::Closure quit_closure_2_
;
57 class DummyDelegate
: public DevToolsHttpHandlerDelegate
{
59 virtual std::string
GetDiscoveryPageHTML() OVERRIDE
{ return std::string(); }
60 virtual bool BundlesFrontendResources() OVERRIDE
{ return true; }
61 virtual base::FilePath
GetDebugFrontendDir() OVERRIDE
{
62 return base::FilePath();
64 virtual std::string
GetPageThumbnailData(const GURL
& url
) OVERRIDE
{
67 virtual RenderViewHost
* CreateNewTarget() OVERRIDE
{ return NULL
; }
68 virtual TargetType
GetTargetType(RenderViewHost
*) OVERRIDE
{
69 return kTargetTypeTab
;
71 virtual std::string
GetViewDescription(content::RenderViewHost
*) OVERRIDE
{
74 virtual scoped_ptr
<net::StreamListenSocket
> CreateSocketForTethering(
75 net::StreamListenSocket::Delegate
* delegate
,
76 std::string
* name
) OVERRIDE
{
77 return scoped_ptr
<net::StreamListenSocket
>();
83 class DevToolsHttpHandlerTest
: public testing::Test
{
85 DevToolsHttpHandlerTest()
86 : ui_thread_(BrowserThread::UI
, &message_loop_
) {
89 virtual void SetUp() {
90 file_thread_
.reset(new BrowserThreadImpl(BrowserThread::FILE));
91 file_thread_
->Start();
93 virtual void TearDown() {
97 base::MessageLoopForIO message_loop_
;
98 BrowserThreadImpl ui_thread_
;
99 scoped_ptr
<BrowserThreadImpl
> file_thread_
;
102 TEST_F(DevToolsHttpHandlerTest
, TestStartStop
) {
103 base::RunLoop run_loop
, run_loop_2
;
104 content::DevToolsHttpHandler
* devtools_http_handler_
=
105 content::DevToolsHttpHandler::Start(
106 new DummyListenSocketFactory(run_loop
.QuitClosure(),
107 run_loop_2
.QuitClosure()),
109 new DummyDelegate());
110 // Our dummy socket factory will post a quit message once the server will
113 devtools_http_handler_
->Stop();
114 // Make sure the handler actually stops.
118 } // namespace content