Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / test / browser_test_base.h
blobe323d8878176f3b5e891173f2ede349b9477b780
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.
5 #ifndef CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
6 #define CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
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"
14 namespace base {
15 class CommandLine;
16 class FilePath;
19 namespace net {
20 namespace test_server {
21 class EmbeddedTestServer;
24 class RuleBasedHostResolverProc;
25 } // namespace net
27 namespace content {
29 class BrowserTestBase : public testing::Test {
30 public:
31 BrowserTestBase();
32 ~BrowserTestBase() override;
34 // We do this so we can be used in a Task.
35 void AddRef() {}
36 void Release() {}
38 // Configures everything for an in process browser test, then invokes
39 // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop.
40 void SetUp() override;
42 // Restores state configured in SetUp.
43 void TearDown() override;
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() {}
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() {}
54 // Override this to add command line flags specific to your test.
55 virtual void SetUpCommandLine(base::CommandLine* command_line) {}
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();
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.
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() {}
73 // Override this for things you would normally override TearDown for.
74 virtual void TearDownInProcessBrowserTestFixture() {}
76 // Override this rather than TestBody.
77 virtual void RunTestOnMainThread() = 0;
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;
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; }
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();
92 net::SpawnedTestServer* test_server() { return test_server_.get(); }
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();
98 net::test_server::EmbeddedTestServer* embedded_test_server() {
99 return embedded_test_server_.get();
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;
108 #endif
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);
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);
123 // Call this before SetUp() to cause the test to generate pixel output.
124 void EnablePixelOutput();
126 // Call this before SetUp() to not use GL, but use software compositing
127 // instead.
128 void UseSoftwareCompositing();
130 // Returns true if the test will be using GL acceleration via OSMesa.
131 bool UsingOSMesa() const;
133 private:
134 void ProxyRunTestOnMainThreadLoop();
136 // Testing server, started on demand.
137 scoped_ptr<net::SpawnedTestServer> test_server_;
139 // Embedded test server, cheap to create, started on demand.
140 scoped_ptr<net::test_server::EmbeddedTestServer> embedded_test_server_;
142 // Host resolver used during tests.
143 scoped_refptr<net::RuleBasedHostResolverProc> rule_based_resolver_;
145 // Expected exit code (default is 0).
146 int expected_exit_code_;
148 // When true, the compositor will produce pixel output that can be read back
149 // for pixel tests.
150 bool enable_pixel_output_;
152 // When true, do compositing with the software backend instead of using GL.
153 bool use_software_compositing_;
155 #if defined(OS_POSIX)
156 bool handle_sigterm_;
157 #endif
160 } // namespace content
162 #endif // CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_