chromium/chrome/browser/ui/startup/first_run_service_browsertest.cc

// Copyright 2022 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/ui/startup/first_run_service.h"

#include <optional>
#include <string_view>

#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_string_value_serializer.h"
#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/profiles/profile_picker.h"
#include "chrome/browser/ui/profiles/profile_ui_test_utils.h"
#include "chrome/browser/ui/startup/first_run_test_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/signin_metrics.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/primary_account_mutator.h"
#include "components/variations/synthetic_trials_active_group_id_provider.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/lacros/device_settings_lacros.h"
#include "chromeos/crosapi/mojom/crosapi.mojom.h"
#include "chromeos/crosapi/mojom/device_settings_service.mojom.h"
#include "chromeos/startup/browser_init_params.h"
#endif

namespace {
class PolicyUpdateObserver : public policy::PolicyService::Observer {};

// Converts JSON string to `base::Value` object.
static base::Value GetJSONAsValue(std::string_view json) {}

}  // namespace

class FirstRunServiceBrowserTest : public FirstRunServiceBrowserTestBase {};

IN_PROC_BROWSER_TEST_F(FirstRunServiceBrowserTest,
                       OpenFirstRunIfNeededOpensPicker) {}

IN_PROC_BROWSER_TEST_F(FirstRunServiceBrowserTest,
                       OpenFirstRunIfNeededCalledTwice) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
IN_PROC_BROWSER_TEST_F(FirstRunServiceBrowserTest,
                       FinishedSilentlyAlreadySyncing) {
  signin::IdentityManager* identity_manager =
      identity_test_env()->identity_manager();
  CoreAccountId account_id =
      identity_manager->GetPrimaryAccountId(signin::ConsentLevel::kSignin);
  ASSERT_FALSE(account_id.empty());
  identity_manager->GetPrimaryAccountMutator()->SetPrimaryAccount(
      account_id, signin::ConsentLevel::kSync);
  base::HistogramTester histogram_tester;

  ASSERT_TRUE(profile()->IsMainProfile());
  EXPECT_TRUE(ShouldOpenFirstRun(profile()));

  ASSERT_TRUE(fre_service());

  // The FRE should be finished silently during the creation of the service.
  EXPECT_TRUE(GetFirstRunFinishedPrefValue());
  EXPECT_FALSE(fre_service()->ShouldOpenFirstRun());
}

IN_PROC_BROWSER_TEST_F(FirstRunServiceBrowserTest,
                       FinishedSilentlySyncConsentDisabled) {
  signin::IdentityManager* identity_manager =
      identity_test_env()->identity_manager();
  base::HistogramTester histogram_tester;

  profile()->GetPrefs()->SetBoolean(prefs::kEnableSyncConsent, false);
  EXPECT_FALSE(
      identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSync));

  ASSERT_TRUE(profile()->IsMainProfile());
  EXPECT_TRUE(ShouldOpenFirstRun(profile()));

  ASSERT_TRUE(fre_service());

  // The FRE should be finished silently during the creation of the service.
  EXPECT_TRUE(GetFirstRunFinishedPrefValue());
  EXPECT_FALSE(ShouldOpenFirstRun(profile()));

  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSync));
}

IN_PROC_BROWSER_TEST_F(FirstRunServiceBrowserTest,
                       FinishedSilentlyIsCurrentUserEphemeral) {
  signin::IdentityManager* identity_manager =
      identity_test_env()->identity_manager();
  base::HistogramTester histogram_tester;

  // Setup the ephemeral for Lacros.
  auto init_params = chromeos::BrowserInitParams::GetForTests()->Clone();
  init_params->is_current_user_ephemeral = true;
  chromeos::BrowserInitParams::SetInitParamsForTests(std::move(init_params));

  ASSERT_TRUE(profile()->IsMainProfile());
  EXPECT_TRUE(ShouldOpenFirstRun(profile()));

  ASSERT_TRUE(fre_service());

  EXPECT_TRUE(GetFirstRunFinishedPrefValue());
  EXPECT_FALSE(ShouldOpenFirstRun(profile()));

  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(identity_manager->HasPrimaryAccount(signin::ConsentLevel::kSync));
}
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
IN_PROC_BROWSER_TEST_F(FirstRunServiceBrowserTest, CloseProceeds) {}
#endif  // BUILDFLAG(ENABLE_DICE_SUPPORT)

struct PolicyTestParam {};

const PolicyTestParam kPolicyTestParams[] =;

std::string PolicyParamToTestSuffix(
    const ::testing::TestParamInfo<PolicyTestParam>& info) {}

class FirstRunServicePolicyBrowserTest
    : public FirstRunServiceBrowserTest,
      public testing::WithParamInterface<PolicyTestParam> {};

IN_PROC_BROWSER_TEST_P(FirstRunServicePolicyBrowserTest, OpenFirstRunIfNeeded) {}

INSTANTIATE_TEST_SUITE_P();