diff options
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/in_process_browser_test.cc | 37 | ||||
-rw-r--r-- | chrome/test/in_process_browser_test.h | 20 |
2 files changed, 37 insertions, 20 deletions
diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc index c09902e..8536bf4 100644 --- a/chrome/test/in_process_browser_test.cc +++ b/chrome/test/in_process_browser_test.cc @@ -9,7 +9,6 @@ #include "base/file_util.h" #include "base/path_service.h" #include "base/scoped_nsautorelease_pool.h" -#include "base/scoped_temp_dir.h" #include "base/string_number_conversions.h" #include "base/test/test_file_util.h" #include "chrome/browser/browser.h" @@ -115,17 +114,9 @@ void InProcessBrowserTest::SetUp() { CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); original_command_line_.reset(new CommandLine(*command_line)); - // Update the information about user data directory location before calling - // BrowserMain(). In some cases there will be no --user-data-dir switch (for - // example, when debugging). If there is no switch, do nothing. - FilePath user_data_dir = - command_line->GetSwitchValuePath(switches::kUserDataDir); - if (user_data_dir.empty()) { - // TODO(rohitrao): Create a ScopedTempDir here if people have problems. - LOG(ERROR) << "InProcessBrowserTest is using the default user data dir."; - } else { - ASSERT_TRUE(test_launcher_utils::OverrideUserDataDir(user_data_dir)); - } + // Create a temporary user data directory if required. + ASSERT_TRUE(CreateUserDataDirectory()) + << "Could not create user data directory."; // The unit test suite creates a testingbrowser, but we want the real thing. // Delete the current one. We'll install the testing one in TearDown. @@ -134,7 +125,8 @@ void InProcessBrowserTest::SetUp() { // Allow subclasses the opportunity to make changes to the default user data // dir before running any tests. - SetUpUserDataDirectory(); + ASSERT_TRUE(SetUpUserDataDirectory()) + << "Could not set up user data directory."; // Don't delete the resources when BrowserMain returns. Many ui classes // cache SkBitmaps in a static field so that if we delete the resource @@ -154,8 +146,6 @@ void InProcessBrowserTest::SetUp() { if (dom_automation_enabled_) command_line->AppendSwitch(switches::kDomAutomationController); - command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir); - // This is a Browser test. command_line->AppendSwitchASCII(switches::kTestType, kBrowserTestType); @@ -247,6 +237,23 @@ void InProcessBrowserTest::SetUp() { TearDownInProcessBrowserTestFixture(); } +bool InProcessBrowserTest::CreateUserDataDirectory() { + CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); + FilePath user_data_dir = + command_line->GetSwitchValuePath(switches::kUserDataDir); + if (user_data_dir.empty()) { + if (temp_user_data_dir_.CreateUniqueTempDir() && + temp_user_data_dir_.IsValid()) { + user_data_dir = temp_user_data_dir_.path(); + } else { + LOG(ERROR) << "Could not create temporary user data directory \"" + << temp_user_data_dir_.path().value() << "\"."; + return false; + } + } + return test_launcher_utils::OverrideUserDataDir(user_data_dir); +} + void InProcessBrowserTest::TearDown() { // Reinstall testing browser process. delete g_browser_process; diff --git a/chrome/test/in_process_browser_test.h b/chrome/test/in_process_browser_test.h index fdccf57..67736f7 100644 --- a/chrome/test/in_process_browser_test.h +++ b/chrome/test/in_process_browser_test.h @@ -9,6 +9,7 @@ #include "base/compiler_specific.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" +#include "base/scoped_temp_dir.h" #include "net/test/test_server.h" #include "testing/gtest/include/gtest/gtest.h" @@ -69,11 +70,12 @@ class InProcessBrowserTest : public testing::Test { // Override this rather than TestBody. virtual void RunTestOnMainThread() = 0; - // Helper to initialize the user data directory. Called by SetUp() after - // erasing the user data directory, but before any browser is launched. - // If a test wishes to set up some initial non-empty state in the user - // data directory before the browser starts up, it can do so here. - virtual void SetUpUserDataDirectory() {}; + // Initializes the contents of the user data directory. Called by SetUp() + // after creating the user data directory, but before any browser is launched. + // If a test wishes to set up some initial non-empty state in the user data + // directory before the browser starts up, it can do so here. Returns true if + // successful. + virtual bool SetUpUserDataDirectory() WARN_UNUSED_RESULT { return true; } // We need these special methods because InProcessBrowserTest::SetUp is the // bottom of the stack that winds up calling your test method, so it is not @@ -123,6 +125,10 @@ class InProcessBrowserTest : public testing::Test { } private: + // Creates a user data directory for the test if one is needed. Returns true + // if successful. + virtual bool CreateUserDataDirectory() WARN_UNUSED_RESULT; + // This is invoked from main after browser_init/browser_main have completed. // This prepares for the test by creating a new browser, runs the test // (RunTestOnMainThread), quits the browsers and returns. @@ -159,6 +165,10 @@ class InProcessBrowserTest : public testing::Test { // Host resolver to use during the test. scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_; + // Temporary user data directory. Used only when a user data directory is not + // specified in the command line. + ScopedTempDir temp_user_data_dir_; + DISALLOW_COPY_AND_ASSIGN(InProcessBrowserTest); }; |