1 // Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
2 // 2011 The Chromium Authors. All rights reserved. Use of this source code is
3 // governed by a BSD-style license that can be found in the LICENSE file.
4
5 #include <string>
6
7 #include "include/wrapper/cef_scoped_temp_dir.h"
8 #include "tests/gtest/include/gtest/gtest.h"
9 #include "tests/shared/browser/file_util.h"
10
TEST(FileUtil,JoinPath)11 TEST(FileUtil, JoinPath) {
12 // Should return whichever path component is non-empty.
13 EXPECT_STREQ("", client::file_util::JoinPath("", "").c_str());
14 EXPECT_STREQ("path1", client::file_util::JoinPath("path1", "").c_str());
15 EXPECT_STREQ("path2", client::file_util::JoinPath("", "path2").c_str());
16
17 const std::string& expected =
18 std::string("path1") + client::file_util::kPathSep + std::string("path2");
19
20 // Should always be 1 kPathSep character between paths.
21 EXPECT_STREQ(expected.c_str(),
22 client::file_util::JoinPath("path1", "path2").c_str());
23 EXPECT_STREQ(expected.c_str(),
24 client::file_util::JoinPath(
25 std::string("path1") + client::file_util::kPathSep, "path2")
26 .c_str());
27 EXPECT_STREQ(expected.c_str(),
28 client::file_util::JoinPath(
29 "path1", client::file_util::kPathSep + std::string("path2"))
30 .c_str());
31 EXPECT_STREQ(expected.c_str(),
32 client::file_util::JoinPath(
33 std::string("path1") + client::file_util::kPathSep,
34 client::file_util::kPathSep + std::string("path2"))
35 .c_str());
36 }
37
TEST(FileUtil,WriteAndReadFile)38 TEST(FileUtil, WriteAndReadFile) {
39 CefScopedTempDir dir;
40 EXPECT_TRUE(dir.CreateUniqueTempDir());
41
42 const std::string& data = "Test contents to read/write";
43 const std::string& path =
44 client::file_util::JoinPath(dir.GetPath(), "test.txt");
45
46 EXPECT_EQ(static_cast<int>(data.size()),
47 client::file_util::WriteFile(path.c_str(), data.data(),
48 static_cast<int>(data.size())));
49
50 std::string read;
51 EXPECT_TRUE(client::file_util::ReadFileToString(path.c_str(), &read));
52 EXPECT_STREQ(data.c_str(), read.c_str());
53 }
54
TEST(FileUtil,GetFileExtension)55 TEST(FileUtil, GetFileExtension) {
56 EXPECT_TRUE(client::file_util::GetFileExtension(std::string()).empty());
57 EXPECT_TRUE(client::file_util::GetFileExtension("/path/to/foo").empty());
58 EXPECT_STREQ("ext",
59 client::file_util::GetFileExtension("/path/to/foo.ext").c_str());
60 }
61