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