Home | History | Annotate | Download | only in test
      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 #ifndef CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
      6 #define CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/compiler_specific.h"
     10 #include "net/test/spawned_test_server/spawned_test_server.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 class CommandLine;
     14 
     15 namespace base {
     16 class FilePath;
     17 }
     18 
     19 namespace net {
     20 namespace test_server {
     21 class EmbeddedTestServer;
     22 }
     23 }
     24 
     25 namespace content {
     26 
     27 class BrowserTestBase : public testing::Test {
     28  public:
     29   BrowserTestBase();
     30   virtual ~BrowserTestBase();
     31 
     32   // We do this so we can be used in a Task.
     33   void AddRef() {}
     34   void Release() {}
     35 
     36   // Configures everything for an in process browser test, then invokes
     37   // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop.
     38   virtual void SetUp() OVERRIDE;
     39 
     40   // Restores state configured in SetUp.
     41   virtual void TearDown() OVERRIDE;
     42 
     43   // Override this to add any custom setup code that needs to be done on the
     44   // main thread after the browser is created and just before calling
     45   // RunTestOnMainThread().
     46   virtual void SetUpOnMainThread() {}
     47 
     48   // Override this to add any custom teardown code that needs to be done on the
     49   // main thread right after RunTestOnMainThread().
     50   virtual void TearDownOnMainThread() {}
     51 
     52   // Override this to add command line flags specific to your test.
     53   virtual void SetUpCommandLine(CommandLine* command_line) {}
     54 
     55  protected:
     56   // We need these special methods because SetUp is the bottom of the stack
     57   // that winds up calling your test method, so it is not always an option
     58   // to do what you want by overriding it and calling the superclass version.
     59   //
     60   // Override this for things you would normally override SetUp for. It will be
     61   // called before your individual test fixture method is run, but after most
     62   // of the overhead initialization has occured.
     63   virtual void SetUpInProcessBrowserTestFixture() {}
     64 
     65   // Override this for things you would normally override TearDown for.
     66   virtual void TearDownInProcessBrowserTestFixture() {}
     67 
     68   // Override this rather than TestBody.
     69   virtual void RunTestOnMainThread() = 0;
     70 
     71   // This is invoked from main after browser_init/browser_main have completed.
     72   // This prepares for the test by creating a new browser, runs the test
     73   // (RunTestOnMainThread), quits the browsers and returns.
     74   virtual void RunTestOnMainThreadLoop() = 0;
     75 
     76   // Returns the testing server. Guaranteed to be non-NULL.
     77   // TODO(phajdan.jr): Remove test_server accessor (http://crbug.com/96594).
     78   const net::SpawnedTestServer* test_server() const {
     79     return test_server_.get();
     80   }
     81   net::SpawnedTestServer* test_server() { return test_server_.get(); }
     82 
     83   // Returns the embedded test server. Guaranteed to be non-NULL.
     84   const net::test_server::EmbeddedTestServer* embedded_test_server() const {
     85     return embedded_test_server_.get();
     86   }
     87   net::test_server::EmbeddedTestServer* embedded_test_server() {
     88     return embedded_test_server_.get();
     89   }
     90 
     91 #if defined(OS_POSIX)
     92   // This is only needed by a test that raises SIGTERM to ensure that a specific
     93   // codepath is taken.
     94   void DisableSIGTERMHandling() {
     95     handle_sigterm_ = false;
     96   }
     97 #endif
     98 
     99   // This function is meant only for classes that directly derive from this
    100   // class to construct the test server in their constructor. They might need to
    101   // call this after setting up the paths. Actual test cases should never call
    102   // this.
    103   // |test_server_base| is the path, relative to src, to give to the test HTTP
    104   // server.
    105   void CreateTestServer(const base::FilePath& test_server_base);
    106 
    107   // When the test is running in --single-process mode, runs the given task on
    108   // the in-process renderer thread. A nested message loop is run until it
    109   // returns.
    110   void PostTaskToInProcessRendererAndWait(const base::Closure& task);
    111 
    112   // Call this before SetUp() to use real GL contexts in Compositor for the
    113   // test.
    114   void UseRealGLContexts() { allow_test_contexts_ = false; }
    115 
    116   // Call this before SetUp() to use real GL drivers instead of OSMesa for the
    117   // test.
    118   void UseRealGLBindings() { allow_osmesa_ = false; }
    119 
    120  private:
    121   void ProxyRunTestOnMainThreadLoop();
    122 
    123   // Testing server, started on demand.
    124   scoped_ptr<net::SpawnedTestServer> test_server_;
    125 
    126   // Embedded test server, cheap to create, started on demand.
    127   scoped_ptr<net::test_server::EmbeddedTestServer> embedded_test_server_;
    128 
    129   // When false, the ui::Compositor will be forced to use real GL contexts for
    130   // the test, so that it produces real pixel output.
    131   bool allow_test_contexts_;
    132 
    133   // When false, the GL backend will use a real GPU. When true, it uses OSMesa
    134   // to run GL on the CPU in a way that works across all platforms.
    135   bool allow_osmesa_;
    136 
    137 #if defined(OS_POSIX)
    138   bool handle_sigterm_;
    139 #endif
    140 };
    141 
    142 }  // namespace content
    143 
    144 #endif  // CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
    145