Commit 3c0dcc61 authored by Doug Arnett's avatar Doug Arnett Committed by Commit Bot

Adds experiment_name support to PageHint based previews.

Checks experiment_name field on Optimization defined at PageHint
level and only whitelists if that experiment name is enabled for
the client.

Bug: 888666
Change-Id: I42425aaf9403a023d684c94ce94860cba27513f2
Reviewed-on: https://chromium-review.googlesource.com/1241214Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Commit-Queue: Doug Arnett <dougarnett@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594384}
parent 74093950
......@@ -250,6 +250,11 @@ std::unique_ptr<PreviewsHints> PreviewsHints::CreateFromConfig(
for (const auto& page_hint : hint.page_hints()) {
for (const auto& optimization : page_hint.whitelisted_optimizations()) {
if (IsDisabledExperimentalOptimization(optimization)) {
// This is an experimental optimization that is not enabled so
// continue in case there is a non-experimental one.
continue;
}
base::Optional<PreviewsType> previews_type =
ConvertProtoOptimizationTypeToPreviewsOptimizationType(
optimization.optimization_type());
......@@ -442,6 +447,11 @@ bool PreviewsHints::IsWhitelisted(const GURL& url,
matched_page_hint->whitelisted_optimizations()) {
if (ConvertProtoOptimizationTypeToPreviewsOptimizationType(
optimization.optimization_type()) == type) {
if (IsDisabledExperimentalOptimization(optimization)) {
// This is an experimental optimization that is not enabled so continue
// in case there is a non-experimental one.
continue;
}
*out_inflation_percent = optimization.inflation_percent();
return true;
}
......
......@@ -41,6 +41,7 @@ class PreviewsHintsTest : public testing::Test {
base::Version("1.0"),
temp_dir_.GetPath().Append(FILE_PATH_LITERAL("somefile.pb")));
previews_hints_ = PreviewsHints::CreateFromConfig(config, info);
previews_hints_->Initialize();
}
PreviewsHints* previews_hints() { return previews_hints_.get(); }
......@@ -217,4 +218,64 @@ TEST_F(PreviewsHintsTest, ParseConfigWithTooLargeBlacklist) {
GURL("https://black.com/path"), PreviewsType::LITE_PAGE_REDIRECT));
}
TEST_F(PreviewsHintsTest, IsWhitelistedForExperimentalPreview) {
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeature(features::kResourceLoadingHints);
optimization_guide::proto::Configuration config;
optimization_guide::proto::Hint* hint1 = config.add_hints();
hint1->set_key("somedomain.org");
hint1->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
// Page hint for "/experimental_preview/"
optimization_guide::proto::PageHint* page_hint1 = hint1->add_page_hints();
page_hint1->set_page_pattern("/experimental_preview/");
// First add experimental PageHint optimization.
optimization_guide::proto::Optimization* experimental_optimization =
page_hint1->add_whitelisted_optimizations();
experimental_optimization->set_experiment_name("foo_experiment");
experimental_optimization->set_inflation_percent(99);
experimental_optimization->set_optimization_type(
optimization_guide::proto::RESOURCE_LOADING);
optimization_guide::proto::ResourceLoadingHint* experimental_resourcehint =
experimental_optimization->add_resource_loading_hints();
experimental_resourcehint->set_loading_optimization_type(
optimization_guide::proto::LOADING_BLOCK_RESOURCE);
experimental_resourcehint->set_resource_pattern("experimental_resource.js");
// Add a non-experimental PageHint optimization with same resource pattern.
optimization_guide::proto::Optimization* default_pagehint_optimization =
page_hint1->add_whitelisted_optimizations();
default_pagehint_optimization->set_inflation_percent(33);
default_pagehint_optimization->set_optimization_type(
optimization_guide::proto::RESOURCE_LOADING);
optimization_guide::proto::ResourceLoadingHint* default_resourcehint =
default_pagehint_optimization->add_resource_loading_hints();
default_resourcehint->set_loading_optimization_type(
optimization_guide::proto::LOADING_BLOCK_RESOURCE);
default_resourcehint->set_resource_pattern("experimental_resource.js");
ParseConfig(config);
// Verify default resource hint whitelisted (via inflation_percent).
int inflation_percent;
EXPECT_TRUE(previews_hints()->IsWhitelisted(
GURL("https://www.somedomain.org/experimental_preview/"
"experimental_resource.js"),
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent));
EXPECT_EQ(33, inflation_percent);
// Now enable the experiment and verify experimental resource hint chosen.
{
base::test::ScopedFeatureList scoped_list2;
scoped_list2.InitAndEnableFeatureWithParameters(
features::kOptimizationHintsExperiments,
{{"experiment_name", "foo_experiment"}});
int inflation_percent;
EXPECT_TRUE(previews_hints()->IsWhitelisted(
GURL("https://www.somedomain.org/experimental_preview/"
"experimental_resource.js"),
PreviewsType::RESOURCE_LOADING_HINTS, &inflation_percent));
EXPECT_EQ(99, inflation_percent);
}
}
} // namespace previews
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment