diff options
-rw-r--r-- | chrome/chrome_tests.gypi | 2 | ||||
-rw-r--r-- | chrome/test/in_process_browser_test.cc | 47 | ||||
-rw-r--r-- | chrome/test/test_launcher/out_of_proc_test_runner.cc | 19 | ||||
-rw-r--r-- | chrome/test/test_launcher_utils.cc | 34 | ||||
-rw-r--r-- | chrome/test/test_launcher_utils.h | 20 | ||||
-rw-r--r-- | chrome/test/ui/ui_test.cc | 18 |
6 files changed, 103 insertions, 37 deletions
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e96c6b4..dbd9519 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -117,6 +117,8 @@ 'test/model_test_utils.h', 'test/profile_mock.h', 'test/test_browser_window.h', + 'test/test_launcher_utils.cc', + 'test/test_launcher_utils.h', 'test/test_location_bar.h', 'test/test_switches.cc', 'test/test_switches.h', diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc index 57d43fde..ad26580 100644 --- a/chrome/test/in_process_browser_test.cc +++ b/chrome/test/in_process_browser_test.cc @@ -9,6 +9,7 @@ #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" @@ -32,6 +33,7 @@ #include "chrome/common/notification_registrar.h" #include "chrome/common/notification_type.h" #include "chrome/common/url_constants.h" +#include "chrome/test/test_launcher_utils.h" #include "chrome/test/testing_browser_process.h" #include "chrome/test/ui_test_utils.h" #include "net/base/mock_host_resolver.h" @@ -43,6 +45,7 @@ #endif #if defined(OS_LINUX) +#include "base/environment.h" #include "base/singleton.h" #include "chrome/browser/renderer_host/render_sandbox_host_linux.h" #include "chrome/browser/zygote_host_linux.h" @@ -97,26 +100,33 @@ InProcessBrowserTest::~InProcessBrowserTest() { } void InProcessBrowserTest::SetUp() { - // Cleanup the user data dir. - FilePath user_data_dir; - PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); - ASSERT_LT(10, static_cast<int>(user_data_dir.value().size())) << - "The user data directory name passed into this test was too " - "short to delete safely. Please check the user-data-dir " - "argument and try again."; - ASSERT_TRUE(file_util::DieFileDie(user_data_dir, true)); - - // Recreate the user data dir. (PathService::Get guarantees that the directory - // exists if it returns true, but it only actually checks on the first call, - // the rest are cached. Thus we need to recreate it ourselves to not break - // the PathService guarantee.) - ASSERT_TRUE(file_util::CreateDirectory(user_data_dir)); + // Remember the command line. Normally this doesn't matter, because the test + // harness creates a new process for each test, but when the test harness is + // running in single process mode, we can't let one test's command-line + // changes (e.g. enabling DOM automation) affect other tests. + // TODO(phajdan.jr): This save/restore logic is unnecessary. Remove it. + 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)); + } // 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. delete g_browser_process; g_browser_process = NULL; + // Allow subclasses the opportunity to make changes to the default user data + // dir before running any tests. SetUpUserDataDirectory(); // Don't delete the resources when BrowserMain returns. Many ui classes @@ -124,13 +134,8 @@ void InProcessBrowserTest::SetUp() { // bundle we'll crash. browser_shutdown::delete_resources_on_shutdown = false; - // Remember the command line. Normally this doesn't matter, because the test - // harness creates a new process for each test, but when the test harness is - // running in single process mode, we can't let one test's command-line - // changes (e.g. enabling DOM automation) affect other tests. - CommandLine* command_line = CommandLine::ForCurrentProcessMutable(); - original_command_line_.reset(new CommandLine(*command_line)); - + // Allow subclasses the opportunity to make changes to the command line before + // running any tests. SetUpCommandLine(command_line); #if defined(OS_WIN) diff --git a/chrome/test/test_launcher/out_of_proc_test_runner.cc b/chrome/test/test_launcher/out_of_proc_test_runner.cc index 282ae4a..e588dc7 100644 --- a/chrome/test/test_launcher/out_of_proc_test_runner.cc +++ b/chrome/test/test_launcher/out_of_proc_test_runner.cc @@ -7,15 +7,17 @@ #include "base/command_line.h" #include "base/logging.h" #include "base/process_util.h" +#include "base/scoped_nsautorelease_pool.h" +#include "base/scoped_temp_dir.h" #include "base/string_number_conversions.h" #include "base/test/test_suite.h" +#include "chrome/common/chrome_switches.h" #include "chrome/test/test_launcher/test_runner.h" #include "chrome/test/unit/chrome_test_suite.h" #if defined(OS_WIN) #include "base/base_switches.h" #include "chrome/common/chrome_constants.h" -#include "chrome/common/chrome_switches.h" #include "chrome/common/sandbox_policy.h" #include "sandbox/src/dep.h" #include "sandbox/src/sandbox_factory.h" @@ -59,6 +61,10 @@ class OutOfProcTestRunner : public tests::TestRunner { // Returns true if the test succeeded, false if it failed. bool RunTest(const std::string& test_name) { + // Some of the below method calls will leak objects if there is no + // autorelease pool in place. + base::ScopedNSAutoreleasePool pool; + const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); CommandLine new_cmd_line(cmd_line->GetProgram()); CommandLine::SwitchMap switches = cmd_line->GetSwitches(); @@ -73,6 +79,9 @@ class OutOfProcTestRunner : public tests::TestRunner { // has been shut down and will actually crash). switches.erase(kGTestRepeatFlag); + // Strip out user-data-dir if present. We will add it back in again later. + switches.erase(switches::kUserDataDir); + for (CommandLine::SwitchMap::const_iterator iter = switches.begin(); iter != switches.end(); ++iter) { new_cmd_line.AppendSwitchNative((*iter).first, (*iter).second); @@ -88,6 +97,14 @@ class OutOfProcTestRunner : public tests::TestRunner { // failure status back to the parent. new_cmd_line.AppendSwitch(base::TestSuite::kStrictFailureHandling); + // Create a new user data dir and pass it to the child. + ScopedTempDir temp_dir; + if (!temp_dir.CreateUniqueTempDir() || !temp_dir.IsValid()) { + LOG(ERROR) << "Error creating temp profile directory"; + return false; + } + new_cmd_line.AppendSwitchPath(switches::kUserDataDir, temp_dir.path()); + base::ProcessHandle process_handle; #if defined(OS_POSIX) // On POSIX, we launch the test in a new process group with pgid equal to diff --git a/chrome/test/test_launcher_utils.cc b/chrome/test/test_launcher_utils.cc new file mode 100644 index 0000000..d5ccd18 --- /dev/null +++ b/chrome/test/test_launcher_utils.cc @@ -0,0 +1,34 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/environment.h" +#include "base/path_service.h" +#include "base/scoped_ptr.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/test/test_launcher_utils.h" + +namespace test_launcher_utils { + +bool OverrideUserDataDir(const FilePath& user_data_dir) { + bool success = true; + + // PathService::Override() is the best way to change the user data directory. + // This matches what is done in ChromeMain(). + success = PathService::Override(chrome::DIR_USER_DATA, user_data_dir); + +#if defined(OS_LINUX) + // Make sure the cache directory is inside our clear profile. Otherwise + // the cache may contain data from earlier tests that could break the + // current test. + // + // Note: we use an environment variable here, because we have to pass the + // value to the child process. This is the simplest way to do it. + scoped_ptr<base::Environment> env(base::Environment::Create()); + success = success && env->SetVar("XDG_CACHE_HOME", user_data_dir.value()); +#endif + + return success; +} + +} // namespace test_launcher_utils diff --git a/chrome/test/test_launcher_utils.h b/chrome/test/test_launcher_utils.h new file mode 100644 index 0000000..e83b519 --- /dev/null +++ b/chrome/test/test_launcher_utils.h @@ -0,0 +1,20 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_TEST_TEST_LAUNCHER_UTILS_H_ +#define CHROME_TEST_TEST_LAUNCHER_UTILS_H_ +#pragma once + +#include "base/compiler_specific.h" +#include "base/file_path.h" + +// A set of utilities for test code that launches separate processes. +namespace test_launcher_utils { + +// Overrides the current process' user data dir. +bool OverrideUserDataDir(const FilePath& user_data_dir) WARN_UNUSED_RESULT; + +} // namespace test_launcher_utils + +#endif // CHROME_TEST_TEST_LAUNCHER_UTILS_H_ diff --git a/chrome/test/ui/ui_test.cc b/chrome/test/ui/ui_test.cc index 8dcc3a3..9b0fd69 100644 --- a/chrome/test/ui/ui_test.cc +++ b/chrome/test/ui/ui_test.cc @@ -43,6 +43,7 @@ #include "chrome/test/automation/tab_proxy.h" #include "chrome/test/automation/window_proxy.h" #include "chrome/test/chrome_process_util.h" +#include "chrome/test/test_launcher_utils.h" #include "chrome/test/test_switches.h" #include "googleurl/src/gurl.h" #include "net/base/net_util.h" @@ -295,21 +296,8 @@ void UITestBase::LaunchBrowser(const CommandLine& arguments, temp_profile_dir_.reset(new ScopedTempDir()); ASSERT_TRUE(temp_profile_dir_->CreateUniqueTempDir()); - // Update the information about user data directory location on the ui_test - // side. Using PathService seems to be the most reliable, consistent way - // to do that. - ASSERT_TRUE(PathService::Override(chrome::DIR_USER_DATA, user_data_dir())); - -#if defined(OS_LINUX) - // Make sure the cache directory is inside our clear profile. Otherwise - // the cache may contain data from earlier tests that could break the - // current test. - // - // Note: we use an environment variable here, because we have to pass the - // value to the child process. This is the simplest way to do it. - scoped_ptr<base::Environment> env(base::Environment::Create()); - env->SetVar("XDG_CACHE_HOME", user_data_dir().value()); -#endif + ASSERT_TRUE( + test_launcher_utils::OverrideUserDataDir(temp_profile_dir_->path())); } if (!template_user_data_.empty()) { |