product_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
18using base::win::RegKey;
19using installer::Product;
20using installer::MasterPreferences;
21using registry_util::RegistryOverrideManager;
22
23void TestWithTempDir::SetUp() {
24  // Name a subdirectory of the user temp directory.
25  ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
26}
27
28void TestWithTempDir::TearDown() {
29  logging::CloseLogFile();
30  ASSERT_TRUE(test_dir_.Delete());
31}
32
33////////////////////////////////////////////////////////////////////////////////
34
35void TestWithTempDirAndDeleteTempOverrideKeys::SetUp() {
36  TestWithTempDir::SetUp();
37}
38
39void TestWithTempDirAndDeleteTempOverrideKeys::TearDown() {
40  TestWithTempDir::TearDown();
41}
42
43////////////////////////////////////////////////////////////////////////////////
44
45class 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
56TEST_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                           base::UTF8ToWide(
114                               current_version.GetString()).c_str());
115
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());
124    }
125
126    // Create a make-believe client state key.
127    RegKey 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));
131
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();
135    chrome_state =
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());
140  }
141}
142
143TEST_F(ProductTest, LaunchChrome) {
144  // TODO(tommi): Test Product::LaunchChrome and
145  // Product::LaunchChromeAndWait.
146  LOG(ERROR) << "Test not implemented.";
147}
148