2016-11-15 18:56:02 +01:00
|
|
|
// Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
|
|
|
|
// 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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "tests/shared/browser/file_util.h"
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "include/wrapper/cef_scoped_temp_dir.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
TEST(FileUtil, JoinPath) {
|
|
|
|
// Should return whichever path component is non-empty.
|
2017-08-04 00:55:19 +02:00
|
|
|
EXPECT_STREQ("", client::file_util::JoinPath("", "").c_str());
|
|
|
|
EXPECT_STREQ("path1", client::file_util::JoinPath("path1", "").c_str());
|
|
|
|
EXPECT_STREQ("path2", client::file_util::JoinPath("", "path2").c_str());
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
const std::string& expected =
|
2017-08-04 00:55:19 +02:00
|
|
|
std::string("path1") + client::file_util::kPathSep + std::string("path2");
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
// Should always be 1 kPathSep character between paths.
|
|
|
|
EXPECT_STREQ(expected.c_str(),
|
2017-08-04 00:55:19 +02:00
|
|
|
client::file_util::JoinPath("path1", "path2").c_str());
|
|
|
|
EXPECT_STREQ(expected.c_str(),
|
|
|
|
client::file_util::JoinPath(
|
|
|
|
std::string("path1") + client::file_util::kPathSep, "path2")
|
|
|
|
.c_str());
|
|
|
|
EXPECT_STREQ(expected.c_str(),
|
|
|
|
client::file_util::JoinPath(
|
|
|
|
"path1", client::file_util::kPathSep + std::string("path2"))
|
|
|
|
.c_str());
|
|
|
|
EXPECT_STREQ(expected.c_str(),
|
|
|
|
client::file_util::JoinPath(
|
|
|
|
std::string("path1") + client::file_util::kPathSep,
|
|
|
|
client::file_util::kPathSep + std::string("path2"))
|
2017-05-17 11:29:28 +02:00
|
|
|
.c_str());
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FileUtil, WriteAndReadFile) {
|
|
|
|
CefScopedTempDir dir;
|
|
|
|
EXPECT_TRUE(dir.CreateUniqueTempDir());
|
|
|
|
|
|
|
|
const std::string& data = "Test contents to read/write";
|
2017-08-04 00:55:19 +02:00
|
|
|
const std::string& path =
|
|
|
|
client::file_util::JoinPath(dir.GetPath(), "test.txt");
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(static_cast<int>(data.size()),
|
2017-08-04 00:55:19 +02:00
|
|
|
client::file_util::WriteFile(path.c_str(), data.data(),
|
|
|
|
static_cast<int>(data.size())));
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
std::string read;
|
2017-08-04 00:55:19 +02:00
|
|
|
EXPECT_TRUE(client::file_util::ReadFileToString(path.c_str(), &read));
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_STREQ(data.c_str(), read.c_str());
|
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
|
|
|
|
TEST(FileUtil, GetFileExtension) {
|
|
|
|
EXPECT_TRUE(client::file_util::GetFileExtension(std::string()).empty());
|
|
|
|
EXPECT_TRUE(client::file_util::GetFileExtension("/path/to/foo").empty());
|
|
|
|
EXPECT_STREQ("ext",
|
|
|
|
client::file_util::GetFileExtension("/path/to/foo.ext").c_str());
|
|
|
|
}
|