[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / chrome_browser_field_trials_unittest.cc
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chrome_browser_field_trials.h"
6
7 #include <memory>
8
9 #include "base/feature_list.h"
10 #include "components/prefs/testing_pref_service.h"
11 #include "components/ukm/ukm_recorder_impl.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
15 TEST(ChromeBrowserFieldTrialsTest, SamplingTrials) {
16   TestingPrefServiceSimple local_state;
17   ChromeBrowserFieldTrials chrome_browser_field_trials(&local_state);
18
19   const char kSamplingTrialName[] = "MetricsAndCrashSampling";
20 #if BUILDFLAG(IS_ANDROID)
21   const char kPostFREFixSamplingTrialName[] =
22       "PostFREFixMetricsAndCrashSampling";
23 #endif  // BUILDFLAG(IS_ANDROID)
24   const char kUkmSamplingTrialName[] = "UkmSamplingRate";
25
26   // Verify that initially, sampling trials do not exist.
27   EXPECT_FALSE(base::FieldTrialList::TrialExists(kSamplingTrialName));
28 #if BUILDFLAG(IS_ANDROID)
29   EXPECT_FALSE(base::FieldTrialList::TrialExists(kPostFREFixSamplingTrialName));
30 #endif  // BUILDFLAG(IS_ANDROID)
31   EXPECT_FALSE(base::FieldTrialList::TrialExists(kUkmSamplingTrialName));
32
33   // Call SetUpClientSideFieldTrials(), which should create fallback
34   // sampling trials since they do not exist yet.
35   variations::EntropyProviders entropy_providers("client_id", {0, 8000});
36   auto feature_list = std::make_unique<base::FeatureList>();
37   chrome_browser_field_trials.SetUpClientSideFieldTrials(
38       /*has_seed=*/false, entropy_providers, feature_list.get());
39
40   // Verify that the sampling trials were created.
41   EXPECT_TRUE(base::FieldTrialList::TrialExists(kSamplingTrialName));
42 #if BUILDFLAG(IS_ANDROID)
43   EXPECT_TRUE(base::FieldTrialList::TrialExists(kPostFREFixSamplingTrialName));
44 #endif  // BUILDFLAG(IS_ANDROID)
45   EXPECT_TRUE(base::FieldTrialList::TrialExists(kUkmSamplingTrialName));
46
47   // Call SetUpClientSideFieldTrials() again. This should be a no-op,
48   // since the sampling trials already exist. If the trials are created again,
49   // a CHECK will be triggered and this will crash.
50   chrome_browser_field_trials.SetUpClientSideFieldTrials(
51       /*has_seed=*/false, entropy_providers, feature_list.get());
52 }
53 #endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)