Commit 72623f81 authored by danakj's avatar danakj Committed by Commit Bot

Reset the user data dir in android_browsertests for each test.

Without this, state persists between tests, which of course causes
failures.

R=sky@chromium.org

Bug: 961849
Change-Id: Ie74936ebfed4bacfa721dcba446905f4fe770faa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1677591
Commit-Queue: danakj <danakj@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672669}
parent 49dbf987
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/test/base/android/android_browser_test.h" #include "chrome/test/base/android/android_browser_test.h"
#include "base/command_line.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h" #include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h" #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/test/base/test_launcher_utils.h" #include "chrome/test/base/test_launcher_utils.h"
...@@ -16,6 +17,7 @@ void AndroidBrowserTest::SetUp() { ...@@ -16,6 +17,7 @@ void AndroidBrowserTest::SetUp() {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
SetUpCommandLine(command_line); SetUpCommandLine(command_line);
SetUpDefaultCommandLine(command_line); SetUpDefaultCommandLine(command_line);
ASSERT_TRUE(test_launcher_utils::CreateUserDataDir(&temp_user_data_dir_));
BrowserTestBase::SetUp(); BrowserTestBase::SetUp();
} }
......
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#ifndef CHROME_TEST_BASE_ANDROID_ANDROID_BROWSER_TEST_H_ #ifndef CHROME_TEST_BASE_ANDROID_ANDROID_BROWSER_TEST_H_
#define CHROME_TEST_BASE_ANDROID_ANDROID_BROWSER_TEST_H_ #define CHROME_TEST_BASE_ANDROID_ANDROID_BROWSER_TEST_H_
#include "base/macros.h"
#include "base/files/scoped_temp_dir.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h" #include "content/public/test/browser_test_base.h"
class AndroidBrowserTest : public content::BrowserTestBase { class AndroidBrowserTest : public content::BrowserTestBase {
...@@ -23,6 +26,13 @@ class AndroidBrowserTest : public content::BrowserTestBase { ...@@ -23,6 +26,13 @@ class AndroidBrowserTest : public content::BrowserTestBase {
void SetUp() override; void SetUp() override;
void PreRunTestOnMainThread() override; void PreRunTestOnMainThread() override;
void PostRunTestOnMainThread() override; void PostRunTestOnMainThread() override;
private:
// Temporary user data directory. Used only when a user data directory is not
// specified in the command line.
base::ScopedTempDir temp_user_data_dir_;
DISALLOW_COPY_AND_ASSIGN(AndroidBrowserTest);
}; };
#endif // CHROME_TEST_BASE_ANDROID_ANDROID_BROWSER_TEST_H_ #endif // CHROME_TEST_BASE_ANDROID_ANDROID_BROWSER_TEST_H_
...@@ -212,7 +212,7 @@ void InProcessBrowserTest::SetUp() { ...@@ -212,7 +212,7 @@ void InProcessBrowserTest::SetUp() {
SetUpDefaultCommandLine(command_line); SetUpDefaultCommandLine(command_line);
// Create a temporary user data directory if required. // Create a temporary user data directory if required.
ASSERT_TRUE(CreateUserDataDirectory()) ASSERT_TRUE(test_launcher_utils::CreateUserDataDir(&temp_user_data_dir_))
<< "Could not create user data directory."; << "Could not create user data directory.";
// Allow subclasses the opportunity to make changes to the default user data // Allow subclasses the opportunity to make changes to the default user data
...@@ -312,23 +312,6 @@ void InProcessBrowserTest::SetUpDefaultCommandLine( ...@@ -312,23 +312,6 @@ void InProcessBrowserTest::SetUpDefaultCommandLine(
command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests); command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests);
} }
bool InProcessBrowserTest::CreateUserDataDirectory() {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
base::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_.GetPath();
} else {
LOG(ERROR) << "Could not create temporary user data directory \""
<< temp_user_data_dir_.GetPath().value() << "\".";
return false;
}
}
return test_launcher_utils::OverrideUserDataDir(user_data_dir);
}
void InProcessBrowserTest::TearDown() { void InProcessBrowserTest::TearDown() {
DCHECK(!g_browser_process); DCHECK(!g_browser_process);
#if defined(OS_WIN) #if defined(OS_WIN)
......
...@@ -258,10 +258,6 @@ class InProcessBrowserTest : public content::BrowserTestBase { ...@@ -258,10 +258,6 @@ class InProcessBrowserTest : public content::BrowserTestBase {
private: private:
void Initialize(); void Initialize();
// Creates a user data directory for the test if one is needed. Returns true
// if successful.
virtual bool CreateUserDataDirectory() WARN_UNUSED_RESULT;
// Quits all open browsers and waits until there are no more browsers. // Quits all open browsers and waits until there are no more browsers.
void QuitBrowsers(); void QuitBrowsers();
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/environment.h" #include "base/environment.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
...@@ -93,6 +94,23 @@ void RemoveCommandLineSwitch(const base::CommandLine& in_command_line, ...@@ -93,6 +94,23 @@ void RemoveCommandLineSwitch(const base::CommandLine& in_command_line,
} }
} }
bool CreateUserDataDir(base::ScopedTempDir* temp_dir) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
base::FilePath user_data_dir =
command_line->GetSwitchValuePath(switches::kUserDataDir);
if (user_data_dir.empty()) {
DCHECK(temp_dir);
if (temp_dir->CreateUniqueTempDir() && temp_dir->IsValid()) {
user_data_dir = temp_dir->GetPath();
} else {
LOG(ERROR) << "Could not create temporary user data directory \""
<< temp_dir->GetPath().value() << "\".";
return false;
}
}
return OverrideUserDataDir(user_data_dir);
}
bool OverrideUserDataDir(const base::FilePath& user_data_dir) { bool OverrideUserDataDir(const base::FilePath& user_data_dir) {
bool success = true; bool success = true;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace base { namespace base {
class CommandLine; class CommandLine;
class ScopedTempDir;
} }
// A set of utilities for test code that launches separate processes. // A set of utilities for test code that launches separate processes.
...@@ -34,9 +35,12 @@ void RemoveCommandLineSwitch(const base::CommandLine& in_command_line, ...@@ -34,9 +35,12 @@ void RemoveCommandLineSwitch(const base::CommandLine& in_command_line,
const std::string& switch_to_remove, const std::string& switch_to_remove,
base::CommandLine* out_command_line); base::CommandLine* out_command_line);
// Creates and overrides the current process' user data dir.
bool CreateUserDataDir(base::ScopedTempDir* temp_dir) WARN_UNUSED_RESULT;
// Overrides the current process' user data dir. // Overrides the current process' user data dir.
bool OverrideUserDataDir( bool OverrideUserDataDir(const base::FilePath& user_data_dir)
const base::FilePath& user_data_dir) WARN_UNUSED_RESULT; WARN_UNUSED_RESULT;
} // namespace test_launcher_utils } // namespace test_launcher_utils
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment