Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 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 "content/browser/preloading/preloading_config.h" |
| 6 | |
| 7 | #include "base/test/scoped_feature_list.h" |
Arthur Sonzogni | bdeca8e | 2023-09-11 08:32:12 | [diff] [blame] | 8 | #include "content/common/features.h" |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 9 | #include "content/public/browser/preloading.h" |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace content { |
| 13 | namespace { |
| 14 | |
| 15 | class PreloadingConfigTest : public ::testing::Test {}; |
| 16 | |
| 17 | TEST_F(PreloadingConfigTest, EmptyConfig) { |
| 18 | base::test::ScopedFeatureList features; |
| 19 | features.InitAndEnableFeatureWithParameters(features::kPreloadingConfig, |
| 20 | {{"preloading_config", ""}}); |
| 21 | PreloadingConfig& config = PreloadingConfig::GetInstance(); |
| 22 | config.ParseConfig(); |
| 23 | // Unspecified configs should not be held back. |
| 24 | EXPECT_FALSE( |
| 25 | config.ShouldHoldback(PreloadingType::kPreconnect, |
| 26 | preloading_predictor::kUrlPointerDownOnAnchor)); |
| 27 | // Unspecified configs should log 100% of PreloadingAttempts. |
| 28 | EXPECT_EQ( |
| 29 | config.SamplingLikelihood(PreloadingType::kPreconnect, |
| 30 | preloading_predictor::kUrlPointerDownOnAnchor), |
| 31 | 1.0); |
| 32 | } |
| 33 | |
| 34 | TEST_F(PreloadingConfigTest, NonJsonConfig) { |
| 35 | base::test::ScopedFeatureList features; |
| 36 | features.InitAndEnableFeatureWithParameters(features::kPreloadingConfig, |
| 37 | {{"preloading_config", "bad"}}); |
| 38 | PreloadingConfig& config = PreloadingConfig::GetInstance(); |
| 39 | config.ParseConfig(); |
| 40 | // Unspecified configs should not be held back. |
| 41 | EXPECT_FALSE( |
| 42 | config.ShouldHoldback(PreloadingType::kPreconnect, |
| 43 | preloading_predictor::kUrlPointerDownOnAnchor)); |
| 44 | // Unspecified configs should log 100% of PreloadingAttempts. |
| 45 | EXPECT_EQ( |
| 46 | config.SamplingLikelihood(PreloadingType::kPreconnect, |
| 47 | preloading_predictor::kUrlPointerDownOnAnchor), |
| 48 | 1.0); |
| 49 | } |
| 50 | |
| 51 | TEST_F(PreloadingConfigTest, InvalidPreloadingType) { |
| 52 | base::test::ScopedFeatureList features; |
| 53 | features.InitAndEnableFeatureWithParameters(features::kPreloadingConfig, |
| 54 | {{"preloading_config", R"( |
| 55 | [{ |
| 56 | "preloading_type": "Foo", |
| 57 | "preloading_predictor": "UrlPointerDownOnAnchor", |
| 58 | "holdback": true |
| 59 | }] |
| 60 | )"}}); |
| 61 | PreloadingConfig& config = PreloadingConfig::GetInstance(); |
| 62 | config.ParseConfig(); |
| 63 | // Unspecified configs should not be held back. |
| 64 | EXPECT_FALSE( |
| 65 | config.ShouldHoldback(PreloadingType::kPreconnect, |
| 66 | preloading_predictor::kUrlPointerDownOnAnchor)); |
| 67 | } |
| 68 | |
| 69 | TEST_F(PreloadingConfigTest, InvalidPredictor) { |
| 70 | base::test::ScopedFeatureList features; |
| 71 | features.InitAndEnableFeatureWithParameters(features::kPreloadingConfig, |
| 72 | {{"preloading_config", R"( |
| 73 | [{ |
| 74 | "preloading_type": "Preconnect", |
| 75 | "preloading_predictor": "Foo", |
| 76 | "holdback": true |
| 77 | }] |
| 78 | )"}}); |
| 79 | PreloadingConfig& config = PreloadingConfig::GetInstance(); |
| 80 | config.ParseConfig(); |
| 81 | // Unspecified configs should not be held back. |
| 82 | EXPECT_FALSE( |
| 83 | config.ShouldHoldback(PreloadingType::kPreconnect, |
| 84 | preloading_predictor::kUrlPointerDownOnAnchor)); |
| 85 | } |
| 86 | |
| 87 | TEST_F(PreloadingConfigTest, ValidConfig) { |
| 88 | base::test::ScopedFeatureList features; |
| 89 | features.InitAndEnableFeatureWithParameters(features::kPreloadingConfig, |
| 90 | {{"preloading_config", R"( |
| 91 | [{ |
| 92 | "preloading_type": "Preconnect", |
| 93 | "preloading_predictor": "UrlPointerDownOnAnchor", |
| 94 | "holdback": true, |
| 95 | "sampling_likelihood": 0.5 |
| 96 | },{ |
| 97 | "preloading_type": "Prerender", |
| 98 | "preloading_predictor": "UrlPointerHoverOnAnchor", |
| 99 | "holdback": true, |
| 100 | "sampling_likelihood": 0.25 |
| 101 | }] |
| 102 | )"}}); |
| 103 | PreloadingConfig& config = PreloadingConfig::GetInstance(); |
| 104 | config.ParseConfig(); |
| 105 | EXPECT_TRUE( |
| 106 | config.ShouldHoldback(PreloadingType::kPreconnect, |
| 107 | preloading_predictor::kUrlPointerDownOnAnchor)); |
| 108 | EXPECT_EQ( |
| 109 | config.SamplingLikelihood(PreloadingType::kPreconnect, |
| 110 | preloading_predictor::kUrlPointerDownOnAnchor), |
| 111 | 0.5); |
| 112 | EXPECT_TRUE( |
| 113 | config.ShouldHoldback(PreloadingType::kPrerender, |
| 114 | preloading_predictor::kUrlPointerHoverOnAnchor)); |
| 115 | EXPECT_EQ( |
| 116 | config.SamplingLikelihood(PreloadingType::kPrerender, |
| 117 | preloading_predictor::kUrlPointerHoverOnAnchor), |
| 118 | 0.25); |
| 119 | // This isn't in the config, so make sure we get the default settings. |
| 120 | EXPECT_FALSE( |
| 121 | config.ShouldHoldback(PreloadingType::kPrerender, |
| 122 | preloading_predictor::kUrlPointerDownOnAnchor)); |
| 123 | EXPECT_EQ( |
| 124 | config.SamplingLikelihood(PreloadingType::kPrerender, |
| 125 | preloading_predictor::kUrlPointerDownOnAnchor), |
| 126 | 1.0); |
| 127 | } |
| 128 | |
| 129 | } // namespace |
| 130 | } // namespace content |