OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/previews/core/previews_experiments.h" | 5 #include "components/previews/core/previews_experiments.h" |
6 | 6 |
| 7 #include <string> |
| 8 |
7 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "components/variations/variations_associated_data.h" |
| 13 |
8 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
9 | 15 |
| 16 namespace previews { |
| 17 |
10 namespace { | 18 namespace { |
11 | 19 |
12 using PreviewsExperimentsTest = testing::Test; | 20 using PreviewsExperimentsTest = testing::Test; |
13 | 21 |
| 22 // Used as field trial values. Somewhat random yet valid values. |
| 23 const char kMaxStoredHistoryLengthPerHost[] = "3"; |
| 24 const char kMaxStoredHistoryLengthHostIndifferent[] = "4"; |
| 25 const char kMaxHostsInBlackList[] = "13"; |
| 26 const char kPerHostOptOutThreshold[] = "12"; |
| 27 const char kHostIndifferentOptOutThreshold[] = "84"; |
| 28 const char kPerHostBlackListDurationInDays[] = "99"; |
| 29 const char kHostIndifferentBlackListDurationInDays[] = "64"; |
| 30 const char kSingleOptOutDurationInSeconds[] = "28"; |
| 31 const char kOfflinePreviewFreshnessDurationInDays[] = "12"; |
| 32 const char kEffectiveConnectionTypeThreshold[] = "4G"; |
| 33 |
| 34 // Verifies that the default params are the expected values. |
| 35 void VerifyDefaultParams() { |
| 36 EXPECT_EQ(4u, params::MaxStoredHistoryLengthForPerHostBlackList()); |
| 37 EXPECT_EQ(10u, params::MaxStoredHistoryLengthForHostIndifferentBlackList()); |
| 38 EXPECT_EQ(100u, params::MaxInMemoryHostsInBlackList()); |
| 39 EXPECT_EQ(2, params::PerHostBlackListOptOutThreshold()); |
| 40 EXPECT_EQ(4, params::HostIndifferentBlackListOptOutThreshold()); |
| 41 EXPECT_EQ(base::TimeDelta::FromDays(30), params::PerHostBlackListDuration()); |
| 42 EXPECT_EQ(base::TimeDelta::FromDays(365 * 100), |
| 43 params::HostIndifferentBlackListPerHostDuration()); |
| 44 EXPECT_EQ(base::TimeDelta::FromSeconds(60 * 5), |
| 45 params::SingleOptOutDuration()); |
| 46 EXPECT_EQ(base::TimeDelta::FromDays(7), |
| 47 params::OfflinePreviewFreshnessDuration()); |
| 48 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G, |
| 49 params::EffectiveConnectionTypeThreshold()); |
| 50 } |
| 51 |
| 52 // Creates a map of the params names to the const string values above. |
| 53 std::map<std::string, std::string> GetFieldTrialParams() { |
| 54 std::map<std::string, std::string> params; |
| 55 // Assign different values than defaults. |
| 56 params["per_host_max_stored_history_length"] = kMaxStoredHistoryLengthPerHost; |
| 57 params["host_indifferent_max_stored_history_length"] = |
| 58 kMaxStoredHistoryLengthHostIndifferent; |
| 59 params["max_hosts_in_blacklist"] = kMaxHostsInBlackList; |
| 60 params["per_host_opt_out_threshold"] = kPerHostOptOutThreshold; |
| 61 params["host_indifferent_opt_out_threshold"] = |
| 62 kHostIndifferentOptOutThreshold; |
| 63 params["per_host_black_list_duration_in_days"] = |
| 64 kPerHostBlackListDurationInDays; |
| 65 params["host_indifferent_black_list_duration_in_days"] = |
| 66 kHostIndifferentBlackListDurationInDays; |
| 67 params["single_opt_out_duration_in_seconds"] = kSingleOptOutDurationInSeconds; |
| 68 params["offline_preview_freshness_duration_in_days"] = |
| 69 kOfflinePreviewFreshnessDurationInDays; |
| 70 params["max_allowed_effective_connection_type"] = |
| 71 kEffectiveConnectionTypeThreshold; |
| 72 |
| 73 return params; |
| 74 } |
| 75 |
| 76 // Verifies that calling the params methods returns the correct values based on |
| 77 // the const string values above. |
| 78 void VerifyNewParams() { |
| 79 { |
| 80 size_t history_length; |
| 81 EXPECT_TRUE( |
| 82 base::StringToSizeT(kMaxStoredHistoryLengthPerHost, &history_length)); |
| 83 EXPECT_EQ(history_length, |
| 84 params::MaxStoredHistoryLengthForPerHostBlackList()); |
| 85 } |
| 86 { |
| 87 size_t history_length; |
| 88 EXPECT_TRUE(base::StringToSizeT(kMaxStoredHistoryLengthHostIndifferent, |
| 89 &history_length)); |
| 90 EXPECT_EQ(history_length, |
| 91 params::MaxStoredHistoryLengthForHostIndifferentBlackList()); |
| 92 } |
| 93 { |
| 94 size_t history_length; |
| 95 EXPECT_TRUE(base::StringToSizeT(kMaxHostsInBlackList, &history_length)); |
| 96 EXPECT_EQ(history_length, params::MaxInMemoryHostsInBlackList()); |
| 97 } |
| 98 { |
| 99 int threshold; |
| 100 EXPECT_TRUE(base::StringToInt(kPerHostOptOutThreshold, &threshold)); |
| 101 EXPECT_EQ(threshold, params::PerHostBlackListOptOutThreshold()); |
| 102 } |
| 103 { |
| 104 int threshold; |
| 105 EXPECT_TRUE(base::StringToInt(kHostIndifferentOptOutThreshold, &threshold)); |
| 106 EXPECT_EQ(threshold, params::HostIndifferentBlackListOptOutThreshold()); |
| 107 } |
| 108 { |
| 109 int days; |
| 110 EXPECT_TRUE(base::StringToInt(kPerHostBlackListDurationInDays, &days)); |
| 111 EXPECT_EQ(base::TimeDelta::FromDays(days), |
| 112 params::PerHostBlackListDuration()); |
| 113 } |
| 114 { |
| 115 int days; |
| 116 EXPECT_TRUE( |
| 117 base::StringToInt(kHostIndifferentBlackListDurationInDays, &days)); |
| 118 EXPECT_EQ(base::TimeDelta::FromDays(days), |
| 119 params::HostIndifferentBlackListPerHostDuration()); |
| 120 } |
| 121 { |
| 122 int seconds; |
| 123 EXPECT_TRUE(base::StringToInt(kSingleOptOutDurationInSeconds, &seconds)); |
| 124 EXPECT_EQ(base::TimeDelta::FromSeconds(seconds), |
| 125 params::SingleOptOutDuration()); |
| 126 } |
| 127 { |
| 128 int days; |
| 129 EXPECT_TRUE( |
| 130 base::StringToInt(kOfflinePreviewFreshnessDurationInDays, &days)); |
| 131 EXPECT_EQ(base::TimeDelta::FromDays(days), |
| 132 params::OfflinePreviewFreshnessDuration()); |
| 133 } |
| 134 { |
| 135 net::EffectiveConnectionType effective_connection_type; |
| 136 EXPECT_TRUE(net::GetEffectiveConnectionTypeForName( |
| 137 kEffectiveConnectionTypeThreshold, &effective_connection_type)); |
| 138 EXPECT_EQ(effective_connection_type, |
| 139 params::EffectiveConnectionTypeThreshold()); |
| 140 } |
| 141 } |
| 142 |
14 } // namespace | 143 } // namespace |
15 | 144 |
16 namespace previews { | 145 // Verifies that we can enable offline previews via field trial. |
17 | |
18 TEST_F(PreviewsExperimentsTest, TestFieldTrialOfflinePage) { | 146 TEST_F(PreviewsExperimentsTest, TestFieldTrialOfflinePage) { |
19 EXPECT_FALSE(IsIncludedInClientSidePreviewsExperimentsFieldTrial()); | 147 EXPECT_FALSE(IsIncludedInClientSidePreviewsExperimentsFieldTrial()); |
20 EXPECT_FALSE(IsPreviewsTypeEnabled(PreviewsType::OFFLINE)); | 148 EXPECT_FALSE(IsPreviewsTypeEnabled(PreviewsType::OFFLINE)); |
21 | 149 |
22 base::FieldTrialList field_trial_list(nullptr); | 150 base::FieldTrialList field_trial_list(nullptr); |
23 ASSERT_TRUE(EnableOfflinePreviewsForTesting()); | 151 ASSERT_TRUE(EnableOfflinePreviewsForTesting()); |
24 | 152 |
25 EXPECT_TRUE(IsIncludedInClientSidePreviewsExperimentsFieldTrial()); | 153 EXPECT_TRUE(IsIncludedInClientSidePreviewsExperimentsFieldTrial()); |
26 EXPECT_TRUE(IsPreviewsTypeEnabled(PreviewsType::OFFLINE)); | 154 EXPECT_TRUE(IsPreviewsTypeEnabled(PreviewsType::OFFLINE)); |
| 155 variations::testing::ClearAllVariationParams(); |
| 156 } |
| 157 |
| 158 // Verifies that we can enable offline previews via field trial and that the |
| 159 // default params for previews field trials are accurate. |
| 160 TEST_F(PreviewsExperimentsTest, TestAllDefaultParams) { |
| 161 VerifyDefaultParams(); |
| 162 base::FieldTrialList field_trial_list(nullptr); |
| 163 EXPECT_TRUE(variations::AssociateVariationParams( |
| 164 "ClientSidePreviews", "Enabled", GetFieldTrialParams())); |
| 165 EXPECT_TRUE( |
| 166 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled")); |
| 167 VerifyNewParams(); |
| 168 variations::testing::ClearAllVariationParams(); |
27 } | 169 } |
28 | 170 |
29 } // namespace previews | 171 } // namespace previews |
OLD | NEW |