Fixes bug in about:flags where we weren't pruning experiments that
are not enabled for the current platform but were at one point.

BUG=60206
TEST=none

Review URL: http://codereview.chromium.org/3989002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63952 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/about_flags_unittest.cc b/chrome/browser/about_flags_unittest.cc
index 3122cc9..6d86cc9 100644
--- a/chrome/browser/about_flags_unittest.cc
+++ b/chrome/browser/about_flags_unittest.cc
@@ -4,20 +4,50 @@
 
 #include "chrome/browser/about_flags.h"
 
+#include "base/values.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/pref_names.h"
 #include "chrome/test/testing_pref_service.h"
+#include "grit/chromium_strings.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
-// These two should be two arbitrary, but valid names from about_flags.cc.
-const char kFlags1[] = "remoting";
-const char kFlags2[] = "print-preview";
+const char kFlags1[] = "flag1";
+const char kFlags2[] = "flag2";
+const char kFlags3[] = "flag3";
 
-// The command line flag corresponding to |kFlags1|.
-const char* const kFlag1CommandLine = switches::kEnableRemoting;
+const char kSwitch1[] = "switch";
+const char kSwitch2[] = "switch2";
+const char kSwitch3[] = "switch3";
 
 namespace about_flags {
 
+// The experiments that are set for these tests. The first two experiments are
+// supported on the current platform, but the last is only supported on a
+// platform other than the current.
+static Experiment kExperiments[] = {
+  {
+    kFlags1,
+    IDS_PRODUCT_NAME,
+    IDS_PRODUCT_NAME,
+    0,  // Ends up being mapped to the current platform.
+    kSwitch1
+  },
+  {
+    kFlags2,
+    IDS_PRODUCT_NAME,
+    IDS_PRODUCT_NAME,
+    0,  // Ends up being mapped to the current platform.
+    kSwitch2
+  },
+  {
+    kFlags3,
+    IDS_PRODUCT_NAME,
+    IDS_PRODUCT_NAME,
+    0,  // This ends up enabling for an OS other than the current.
+    kSwitch3
+  },
+};
+
 class AboutFlagsTest : public ::testing::Test {
  protected:
   AboutFlagsTest() {
@@ -25,6 +55,22 @@
     testing::ClearState();
   }
 
+  virtual void SetUp() {
+    for (size_t i = 0; i < arraysize(kExperiments); ++i)
+      kExperiments[i].supported_platforms = GetCurrentPlatform();
+
+    int os_other_than_current = 1;
+    while (os_other_than_current == GetCurrentPlatform())
+      os_other_than_current <<= 1;
+    kExperiments[2].supported_platforms = os_other_than_current;
+
+    testing::SetExperiments(kExperiments, arraysize(kExperiments));
+  }
+
+  virtual void TearDown() {
+    testing::SetExperiments(NULL, 0);
+  }
+
   TestingPrefService prefs_;
 };
 
@@ -85,17 +131,17 @@
   command_line.AppendSwitch("foo");
 
   EXPECT_TRUE(command_line.HasSwitch("foo"));
-  EXPECT_FALSE(command_line.HasSwitch(kFlag1CommandLine));
+  EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
 
   ConvertFlagsToSwitches(&prefs_, &command_line);
 
   EXPECT_TRUE(command_line.HasSwitch("foo"));
-  EXPECT_TRUE(command_line.HasSwitch(kFlag1CommandLine));
+  EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
 }
 
 TEST_F(AboutFlagsTest, RemoveFlagSwitches) {
   std::map<std::string, CommandLine::StringType> switch_list;
-  switch_list[kFlag1CommandLine] = CommandLine::StringType();
+  switch_list[kSwitch1] = CommandLine::StringType();
   switch_list[switches::kFlagSwitchesBegin] = CommandLine::StringType();
   switch_list[switches::kFlagSwitchesEnd] = CommandLine::StringType();
   switch_list["foo"] = CommandLine::StringType();
@@ -105,7 +151,7 @@
   // This shouldn't do anything before ConvertFlagsToSwitches() wasn't called.
   RemoveFlagsSwitches(&switch_list);
   ASSERT_EQ(4u, switch_list.size());
-  EXPECT_TRUE(switch_list.find(kFlag1CommandLine) != switch_list.end());
+  EXPECT_TRUE(switch_list.find(kSwitch1) != switch_list.end());
   EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesBegin) !=
               switch_list.end());
   EXPECT_TRUE(switch_list.find(switches::kFlagSwitchesEnd) !=
@@ -123,4 +169,25 @@
   EXPECT_TRUE(switch_list.find("foo") != switch_list.end());
 }
 
+// Tests enabling experiments that aren't supported on the current platform.
+TEST_F(AboutFlagsTest, PersistAndPrune) {
+  // Enable exerpiement 1 and experiment 3.
+  SetExperimentEnabled(&prefs_, kFlags1, true);
+  SetExperimentEnabled(&prefs_, kFlags3, true);
+  CommandLine command_line(CommandLine::NO_PROGRAM);
+  EXPECT_FALSE(command_line.HasSwitch(kSwitch1));
+  EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
+
+  // Convert the flags to switches. Experiment 3 shouldn't be among the switches
+  // as it is not applicable to the current platform.
+  ConvertFlagsToSwitches(&prefs_, &command_line);
+  EXPECT_TRUE(command_line.HasSwitch(kSwitch1));
+  EXPECT_FALSE(command_line.HasSwitch(kSwitch3));
+
+  // Experiment 3 should show still be persisted in preferences though.
+  scoped_ptr<ListValue> switch_prefs(GetFlagsExperimentsData(&prefs_));
+  ASSERT_TRUE(switch_prefs.get());
+  EXPECT_EQ(2u, switch_prefs->GetSize());
+}
+
 }  // namespace about_flags