chasej | dfec22ae | 2017-03-16 20:58:55 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 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 "base/command_line.h" |
| 6 | #include "build/build_config.h" |
| 7 | #include "chrome/browser/browser_process.h" |
| 8 | #include "chrome/common/chrome_switches.h" |
| 9 | #include "chrome/common/pref_names.h" |
| 10 | #include "chrome/test/base/in_process_browser_test.h" |
| 11 | #include "chrome/test/base/testing_browser_process.h" |
| 12 | #include "components/prefs/scoped_user_pref_update.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | struct DisabledItemsTestData { |
| 18 | const std::vector<std::string> input_list; |
| 19 | const std::string expected_switch; |
| 20 | }; |
| 21 | |
| 22 | static const char kNewPublicKey[] = "new public key"; |
| 23 | |
| 24 | const DisabledItemsTestData kDisabledFeaturesTests[] = { |
| 25 | // One feature |
| 26 | {{"A"}, "A"}, |
| 27 | // Two features |
| 28 | {{"A", "B"}, "A|B"}, |
| 29 | // Three features |
| 30 | {{"A", "B", "C"}, "A|B|C"}, |
| 31 | // Spaces in feature name |
| 32 | {{"A", "B C"}, "A|B C"}, |
| 33 | }; |
| 34 | |
| 35 | const DisabledItemsTestData kDisabledTokensTests[] = { |
| 36 | // One token |
| 37 | {{"t1"}, "t1"}, |
| 38 | // Two tokens |
| 39 | {{"t1", "t2"}, "t1|t2"}, |
| 40 | // Three tokens |
| 41 | {{"t1", "t2", "t3"}, "t1|t2|t3"}, |
| 42 | }; |
| 43 | |
| 44 | class ChromeOriginTrialsTest : public InProcessBrowserTest { |
| 45 | protected: |
| 46 | ChromeOriginTrialsTest() {} |
| 47 | |
| 48 | std::string GetCommandLineSwitch(const base::StringPiece& switch_name) { |
| 49 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 50 | EXPECT_TRUE(command_line->HasSwitch(switch_name)); |
| 51 | return command_line->GetSwitchValueASCII(switch_name); |
| 52 | } |
| 53 | |
| 54 | void AddDisabledFeaturesToPrefs(const std::vector<std::string>& features) { |
| 55 | base::ListValue disabled_feature_list; |
| 56 | disabled_feature_list.AppendStrings(features); |
| 57 | ListPrefUpdate update(local_state(), prefs::kOriginTrialDisabledFeatures); |
| 58 | update->Swap(&disabled_feature_list); |
| 59 | } |
| 60 | |
| 61 | void AddDisabledTokensToPrefs(const std::vector<std::string>& tokens) { |
| 62 | base::ListValue disabled_token_list; |
| 63 | disabled_token_list.AppendStrings(tokens); |
| 64 | ListPrefUpdate update(local_state(), prefs::kOriginTrialDisabledTokens); |
| 65 | update->Swap(&disabled_token_list); |
| 66 | } |
| 67 | |
| 68 | PrefService* local_state() { return g_browser_process->local_state(); } |
| 69 | |
| 70 | private: |
| 71 | DISALLOW_COPY_AND_ASSIGN(ChromeOriginTrialsTest); |
| 72 | }; |
| 73 | |
| 74 | // Tests to verify that the command line is not set, when no prefs exist for |
| 75 | // the various updates. |
| 76 | |
| 77 | IN_PROC_BROWSER_TEST_F(ChromeOriginTrialsTest, NoPublicKeySet) { |
| 78 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 79 | EXPECT_FALSE(command_line->HasSwitch(switches::kOriginTrialPublicKey)); |
| 80 | } |
| 81 | |
| 82 | IN_PROC_BROWSER_TEST_F(ChromeOriginTrialsTest, NoDisabledFeatures) { |
| 83 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 84 | EXPECT_FALSE(command_line->HasSwitch(switches::kOriginTrialDisabledFeatures)); |
| 85 | } |
| 86 | |
| 87 | IN_PROC_BROWSER_TEST_F(ChromeOriginTrialsTest, NoDisabledTokens) { |
| 88 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 89 | EXPECT_FALSE(command_line->HasSwitch(switches::kOriginTrialDisabledTokens)); |
| 90 | } |
| 91 | |
| 92 | // Tests to verify that the public key is correctly read from prefs and |
| 93 | // added to the command line |
| 94 | IN_PROC_BROWSER_TEST_F(ChromeOriginTrialsTest, PRE_PublicKeySetOnCommandLine) { |
| 95 | local_state()->Set(prefs::kOriginTrialPublicKey, base::Value(kNewPublicKey)); |
| 96 | ASSERT_EQ(kNewPublicKey, |
| 97 | local_state()->GetString(prefs::kOriginTrialPublicKey)); |
| 98 | } |
| 99 | |
| 100 | IN_PROC_BROWSER_TEST_F(ChromeOriginTrialsTest, PublicKeySetOnCommandLine) { |
| 101 | ASSERT_EQ(kNewPublicKey, |
| 102 | local_state()->GetString(prefs::kOriginTrialPublicKey)); |
| 103 | std::string actual = GetCommandLineSwitch(switches::kOriginTrialPublicKey); |
| 104 | EXPECT_EQ(kNewPublicKey, actual); |
| 105 | } |
| 106 | |
| 107 | // Tests to verify that disabled features are correctly read from prefs and |
| 108 | // added to the command line |
| 109 | class ChromeOriginTrialsDisabledFeaturesTest |
| 110 | : public ChromeOriginTrialsTest, |
| 111 | public ::testing::WithParamInterface<DisabledItemsTestData> {}; |
| 112 | |
| 113 | IN_PROC_BROWSER_TEST_P(ChromeOriginTrialsDisabledFeaturesTest, |
| 114 | PRE_DisabledFeaturesSetOnCommandLine) { |
| 115 | AddDisabledFeaturesToPrefs(GetParam().input_list); |
| 116 | ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledFeatures)); |
| 117 | } |
| 118 | |
| 119 | IN_PROC_BROWSER_TEST_P(ChromeOriginTrialsDisabledFeaturesTest, |
| 120 | DisabledFeaturesSetOnCommandLine) { |
| 121 | ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledFeatures)); |
| 122 | std::string actual = |
| 123 | GetCommandLineSwitch(switches::kOriginTrialDisabledFeatures); |
| 124 | EXPECT_EQ(GetParam().expected_switch, actual); |
| 125 | } |
| 126 | |
| 127 | INSTANTIATE_TEST_CASE_P(, |
| 128 | ChromeOriginTrialsDisabledFeaturesTest, |
| 129 | ::testing::ValuesIn(kDisabledFeaturesTests)); |
| 130 | |
| 131 | // Tests to verify that disabled tokens are correctly read from prefs and |
| 132 | // added to the command line |
| 133 | class ChromeOriginTrialsDisabledTokensTest |
| 134 | : public ChromeOriginTrialsTest, |
| 135 | public ::testing::WithParamInterface<DisabledItemsTestData> {}; |
| 136 | |
| 137 | IN_PROC_BROWSER_TEST_P(ChromeOriginTrialsDisabledTokensTest, |
| 138 | PRE_DisabledTokensSetOnCommandLine) { |
| 139 | AddDisabledTokensToPrefs(GetParam().input_list); |
| 140 | ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 141 | } |
| 142 | |
| 143 | IN_PROC_BROWSER_TEST_P(ChromeOriginTrialsDisabledTokensTest, |
| 144 | DisabledTokensSetOnCommandLine) { |
| 145 | ASSERT_TRUE(local_state()->HasPrefPath(prefs::kOriginTrialDisabledTokens)); |
| 146 | std::string actual = |
| 147 | GetCommandLineSwitch(switches::kOriginTrialDisabledTokens); |
| 148 | EXPECT_EQ(GetParam().expected_switch, actual); |
| 149 | } |
| 150 | |
| 151 | INSTANTIATE_TEST_CASE_P(, |
| 152 | ChromeOriginTrialsDisabledTokensTest, |
| 153 | ::testing::ValuesIn(kDisabledTokensTests)); |
| 154 | |
| 155 | } // namespace |