Commit 2c28f7c1 authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

Resource loading hints browsertests for experimental hints

Add few browser tests that create component info with
experimental and/or default loading hints. Then, load a webpage
to verify that the resources specified by experimental/default
hints are blocked based on the finch experiment settings.

Change-Id: Id2d20d6f696d8de88a8cf0ed65a319ed67e9a500
Bug: 900699
Reviewed-on: https://chromium-review.googlesource.com/c/1314135
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Reviewed-by: default avatarDoug Arnett <dougarnett@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605377}
parent 43d76b65
...@@ -134,7 +134,8 @@ class ResourceLoadingNoFeaturesBrowserTest : public InProcessBrowserTest { ...@@ -134,7 +134,8 @@ class ResourceLoadingNoFeaturesBrowserTest : public InProcessBrowserTest {
cmd->AppendSwitch("enable-spdy-proxy-auth"); cmd->AppendSwitch("enable-spdy-proxy-auth");
} }
void SetResourceLoadingHints(const std::vector<std::string>& hints_sites) { void SetDefaultOnlyResourceLoadingHints(
const std::vector<std::string>& hints_sites) {
std::vector<std::string> resource_patterns; std::vector<std::string> resource_patterns;
resource_patterns.push_back("foo.jpg"); resource_patterns.push_back("foo.jpg");
resource_patterns.push_back("png"); resource_patterns.push_back("png");
...@@ -152,6 +153,51 @@ class ResourceLoadingNoFeaturesBrowserTest : public InProcessBrowserTest { ...@@ -152,6 +153,51 @@ class ResourceLoadingNoFeaturesBrowserTest : public InProcessBrowserTest {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
// Sets the resource loading hints in optimization guide service. The hints
// are set as experimental.
void SetExperimentOnlyResourceLoadingHints(
const std::vector<std::string>& hints_sites) {
std::vector<std::string> resource_patterns;
resource_patterns.push_back("foo.jpg");
resource_patterns.push_back("png");
resource_patterns.push_back("woff2");
const optimization_guide::ComponentInfo& component_info =
test_component_creator_.CreateComponentInfoWithExperimentalPageHints(
optimization_guide::proto::RESOURCE_LOADING, hints_sites,
resource_patterns);
g_browser_process->optimization_guide_service()->ProcessHints(
component_info);
// Wait for hints to be processed by PreviewsOptimizationGuide.
base::RunLoop().RunUntilIdle();
}
// Sets the resource loading hints in optimization guide service. Some hints
// are set as experimental, while others are set as default.
void SetMixResourceLoadingHints(const std::vector<std::string>& hints_sites) {
std::vector<std::string> experimental_resource_patterns;
experimental_resource_patterns.push_back("foo.jpg");
experimental_resource_patterns.push_back("png");
experimental_resource_patterns.push_back("woff2");
std::vector<std::string> default_resource_patterns;
default_resource_patterns.push_back("bar.jpg");
default_resource_patterns.push_back("woff2");
const optimization_guide::ComponentInfo& component_info =
test_component_creator_.CreateComponentInfoWithMixPageHints(
optimization_guide::proto::RESOURCE_LOADING, hints_sites,
experimental_resource_patterns, default_resource_patterns);
g_browser_process->optimization_guide_service()->ProcessHints(
component_info);
// Wait for hints to be processed by PreviewsOptimizationGuide.
base::RunLoop().RunUntilIdle();
}
void AddTestOptimizationGuideServiceObserver( void AddTestOptimizationGuideServiceObserver(
TestOptimizationGuideServiceObserver* observer) { TestOptimizationGuideServiceObserver* observer) {
g_browser_process->optimization_guide_service()->AddObserver(observer); g_browser_process->optimization_guide_service()->AddObserver(observer);
...@@ -282,34 +328,15 @@ class ResourceLoadingHintsBrowserTest ...@@ -282,34 +328,15 @@ class ResourceLoadingHintsBrowserTest
// Previews InfoBar (which these tests triggers) does not work on Mac. // Previews InfoBar (which these tests triggers) does not work on Mac.
// See https://crbug.com/782322 for details. Also occasional flakes on win7 // See https://crbug.com/782322 for details. Also occasional flakes on win7
// (https://crbug.com/789542). // (https://crbug.com/789542).
// Additionally, ResourceLoadingHintsHttpsWhitelistedRedirectToHttps is disabled #if defined(OS_WIN) || defined(OS_MACOSX)
// on all OS types until hints are made to work with redirects. #define DISABLE_ON_WIN_MAC(x) DISABLED_##x
// TODO(jegray): Re-enable ResourceLoadingHintsHttpsWhitelistedRedirectToHttps
// when support for redirects is added: https://crbug.com/891752
#if !defined(OS_MACOSX) && !defined(OS_WIN)
#define MAYBE_ResourceLoadingHintsHttpsWhitelisted \
ResourceLoadingHintsHttpsWhitelisted
#define MAYBE_ResourceLoadingHintsHttpsWhitelistedRedirectToHttps \
DISABLED_ResourceLoadingHintsHttpsWhitelistedRedirectToHttps
#define MAYBE_ResourceLoadingHintsHttpsNoWhitelisted \
ResourceLoadingHintsHttpsNoWhitelisted
#define MAYBE_ResourceLoadingHintsHttp ResourceLoadingHintsHttp
#define MAYBE_ResourceLoadingHintsHttpsWhitelistedNoTransform \
ResourceLoadingHintsHttpsWhitelistedNoTransform
#else #else
#define MAYBE_ResourceLoadingHintsHttpsWhitelisted \ #define DISABLE_ON_WIN_MAC(x) x
DISABLED_ResourceLoadingHintsHttpsWhitelisted
#define MAYBE_ResourceLoadingHintsHttpsWhitelistedRedirectToHttps \
DISABLED_ResourceLoadingHintsHttpsWhitelistedRedirectToHttps
#define MAYBE_ResourceLoadingHintsHttpsNoWhitelisted \
DISABLED_ResourceLoadingHintsHttpsNoWhitelisted
#define MAYBE_ResourceLoadingHintsHttp DISABLED_ResourceLoadingHintsHttp
#define MAYBE_ResourceLoadingHintsHttpsWhitelistedNoTransform \
DISABLED_ResourceLoadingHintsHttpsWhitelistedNoTransform
#endif #endif
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, IN_PROC_BROWSER_TEST_F(
MAYBE_ResourceLoadingHintsHttpsWhitelisted) { ResourceLoadingHintsBrowserTest,
DISABLE_ON_WIN_MAC(ResourceLoadingHintsHttpsWhitelisted)) {
SetExpectedFooJpgRequest(false); SetExpectedFooJpgRequest(false);
SetExpectedBarJpgRequest(true); SetExpectedBarJpgRequest(true);
...@@ -318,7 +345,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -318,7 +345,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Whitelist test URL for resource loading hints. // Whitelist test URL for resource loading hints.
SetResourceLoadingHints({https_url().host()}); SetDefaultOnlyResourceLoadingHints({https_url().host()});
observer.WaitForNotification(); observer.WaitForNotification();
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
...@@ -335,7 +362,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -335,7 +362,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1); static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
histogram_tester.ExpectBucketCount( histogram_tester.ExpectBucketCount(
"Previews.InfoBarAction.ResourceLoadingHints", 0, 1); "Previews.InfoBarAction.ResourceLoadingHints", 0, 1);
// SetResourceLoadingHints sets 3 resource loading hints patterns. // SetDefaultOnlyResourceLoadingHints sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount( histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1); "ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1);
EXPECT_TRUE(resource_loading_hint_intervention_header_seen()); EXPECT_TRUE(resource_loading_hint_intervention_header_seen());
...@@ -358,15 +385,95 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -358,15 +385,95 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 2); static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 2);
histogram_tester.ExpectBucketCount( histogram_tester.ExpectBucketCount(
"Previews.InfoBarAction.ResourceLoadingHints", 0, 2); "Previews.InfoBarAction.ResourceLoadingHints", 0, 2);
// SetResourceLoadingHints sets 3 resource loading hints patterns. // SetDefaultOnlyResourceLoadingHints sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount( histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 2); "ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 2);
EXPECT_TRUE(resource_loading_hint_intervention_header_seen()); EXPECT_TRUE(resource_loading_hint_intervention_header_seen());
} }
// Sets only the experimental hints, but does not enable the matching
// experiment. Verifies that the hints are not used, and the resource loading is
// not blocked.
IN_PROC_BROWSER_TEST_F(
ResourceLoadingHintsBrowserTest,
DISABLE_ON_WIN_MAC(ExperimentalHints_ExperimentIsNotEnabled)) {
SetExpectedFooJpgRequest(true);
SetExpectedBarJpgRequest(true);
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
// Whitelist test URL for resource loading hints.
SetExperimentOnlyResourceLoadingHints({https_url().host()});
observer.WaitForNotification();
base::HistogramTester histogram_tester;
ui_test_utils::NavigateToURL(browser(), https_url());
histogram_tester.ExpectBucketCount(
"Previews.EligibilityReason.ResourceLoadingHints",
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
histogram_tester.ExpectTotalCount(
"Previews.InfoBarAction.ResourceLoadingHints", 0);
histogram_tester.ExpectTotalCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 0);
EXPECT_FALSE(resource_loading_hint_intervention_header_seen());
}
// Sets only the experimental hints, and enables the matching experiment.
// Verifies that the hints are used, and the resource loading is blocked.
IN_PROC_BROWSER_TEST_F(
ResourceLoadingHintsBrowserTest,
DISABLE_ON_WIN_MAC(ExperimentalHints_ExperimentIsEnabled)) {
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeatureWithParameters(
previews::features::kOptimizationHintsExperiments,
{{previews::features::kOptimizationHintsExperimentNameParam,
optimization_guide::testing::kFooExperimentName}});
SetExpectedFooJpgRequest(false);
SetExpectedBarJpgRequest(true);
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
// Whitelist test URL for resource loading hints.
SetExperimentOnlyResourceLoadingHints({https_url().host()});
observer.WaitForNotification();
base::HistogramTester histogram_tester;
ui_test_utils::NavigateToURL(browser(), https_url());
RetryForHistogramUntilCountReached(
&histogram_tester, "ResourceLoadingHints.CountBlockedSubresourcePatterns",
1);
histogram_tester.ExpectUniqueSample(
"ResourceLoadingHints.ResourcePatternsAvailableAtCommit", 1, 1);
histogram_tester.ExpectBucketCount(
"Previews.EligibilityReason.ResourceLoadingHints",
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
histogram_tester.ExpectBucketCount(
"Previews.InfoBarAction.ResourceLoadingHints", 0, 1);
// SetDefaultOnlyResourceLoadingHints sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1);
EXPECT_TRUE(resource_loading_hint_intervention_header_seen());
}
// Sets both the experimental and default hints, and enables the matching
// experiment. Verifies that the hints are used, and the resource loading is
// blocked.
IN_PROC_BROWSER_TEST_F( IN_PROC_BROWSER_TEST_F(
ResourceLoadingHintsBrowserTest, ResourceLoadingHintsBrowserTest,
MAYBE_ResourceLoadingHintsHttpsWhitelistedRedirectToHttps) { DISABLE_ON_WIN_MAC(MixExperimentalHints_ExperimentIsEnabled)) {
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeatureWithParameters(
previews::features::kOptimizationHintsExperiments,
{{previews::features::kOptimizationHintsExperimentNameParam,
optimization_guide::testing::kFooExperimentName}});
SetExpectedFooJpgRequest(false); SetExpectedFooJpgRequest(false);
SetExpectedBarJpgRequest(true); SetExpectedBarJpgRequest(true);
...@@ -374,7 +481,84 @@ IN_PROC_BROWSER_TEST_F( ...@@ -374,7 +481,84 @@ IN_PROC_BROWSER_TEST_F(
AddTestOptimizationGuideServiceObserver(&observer); AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
SetResourceLoadingHints({https_url().host()}); // Whitelist test URL for resource loading hints. Set both experimental and
// non-experimental hints.
SetMixResourceLoadingHints({https_url().host()});
observer.WaitForNotification();
base::HistogramTester histogram_tester;
ui_test_utils::NavigateToURL(browser(), https_url());
RetryForHistogramUntilCountReached(
&histogram_tester, "ResourceLoadingHints.CountBlockedSubresourcePatterns",
1);
histogram_tester.ExpectUniqueSample(
"ResourceLoadingHints.ResourcePatternsAvailableAtCommit", 1, 1);
histogram_tester.ExpectBucketCount(
"Previews.EligibilityReason.ResourceLoadingHints",
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
histogram_tester.ExpectBucketCount(
"Previews.InfoBarAction.ResourceLoadingHints", 0, 1);
// SetDefaultOnlyResourceLoadingHints sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1);
EXPECT_TRUE(resource_loading_hint_intervention_header_seen());
}
// Sets both the experimental and default hints, but does not enable the
// matching experiment. Verifies that the hints from the experiment are not
// used.
IN_PROC_BROWSER_TEST_F(
ResourceLoadingHintsBrowserTest,
DISABLE_ON_WIN_MAC(MixExperimentalHints_ExperimentIsNotEnabled)) {
base::test::ScopedFeatureList scoped_list;
scoped_list.InitAndEnableFeatureWithParameters(
previews::features::kOptimizationHintsExperiments,
{{previews::features::kOptimizationHintsExperimentNameParam,
"some_other_experiment"}});
SetExpectedFooJpgRequest(true);
SetExpectedBarJpgRequest(false);
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
// Whitelist test URL for resource loading hints.
SetMixResourceLoadingHints({https_url().host()});
observer.WaitForNotification();
base::HistogramTester histogram_tester;
ui_test_utils::NavigateToURL(browser(), https_url());
RetryForHistogramUntilCountReached(
&histogram_tester, "ResourceLoadingHints.CountBlockedSubresourcePatterns",
1);
histogram_tester.ExpectBucketCount(
"Previews.EligibilityReason.ResourceLoadingHints",
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
// Infobar would still be shown since there were at least one resource
// loading hints available, even though none of them matched.
histogram_tester.ExpectTotalCount(
"Previews.InfoBarAction.ResourceLoadingHints", 1);
EXPECT_TRUE(resource_loading_hint_intervention_header_seen());
}
// Disabled on all OS types until hints are made to work with redirects.
// TODO(jegray): https://crbug.com/891752. Re-enable when support for redirects
// is added.
IN_PROC_BROWSER_TEST_F(
ResourceLoadingHintsBrowserTest,
DISABLED_ResourceLoadingHintsHttpsWhitelistedRedirectToHttps) {
SetExpectedFooJpgRequest(false);
SetExpectedBarJpgRequest(true);
TestOptimizationGuideServiceObserver observer;
AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle();
SetDefaultOnlyResourceLoadingHints({https_url().host()});
observer.WaitForNotification(); observer.WaitForNotification();
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
...@@ -389,14 +573,15 @@ IN_PROC_BROWSER_TEST_F( ...@@ -389,14 +573,15 @@ IN_PROC_BROWSER_TEST_F(
static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1); static_cast<int>(previews::PreviewsEligibilityReason::ALLOWED), 1);
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"Previews.InfoBarAction.ResourceLoadingHints", 1); "Previews.InfoBarAction.ResourceLoadingHints", 1);
// SetResourceLoadingHints sets 3 resource loading hints patterns. // SetDefaultOnlyResourceLoadingHints sets 3 resource loading hints patterns.
histogram_tester.ExpectBucketCount( histogram_tester.ExpectBucketCount(
"ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1); "ResourceLoadingHints.CountBlockedSubresourcePatterns", 3, 1);
EXPECT_TRUE(resource_loading_hint_intervention_header_seen()); EXPECT_TRUE(resource_loading_hint_intervention_header_seen());
} }
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, IN_PROC_BROWSER_TEST_F(
MAYBE_ResourceLoadingHintsHttpsNoWhitelisted) { ResourceLoadingHintsBrowserTest,
DISABLE_ON_WIN_MAC(ResourceLoadingHintsHttpsNoWhitelisted)) {
SetExpectedFooJpgRequest(true); SetExpectedFooJpgRequest(true);
SetExpectedBarJpgRequest(true); SetExpectedBarJpgRequest(true);
...@@ -404,7 +589,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -404,7 +589,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
AddTestOptimizationGuideServiceObserver(&observer); AddTestOptimizationGuideServiceObserver(&observer);
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
SetResourceLoadingHints({}); SetDefaultOnlyResourceLoadingHints({});
observer.WaitForNotification(); observer.WaitForNotification();
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
...@@ -426,7 +611,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -426,7 +611,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
} }
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
MAYBE_ResourceLoadingHintsHttp) { DISABLE_ON_WIN_MAC(ResourceLoadingHintsHttp)) {
SetExpectedFooJpgRequest(true); SetExpectedFooJpgRequest(true);
SetExpectedBarJpgRequest(true); SetExpectedBarJpgRequest(true);
...@@ -435,7 +620,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -435,7 +620,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Whitelist test HTTP URL for resource loading hints. // Whitelist test HTTP URL for resource loading hints.
SetResourceLoadingHints({https_url().host()}); SetDefaultOnlyResourceLoadingHints({https_url().host()});
observer.WaitForNotification(); observer.WaitForNotification();
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
...@@ -453,8 +638,9 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -453,8 +638,9 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
EXPECT_FALSE(resource_loading_hint_intervention_header_seen()); EXPECT_FALSE(resource_loading_hint_intervention_header_seen());
} }
IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, IN_PROC_BROWSER_TEST_F(
MAYBE_ResourceLoadingHintsHttpsWhitelistedNoTransform) { ResourceLoadingHintsBrowserTest,
DISABLE_ON_WIN_MAC(ResourceLoadingHintsHttpsWhitelistedNoTransform)) {
SetExpectedFooJpgRequest(true); SetExpectedFooJpgRequest(true);
SetExpectedBarJpgRequest(true); SetExpectedBarJpgRequest(true);
...@@ -463,7 +649,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest, ...@@ -463,7 +649,7 @@ IN_PROC_BROWSER_TEST_F(ResourceLoadingHintsBrowserTest,
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Whitelist test URL for resource loading hints. // Whitelist test URL for resource loading hints.
SetResourceLoadingHints({https_url().host()}); SetDefaultOnlyResourceLoadingHints({https_url().host()});
observer.WaitForNotification(); observer.WaitForNotification();
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
......
...@@ -71,6 +71,86 @@ TestComponentCreator::CreateComponentInfoWithPageHints( ...@@ -71,6 +71,86 @@ TestComponentCreator::CreateComponentInfoWithPageHints(
return WriteConfigToFileAndReturnComponentInfo(config); return WriteConfigToFileAndReturnComponentInfo(config);
} }
optimization_guide::ComponentInfo
TestComponentCreator::CreateComponentInfoWithExperimentalPageHints(
optimization_guide::proto::OptimizationType optimization_type,
const std::vector<std::string>& page_hint_host_suffixes,
const std::vector<std::string>& experimental_resource_patterns) {
optimization_guide::proto::Configuration config;
for (const auto& page_hint_site : page_hint_host_suffixes) {
optimization_guide::proto::Hint* hint = config.add_hints();
hint->set_key(page_hint_site);
hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
optimization_guide::proto::PageHint* page_hint = hint->add_page_hints();
page_hint->set_page_pattern("*");
optimization_guide::proto::Optimization* optimization =
page_hint->add_whitelisted_optimizations();
optimization->set_optimization_type(optimization_type);
optimization->set_experiment_name(kFooExperimentName);
for (auto resource_blocking_pattern : experimental_resource_patterns) {
optimization_guide::proto::ResourceLoadingHint* resource_loading_hint =
optimization->add_resource_loading_hints();
resource_loading_hint->set_loading_optimization_type(
optimization_guide::proto::LOADING_BLOCK_RESOURCE);
resource_loading_hint->set_resource_pattern(resource_blocking_pattern);
}
}
return WriteConfigToFileAndReturnComponentInfo(config);
}
optimization_guide::ComponentInfo
TestComponentCreator::CreateComponentInfoWithMixPageHints(
optimization_guide::proto::OptimizationType optimization_type,
const std::vector<std::string>& page_hint_host_suffixes,
const std::vector<std::string>& experimental_resource_patterns,
const std::vector<std::string>& default_resource_patterns) {
optimization_guide::proto::Configuration config;
for (const auto& page_hint_site : page_hint_host_suffixes) {
optimization_guide::proto::Hint* hint = config.add_hints();
hint->set_key(page_hint_site);
hint->set_key_representation(optimization_guide::proto::HOST_SUFFIX);
optimization_guide::proto::PageHint* page_hint = hint->add_page_hints();
page_hint->set_page_pattern("*");
// Add experimental patterns first so they get higher priority.
{
optimization_guide::proto::Optimization* optimization =
page_hint->add_whitelisted_optimizations();
optimization->set_optimization_type(optimization_type);
optimization->set_experiment_name(kFooExperimentName);
for (auto resource_blocking_pattern : experimental_resource_patterns) {
optimization_guide::proto::ResourceLoadingHint* resource_loading_hint =
optimization->add_resource_loading_hints();
resource_loading_hint->set_loading_optimization_type(
optimization_guide::proto::LOADING_BLOCK_RESOURCE);
resource_loading_hint->set_resource_pattern(resource_blocking_pattern);
}
}
{
optimization_guide::proto::Optimization* optimization =
page_hint->add_whitelisted_optimizations();
optimization->set_optimization_type(optimization_type);
for (auto resource_blocking_pattern : default_resource_patterns) {
optimization_guide::proto::ResourceLoadingHint* resource_loading_hint =
optimization->add_resource_loading_hints();
resource_loading_hint->set_loading_optimization_type(
optimization_guide::proto::LOADING_BLOCK_RESOURCE);
resource_loading_hint->set_resource_pattern(resource_blocking_pattern);
}
}
}
return WriteConfigToFileAndReturnComponentInfo(config);
}
base::FilePath TestComponentCreator::GetFilePath(std::string file_path_suffix) { base::FilePath TestComponentCreator::GetFilePath(std::string file_path_suffix) {
base::ScopedAllowBlockingForTesting allow_blocking; base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_TRUE(scoped_temp_dir_->IsValid() || EXPECT_TRUE(scoped_temp_dir_->IsValid() ||
......
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
namespace optimization_guide { namespace optimization_guide {
namespace testing { namespace testing {
// Experiment name used for experimental resource loading hints.
static const char kFooExperimentName[] = "foo_experiment";
// Helper class to create test OptimizationHints components for testing. // Helper class to create test OptimizationHints components for testing.
// //
// All temporary files and paths are cleaned up when this instance goes out of // All temporary files and paths are cleaned up when this instance goes out of
...@@ -31,14 +34,42 @@ class TestComponentCreator { ...@@ -31,14 +34,42 @@ class TestComponentCreator {
optimization_guide::ComponentInfo CreateComponentInfoWithTopLevelWhitelist( optimization_guide::ComponentInfo CreateComponentInfoWithTopLevelWhitelist(
optimization_guide::proto::OptimizationType optimization_type, optimization_guide::proto::OptimizationType optimization_type,
const std::vector<std::string>& whitelisted_host_suffixes); const std::vector<std::string>& whitelisted_host_suffixes);
// Creates component data based on |whitelisted_host_suffixes| with page hints // Creates component data based on |whitelisted_host_suffixes| with page hints
// for type |optimization_type| blocking resources specified by // for type |optimization_type| blocking resources specified by
// |resource_patterns|.and returns the ComponentInfo for it. // |resource_patterns|, and returns the ComponentInfo for it.
optimization_guide::ComponentInfo CreateComponentInfoWithPageHints( optimization_guide::ComponentInfo CreateComponentInfoWithPageHints(
optimization_guide::proto::OptimizationType optimization_type, optimization_guide::proto::OptimizationType optimization_type,
const std::vector<std::string>& whitelisted_host_suffixes, const std::vector<std::string>& whitelisted_host_suffixes,
const std::vector<std::string>& resource_patterns); const std::vector<std::string>& resource_patterns);
// Creates component data based on |whitelisted_host_suffixes| with page hints
// for type |optimization_type| blocking resources specified by
// |experimental_resource_patterns|, and returns the ComponentInfo for it.
// The loading hints are set as experimental with experiment name set to
// kFooExperimentName.
// Creates component data for testing with experimental optimizations. It
// creates a PageHint (with page pattern "*" for each key in
// |whitelisted_host_suffixes| that each has resource blocking patterns from
// |experimental_resource_patterns|.
optimization_guide::ComponentInfo
CreateComponentInfoWithExperimentalPageHints(
optimization_guide::proto::OptimizationType optimization_type,
const std::vector<std::string>& whitelisted_host_suffixes,
const std::vector<std::string>& experimental_resource_patterns);
// Creates component data for testing with both default and experimental
// optimizations. It creates a PageHint (with page pattern "*" for each key in
// |whitelisted_host_suffixes| that each has resource blocking patterns from
// |default_resource_patterns| and |experimental_resource_patterns|. The
// experimental hints are guarded behind experiment kFooExperimentName.
optimization_guide::ComponentInfo CreateComponentInfoWithMixPageHints(
optimization_guide::proto::OptimizationType optimization_type,
const std::vector<std::string>& whitelisted_host_suffixes,
const std::vector<std::string>& experimental_resource_patterns,
const std::vector<std::string>& default_resource_patterns);
private: private:
// Returns the scoped temp directory path with the |file_path_suffix| that is // Returns the scoped temp directory path with the |file_path_suffix| that is
// valid for the lifetime of this instance. // valid for the lifetime of this instance.
......
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