Add scoped class for overriding field trials.

To be used in tests that depend on specific field-trial settings without
overwriting the command-line flag for overriding field trials.

BUG=webrtc:4820
R=stefan@webrtc.org

Review URL: https://codereview.webrtc.org/1227653002

Cr-Commit-Position: refs/heads/master@{#9547}
diff --git a/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc b/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc
index 320cb43..1bb28c8 100644
--- a/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc
+++ b/webrtc/modules/remote_bitrate_estimator/overuse_detector_unittest.cc
@@ -603,14 +603,17 @@
 }
 
 class OveruseDetectorExperimentTest : public OveruseDetectorTest {
+ public:
+  OveruseDetectorExperimentTest()
+      : override_field_trials_(
+            "WebRTC-AdaptiveBweThreshold/Enabled-0.01,0.00018/") {}
+
  protected:
   void SetUp() override {
-    test::InitFieldTrialsFromString(
-        "WebRTC-AdaptiveBweThreshold/Enabled-0.01,0.00018/");
     overuse_detector_.reset(new OveruseDetector(options_));
   }
 
-  void TearDown() override { test::InitFieldTrialsFromString(""); }
+  test::ScopedFieldTrials override_field_trials_;
 };
 
 TEST_F(OveruseDetectorExperimentTest, ThresholdAdapts) {
diff --git a/webrtc/test/field_trial.cc b/webrtc/test/field_trial.cc
index 1f56ad3..92aa6b0 100644
--- a/webrtc/test/field_trial.cc
+++ b/webrtc/test/field_trial.cc
@@ -45,12 +45,12 @@
 void InitFieldTrialsFromString(const std::string& trials_string) {
   static const char kPersistentStringSeparator = '/';
 
+  // Catch an error if this is called more than once.
+  assert(field_trials_initiated_ == false);
   field_trials_initiated_ = true;
 
-  if (trials_string.empty()) {
-    field_trials_.clear();
+  if (trials_string == "")
     return;
-  }
 
   size_t next_item = 0;
   while (next_item < trials_string.length()) {
@@ -83,5 +83,18 @@
   // Using abort so it crashs both in debug and release mode.
   abort();
 }
+
+ScopedFieldTrials::ScopedFieldTrials(const std::string& config)
+    : previous_field_trials_(field_trials_) {
+  assert(field_trials_initiated_);
+  field_trials_initiated_ = false;
+  field_trials_ = std::map<std::string, std::string>();
+  InitFieldTrialsFromString(config);
+}
+
+ScopedFieldTrials::~ScopedFieldTrials() {
+  field_trials_ = previous_field_trials_;
+}
+
 }  // namespace test
 }  // namespace webrtc
diff --git a/webrtc/test/field_trial.h b/webrtc/test/field_trial.h
index 6503254..d448f34 100644
--- a/webrtc/test/field_trial.h
+++ b/webrtc/test/field_trial.h
@@ -12,6 +12,7 @@
 #define WEBRTC_TEST_FIELD_TRIAL_H_
 
 #include <string>
+#include <map>
 
 namespace webrtc {
 namespace test {
@@ -31,6 +32,16 @@
 // passed to it. That can be used to find out if a binary is parsing the flags.
 void InitFieldTrialsFromString(const std::string& config);
 
+// This class is used to override field-trial configs within specific tests.
+// After this class goes out of scope previous field trials will be restored.
+class ScopedFieldTrials {
+ public:
+  explicit ScopedFieldTrials(const std::string& config);
+  ~ScopedFieldTrials();
+ private:
+  const std::map<std::string, std::string> previous_field_trials_;
+};
+
 }  // namespace test
 }  // namespace webrtc