OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/setup/install_worker.h" |
| 6 |
| 7 #include "base/win/registry.h" |
| 8 #include "base/version.h" |
| 9 #include "chrome/installer/util/installation_state.h" |
| 10 #include "chrome/installer/util/installer_state.h" |
| 11 #include "chrome/installer/util/package.h" |
| 12 #include "chrome/installer/util/package_properties.h" |
| 13 #include "chrome/installer/util/work_item_list.h" |
| 14 |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 |
| 18 using installer::ChromiumPackageProperties; |
| 19 using installer::InstallationState; |
| 20 using installer::InstallerState; |
| 21 using installer::Package; |
| 22 using installer::PackageProperties; |
| 23 using installer::PackagePropertiesImpl; |
| 24 using installer::ProductState; |
| 25 |
| 26 using ::testing::_; |
| 27 using ::testing::AtLeast; |
| 28 |
| 29 // Mock classes to help with testing |
| 30 //------------------------------------------------------------------------------ |
| 31 |
| 32 class MockWorkItemList : public WorkItemList { |
| 33 public: |
| 34 MockWorkItemList() {} |
| 35 |
| 36 MOCK_METHOD5(AddCopyTreeWorkItem, WorkItem*(const std::wstring&, |
| 37 const std::wstring&, |
| 38 const std::wstring&, |
| 39 CopyOverWriteOption, |
| 40 const std::wstring&)); |
| 41 MOCK_METHOD1(AddCreateDirWorkItem, WorkItem* (const FilePath&)); |
| 42 MOCK_METHOD2(AddCreateRegKeyWorkItem, WorkItem* (HKEY, const std::wstring&)); |
| 43 MOCK_METHOD2(AddDeleteRegKeyWorkItem, WorkItem* (HKEY, const std::wstring&)); |
| 44 MOCK_METHOD4(AddDeleteRegValueWorkItem, WorkItem* (HKEY, |
| 45 const std::wstring&, |
| 46 const std::wstring&, |
| 47 bool)); |
| 48 MOCK_METHOD2(AddDeleteTreeWorkItem, WorkItem* (const FilePath&, |
| 49 const std::vector<FilePath>&)); |
| 50 MOCK_METHOD1(AddDeleteTreeWorkItem, WorkItem* (const FilePath&)); |
| 51 MOCK_METHOD3(AddMoveTreeWorkItem, WorkItem* (const std::wstring&, |
| 52 const std::wstring&, |
| 53 const std::wstring&)); |
| 54 MOCK_METHOD5(AddSetRegValueWorkItem, WorkItem*(HKEY, |
| 55 const std::wstring&, |
| 56 const std::wstring&, |
| 57 const std::wstring&, |
| 58 bool)); |
| 59 MOCK_METHOD5(AddSetRegValueWorkItem, WorkItem* (HKEY, |
| 60 const std::wstring&, |
| 61 const std::wstring&, |
| 62 DWORD, |
| 63 bool)); |
| 64 MOCK_METHOD3(AddSelfRegWorkItem, WorkItem* (const std::wstring&, |
| 65 bool, |
| 66 bool)); |
| 67 }; |
| 68 |
| 69 // Okay, so this isn't really a mock as such, but it does add setter methods |
| 70 // to make it easier to build custom InstallationStates. |
| 71 class MockInstallationState : public InstallationState { |
| 72 public: |
| 73 // Included for testing. |
| 74 void SetMultiPackageState(bool system_install, |
| 75 const ProductState& package_state) { |
| 76 ProductState& target = |
| 77 (system_install ? system_products_ : user_products_) |
| 78 [MULTI_PACKAGE_INDEX]; |
| 79 target.CopyFrom(package_state); |
| 80 } |
| 81 |
| 82 // Included for testing. |
| 83 void SetProductState(bool system_install, |
| 84 BrowserDistribution::Type type, |
| 85 const ProductState& product_state) { |
| 86 ProductState& target = (system_install ? system_products_ : |
| 87 user_products_)[IndexFromDistType(type)]; |
| 88 target.CopyFrom(product_state); |
| 89 } |
| 90 }; |
| 91 |
| 92 class MockInstallerState : public InstallerState { |
| 93 public: |
| 94 void set_system_install(bool system_install) { |
| 95 system_install_ = system_install; |
| 96 } |
| 97 |
| 98 void set_operation(Operation operation) { operation_ = operation; } |
| 99 |
| 100 void set_state_key(const std::wstring& state_key) { |
| 101 state_key_ = state_key; |
| 102 } |
| 103 }; |
| 104 |
| 105 // The test fixture |
| 106 //------------------------------------------------------------------------------ |
| 107 |
| 108 class InstallWorkerTest : public testing::Test { |
| 109 public: |
| 110 virtual void SetUp() { |
| 111 current_version_.reset(Version::GetVersionFromString("1.0.0.0")); |
| 112 new_version_.reset(Version::GetVersionFromString("42.0.0.0")); |
| 113 |
| 114 // Don't bother ensuring that these paths exist. Since we're just |
| 115 // building the work item lists and not running them, they shouldn't |
| 116 // actually be touched. |
| 117 archive_path_ = FilePath(L"C:\\UnlikelyPath\\Temp\\chrome_123\\chrome.7z"); |
| 118 // TODO(robertshield): Take this from the BrowserDistribution once that |
| 119 // no longer depends on MasterPreferences. |
| 120 installation_path_ = FilePath(L"C:\\Program Files\\Google\\Chrome\\"); |
| 121 src_path_ = |
| 122 FilePath(L"C:\\UnlikelyPath\\Temp\\chrome_123\\source\\Chrome-bin"); |
| 123 setup_path_ = FilePath(L"C:\\UnlikelyPath\\Temp\\CR_123.tmp\\setup.exe"); |
| 124 temp_dir_ = FilePath(L"C:\\UnlikelyPath\\Temp\\chrome_123"); |
| 125 } |
| 126 |
| 127 virtual void TearDown() { |
| 128 |
| 129 } |
| 130 |
| 131 MockInstallationState* BuildChromeSingleSystemInstallationState() { |
| 132 scoped_ptr<MockInstallationState> installation_state( |
| 133 new MockInstallationState()); |
| 134 |
| 135 ProductState product_state; |
| 136 product_state.set_version(current_version_->Clone()); |
| 137 // Do not call SetMultiPackageState since this is a single install. |
| 138 installation_state->SetProductState(true, |
| 139 BrowserDistribution::CHROME_BROWSER, |
| 140 product_state); |
| 141 |
| 142 return installation_state.release(); |
| 143 } |
| 144 |
| 145 MockInstallerState* BuildChromeSingleSystemInstallerState() { |
| 146 scoped_ptr<MockInstallerState> installer_state(new MockInstallerState()); |
| 147 |
| 148 installer_state->set_system_install(true); |
| 149 installer_state->set_operation(InstallerState::SINGLE_INSTALL_OR_UPDATE); |
| 150 // Hope this next one isn't checked for now. |
| 151 installer_state->set_state_key(L"PROBABLY_INVALID_REG_PATH"); |
| 152 |
| 153 return installer_state.release(); |
| 154 } |
| 155 |
| 156 protected: |
| 157 scoped_ptr<Version> current_version_; |
| 158 scoped_ptr<Version> new_version_; |
| 159 FilePath archive_path_; |
| 160 FilePath installation_path_; |
| 161 FilePath setup_path_; |
| 162 FilePath src_path_; |
| 163 FilePath temp_dir_; |
| 164 }; |
| 165 |
| 166 // Tests |
| 167 //------------------------------------------------------------------------------ |
| 168 |
| 169 TEST_F(InstallWorkerTest, TestInstallChromeSingleSystem) { |
| 170 MockWorkItemList work_item_list; |
| 171 |
| 172 scoped_ptr<InstallationState> installation_state( |
| 173 BuildChromeSingleSystemInstallationState()); |
| 174 |
| 175 scoped_ptr<InstallerState> installer_state( |
| 176 BuildChromeSingleSystemInstallerState()); |
| 177 |
| 178 // This MUST outlive the package, since the package doesn't assume ownership |
| 179 // of it. Note: This feels like an implementation bug: |
| 180 // The PackageProperties <-> Package relationship is 1:1 and nothing else |
| 181 // uses the PackageProperties. I have the feeling that PackageProperties, and |
| 182 // perhaps Package itself should not exist at all. |
| 183 scoped_ptr<PackageProperties> package_properties( |
| 184 new ChromiumPackageProperties()); |
| 185 |
| 186 scoped_refptr<Package> package( |
| 187 new Package(false, true, installation_path_, package_properties.get())); |
| 188 |
| 189 // Set up some expectations. |
| 190 // TODO(robertshield): Set up some real expectations. |
| 191 EXPECT_CALL(work_item_list, AddCopyTreeWorkItem(_,_,_,_,_)) |
| 192 .Times(AtLeast(1)); |
| 193 |
| 194 AddInstallWorkItems(*installation_state.get(), |
| 195 *installer_state.get(), |
| 196 false, |
| 197 setup_path_, |
| 198 archive_path_, |
| 199 src_path_, |
| 200 temp_dir_, |
| 201 *new_version_.get(), |
| 202 ¤t_version_, |
| 203 *package.get(), |
| 204 &work_item_list); |
| 205 } |
OLD | NEW |