chromium/chrome/browser/first_run/first_run_browsertest.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/numerics/safe_conversions.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/first_run/first_run_internal.h"
#include "chrome/browser/importer/importer_list.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/prefs/chrome_pref_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "components/variations/metrics.h"
#include "components/variations/pref_names.h"
#include "components/variations/variations_switches.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_launcher.h"
#include "testing/gtest/include/gtest/gtest.h"

FirstRunBrowserTest;

namespace first_run {

#if !BUILDFLAG(IS_CHROMEOS)
namespace {

// A generic test class to be subclassed by test classes testing specific
// master_preferences. All subclasses must call SetInitialPreferencesForTest()
// from their SetUp() method before deferring the remainder of Setup() to this
// class.
class FirstRunMasterPrefsBrowserTestBase : public InProcessBrowserTest {};

template<const char Text[]>
class FirstRunMasterPrefsBrowserTestT
    : public FirstRunMasterPrefsBrowserTestBase {};

// Returns the true expected import state, derived from the original
// |expected_import_state|, for the current test machine's configuration. Some
// bot configurations do not have another profile (browser) to import from and
// thus the import must not be expected to have occurred.
int MaskExpectedImportState(int expected_import_state) {}

}  // namespace

const char kImportDefault[] =;
FirstRunMasterPrefsImportDefault;
// No items are imported by default.
IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportDefault, ImportDefault) {}

const char kImportAll[] =;
FirstRunMasterPrefsImportAll;
IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportAll, ImportAll) {}

// The bookmarks file doesn't actually need to exist for this integration test
// to trigger the interaction being tested.
const char kImportBookmarksFile[] =;
FirstRunMasterPrefsImportBookmarksFile;
IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportBookmarksFile,
                       ImportBookmarksFile) {}

// Test an import with all import options disabled. This is a regression test
// for http://crbug.com/169984 where this would cause the import process to
// stay running, and the NTP to be loaded with no apps.
const char kImportNothing[] =;
FirstRunMasterPrefsImportNothing;
IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsImportNothing,
                       ImportNothingAndShowNewTabPage) {}

// Test first run with some tracked preferences.
const char kWithTrackedPrefs[] =;
// A test fixture that will run in a first run scenario with master_preferences
// set to kWithTrackedPrefs.
class FirstRunMasterPrefsWithTrackedPreferences
    : public FirstRunMasterPrefsBrowserTestT<kWithTrackedPrefs> {};

IN_PROC_BROWSER_TEST_F(FirstRunMasterPrefsWithTrackedPreferences,
                       TrackedPreferencesSurviveFirstRun) {}

#define COMPRESSED_SEED_TEST_VALUE
#define SEED_SIGNATURE_TEST_VALUE

constexpr char kCompressedSeedTestValue[] =;
constexpr char kSignatureValue[] =;

const char kWithVariationsPrefs[] =;

#undef COMPRESSED_SEED_TEST_VALUE
#undef SEED_SIGNATURE_TEST_VALUE

// Note: This test is parametrized on metrics consent state, since that affects
// field trial randomization.
class FirstRunMasterPrefsVariationsSeedTest
    : public FirstRunMasterPrefsBrowserTestT<kWithVariationsPrefs>,
      public testing::WithParamInterface<bool> {};

IN_PROC_BROWSER_TEST_P(FirstRunMasterPrefsVariationsSeedTest, Test) {}

// The following tests are only enabled on Windows, since it is the only
// platform where master prefs is used to deliver first run variations. The
// tests do not pass on other platforms due to the provisional client id logic
// in metrics_state_manager.cc. See the comment there for details.

#if BUILDFLAG(IS_WIN)

// The trial and groups encoded in the above seed.
constexpr char kTrialName[] = "UMA-Uniformity-Trial-10-Percent";
const char* kTrialGroups[] = {"default",  "group_01", "group_02", "group_03",
                              "group_04", "group_05", "group_06", "group_07",
                              "group_08", "group_09"};

IN_PROC_BROWSER_TEST_P(FirstRunMasterPrefsVariationsSeedTest, PRE_SecondRun) {
  // Check that the trial from the seed exists and is in one of the expected
  // states. Persist the state so that we can verify its randomization persists
  // in FirstRunMasterPrefsVariationsSeedTest.SecondRun.
  const std::string group_name = base::FieldTrialList::FindFullName(kTrialName);
  ASSERT_TRUE(base::Contains(kTrialGroups, group_name)) << group_name;
  // Ensure trial is active (not disabled).
  ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName));
  WriteTrialGroupToTestFile(group_name);
}

IN_PROC_BROWSER_TEST_P(FirstRunMasterPrefsVariationsSeedTest, SecondRun) {
  // This test runs after PRE_SecondRun and verifies that the trial state on
  // the second run matches what was seen in the PRE_ test.
  const std::string group_name = base::FieldTrialList::FindFullName(kTrialName);
  ASSERT_TRUE(base::Contains(kTrialGroups, group_name)) << group_name;
  // Ensure trial is active (not disabled).
  ASSERT_TRUE(base::FieldTrialList::IsTrialActive(kTrialName));
  // Read the trial group name that was saved by PRE_ForceTrials from the
  // corresponding test file.
  EXPECT_EQ(group_name, ReadTrialGroupFromTestFile());
}
#endif  // BUILDFLAG(IS_WIN)

INSTANTIATE_TEST_SUITE_P();

#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace first_run