Commit 23040247 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Improve ThemeService unit test robustness, and clean up.

* Fixes some (but not all) "Could not delete temp dir" warnings by adding a new
  ThemeScoper object.  This is the bulk of this change.
* Fixes some tests that failed on sytems with dark mode on by default.
* Cleans up some tests by removing unnecessary bits (e.g.
  UseDefaultTheme()/RunUntilIdle() calls on test start) and refactoring (e.g.
  adding a |theme_service_| convenience member alongside the existing
  |registry_|).

Bug: none
Change-Id: Ie5c3fcccda294a863c6a598a0513f076788b314d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1986284
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarkylechar <kylechar@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#728675}
parent 10ebffd7
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h"
namespace base { namespace base {
...@@ -18,6 +19,16 @@ constexpr FilePath::CharType kScopedDirPrefix[] = ...@@ -18,6 +19,16 @@ constexpr FilePath::CharType kScopedDirPrefix[] =
ScopedTempDir::ScopedTempDir() = default; ScopedTempDir::ScopedTempDir() = default;
ScopedTempDir::ScopedTempDir(ScopedTempDir&& other) noexcept
: path_(other.Take()) {}
ScopedTempDir& ScopedTempDir::operator=(ScopedTempDir&& other) {
if (!path_.empty() && !Delete())
DLOG(WARNING) << "Could not delete temp dir in operator=().";
path_ = other.Take();
return *this;
}
ScopedTempDir::~ScopedTempDir() { ScopedTempDir::~ScopedTempDir() {
if (!path_.empty() && !Delete()) if (!path_.empty() && !Delete())
DLOG(WARNING) << "Could not delete temp dir in dtor."; DLOG(WARNING) << "Could not delete temp dir in dtor.";
...@@ -75,9 +86,7 @@ bool ScopedTempDir::Delete() { ...@@ -75,9 +86,7 @@ bool ScopedTempDir::Delete() {
} }
FilePath ScopedTempDir::Take() { FilePath ScopedTempDir::Take() {
FilePath ret = path_; return std::exchange(path_, FilePath());
path_ = FilePath();
return ret;
} }
const FilePath& ScopedTempDir::GetPath() const { const FilePath& ScopedTempDir::GetPath() const {
......
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
// intervening calls to Delete or Take, or the calls will fail. // intervening calls to Delete or Take, or the calls will fail.
#include "base/base_export.h" #include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/macros.h"
namespace base { namespace base {
...@@ -28,6 +28,9 @@ class BASE_EXPORT ScopedTempDir { ...@@ -28,6 +28,9 @@ class BASE_EXPORT ScopedTempDir {
// No directory is owned/created initially. // No directory is owned/created initially.
ScopedTempDir(); ScopedTempDir();
ScopedTempDir(ScopedTempDir&&) noexcept;
ScopedTempDir& operator=(ScopedTempDir&&);
// Recursively delete path. // Recursively delete path.
~ScopedTempDir(); ~ScopedTempDir();
...@@ -62,8 +65,6 @@ class BASE_EXPORT ScopedTempDir { ...@@ -62,8 +65,6 @@ class BASE_EXPORT ScopedTempDir {
private: private:
FilePath path_; FilePath path_;
DISALLOW_COPY_AND_ASSIGN(ScopedTempDir);
}; };
} // namespace base } // namespace base
......
...@@ -94,6 +94,20 @@ TEST(ScopedTempDir, MultipleInvocations) { ...@@ -94,6 +94,20 @@ TEST(ScopedTempDir, MultipleInvocations) {
EXPECT_FALSE(other_dir.CreateUniqueTempDir()); EXPECT_FALSE(other_dir.CreateUniqueTempDir());
} }
TEST(ScopedTempDir, Move) {
ScopedTempDir dir;
EXPECT_TRUE(dir.CreateUniqueTempDir());
FilePath dir_path = dir.GetPath();
EXPECT_TRUE(DirectoryExists(dir_path));
{
ScopedTempDir other_dir(std::move(dir));
EXPECT_EQ(dir_path, other_dir.GetPath());
EXPECT_TRUE(DirectoryExists(dir_path));
EXPECT_FALSE(dir.IsValid());
}
EXPECT_FALSE(DirectoryExists(dir_path));
}
#if defined(OS_WIN) #if defined(OS_WIN)
TEST(ScopedTempDir, LockedTempDir) { TEST(ScopedTempDir, LockedTempDir) {
ScopedTempDir dir; ScopedTempDir dir;
......
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