• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/installer/util/product_unittest.h"
6 
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"
17 
18 using base::win::RegKey;
19 using installer::Product;
20 using installer::MasterPreferences;
21 using registry_util::RegistryOverrideManager;
22 
SetUp()23 void TestWithTempDir::SetUp() {
24   // Name a subdirectory of the user temp directory.
25   ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
26 }
27 
TearDown()28 void TestWithTempDir::TearDown() {
29   logging::CloseLogFile();
30   ASSERT_TRUE(test_dir_.Delete());
31 }
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 
SetUp()35 void TestWithTempDirAndDeleteTempOverrideKeys::SetUp() {
36   TestWithTempDir::SetUp();
37 }
38 
TearDown()39 void TestWithTempDirAndDeleteTempOverrideKeys::TearDown() {
40   TestWithTempDir::TearDown();
41 }
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 
45 class ProductTest : public TestWithTempDirAndDeleteTempOverrideKeys {
46  protected:
47 };
48 
49 // This test is flaky on Win, see http://crbug.com/100567
50 #if defined(OS_WIN)
51 #define MAYBE_ProductInstallBasic DISABLED_ProductInstallBasic
52 #else
53 #define MAYBE_ProductInstallBasic ProductInstallBasic
54 #endif
55 
TEST_F(ProductTest,MAYBE_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);
69 
70   const Product* product = installer_state.products()[0];
71   BrowserDistribution* distribution = product->distribution();
72   EXPECT_EQ(BrowserDistribution::CHROME_BROWSER, distribution->GetType());
73 
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());
83     EXPECT_NE(
84         std::wstring::npos,
85         user_data_paths[1].value().find(chrome::kMetroChromeUserDataSubDir));
86   }
87 
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()));
94 
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);
99 
100   HKEY root = installer_state.root_key();
101   {
102     RegistryOverrideManager override_manager;
103     override_manager.OverrideRegistry(root, L"root_pit");
104 
105     // Let's pretend chrome is installed.
106     RegKey version_key(root, distribution->GetVersionKey().c_str(),
107                        KEY_ALL_ACCESS);
108     ASSERT_TRUE(version_key.Valid());
109 
110     const char kCurrentVersion[] = "1.2.3.4";
111     Version current_version(kCurrentVersion);
112     version_key.WriteValue(google_update::kRegVersionField,
113                            UTF8ToWide(current_version.GetString()).c_str());
114 
115     // We started out with a non-msi product.
116     machine_state.Initialize();
117     const installer::ProductState* chrome_state =
118         machine_state.GetProductState(system_level, distribution->GetType());
119     EXPECT_TRUE(chrome_state != NULL);
120     if (chrome_state != NULL) {
121       EXPECT_TRUE(chrome_state->version().Equals(current_version));
122       EXPECT_FALSE(chrome_state->is_msi());
123     }
124 
125     // Create a make-believe client state key.
126     RegKey key;
127     std::wstring state_key_path(distribution->GetStateKey());
128     ASSERT_EQ(ERROR_SUCCESS,
129         key.Create(root, state_key_path.c_str(), KEY_ALL_ACCESS));
130 
131     // Set the MSI marker, refresh, and verify that we now see the MSI marker.
132     EXPECT_TRUE(product->SetMsiMarker(system_level, true));
133     machine_state.Initialize();
134     chrome_state =
135         machine_state.GetProductState(system_level, distribution->GetType());
136     EXPECT_TRUE(chrome_state != NULL);
137     if (chrome_state != NULL)
138       EXPECT_TRUE(chrome_state->is_msi());
139   }
140 }
141 
TEST_F(ProductTest,LaunchChrome)142 TEST_F(ProductTest, LaunchChrome) {
143   // TODO(tommi): Test Product::LaunchChrome and
144   // Product::LaunchChromeAndWait.
145   LOG(ERROR) << "Test not implemented.";
146 }
147