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