diff options
author | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-27 00:45:36 +0000 |
---|---|---|
committer | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-27 00:45:36 +0000 |
commit | d3e6d251d0c24fb352246fdbb926f16e4ed335ba (patch) | |
tree | e8d6f7e14daf1d9d797c47acfb165ebc9056fe33 | |
parent | 8b9af6bb0a2e0b46f361bd0ffdc4d5d0dc76544e (diff) | |
download | chromium_src-d3e6d251d0c24fb352246fdbb926f16e4ed335ba.zip chromium_src-d3e6d251d0c24fb352246fdbb926f16e4ed335ba.tar.gz chromium_src-d3e6d251d0c24fb352246fdbb926f16e4ed335ba.tar.bz2 |
Add ScopedPathOverride to the unit test suite.
Unit tests have to restore the Overrdies they make because they all live in the same
process and not cleaning up their mess means other tests will run against possibly
modified profiles. This class allows tests to set up path overrides and clean them up
in a convenient way and makes sure the global path map stays clean afterwards.
BUG=149161
TEST=unit_tests still pass.
Review URL: https://chromiumcodereview.appspot.com/10948010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@158951 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/path_service.h | 5 | ||||
-rw-r--r-- | base/test/scoped_path_override.cc | 30 | ||||
-rw-r--r-- | base/test/scoped_path_override.h | 34 |
4 files changed, 71 insertions, 0 deletions
diff --git a/base/base.gyp b/base/base.gyp index c33eff1..0904813 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -720,6 +720,8 @@ 'test/perf_test_suite.h', 'test/scoped_locale.cc', 'test/scoped_locale.h', + 'test/scoped_path_override.cc', + 'test/scoped_path_override.h', 'test/sequenced_task_runner_test_template.cc', 'test/sequenced_task_runner_test_template.h', 'test/task_runner_test_template.cc', diff --git a/base/path_service.h b/base/path_service.h index 40da55a..94b0db3 100644 --- a/base/path_service.h +++ b/base/path_service.h @@ -14,6 +14,10 @@ class FilePath; +namespace base { +class ScopedPathOverride; +} // namespace + // The path service is a global table mapping keys to file system paths. It is // OK to use this service from multiple threads. // @@ -64,6 +68,7 @@ class BASE_EXPORT PathService { int key_end); private: + friend class base::ScopedPathOverride; FRIEND_TEST_ALL_PREFIXES(PathServiceTest, RemoveOverride); // Removes an override for a special directory or file. Returns true if there diff --git a/base/test/scoped_path_override.cc b/base/test/scoped_path_override.cc new file mode 100644 index 0000000..bb38c7f --- /dev/null +++ b/base/test/scoped_path_override.cc @@ -0,0 +1,30 @@ +// Copyright (c) 2012 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/test/scoped_path_override.h" + +#include "base/logging.h" +#include "base/path_service.h" + +namespace base { + +ScopedPathOverride::ScopedPathOverride(int key) : key_(key) { + bool result = temp_dir_.CreateUniqueTempDir(); + CHECK(result); + result = PathService::Override(key, temp_dir_.path()); + CHECK(result); +} + +ScopedPathOverride::ScopedPathOverride(int key, const FilePath& dir) + : key_(key) { + bool result = PathService::Override(key, dir); + CHECK(result); +} + +ScopedPathOverride::~ScopedPathOverride() { + bool result = PathService::RemoveOverride(key_); + CHECK(result) << "The override seems to have been removed already!"; +} + +} // namespace base diff --git a/base/test/scoped_path_override.h b/base/test/scoped_path_override.h new file mode 100644 index 0000000..ee990a29 --- /dev/null +++ b/base/test/scoped_path_override.h @@ -0,0 +1,34 @@ +// Copyright (c) 2012 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 BASE_TEST_SCOPED_PATH_OVERRIDE_H_ +#define BASE_TEST_SCOPED_PATH_OVERRIDE_H_ + +#include "base/basictypes.h" +#include "base/scoped_temp_dir.h" + +class FilePath; + +namespace base { + +// Sets the given |locale| on construction, and restores the previous locale +// on destruction. This class is intended to be used by tests that need to +// override paths to ensure their overrides are properly handled and reverted +// when the scope of the test is left. +class ScopedPathOverride { + public: + explicit ScopedPathOverride(int key); + ScopedPathOverride(int key, const FilePath& dir); + ~ScopedPathOverride(); + + private: + int key_; + ScopedTempDir temp_dir_; + + DISALLOW_COPY_AND_ASSIGN(ScopedPathOverride); +}; + +} // namespace base + +#endif // BASE_TEST_SCOPED_PATH_OVERRIDE_H_ |