#include <string>
#include "include/cef_file_util.h"
#include "include/wrapper/cef_scoped_temp_dir.h"
#include "tests/gtest/include/gtest/gtest.h"
TEST(ScopedTempDir, FullPath) {
CefString test_path;
CefCreateNewTempDirectory("scoped_temp_dir", test_path);
EXPECT_TRUE(CefDirectoryExists(test_path));
{
CefScopedTempDir dir;
EXPECT_TRUE(dir.Set(test_path));
EXPECT_TRUE(dir.IsValid());
}
EXPECT_FALSE(CefDirectoryExists(test_path));
{
CefScopedTempDir dir;
EXPECT_TRUE(dir.Set(test_path));
EXPECT_TRUE(CefDirectoryExists(test_path));
CefString path = dir.Take();
EXPECT_STREQ(path.ToString().c_str(), test_path.ToString().c_str());
EXPECT_FALSE(dir.IsValid());
}
EXPECT_TRUE(CefDirectoryExists(test_path));
{
CefScopedTempDir dir;
EXPECT_TRUE(dir.Set(test_path));
}
EXPECT_FALSE(CefDirectoryExists(test_path));
}
TEST(ScopedTempDir, TempDir) {
CefString test_path;
{
CefScopedTempDir dir;
EXPECT_TRUE(dir.CreateUniqueTempDir());
test_path = dir.GetPath();
EXPECT_TRUE(CefDirectoryExists(test_path));
CefString tmp_dir;
EXPECT_TRUE(CefGetTempDirectory(tmp_dir));
EXPECT_TRUE(test_path.ToString().find(tmp_dir.ToString()) !=
std::string::npos);
}
EXPECT_FALSE(CefDirectoryExists(test_path));
}
TEST(ScopedTempDir, UniqueTempDirUnderPath) {
CefString base_path;
ASSERT_TRUE(CefCreateNewTempDirectory("base_dir", base_path));
CefString test_path;
{
CefScopedTempDir dir;
EXPECT_TRUE(dir.CreateUniqueTempDirUnderPath(base_path));
test_path = dir.GetPath();
EXPECT_TRUE(CefDirectoryExists(test_path));
EXPECT_TRUE(test_path.ToString().find(base_path.ToString()) == 0);
}
EXPECT_FALSE(CefDirectoryExists(test_path));
CefDeleteFile(base_path, true);
}
TEST(ScopedTempDir, MultipleInvocations) {
CefScopedTempDir dir;
EXPECT_TRUE(dir.CreateUniqueTempDir());
EXPECT_FALSE(dir.CreateUniqueTempDir());
EXPECT_TRUE(dir.Delete());
EXPECT_TRUE(dir.CreateUniqueTempDir());
EXPECT_FALSE(dir.CreateUniqueTempDir());
CefScopedTempDir other_dir;
EXPECT_TRUE(other_dir.Set(dir.Take()));
EXPECT_TRUE(dir.CreateUniqueTempDir());
EXPECT_FALSE(dir.CreateUniqueTempDir());
EXPECT_FALSE(other_dir.CreateUniqueTempDir());
}