variations_field_trial_creator_unittest.cc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. // Copyright 2017 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. #include "components/variations/service/variations_field_trial_creator.h"
  5. #include <stddef.h>
  6. #include <cstring>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "base/base_switches.h"
  11. #include "base/callback.h"
  12. #include "base/callback_helpers.h"
  13. #include "base/command_line.h"
  14. #include "base/feature_list.h"
  15. #include "base/files/file_path.h"
  16. #include "base/files/file_util.h"
  17. #include "base/files/scoped_temp_dir.h"
  18. #include "base/memory/raw_ptr.h"
  19. #include "base/metrics/field_trial_params.h"
  20. #include "base/strings/stringprintf.h"
  21. #include "base/test/metrics/histogram_tester.h"
  22. #include "base/test/mock_entropy_provider.h"
  23. #include "base/test/task_environment.h"
  24. #include "base/time/time.h"
  25. #include "base/version.h"
  26. #include "build/branding_buildflags.h"
  27. #include "build/build_config.h"
  28. #include "components/metrics/clean_exit_beacon.h"
  29. #include "components/metrics/client_info.h"
  30. #include "components/metrics/metrics_service.h"
  31. #include "components/metrics/metrics_state_manager.h"
  32. #include "components/metrics/test/test_enabled_state_provider.h"
  33. #include "components/prefs/testing_pref_service.h"
  34. #include "components/variations/field_trial_config/field_trial_util.h"
  35. #include "components/variations/platform_field_trials.h"
  36. #include "components/variations/pref_names.h"
  37. #include "components/variations/proto/variations_seed.pb.h"
  38. #include "components/variations/scoped_variations_ids_provider.h"
  39. #include "components/variations/service/buildflags.h"
  40. #include "components/variations/service/safe_seed_manager.h"
  41. #include "components/variations/service/variations_service.h"
  42. #include "components/variations/service/variations_service_client.h"
  43. #include "components/variations/variations_seed_store.h"
  44. #include "components/variations/variations_switches.h"
  45. #include "components/variations/variations_test_utils.h"
  46. #include "components/version_info/channel.h"
  47. #include "components/version_info/version_info.h"
  48. #include "services/network/public/cpp/shared_url_loader_factory.h"
  49. #include "testing/gmock/include/gmock/gmock.h"
  50. #include "testing/gtest/include/gtest/gtest.h"
  51. #if BUILDFLAG(IS_ANDROID)
  52. #include "components/variations/seed_response.h"
  53. #endif
  54. namespace variations {
  55. namespace {
  56. using ::testing::_;
  57. using ::testing::Ge;
  58. using ::testing::NiceMock;
  59. using ::testing::Return;
  60. // Constants used to create the test seeds.
  61. const char kTestSeedStudyName[] = "test";
  62. const char kTestSeedExperimentName[] = "abc";
  63. const char kTestSafeSeedExperimentName[] = "abc.safe";
  64. const int kTestSeedExperimentProbability = 100;
  65. const char kTestSeedSerialNumber[] = "123";
  66. // Constants used to mock the serialized seed state.
  67. const char kTestSeedSerializedData[] = "a serialized seed, 100% realistic";
  68. const char kTestSeedSignature[] = "a totally valid signature, I swear!";
  69. const int kTestSeedMilestone = 90;
  70. // Used for similar tests.
  71. struct TestParams {
  72. // Inputs.
  73. int days;
  74. const base::Time binary_build_time;
  75. };
  76. // Returns a seed with simple test data. The seed has a single study,
  77. // "UMA-Uniformity-Trial-10-Percent", which has a single experiment, "abc", with
  78. // probability weight 100.
  79. VariationsSeed CreateTestSeed() {
  80. VariationsSeed seed;
  81. Study* study = seed.add_study();
  82. study->set_name(kTestSeedStudyName);
  83. study->set_default_experiment_name(kTestSeedExperimentName);
  84. Study_Experiment* experiment = study->add_experiment();
  85. experiment->set_name(kTestSeedExperimentName);
  86. experiment->set_probability_weight(kTestSeedExperimentProbability);
  87. seed.set_serial_number(kTestSeedSerialNumber);
  88. return seed;
  89. }
  90. // Returns a seed with simple test data. The seed has a single study,
  91. // "UMA-Uniformity-Trial-10-Percent", which has a single experiment,
  92. // "abc.safe", with probability weight 100.
  93. //
  94. // Intended to be used when a "safe" seed is needed so that test expectations
  95. // can distinguish between a regular and safe seeds.
  96. VariationsSeed CreateTestSafeSeed() {
  97. VariationsSeed seed = CreateTestSeed();
  98. Study* study = seed.mutable_study(0);
  99. study->set_default_experiment_name(kTestSafeSeedExperimentName);
  100. study->mutable_experiment(0)->set_name(kTestSafeSeedExperimentName);
  101. return seed;
  102. }
  103. #if BUILDFLAG(IS_ANDROID)
  104. const char kTestSeedCountry[] = "in";
  105. // Populates |seed| with simple test data, targetting only users in a specific
  106. // country. The resulting seed will contain one study called "test", which
  107. // contains one experiment called "abc" with probability weight 100, restricted
  108. // just to users in |kTestSeedCountry|.
  109. VariationsSeed CreateTestSeedWithCountryFilter() {
  110. VariationsSeed seed = CreateTestSeed();
  111. Study* study = seed.mutable_study(0);
  112. Study::Filter* filter = study->mutable_filter();
  113. filter->add_country(kTestSeedCountry);
  114. filter->add_platform(Study::PLATFORM_ANDROID);
  115. return seed;
  116. }
  117. // Serializes |seed| to protobuf binary format.
  118. std::string SerializeSeed(const VariationsSeed& seed) {
  119. std::string serialized_seed;
  120. seed.SerializeToString(&serialized_seed);
  121. return serialized_seed;
  122. }
  123. #endif // BUILDFLAG(IS_ANDROID)
  124. class TestPlatformFieldTrials : public PlatformFieldTrials {
  125. public:
  126. TestPlatformFieldTrials() = default;
  127. TestPlatformFieldTrials(const TestPlatformFieldTrials&) = delete;
  128. TestPlatformFieldTrials& operator=(const TestPlatformFieldTrials&) = delete;
  129. ~TestPlatformFieldTrials() override = default;
  130. // PlatformFieldTrials:
  131. void SetUpFieldTrials() override {}
  132. void SetUpFeatureControllingFieldTrials(
  133. bool has_seed,
  134. const base::FieldTrial::EntropyProvider* low_entropy_provider,
  135. base::FeatureList* feature_list) override {}
  136. };
  137. class MockSafeSeedManager : public SafeSeedManager {
  138. public:
  139. explicit MockSafeSeedManager(PrefService* local_state)
  140. : SafeSeedManager(local_state) {}
  141. MockSafeSeedManager(const MockSafeSeedManager&) = delete;
  142. MockSafeSeedManager& operator=(const MockSafeSeedManager&) = delete;
  143. ~MockSafeSeedManager() override = default;
  144. // Returns false by default.
  145. MOCK_CONST_METHOD0(ShouldRunInSafeMode, bool());
  146. MOCK_METHOD5(DoSetActiveSeedState,
  147. void(const std::string& seed_data,
  148. const std::string& base64_seed_signature,
  149. int seed_milestone,
  150. ClientFilterableState* client_filterable_state,
  151. base::Time seed_fetch_time));
  152. void SetActiveSeedState(
  153. const std::string& seed_data,
  154. const std::string& base64_seed_signature,
  155. int seed_milestone,
  156. std::unique_ptr<ClientFilterableState> client_filterable_state,
  157. base::Time seed_fetch_time) override {
  158. DoSetActiveSeedState(seed_data, base64_seed_signature, seed_milestone,
  159. client_filterable_state.get(), seed_fetch_time);
  160. }
  161. };
  162. // TODO(crbug/1167566): Remove when fake VariationsServiceClient created.
  163. class TestVariationsServiceClient : public VariationsServiceClient {
  164. public:
  165. TestVariationsServiceClient() = default;
  166. TestVariationsServiceClient(const TestVariationsServiceClient&) = delete;
  167. TestVariationsServiceClient& operator=(const TestVariationsServiceClient&) =
  168. delete;
  169. ~TestVariationsServiceClient() override = default;
  170. // VariationsServiceClient:
  171. base::Version GetVersionForSimulation() override { return base::Version(); }
  172. scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory()
  173. override {
  174. return nullptr;
  175. }
  176. network_time::NetworkTimeTracker* GetNetworkTimeTracker() override {
  177. return nullptr;
  178. }
  179. bool OverridesRestrictParameter(std::string* parameter) override {
  180. if (restrict_parameter_.empty())
  181. return false;
  182. *parameter = restrict_parameter_;
  183. return true;
  184. }
  185. bool IsEnterprise() override { return false; }
  186. private:
  187. // VariationsServiceClient:
  188. version_info::Channel GetChannel() override {
  189. return version_info::Channel::UNKNOWN;
  190. }
  191. std::string restrict_parameter_;
  192. };
  193. class MockVariationsServiceClient : public TestVariationsServiceClient {
  194. public:
  195. MOCK_METHOD(version_info::Channel, GetChannel, (), (override));
  196. };
  197. class TestVariationsSeedStore : public VariationsSeedStore {
  198. public:
  199. explicit TestVariationsSeedStore(PrefService* local_state)
  200. : VariationsSeedStore(local_state) {}
  201. TestVariationsSeedStore(const TestVariationsSeedStore&) = delete;
  202. TestVariationsSeedStore& operator=(const TestVariationsSeedStore&) = delete;
  203. ~TestVariationsSeedStore() override = default;
  204. bool LoadSeed(VariationsSeed* seed,
  205. std::string* seed_data,
  206. std::string* base64_signature) override {
  207. *seed = CreateTestSeed();
  208. *seed_data = kTestSeedSerializedData;
  209. *base64_signature = kTestSeedSignature;
  210. return true;
  211. }
  212. bool LoadSafeSeed(VariationsSeed* seed,
  213. ClientFilterableState* client_state) override {
  214. if (has_unloadable_safe_seed_)
  215. return false;
  216. *seed = CreateTestSafeSeed();
  217. return true;
  218. }
  219. void set_has_unloadable_safe_seed(bool is_unloadable) {
  220. has_unloadable_safe_seed_ = is_unloadable;
  221. }
  222. private:
  223. // Whether to simulate having an unloadable (e.g. corrupted, empty, etc.) safe
  224. // seed.
  225. bool has_unloadable_safe_seed_ = false;
  226. };
  227. class TestVariationsFieldTrialCreator : public VariationsFieldTrialCreator {
  228. public:
  229. TestVariationsFieldTrialCreator(
  230. PrefService* local_state,
  231. TestVariationsServiceClient* client,
  232. SafeSeedManager* safe_seed_manager,
  233. const base::FilePath user_data_dir = base::FilePath(),
  234. metrics::StartupVisibility startup_visibility =
  235. metrics::StartupVisibility::kUnknown)
  236. : VariationsFieldTrialCreator(
  237. client,
  238. std::make_unique<VariationsSeedStore>(local_state),
  239. UIStringOverrider()),
  240. enabled_state_provider_(/*consent=*/true, /*enabled=*/true),
  241. seed_store_(local_state),
  242. safe_seed_manager_(safe_seed_manager),
  243. build_time_(base::Time::Now()) {
  244. metrics_state_manager_ = metrics::MetricsStateManager::Create(
  245. local_state, &enabled_state_provider_, std::wstring(), user_data_dir,
  246. startup_visibility);
  247. metrics_state_manager_->InstantiateFieldTrialList();
  248. }
  249. TestVariationsFieldTrialCreator(const TestVariationsFieldTrialCreator&) =
  250. delete;
  251. TestVariationsFieldTrialCreator& operator=(
  252. const TestVariationsFieldTrialCreator&) = delete;
  253. ~TestVariationsFieldTrialCreator() override = default;
  254. // A convenience wrapper around SetUpFieldTrials() which passes default values
  255. // for uninteresting params.
  256. bool SetUpFieldTrials() {
  257. TestPlatformFieldTrials platform_field_trials;
  258. return VariationsFieldTrialCreator::SetUpFieldTrials(
  259. /*variation_ids=*/std::vector<std::string>(),
  260. base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
  261. switches::kForceVariationIds),
  262. std::vector<base::FeatureList::FeatureOverrideInfo>(),
  263. /*low_entropy_provider=*/nullptr, std::make_unique<base::FeatureList>(),
  264. metrics_state_manager_.get(), &platform_field_trials,
  265. safe_seed_manager_, /*low_entropy_source_value=*/absl::nullopt);
  266. }
  267. TestVariationsSeedStore* seed_store() { return &seed_store_; }
  268. void SetBuildTime(const base::Time& time) { build_time_ = time; }
  269. protected:
  270. #if BUILDFLAG(FIELDTRIAL_TESTING_ENABLED)
  271. // We override this method so that a mock testing config is used instead of
  272. // the one defined in fieldtrial_testing_config.json.
  273. void ApplyFieldTrialTestingConfig(base::FeatureList* feature_list) override {
  274. AssociateParamsFromFieldTrialConfig(
  275. kTestingConfig,
  276. base::BindRepeating(&TestVariationsFieldTrialCreator::OverrideUIString,
  277. base::Unretained(this)),
  278. GetPlatform(), GetCurrentFormFactor(), feature_list);
  279. }
  280. #endif // BUILDFLAG(FIELDTRIAL_TESTING_ENABLED)
  281. private:
  282. VariationsSeedStore* GetSeedStore() override { return &seed_store_; }
  283. base::Time GetBuildTime() const override { return build_time_; }
  284. metrics::TestEnabledStateProvider enabled_state_provider_;
  285. TestVariationsSeedStore seed_store_;
  286. const raw_ptr<SafeSeedManager> safe_seed_manager_;
  287. base::Time build_time_;
  288. std::unique_ptr<metrics::MetricsStateManager> metrics_state_manager_;
  289. };
  290. } // namespace
  291. class FieldTrialCreatorTest : public ::testing::Test {
  292. public:
  293. FieldTrialCreatorTest() {
  294. metrics::MetricsService::RegisterPrefs(local_state_.registry());
  295. VariationsService::RegisterPrefs(local_state_.registry());
  296. global_feature_list_ = base::FeatureList::ClearInstanceForTesting();
  297. }
  298. FieldTrialCreatorTest(const FieldTrialCreatorTest&) = delete;
  299. FieldTrialCreatorTest& operator=(const FieldTrialCreatorTest&) = delete;
  300. ~FieldTrialCreatorTest() override {
  301. // Clear out any features created by tests in this suite and restore the
  302. // global feature list.
  303. base::FeatureList::ClearInstanceForTesting();
  304. base::FeatureList::RestoreInstanceForTesting(
  305. std::move(global_feature_list_));
  306. }
  307. void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
  308. void ResetFeatureList() {
  309. base::FeatureList::ClearInstanceForTesting();
  310. base::FeatureList::RestoreInstanceForTesting(
  311. std::move(global_feature_list_));
  312. global_feature_list_ = base::FeatureList::ClearInstanceForTesting();
  313. }
  314. PrefService* local_state() { return &local_state_; }
  315. const base::FilePath user_data_dir_path() const {
  316. return temp_dir_.GetPath();
  317. }
  318. private:
  319. TestingPrefServiceSimple local_state_;
  320. base::ScopedTempDir temp_dir_;
  321. variations::ScopedVariationsIdsProvider scoped_variations_ids_provider_{
  322. variations::VariationsIdsProvider::Mode::kUseSignedInState};
  323. // The global feature list, which is ignored by tests in this suite.
  324. std::unique_ptr<base::FeatureList> global_feature_list_;
  325. };
  326. struct StartupVisibilityTestParams {
  327. const std::string test_name;
  328. metrics::StartupVisibility startup_visibility;
  329. bool extend_safe_mode;
  330. };
  331. class FieldTrialCreatorTestWithStartupVisibility
  332. : public FieldTrialCreatorTest,
  333. public ::testing::WithParamInterface<StartupVisibilityTestParams> {};
  334. // Verify that unexpired seeds are used.
  335. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_ValidSeed_NotExpired) {
  336. DisableTestingConfig();
  337. const base::Time now = base::Time::Now();
  338. std::vector<TestParams> test_cases = {
  339. // Verify that when the binary is newer than the most recent seed, the
  340. // seed is applied as long as it was downloaded within the last 30 days.
  341. {.days = 30, .binary_build_time = now},
  342. // Verify that when the binary is older than the most recent seed, the
  343. // seed is applied even though it was downloaded more than 30 days ago.
  344. {.days = 31, .binary_build_time = now - base::Days(32)}};
  345. for (const TestParams& test_case : test_cases) {
  346. const base::Time seed_fetch_time = now - base::Days(test_case.days);
  347. // The seed should be used, so the safe seed manager should be informed of
  348. // the active seed state.
  349. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  350. EXPECT_CALL(
  351. safe_seed_manager,
  352. DoSetActiveSeedState(kTestSeedSerializedData, kTestSeedSignature,
  353. kTestSeedMilestone, _, seed_fetch_time))
  354. .Times(1);
  355. TestVariationsServiceClient variations_service_client;
  356. TestVariationsFieldTrialCreator field_trial_creator(
  357. local_state(), &variations_service_client, &safe_seed_manager);
  358. field_trial_creator.SetBuildTime(test_case.binary_build_time);
  359. // Simulate the seed being stored.
  360. local_state()->SetTime(prefs::kVariationsLastFetchTime, seed_fetch_time);
  361. // Simulate a seed from an earlier (i.e. valid) milestone.
  362. local_state()->SetInteger(prefs::kVariationsSeedMilestone,
  363. kTestSeedMilestone);
  364. // Check that field trials are created from the seed. Since the test study
  365. // has only one experiment with 100% probability weight, we must be part of
  366. // it.
  367. base::HistogramTester histogram_tester;
  368. EXPECT_TRUE(field_trial_creator.SetUpFieldTrials());
  369. EXPECT_EQ(kTestSeedExperimentName,
  370. base::FieldTrialList::FindFullName(kTestSeedStudyName));
  371. // Verify metrics.
  372. histogram_tester.ExpectUniqueSample("Variations.CreateTrials.SeedExpiry",
  373. VariationsSeedExpiry::kNotExpired, 1);
  374. int freshness_in_minutes = test_case.days * 24 * 60;
  375. histogram_tester.ExpectUniqueSample("Variations.SeedFreshness",
  376. freshness_in_minutes, 1);
  377. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  378. SeedUsage::kRegularSeedUsed, 1);
  379. histogram_tester.ExpectUniqueSample("Variations.AppliedSeed.Size",
  380. strlen(kTestSeedSerializedData), 1);
  381. ResetFeatureList();
  382. }
  383. }
  384. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_ValidSeed_NoLastFetchTime) {
  385. DisableTestingConfig();
  386. // With a valid seed on first run, the safe seed manager should be informed of
  387. // the active seed state. The last fetch time in this case is expected to be
  388. // inferred to be recent.
  389. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  390. const base::Time start_time = base::Time::Now();
  391. EXPECT_CALL(safe_seed_manager,
  392. DoSetActiveSeedState(kTestSeedSerializedData, kTestSeedSignature,
  393. _, _, Ge(start_time)))
  394. .Times(1);
  395. TestVariationsServiceClient variations_service_client;
  396. TestVariationsFieldTrialCreator field_trial_creator(
  397. local_state(), &variations_service_client, &safe_seed_manager);
  398. // Simulate a first run by leaving |prefs::kVariationsLastFetchTime| empty.
  399. EXPECT_EQ(0, local_state()->GetInt64(prefs::kVariationsLastFetchTime));
  400. // Check that field trials are created from the seed. Since the test study has
  401. // only one experiment with 100% probability weight, we must be part of it.
  402. base::HistogramTester histogram_tester;
  403. EXPECT_TRUE(field_trial_creator.SetUpFieldTrials());
  404. EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName),
  405. kTestSeedExperimentName);
  406. // Verify metrics. The seed freshness metric should be recorded with a value
  407. // of 0 on first run.
  408. histogram_tester.ExpectUniqueSample("Variations.CreateTrials.SeedExpiry",
  409. VariationsSeedExpiry::kFetchTimeMissing,
  410. 1);
  411. histogram_tester.ExpectUniqueSample("Variations.SeedFreshness", 0, 1);
  412. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  413. SeedUsage::kRegularSeedUsed, 1);
  414. }
  415. // Verify that a regular seed can be used when the milestone with which the seed
  416. // was fetched is unknown. This can happen if the seed was fetched before the
  417. // milestone pref was added.
  418. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_ValidSeed_NoMilestone) {
  419. DisableTestingConfig();
  420. // The regular seed should be used, so the safe seed manager should be
  421. // informed of the active seed state.
  422. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  423. const int minutes = 45;
  424. const base::Time seed_fetch_time = base::Time::Now() - base::Minutes(minutes);
  425. EXPECT_CALL(safe_seed_manager,
  426. DoSetActiveSeedState(kTestSeedSerializedData, kTestSeedSignature,
  427. 0, _, seed_fetch_time))
  428. .Times(1);
  429. TestVariationsServiceClient variations_service_client;
  430. TestVariationsFieldTrialCreator field_trial_creator(
  431. local_state(), &variations_service_client, &safe_seed_manager);
  432. // Simulate the seed being stored.
  433. local_state()->SetTime(prefs::kVariationsLastFetchTime, seed_fetch_time);
  434. // Simulate the absence of a milestone by leaving
  435. // |prefs::kVariationsSeedMilestone| empty.
  436. EXPECT_EQ(0, local_state()->GetInteger(prefs::kVariationsSeedMilestone));
  437. // Check that field trials are created from the seed. Since the test study has
  438. // only one experiment with 100% probability weight, we must be part of it.
  439. base::HistogramTester histogram_tester;
  440. EXPECT_TRUE(field_trial_creator.SetUpFieldTrials());
  441. EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName),
  442. kTestSeedExperimentName);
  443. // Verify metrics.
  444. histogram_tester.ExpectUniqueSample("Variations.CreateTrials.SeedExpiry",
  445. VariationsSeedExpiry::kNotExpired, 1);
  446. histogram_tester.ExpectUniqueSample("Variations.SeedFreshness", minutes, 1);
  447. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  448. SeedUsage::kRegularSeedUsed, 1);
  449. }
  450. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_ExpiredSeed) {
  451. DisableTestingConfig();
  452. // When the seed is older than 30 days and older than the binary, no field
  453. // trials should be created from the seed. Hence, no active state should be
  454. // passed to the safe seed manager.
  455. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  456. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  457. TestVariationsServiceClient variations_service_client;
  458. TestVariationsFieldTrialCreator field_trial_creator(
  459. local_state(), &variations_service_client, &safe_seed_manager);
  460. const base::Time now = base::Time::Now();
  461. field_trial_creator.SetBuildTime(now);
  462. // Simulate an expired seed. For a seed to be expired, it must be older than
  463. // 30 days and be older than the binary.
  464. const base::Time seed_date = now - base::Days(31);
  465. local_state()->SetTime(prefs::kVariationsLastFetchTime, seed_date);
  466. // Check that field trials are not created from the expired seed.
  467. base::HistogramTester histogram_tester;
  468. EXPECT_FALSE(field_trial_creator.SetUpFieldTrials());
  469. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  470. // Verify metrics. The seed freshness metric should not be recorded for an
  471. // expired seed.
  472. histogram_tester.ExpectUniqueSample("Variations.CreateTrials.SeedExpiry",
  473. VariationsSeedExpiry::kExpired, 1);
  474. histogram_tester.ExpectTotalCount("Variations.SeedFreshness", 0);
  475. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  476. SeedUsage::kExpiredRegularSeedNotUsed, 1);
  477. }
  478. // Verify that a regular seed is not used when the milestone with which it was
  479. // fetched is greater than the client's milestone.
  480. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_FutureMilestone) {
  481. DisableTestingConfig();
  482. const int future_seed_milestone = 7890;
  483. // When the seed is associated with a future milestone (relative to the
  484. // client's milestone), no field trials should be created from the seed.
  485. // Hence, no active state should be passed to the safe seed manager.
  486. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  487. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  488. TestVariationsServiceClient variations_service_client;
  489. TestVariationsFieldTrialCreator field_trial_creator(
  490. local_state(), &variations_service_client, &safe_seed_manager);
  491. const base::Time now = base::Time::Now();
  492. field_trial_creator.SetBuildTime(now);
  493. // Simulate a seed from a future milestone.
  494. local_state()->SetInteger(prefs::kVariationsSeedMilestone,
  495. future_seed_milestone);
  496. // Check that field trials are not created from the seed.
  497. base::HistogramTester histogram_tester;
  498. EXPECT_FALSE(field_trial_creator.SetUpFieldTrials());
  499. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  500. // Verify metrics.
  501. histogram_tester.ExpectUniqueSample(
  502. "Variations.SeedUsage", SeedUsage::kRegularSeedForFutureMilestoneNotUsed,
  503. 1);
  504. }
  505. // Verify that unexpired safe seeds are used.
  506. TEST_F(FieldTrialCreatorTest,
  507. SetUpFieldTrials_ValidSafeSeed_NewBinaryUsesSeed) {
  508. DisableTestingConfig();
  509. const base::Time now = base::Time::Now();
  510. std::vector<TestParams> test_cases = {
  511. // Verify that when (i) safe mode is triggered and (ii) the binary is
  512. // newer than the safe seed, the safe seed is applied as long as it was
  513. // downloaded within the last 30 days.
  514. {.days = 30, .binary_build_time = now},
  515. // Verify that when (i) safe mode is triggered and (ii) the binary is
  516. // older than the safe seed, the safe seed is applied even though it was
  517. // downloaded more than 30 days ago.
  518. {.days = 31, .binary_build_time = now - base::Days(32)}};
  519. for (const TestParams& test_case : test_cases) {
  520. // With a valid safe seed, the safe seed manager should not be informed of
  521. // the active seed state. This is an optimization to avoid saving a safe
  522. // seed when already running in safe mode.
  523. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  524. ON_CALL(safe_seed_manager, ShouldRunInSafeMode())
  525. .WillByDefault(Return(true));
  526. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _))
  527. .Times(0);
  528. TestVariationsServiceClient variations_service_client;
  529. TestVariationsFieldTrialCreator field_trial_creator(
  530. local_state(), &variations_service_client, &safe_seed_manager);
  531. field_trial_creator.SetBuildTime(test_case.binary_build_time);
  532. // Simulate the safe seed being stored.
  533. const base::Time seed_fetch_time = now - base::Days(test_case.days);
  534. local_state()->SetTime(prefs::kVariationsSafeSeedFetchTime,
  535. seed_fetch_time);
  536. // Check that field trials are created from the safe seed. Since the test
  537. // study has only one experiment with 100% probability weight, we must be
  538. // part of it.
  539. base::HistogramTester histogram_tester;
  540. EXPECT_TRUE(field_trial_creator.SetUpFieldTrials());
  541. EXPECT_EQ(kTestSafeSeedExperimentName,
  542. base::FieldTrialList::FindFullName(kTestSeedStudyName));
  543. // Verify metrics.
  544. histogram_tester.ExpectUniqueSample(
  545. "Variations.SafeMode.CreateTrials.SeedExpiry",
  546. VariationsSeedExpiry::kNotExpired, 1);
  547. int freshness_in_minutes = test_case.days * 24 * 60;
  548. histogram_tester.ExpectUniqueSample("Variations.SeedFreshness",
  549. freshness_in_minutes, 1);
  550. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  551. SeedUsage::kSafeSeedUsed, 1);
  552. ResetFeatureList();
  553. }
  554. }
  555. // Verify that Chrome does not apply a variations seed when Chrome should run in
  556. // Variations Safe Mode but the safe seed is unloadable.
  557. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_UnloadableSafeSeedNotUsed) {
  558. DisableTestingConfig();
  559. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  560. ON_CALL(safe_seed_manager, ShouldRunInSafeMode()).WillByDefault(Return(true));
  561. // When falling back to client-side defaults, the safe seed manager should not
  562. // be informed of the active seed state.
  563. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  564. TestVariationsServiceClient variations_service_client;
  565. TestVariationsFieldTrialCreator field_trial_creator(
  566. local_state(), &variations_service_client, &safe_seed_manager);
  567. field_trial_creator.seed_store()->set_has_unloadable_safe_seed(true);
  568. base::HistogramTester histogram_tester;
  569. // Verify that field trials were not set up.
  570. EXPECT_FALSE(field_trial_creator.SetUpFieldTrials());
  571. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  572. // Verify that Chrome did not apply the safe seed.
  573. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  574. SeedUsage::kUnloadableSafeSeedNotUsed, 1);
  575. }
  576. // Verify that valid safe seeds with missing download times are applied.
  577. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_ValidSafeSeed_NoLastFetchTime) {
  578. DisableTestingConfig();
  579. // With a valid safe seed, the safe seed manager should not be informed of the
  580. // active seed state. This is an optimization to avoid saving a safe seed when
  581. // already running in safe mode.
  582. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  583. ON_CALL(safe_seed_manager, ShouldRunInSafeMode()).WillByDefault(Return(true));
  584. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  585. TestVariationsServiceClient variations_service_client;
  586. TestVariationsFieldTrialCreator field_trial_creator(
  587. local_state(), &variations_service_client, &safe_seed_manager);
  588. // Verify that the safe seed does not have a fetch time.
  589. EXPECT_EQ(0, local_state()->GetInt64(prefs::kVariationsSafeSeedFetchTime));
  590. // Check that field trials are created from the safe seed. Since the test
  591. // study has only one experiment with 100% probability weight, we must be part
  592. // of it.
  593. base::HistogramTester histogram_tester;
  594. EXPECT_TRUE(field_trial_creator.SetUpFieldTrials());
  595. EXPECT_EQ(kTestSafeSeedExperimentName,
  596. base::FieldTrialList::FindFullName(kTestSeedStudyName));
  597. // Verify metrics. The freshness should not be recorded when the fetch time is
  598. // missing.
  599. histogram_tester.ExpectUniqueSample(
  600. "Variations.SafeMode.CreateTrials.SeedExpiry",
  601. VariationsSeedExpiry::kFetchTimeMissing, 1);
  602. histogram_tester.ExpectTotalCount("Variations.SeedFreshness", 0);
  603. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  604. SeedUsage::kSafeSeedUsed, 1);
  605. }
  606. // Verify that no seed is applied when (i) safe mode is triggered and (ii) the
  607. // loaded safe seed has expired.
  608. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_ExpiredSafeSeed) {
  609. DisableTestingConfig();
  610. // The safe seed manager should not be informed of the active seed state.
  611. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  612. ON_CALL(safe_seed_manager, ShouldRunInSafeMode()).WillByDefault(Return(true));
  613. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  614. TestVariationsServiceClient variations_service_client;
  615. TestVariationsFieldTrialCreator field_trial_creator(
  616. local_state(), &variations_service_client, &safe_seed_manager);
  617. const base::Time now = base::Time::Now();
  618. field_trial_creator.SetBuildTime(now);
  619. // Simulate an expired seed. For a seed to be expired, it must be older than
  620. // 30 days and be older than the binary.
  621. const base::Time seed_date = now - base::Days(31);
  622. local_state()->SetTime(prefs::kVariationsSafeSeedFetchTime, seed_date);
  623. // Check that field trials are not created from the expired seed.
  624. base::HistogramTester histogram_tester;
  625. EXPECT_FALSE(field_trial_creator.SetUpFieldTrials());
  626. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  627. // Verify metrics. The seed freshness metric should not be recorded for an
  628. // expired seed.
  629. histogram_tester.ExpectUniqueSample(
  630. "Variations.SafeMode.CreateTrials.SeedExpiry",
  631. VariationsSeedExpiry::kExpired, 1);
  632. histogram_tester.ExpectTotalCount("Variations.SeedFreshness", 0);
  633. histogram_tester.ExpectUniqueSample("Variations.SeedUsage",
  634. SeedUsage::kExpiredSafeSeedNotUsed, 1);
  635. }
  636. // Verify that no seed is applied when (i) safe mode is triggered and (ii) the
  637. // loaded safe seed was fetched with a future milestone.
  638. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_SafeSeedForFutureMilestone) {
  639. DisableTestingConfig();
  640. const int future_seed_milestone = 7890;
  641. // The safe seed manager should not be informed of the active seed state.
  642. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  643. ON_CALL(safe_seed_manager, ShouldRunInSafeMode()).WillByDefault(Return(true));
  644. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  645. TestVariationsServiceClient variations_service_client;
  646. TestVariationsFieldTrialCreator field_trial_creator(
  647. local_state(), &variations_service_client, &safe_seed_manager);
  648. // Simulate a safe seed that was fetched with a future milestone.
  649. local_state()->SetInteger(prefs::kVariationsSafeSeedMilestone,
  650. future_seed_milestone);
  651. // Check that field trials are not created from the safe seed.
  652. base::HistogramTester histogram_tester;
  653. EXPECT_FALSE(field_trial_creator.SetUpFieldTrials());
  654. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  655. // Verify metrics.
  656. histogram_tester.ExpectUniqueSample(
  657. "Variations.SeedUsage", SeedUsage::kSafeSeedForFutureMilestoneNotUsed, 1);
  658. }
  659. TEST_F(FieldTrialCreatorTest, LoadSeedFromTestSeedPath) {
  660. DisableTestingConfig();
  661. base::ScopedTempDir temp_dir;
  662. ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  663. const base::FilePath test_seed_file =
  664. temp_dir.GetPath().Append(FILE_PATH_LITERAL("TEST SEED"));
  665. // This seed contains the data for a test experiment.
  666. base::WriteFile(test_seed_file,
  667. base::StringPrintf("{\"variations_compressed_seed\": \"%s\","
  668. "\"variations_seed_signature\": \"%s\"}",
  669. kTestSeedData.base64_compressed_data,
  670. kTestSeedData.base64_signature));
  671. base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
  672. variations::switches::kVariationsTestSeedPath, test_seed_file);
  673. // Use a real VariationsFieldTrialCreator and VariationsSeedStore to exercise
  674. // the VariationsSeedStore::LoadSeed() logic.
  675. TestVariationsServiceClient variations_service_client;
  676. auto seed_store = std::make_unique<VariationsSeedStore>(local_state());
  677. VariationsFieldTrialCreator field_trial_creator(
  678. &variations_service_client, std::move(seed_store), UIStringOverrider());
  679. metrics::TestEnabledStateProvider enabled_state_provider(
  680. /*consent=*/true,
  681. /*enabled=*/true);
  682. auto metrics_state_manager = metrics::MetricsStateManager::Create(
  683. local_state(), &enabled_state_provider, std::wstring(), base::FilePath());
  684. TestPlatformFieldTrials platform_field_trials;
  685. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  686. ASSERT_FALSE(base::FieldTrialList::TrialExists(kTestSeedData.study_names[0]));
  687. EXPECT_TRUE(field_trial_creator.SetUpFieldTrials(
  688. /*variation_ids=*/{},
  689. /*command_line_variation_ids=*/std::string(),
  690. std::vector<base::FeatureList::FeatureOverrideInfo>(),
  691. /*low_entropy_provider=*/nullptr, std::make_unique<base::FeatureList>(),
  692. metrics_state_manager.get(), &platform_field_trials, &safe_seed_manager,
  693. /*low_entropy_source_value=*/absl::nullopt));
  694. EXPECT_TRUE(base::FieldTrialList::TrialExists(kTestSeedData.study_names[0]));
  695. EXPECT_EQ(
  696. local_state()->GetInteger(prefs::kVariationsFailedToFetchSeedStreak), 0);
  697. EXPECT_EQ(local_state()->GetInteger(prefs::kVariationsCrashStreak), 0);
  698. }
  699. #if BUILDFLAG(IS_ANDROID)
  700. // This is a regression test for crbug/829527.
  701. TEST_F(FieldTrialCreatorTest, SetUpFieldTrials_LoadsCountryOnFirstRun) {
  702. DisableTestingConfig();
  703. // Simulate having received a seed in Java during First Run.
  704. const base::Time one_day_ago = base::Time::Now() - base::Days(1);
  705. auto initial_seed = std::make_unique<SeedResponse>();
  706. initial_seed->data = SerializeSeed(CreateTestSeedWithCountryFilter());
  707. initial_seed->signature = kTestSeedSignature;
  708. initial_seed->country = kTestSeedCountry;
  709. initial_seed->date = one_day_ago.ToJavaTime();
  710. initial_seed->is_gzip_compressed = false;
  711. TestVariationsServiceClient variations_service_client;
  712. TestPlatformFieldTrials platform_field_trials;
  713. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  714. // Note: Unlike other tests, this test does not mock out the seed store, since
  715. // the interaction between these two classes is what's being tested.
  716. auto seed_store = std::make_unique<VariationsSeedStore>(
  717. local_state(), std::move(initial_seed),
  718. /*signature_verification_enabled=*/false);
  719. VariationsFieldTrialCreator field_trial_creator(
  720. &variations_service_client, std::move(seed_store), UIStringOverrider());
  721. metrics::TestEnabledStateProvider enabled_state_provider(/*consent=*/true,
  722. /*enabled=*/true);
  723. auto metrics_state_manager = metrics::MetricsStateManager::Create(
  724. local_state(), &enabled_state_provider, std::wstring(), base::FilePath());
  725. // Check that field trials are created from the seed. The test seed contains a
  726. // single study with an experiment targeting 100% of users in India. Since
  727. // |initial_seed| included the country code for India, this study should be
  728. // active.
  729. EXPECT_TRUE(field_trial_creator.SetUpFieldTrials(
  730. /*variation_ids=*/std::vector<std::string>(),
  731. base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
  732. switches::kForceVariationIds),
  733. std::vector<base::FeatureList::FeatureOverrideInfo>(),
  734. /*low_entropy_provider=*/nullptr, std::make_unique<base::FeatureList>(),
  735. metrics_state_manager.get(), &platform_field_trials, &safe_seed_manager,
  736. /*low_entropy_source_value=*/absl::nullopt));
  737. EXPECT_EQ(kTestSeedExperimentName,
  738. base::FieldTrialList::FindFullName(kTestSeedStudyName));
  739. }
  740. // Tests that the hardware class is set on Android.
  741. TEST_F(FieldTrialCreatorTest, ClientFilterableState_HardwareClass) {
  742. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  743. TestVariationsServiceClient variations_service_client;
  744. TestVariationsFieldTrialCreator field_trial_creator(
  745. local_state(), &variations_service_client, &safe_seed_manager);
  746. const base::Version& current_version = version_info::GetVersion();
  747. EXPECT_TRUE(current_version.IsValid());
  748. std::unique_ptr<ClientFilterableState> client_filterable_state =
  749. field_trial_creator.GetClientFilterableStateForVersion(current_version);
  750. EXPECT_NE(client_filterable_state->hardware_class, std::string());
  751. }
  752. #endif // BUILDFLAG(IS_ANDROID)
  753. #if BUILDFLAG(FIELDTRIAL_TESTING_ENABLED)
  754. // Used to create a TestVariationsFieldTrialCreator with a valid unexpired seed.
  755. std::unique_ptr<TestVariationsFieldTrialCreator>
  756. SetUpFieldTrialCreatorWithValidSeed(
  757. PrefService* local_state,
  758. TestVariationsServiceClient* variations_service_client,
  759. NiceMock<MockSafeSeedManager>* safe_seed_manager) {
  760. // Set up a valid unexpired seed.
  761. const base::Time now = base::Time::Now();
  762. const base::Time seed_fetch_time = now - base::Days(1);
  763. std::unique_ptr<TestVariationsFieldTrialCreator> field_trial_creator =
  764. std::make_unique<TestVariationsFieldTrialCreator>(
  765. local_state, variations_service_client, safe_seed_manager);
  766. field_trial_creator->SetBuildTime(now);
  767. // Simulate the seed being stored.
  768. local_state->SetTime(prefs::kVariationsLastFetchTime, seed_fetch_time);
  769. // Simulate a seed from an earlier (i.e. valid) milestone.
  770. local_state->SetInteger(prefs::kVariationsSeedMilestone, kTestSeedMilestone);
  771. return field_trial_creator;
  772. }
  773. // Verifies that a valid seed is used instead of the testing config when we
  774. // disable it.
  775. TEST_F(FieldTrialCreatorTest, NotSetUpFieldTrialConfig_ValidSeed) {
  776. #if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
  777. // Note: Non-Google Chrome branded builds do not disable the testing config by
  778. // default. We explicitly disable it.
  779. DisableTestingConfig();
  780. #endif
  781. // Create a field trial creator with a valid unexpired seed.
  782. TestVariationsServiceClient variations_service_client;
  783. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  784. std::unique_ptr<TestVariationsFieldTrialCreator> field_trial_creator =
  785. SetUpFieldTrialCreatorWithValidSeed(
  786. local_state(), &variations_service_client, &safe_seed_manager);
  787. // Verify that |SetUpFieldTrials| uses the seed. |SetUpFieldTrials| returns
  788. // true if it used a seed.
  789. EXPECT_CALL(safe_seed_manager,
  790. DoSetActiveSeedState(kTestSeedSerializedData, kTestSeedSignature,
  791. kTestSeedMilestone, _, _))
  792. .Times(1);
  793. EXPECT_TRUE(field_trial_creator->SetUpFieldTrials());
  794. EXPECT_TRUE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  795. // Verify that the |UnitTest| trial from the field trial testing config was
  796. // not registered.
  797. ASSERT_FALSE(base::FieldTrialList::TrialExists("UnitTest"));
  798. ResetVariations();
  799. }
  800. // Verifies that field trial testing config is used when enabled, even when
  801. // there is a valid unexpired seed.
  802. TEST_F(FieldTrialCreatorTest, SetUpFieldTrialConfig_ValidSeed) {
  803. EnableTestingConfig();
  804. // Create a field trial creator with a valid unexpired seed.
  805. TestVariationsServiceClient variations_service_client;
  806. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  807. std::unique_ptr<TestVariationsFieldTrialCreator> field_trial_creator =
  808. SetUpFieldTrialCreatorWithValidSeed(
  809. local_state(), &variations_service_client, &safe_seed_manager);
  810. // Verify that |SetUpFieldTrials| does not use the seed, despite it being
  811. // valid and unexpired. |SetUpFieldTrials| returns false if it did not use a
  812. // seed.
  813. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  814. EXPECT_FALSE(field_trial_creator->SetUpFieldTrials());
  815. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  816. // Verify that the |UnitTest| trial from the field trial testing config has
  817. // been registered, and that the group name is |Enabled|.
  818. ASSERT_EQ("Enabled", base::FieldTrialList::FindFullName("UnitTest"));
  819. // Verify the |UnitTest| trial params.
  820. base::FieldTrialParams params;
  821. ASSERT_TRUE(base::GetFieldTrialParams("UnitTest", &params));
  822. ASSERT_EQ(1U, params.size());
  823. EXPECT_EQ("1", params["x"]);
  824. // Verify that the |UnitTestEnabled| feature is active.
  825. const base::Feature kFeature1{"UnitTestEnabled",
  826. base::FEATURE_DISABLED_BY_DEFAULT};
  827. EXPECT_TRUE(base::FeatureList::IsEnabled(kFeature1));
  828. ResetVariations();
  829. }
  830. // Verifies that trials from the testing config and the |--force-fieldtrials|
  831. // switch are registered when they are both used (assuming there are no
  832. // conflicts).
  833. TEST_F(FieldTrialCreatorTest, SetUpFieldTrialConfig_ForceFieldTrials) {
  834. EnableTestingConfig();
  835. // Simulate passing |--force-fieldtrials="UnitTest2/Enabled"|.
  836. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  837. ::switches::kForceFieldTrials, "UnitTest2/Enabled");
  838. // Simulate passing |--force-fieldtrial-params="UnitTest2.Enabled:y/1"|.
  839. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  840. switches::kForceFieldTrialParams, "UnitTest2.Enabled:y/1");
  841. // Simulate passing |--enable-features="UnitTest2Enabled<UnitTest2"|.
  842. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  843. ::switches::kEnableFeatures, "UnitTest2Enabled<UnitTest2");
  844. // Create a field trial creator with a valid unexpired seed.
  845. TestVariationsServiceClient variations_service_client;
  846. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  847. std::unique_ptr<TestVariationsFieldTrialCreator> field_trial_creator =
  848. SetUpFieldTrialCreatorWithValidSeed(
  849. local_state(), &variations_service_client, &safe_seed_manager);
  850. // Verify that |SetUpFieldTrials| does not use the seed, despite it being
  851. // valid and unexpired. |SetUpFieldTrials| returns false if it did not use a
  852. // seed.
  853. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  854. EXPECT_FALSE(field_trial_creator->SetUpFieldTrials());
  855. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  856. // Verify that the |UnitTest| trial from the field trial testing config has
  857. // been registered, and that the group name is |Enabled|.
  858. ASSERT_EQ("Enabled", base::FieldTrialList::FindFullName("UnitTest"));
  859. // Verify that the |UnitTest2| trial from the |--force-fieldtrials| switch has
  860. // been registered, and that the group name is |Enabled|.
  861. ASSERT_EQ("Enabled", base::FieldTrialList::FindFullName("UnitTest2"));
  862. // Verify the |UnitTest| trial params.
  863. base::FieldTrialParams params;
  864. ASSERT_TRUE(base::GetFieldTrialParams("UnitTest", &params));
  865. ASSERT_EQ(1U, params.size());
  866. EXPECT_EQ("1", params["x"]);
  867. // Verify the |UnitTest2| trial params.
  868. base::FieldTrialParams params2;
  869. ASSERT_TRUE(base::GetFieldTrialParams("UnitTest2", &params2));
  870. ASSERT_EQ(1U, params2.size());
  871. EXPECT_EQ("1", params2["y"]);
  872. // Verify that the |UnitTestEnabled| and |UnitTestEnabled2| features are
  873. // active.
  874. const base::Feature kFeature1{"UnitTestEnabled",
  875. base::FEATURE_DISABLED_BY_DEFAULT};
  876. EXPECT_TRUE(base::FeatureList::IsEnabled(kFeature1));
  877. const base::Feature kFeature2{"UnitTest2Enabled",
  878. base::FEATURE_DISABLED_BY_DEFAULT};
  879. EXPECT_TRUE(base::FeatureList::IsEnabled(kFeature2));
  880. ResetVariations();
  881. }
  882. // Verifies that when field trial testing config is used, trials and groups
  883. // specified using |--force-fieldtrials| take precedence if they specify the
  884. // same trials but different groups.
  885. TEST_F(FieldTrialCreatorTest, SetUpFieldTrialConfig_ForceFieldTrialsOverride) {
  886. EnableTestingConfig();
  887. // Simulate passing |--force-fieldtrials="UnitTest/Disabled"| switch.
  888. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  889. ::switches::kForceFieldTrials, "UnitTest/Disabled");
  890. // Create a field trial creator with a valid unexpired seed.
  891. TestVariationsServiceClient variations_service_client;
  892. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  893. std::unique_ptr<TestVariationsFieldTrialCreator> field_trial_creator =
  894. SetUpFieldTrialCreatorWithValidSeed(
  895. local_state(), &variations_service_client, &safe_seed_manager);
  896. // Verify that |SetUpFieldTrials| does not use the seed, despite it being
  897. // valid and unexpired. |SetUpFieldTrials| returns false if it did not use a
  898. // seed.
  899. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  900. EXPECT_FALSE(field_trial_creator->SetUpFieldTrials());
  901. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  902. // Verify that the |UnitTest| trial from the |--force-fieldtrials| switch (and
  903. // not from the field trial testing config) has been registered, and that the
  904. // group name is |Disabled|.
  905. ASSERT_EQ("Disabled", base::FieldTrialList::FindFullName("UnitTest"));
  906. // Verify that the |UnitTest| trial params from the field trial testing config
  907. // were not used. |GetFieldTrialParams| returns false if no parameters are
  908. // defined for a specified trial.
  909. base::FieldTrialParams params;
  910. ASSERT_FALSE(base::GetFieldTrialParams("UnitTest", &params));
  911. // Verify that the |UnitTestEnabled| feature from the testing config is not
  912. // active.
  913. const base::Feature kFeature1{"UnitTestEnabled",
  914. base::FEATURE_DISABLED_BY_DEFAULT};
  915. EXPECT_FALSE(base::FeatureList::IsEnabled(kFeature1));
  916. ResetVariations();
  917. }
  918. // Verifies that when field trial testing config is used, params specified using
  919. // |--force-fieldtrial-params| take precedence if they specify the same trial
  920. // and group.
  921. TEST_F(FieldTrialCreatorTest, SetUpFieldTrialConfig_ForceFieldTrialParams) {
  922. EnableTestingConfig();
  923. // Simulate passing |--force-fieldtrial-params="UnitTest.Enabled:x/2/y/2"|
  924. // switch.
  925. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  926. switches::kForceFieldTrialParams, "UnitTest.Enabled:x/2/y/2");
  927. // Create a field trial creator with a valid unexpired seed.
  928. TestVariationsServiceClient variations_service_client;
  929. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  930. std::unique_ptr<TestVariationsFieldTrialCreator> field_trial_creator =
  931. SetUpFieldTrialCreatorWithValidSeed(
  932. local_state(), &variations_service_client, &safe_seed_manager);
  933. // Verify that |SetUpFieldTrials| does not use the seed, despite it being
  934. // valid and unexpired. |SetUpFieldTrials| returns false if it did not use a
  935. // seed.
  936. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  937. EXPECT_FALSE(field_trial_creator->SetUpFieldTrials());
  938. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  939. // Verify that the |UnitTest| trial from the field trial testing config has
  940. // been registered, and that the group name is |Enabled|.
  941. ASSERT_EQ("Enabled", base::FieldTrialList::FindFullName("UnitTest"));
  942. // Verify the |UnitTest| trial params, and that the
  943. // |--force-fieldtrial-params| took precedence over the params defined in the
  944. // field trial testing config.
  945. base::FieldTrialParams params;
  946. ASSERT_TRUE(base::GetFieldTrialParams("UnitTest", &params));
  947. ASSERT_EQ(2U, params.size());
  948. EXPECT_EQ("2", params["x"]);
  949. EXPECT_EQ("2", params["y"]);
  950. // Verify that the |UnitTestEnabled| feature is still active.
  951. const base::Feature kFeature1{"UnitTestEnabled",
  952. base::FEATURE_DISABLED_BY_DEFAULT};
  953. EXPECT_TRUE(base::FeatureList::IsEnabled(kFeature1));
  954. ResetVariations();
  955. }
  956. class FieldTrialCreatorTestWithFeatures
  957. : public FieldTrialCreatorTest,
  958. public ::testing::WithParamInterface<const char*> {};
  959. INSTANTIATE_TEST_SUITE_P(All,
  960. FieldTrialCreatorTestWithFeatures,
  961. ::testing::Values(::switches::kEnableFeatures,
  962. ::switches::kDisableFeatures));
  963. // Verifies that studies from field trial testing config should be ignored
  964. // if they enable/disable features overridden by |--enable-features| or
  965. // |--disable-features|.
  966. TEST_P(FieldTrialCreatorTestWithFeatures,
  967. SetUpFieldTrialConfig_OverrideFeatures) {
  968. EnableTestingConfig();
  969. // Simulate passing either |--enable-features="UnitTestEnabled"| or
  970. // |--disable-features="UnitTestEnabled"| switch.
  971. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(GetParam(),
  972. "UnitTestEnabled");
  973. // Create a field trial creator with a valid unexpired seed.
  974. TestVariationsServiceClient variations_service_client;
  975. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  976. std::unique_ptr<TestVariationsFieldTrialCreator> field_trial_creator =
  977. SetUpFieldTrialCreatorWithValidSeed(
  978. local_state(), &variations_service_client, &safe_seed_manager);
  979. // Verify that |SetUpFieldTrials| does not use the seed, despite it being
  980. // valid and unexpired. |SetUpFieldTrials| returns false if it did not use a
  981. // seed.
  982. EXPECT_CALL(safe_seed_manager, DoSetActiveSeedState(_, _, _, _, _)).Times(0);
  983. EXPECT_FALSE(field_trial_creator->SetUpFieldTrials());
  984. EXPECT_FALSE(base::FieldTrialList::TrialExists(kTestSeedStudyName));
  985. // Verify that the |UnitTest| trial from the field trial testing config was
  986. // NOT registered. Even if the study |UnitTest| enables feature
  987. // |UnitTestEnabled|, and we pass |--enable-features="UnitTestEnabled"|, the
  988. // study should be disabled.
  989. EXPECT_FALSE(base::FieldTrialList::TrialExists("UnitTest"));
  990. // Verify that the |UnitTestEnabled| feature is enabled or disabled depending
  991. // on whether we passed it in |--enable-features| or |--disable-features|.
  992. const base::Feature kFeature1{"UnitTestEnabled",
  993. base::FEATURE_DISABLED_BY_DEFAULT};
  994. EXPECT_EQ(GetParam() == ::switches::kEnableFeatures,
  995. base::FeatureList::IsEnabled(kFeature1));
  996. ResetVariations();
  997. }
  998. #endif // BUILDFLAG(FIELDTRIAL_TESTING_ENABLED)
  999. // Verify that a beacon file is not written when passing an empty user data
  1000. // directory path. Some platforms deliberately pass an empty path.
  1001. TEST_F(FieldTrialCreatorTest, DoNotWriteBeaconFile) {
  1002. DisableTestingConfig();
  1003. TestVariationsServiceClient variations_service_client;
  1004. // Ensure that Variations Safe Mode is not triggered.
  1005. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  1006. // Pass an empty path instead of a path to the user data dir.
  1007. TestVariationsFieldTrialCreator field_trial_creator(
  1008. local_state(), &variations_service_client, &safe_seed_manager,
  1009. base::FilePath());
  1010. base::HistogramTester histogram_tester;
  1011. ASSERT_TRUE(field_trial_creator.SetUpFieldTrials());
  1012. EXPECT_FALSE(base::PathExists(
  1013. user_data_dir_path().Append(metrics::kCleanExitBeaconFilename)));
  1014. histogram_tester.ExpectTotalCount(
  1015. "Variations.ExtendedSafeMode.BeaconFileWrite", 0);
  1016. }
  1017. INSTANTIATE_TEST_SUITE_P(
  1018. All,
  1019. FieldTrialCreatorTestWithStartupVisibility,
  1020. ::testing::Values(
  1021. StartupVisibilityTestParams{
  1022. .test_name = "UnknownVisibility",
  1023. .startup_visibility = metrics::StartupVisibility::kUnknown,
  1024. .extend_safe_mode = true},
  1025. StartupVisibilityTestParams{
  1026. .test_name = "BackgroundVisibility",
  1027. .startup_visibility = metrics::StartupVisibility::kBackground,
  1028. .extend_safe_mode = false},
  1029. StartupVisibilityTestParams{
  1030. .test_name = "ForegroundVisibility",
  1031. .startup_visibility = metrics::StartupVisibility::kForeground,
  1032. .extend_safe_mode = true}),
  1033. [](const ::testing::TestParamInfo<StartupVisibilityTestParams>& params) {
  1034. return params.param.test_name;
  1035. });
  1036. // Verify that Chrome starts watching for crashes for unknown and foreground
  1037. // startup visibilities. Verify that Chrome does not start watching for crashes
  1038. // in background sessions.
  1039. TEST_P(FieldTrialCreatorTestWithStartupVisibility,
  1040. StartupVisibilityAffectsBrowserCrashMonitoring) {
  1041. DisableTestingConfig();
  1042. TestVariationsServiceClient variations_service_client;
  1043. // Ensure that Variations Safe Mode is not triggered.
  1044. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  1045. StartupVisibilityTestParams params = GetParam();
  1046. TestVariationsFieldTrialCreator field_trial_creator(
  1047. local_state(), &variations_service_client, &safe_seed_manager,
  1048. user_data_dir_path(), params.startup_visibility);
  1049. ASSERT_TRUE(field_trial_creator.SetUpFieldTrials());
  1050. // Verify that Chrome did (or did not) start watching for crashes.
  1051. EXPECT_EQ(base::PathExists(
  1052. user_data_dir_path().Append(metrics::kCleanExitBeaconFilename)),
  1053. params.extend_safe_mode);
  1054. }
  1055. // Verify that the beacon file contents are as expected when Chrome starts
  1056. // watching for browser crashes before setting up field trials.
  1057. TEST_F(FieldTrialCreatorTest, WriteBeaconFile) {
  1058. DisableTestingConfig();
  1059. TestVariationsServiceClient variations_service_client;
  1060. // Ensure that Variations Safe Mode is not triggered.
  1061. NiceMock<MockSafeSeedManager> safe_seed_manager(local_state());
  1062. TestVariationsFieldTrialCreator field_trial_creator(
  1063. local_state(), &variations_service_client, &safe_seed_manager,
  1064. user_data_dir_path());
  1065. base::HistogramTester histogram_tester;
  1066. ASSERT_TRUE(field_trial_creator.SetUpFieldTrials());
  1067. // Verify that the beacon file was written and that the contents are correct.
  1068. const base::FilePath variations_file_path =
  1069. user_data_dir_path().Append(metrics::kCleanExitBeaconFilename);
  1070. EXPECT_TRUE(base::PathExists(variations_file_path));
  1071. std::string beacon_file_contents;
  1072. ASSERT_TRUE(
  1073. base::ReadFileToString(variations_file_path, &beacon_file_contents));
  1074. EXPECT_EQ(beacon_file_contents,
  1075. "{\"user_experience_metrics.stability.exited_cleanly\":false,"
  1076. "\"variations_crash_streak\":0}");
  1077. // Verify metrics.
  1078. histogram_tester.ExpectUniqueSample(
  1079. "Variations.ExtendedSafeMode.BeaconFileWrite", 1, 1);
  1080. }
  1081. } // namespace variations