OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 protected: | 40 protected: |
41 }; | 41 }; |
42 | 42 |
43 // An installer state on which we can tweak the target path. | 43 // An installer state on which we can tweak the target path. |
44 class MockInstallerState : public InstallerState { | 44 class MockInstallerState : public InstallerState { |
45 public: | 45 public: |
46 MockInstallerState() : InstallerState() { } | 46 MockInstallerState() : InstallerState() { } |
47 void set_target_path(const FilePath& target_path) { | 47 void set_target_path(const FilePath& target_path) { |
48 target_path_ = target_path; | 48 target_path_ = target_path; |
49 } | 49 } |
| 50 bool IsFileInUse(const FilePath& file) { |
| 51 return InstallerState::IsFileInUse(file); |
| 52 } |
50 }; | 53 }; |
51 | 54 |
52 // Simple function to dump some text into a new file. | 55 // Simple function to dump some text into a new file. |
53 void CreateTextFile(const std::wstring& filename, | 56 void CreateTextFile(const std::wstring& filename, |
54 const std::wstring& contents) { | 57 const std::wstring& contents) { |
55 std::ofstream file; | 58 std::ofstream file; |
56 file.open(filename.c_str()); | 59 file.open(filename.c_str()); |
57 ASSERT_TRUE(file.is_open()); | 60 ASSERT_TRUE(file.is_open()); |
58 file << contents; | 61 file << contents; |
59 file.close(); | 62 file.close(); |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 CommandLine cmd_line( | 453 CommandLine cmd_line( |
451 CommandLine::FromString(L"setup.exe --multi-install --chrome")); | 454 CommandLine::FromString(L"setup.exe --multi-install --chrome")); |
452 MasterPreferences prefs(cmd_line); | 455 MasterPreferences prefs(cmd_line); |
453 InstallerState installer_state; | 456 InstallerState installer_state; |
454 installer_state.Initialize(cmd_line, prefs, machine_state); | 457 installer_state.Initialize(cmd_line, prefs, machine_state); |
455 | 458 |
456 // Is the Chrome version picked up? | 459 // Is the Chrome version picked up? |
457 scoped_ptr<Version> version(installer_state.GetCurrentVersion(machine_state)); | 460 scoped_ptr<Version> version(installer_state.GetCurrentVersion(machine_state)); |
458 EXPECT_TRUE(version.get() != NULL); | 461 EXPECT_TRUE(version.get() != NULL); |
459 } | 462 } |
| 463 |
| 464 TEST_F(InstallerStateTest, IsFileInUse) { |
| 465 ScopedTempDir temp_dir; |
| 466 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 467 |
| 468 FilePath temp_file; |
| 469 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), &temp_file)); |
| 470 |
| 471 MockInstallerState mock_installer_state; |
| 472 EXPECT_FALSE(mock_installer_state.IsFileInUse(temp_file)); |
| 473 |
| 474 { |
| 475 // Open a handle to the file with the same access mode and sharing options |
| 476 // as the loader. |
| 477 base::win::ScopedHandle temp_handle( |
| 478 CreateFile(temp_file.value().c_str(), |
| 479 SYNCHRONIZE | FILE_EXECUTE, |
| 480 FILE_SHARE_DELETE | FILE_SHARE_READ, |
| 481 NULL, OPEN_EXISTING, 0, 0)); |
| 482 ASSERT_TRUE(temp_handle != NULL); |
| 483 |
| 484 // The file should now be in use. |
| 485 EXPECT_TRUE(mock_installer_state.IsFileInUse(temp_file)); |
| 486 } |
| 487 |
| 488 // And once the handle is gone, it should no longer be in use. |
| 489 EXPECT_FALSE(mock_installer_state.IsFileInUse(temp_file)); |
| 490 } |
OLD | NEW |