Commit 1322865e authored by Sophie Chang's avatar Sophie Chang Committed by Chromium LUCI CQ

Enable model fetching for non-MSBB

This ensures we don't send hosts and field trials for non-MSBB users

Bug: 1146151
Change-Id: I146977f62943aaa77a0b85cd7196a13ad5ca660a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2579838Reviewed-by: default avatarMichael Crouse <mcrouse@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835259}
parent 7bc42a7e
......@@ -321,6 +321,10 @@ void PredictionManager::RegisterOptimizationTargets(
if (new_optimization_targets.size() == 0)
return;
// If no fetch is scheduled, maybe schedule one.
if (!fetch_timer_.IsRunning())
MaybeScheduleModelAndHostModelFeaturesFetch();
// Start loading the host model features if they are not already.
if (!host_model_features_loaded_) {
LoadHostModelFeatures();
......@@ -605,7 +609,8 @@ void PredictionManager::SetPredictionModelDownloadManagerForTesting(
void PredictionManager::FetchModelsAndHostModelFeatures() {
SEQUENCE_CHECKER(sequence_checker_);
if (!IsUserPermittedToFetchFromRemoteOptimizationGuide(profile_))
if (!features::IsRemoteFetchingEnabled())
return;
ScheduleModelsAndHostModelFeaturesFetch();
......@@ -633,24 +638,31 @@ void PredictionManager::FetchModelsAndHostModelFeatures() {
prediction_model_download_manager_->CancelAllPendingDownloads();
std::vector<std::string> top_hosts;
// If the top host provider is not available, the user has likely not seen the
// Lite mode infobar, so top hosts cannot be provided. However, prediction
// models are allowed to be fetched.
if (top_host_provider_) {
top_hosts = top_host_provider_->GetTopHosts();
// Remove hosts that are already available in the host model features cache.
// The request should still be made in case there is a new model or a model
// that does not rely on host model features to be fetched.
auto it = top_hosts.begin();
while (it != top_hosts.end()) {
if (host_model_features_cache_.Peek(*it) !=
host_model_features_cache_.end()) {
it = top_hosts.erase(it);
continue;
std::vector<proto::FieldTrial> active_field_trials;
// Top hosts and active field trials convey some sort of user information, so
// ensure that the user has opted into the right permissions before adding
// these fields to the request.
if (IsUserPermittedToFetchFromRemoteOptimizationGuide(profile_)) {
if (top_host_provider_) {
top_hosts = top_host_provider_->GetTopHosts();
// Remove hosts that are already available in the host model features
// cache. The request should still be made in case there is a new model or
// a model that does not rely on host model features to be fetched.
auto it = top_hosts.begin();
while (it != top_hosts.end()) {
if (host_model_features_cache_.Peek(*it) !=
host_model_features_cache_.end()) {
it = top_hosts.erase(it);
continue;
}
++it;
}
++it;
}
google::protobuf::RepeatedPtrField<proto::FieldTrial> current_field_trials =
GetActiveFieldTrialsAllowedForFetch();
active_field_trials = std::vector<proto::FieldTrial>(
{current_field_trials.begin(), current_field_trials.end()});
}
if (!prediction_model_fetcher_) {
......@@ -688,7 +700,8 @@ void PredictionManager::FetchModelsAndHostModelFeatures() {
}
prediction_model_fetcher_->FetchOptimizationGuideServiceModels(
models_info, top_hosts, optimization_guide::proto::CONTEXT_BATCH_UPDATE,
models_info, top_hosts, active_field_trials,
optimization_guide::proto::CONTEXT_BATCH_UPDATE,
base::BindOnce(&PredictionManager::OnModelsAndHostFeaturesFetched,
ui_weak_ptr_factory_.GetWeakPtr()));
}
......@@ -1097,7 +1110,7 @@ bool PredictionManager::ProcessAndStoreHostModelFeatures(
}
void PredictionManager::MaybeScheduleModelAndHostModelFeaturesFetch() {
if (!IsUserPermittedToFetchFromRemoteOptimizationGuide(profile_))
if (!features::IsRemoteFetchingEnabled())
return;
if (optimization_guide::switches::
......
......@@ -350,6 +350,10 @@ class PredictionManagerBrowserTestBase : public InProcessBrowserTest {
expected_field_trial_name_hashes_ = expected_field_trial_name_hashes;
}
void SetExpectedHostsSentInRequest(bool expected_hosts_sent_in_request) {
expected_hosts_sent_in_request_ = expected_hosts_sent_in_request;
}
GURL https_url_with_content() { return https_url_with_content_; }
GURL https_url_without_content() { return https_url_without_content_; }
......@@ -392,11 +396,15 @@ class PredictionManagerBrowserTestBase : public InProcessBrowserTest {
EXPECT_EQ(seen_field_trial_name_hashes.size(),
expected_field_trial_name_hashes_.size());
EXPECT_EQ(expected_hosts_sent_in_request_, !models_request.hosts().empty());
std::vector<std::string> hosts;
if (expected_hosts_sent_in_request_) {
hosts = {"example1.com", https_server_->GetURL("/").host()};
}
response->set_code(net::HTTP_OK);
std::unique_ptr<optimization_guide::proto::GetModelsResponse>
get_models_response = BuildGetModelsResponse(
{"example1.com", https_server_->GetURL("/").host()},
{client_model_feature_});
get_models_response =
BuildGetModelsResponse(hosts, {client_model_feature_});
if (response_type_ == PredictionModelsFetcherRemoteResponseType::
kSuccessfulWithFeaturesAndNoModels) {
get_models_response->clear_models();
......@@ -431,6 +439,7 @@ class PredictionManagerBrowserTestBase : public InProcessBrowserTest {
kSuccessfulWithModelsAndFeatures;
std::unique_ptr<OptimizationGuideConsumerWebContentsObserver> consumer_;
base::flat_set<uint32_t> expected_field_trial_name_hashes_;
bool expected_hosts_sent_in_request_ = true;
};
class PredictionManagerBrowserTest : public PredictionManagerBrowserTestBase {
......@@ -753,6 +762,72 @@ IN_PROC_BROWSER_TEST_F(
run_loop->Run();
}
class PredictionManagerNoUserPermissionsTest
: public PredictionManagerBrowserTest {
public:
PredictionManagerNoUserPermissionsTest() {
// Hosts and field trials should not be sent.
SetExpectedHostsSentInRequest(false);
SetExpectedFieldTrialNames({});
}
~PredictionManagerNoUserPermissionsTest() override = default;
void SetUpCommandLine(base::CommandLine* cmd) override {
PredictionManagerBrowserTest::SetUpCommandLine(cmd);
// Remove switches that enable user permissions.
cmd->RemoveSwitch("enable-spdy-proxy-auth");
cmd->RemoveSwitch(switches::kDisableCheckingUserPermissionsForTesting);
}
private:
void InitializeFeatureList() override {
scoped_feature_list_.InitWithFeaturesAndParameters(
{
{features::kOptimizationHints, {}},
{features::kRemoteOptimizationGuideFetching, {}},
{features::kOptimizationTargetPrediction, {}},
{features::kOptimizationHintsFieldTrials,
{{"allowed_field_trial_names",
"scoped_feature_list_trial_for_OptimizationHints,scoped_feature_"
"list_trial_for_OptimizationHintsFetching"}}},
},
{});
}
};
IN_PROC_BROWSER_TEST_F(PredictionManagerNoUserPermissionsTest,
DISABLE_ON_WIN_MAC_CHROMEOS(
HostsAndFieldTrialsNotPassedWhenNoUserPermissions)) {
base::HistogramTester histogram_tester;
SetResponseType(PredictionModelsFetcherRemoteResponseType::
kSuccessfulWithModelsAndFeatures);
RegisterWithKeyedService();
RetryForHistogramUntilCountReached(
&histogram_tester,
"OptimizationGuide.PredictionManager.HostModelFeaturesStored", 1);
RetryForHistogramUntilCountReached(
&histogram_tester,
"OptimizationGuide.PredictionManager.PredictionModelsStored", 1);
RetryForHistogramUntilCountReached(
&histogram_tester,
"OptimizationGuide.PredictionModelLoadedVersion.PainfulPageLoad", 1);
SetCallbackOnConsumer(base::DoNothing());
ui_test_utils::NavigateToURL(browser(), https_url_with_content());
// Expect that we did not fetch for host and that we did not get any host
// model features.
histogram_tester.ExpectBucketCount(
"OptimizationGuide.PredictionManager.HasHostModelFeaturesForHost", false,
1);
}
// Implementation of a download system logger that provides the ability to wait
// for certain events to happen, notably added and progressing downloads.
class DownloadServiceObserver : public download::Logger::Observer {
......@@ -860,8 +935,17 @@ class PredictionManagerModelDownloadingBrowserTest
{features::kOptimizationTargetPrediction, {}},
{features::kOptimizationGuideModelDownloading,
{{"unrestricted_model_downloading", "true"}}},
{features::kOptimizationHintsFieldTrials,
{{"allowed_field_trial_names",
"scoped_feature_list_trial_for_OptimizationHints,scoped_feature_"
"list_trial_for_OptimizationHintsFetching"}}},
},
{});
SetExpectedFieldTrialNames(base::flat_set<uint32_t>(
{variations::HashName(
"scoped_feature_list_trial_for_OptimizationHints"),
variations::HashName(
"scoped_feature_list_trial_for_OptimizationHintsFetching")}));
}
std::unique_ptr<DownloadServiceObserver> download_service_observer_;
......
......@@ -240,10 +240,10 @@ class TestPredictionModelFetcher : public PredictionModelFetcher {
fetch_state_(fetch_state) {}
bool FetchOptimizationGuideServiceModels(
const std::vector<optimization_guide::proto::ModelInfo>&
models_request_info,
const std::vector<proto::ModelInfo>& models_request_info,
const std::vector<std::string>& hosts,
optimization_guide::proto::RequestContext request_context,
const std::vector<proto::FieldTrial>& active_field_trials,
proto::RequestContext request_context,
ModelsFetchedCallback models_fetched_callback) override {
if (!ValidateModelsInfoForFetch(models_request_info)) {
std::move(models_fetched_callback).Run(base::nullopt);
......@@ -631,7 +631,29 @@ TEST_F(PredictionManagerTest,
EXPECT_FALSE(prediction_manager()->registered_optimization_targets().empty());
}
TEST_F(PredictionManagerTest, RemoteFetchingDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(
features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
BuildTestPredictionModelFetcher(
PredictionModelFetcherEndState::
kFetchSuccessWithModelsAndHostsModelFeatures));
prediction_manager()->RegisterOptimizationTargets(
{optimization_guide::proto::OPTIMIZATION_TARGET_PAINFUL_PAGE_LOAD});
SetStoreInitialized();
EXPECT_FALSE(prediction_model_fetcher()->models_fetched());
}
TEST_F(PredictionManagerTest, OptimizationTargetNotRegisteredForNavigation) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -680,6 +702,9 @@ TEST_F(PredictionManagerTest, OptimizationTargetNotRegisteredForNavigation) {
}
TEST_F(PredictionManagerTest, AddObserverForOptimizationTargetModel) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -767,6 +792,9 @@ TEST_F(PredictionManagerTest, AddObserverForOptimizationTargetModel) {
TEST_F(PredictionManagerTest,
AddObserverForOptimizationTargetModelExistingFile) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
FakeOptimizationTargetModelObserver observer1;
......@@ -812,6 +840,9 @@ TEST_F(PredictionManagerTest,
TEST_F(PredictionManagerTest,
DISABLE_ON_WIN_MAC_CHROMEOS(
NoPredictionModelForRegisteredOptimizationTarget)) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -846,6 +877,9 @@ TEST_F(PredictionManagerTest,
}
TEST_F(PredictionManagerTest, EvaluatePredictionModel) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -898,6 +932,9 @@ TEST_F(PredictionManagerTest, EvaluatePredictionModel) {
}
TEST_F(PredictionManagerTest, UpdatePredictionModelsWithInvalidModel) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
......@@ -928,6 +965,9 @@ TEST_F(PredictionManagerTest, UpdatePredictionModelsWithInvalidModel) {
}
TEST_F(PredictionManagerTest, UpdateModelWithSameVersion) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
......@@ -971,6 +1011,9 @@ TEST_F(PredictionManagerTest, UpdateModelWithSameVersion) {
}
TEST_F(PredictionManagerTest, UpdateModelFileWithSameVersion) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
......@@ -1011,6 +1054,9 @@ TEST_F(PredictionManagerTest, UpdateModelFileWithSameVersion) {
TEST_F(PredictionManagerTest,
EvaluatePredictionModelUsesDecisionFromPostiveEvalIfModelWasEvaluated) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
GURL("https://foo.com"));
......@@ -1051,6 +1097,9 @@ TEST_F(PredictionManagerTest,
TEST_F(PredictionManagerTest,
EvaluatePredictionModelUsesDecisionFromNegativeEvalIfModelWasEvaluated) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1094,6 +1143,9 @@ TEST_F(PredictionManagerTest,
TEST_F(PredictionManagerTest,
EvaluatePredictionModelUsesDecisionFromHoldbackEvalIfModelWasEvaluated) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
GURL("https://foo.com"));
......@@ -1132,6 +1184,9 @@ TEST_F(PredictionManagerTest,
}
TEST_F(PredictionManagerTest, DownloadManagerUnavailableShouldNotFetch) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1159,6 +1214,9 @@ TEST_F(PredictionManagerTest, DownloadManagerUnavailableShouldNotFetch) {
}
TEST_F(PredictionManagerTest, UpdateModelWithDownloadUrl) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1196,6 +1254,9 @@ TEST_F(PredictionManagerTest, UpdateModelWithDownloadUrl) {
}
TEST_F(PredictionManagerTest, EvaluatePredictionModelPopulatesNavData) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1249,10 +1310,11 @@ TEST_F(PredictionManagerTest, EvaluatePredictionModelPopulatesNavData) {
TEST_F(PredictionManagerTest,
EvaluatePredictionModelPopulatesNavDataEvenWithHoldback) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kOptimizationTargetPrediction,
{{"painful_page_load_metrics_only", "true"}}}},
{});
scoped_feature_list.InitWithFeaturesAndParameters(
{{features::kOptimizationTargetPrediction,
{{"painful_page_load_metrics_only", "true"}}},
{features::kRemoteOptimizationGuideFetching, {}}},
{});
base::HistogramTester histogram_tester;
......@@ -1305,6 +1367,9 @@ TEST_F(PredictionManagerTest,
}
TEST_F(PredictionManagerTest, ShouldTargetNavigationStoreAvailableNoModel) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1339,6 +1404,9 @@ TEST_F(PredictionManagerTest, ShouldTargetNavigationStoreAvailableNoModel) {
TEST_F(PredictionManagerTest,
ShouldTargetNavigationStoreAvailableModelNotLoaded) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1377,6 +1445,9 @@ TEST_F(PredictionManagerTest,
TEST_F(PredictionManagerTest,
DISABLE_ON_WIN_MAC_CHROMEOS(
ShouldTargetNavigationStoreUnavailableModelUnknown)) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1408,6 +1479,9 @@ TEST_F(PredictionManagerTest,
}
TEST_F(PredictionManagerTest, UpdateModelForUnregisteredTarget) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
......@@ -1441,6 +1515,9 @@ TEST_F(PredictionManagerTest, UpdateModelForUnregisteredTarget) {
}
TEST_F(PredictionManagerTest, UpdateModelForUnregisteredTargetOnModelReady) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
......@@ -1463,6 +1540,9 @@ TEST_F(PredictionManagerTest, UpdateModelForUnregisteredTargetOnModelReady) {
}
TEST_F(PredictionManagerTest, UpdateModelForRegisteredTargetButNowFile) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
GURL("https://foo.com"));
......@@ -1509,6 +1589,9 @@ TEST_F(PredictionManagerTest, UpdateModelForRegisteredTargetButNowFile) {
TEST_F(
PredictionManagerTest,
DISABLE_ON_WIN_MAC_CHROMEOS(UpdateModelWithUnsupportedOptimizationTarget)) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
GURL("https://foo.com"));
......@@ -1546,6 +1629,9 @@ TEST_F(
}
TEST_F(PredictionManagerTest, HasHostModelFeaturesForHost) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
......@@ -1576,6 +1662,9 @@ TEST_F(PredictionManagerTest, HasHostModelFeaturesForHost) {
}
TEST_F(PredictionManagerTest, NoHostModelFeaturesForHost) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
......@@ -1616,6 +1705,9 @@ TEST_F(PredictionManagerTest, NoHostModelFeaturesForHost) {
}
TEST_F(PredictionManagerTest, UpdateHostModelFeaturesMissingHost) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
BuildTestPredictionModelFetcher(
......@@ -1640,6 +1732,9 @@ TEST_F(PredictionManagerTest, UpdateHostModelFeaturesMissingHost) {
}
TEST_F(PredictionManagerTest, UpdateHostModelFeaturesNoFeature) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
BuildTestPredictionModelFetcher(
......@@ -1663,6 +1758,9 @@ TEST_F(PredictionManagerTest, UpdateHostModelFeaturesNoFeature) {
}
TEST_F(PredictionManagerTest, UpdateHostModelFeaturesNoFeatureName) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
BuildTestPredictionModelFetcher(
......@@ -1689,6 +1787,9 @@ TEST_F(PredictionManagerTest, UpdateHostModelFeaturesNoFeatureName) {
}
TEST_F(PredictionManagerTest, UpdateHostModelFeaturesDoubleValue) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
BuildTestPredictionModelFetcher(
......@@ -1714,6 +1815,9 @@ TEST_F(PredictionManagerTest, UpdateHostModelFeaturesDoubleValue) {
}
TEST_F(PredictionManagerTest, UpdateHostModelFeaturesIntValue) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
BuildTestPredictionModelFetcher(
......@@ -1741,6 +1845,9 @@ TEST_F(PredictionManagerTest, UpdateHostModelFeaturesIntValue) {
}
TEST_F(PredictionManagerTest, RestrictHostModelFeaturesCacheSize) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
CreatePredictionManager({});
prediction_manager()->SetPredictionModelFetcherForTesting(
BuildTestPredictionModelFetcher(
......@@ -1766,6 +1873,9 @@ TEST_F(PredictionManagerTest, RestrictHostModelFeaturesCacheSize) {
}
TEST_F(PredictionManagerTest, FetchWithoutTopHostProvider) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManagerWithoutTopHostProvider({});
......@@ -1787,6 +1897,9 @@ TEST_F(PredictionManagerTest, FetchWithoutTopHostProvider) {
}
TEST_F(PredictionManagerTest, UpdateHostModelFeaturesUpdateDataInMap) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
......@@ -1856,6 +1969,9 @@ INSTANTIATE_TEST_SUITE_P(ClientFeature,
proto::ClientModelFeature_MAX));
TEST_P(PredictionManagerClientFeatureTest, ClientFeature) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
CreateMockNavigationHandleWithOptimizationGuideWebContentsObserver(
......@@ -1907,6 +2023,9 @@ TEST_P(PredictionManagerClientFeatureTest, ClientFeature) {
}
TEST_F(PredictionManagerTest, PreviousSessionStatisticsUsed) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
GURL previous_url = GURL("https://foo.com");
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
......@@ -1975,6 +2094,9 @@ TEST_F(PredictionManagerTest, PreviousSessionStatisticsUsed) {
TEST_F(PredictionManagerTest,
OverriddenClientModelFeaturesUsedIfProvidedBackfilledIfNot) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
GURL previous_url = GURL("https://foo.com");
std::unique_ptr<content::MockNavigationHandle> navigation_handle =
......@@ -2050,6 +2172,9 @@ TEST_F(PredictionManagerTest,
TEST_F(PredictionManagerTest,
StoreInitializedAfterOptimizationTargetRegistered) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
// Ensure that the fetch does not cause any models or features to load.
......@@ -2074,6 +2199,9 @@ TEST_F(PredictionManagerTest,
TEST_F(PredictionManagerTest,
StoreInitializedBeforeOptimizationTargetRegistered) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::HistogramTester histogram_tester;
CreatePredictionManager({});
// Ensure that the fetch does not cause any models or features to load.
......@@ -2100,8 +2228,7 @@ TEST_F(PredictionManagerTest,
TEST_F(PredictionManagerTest, ModelFetcherTimerRetryDelay) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{optimization_guide::features::kRemoteOptimizationGuideFetching}, {});
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::CommandLine::ForCurrentProcess()->RemoveSwitch(
switches::kFetchModelsAndHostModelFeaturesOverrideTimer);
......@@ -2131,8 +2258,7 @@ TEST_F(PredictionManagerTest, ModelFetcherTimerRetryDelay) {
TEST_F(PredictionManagerTest, ModelFetcherTimerFetchSucceeds) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
{optimization_guide::features::kRemoteOptimizationGuideFetching}, {});
feature_list.InitAndEnableFeature(features::kRemoteOptimizationGuideFetching);
base::CommandLine::ForCurrentProcess()->RemoveSwitch(
switches::kFetchModelsAndHostModelFeaturesOverrideTimer);
......
......@@ -44,10 +44,10 @@ PredictionModelFetcher::PredictionModelFetcher(
PredictionModelFetcher::~PredictionModelFetcher() = default;
bool PredictionModelFetcher::FetchOptimizationGuideServiceModels(
const std::vector<optimization_guide::proto::ModelInfo>&
models_request_info,
const std::vector<proto::ModelInfo>& models_request_info,
const std::vector<std::string>& hosts,
optimization_guide::proto::RequestContext request_context,
const std::vector<proto::FieldTrial>& active_field_trials,
proto::RequestContext request_context,
ModelsFetchedCallback models_fetched_callback) {
SEQUENCE_CHECKER(sequence_checker_);
......@@ -70,8 +70,8 @@ bool PredictionModelFetcher::FetchOptimizationGuideServiceModels(
pending_models_request_->set_request_context(request_context);
*pending_models_request_->mutable_active_field_trials() =
optimization_guide::GetActiveFieldTrialsAllowedForFetch();
*pending_models_request_->mutable_active_field_trials() = {
active_field_trials.begin(), active_field_trials.end()};
// Limit the number of hosts to fetch features for, the list of hosts
// is assumed to be ordered from most to least important by the top
......
......@@ -48,10 +48,10 @@ class PredictionModelFetcher {
// request is complete providing the GetModelsResponse object if successful or
// nullopt if the fetch failed or no fetch is needed. Virtualized for testing.
virtual bool FetchOptimizationGuideServiceModels(
const std::vector<optimization_guide::proto::ModelInfo>&
models_request_info,
const std::vector<proto::ModelInfo>& models_request_info,
const std::vector<std::string>& hosts,
optimization_guide::proto::RequestContext request_context,
const std::vector<proto::FieldTrial>& active_field_trials,
proto::RequestContext request_context,
ModelsFetchedCallback models_fetched_callback);
private:
......
......@@ -42,10 +42,8 @@ class PredictionModelFetcherTest : public testing::Test {
~PredictionModelFetcherTest() override {}
void OnModelsFetched(
base::Optional<
std::unique_ptr<optimization_guide::proto::GetModelsResponse>>
get_models_response) {
void OnModelsFetched(base::Optional<std::unique_ptr<proto::GetModelsResponse>>
get_models_response) {
if (get_models_response)
models_fetched_ = true;
}
......@@ -65,14 +63,13 @@ class PredictionModelFetcherTest : public testing::Test {
}
protected:
bool FetchModels(
const std::vector<optimization_guide::proto::ModelInfo>
models_request_info,
const std::vector<std::string>& hosts,
const optimization_guide::proto::RequestContext& request_context) {
bool FetchModels(const std::vector<proto::ModelInfo> models_request_info,
const std::vector<std::string>& hosts,
const std::vector<proto::FieldTrial>& active_field_trials,
proto::RequestContext request_context) {
bool status =
prediction_model_fetcher_->FetchOptimizationGuideServiceModels(
models_request_info, hosts, request_context,
models_request_info, hosts, active_field_trials, request_context,
base::BindOnce(&PredictionModelFetcherTest::OnModelsFetched,
base::Unretained(this)));
RunUntilIdle();
......@@ -120,10 +117,10 @@ TEST_F(PredictionModelFetcherTest, FetchOptimizationGuideServiceModels) {
base::HistogramTester histogram_tester;
std::string response_content;
std::vector<std::string> hosts = {"foo.com", "bar.com"};
std::vector<optimization_guide::proto::ModelInfo> models_request_info({});
EXPECT_TRUE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
std::vector<proto::ModelInfo> models_request_info({});
std::vector<proto::FieldTrial> active_field_trials({});
EXPECT_TRUE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
VerifyHasPendingFetchRequests();
histogram_tester.ExpectUniqueSample(
......@@ -146,12 +143,14 @@ TEST_F(PredictionModelFetcherTest,
std::string response_content;
std::vector<std::string> hosts;
for (size_t i = 0;
i <= features::MaxHostsForOptimizationGuideServiceModelsFetch() + 1; i++)
i <= features::MaxHostsForOptimizationGuideServiceModelsFetch() + 1;
i++) {
hosts.push_back("host" + base::NumberToString(i) + ".com");
std::vector<optimization_guide::proto::ModelInfo> models_request_info({});
EXPECT_TRUE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
}
std::vector<proto::ModelInfo> models_request_info({});
std::vector<proto::FieldTrial> active_field_trials({});
EXPECT_TRUE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
VerifyHasPendingFetchRequests();
histogram_tester.ExpectUniqueSample(
......@@ -173,10 +172,10 @@ TEST_F(PredictionModelFetcherTest, FetchFilterInvalidHosts) {
std::string response_content;
std::vector<std::string> hosts = {"192.168.1.1", "_abc", "localhost",
"foo.com"};
std::vector<optimization_guide::proto::ModelInfo> models_request_info({});
EXPECT_TRUE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
std::vector<proto::ModelInfo> models_request_info({});
std::vector<proto::FieldTrial> active_field_trials({});
EXPECT_TRUE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
VerifyHasPendingFetchRequests();
histogram_tester.ExpectUniqueSample(
......@@ -199,10 +198,10 @@ TEST_F(PredictionModelFetcherTest, FetchReturned404) {
std::string response_content;
std::vector<std::string> hosts = {"foo.com", "bar.com"};
std::vector<optimization_guide::proto::ModelInfo> models_request_info({});
EXPECT_TRUE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
std::vector<proto::ModelInfo> models_request_info({});
std::vector<proto::FieldTrial> active_field_trials({});
EXPECT_TRUE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
// Send a 404 to HintsFetcher.
SimulateResponse(response_content, net::HTTP_NOT_FOUND);
EXPECT_FALSE(models_fetched());
......@@ -220,10 +219,10 @@ TEST_F(PredictionModelFetcherTest, FetchReturnBadResponse) {
std::string response_content = "not proto";
std::vector<std::string> hosts = {"foo.com", "bar.com"};
std::vector<optimization_guide::proto::ModelInfo> models_request_info({});
EXPECT_TRUE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
std::vector<proto::ModelInfo> models_request_info({});
std::vector<proto::FieldTrial> active_field_trials({});
EXPECT_TRUE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
VerifyHasPendingFetchRequests();
EXPECT_TRUE(SimulateResponse(response_content, net::HTTP_OK));
EXPECT_FALSE(models_fetched());
......@@ -233,16 +232,15 @@ TEST_F(PredictionModelFetcherTest, FetchAttemptWhenNetworkOffline) {
SetConnectionOffline();
std::string response_content;
std::vector<std::string> hosts = {"foo.com", "bar.com"};
std::vector<optimization_guide::proto::ModelInfo> models_request_info({});
EXPECT_FALSE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
std::vector<proto::ModelInfo> models_request_info({});
std::vector<proto::FieldTrial> active_field_trials({});
EXPECT_FALSE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
EXPECT_FALSE(models_fetched());
SetConnectionOnline();
EXPECT_TRUE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
EXPECT_TRUE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
VerifyHasPendingFetchRequests();
EXPECT_TRUE(SimulateResponse(response_content, net::HTTP_OK));
EXPECT_TRUE(models_fetched());
......@@ -252,10 +250,10 @@ TEST_F(PredictionModelFetcherTest, EmptyModelInfoAndHosts) {
base::HistogramTester histogram_tester;
std::string response_content;
std::vector<std::string> hosts = {};
std::vector<optimization_guide::proto::ModelInfo> models_request_info({});
EXPECT_FALSE(FetchModels(
models_request_info, hosts,
optimization_guide::proto::RequestContext::CONTEXT_BATCH_UPDATE));
std::vector<proto::ModelInfo> models_request_info({});
std::vector<proto::FieldTrial> active_field_trials({});
EXPECT_FALSE(FetchModels(models_request_info, hosts, active_field_trials,
proto::RequestContext::CONTEXT_BATCH_UPDATE));
EXPECT_FALSE(models_fetched());
}
......
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