1 // Copyright (c) 2012 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.
5 #include "chrome/installer/util/product_unittest.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/test_reg_util_win.h"
10 #include "chrome/common/chrome_constants.h"
11 #include "chrome/installer/util/chrome_frame_distribution.h"
12 #include "chrome/installer/util/google_update_constants.h"
13 #include "chrome/installer/util/installation_state.h"
14 #include "chrome/installer/util/installer_state.h"
15 #include "chrome/installer/util/master_preferences.h"
16 #include "chrome/installer/util/product.h"
18 using base::win::RegKey
;
19 using installer::Product
;
20 using installer::MasterPreferences
;
21 using registry_util::RegistryOverrideManager
;
23 void TestWithTempDir::SetUp() {
24 // Name a subdirectory of the user temp directory.
25 ASSERT_TRUE(test_dir_
.CreateUniqueTempDir());
28 void TestWithTempDir::TearDown() {
29 logging::CloseLogFile();
30 ASSERT_TRUE(test_dir_
.Delete());
33 ////////////////////////////////////////////////////////////////////////////////
35 void TestWithTempDirAndDeleteTempOverrideKeys::SetUp() {
36 TestWithTempDir::SetUp();
39 void TestWithTempDirAndDeleteTempOverrideKeys::TearDown() {
40 TestWithTempDir::TearDown();
43 ////////////////////////////////////////////////////////////////////////////////
45 class ProductTest
: public TestWithTempDirAndDeleteTempOverrideKeys
{
49 // This test is flaky on Win, see http://crbug.com/100567
51 #define MAYBE_ProductInstallBasic DISABLED_ProductInstallBasic
53 #define MAYBE_ProductInstallBasic ProductInstallBasic
56 TEST_F(ProductTest
, MAYBE_ProductInstallBasic
) {
57 // TODO(tommi): We should mock this and use our mocked distribution.
58 const bool multi_install
= false;
59 const bool system_level
= true;
60 CommandLine cmd_line
= CommandLine::FromString(
61 std::wstring(L
"setup.exe") +
62 (multi_install
? L
" --multi-install --chrome" : L
"") +
63 (system_level
? L
" --system-level" : L
""));
64 installer::MasterPreferences
prefs(cmd_line
);
65 installer::InstallationState machine_state
;
66 machine_state
.Initialize();
67 installer::InstallerState installer_state
;
68 installer_state
.Initialize(cmd_line
, prefs
, machine_state
);
70 const Product
* product
= installer_state
.products()[0];
71 BrowserDistribution
* distribution
= product
->distribution();
72 EXPECT_EQ(BrowserDistribution::CHROME_BROWSER
, distribution
->GetType());
74 std::vector
<base::FilePath
> user_data_paths
;
75 product
->GetUserDataPaths(&user_data_paths
);
76 EXPECT_GE(user_data_paths
.size(), static_cast<size_t>(1));
77 const base::FilePath
& user_data
= user_data_paths
[0];
78 EXPECT_FALSE(user_data_paths
[0].empty());
79 EXPECT_NE(std::wstring::npos
,
80 user_data_paths
[0].value().find(installer::kInstallUserDataDir
));
81 if (user_data_paths
.size() > 1) {
82 EXPECT_FALSE(user_data_paths
[1].empty());
85 user_data_paths
[1].value().find(chrome::kMetroChromeUserDataSubDir
));
88 base::FilePath program_files
;
89 PathService::Get(base::DIR_PROGRAM_FILES
, &program_files
);
90 // The User Data path should never be under program files, even though
91 // system_level is true.
92 EXPECT_EQ(std::wstring::npos
,
93 user_data
.value().find(program_files
.value()));
95 // There should be no installed version in the registry.
96 machine_state
.Initialize();
97 EXPECT_TRUE(machine_state
.GetProductState(
98 system_level
, distribution
->GetType()) == NULL
);
100 HKEY root
= installer_state
.root_key();
102 RegistryOverrideManager override_manager
;
103 override_manager
.OverrideRegistry(root
, L
"root_pit");
105 // Let's pretend chrome is installed.
106 RegKey
version_key(root
, distribution
->GetVersionKey().c_str(),
108 ASSERT_TRUE(version_key
.Valid());
110 const char kCurrentVersion
[] = "1.2.3.4";
111 Version
current_version(kCurrentVersion
);
112 version_key
.WriteValue(google_update::kRegVersionField
,
114 current_version
.GetString()).c_str());
116 // We started out with a non-msi product.
117 machine_state
.Initialize();
118 const installer::ProductState
* chrome_state
=
119 machine_state
.GetProductState(system_level
, distribution
->GetType());
120 EXPECT_TRUE(chrome_state
!= NULL
);
121 if (chrome_state
!= NULL
) {
122 EXPECT_TRUE(chrome_state
->version().Equals(current_version
));
123 EXPECT_FALSE(chrome_state
->is_msi());
126 // Create a make-believe client state key.
128 std::wstring
state_key_path(distribution
->GetStateKey());
129 ASSERT_EQ(ERROR_SUCCESS
,
130 key
.Create(root
, state_key_path
.c_str(), KEY_ALL_ACCESS
));
132 // Set the MSI marker, refresh, and verify that we now see the MSI marker.
133 EXPECT_TRUE(product
->SetMsiMarker(system_level
, true));
134 machine_state
.Initialize();
136 machine_state
.GetProductState(system_level
, distribution
->GetType());
137 EXPECT_TRUE(chrome_state
!= NULL
);
138 if (chrome_state
!= NULL
)
139 EXPECT_TRUE(chrome_state
->is_msi());
143 TEST_F(ProductTest
, LaunchChrome
) {
144 // TODO(tommi): Test Product::LaunchChrome and
145 // Product::LaunchChromeAndWait.
146 LOG(ERROR
) << "Test not implemented.";