chromium/components/variations/field_trial_config/field_trial_util_unittest.cc

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO: crbug.com/352691908 - Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "components/variations/field_trial_config/field_trial_util.h"

#include <map>
#include <memory>
#include <optional>
#include <utility>

#include "base/command_line.h"
#include "base/containers/span.h"
#include "base/memory/raw_span.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/test/scoped_feature_list.h"
#include "components/variations/client_filterable_state.h"
#include "components/variations/field_trial_config/fieldtrial_testing_config.h"
#include "components/variations/service/variations_service_client.h"
#include "components/variations/variations_associated_data.h"
#include "components/variations/variations_seed_processor.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace variations {
namespace {

class ExperimentBuilder {};

class TestOverrideStringCallback {};

// TODO(crbug.com/40742801): Remove when fake VariationsServiceClient created.
class TestVariationsServiceClient : public VariationsServiceClient {};

class FieldTrialUtilTest : public ::testing::Test {};

}  // namespace

TEST_F(FieldTrialUtilTest, AssociateParamsFromString) {}

TEST_F(FieldTrialUtilTest, AssociateParamsFromStringWithSameTrial) {}

TEST_F(FieldTrialUtilTest, AssociateParamsFromFieldTrialConfig) {}

// Verifies that studies in the field trial config should be skipped if they
// enable/disable features that were overridden through the command line.
TEST_F(FieldTrialUtilTest, FieldTrialConfigSkipOverridden) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithEachPlatform) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithDifferentPlatform) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithMultiplePlatforms) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithAllFormFactors) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithSingleFormFactor) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithDifferentFormFactor) {}

TEST_F(FieldTrialUtilTest, AssociateFeaturesFromFieldTrialConfig) {}

TEST_F(FieldTrialUtilTest, AssociateForcingFlagsFromFieldTrialConfig) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithUIStringOverrides) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithIsLowEndDeviceMatch) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithIsLowEndDeviceMismatch) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithMinOsVersionMatch) {}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithMinOsVersionMismatch) {}

TEST_F(FieldTrialUtilTest, TestEscapeValue) {}

#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_ANDROID)
TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithHardwareClassMatch) {
  std::string hardware_class = ClientFilterableState::GetHardwareClass();
  std::string unmatched_hardware_class = hardware_class + "foo";
  const char* classes[] = {
      hardware_class.c_str(),
      unmatched_hardware_class.c_str(),
  };

  Study::Platform platform = Study::PLATFORM_CHROMEOS;
  ExperimentBuilder experiment_builder;
  experiment_builder.name = "TestGroup";
  experiment_builder.platforms = base::span_from_ref(platform);
  experiment_builder.hardware_classes = classes;
  FieldTrialTestingExperiment experiment = experiment_builder.Build();

  FieldTrialTestingStudy study = {
      /*name=*/"TestTrial",
      /*experiments=*/&experiment,
      /*experiments_size=*/1,
  };

  FieldTrialTestingConfig config = {
      /*studies=*/&study,
      /*studies_size=*/1,
  };

  base::FeatureList feature_list;
  AssociateParamsFromFieldTrialConfig(
      config, override_callback_.callback(), platform,
      variation_service_client_.GetCurrentFormFactor(), &feature_list);

  EXPECT_EQ("TestGroup", base::FieldTrialList::FindFullName("TestTrial"));
}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithHardwareClassMismatch) {
  std::string hardware_class = ClientFilterableState::GetHardwareClass();
  std::string unmatched_hardware_class = hardware_class + "foo";
  const char* classes[] = {
      unmatched_hardware_class.c_str(),
  };

  Study::Platform platform = Study::PLATFORM_CHROMEOS;
  ExperimentBuilder experiment_builder;
  experiment_builder.name = "TestGroup";
  experiment_builder.platforms = base::span_from_ref(platform);
  experiment_builder.hardware_classes = classes;
  FieldTrialTestingExperiment experiment = experiment_builder.Build();

  FieldTrialTestingStudy study = {
      /*name=*/"TestTrial",
      /*experiments=*/&experiment,
      /*experiments_size=*/1,
  };

  FieldTrialTestingConfig config = {
      /*studies=*/&study,
      /*studies_size=*/1,
  };

  base::FeatureList feature_list;
  AssociateParamsFromFieldTrialConfig(
      config, override_callback_.callback(), platform,
      variation_service_client_.GetCurrentFormFactor(), &feature_list);

  EXPECT_EQ("", base::FieldTrialList::FindFullName("TestTrial"));
}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithExcludHardwareClassMatch) {
  std::string hardware_class = ClientFilterableState::GetHardwareClass();
  std::string unmatched_hardware_class = hardware_class + "foo";
  const char* classes[] = {
      hardware_class.c_str(),
      unmatched_hardware_class.c_str(),
  };

  Study::Platform platform = Study::PLATFORM_CHROMEOS;
  ExperimentBuilder experiment_builder;
  experiment_builder.name = "TestGroup";
  experiment_builder.platforms = base::span_from_ref(platform);
  experiment_builder.exclude_hardware_classes = classes;
  FieldTrialTestingExperiment experiment = experiment_builder.Build();

  FieldTrialTestingStudy study = {
      /*name=*/"TestTrial",
      /*experiments=*/&experiment,
      /*experiments_size=*/1,
  };

  FieldTrialTestingConfig config = {
      /*studies=*/&study,
      /*studies_size=*/1,
  };

  base::FeatureList feature_list;
  AssociateParamsFromFieldTrialConfig(
      config, override_callback_.callback(), platform,
      variation_service_client_.GetCurrentFormFactor(), &feature_list);

  EXPECT_EQ("", base::FieldTrialList::FindFullName("TestTrial"));
}

TEST_F(FieldTrialUtilTest,
       AssociateParamsFromFieldTrialConfigWithExcludHardwareClassMismatch) {
  std::string hardware_class = ClientFilterableState::GetHardwareClass();
  std::string unmatched_hardware_class = hardware_class + "foo";
  const char* classes[] = {
      unmatched_hardware_class.c_str(),
  };

  Study::Platform platform = Study::PLATFORM_CHROMEOS;
  ExperimentBuilder experiment_builder;
  experiment_builder.name = "TestGroup";
  experiment_builder.platforms = base::span_from_ref(platform);
  experiment_builder.exclude_hardware_classes = classes;
  FieldTrialTestingExperiment experiment = experiment_builder.Build();

  FieldTrialTestingStudy study = {
      /*name=*/"TestTrial",
      /*experiments=*/&experiment,
      /*experiments_size=*/1,
  };

  FieldTrialTestingConfig config = {
      /*studies=*/&study,
      /*studies_size=*/1,
  };

  base::FeatureList feature_list;
  AssociateParamsFromFieldTrialConfig(
      config, override_callback_.callback(), platform,
      variation_service_client_.GetCurrentFormFactor(), &feature_list);

  EXPECT_EQ("TestGroup", base::FieldTrialList::FindFullName("TestTrial"));
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_ANDROID)

}  // namespace variations