Commit 8a86199b authored by mlcui's avatar mlcui Committed by Chromium LUCI CQ

Restore original override in base::ScopedPathOverride

This affects ui/base unit tests, where the test suite overrides
ui::DIR_LOCALES in its initialization function. Any ScopedPathOverride
which overrides ui::DIR_LOCALES will cause it to reset to the original
ui::DIR_LOCALES value - not the override in the test suite
initialization - which might cause order dependent tests.

Fixed: 1153235
Change-Id: I03981980a3e0c6ab2596989a2e499ec237fce206
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2563013
Commit-Queue: Michael Cui <mlcui@google.com>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833181}
parent 36c74050
......@@ -302,6 +302,16 @@ bool PathService::RemoveOverride(int key) {
return true;
}
// static
bool PathService::IsOverriddenForTests(int key) {
PathData* path_data = GetPathData();
DCHECK(path_data);
AutoLock scoped_lock(path_data->lock);
return path_data->overrides.find(key) != path_data->overrides.end();
}
// static
void PathService::RegisterProvider(ProviderFunc func, int key_start,
int key_end) {
......
......@@ -86,6 +86,9 @@ class BASE_EXPORT PathService {
// was an override to remove or false if none was present.
// NOTE: This function is intended to be used by tests only!
static bool RemoveOverride(int key);
// Returns whether an override is present for a special directory or file.
static bool IsOverriddenForTests(int key);
};
} // namespace base
......
......@@ -10,6 +10,7 @@
namespace base {
ScopedPathOverride::ScopedPathOverride(int key) : key_(key) {
SaveOriginal();
bool result = temp_dir_.CreateUniqueTempDir();
CHECK(result);
result = PathService::Override(key, temp_dir_.GetPath());
......@@ -18,6 +19,7 @@ ScopedPathOverride::ScopedPathOverride(int key) : key_(key) {
ScopedPathOverride::ScopedPathOverride(int key, const base::FilePath& dir)
: key_(key) {
SaveOriginal();
bool result = PathService::Override(key, dir);
CHECK(result);
}
......@@ -27,14 +29,31 @@ ScopedPathOverride::ScopedPathOverride(int key,
bool is_absolute,
bool create)
: key_(key) {
SaveOriginal();
bool result =
PathService::OverrideAndCreateIfNeeded(key, path, is_absolute, create);
CHECK(result);
}
void ScopedPathOverride::SaveOriginal() {
if (PathService::IsOverriddenForTests(key_)) {
original_override_ = PathService::CheckedGet(key_);
}
}
ScopedPathOverride::~ScopedPathOverride() {
bool result = PathService::RemoveOverride(key_);
CHECK(result) << "The override seems to have been removed already!";
if (original_override_) {
// PathService::Override, by default, does some (blocking) checks to ensure
// that the path is absolute and exists. As the original override must have
// already gone through these checks, we can skip these checks here.
// This is needed for some tests which use ScopedPathOverride in scopes
// that disallow blocking.
result = PathService::OverrideAndCreateIfNeeded(
key_, *original_override_, /*is_absolute=*/true, /*create=*/false);
CHECK(result);
}
}
} // namespace base
......@@ -7,6 +7,7 @@
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/optional.h"
namespace base {
......@@ -32,8 +33,12 @@ class ScopedPathOverride {
~ScopedPathOverride();
private:
// Used for saving original_override_ when an override already exists.
void SaveOriginal();
int key_;
ScopedTempDir temp_dir_;
base::Optional<FilePath> original_override_;
DISALLOW_COPY_AND_ASSIGN(ScopedPathOverride);
};
......
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