Fix crash with forcing variations when the trial exists.

This can happen, for example, when a user used the
--force-fieldtrials= command-line to set a different
group than what they have selected from chrome://flags.

Includes a test.

BUG=409976

Review URL: https://codereview.chromium.org/529183002

Cr-Commit-Position: refs/heads/master@{#293173}
diff --git a/components/variations/variations_seed_processor_unittest.cc b/components/variations/variations_seed_processor_unittest.cc
index 07eed692..fe58842 100644
--- a/components/variations/variations_seed_processor_unittest.cc
+++ b/components/variations/variations_seed_processor_unittest.cc
@@ -463,4 +463,28 @@
             base::FieldTrialList::FindFullName(kFlagStudyName));
 }
 
+TEST_F(VariationsSeedProcessorTest, ForcingFlagAlreadyForced) {
+  Study study = CreateStudyWithFlagGroups(100, 0, 0);
+  ASSERT_EQ(kNonFlagGroupName, study.experiment(0).name());
+  Study_Experiment_Param* param = study.mutable_experiment(0)->add_param();
+  param->set_name("x");
+  param->set_value("y");
+  study.mutable_experiment(0)->set_google_web_experiment_id(kExperimentId);
+
+  base::FieldTrialList field_trial_list(NULL);
+  base::FieldTrialList::CreateFieldTrial(kFlagStudyName, kNonFlagGroupName);
+
+  CommandLine::ForCurrentProcess()->AppendSwitch(kForcingFlag1);
+  EXPECT_TRUE(CreateTrialFromStudy(&study));
+  // The previously forced experiment should still hold.
+  EXPECT_EQ(kNonFlagGroupName,
+            base::FieldTrialList::FindFullName(study.name()));
+
+  // Check that params and experiment ids correspond.
+  EXPECT_EQ("y", GetVariationParamValue(study.name(), "x"));
+  VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, kFlagStudyName,
+                                        kNonFlagGroupName);
+  EXPECT_EQ(kExperimentId, id);
+}
+
 }  // namespace variations