blob: 368550a8cf5c382139a673a63b90a2f326e017eb [file] [log] [blame]
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/path_service.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/optimization_guide/page_content_annotations_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/history/core/browser/history_database.h"
#include "components/history/core/browser/history_db_task.h"
#include "components/history/core/browser/history_service.h"
#include "components/optimization_guide/content/browser/page_content_annotations_service.h"
#include "components/optimization_guide/core/optimization_guide_enums.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/machine_learning_tflite_buildflags.h"
#include "components/optimization_guide/proto/page_topics_model_metadata.pb.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
namespace {
// Fetch and calculate the total number of samples from all the bins for
// |histogram_name|. Note: from some browertests run, there might be two
// profiles created, and this will return the total sample count across
// profiles.
int GetTotalHistogramSamples(const base::HistogramTester& histogram_tester,
const std::string& histogram_name) {
std::vector<base::Bucket> buckets =
histogram_tester.GetAllSamples(histogram_name);
int total = 0;
for (const auto& bucket : buckets)
total += bucket.count;
return total;
}
// Retries fetching |histogram_name| until it contains at least |count| samples.
int RetryForHistogramUntilCountReached(
const base::HistogramTester& histogram_tester,
const std::string& histogram_name,
int count) {
int total = 0;
while (true) {
base::ThreadPoolInstance::Get()->FlushForTesting();
base::RunLoop().RunUntilIdle();
total = GetTotalHistogramSamples(histogram_tester, histogram_name);
if (total >= count)
return total;
}
}
} // namespace
namespace optimization_guide {
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
// A HistoryDBTask that retrieves content annotations.
class GetContentAnnotationsTask : public history::HistoryDBTask {
public:
GetContentAnnotationsTask(
const GURL& url,
base::OnceCallback<void(
const base::Optional<history::VisitContentAnnotations>&)> callback)
: url_(url), callback_(std::move(callback)) {}
~GetContentAnnotationsTask() override = default;
// history::HistoryDBTask:
bool RunOnDBThread(history::HistoryBackend* backend,
history::HistoryDatabase* db) override {
// Get visits for URL.
const history::URLID url_id = db->GetRowForURL(url_, nullptr);
history::VisitVector visits;
if (!db->GetVisitsForURL(url_id, &visits))
return true;
// No visits for URL.
if (visits.empty())
return true;
history::VisitContentAnnotations annotations;
if (db->GetContentAnnotationsForVisit(visits.at(0).visit_id, &annotations))
stored_content_annotations_ = annotations;
return true;
}
void DoneRunOnMainThread() override {
std::move(callback_).Run(stored_content_annotations_);
}
private:
// The URL to get content annotations for.
const GURL url_;
// The callback to invoke when the database call has completed.
base::OnceCallback<void(
const base::Optional<history::VisitContentAnnotations>&)>
callback_;
// The content annotations that were stored for |url_|.
base::Optional<history::VisitContentAnnotations> stored_content_annotations_;
};
class PageContentAnnotationsServiceDisabledBrowserTest
: public InProcessBrowserTest {
public:
PageContentAnnotationsServiceDisabledBrowserTest() {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{},
{features::kOptimizationHints, features::kPageContentAnnotations});
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
#endif
IN_PROC_BROWSER_TEST_F(PageContentAnnotationsServiceDisabledBrowserTest,
KeyedServiceEnabledButFeaturesDisabled) {
EXPECT_EQ(nullptr, PageContentAnnotationsServiceFactory::GetForProfile(
browser()->profile()));
}
class PageContentAnnotationsServiceBrowserTest : public InProcessBrowserTest {
public:
PageContentAnnotationsServiceBrowserTest() {
scoped_feature_list_.InitWithFeaturesAndParameters(
{{features::kOptimizationHints, {}},
{features::kPageContentAnnotations,
{
{"write_to_history_service", "true"},
}}},
/*disabled_features=*/{});
}
~PageContentAnnotationsServiceBrowserTest() override = default;
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
InProcessBrowserTest::SetUpOnMainThread();
embedded_test_server()->ServeFilesFromSourceDirectory(
"chrome/test/data/optimization_guide");
ASSERT_TRUE(embedded_test_server()->Start());
base::HistogramTester histogram_tester;
proto::Any any_metadata;
any_metadata.set_type_url(
"type.googleapis.com/com.foo.PageTopicsModelMetadata");
proto::PageTopicsModelMetadata page_topics_model_metadata;
page_topics_model_metadata.set_version(123);
page_topics_model_metadata.add_supported_output(
proto::PAGE_TOPICS_SUPPORTED_OUTPUT_CATEGORIES);
auto* output_params =
page_topics_model_metadata.mutable_output_postprocessing_params();
auto* category_params = output_params->mutable_category_params();
category_params->set_max_categories(5);
category_params->set_min_none_weight(0.8);
category_params->set_min_category_weight(0.0);
category_params->set_min_normalized_weight_within_top_n(0.1);
output_params->mutable_floc_protected_params()->set_category_name(
"FLOC_PROTECTED");
page_topics_model_metadata.SerializeToString(any_metadata.mutable_value());
base::FilePath source_root_dir;
base::PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir);
base::FilePath model_file_path =
source_root_dir.AppendASCII("components")
.AppendASCII("test")
.AppendASCII("data")
.AppendASCII("optimization_guide")
.AppendASCII("bert_page_topics_model.tflite");
OptimizationGuideKeyedServiceFactory::GetForProfile(browser()->profile())
->OverrideTargetModelFileForTesting(
proto::OPTIMIZATION_TARGET_PAGE_TOPICS, any_metadata,
model_file_path);
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.ModelExecutor.ModelLoadingResult.PageTopics", 1);
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.ModelExecutor.ModelLoadingResult.PageTopics",
ModelExecutorLoadingState::kModelFileValidAndMemoryMapped, 1);
histogram_tester.ExpectTotalCount(
"OptimizationGuide.ModelExecutor.ModelLoadingDuration.PageTopics", 1);
#else
base::RunLoop().RunUntilIdle();
#endif
}
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
base::Optional<history::VisitContentAnnotations> GetContentAnnotationsForURL(
const GURL& url) {
history::HistoryService* history_service =
HistoryServiceFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS);
if (!history_service)
return base::nullopt;
std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
base::Optional<history::VisitContentAnnotations> got_content_annotations;
base::CancelableTaskTracker task_tracker;
history_service->ScheduleDBTask(
FROM_HERE,
std::make_unique<GetContentAnnotationsTask>(
url, base::BindOnce(
[](base::RunLoop* run_loop,
base::Optional<history::VisitContentAnnotations>*
out_content_annotations,
const base::Optional<history::VisitContentAnnotations>&
content_annotations) {
*out_content_annotations = content_annotations;
run_loop->Quit();
},
run_loop.get(), &got_content_annotations)),
&task_tracker);
run_loop->Run();
return got_content_annotations;
}
#endif
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(PageContentAnnotationsServiceBrowserTest,
ModelExecutes) {
base::HistogramTester histogram_tester;
GURL url(embedded_test_server()->GetURL("a.com", "/hello.html"));
ui_test_utils::NavigateToURL(browser(), url);
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
int expected_count = 1;
#else
int expected_count = 0;
#endif
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.PageContentAnnotationsService.ContentAnnotated",
expected_count);
histogram_tester.ExpectTotalCount(
"OptimizationGuide.PageContentAnnotationsService.ContentAnnotated",
expected_count);
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.PageContentAnnotationsService.ContentAnnotated", true,
1);
#endif
PageContentAnnotationsService* service =
PageContentAnnotationsServiceFactory::GetForProfile(browser()->profile());
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
base::Optional<int64_t> model_version = service->GetPageTopicsModelVersion();
EXPECT_TRUE(model_version.has_value());
EXPECT_EQ(123, *model_version);
#else
EXPECT_FALSE(service->GetPageTopicsModelVersion().has_value());
#endif
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.PageContentAnnotationsService."
"ContentAnnotationsStorageStatus",
1);
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.PageContentAnnotationsService."
"ContentAnnotationsStorageStatus",
PageContentAnnotationsStorageStatus::kSuccess, 1);
base::Optional<history::VisitContentAnnotations> got_content_annotations =
GetContentAnnotationsForURL(url);
ASSERT_TRUE(got_content_annotations.has_value());
EXPECT_NE(-1.0,
got_content_annotations->model_annotations.floc_protected_score);
EXPECT_FALSE(got_content_annotations->model_annotations.categories.empty());
EXPECT_EQ(
123,
got_content_annotations->model_annotations.page_topics_model_version);
#endif
}
#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
IN_PROC_BROWSER_TEST_F(PageContentAnnotationsServiceBrowserTest,
NoVisitsForUrlInHistory) {
base::HistogramTester histogram_tester;
PageContentAnnotationsService* service =
PageContentAnnotationsServiceFactory::GetForProfile(browser()->profile());
HistoryVisit history_visit;
history_visit.url = GURL("https://probablynotarealurl.com/");
service->Annotate(history_visit, "sometext");
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.PageContentAnnotationsService.ContentAnnotated", 1);
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.PageContentAnnotationsService.ContentAnnotated", true,
1);
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.PageContentAnnotationsService."
"ContentAnnotationsStorageStatus",
1);
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.PageContentAnnotationsService."
"ContentAnnotationsStorageStatus",
PageContentAnnotationsStorageStatus::kNoVisitsForUrl, 1);
EXPECT_FALSE(GetContentAnnotationsForURL(history_visit.url).has_value());
}
class PageContentAnnotationsServiceNoHistoryTest
: public PageContentAnnotationsServiceBrowserTest {
public:
PageContentAnnotationsServiceNoHistoryTest() {
scoped_feature_list_.InitWithFeaturesAndParameters(
{{features::kOptimizationHints, {}},
{features::kPageContentAnnotations,
{
{"write_to_history_service", "false"},
}}},
/*disabled_features=*/{});
}
~PageContentAnnotationsServiceNoHistoryTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(PageContentAnnotationsServiceNoHistoryTest,
ModelExecutesButDoesntWriteToHistory) {
base::HistogramTester histogram_tester;
GURL url(embedded_test_server()->GetURL("a.com", "/hello-no-history.html"));
ui_test_utils::NavigateToURL(browser(), url);
RetryForHistogramUntilCountReached(
histogram_tester,
"OptimizationGuide.PageContentAnnotationsService.ContentAnnotated", 1);
histogram_tester.ExpectUniqueSample(
"OptimizationGuide.PageContentAnnotationsService.ContentAnnotated", true,
1);
base::RunLoop().RunUntilIdle();
histogram_tester.ExpectTotalCount(
"OptimizationGuide.PageContentAnnotationsService."
"ContentAnnotationsStorageStatus",
0);
EXPECT_FALSE(GetContentAnnotationsForURL(url).has_value());
}
#endif
} // namespace optimization_guide