1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/first_run/first_run.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_path_override.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/first_run/first_run_internal.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/installer/util/initial_preferences.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace first_run {
namespace {
base::FilePath GetTestDataPath(const std::string& test_name) {
return base::PathService::CheckedGet(chrome::DIR_TEST_DATA)
.AppendASCII("first_run")
.AppendASCII(test_name);
}
base::FilePath GetSentinelFilePath() {
return base::PathService::CheckedGet(chrome::DIR_USER_DATA)
.Append(chrome::kFirstRunSentinel);
}
} // namespace
class FirstRunTest : public testing::Test {
protected:
void TearDown() override {
first_run::ResetCachedSentinelDataForTesting();
Test::TearDown();
}
private:
base::ScopedPathOverride user_data_dir_override_{chrome::DIR_USER_DATA};
};
TEST_F(FirstRunTest, SetupInitialPrefsFromInstallPrefs_NoVariationsSeed) {
installer::InitialPreferences install_prefs("{ }");
EXPECT_TRUE(install_prefs.initial_dictionary().empty());
EXPECT_TRUE(install_prefs.GetCompressedVariationsSeed().empty());
EXPECT_TRUE(install_prefs.GetVariationsSeedSignature().empty());
}
TEST_F(FirstRunTest,
SetupInitialPrefsFromInstallPrefs_VariationsSeedSignature) {
installer::InitialPreferences install_prefs(
"{\"variations_compressed_seed\":\"xyz\","
" \"variations_seed_signature\":\"abc\"}");
EXPECT_EQ(2U, install_prefs.initial_dictionary().size());
EXPECT_EQ("xyz", install_prefs.GetCompressedVariationsSeed());
EXPECT_EQ("abc", install_prefs.GetVariationsSeedSignature());
// Variations prefs should have been extracted (removed) from the dictionary.
EXPECT_TRUE(install_prefs.initial_dictionary().empty());
}
// No switches and no sentinel present. This is the standard case for first run.
TEST_F(FirstRunTest, DetermineFirstRunState_FirstRun) {
internal::FirstRunState result =
internal::DetermineFirstRunState(false, false, false);
EXPECT_EQ(internal::FIRST_RUN_TRUE, result);
}
// Force switch is present, overriding both sentinel and suppress switch.
TEST_F(FirstRunTest, DetermineFirstRunState_ForceSwitch) {
internal::FirstRunState result =
internal::DetermineFirstRunState(true, true, true);
EXPECT_EQ(internal::FIRST_RUN_TRUE, result);
result = internal::DetermineFirstRunState(true, true, false);
EXPECT_EQ(internal::FIRST_RUN_TRUE, result);
result = internal::DetermineFirstRunState(false, true, true);
EXPECT_EQ(internal::FIRST_RUN_TRUE, result);
result = internal::DetermineFirstRunState(false, true, false);
EXPECT_EQ(internal::FIRST_RUN_TRUE, result);
}
// No switches, but sentinel present. This is not a first run.
TEST_F(FirstRunTest, DetermineFirstRunState_NotFirstRun) {
internal::FirstRunState result =
internal::DetermineFirstRunState(true, false, false);
EXPECT_EQ(internal::FIRST_RUN_FALSE, result);
}
// Suppress switch is present, overriding sentinel state.
TEST_F(FirstRunTest, DetermineFirstRunState_SuppressSwitch) {
internal::FirstRunState result =
internal::DetermineFirstRunState(false, false, true);
EXPECT_EQ(internal::FIRST_RUN_FALSE, result);
result = internal::DetermineFirstRunState(true, false, true);
EXPECT_EQ(internal::FIRST_RUN_FALSE, result);
}
TEST_F(FirstRunTest, GetFirstRunSentinelCreationTime_Created) {
base::HistogramTester histogram_tester;
first_run::CreateSentinelIfNeeded();
histogram_tester.ExpectUniqueSample(
"FirstRun.Sentinel.Created",
startup_metric_utils::FirstRunSentinelCreationResult::kSuccess, 1);
// Gets the creation time of the first run sentinel.
base::File::Info info;
ASSERT_TRUE(base::GetFileInfo(GetSentinelFilePath(), &info));
EXPECT_EQ(info.creation_time, first_run::GetFirstRunSentinelCreationTime());
}
TEST_F(FirstRunTest, GetFirstRunSentinelCreationTime_NotCreated) {
base::File::Info info;
ASSERT_FALSE(base::GetFileInfo(GetSentinelFilePath(), &info));
EXPECT_EQ(
0,
first_run::GetFirstRunSentinelCreationTime().InSecondsFSinceUnixEpoch());
}
TEST_F(FirstRunTest, CreateSentinelIfNeeded) {
{
base::HistogramTester histogram_tester;
EXPECT_FALSE(base::PathExists(GetSentinelFilePath()));
EXPECT_TRUE(IsChromeFirstRun());
first_run::CreateSentinelIfNeeded();
histogram_tester.ExpectUniqueSample(
"FirstRun.Sentinel.Created",
startup_metric_utils::FirstRunSentinelCreationResult::kSuccess, 1);
}
{
base::HistogramTester histogram_tester;
EXPECT_TRUE(base::PathExists(GetSentinelFilePath()));
EXPECT_TRUE(IsChromeFirstRun());
first_run::CreateSentinelIfNeeded();
// We are still considered in the first run, but we'll attempt a creation
// even if the file exists.
histogram_tester.ExpectUniqueSample(
"FirstRun.Sentinel.Created",
startup_metric_utils::FirstRunSentinelCreationResult::kFilePathExists,
1);
}
first_run::ResetCachedSentinelDataForTesting();
{
base::HistogramTester histogram_tester;
EXPECT_TRUE(base::PathExists(GetSentinelFilePath()));
EXPECT_FALSE(IsChromeFirstRun());
first_run::CreateSentinelIfNeeded();
// The file already exists, and we identified that we are not in the first
// run, the creation is not needed.
histogram_tester.ExpectTotalCount("FirstRun.Sentinel.Created", 0);
}
}
TEST_F(FirstRunTest, CreateSentinelIfNeeded_DoneEvenIfForced) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kForceFirstRun);
{
base::HistogramTester histogram_tester;
EXPECT_FALSE(base::PathExists(GetSentinelFilePath()));
EXPECT_TRUE(IsChromeFirstRun());
first_run::CreateSentinelIfNeeded();
histogram_tester.ExpectUniqueSample(
"FirstRun.Sentinel.Created",
startup_metric_utils::FirstRunSentinelCreationResult::kSuccess, 1);
}
{
base::HistogramTester histogram_tester;
EXPECT_TRUE(base::PathExists(GetSentinelFilePath()));
EXPECT_TRUE(IsChromeFirstRun());
first_run::CreateSentinelIfNeeded();
// While the first run state is forced, we'll always attempt to create the
// sentinel.
histogram_tester.ExpectUniqueSample(
"FirstRun.Sentinel.Created",
startup_metric_utils::FirstRunSentinelCreationResult::kFilePathExists,
1);
}
first_run::ResetCachedSentinelDataForTesting();
{
base::HistogramTester histogram_tester;
EXPECT_TRUE(base::PathExists(GetSentinelFilePath()));
EXPECT_TRUE(IsChromeFirstRun());
first_run::CreateSentinelIfNeeded();
// While the first run state is forced, we'll always attempt to create the
// sentinel.
histogram_tester.ExpectUniqueSample(
"FirstRun.Sentinel.Created",
startup_metric_utils::FirstRunSentinelCreationResult::kFilePathExists,
1);
}
}
TEST_F(FirstRunTest, CreateSentinelIfNeeded_SkippedIfSuppressed) {
base::HistogramTester histogram_tester;
base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kNoFirstRun);
first_run::CreateSentinelIfNeeded();
histogram_tester.ExpectTotalCount("FirstRun.Sentinel.Created", 0);
EXPECT_FALSE(base::PathExists(GetSentinelFilePath()));
EXPECT_FALSE(IsChromeFirstRun());
}
#if BUILDFLAG(IS_POSIX) // This test relies on Posix file permissions.
TEST_F(FirstRunTest, CreateSentinelIfNeeded_FileSystemError) {
base::HistogramTester histogram_tester;
// Make the user data dir read-only so the sentinel can't be written.
// Note: the test fixture registers an override to a temp dir for the
// scope of each test, the below is not as destructive as it seems.
auto path = base::PathService::CheckedGet(chrome::DIR_USER_DATA);
ASSERT_TRUE(SetPosixFilePermissions(
path, DirectoryExists(path) ? (S_IRUSR | S_IXUSR) : S_IRUSR));
first_run::CreateSentinelIfNeeded();
histogram_tester.ExpectUniqueSample(
"FirstRun.Sentinel.Created",
startup_metric_utils::FirstRunSentinelCreationResult::kFileSystemError,
1);
EXPECT_FALSE(base::PathExists(GetSentinelFilePath()));
EXPECT_TRUE(IsChromeFirstRun()); // This is still a first run.
}
#endif
// This test, and the one below, require customizing the path that the initial
// prefs code will search. On non-Mac platforms that path is derived from
// PathService, but on macOS it instead comes from the system analog of
// PathService (NSSearchPathForDirectoriesInDomains), which we don't have an
// analogous scoped override for.
//
// TODO(ellyjones): Add a scoped override for
// NSSearchPathForDirectoriesInDomains, then re-enable these on macOS.
#if BUILDFLAG(IS_MAC)
#define MAYBE_InitialPrefsUsedIfReadable DISABLED_InitialPrefsUsedIfReadable
#else
#define MAYBE_InitialPrefsUsedIfReadable InitialPrefsUsedIfReadable
#endif
TEST_F(FirstRunTest, MAYBE_InitialPrefsUsedIfReadable) {
base::ScopedPathOverride override(base::DIR_EXE, GetTestDataPath("initial"));
std::unique_ptr<installer::InitialPreferences> prefs =
first_run::LoadInitialPrefs();
ASSERT_TRUE(prefs);
EXPECT_EQ(prefs->GetFirstRunTabs()[0], "https://www.chromium.org/initial");
}
#if BUILDFLAG(IS_MAC)
#define MAYBE_LegacyInitialPrefsUsedIfNewFileIsNotPresent \
DISABLED_LegacyInitialPrefsUsedIfNewFileIsNotPresent
#else
#define MAYBE_LegacyInitialPrefsUsedIfNewFileIsNotPresent \
LegacyInitialPrefsUsedIfNewFileIsNotPresent
#endif
TEST_F(FirstRunTest, MAYBE_LegacyInitialPrefsUsedIfNewFileIsNotPresent) {
base::ScopedPathOverride override(base::DIR_EXE, GetTestDataPath("legacy"));
std::unique_ptr<installer::InitialPreferences> prefs =
first_run::LoadInitialPrefs();
ASSERT_TRUE(prefs);
EXPECT_EQ(prefs->GetFirstRunTabs()[0], "https://www.chromium.org/legacy");
}
} // namespace first_run
|