diff options
Diffstat (limited to 'xbmc/utils/test/TestFileOperationJob.cpp')
| -rw-r--r-- | xbmc/utils/test/TestFileOperationJob.cpp | 288 |
1 files changed, 288 insertions, 0 deletions
diff --git a/xbmc/utils/test/TestFileOperationJob.cpp b/xbmc/utils/test/TestFileOperationJob.cpp new file mode 100644 index 0000000..cab4125 --- /dev/null +++ b/xbmc/utils/test/TestFileOperationJob.cpp | |||
| @@ -0,0 +1,288 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "filesystem/Directory.h" | ||
| 10 | #include "filesystem/File.h" | ||
| 11 | #include "test/TestUtils.h" | ||
| 12 | #include "utils/FileOperationJob.h" | ||
| 13 | #include "utils/URIUtils.h" | ||
| 14 | |||
| 15 | #include <gtest/gtest.h> | ||
| 16 | |||
| 17 | TEST(TestFileOperationJob, ActionCopy) | ||
| 18 | { | ||
| 19 | XFILE::CFile *tmpfile; | ||
| 20 | std::string tmpfilepath, destfile; | ||
| 21 | CFileItemList items; | ||
| 22 | CFileOperationJob job; | ||
| 23 | |||
| 24 | ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); | ||
| 25 | tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); | ||
| 26 | tmpfile->Close(); | ||
| 27 | |||
| 28 | CFileItemPtr item(new CFileItem(tmpfilepath)); | ||
| 29 | item->SetPath(tmpfilepath); | ||
| 30 | item->m_bIsFolder = false; | ||
| 31 | item->Select(true); | ||
| 32 | items.Add(item); | ||
| 33 | |||
| 34 | std::string destpath = URIUtils::GetDirectory(tmpfilepath); | ||
| 35 | destpath = URIUtils::AddFileToFolder(destpath, "copy"); | ||
| 36 | destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); | ||
| 37 | ASSERT_FALSE(XFILE::CFile::Exists(destfile)); | ||
| 38 | |||
| 39 | job.SetFileOperation(CFileOperationJob::ActionCopy, items, destpath); | ||
| 40 | EXPECT_EQ(CFileOperationJob::ActionCopy, job.GetAction()); | ||
| 41 | |||
| 42 | EXPECT_TRUE(job.DoWork()); | ||
| 43 | EXPECT_TRUE(XFILE::CFile::Exists(tmpfilepath)); | ||
| 44 | EXPECT_TRUE(XFILE::CFile::Exists(destfile)); | ||
| 45 | |||
| 46 | EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); | ||
| 47 | EXPECT_TRUE(XFILE::CFile::Delete(destfile)); | ||
| 48 | EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); | ||
| 49 | } | ||
| 50 | |||
| 51 | TEST(TestFileOperationJob, ActionMove) | ||
| 52 | { | ||
| 53 | XFILE::CFile *tmpfile; | ||
| 54 | std::string tmpfilepath, destfile; | ||
| 55 | CFileItemList items; | ||
| 56 | CFileOperationJob job; | ||
| 57 | |||
| 58 | ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); | ||
| 59 | tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); | ||
| 60 | tmpfile->Close(); | ||
| 61 | |||
| 62 | CFileItemPtr item(new CFileItem(tmpfilepath)); | ||
| 63 | item->SetPath(tmpfilepath); | ||
| 64 | item->m_bIsFolder = false; | ||
| 65 | item->Select(true); | ||
| 66 | items.Add(item); | ||
| 67 | |||
| 68 | std::string destpath = URIUtils::GetDirectory(tmpfilepath); | ||
| 69 | destpath = URIUtils::AddFileToFolder(destpath, "move"); | ||
| 70 | destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); | ||
| 71 | ASSERT_FALSE(XFILE::CFile::Exists(destfile)); | ||
| 72 | ASSERT_TRUE(XFILE::CDirectory::Create(destpath)); | ||
| 73 | |||
| 74 | job.SetFileOperation(CFileOperationJob::ActionMove, items, destpath); | ||
| 75 | EXPECT_EQ(CFileOperationJob::ActionMove, job.GetAction()); | ||
| 76 | |||
| 77 | EXPECT_TRUE(job.DoWork()); | ||
| 78 | EXPECT_FALSE(XFILE::CFile::Exists(tmpfilepath)); | ||
| 79 | EXPECT_TRUE(XFILE::CFile::Exists(destfile)); | ||
| 80 | |||
| 81 | EXPECT_TRUE(XFILE::CFile::Delete(destfile)); | ||
| 82 | EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); | ||
| 83 | } | ||
| 84 | |||
| 85 | TEST(TestFileOperationJob, ActionDelete) | ||
| 86 | { | ||
| 87 | XFILE::CFile *tmpfile; | ||
| 88 | std::string tmpfilepath, destfile; | ||
| 89 | CFileItemList items; | ||
| 90 | CFileOperationJob job; | ||
| 91 | |||
| 92 | ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); | ||
| 93 | tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); | ||
| 94 | tmpfile->Close(); | ||
| 95 | |||
| 96 | CFileItemPtr item(new CFileItem(tmpfilepath)); | ||
| 97 | item->SetPath(tmpfilepath); | ||
| 98 | item->m_bIsFolder = false; | ||
| 99 | item->Select(true); | ||
| 100 | items.Add(item); | ||
| 101 | |||
| 102 | std::string destpath = URIUtils::GetDirectory(tmpfilepath); | ||
| 103 | destpath = URIUtils::AddFileToFolder(destpath, "delete"); | ||
| 104 | destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); | ||
| 105 | ASSERT_FALSE(XFILE::CFile::Exists(destfile)); | ||
| 106 | |||
| 107 | job.SetFileOperation(CFileOperationJob::ActionCopy, items, destpath); | ||
| 108 | EXPECT_EQ(CFileOperationJob::ActionCopy, job.GetAction()); | ||
| 109 | |||
| 110 | EXPECT_TRUE(job.DoWork()); | ||
| 111 | EXPECT_TRUE(XFILE::CFile::Exists(tmpfilepath)); | ||
| 112 | EXPECT_TRUE(XFILE::CFile::Exists(destfile)); | ||
| 113 | |||
| 114 | job.SetFileOperation(CFileOperationJob::ActionDelete, items, ""); | ||
| 115 | EXPECT_EQ(CFileOperationJob::ActionDelete, job.GetAction()); | ||
| 116 | |||
| 117 | EXPECT_TRUE(job.DoWork()); | ||
| 118 | EXPECT_FALSE(XFILE::CFile::Exists(tmpfilepath)); | ||
| 119 | |||
| 120 | items.Clear(); | ||
| 121 | CFileItemPtr item2(new CFileItem(destfile)); | ||
| 122 | item2->SetPath(destfile); | ||
| 123 | item2->m_bIsFolder = false; | ||
| 124 | item2->Select(true); | ||
| 125 | items.Add(item2); | ||
| 126 | |||
| 127 | job.SetFileOperation(CFileOperationJob::ActionDelete, items, ""); | ||
| 128 | EXPECT_EQ(CFileOperationJob::ActionDelete, job.GetAction()); | ||
| 129 | |||
| 130 | EXPECT_TRUE(job.DoWork()); | ||
| 131 | EXPECT_FALSE(XFILE::CFile::Exists(destfile)); | ||
| 132 | EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); | ||
| 133 | } | ||
| 134 | |||
| 135 | TEST(TestFileOperationJob, ActionReplace) | ||
| 136 | { | ||
| 137 | XFILE::CFile *tmpfile; | ||
| 138 | std::string tmpfilepath, destfile; | ||
| 139 | CFileItemList items; | ||
| 140 | CFileOperationJob job; | ||
| 141 | |||
| 142 | ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); | ||
| 143 | tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); | ||
| 144 | tmpfile->Close(); | ||
| 145 | |||
| 146 | CFileItemPtr item(new CFileItem(tmpfilepath)); | ||
| 147 | item->SetPath(tmpfilepath); | ||
| 148 | item->m_bIsFolder = false; | ||
| 149 | item->Select(true); | ||
| 150 | items.Add(item); | ||
| 151 | |||
| 152 | std::string destpath = URIUtils::GetDirectory(tmpfilepath); | ||
| 153 | destpath = URIUtils::AddFileToFolder(destpath, "replace"); | ||
| 154 | destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); | ||
| 155 | ASSERT_FALSE(XFILE::CFile::Exists(destfile)); | ||
| 156 | |||
| 157 | job.SetFileOperation(CFileOperationJob::ActionCopy, items, destpath); | ||
| 158 | EXPECT_EQ(CFileOperationJob::ActionCopy, job.GetAction()); | ||
| 159 | |||
| 160 | EXPECT_TRUE(job.DoWork()); | ||
| 161 | EXPECT_TRUE(XFILE::CFile::Exists(tmpfilepath)); | ||
| 162 | EXPECT_TRUE(XFILE::CFile::Exists(destfile)); | ||
| 163 | |||
| 164 | job.SetFileOperation(CFileOperationJob::ActionReplace, items, destpath); | ||
| 165 | EXPECT_EQ(CFileOperationJob::ActionReplace, job.GetAction()); | ||
| 166 | |||
| 167 | EXPECT_TRUE(job.DoWork()); | ||
| 168 | EXPECT_TRUE(XFILE::CFile::Exists(destfile)); | ||
| 169 | |||
| 170 | EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); | ||
| 171 | EXPECT_TRUE(XFILE::CFile::Delete(destfile)); | ||
| 172 | EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); | ||
| 173 | } | ||
| 174 | |||
| 175 | TEST(TestFileOperationJob, ActionCreateFolder) | ||
| 176 | { | ||
| 177 | XFILE::CFile *tmpfile; | ||
| 178 | std::string tmpfilepath, destpath; | ||
| 179 | CFileItemList items; | ||
| 180 | CFileOperationJob job; | ||
| 181 | |||
| 182 | ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); | ||
| 183 | tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); | ||
| 184 | |||
| 185 | std::string tmpfiledirectory = | ||
| 186 | CXBMCTestUtils::Instance().TempFileDirectory(tmpfile); | ||
| 187 | |||
| 188 | tmpfile->Close(); | ||
| 189 | |||
| 190 | destpath = tmpfilepath; | ||
| 191 | destpath += ".createfolder"; | ||
| 192 | ASSERT_FALSE(XFILE::CFile::Exists(destpath)); | ||
| 193 | |||
| 194 | CFileItemPtr item(new CFileItem(destpath)); | ||
| 195 | item->SetPath(destpath); | ||
| 196 | item->m_bIsFolder = true; | ||
| 197 | item->Select(true); | ||
| 198 | items.Add(item); | ||
| 199 | |||
| 200 | job.SetFileOperation(CFileOperationJob::ActionCreateFolder, items, tmpfiledirectory); | ||
| 201 | EXPECT_EQ(CFileOperationJob::ActionCreateFolder, job.GetAction()); | ||
| 202 | |||
| 203 | EXPECT_TRUE(job.DoWork()); | ||
| 204 | EXPECT_TRUE(XFILE::CDirectory::Exists(destpath)); | ||
| 205 | |||
| 206 | EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); | ||
| 207 | EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); | ||
| 208 | } | ||
| 209 | |||
| 210 | // This test will fail until ActionDeleteFolder has a proper implementation | ||
| 211 | TEST(TestFileOperationJob, ActionDeleteFolder) | ||
| 212 | { | ||
| 213 | XFILE::CFile *tmpfile; | ||
| 214 | std::string tmpfilepath, destpath; | ||
| 215 | CFileItemList items; | ||
| 216 | CFileOperationJob job; | ||
| 217 | |||
| 218 | ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); | ||
| 219 | tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); | ||
| 220 | |||
| 221 | std::string tmpfiledirectory = | ||
| 222 | CXBMCTestUtils::Instance().TempFileDirectory(tmpfile); | ||
| 223 | |||
| 224 | tmpfile->Close(); | ||
| 225 | |||
| 226 | destpath = tmpfilepath; | ||
| 227 | destpath += ".deletefolder"; | ||
| 228 | ASSERT_FALSE(XFILE::CFile::Exists(destpath)); | ||
| 229 | |||
| 230 | CFileItemPtr item(new CFileItem(destpath)); | ||
| 231 | item->SetPath(destpath); | ||
| 232 | item->m_bIsFolder = true; | ||
| 233 | item->Select(true); | ||
| 234 | items.Add(item); | ||
| 235 | |||
| 236 | job.SetFileOperation(CFileOperationJob::ActionCreateFolder, items, tmpfiledirectory); | ||
| 237 | EXPECT_EQ(CFileOperationJob::ActionCreateFolder, job.GetAction()); | ||
| 238 | |||
| 239 | EXPECT_TRUE(job.DoWork()); | ||
| 240 | EXPECT_TRUE(XFILE::CDirectory::Exists(destpath)); | ||
| 241 | |||
| 242 | job.SetFileOperation(CFileOperationJob::ActionDeleteFolder, items, tmpfiledirectory); | ||
| 243 | EXPECT_EQ(CFileOperationJob::ActionDeleteFolder, job.GetAction()); | ||
| 244 | |||
| 245 | EXPECT_TRUE(job.DoWork()); | ||
| 246 | EXPECT_FALSE(XFILE::CDirectory::Exists(destpath)); | ||
| 247 | |||
| 248 | EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); | ||
| 249 | } | ||
| 250 | |||
| 251 | TEST(TestFileOperationJob, GetFunctions) | ||
| 252 | { | ||
| 253 | XFILE::CFile *tmpfile; | ||
| 254 | std::string tmpfilepath, destfile; | ||
| 255 | CFileItemList items; | ||
| 256 | CFileOperationJob job; | ||
| 257 | |||
| 258 | ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); | ||
| 259 | tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); | ||
| 260 | tmpfile->Close(); | ||
| 261 | |||
| 262 | CFileItemPtr item(new CFileItem(tmpfilepath)); | ||
| 263 | item->SetPath(tmpfilepath); | ||
| 264 | item->m_bIsFolder = false; | ||
| 265 | item->Select(true); | ||
| 266 | items.Add(item); | ||
| 267 | |||
| 268 | std::string destpath = URIUtils::GetDirectory(tmpfilepath); | ||
| 269 | destpath = URIUtils::AddFileToFolder(destpath, "getfunctions"); | ||
| 270 | destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); | ||
| 271 | ASSERT_FALSE(XFILE::CFile::Exists(destfile)); | ||
| 272 | |||
| 273 | job.SetFileOperation(CFileOperationJob::ActionCopy, items, destpath); | ||
| 274 | EXPECT_EQ(CFileOperationJob::ActionCopy, job.GetAction()); | ||
| 275 | |||
| 276 | EXPECT_TRUE(job.DoWork()); | ||
| 277 | EXPECT_TRUE(XFILE::CFile::Exists(tmpfilepath)); | ||
| 278 | EXPECT_TRUE(XFILE::CFile::Exists(destfile)); | ||
| 279 | |||
| 280 | std::cout << "GetAverageSpeed(): " << job.GetAverageSpeed() << std::endl; | ||
| 281 | std::cout << "GetCurrentOperation(): " << job.GetCurrentOperation() << std::endl; | ||
| 282 | std::cout << "GetCurrentFile(): " << job.GetCurrentFile() << std::endl; | ||
| 283 | EXPECT_FALSE(job.GetItems().IsEmpty()); | ||
| 284 | |||
| 285 | EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); | ||
| 286 | EXPECT_TRUE(XFILE::CFile::Delete(destfile)); | ||
| 287 | EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); | ||
| 288 | } | ||
