browser_test_base.h revision 58537e28ecd584eab876aee8be7156509866d23a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/callback.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/threading/thread.h"
11b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/test/spawned_test_server/spawned_test_server.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CommandLine;
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FilePath;
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace net {
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace test_server {
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class EmbeddedTestServer;
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BrowserTestBase : public testing::Test {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserTestBase();
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~BrowserTestBase();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We do this so we can be used in a Task.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AddRef() {}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Release() {}
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Configures everything for an in process browser test, then invokes
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BrowserMain. BrowserMain ends up invoking RunTestOnMainThreadLoop.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() OVERRIDE;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Restores state configured in SetUp.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() OVERRIDE;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Override this to add any custom setup code that needs to be done on the
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // main thread after the browser is created and just before calling
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // RunTestOnMainThread().
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpOnMainThread() {}
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Override this to add any custom teardown code that needs to be done on the
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // main thread right after RunTestOnMainThread().
51eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual void TearDownOnMainThread() {}
52eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Override this to add command line flags specific to your test.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) {}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We need these special methods because SetUp is the bottom of the stack
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that winds up calling your test method, so it is not always an option
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to do what you want by overriding it and calling the superclass version.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Override this for things you would normally override SetUp for. It will be
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // called before your individual test fixture method is run, but after most
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of the overhead initialization has occured.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpInProcessBrowserTestFixture() {}
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Override this for things you would normally override TearDown for.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDownInProcessBrowserTestFixture() {}
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Override this rather than TestBody.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RunTestOnMainThread() = 0;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is invoked from main after browser_init/browser_main have completed.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This prepares for the test by creating a new browser, runs the test
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (RunTestOnMainThread), quits the browsers and returns.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RunTestOnMainThreadLoop() = 0;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the testing server. Guaranteed to be non-NULL.
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // TODO(phajdan.jr): Remove test_server accessor (http://crbug.com/96594).
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  const net::SpawnedTestServer* test_server() const {
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return test_server_.get();
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  net::SpawnedTestServer* test_server() { return test_server_.get(); }
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns the embedded test server. Guaranteed to be non-NULL.
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const net::test_server::EmbeddedTestServer* embedded_test_server() const {
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return embedded_test_server_.get();
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  net::test_server::EmbeddedTestServer* embedded_test_server() {
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return embedded_test_server_.get();
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is only needed by a test that raises SIGTERM to ensure that a specific
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // codepath is taken.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DisableSIGTERMHandling() {
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    handle_sigterm_ = false;
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function is meant only for classes that directly derive from this
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // class to construct the test server in their constructor. They might need to
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // call this after setting up the paths. Actual test cases should never call
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |test_server_base| is the path, relative to src, to give to the test HTTP
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // server.
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CreateTestServer(const base::FilePath& test_server_base);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // When the test is running in --single-process mode, runs the given task on
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the in-process renderer thread. A nested message loop is run until it
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // returns.
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void PostTaskToInProcessRendererAndWait(const base::Closure& task);
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
113c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // Call this before SetUp() to use real GL contexts in Compositor for the
114c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // test.
115c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  void UseRealGLContexts() { allow_test_contexts_ = false; }
116c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
117c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // Call this before SetUp() to use real GL drivers instead of OSMesa for the
118c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // test.
119c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  void UseRealGLBindings() { allow_osmesa_ = false; }
120c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ProxyRunTestOnMainThreadLoop();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Testing server, started on demand.
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<net::SpawnedTestServer> test_server_;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Embedded test server, cheap to create, started on demand.
12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  base::Thread embedded_test_server_io_thread_;
12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  scoped_ptr<net::test_server::EmbeddedTestServer> embedded_test_server_;
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
131c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // When false, the ui::Compositor will be forced to use real GL contexts for
132c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // the test, so that it produces real pixel output.
133c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  bool allow_test_contexts_;
134c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
135c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // When false, the GL backend will use a real GPU. When true, it uses OSMesa
136c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  // to run GL on the CPU in a way that works across all platforms.
137c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch  bool allow_osmesa_;
138c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool handle_sigterm_;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_PUBLIC_TEST_BROWSER_TEST_BASE_H_
147