| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <windows.h> | 5 #include <windows.h> |
| 6 | 6 |
| 7 #include <fstream> | 7 #include <fstream> |
| 8 #include <iostream> | 8 #include <iostream> |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 } | 70 } |
| 71 | 71 |
| 72 wchar_t text_content_1[] = L"Gooooooooooooooooooooogle"; | 72 wchar_t text_content_1[] = L"Gooooooooooooooooooooogle"; |
| 73 wchar_t text_content_2[] = L"Overwrite Me"; | 73 wchar_t text_content_2[] = L"Overwrite Me"; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 // Move one directory from source to destination when destination does not | 76 // Move one directory from source to destination when destination does not |
| 77 // exist. | 77 // exist. |
| 78 TEST_F(MoveTreeWorkItemTest, MoveDirectory) { | 78 TEST_F(MoveTreeWorkItemTest, MoveDirectory) { |
| 79 // Create two level deep source dir | 79 // Create two level deep source dir |
| 80 std::wstring from_dir1(test_dir_.ToWStringHack()); | 80 FilePath from_dir1(test_dir_); |
| 81 file_util::AppendToPath(&from_dir1, L"From_Dir1"); | 81 from_dir1 = from_dir1.AppendASCII("From_Dir1"); |
| 82 CreateDirectory(from_dir1.c_str(), NULL); | 82 CreateDirectory(from_dir1.value().c_str(), NULL); |
| 83 ASSERT_TRUE(file_util::PathExists(from_dir1)); | 83 ASSERT_TRUE(file_util::PathExists(from_dir1)); |
| 84 | 84 |
| 85 std::wstring from_dir2(from_dir1); | 85 FilePath from_dir2(from_dir1); |
| 86 file_util::AppendToPath(&from_dir2, L"From_Dir2"); | 86 from_dir2 = from_dir2.AppendASCII("From_Dir2"); |
| 87 CreateDirectory(from_dir2.c_str(), NULL); | 87 CreateDirectory(from_dir2.value().c_str(), NULL); |
| 88 ASSERT_TRUE(file_util::PathExists(from_dir2)); | 88 ASSERT_TRUE(file_util::PathExists(from_dir2)); |
| 89 | 89 |
| 90 std::wstring from_file(from_dir2); | 90 FilePath from_file(from_dir2); |
| 91 file_util::AppendToPath(&from_file, L"From_File"); | 91 from_file = from_file.AppendASCII("From_File"); |
| 92 CreateTextFile(from_file, text_content_1); | 92 CreateTextFile(from_file.value(), text_content_1); |
| 93 ASSERT_TRUE(file_util::PathExists(from_file)); | 93 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 94 | 94 |
| 95 // Generate destination path | 95 // Generate destination path |
| 96 std::wstring to_dir(test_dir_.ToWStringHack()); | 96 FilePath to_dir(test_dir_); |
| 97 file_util::AppendToPath(&to_dir, L"To_Dir"); | 97 to_dir = to_dir.AppendASCII("To_Dir"); |
| 98 ASSERT_FALSE(file_util::PathExists(to_dir)); | 98 ASSERT_FALSE(file_util::PathExists(to_dir)); |
| 99 | 99 |
| 100 std::wstring to_file(to_dir); | 100 FilePath to_file(to_dir); |
| 101 file_util::AppendToPath(&to_file, L"From_Dir2"); | 101 to_file = to_file.AppendASCII("From_Dir2"); |
| 102 file_util::AppendToPath(&to_file, L"From_File"); | 102 to_file = to_file.AppendASCII("From_File"); |
| 103 ASSERT_FALSE(file_util::PathExists(to_file)); | 103 ASSERT_FALSE(file_util::PathExists(to_file)); |
| 104 | 104 |
| 105 // test Do() | 105 // test Do() |
| 106 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( | 106 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( |
| 107 from_dir1, to_dir, temp_dir_.ToWStringHack())); | 107 from_dir1.ToWStringHack(), to_dir.ToWStringHack(), |
| 108 temp_dir_.ToWStringHack())); |
| 108 EXPECT_TRUE(work_item->Do()); | 109 EXPECT_TRUE(work_item->Do()); |
| 109 | 110 |
| 110 EXPECT_FALSE(file_util::PathExists(from_dir1)); | 111 EXPECT_FALSE(file_util::PathExists(from_dir1)); |
| 111 EXPECT_TRUE(file_util::PathExists(to_dir)); | 112 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 112 EXPECT_TRUE(file_util::PathExists(to_file)); | 113 EXPECT_TRUE(file_util::PathExists(to_file)); |
| 113 | 114 |
| 114 // test rollback() | 115 // test rollback() |
| 115 work_item->Rollback(); | 116 work_item->Rollback(); |
| 116 | 117 |
| 117 EXPECT_TRUE(file_util::PathExists(from_dir1)); | 118 EXPECT_TRUE(file_util::PathExists(from_dir1)); |
| 118 EXPECT_TRUE(file_util::PathExists(from_file)); | 119 EXPECT_TRUE(file_util::PathExists(from_file)); |
| 119 EXPECT_FALSE(file_util::PathExists(to_dir)); | 120 EXPECT_FALSE(file_util::PathExists(to_dir)); |
| 120 } | 121 } |
| 121 | 122 |
| 122 // Move one directory from source to destination when destination already | 123 // Move one directory from source to destination when destination already |
| 123 // exists. | 124 // exists. |
| 124 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExists) { | 125 TEST_F(MoveTreeWorkItemTest, MoveDirectoryDestExists) { |
| 125 // Create two level deep source dir | 126 // Create two level deep source dir |
| 126 std::wstring from_dir1(test_dir_.ToWStringHack()); | 127 FilePath from_dir1(test_dir_); |
| 127 file_util::AppendToPath(&from_dir1, L"From_Dir1"); | 128 from_dir1 = from_dir1.AppendASCII("From_Dir1"); |
| 128 CreateDirectory(from_dir1.c_str(), NULL); | 129 CreateDirectory(from_dir1.value().c_str(), NULL); |
| 129 ASSERT_TRUE(file_util::PathExists(from_dir1)); | 130 ASSERT_TRUE(file_util::PathExists(from_dir1)); |
| 130 | 131 |
| 131 std::wstring from_dir2(from_dir1); | 132 FilePath from_dir2(from_dir1); |
| 132 file_util::AppendToPath(&from_dir2, L"From_Dir2"); | 133 from_dir2 = from_dir2.AppendASCII("From_Dir2"); |
| 133 CreateDirectory(from_dir2.c_str(), NULL); | 134 CreateDirectory(from_dir2.value().c_str(), NULL); |
| 134 ASSERT_TRUE(file_util::PathExists(from_dir2)); | 135 ASSERT_TRUE(file_util::PathExists(from_dir2)); |
| 135 | 136 |
| 136 std::wstring from_file(from_dir2); | 137 FilePath from_file(from_dir2); |
| 137 file_util::AppendToPath(&from_file, L"From_File"); | 138 from_file = from_file.AppendASCII("From_File"); |
| 138 CreateTextFile(from_file, text_content_1); | 139 CreateTextFile(from_file.value(), text_content_1); |
| 139 ASSERT_TRUE(file_util::PathExists(from_file)); | 140 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 140 | 141 |
| 141 // Create destination path | 142 // Create destination path |
| 142 std::wstring to_dir(test_dir_.ToWStringHack()); | 143 FilePath to_dir(test_dir_); |
| 143 file_util::AppendToPath(&to_dir, L"To_Dir"); | 144 to_dir = to_dir.AppendASCII("To_Dir"); |
| 144 CreateDirectory(to_dir.c_str(), NULL); | 145 CreateDirectory(to_dir.value().c_str(), NULL); |
| 145 ASSERT_TRUE(file_util::PathExists(to_dir)); | 146 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 146 | 147 |
| 147 std::wstring orig_to_file(to_dir); | 148 FilePath orig_to_file(to_dir); |
| 148 file_util::AppendToPath(&orig_to_file, L"To_File"); | 149 orig_to_file = orig_to_file.AppendASCII("To_File"); |
| 149 CreateTextFile(orig_to_file, text_content_2); | 150 CreateTextFile(orig_to_file.value(), text_content_2); |
| 150 ASSERT_TRUE(file_util::PathExists(orig_to_file)); | 151 ASSERT_TRUE(file_util::PathExists(orig_to_file)); |
| 151 | 152 |
| 152 std::wstring new_to_file(to_dir); | 153 FilePath new_to_file(to_dir); |
| 153 file_util::AppendToPath(&new_to_file, L"From_Dir2"); | 154 new_to_file = new_to_file.AppendASCII("From_Dir2"); |
| 154 file_util::AppendToPath(&new_to_file, L"From_File"); | 155 new_to_file = new_to_file.AppendASCII("From_File"); |
| 155 ASSERT_FALSE(file_util::PathExists(new_to_file)); | 156 ASSERT_FALSE(file_util::PathExists(new_to_file)); |
| 156 | 157 |
| 157 // test Do() | 158 // test Do() |
| 158 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( | 159 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( |
| 159 from_dir1, to_dir, temp_dir_.ToWStringHack())); | 160 from_dir1.ToWStringHack(), to_dir.ToWStringHack(), |
| 161 temp_dir_.ToWStringHack())); |
| 160 EXPECT_TRUE(work_item->Do()); | 162 EXPECT_TRUE(work_item->Do()); |
| 161 | 163 |
| 162 EXPECT_FALSE(file_util::PathExists(from_dir1)); | 164 EXPECT_FALSE(file_util::PathExists(from_dir1)); |
| 163 EXPECT_TRUE(file_util::PathExists(to_dir)); | 165 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 164 EXPECT_TRUE(file_util::PathExists(new_to_file)); | 166 EXPECT_TRUE(file_util::PathExists(new_to_file)); |
| 165 EXPECT_FALSE(file_util::PathExists(orig_to_file)); | 167 EXPECT_FALSE(file_util::PathExists(orig_to_file)); |
| 166 | 168 |
| 167 // test rollback() | 169 // test rollback() |
| 168 work_item->Rollback(); | 170 work_item->Rollback(); |
| 169 | 171 |
| 170 EXPECT_TRUE(file_util::PathExists(from_dir1)); | 172 EXPECT_TRUE(file_util::PathExists(from_dir1)); |
| 171 EXPECT_TRUE(file_util::PathExists(to_dir)); | 173 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 172 EXPECT_FALSE(file_util::PathExists(new_to_file)); | 174 EXPECT_FALSE(file_util::PathExists(new_to_file)); |
| 173 EXPECT_TRUE(file_util::PathExists(orig_to_file)); | 175 EXPECT_TRUE(file_util::PathExists(orig_to_file)); |
| 174 EXPECT_EQ(0, ReadTextFile(orig_to_file).compare(text_content_2)); | 176 EXPECT_EQ(0, ReadTextFile(orig_to_file.ToWStringHack()).compare( |
| 175 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 177 text_content_2)); |
| 178 EXPECT_EQ(0, ReadTextFile(from_file.ToWStringHack()).compare(text_content_1)); |
| 176 } | 179 } |
| 177 | 180 |
| 178 // Move one file from source to destination when destination does not | 181 // Move one file from source to destination when destination does not |
| 179 // exist. | 182 // exist. |
| 180 TEST_F(MoveTreeWorkItemTest, MoveAFile) { | 183 TEST_F(MoveTreeWorkItemTest, MoveAFile) { |
| 181 // Create a file inside source dir | 184 // Create a file inside source dir |
| 182 std::wstring from_dir(test_dir_.ToWStringHack()); | 185 FilePath from_dir(test_dir_); |
| 183 file_util::AppendToPath(&from_dir, L"From_Dir"); | 186 from_dir = from_dir.AppendASCII("From_Dir"); |
| 184 CreateDirectory(from_dir.c_str(), NULL); | 187 CreateDirectory(from_dir.value().c_str(), NULL); |
| 185 ASSERT_TRUE(file_util::PathExists(from_dir)); | 188 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 186 | 189 |
| 187 std::wstring from_file(from_dir); | 190 FilePath from_file(from_dir); |
| 188 file_util::AppendToPath(&from_file, L"From_File"); | 191 from_file = from_file.AppendASCII("From_File"); |
| 189 CreateTextFile(from_file, text_content_1); | 192 CreateTextFile(from_file.value(), text_content_1); |
| 190 ASSERT_TRUE(file_util::PathExists(from_file)); | 193 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 191 | 194 |
| 192 // Generate destination file name | 195 // Generate destination file name |
| 193 std::wstring to_file(test_dir_.ToWStringHack()); | 196 FilePath to_file(test_dir_); |
| 194 file_util::AppendToPath(&to_file, L"To_File"); | 197 to_file = to_file.AppendASCII("To_File"); |
| 195 ASSERT_FALSE(file_util::PathExists(to_file)); | 198 ASSERT_FALSE(file_util::PathExists(to_file)); |
| 196 | 199 |
| 197 // test Do() | 200 // test Do() |
| 198 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( | 201 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( |
| 199 from_file, to_file, temp_dir_.ToWStringHack())); | 202 from_file.ToWStringHack(), to_file.ToWStringHack(), |
| 203 temp_dir_.ToWStringHack())); |
| 200 EXPECT_TRUE(work_item->Do()); | 204 EXPECT_TRUE(work_item->Do()); |
| 201 | 205 |
| 202 EXPECT_TRUE(file_util::PathExists(from_dir)); | 206 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 203 EXPECT_FALSE(file_util::PathExists(from_file)); | 207 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 204 EXPECT_TRUE(file_util::PathExists(to_file)); | 208 EXPECT_TRUE(file_util::PathExists(to_file)); |
| 205 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); | 209 EXPECT_EQ(0, ReadTextFile(to_file.value()).compare(text_content_1)); |
| 206 | 210 |
| 207 // test rollback() | 211 // test rollback() |
| 208 work_item->Rollback(); | 212 work_item->Rollback(); |
| 209 | 213 |
| 210 EXPECT_TRUE(file_util::PathExists(from_dir)); | 214 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 211 EXPECT_TRUE(file_util::PathExists(from_file)); | 215 EXPECT_TRUE(file_util::PathExists(from_file)); |
| 212 EXPECT_FALSE(file_util::PathExists(to_file)); | 216 EXPECT_FALSE(file_util::PathExists(to_file)); |
| 213 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 217 EXPECT_EQ(0, ReadTextFile(from_file.value()).compare(text_content_1)); |
| 214 } | 218 } |
| 215 | 219 |
| 216 // Move one file from source to destination when destination already | 220 // Move one file from source to destination when destination already |
| 217 // exists. | 221 // exists. |
| 218 TEST_F(MoveTreeWorkItemTest, MoveFileDestExists) { | 222 TEST_F(MoveTreeWorkItemTest, MoveFileDestExists) { |
| 219 // Create a file inside source dir | 223 // Create a file inside source dir |
| 220 std::wstring from_dir(test_dir_.ToWStringHack()); | 224 FilePath from_dir(test_dir_); |
| 221 file_util::AppendToPath(&from_dir, L"From_Dir"); | 225 from_dir = from_dir.AppendASCII("From_Dir"); |
| 222 CreateDirectory(from_dir.c_str(), NULL); | 226 CreateDirectory(from_dir.value().c_str(), NULL); |
| 223 ASSERT_TRUE(file_util::PathExists(from_dir)); | 227 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 224 | 228 |
| 225 std::wstring from_file(from_dir); | 229 FilePath from_file(from_dir); |
| 226 file_util::AppendToPath(&from_file, L"From_File"); | 230 from_file = from_file.AppendASCII("From_File"); |
| 227 CreateTextFile(from_file, text_content_1); | 231 CreateTextFile(from_file.value(), text_content_1); |
| 228 ASSERT_TRUE(file_util::PathExists(from_file)); | 232 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 229 | 233 |
| 230 // Create destination path | 234 // Create destination path |
| 231 std::wstring to_dir(test_dir_.ToWStringHack()); | 235 FilePath to_dir(test_dir_); |
| 232 file_util::AppendToPath(&to_dir, L"To_Dir"); | 236 to_dir = to_dir.AppendASCII("To_Dir"); |
| 233 CreateDirectory(to_dir.c_str(), NULL); | 237 CreateDirectory(to_dir.value().c_str(), NULL); |
| 234 ASSERT_TRUE(file_util::PathExists(to_dir)); | 238 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 235 | 239 |
| 236 std::wstring to_file(to_dir); | 240 FilePath to_file(to_dir); |
| 237 file_util::AppendToPath(&to_file, L"To_File"); | 241 to_file = to_file.AppendASCII("To_File"); |
| 238 CreateTextFile(to_file, text_content_2); | 242 CreateTextFile(to_file.value(), text_content_2); |
| 239 ASSERT_TRUE(file_util::PathExists(to_file)); | 243 ASSERT_TRUE(file_util::PathExists(to_file)); |
| 240 | 244 |
| 241 // test Do() | 245 // test Do() |
| 242 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( | 246 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( |
| 243 from_file, to_dir, temp_dir_.ToWStringHack())); | 247 from_file.ToWStringHack(), to_dir.ToWStringHack(), |
| 248 temp_dir_.ToWStringHack())); |
| 244 EXPECT_TRUE(work_item->Do()); | 249 EXPECT_TRUE(work_item->Do()); |
| 245 | 250 |
| 246 EXPECT_TRUE(file_util::PathExists(from_dir)); | 251 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 247 EXPECT_FALSE(file_util::PathExists(from_file)); | 252 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 248 EXPECT_TRUE(file_util::PathExists(to_dir)); | 253 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 249 EXPECT_FALSE(file_util::PathExists(to_file)); | 254 EXPECT_FALSE(file_util::PathExists(to_file)); |
| 250 EXPECT_EQ(0, ReadTextFile(to_dir).compare(text_content_1)); | 255 EXPECT_EQ(0, ReadTextFile(to_dir.value()).compare(text_content_1)); |
| 251 | 256 |
| 252 // test rollback() | 257 // test rollback() |
| 253 work_item->Rollback(); | 258 work_item->Rollback(); |
| 254 | 259 |
| 255 EXPECT_TRUE(file_util::PathExists(from_dir)); | 260 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 256 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 261 EXPECT_EQ(0, ReadTextFile(from_file.value()).compare(text_content_1)); |
| 257 EXPECT_TRUE(file_util::PathExists(to_dir)); | 262 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 258 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_2)); | 263 EXPECT_EQ(0, ReadTextFile(to_file.value()).compare(text_content_2)); |
| 259 } | 264 } |
| 260 | 265 |
| 261 // Move one file from source to destination when destination already | 266 // Move one file from source to destination when destination already |
| 262 // exists and is in use. | 267 // exists and is in use. |
| 263 TEST_F(MoveTreeWorkItemTest, MoveFileDestInUse) { | 268 TEST_F(MoveTreeWorkItemTest, MoveFileDestInUse) { |
| 264 // Create a file inside source dir | 269 // Create a file inside source dir |
| 265 std::wstring from_dir(test_dir_.ToWStringHack()); | 270 FilePath from_dir(test_dir_); |
| 266 file_util::AppendToPath(&from_dir, L"From_Dir"); | 271 from_dir = from_dir.AppendASCII("From_Dir"); |
| 267 CreateDirectory(from_dir.c_str(), NULL); | 272 CreateDirectory(from_dir.value().c_str(), NULL); |
| 268 ASSERT_TRUE(file_util::PathExists(from_dir)); | 273 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 269 | 274 |
| 270 std::wstring from_file(from_dir); | 275 FilePath from_file(from_dir); |
| 271 file_util::AppendToPath(&from_file, L"From_File"); | 276 from_file = from_file.AppendASCII("From_File"); |
| 272 CreateTextFile(from_file, text_content_1); | 277 CreateTextFile(from_file.value(), text_content_1); |
| 273 ASSERT_TRUE(file_util::PathExists(from_file)); | 278 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 274 | 279 |
| 275 // Create an executable in destination path by copying ourself to it. | 280 // Create an executable in destination path by copying ourself to it. |
| 276 std::wstring to_dir(test_dir_.ToWStringHack()); | 281 FilePath to_dir(test_dir_); |
| 277 file_util::AppendToPath(&to_dir, L"To_Dir"); | 282 to_dir = to_dir.AppendASCII("To_Dir"); |
| 278 CreateDirectory(to_dir.c_str(), NULL); | 283 CreateDirectory(to_dir.value().c_str(), NULL); |
| 279 ASSERT_TRUE(file_util::PathExists(to_dir)); | 284 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 280 | 285 |
| 281 wchar_t exe_full_path_str[MAX_PATH]; | 286 wchar_t exe_full_path_str[MAX_PATH]; |
| 282 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); | 287 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); |
| 283 std::wstring exe_full_path(exe_full_path_str); | 288 FilePath exe_full_path(exe_full_path_str); |
| 284 std::wstring to_file(to_dir); | 289 FilePath to_file(to_dir); |
| 285 file_util::AppendToPath(&to_file, L"To_File"); | 290 to_file = to_file.AppendASCII("To_File"); |
| 286 file_util::CopyFile(exe_full_path, to_file); | 291 file_util::CopyFile(exe_full_path, to_file); |
| 287 ASSERT_TRUE(file_util::PathExists(to_file)); | 292 ASSERT_TRUE(file_util::PathExists(to_file)); |
| 288 | 293 |
| 289 // Run the executable in destination path | 294 // Run the executable in destination path |
| 290 STARTUPINFOW si = {sizeof(si)}; | 295 STARTUPINFOW si = {sizeof(si)}; |
| 291 PROCESS_INFORMATION pi = {0}; | 296 PROCESS_INFORMATION pi = {0}; |
| 292 ASSERT_TRUE(::CreateProcess(NULL, const_cast<wchar_t*>(to_file.c_str()), | 297 ASSERT_TRUE(::CreateProcess(NULL, |
| 298 const_cast<wchar_t*>(to_file.value().c_str()), |
| 293 NULL, NULL, FALSE, | 299 NULL, NULL, FALSE, |
| 294 CREATE_NO_WINDOW | CREATE_SUSPENDED, | 300 CREATE_NO_WINDOW | CREATE_SUSPENDED, |
| 295 NULL, NULL, &si, &pi)); | 301 NULL, NULL, &si, &pi)); |
| 296 | 302 |
| 297 // test Do() | 303 // test Do() |
| 298 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( | 304 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( |
| 299 from_file, to_file, temp_dir_.ToWStringHack())); | 305 from_file.ToWStringHack(), to_file.ToWStringHack(), |
| 306 temp_dir_.ToWStringHack())); |
| 300 EXPECT_TRUE(work_item->Do()); | 307 EXPECT_TRUE(work_item->Do()); |
| 301 | 308 |
| 302 EXPECT_TRUE(file_util::PathExists(from_dir)); | 309 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 303 EXPECT_FALSE(file_util::PathExists(from_file)); | 310 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 304 EXPECT_TRUE(file_util::PathExists(to_dir)); | 311 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 305 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); | 312 EXPECT_EQ(0, ReadTextFile(to_file.value()).compare(text_content_1)); |
| 306 | 313 |
| 307 // test rollback() | 314 // test rollback() |
| 308 work_item->Rollback(); | 315 work_item->Rollback(); |
| 309 | 316 |
| 310 EXPECT_TRUE(file_util::PathExists(from_dir)); | 317 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 311 EXPECT_EQ(0, ReadTextFile(from_file).compare(text_content_1)); | 318 EXPECT_EQ(0, ReadTextFile(from_file.value()).compare(text_content_1)); |
| 312 EXPECT_TRUE(file_util::PathExists(to_dir)); | 319 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 313 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); | 320 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); |
| 314 | 321 |
| 315 TerminateProcess(pi.hProcess, 0); | 322 TerminateProcess(pi.hProcess, 0); |
| 316 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 323 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
| 317 CloseHandle(pi.hProcess); | 324 CloseHandle(pi.hProcess); |
| 318 CloseHandle(pi.hThread); | 325 CloseHandle(pi.hThread); |
| 319 } | 326 } |
| 320 | 327 |
| 321 // Move one file that is in use to destination. | 328 // Move one file that is in use to destination. |
| 322 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) { | 329 TEST_F(MoveTreeWorkItemTest, MoveFileInUse) { |
| 323 // Create an executable for source by copying ourself to a new source dir. | 330 // Create an executable for source by copying ourself to a new source dir. |
| 324 std::wstring from_dir(test_dir_.ToWStringHack()); | 331 FilePath from_dir(test_dir_); |
| 325 file_util::AppendToPath(&from_dir, L"From_Dir"); | 332 from_dir = from_dir.AppendASCII("From_Dir"); |
| 326 CreateDirectory(from_dir.c_str(), NULL); | 333 CreateDirectory(from_dir.value().c_str(), NULL); |
| 327 ASSERT_TRUE(file_util::PathExists(from_dir)); | 334 ASSERT_TRUE(file_util::PathExists(from_dir)); |
| 328 | 335 |
| 329 wchar_t exe_full_path_str[MAX_PATH]; | 336 wchar_t exe_full_path_str[MAX_PATH]; |
| 330 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); | 337 ::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH); |
| 331 std::wstring exe_full_path(exe_full_path_str); | 338 FilePath exe_full_path(exe_full_path_str); |
| 332 std::wstring from_file(from_dir); | 339 FilePath from_file(from_dir); |
| 333 file_util::AppendToPath(&from_file, L"From_File"); | 340 from_file = from_file.AppendASCII("From_File"); |
| 334 file_util::CopyFile(exe_full_path, from_file); | 341 file_util::CopyFile(exe_full_path, from_file); |
| 335 ASSERT_TRUE(file_util::PathExists(from_file)); | 342 ASSERT_TRUE(file_util::PathExists(from_file)); |
| 336 | 343 |
| 337 // Create a destination source dir and generate destination file name. | 344 // Create a destination source dir and generate destination file name. |
| 338 std::wstring to_dir(test_dir_.ToWStringHack()); | 345 FilePath to_dir(test_dir_); |
| 339 file_util::AppendToPath(&to_dir, L"To_Dir"); | 346 to_dir = to_dir.AppendASCII("To_Dir"); |
| 340 CreateDirectory(to_dir.c_str(), NULL); | 347 CreateDirectory(to_dir.value().c_str(), NULL); |
| 341 ASSERT_TRUE(file_util::PathExists(to_dir)); | 348 ASSERT_TRUE(file_util::PathExists(to_dir)); |
| 342 | 349 |
| 343 std::wstring to_file(to_dir); | 350 FilePath to_file(to_dir); |
| 344 file_util::AppendToPath(&to_file, L"To_File"); | 351 to_file = to_file.AppendASCII("To_File"); |
| 345 CreateTextFile(to_file, text_content_1); | 352 CreateTextFile(to_file.value(), text_content_1); |
| 346 ASSERT_TRUE(file_util::PathExists(to_file)); | 353 ASSERT_TRUE(file_util::PathExists(to_file)); |
| 347 | 354 |
| 348 // Run the executable in source path | 355 // Run the executable in source path |
| 349 STARTUPINFOW si = {sizeof(si)}; | 356 STARTUPINFOW si = {sizeof(si)}; |
| 350 PROCESS_INFORMATION pi = {0}; | 357 PROCESS_INFORMATION pi = {0}; |
| 351 ASSERT_TRUE(::CreateProcess(NULL, const_cast<wchar_t*>(from_file.c_str()), | 358 ASSERT_TRUE(::CreateProcess(NULL, |
| 359 const_cast<wchar_t*>(from_file.value().c_str()), |
| 352 NULL, NULL, FALSE, | 360 NULL, NULL, FALSE, |
| 353 CREATE_NO_WINDOW | CREATE_SUSPENDED, | 361 CREATE_NO_WINDOW | CREATE_SUSPENDED, |
| 354 NULL, NULL, &si, &pi)); | 362 NULL, NULL, &si, &pi)); |
| 355 | 363 |
| 356 // test Do() | 364 // test Do() |
| 357 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( | 365 scoped_ptr<MoveTreeWorkItem> work_item(WorkItem::CreateMoveTreeWorkItem( |
| 358 from_file, to_file, temp_dir_.ToWStringHack())); | 366 from_file.ToWStringHack(), to_file.ToWStringHack(), |
| 367 temp_dir_.ToWStringHack())); |
| 359 EXPECT_TRUE(work_item->Do()); | 368 EXPECT_TRUE(work_item->Do()); |
| 360 | 369 |
| 361 EXPECT_TRUE(file_util::PathExists(from_dir)); | 370 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 362 EXPECT_FALSE(file_util::PathExists(from_file)); | 371 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 363 EXPECT_TRUE(file_util::PathExists(to_dir)); | 372 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 364 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); | 373 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); |
| 365 | 374 |
| 366 // Close the process and make sure all the conditions after Do() are | 375 // Close the process and make sure all the conditions after Do() are |
| 367 // still true. | 376 // still true. |
| 368 TerminateProcess(pi.hProcess, 0); | 377 TerminateProcess(pi.hProcess, 0); |
| 369 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); | 378 EXPECT_TRUE(WaitForSingleObject(pi.hProcess, 10000) == WAIT_OBJECT_0); |
| 370 CloseHandle(pi.hProcess); | 379 CloseHandle(pi.hProcess); |
| 371 CloseHandle(pi.hThread); | 380 CloseHandle(pi.hThread); |
| 372 | 381 |
| 373 EXPECT_TRUE(file_util::PathExists(from_dir)); | 382 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 374 EXPECT_FALSE(file_util::PathExists(from_file)); | 383 EXPECT_FALSE(file_util::PathExists(from_file)); |
| 375 EXPECT_TRUE(file_util::PathExists(to_dir)); | 384 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 376 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); | 385 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, to_file)); |
| 377 | 386 |
| 378 // test rollback() | 387 // test rollback() |
| 379 work_item->Rollback(); | 388 work_item->Rollback(); |
| 380 | 389 |
| 381 EXPECT_TRUE(file_util::PathExists(from_dir)); | 390 EXPECT_TRUE(file_util::PathExists(from_dir)); |
| 382 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, from_file)); | 391 EXPECT_TRUE(file_util::ContentsEqual(exe_full_path, from_file)); |
| 383 EXPECT_TRUE(file_util::PathExists(to_dir)); | 392 EXPECT_TRUE(file_util::PathExists(to_dir)); |
| 384 EXPECT_EQ(0, ReadTextFile(to_file).compare(text_content_1)); | 393 EXPECT_EQ(0, ReadTextFile(to_file.value()).compare(text_content_1)); |
| 385 } | 394 } |
| OLD | NEW |