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