Verify field trials looked up through field_trial::FindFullName

For now, the run-time check will only be enabled if the
rtc_strict_field_trials GN arg is set.

In order to allow testing with imaginary field trial keys, two test
helpers have been added. It's a bit awkward to test these since the
field trial string is already global, hence the helpers are also
modifying global state. Tests must make sure this global state is reset
between runs. Things won't be an issue anymore when [1] has removed the
global string.

[1] https://crbug.com/webrtc/10335

Bug: webrtc:14154
Change-Id: Ida44cc817079d7177325e2228cf1f1d242b799e2
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/276269
Commit-Queue: Emil Lundmark <lndmrk@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38447}
diff --git a/api/field_trials_unittest.cc b/api/field_trials_unittest.cc
index dc82898..5f0188e 100644
--- a/api/field_trials_unittest.cc
+++ b/api/field_trials_unittest.cc
@@ -11,8 +11,11 @@
 #include "api/field_trials.h"
 
 #include <memory>
+#include <utility>
 
+#include "absl/strings/string_view.h"
 #include "api/transport/field_trial_based_config.h"
+#include "rtc_base/containers/flat_set.h"
 #include "system_wrappers/include/field_trial.h"
 #include "test/gmock.h"
 #include "test/gtest.h"
@@ -26,22 +29,16 @@
 
 using ::testing::NotNull;
 using ::webrtc::field_trial::InitFieldTrialsFromString;
+using ::webrtc::field_trial::ScopedGlobalFieldTrialsForTesting;
 
-class FieldTrialsTest : public testing::Test {
- protected:
-  FieldTrialsTest() {
-    // Make sure global state is consistent between test runs.
-    InitFieldTrialsFromString(nullptr);
-  }
-};
-
-TEST_F(FieldTrialsTest, EmptyStringHasNoEffect) {
+TEST(FieldTrialsTest, EmptyStringHasNoEffect) {
+  ScopedGlobalFieldTrialsForTesting g({"MyCoolTrial"});
   FieldTrials f("");
   EXPECT_FALSE(f.IsEnabled("MyCoolTrial"));
   EXPECT_FALSE(f.IsDisabled("MyCoolTrial"));
 }
 
-TEST_F(FieldTrialsTest, EnabledDisabledMustBeFirstInValue) {
+TEST(FieldTrialsTest, EnabledDisabledMustBeFirstInValue) {
   FieldTrials f(
       "MyCoolTrial/EnabledFoo/"
       "MyUncoolTrial/DisabledBar/"
@@ -51,7 +48,8 @@
   EXPECT_FALSE(f.IsEnabled("AnotherTrial"));
 }
 
-TEST_F(FieldTrialsTest, FieldTrialsDoesNotReadGlobalString) {
+TEST(FieldTrialsTest, FieldTrialsDoesNotReadGlobalString) {
+  ScopedGlobalFieldTrialsForTesting g({"MyCoolTrial", "MyUncoolTrial"});
   static constexpr char s[] = "MyCoolTrial/Enabled/MyUncoolTrial/Disabled/";
   InitFieldTrialsFromString(s);
   FieldTrials f("");
@@ -59,13 +57,14 @@
   EXPECT_FALSE(f.IsDisabled("MyUncoolTrial"));
 }
 
-TEST_F(FieldTrialsTest, FieldTrialsWritesGlobalString) {
+TEST(FieldTrialsTest, FieldTrialsWritesGlobalString) {
+  ScopedGlobalFieldTrialsForTesting g({"MyCoolTrial", "MyUncoolTrial"});
   FieldTrials f("MyCoolTrial/Enabled/MyUncoolTrial/Disabled/");
   EXPECT_TRUE(webrtc::field_trial::IsEnabled("MyCoolTrial"));
   EXPECT_TRUE(webrtc::field_trial::IsDisabled("MyUncoolTrial"));
 }
 
-TEST_F(FieldTrialsTest, FieldTrialsRestoresGlobalStringAfterDestruction) {
+TEST(FieldTrialsTest, FieldTrialsRestoresGlobalStringAfterDestruction) {
   static constexpr char s[] = "SomeString/Enabled/";
   InitFieldTrialsFromString(s);
   {
@@ -77,19 +76,20 @@
 }
 
 #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
-TEST_F(FieldTrialsTest, FieldTrialsDoesNotSupportSimultaneousInstances) {
+TEST(FieldTrialsTest, FieldTrialsDoesNotSupportSimultaneousInstances) {
   FieldTrials f("SomeString/Enabled/");
   RTC_EXPECT_DEATH(FieldTrials("SomeOtherString/Enabled/").Lookup("Whatever"),
                    "Only one instance");
 }
 #endif  // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
 
-TEST_F(FieldTrialsTest, FieldTrialsSupportsSeparateInstances) {
+TEST(FieldTrialsTest, FieldTrialsSupportsSeparateInstances) {
   { FieldTrials f("SomeString/Enabled/"); }
   { FieldTrials f("SomeOtherString/Enabled/"); }
 }
 
-TEST_F(FieldTrialsTest, NonGlobalFieldTrialsInstanceDoesNotModifyGlobalString) {
+TEST(FieldTrialsTest, NonGlobalFieldTrialsInstanceDoesNotModifyGlobalString) {
+  ScopedGlobalFieldTrialsForTesting g({"SomeString"});
   std::unique_ptr<FieldTrials> f =
       FieldTrials::CreateNoGlobal("SomeString/Enabled/");
   ASSERT_THAT(f, NotNull());
@@ -97,7 +97,7 @@
   EXPECT_FALSE(webrtc::field_trial::IsEnabled("SomeString"));
 }
 
-TEST_F(FieldTrialsTest, NonGlobalFieldTrialsSupportSimultaneousInstances) {
+TEST(FieldTrialsTest, NonGlobalFieldTrialsSupportSimultaneousInstances) {
   std::unique_ptr<FieldTrials> f1 =
       FieldTrials::CreateNoGlobal("SomeString/Enabled/");
   std::unique_ptr<FieldTrials> f2 =
@@ -112,7 +112,8 @@
   EXPECT_TRUE(f2->IsEnabled("SomeOtherString"));
 }
 
-TEST_F(FieldTrialsTest, GlobalAndNonGlobalFieldTrialsAreDisjoint) {
+TEST(FieldTrialsTest, GlobalAndNonGlobalFieldTrialsAreDisjoint) {
+  ScopedGlobalFieldTrialsForTesting g({"SomeString", "SomeOtherString"});
   FieldTrials f1("SomeString/Enabled/");
   std::unique_ptr<FieldTrials> f2 =
       FieldTrials::CreateNoGlobal("SomeOtherString/Enabled/");
@@ -125,7 +126,8 @@
   EXPECT_TRUE(f2->IsEnabled("SomeOtherString"));
 }
 
-TEST_F(FieldTrialsTest, FieldTrialBasedConfigReadsGlobalString) {
+TEST(FieldTrialsTest, FieldTrialBasedConfigReadsGlobalString) {
+  ScopedGlobalFieldTrialsForTesting g({"MyCoolTrial", "MyUncoolTrial"});
   static constexpr char s[] = "MyCoolTrial/Enabled/MyUncoolTrial/Disabled/";
   InitFieldTrialsFromString(s);
   FieldTrialBasedConfig f;