summaryrefslogtreecommitdiffstats
path: root/base/scoped_temp_dir_unittest.cc
diff options
context:
space:
mode:
authorthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-19 23:34:17 +0000
committerthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-19 23:34:17 +0000
commite078590661444968a2c49a3e5464413714471dca (patch)
tree13a4dbe4dedccc8057361bd86c89e065390247fc /base/scoped_temp_dir_unittest.cc
parentd9f76627b14170b95639619089139b6f0493ae8c (diff)
downloadchromium_src-e078590661444968a2c49a3e5464413714471dca.zip
chromium_src-e078590661444968a2c49a3e5464413714471dca.tar.gz
chromium_src-e078590661444968a2c49a3e5464413714471dca.tar.bz2
Move scoped_temp_dir and scoped_native_library back from base/memory to base.
It looks like they got moved accidentally in http://codereview.chromium.org/6714032 BUG=none TEST=none Review URL: http://codereview.chromium.org/7048007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86010 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/scoped_temp_dir_unittest.cc')
-rw-r--r--base/scoped_temp_dir_unittest.cc110
1 files changed, 110 insertions, 0 deletions
diff --git a/base/scoped_temp_dir_unittest.cc b/base/scoped_temp_dir_unittest.cc
new file mode 100644
index 0000000..72e410f
--- /dev/null
+++ b/base/scoped_temp_dir_unittest.cc
@@ -0,0 +1,110 @@
+// Copyright (c) 2011 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/file_util.h"
+#include "base/platform_file.h"
+#include "base/scoped_temp_dir.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(ScopedTempDir, FullPath) {
+ FilePath test_path;
+ file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"),
+ &test_path);
+
+ // Against an existing dir, it should get destroyed when leaving scope.
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+ {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.Set(test_path));
+ EXPECT_TRUE(dir.IsValid());
+ }
+ EXPECT_FALSE(file_util::DirectoryExists(test_path));
+
+ {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.Set(test_path));
+ // Now the dir doesn't exist, so ensure that it gets created.
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+ // When we call Release(), it shouldn't get destroyed when leaving scope.
+ FilePath path = dir.Take();
+ EXPECT_EQ(path.value(), test_path.value());
+ EXPECT_FALSE(dir.IsValid());
+ }
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+
+ // Clean up.
+ {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.Set(test_path));
+ }
+ EXPECT_FALSE(file_util::DirectoryExists(test_path));
+}
+
+TEST(ScopedTempDir, TempDir) {
+ // In this case, just verify that a directory was created and that it's a
+ // child of TempDir.
+ FilePath test_path;
+ {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ test_path = dir.path();
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+ FilePath tmp_dir;
+ EXPECT_TRUE(file_util::GetTempDir(&tmp_dir));
+ EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos);
+ }
+ EXPECT_FALSE(file_util::DirectoryExists(test_path));
+}
+
+TEST(ScopedTempDir, UniqueTempDirUnderPath) {
+ // Create a path which will contain a unique temp path.
+ FilePath base_path;
+ file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("base_dir"),
+ &base_path);
+
+ FilePath test_path;
+ {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
+ test_path = dir.path();
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+ EXPECT_TRUE(base_path.IsParent(test_path));
+ EXPECT_TRUE(test_path.value().find(base_path.value()) != std::string::npos);
+ }
+ EXPECT_FALSE(file_util::DirectoryExists(test_path));
+}
+
+TEST(ScopedTempDir, MultipleInvocations) {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(dir.CreateUniqueTempDir());
+ EXPECT_TRUE(dir.Delete());
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(dir.CreateUniqueTempDir());
+ ScopedTempDir other_dir;
+ EXPECT_TRUE(other_dir.Set(dir.Take()));
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(other_dir.CreateUniqueTempDir());
+}
+
+#if defined(OS_WIN)
+TEST(ScopedTempDir, LockedTempDir) {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ int file_flags = base::PLATFORM_FILE_CREATE_ALWAYS |
+ base::PLATFORM_FILE_WRITE;
+ base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
+ FilePath file_path(dir.path().Append(FILE_PATH_LITERAL("temp")));
+ base::PlatformFile file = base::CreatePlatformFile(file_path, file_flags,
+ NULL, &error_code);
+ EXPECT_NE(base::kInvalidPlatformFileValue, file);
+ EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
+ EXPECT_FALSE(dir.Delete()); // We should not be able to delete.
+ EXPECT_FALSE(dir.path().empty()); // We should still have a valid path.
+ EXPECT_TRUE(base::ClosePlatformFile(file));
+ // Now, we should be able to delete.
+ EXPECT_TRUE(dir.Delete());
+}
+#endif // defined(OS_WIN)