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