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