browser_test_base.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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 13class CommandLine; 14 15namespace base { 16class FilePath; 17} 18 19namespace net { 20namespace test_server { 21class EmbeddedTestServer; 22} 23} 24 25namespace content { 26 27class 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 command line flags specific to your test. 49 virtual void SetUpCommandLine(CommandLine* command_line) {} 50 51 protected: 52 // We need these special methods because SetUp is the bottom of the stack 53 // that winds up calling your test method, so it is not always an option 54 // to do what you want by overriding it and calling the superclass version. 55 // 56 // Override this for things you would normally override SetUp for. It will be 57 // called before your individual test fixture method is run, but after most 58 // of the overhead initialization has occured. 59 virtual void SetUpInProcessBrowserTestFixture() {} 60 61 // Override this for things you would normally override TearDown for. 62 virtual void TearDownInProcessBrowserTestFixture() {} 63 64 // Override this rather than TestBody. 65 virtual void RunTestOnMainThread() = 0; 66 67 // This is invoked from main after browser_init/browser_main have completed. 68 // This prepares for the test by creating a new browser, runs the test 69 // (RunTestOnMainThread), quits the browsers and returns. 70 virtual void RunTestOnMainThreadLoop() = 0; 71 72 // Returns the testing server. Guaranteed to be non-NULL. 73 // TODO(phajdan.jr): Remove test_server accessor (http://crbug.com/96594). 74 const net::SpawnedTestServer* test_server() const { 75 return test_server_.get(); 76 } 77 net::SpawnedTestServer* test_server() { return test_server_.get(); } 78 79 // Returns the embedded test server. Guaranteed to be non-NULL. 80 const net::test_server::EmbeddedTestServer* embedded_test_server() const { 81 return embedded_test_server_.get(); 82 } 83 net::test_server::EmbeddedTestServer* embedded_test_server() { 84 return embedded_test_server_.get(); 85 } 86 87#if defined(OS_POSIX) 88 // This is only needed by a test that raises SIGTERM to ensure that a specific 89 // codepath is taken. 90 void DisableSIGTERMHandling() { 91 handle_sigterm_ = false; 92 } 93#endif 94 95 // This function is meant only for classes that directly derive from this 96 // class to construct the test server in their constructor. They might need to 97 // call this after setting up the paths. Actual test cases should never call 98 // this. 99 // |test_server_base| is the path, relative to src, to give to the test HTTP 100 // server. 101 void CreateTestServer(const base::FilePath& test_server_base); 102 103 // When the test is running in --single-process mode, runs the given task on 104 // the in-process renderer thread. A nested message loop is run until it 105 // returns. 106 void PostTaskToInProcessRendererAndWait(const base::Closure& task); 107 108 private: 109 void ProxyRunTestOnMainThreadLoop(); 110 111 // Testing server, started on demand. 112 scoped_ptr<net::SpawnedTestServer> test_server_; 113 114 // Embedded test server, cheap to create, started on demand. 115 scoped_ptr<net::test_server::EmbeddedTestServer> embedded_test_server_; 116 117#if defined(OS_POSIX) 118 bool handle_sigterm_; 119#endif 120}; 121 122} // namespace content 123 124#endif // CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_ 125