browser_test_base.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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 content {
20
21class BrowserTestBase : public testing::Test {
22 public:
23  BrowserTestBase();
24  virtual ~BrowserTestBase();
25
26  // We do this so we can be used in a Task.
27  void AddRef() {}
28  void Release() {}
29
30  // Configures everything for an in process browser test, then invokes
31  // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop.
32  virtual void SetUp() OVERRIDE;
33
34  // Restores state configured in SetUp.
35  virtual void TearDown() OVERRIDE;
36
37  // Override this to add any custom setup code that needs to be done on the
38  // main thread after the browser is created and just before calling
39  // RunTestOnMainThread().
40  virtual void SetUpOnMainThread() {}
41
42  // Override this to add command line flags specific to your test.
43  virtual void SetUpCommandLine(CommandLine* command_line) {}
44
45 protected:
46  // We need these special methods because SetUp is the bottom of the stack
47  // that winds up calling your test method, so it is not always an option
48  // to do what you want by overriding it and calling the superclass version.
49  //
50  // Override this for things you would normally override SetUp for. It will be
51  // called before your individual test fixture method is run, but after most
52  // of the overhead initialization has occured.
53  virtual void SetUpInProcessBrowserTestFixture() {}
54
55  // Override this for things you would normally override TearDown for.
56  virtual void TearDownInProcessBrowserTestFixture() {}
57
58  // Override this rather than TestBody.
59  virtual void RunTestOnMainThread() = 0;
60
61  // This is invoked from main after browser_init/browser_main have completed.
62  // This prepares for the test by creating a new browser, runs the test
63  // (RunTestOnMainThread), quits the browsers and returns.
64  virtual void RunTestOnMainThreadLoop() = 0;
65
66  // Returns the testing server. Guaranteed to be non-NULL.
67  const net::SpawnedTestServer* test_server() const {
68    return test_server_.get();
69  }
70  net::SpawnedTestServer* test_server() { return test_server_.get(); }
71
72#if defined(OS_POSIX)
73  // This is only needed by a test that raises SIGTERM to ensure that a specific
74  // codepath is taken.
75  void DisableSIGTERMHandling() {
76    handle_sigterm_ = false;
77  }
78#endif
79
80  // This function is meant only for classes that directly derive from this
81  // class to construct the test server in their constructor. They might need to
82  // call this after setting up the paths. Actual test cases should never call
83  // this.
84  // |test_server_base| is the path, relative to src, to give to the test HTTP
85  // server.
86  void CreateTestServer(const base::FilePath& test_server_base);
87
88  // When the test is running in --single-process mode, runs the given task on
89  // the in-process renderer thread. A nested message loop is run until it
90  // returns.
91  void PostTaskToInProcessRendererAndWait(const base::Closure& task);
92
93 private:
94  void ProxyRunTestOnMainThreadLoop();
95
96  // Testing server, started on demand.
97  scoped_ptr<net::SpawnedTestServer> test_server_;
98
99#if defined(OS_POSIX)
100  bool handle_sigterm_;
101#endif
102};
103
104}  // namespace content
105
106#endif  // CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
107