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