browsing_topics_service_impl_unittest.cc 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/browsing_topics/browsing_topics_service_impl.h"
  5. #include "base/files/scoped_temp_dir.h"
  6. #include "base/json/json_file_value_serializer.h"
  7. #include "base/json/values_util.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/test/bind.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "base/test/test_future.h"
  12. #include "base/time/time.h"
  13. #include "components/browsing_topics/test_util.h"
  14. #include "components/browsing_topics/util.h"
  15. #include "components/content_settings/core/browser/cookie_settings.h"
  16. #include "components/content_settings/core/browser/host_content_settings_map.h"
  17. #include "components/history/core/browser/history_database_params.h"
  18. #include "components/history/core/browser/history_service.h"
  19. #include "components/history/core/test/test_history_database.h"
  20. #include "components/optimization_guide/content/browser/page_content_annotations_service.h"
  21. #include "components/optimization_guide/content/browser/test_page_content_annotator.h"
  22. #include "components/optimization_guide/core/test_model_info_builder.h"
  23. #include "components/optimization_guide/core/test_optimization_guide_model_provider.h"
  24. #include "components/privacy_sandbox/privacy_sandbox_prefs.h"
  25. #include "components/privacy_sandbox/privacy_sandbox_settings.h"
  26. #include "components/privacy_sandbox/privacy_sandbox_test_util.h"
  27. #include "components/sync_preferences/testing_pref_service_syncable.h"
  28. #include "components/ukm/test_ukm_recorder.h"
  29. #include "content/public/browser/browsing_topics_site_data_manager.h"
  30. #include "content/public/browser/navigation_entry.h"
  31. #include "content/public/test/browsing_topics_test_util.h"
  32. #include "content/public/test/navigation_simulator.h"
  33. #include "content/public/test/test_renderer_host.h"
  34. #include "content/public/test/test_utils.h"
  35. #include "content/public/test/web_contents_tester.h"
  36. #include "content/test/test_render_view_host.h"
  37. #include "services/metrics/public/cpp/ukm_builders.h"
  38. #include "third_party/blink/public/mojom/browsing_topics/browsing_topics.mojom.h"
  39. namespace browsing_topics {
  40. namespace {
  41. constexpr base::TimeDelta kCalculatorDelay = base::Seconds(10);
  42. constexpr browsing_topics::HmacKey kTestKey = {1};
  43. constexpr base::Time kTime1 =
  44. base::Time::FromDeltaSinceWindowsEpoch(base::Days(1));
  45. constexpr base::Time kTime2 =
  46. base::Time::FromDeltaSinceWindowsEpoch(base::Days(2));
  47. constexpr size_t kTaxonomySize = 349;
  48. constexpr int kTaxonomyVersion = 1;
  49. constexpr int64_t kModelVersion = 5000000000LL;
  50. EpochTopics CreateTestEpochTopics(
  51. const std::vector<std::pair<Topic, std::set<HashedDomain>>>& topics,
  52. base::Time calculation_time,
  53. size_t padded_top_topics_start_index = 5) {
  54. DCHECK_EQ(topics.size(), 5u);
  55. std::vector<TopicAndDomains> top_topics_and_observing_domains;
  56. for (size_t i = 0; i < 5; ++i) {
  57. top_topics_and_observing_domains.emplace_back(topics[i].first,
  58. topics[i].second);
  59. }
  60. return EpochTopics(std::move(top_topics_and_observing_domains),
  61. padded_top_topics_start_index, kTaxonomySize,
  62. kTaxonomyVersion, kModelVersion, calculation_time);
  63. }
  64. } // namespace
  65. // A tester class that allows mocking the topics calculators (i.e. the result
  66. // and the finish delay).
  67. class TesterBrowsingTopicsService : public BrowsingTopicsServiceImpl {
  68. public:
  69. TesterBrowsingTopicsService(
  70. const base::FilePath& profile_path,
  71. privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
  72. history::HistoryService* history_service,
  73. content::BrowsingTopicsSiteDataManager* site_data_manager,
  74. optimization_guide::PageContentAnnotationsService* annotations_service,
  75. base::queue<EpochTopics> mock_calculator_results,
  76. base::TimeDelta calculator_finish_delay)
  77. : BrowsingTopicsServiceImpl(profile_path,
  78. privacy_sandbox_settings,
  79. history_service,
  80. site_data_manager,
  81. annotations_service),
  82. mock_calculator_results_(std::move(mock_calculator_results)),
  83. calculator_finish_delay_(calculator_finish_delay) {}
  84. ~TesterBrowsingTopicsService() override = default;
  85. TesterBrowsingTopicsService(const TesterBrowsingTopicsService&) = delete;
  86. TesterBrowsingTopicsService& operator=(const TesterBrowsingTopicsService&) =
  87. delete;
  88. TesterBrowsingTopicsService(TesterBrowsingTopicsService&&) = delete;
  89. TesterBrowsingTopicsService& operator=(TesterBrowsingTopicsService&&) =
  90. delete;
  91. std::unique_ptr<BrowsingTopicsCalculator> CreateCalculator(
  92. privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
  93. history::HistoryService* history_service,
  94. content::BrowsingTopicsSiteDataManager* site_data_manager,
  95. optimization_guide::PageContentAnnotationsService* annotations_service,
  96. const base::circular_deque<EpochTopics>& epochs,
  97. BrowsingTopicsCalculator::CalculateCompletedCallback callback) override {
  98. DCHECK(!mock_calculator_results_.empty());
  99. ++started_calculations_count_;
  100. EpochTopics next_epoch = std::move(mock_calculator_results_.front());
  101. mock_calculator_results_.pop();
  102. return std::make_unique<TesterBrowsingTopicsCalculator>(
  103. privacy_sandbox_settings, history_service, site_data_manager,
  104. annotations_service, std::move(callback), std::move(next_epoch),
  105. calculator_finish_delay_);
  106. }
  107. const BrowsingTopicsState& browsing_topics_state() override {
  108. return BrowsingTopicsServiceImpl::browsing_topics_state();
  109. }
  110. void OnTopicsDataAccessibleSinceUpdated() override {
  111. BrowsingTopicsServiceImpl::OnTopicsDataAccessibleSinceUpdated();
  112. }
  113. void OnURLsDeleted(history::HistoryService* history_service,
  114. const history::DeletionInfo& deletion_info) override {
  115. BrowsingTopicsServiceImpl::OnURLsDeleted(history_service, deletion_info);
  116. }
  117. // The number of calculations that have started, including those that have
  118. // finished, those that are ongoing, and those that have been canceled.
  119. size_t started_calculations_count() const {
  120. return started_calculations_count_;
  121. }
  122. private:
  123. base::queue<EpochTopics> mock_calculator_results_;
  124. base::TimeDelta calculator_finish_delay_;
  125. size_t started_calculations_count_ = 0u;
  126. };
  127. class BrowsingTopicsServiceImplTest
  128. : public content::RenderViewHostTestHarness {
  129. public:
  130. BrowsingTopicsServiceImplTest()
  131. : content::RenderViewHostTestHarness(
  132. base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
  133. scoped_feature_list_.InitWithFeatures(
  134. /*enabled_features=*/{blink::features::kBrowsingTopics},
  135. /*disabled_features=*/{});
  136. OverrideHmacKeyForTesting(kTestKey);
  137. EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
  138. content_settings::CookieSettings::RegisterProfilePrefs(prefs_.registry());
  139. HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry());
  140. privacy_sandbox::RegisterProfilePrefs(prefs_.registry());
  141. host_content_settings_map_ = base::MakeRefCounted<HostContentSettingsMap>(
  142. &prefs_, /*is_off_the_record=*/false, /*store_last_modified=*/false,
  143. /*restore_session=*/false);
  144. cookie_settings_ = base::MakeRefCounted<content_settings::CookieSettings>(
  145. host_content_settings_map_.get(), &prefs_, false, "chrome-extension");
  146. auto privacy_sandbox_delegate = std::make_unique<
  147. privacy_sandbox_test_util::MockPrivacySandboxSettingsDelegate>();
  148. privacy_sandbox_delegate->SetUpDefaultResponse(/*restricted=*/false);
  149. privacy_sandbox_settings_ =
  150. std::make_unique<privacy_sandbox::PrivacySandboxSettings>(
  151. std::move(privacy_sandbox_delegate),
  152. host_content_settings_map_.get(), cookie_settings_, &prefs_,
  153. /*incognito_profile=*/false);
  154. history_service_ = std::make_unique<history::HistoryService>();
  155. history_service_->Init(
  156. history::TestHistoryDatabaseParamsForPath(temp_dir_.GetPath()));
  157. optimization_guide_model_provider_ = std::make_unique<
  158. optimization_guide::TestOptimizationGuideModelProvider>();
  159. page_content_annotations_service_ =
  160. std::make_unique<optimization_guide::PageContentAnnotationsService>(
  161. "en-US", optimization_guide_model_provider_.get(),
  162. history_service_.get(), nullptr, base::FilePath(), nullptr,
  163. nullptr);
  164. page_content_annotations_service_->OverridePageContentAnnotatorForTesting(
  165. &test_page_content_annotator_);
  166. task_environment()->RunUntilIdle();
  167. }
  168. ~BrowsingTopicsServiceImplTest() override = default;
  169. void TearDown() override {
  170. DCHECK(history_service_);
  171. browsing_topics_service_.reset();
  172. base::RunLoop run_loop;
  173. history_service_->SetOnBackendDestroyTask(run_loop.QuitClosure());
  174. history_service_->Shutdown();
  175. run_loop.Run();
  176. page_content_annotations_service_.reset();
  177. optimization_guide_model_provider_.reset();
  178. task_environment()->RunUntilIdle();
  179. host_content_settings_map_->ShutdownOnUIThread();
  180. content::RenderViewHostTestHarness::TearDown();
  181. }
  182. void NavigateToPage(const GURL& url) {
  183. auto simulator = content::NavigationSimulator::CreateBrowserInitiated(
  184. url, web_contents());
  185. simulator->SetTransition(ui::PageTransition::PAGE_TRANSITION_TYPED);
  186. simulator->Commit();
  187. }
  188. content::BrowsingTopicsSiteDataManager* topics_site_data_manager() {
  189. return web_contents()
  190. ->GetPrimaryMainFrame()
  191. ->GetProcess()
  192. ->GetStoragePartition()
  193. ->GetBrowsingTopicsSiteDataManager();
  194. }
  195. base::FilePath BrowsingTopicsStateFilePath() {
  196. return temp_dir_.GetPath().Append(FILE_PATH_LITERAL("BrowsingTopicsState"));
  197. }
  198. void CreateBrowsingTopicsStateFile(
  199. const std::vector<EpochTopics>& epochs,
  200. base::Time next_scheduled_calculation_time) {
  201. base::Value::List epochs_list;
  202. for (const EpochTopics& epoch : epochs) {
  203. epochs_list.Append(epoch.ToDictValue());
  204. }
  205. base::Value::Dict dict;
  206. dict.Set("epochs", std::move(epochs_list));
  207. dict.Set("next_scheduled_calculation_time",
  208. base::TimeToValue(next_scheduled_calculation_time));
  209. dict.Set("hex_encoded_hmac_key", base::HexEncode(kTestKey));
  210. dict.Set("config_version", 1);
  211. JSONFileValueSerializer(BrowsingTopicsStateFilePath()).Serialize(dict);
  212. }
  213. void InitializeBrowsingTopicsService(
  214. base::queue<EpochTopics> mock_calculator_results) {
  215. browsing_topics_service_ = std::make_unique<TesterBrowsingTopicsService>(
  216. temp_dir_.GetPath(), privacy_sandbox_settings_.get(),
  217. history_service_.get(), topics_site_data_manager(),
  218. page_content_annotations_service_.get(),
  219. std::move(mock_calculator_results), kCalculatorDelay);
  220. }
  221. const BrowsingTopicsState& browsing_topics_state() {
  222. DCHECK(browsing_topics_service_);
  223. return browsing_topics_service_->browsing_topics_state();
  224. }
  225. HashedDomain GetHashedDomain(const std::string& domain) {
  226. return HashContextDomainForStorage(kTestKey, domain);
  227. }
  228. protected:
  229. base::test::ScopedFeatureList scoped_feature_list_;
  230. base::ScopedTempDir temp_dir_;
  231. sync_preferences::TestingPrefServiceSyncable prefs_;
  232. scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
  233. scoped_refptr<content_settings::CookieSettings> cookie_settings_;
  234. std::unique_ptr<privacy_sandbox::PrivacySandboxSettings>
  235. privacy_sandbox_settings_;
  236. std::unique_ptr<history::HistoryService> history_service_;
  237. std::unique_ptr<optimization_guide::TestOptimizationGuideModelProvider>
  238. optimization_guide_model_provider_;
  239. std::unique_ptr<optimization_guide::PageContentAnnotationsService>
  240. page_content_annotations_service_;
  241. optimization_guide::TestPageContentAnnotator test_page_content_annotator_;
  242. std::unique_ptr<TesterBrowsingTopicsService> browsing_topics_service_;
  243. };
  244. TEST_F(BrowsingTopicsServiceImplTest, EmptyInitialState_CalculationScheduling) {
  245. base::Time start_time = base::Time::Now();
  246. base::queue<EpochTopics> mock_calculator_results;
  247. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  248. {Topic(2), {}},
  249. {Topic(3), {}},
  250. {Topic(4), {}},
  251. {Topic(5), {}}},
  252. kTime1));
  253. mock_calculator_results.push(CreateTestEpochTopics({{Topic(6), {}},
  254. {Topic(7), {}},
  255. {Topic(8), {}},
  256. {Topic(9), {}},
  257. {Topic(10), {}}},
  258. kTime2));
  259. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  260. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 0u);
  261. // Finish file loading.
  262. task_environment()->RunUntilIdle();
  263. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 1u);
  264. EXPECT_TRUE(browsing_topics_state().epochs().empty());
  265. // Finish the calculation.
  266. task_environment()->FastForwardBy(kCalculatorDelay);
  267. EXPECT_EQ(browsing_topics_state().epochs().size(), 1u);
  268. EXPECT_EQ(browsing_topics_state().epochs()[0].calculation_time(), kTime1);
  269. EXPECT_EQ(browsing_topics_state().next_scheduled_calculation_time(),
  270. start_time + kCalculatorDelay + base::Days(7));
  271. // Advance the time to right before the next scheduled calculation. The next
  272. // calculation should not happen.
  273. task_environment()->FastForwardBy(base::Days(7) - base::Seconds(1));
  274. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 1u);
  275. // Advance the time to the scheduled calculation time. A calculation should
  276. // happen.
  277. task_environment()->FastForwardBy(base::Seconds(1));
  278. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 2u);
  279. // Finish the calculation.
  280. task_environment()->FastForwardBy(kCalculatorDelay);
  281. EXPECT_EQ(browsing_topics_state().epochs().size(), 2u);
  282. EXPECT_EQ(browsing_topics_state().epochs()[1].calculation_time(), kTime2);
  283. EXPECT_EQ(browsing_topics_state().next_scheduled_calculation_time(),
  284. start_time + 2 * kCalculatorDelay + 2 * base::Days(7));
  285. }
  286. TEST_F(BrowsingTopicsServiceImplTest,
  287. StartFromPreexistingState_CalculateAtScheduledTime) {
  288. base::Time start_time = base::Time::Now();
  289. std::vector<EpochTopics> preexisting_epochs;
  290. preexisting_epochs.push_back(CreateTestEpochTopics({{Topic(1), {}},
  291. {Topic(2), {}},
  292. {Topic(3), {}},
  293. {Topic(4), {}},
  294. {Topic(5), {}}},
  295. kTime1));
  296. CreateBrowsingTopicsStateFile(
  297. std::move(preexisting_epochs),
  298. /*next_scheduled_calculation_time=*/start_time + base::Days(1));
  299. base::queue<EpochTopics> mock_calculator_results;
  300. mock_calculator_results.push(CreateTestEpochTopics({{Topic(6), {}},
  301. {Topic(7), {}},
  302. {Topic(8), {}},
  303. {Topic(9), {}},
  304. {Topic(10), {}}},
  305. kTime2));
  306. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  307. // Finish file loading.
  308. task_environment()->RunUntilIdle();
  309. EXPECT_EQ(browsing_topics_state().epochs().size(), 1u);
  310. EXPECT_EQ(browsing_topics_state().epochs()[0].calculation_time(), kTime1);
  311. EXPECT_EQ(browsing_topics_state().next_scheduled_calculation_time(),
  312. start_time + base::Days(1));
  313. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 0u);
  314. // Advance the time to the scheduled calculation time. A calculation should
  315. // happen.
  316. task_environment()->FastForwardBy(base::Days(1));
  317. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 1u);
  318. // Finish the calculation.
  319. task_environment()->FastForwardBy(kCalculatorDelay);
  320. EXPECT_EQ(browsing_topics_state().epochs().size(), 2u);
  321. EXPECT_EQ(browsing_topics_state().epochs()[1].calculation_time(), kTime2);
  322. EXPECT_EQ(browsing_topics_state().next_scheduled_calculation_time(),
  323. start_time + base::Days(1) + kCalculatorDelay + base::Days(7));
  324. }
  325. TEST_F(
  326. BrowsingTopicsServiceImplTest,
  327. StartFromPreexistingState_ScheduledTimeReachedBeforeStartup_CalculateImmediately) {
  328. base::Time start_time = base::Time::Now();
  329. std::vector<EpochTopics> preexisting_epochs;
  330. preexisting_epochs.push_back(CreateTestEpochTopics({{Topic(1), {}},
  331. {Topic(2), {}},
  332. {Topic(3), {}},
  333. {Topic(4), {}},
  334. {Topic(5), {}}},
  335. kTime1));
  336. CreateBrowsingTopicsStateFile(
  337. std::move(preexisting_epochs),
  338. /*next_scheduled_calculation_time=*/start_time - base::Seconds(1));
  339. base::queue<EpochTopics> mock_calculator_results;
  340. mock_calculator_results.push(CreateTestEpochTopics({{Topic(6), {}},
  341. {Topic(7), {}},
  342. {Topic(8), {}},
  343. {Topic(9), {}},
  344. {Topic(10), {}}},
  345. kTime2));
  346. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  347. // Finish file loading.
  348. task_environment()->RunUntilIdle();
  349. EXPECT_EQ(browsing_topics_state().epochs().size(), 1u);
  350. EXPECT_EQ(browsing_topics_state().epochs()[0].calculation_time(), kTime1);
  351. EXPECT_EQ(browsing_topics_state().next_scheduled_calculation_time(),
  352. start_time - base::Seconds(1));
  353. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 1u);
  354. }
  355. TEST_F(
  356. BrowsingTopicsServiceImplTest,
  357. StartFromPreexistingState_TopicsAccessibleSinceUpdated_ResetStateAndStorage_CalculateAtScheduledTime) {
  358. base::Time start_time = base::Time::Now();
  359. std::vector<EpochTopics> preexisting_epochs;
  360. preexisting_epochs.push_back(
  361. CreateTestEpochTopics({{Topic(1), {}},
  362. {Topic(2), {}},
  363. {Topic(3), {}},
  364. {Topic(4), {}},
  365. {Topic(5), {}}},
  366. start_time - base::Days(1)));
  367. // Add some arbitrary data to site data storage. The intent is just to test
  368. // data deletion.
  369. topics_site_data_manager()->OnBrowsingTopicsApiUsed(
  370. HashMainFrameHostForStorage("a.com"), {HashedDomain(1)},
  371. base::Time::Now());
  372. task_environment()->FastForwardBy(base::Seconds(1));
  373. privacy_sandbox_settings_->OnCookiesCleared();
  374. EXPECT_EQ(
  375. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).size(),
  376. 1u);
  377. CreateBrowsingTopicsStateFile(
  378. std::move(preexisting_epochs),
  379. /*next_scheduled_calculation_time=*/start_time + base::Days(1));
  380. base::queue<EpochTopics> mock_calculator_results;
  381. mock_calculator_results.push(
  382. CreateTestEpochTopics({{Topic(6), {}},
  383. {Topic(7), {}},
  384. {Topic(8), {}},
  385. {Topic(9), {}},
  386. {Topic(10), {}}},
  387. start_time - base::Days(1)));
  388. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  389. // Finish file loading.
  390. task_environment()->RunUntilIdle();
  391. EXPECT_EQ(browsing_topics_state().epochs().size(), 0u);
  392. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 0u);
  393. EXPECT_EQ(
  394. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).size(),
  395. 0u);
  396. }
  397. TEST_F(
  398. BrowsingTopicsServiceImplTest,
  399. StartFromPreexistingState_UnexpectedNextCalculationDelay_ResetState_CalculateImmediately) {
  400. base::Time start_time = base::Time::Now();
  401. std::vector<EpochTopics> preexisting_epochs;
  402. preexisting_epochs.push_back(
  403. CreateTestEpochTopics({{Topic(1), {}},
  404. {Topic(2), {}},
  405. {Topic(3), {}},
  406. {Topic(4), {}},
  407. {Topic(5), {}}},
  408. start_time - base::Days(1)));
  409. privacy_sandbox_settings_->OnCookiesCleared();
  410. CreateBrowsingTopicsStateFile(
  411. std::move(preexisting_epochs),
  412. /*next_scheduled_calculation_time=*/start_time + base::Days(15));
  413. base::queue<EpochTopics> mock_calculator_results;
  414. mock_calculator_results.push(CreateTestEpochTopics({{Topic(6), {}},
  415. {Topic(7), {}},
  416. {Topic(8), {}},
  417. {Topic(9), {}},
  418. {Topic(10), {}}},
  419. kTime2));
  420. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  421. // Finish file loading.
  422. task_environment()->RunUntilIdle();
  423. EXPECT_EQ(browsing_topics_state().epochs().size(), 0u);
  424. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 1u);
  425. }
  426. TEST_F(BrowsingTopicsServiceImplTest,
  427. StartFromPreexistingState_DefaultHandlingBeforeLoadFinish) {
  428. ukm::TestAutoSetUkmRecorder ukm_recorder;
  429. base::Time start_time = base::Time::Now();
  430. std::vector<EpochTopics> preexisting_epochs;
  431. preexisting_epochs.push_back(
  432. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  433. {Topic(2), {GetHashedDomain("bar.com")}},
  434. {Topic(3), {GetHashedDomain("bar.com")}},
  435. {Topic(4), {GetHashedDomain("bar.com")}},
  436. {Topic(5), {GetHashedDomain("bar.com")}}},
  437. kTime1));
  438. CreateBrowsingTopicsStateFile(
  439. std::move(preexisting_epochs),
  440. /*next_scheduled_calculation_time=*/start_time + base::Days(1));
  441. base::queue<EpochTopics> mock_calculator_results;
  442. mock_calculator_results.push(CreateTestEpochTopics({{Topic(6), {}},
  443. {Topic(7), {}},
  444. {Topic(8), {}},
  445. {Topic(9), {}},
  446. {Topic(10), {}}},
  447. kTime2));
  448. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  449. NavigateToPage(GURL("https://www.foo.com"));
  450. EXPECT_TRUE(browsing_topics_service_
  451. ->GetBrowsingTopicsForJsApi(
  452. /*context_origin=*/url::Origin::Create(
  453. GURL("https://www.bar.com")),
  454. web_contents()->GetPrimaryMainFrame())
  455. .empty());
  456. auto entries = ukm_recorder.GetEntriesByName(
  457. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  458. kEntryName);
  459. EXPECT_EQ(1u, entries.size());
  460. ukm_recorder.ExpectEntryMetric(
  461. entries.back(),
  462. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  463. kEmptyReasonName,
  464. 0 /* kStateNotReady */);
  465. EXPECT_TRUE(browsing_topics_service_
  466. ->GetTopicsForSiteForDisplay(
  467. url::Origin::Create(GURL("https://www.bar.com")))
  468. .empty());
  469. EXPECT_TRUE(browsing_topics_service_->GetTopTopicsForDisplay().empty());
  470. base::test::TestFuture<mojom::WebUIGetBrowsingTopicsStateResultPtr> future1;
  471. browsing_topics_service_->GetBrowsingTopicsStateForWebUi(
  472. /*calculate_now=*/false, future1.GetCallback());
  473. EXPECT_TRUE(future1.IsReady());
  474. EXPECT_EQ(future1.Take()->get_override_status_message(),
  475. "State loading hasn't finished. Please retry shortly.");
  476. // Finish file loading.
  477. task_environment()->RunUntilIdle();
  478. EXPECT_FALSE(browsing_topics_service_
  479. ->GetBrowsingTopicsForJsApi(
  480. /*context_origin=*/url::Origin::Create(
  481. GURL("https://www.bar.com")),
  482. web_contents()->GetPrimaryMainFrame())
  483. .empty());
  484. EXPECT_FALSE(browsing_topics_service_
  485. ->GetTopicsForSiteForDisplay(
  486. url::Origin::Create(GURL("https://www.bar.com")))
  487. .empty());
  488. EXPECT_FALSE(browsing_topics_service_->GetTopTopicsForDisplay().empty());
  489. base::test::TestFuture<mojom::WebUIGetBrowsingTopicsStateResultPtr> future2;
  490. browsing_topics_service_->GetBrowsingTopicsStateForWebUi(
  491. /*calculate_now=*/false, future2.GetCallback());
  492. EXPECT_TRUE(future2.IsReady());
  493. EXPECT_FALSE(future2.Take()->is_override_status_message());
  494. }
  495. TEST_F(
  496. BrowsingTopicsServiceImplTest,
  497. OnTopicsDataAccessibleSinceUpdated_ResetState_ClearTopicsSiteDataStorage) {
  498. base::Time start_time = base::Time::Now();
  499. base::queue<EpochTopics> mock_calculator_results;
  500. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  501. {Topic(2), {}},
  502. {Topic(3), {}},
  503. {Topic(4), {}},
  504. {Topic(5), {}}},
  505. start_time));
  506. mock_calculator_results.push(
  507. CreateTestEpochTopics({{Topic(6), {}},
  508. {Topic(7), {}},
  509. {Topic(8), {}},
  510. {Topic(9), {}},
  511. {Topic(10), {}}},
  512. start_time + kCalculatorDelay + base::Days(7)));
  513. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  514. // Finish file loading and two calculations.
  515. task_environment()->FastForwardBy(2 * kCalculatorDelay + base::Days(7));
  516. // Add some arbitrary data to site data storage. The intent is just to test
  517. // data deletion.
  518. topics_site_data_manager()->OnBrowsingTopicsApiUsed(
  519. HashMainFrameHostForStorage("a.com"), {HashedDomain(1)},
  520. base::Time::Now());
  521. EXPECT_EQ(browsing_topics_state().epochs().size(), 2u);
  522. EXPECT_EQ(
  523. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).size(),
  524. 1u);
  525. task_environment()->FastForwardBy(base::Seconds(1));
  526. privacy_sandbox_settings_->OnCookiesCleared();
  527. EXPECT_EQ(browsing_topics_state().epochs().size(), 0u);
  528. EXPECT_EQ(
  529. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).size(),
  530. 0u);
  531. }
  532. TEST_F(BrowsingTopicsServiceImplTest,
  533. OnURLsDeleted_TimeRangeOverlapWithOneEpoch) {
  534. base::Time start_time = base::Time::Now();
  535. base::queue<EpochTopics> mock_calculator_results;
  536. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  537. {Topic(2), {}},
  538. {Topic(3), {}},
  539. {Topic(4), {}},
  540. {Topic(5), {}}},
  541. start_time));
  542. mock_calculator_results.push(
  543. CreateTestEpochTopics({{Topic(6), {}},
  544. {Topic(7), {}},
  545. {Topic(8), {}},
  546. {Topic(9), {}},
  547. {Topic(10), {}}},
  548. start_time + kCalculatorDelay + base::Days(7)));
  549. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  550. // Finish file loading and two calculations.
  551. task_environment()->FastForwardBy(2 * kCalculatorDelay + base::Days(7));
  552. EXPECT_EQ(browsing_topics_state().epochs().size(), 2u);
  553. EXPECT_FALSE(browsing_topics_state().epochs()[0].empty());
  554. EXPECT_FALSE(browsing_topics_state().epochs()[1].empty());
  555. history::DeletionInfo deletion_info(
  556. history::DeletionTimeRange(start_time + base::Days(5),
  557. start_time + base::Days(6)),
  558. /*is_from_expiration=*/false, /*deleted_rows=*/{}, /*favicon_urls=*/{},
  559. /*restrict_urls=*/absl::nullopt);
  560. browsing_topics_service_->OnURLsDeleted(history_service_.get(),
  561. deletion_info);
  562. EXPECT_EQ(browsing_topics_state().epochs().size(), 2u);
  563. EXPECT_FALSE(browsing_topics_state().epochs()[0].empty());
  564. EXPECT_TRUE(browsing_topics_state().epochs()[1].empty());
  565. }
  566. TEST_F(BrowsingTopicsServiceImplTest,
  567. OnURLsDeleted_TimeRangeOverlapWithAllEpochs) {
  568. base::Time start_time = base::Time::Now();
  569. base::queue<EpochTopics> mock_calculator_results;
  570. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  571. {Topic(2), {}},
  572. {Topic(3), {}},
  573. {Topic(4), {}},
  574. {Topic(5), {}}},
  575. start_time));
  576. mock_calculator_results.push(
  577. CreateTestEpochTopics({{Topic(6), {}},
  578. {Topic(7), {}},
  579. {Topic(8), {}},
  580. {Topic(9), {}},
  581. {Topic(10), {}}},
  582. start_time + kCalculatorDelay + base::Days(7)));
  583. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  584. // Finish file loading and two calculations.
  585. task_environment()->FastForwardBy(2 * kCalculatorDelay + base::Days(7));
  586. EXPECT_EQ(browsing_topics_state().epochs().size(), 2u);
  587. EXPECT_FALSE(browsing_topics_state().epochs()[0].empty());
  588. EXPECT_FALSE(browsing_topics_state().epochs()[1].empty());
  589. history::DeletionInfo deletion_info(
  590. history::DeletionTimeRange(start_time, start_time + base::Days(2)),
  591. /*is_from_expiration=*/false, /*deleted_rows=*/{}, /*favicon_urls=*/{},
  592. /*restrict_urls=*/absl::nullopt);
  593. browsing_topics_service_->OnURLsDeleted(history_service_.get(),
  594. deletion_info);
  595. EXPECT_EQ(browsing_topics_state().epochs().size(), 2u);
  596. EXPECT_TRUE(browsing_topics_state().epochs()[0].empty());
  597. EXPECT_TRUE(browsing_topics_state().epochs()[1].empty());
  598. }
  599. TEST_F(BrowsingTopicsServiceImplTest, Recalculate) {
  600. base::Time start_time = base::Time::Now();
  601. base::queue<EpochTopics> mock_calculator_results;
  602. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  603. {Topic(2), {}},
  604. {Topic(3), {}},
  605. {Topic(4), {}},
  606. {Topic(5), {}}},
  607. kTime1));
  608. mock_calculator_results.push(CreateTestEpochTopics({{Topic(6), {}},
  609. {Topic(7), {}},
  610. {Topic(8), {}},
  611. {Topic(9), {}},
  612. {Topic(10), {}}},
  613. kTime2));
  614. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  615. task_environment()->FastForwardBy(kCalculatorDelay - base::Seconds(1));
  616. EXPECT_EQ(browsing_topics_state().epochs().size(), 0u);
  617. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 1u);
  618. // History deletion during a calculation should trigger the re-calculation.
  619. history::DeletionInfo deletion_info(
  620. history::DeletionTimeRange(start_time, start_time + base::Days(2)),
  621. /*is_from_expiration=*/false, /*deleted_rows=*/{}, /*favicon_urls=*/{},
  622. /*restrict_urls=*/absl::nullopt);
  623. browsing_topics_service_->OnURLsDeleted(history_service_.get(),
  624. deletion_info);
  625. // The calculation shouldn't finish at the originally expected time, as it was
  626. // dropped and a new calculation has started.
  627. task_environment()->FastForwardBy(base::Seconds(1));
  628. EXPECT_EQ(browsing_topics_state().epochs().size(), 0u);
  629. EXPECT_EQ(browsing_topics_service_->started_calculations_count(), 2u);
  630. // Finish the re-started calculation.
  631. task_environment()->FastForwardBy(kCalculatorDelay - base::Seconds(1));
  632. EXPECT_EQ(browsing_topics_state().epochs().size(), 1u);
  633. // Expect that the result comes from the re-started calculator.
  634. EXPECT_EQ(browsing_topics_state().epochs()[0].calculation_time(), kTime2);
  635. EXPECT_EQ(browsing_topics_state().next_scheduled_calculation_time(),
  636. base::Time::Now() + base::Days(7));
  637. }
  638. TEST_F(BrowsingTopicsServiceImplTest,
  639. GetBrowsingTopicsForJsApi_PrivacySandboxSettingsDisabled) {
  640. ukm::TestAutoSetUkmRecorder ukm_recorder;
  641. base::queue<EpochTopics> mock_calculator_results;
  642. mock_calculator_results.push(
  643. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  644. {Topic(2), {GetHashedDomain("bar.com")}},
  645. {Topic(3), {GetHashedDomain("bar.com")}},
  646. {Topic(4), {GetHashedDomain("bar.com")}},
  647. {Topic(5), {GetHashedDomain("bar.com")}}},
  648. kTime1));
  649. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  650. task_environment()->RunUntilIdle();
  651. privacy_sandbox_settings_->SetPrivacySandboxEnabled(false);
  652. NavigateToPage(GURL("https://www.foo.com"));
  653. EXPECT_TRUE(browsing_topics_service_
  654. ->GetBrowsingTopicsForJsApi(
  655. /*context_origin=*/url::Origin::Create(
  656. GURL("https://www.bar.com")),
  657. web_contents()->GetPrimaryMainFrame())
  658. .empty());
  659. auto entries = ukm_recorder.GetEntriesByName(
  660. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  661. kEntryName);
  662. EXPECT_EQ(1u, entries.size());
  663. ukm_recorder.ExpectEntryMetric(
  664. entries.back(),
  665. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  666. kEmptyReasonName,
  667. 1 /* kAccessDisallowedBySettings */);
  668. }
  669. TEST_F(BrowsingTopicsServiceImplTest, GetBrowsingTopicsForJsApi_OneEpoch) {
  670. ukm::TestAutoSetUkmRecorder ukm_recorder;
  671. base::queue<EpochTopics> mock_calculator_results;
  672. mock_calculator_results.push(
  673. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  674. {Topic(2), {GetHashedDomain("bar.com")}},
  675. {Topic(3), {GetHashedDomain("bar.com")}},
  676. {Topic(4), {GetHashedDomain("bar.com")}},
  677. {Topic(5), {GetHashedDomain("bar.com")}}},
  678. kTime1));
  679. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  680. task_environment()->FastForwardBy(kCalculatorDelay);
  681. NavigateToPage(GURL("https://www.foo.com"));
  682. // Current time is before the epoch switch time.
  683. std::vector<blink::mojom::EpochTopicPtr> result =
  684. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  685. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  686. web_contents()->GetPrimaryMainFrame());
  687. EXPECT_TRUE(result.empty());
  688. auto entries = ukm_recorder.GetEntriesByName(
  689. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  690. kEntryName);
  691. EXPECT_EQ(1u, entries.size());
  692. ukm_recorder.ExpectEntryMetric(
  693. entries.back(),
  694. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  695. kEmptyReasonName,
  696. 2 /* kNoCandicateTopics */);
  697. // Advance to the time after the epoch switch time.
  698. task_environment()->AdvanceClock(base::Days(7) - base::Seconds(1));
  699. result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
  700. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  701. web_contents()->GetPrimaryMainFrame());
  702. EXPECT_EQ(result.size(), 1u);
  703. EXPECT_EQ(result[0]->topic, 2);
  704. EXPECT_EQ(result[0]->config_version, "chrome.1");
  705. EXPECT_EQ(result[0]->taxonomy_version, "1");
  706. EXPECT_EQ(result[0]->model_version, "5000000000");
  707. EXPECT_EQ(result[0]->version, "chrome.1:1:5000000000");
  708. }
  709. TEST_F(BrowsingTopicsServiceImplTest,
  710. GetBrowsingTopicsForJsApi_OneEpoch_Filtered) {
  711. ukm::TestAutoSetUkmRecorder ukm_recorder;
  712. base::queue<EpochTopics> mock_calculator_results;
  713. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  714. {Topic(2), {}},
  715. {Topic(3), {}},
  716. {Topic(4), {}},
  717. {Topic(5), {}}},
  718. kTime1));
  719. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  720. task_environment()->FastForwardBy(kCalculatorDelay);
  721. NavigateToPage(GURL("https://www.foo.com"));
  722. // Advance to the time after the epoch switch time.
  723. task_environment()->AdvanceClock(base::Days(7) - base::Seconds(1));
  724. std::vector<blink::mojom::EpochTopicPtr> result =
  725. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  726. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  727. web_contents()->GetPrimaryMainFrame());
  728. EXPECT_TRUE(result.empty());
  729. auto entries = ukm_recorder.GetEntriesByName(
  730. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  731. kEntryName);
  732. EXPECT_EQ(1u, entries.size());
  733. ukm_recorder.ExpectEntryMetric(
  734. entries.back(),
  735. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  736. kEmptyReasonName,
  737. 3 /* kCandicateTopicsFiltered */);
  738. }
  739. TEST_F(BrowsingTopicsServiceImplTest,
  740. GetBrowsingTopicsForJsApi_TopicNotAllowedByPrivacySandboxSettings) {
  741. base::queue<EpochTopics> mock_calculator_results;
  742. mock_calculator_results.push(
  743. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  744. {Topic(2), {GetHashedDomain("bar.com")}},
  745. {Topic(3), {GetHashedDomain("bar.com")}},
  746. {Topic(4), {GetHashedDomain("bar.com")}},
  747. {Topic(5), {GetHashedDomain("bar.com")}}},
  748. kTime1));
  749. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  750. task_environment()->FastForwardBy(kCalculatorDelay);
  751. NavigateToPage(GURL("https://www.foo.com"));
  752. // Current time is before the epoch switch time.
  753. std::vector<blink::mojom::EpochTopicPtr> result =
  754. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  755. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  756. web_contents()->GetPrimaryMainFrame());
  757. EXPECT_TRUE(result.empty());
  758. // Advance to the time after the epoch switch time.
  759. task_environment()->AdvanceClock(base::Days(7) - base::Seconds(1));
  760. privacy_sandbox_settings_->SetTopicAllowed(
  761. privacy_sandbox::CanonicalTopic(Topic(2), /*taxonomy_version=*/1), false);
  762. result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
  763. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  764. web_contents()->GetPrimaryMainFrame());
  765. EXPECT_TRUE(result.empty());
  766. }
  767. TEST_F(BrowsingTopicsServiceImplTest, GetBrowsingTopicsForJsApi_FourEpochs) {
  768. base::queue<EpochTopics> mock_calculator_results;
  769. mock_calculator_results.push(
  770. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  771. {Topic(2), {GetHashedDomain("bar.com")}},
  772. {Topic(3), {GetHashedDomain("bar.com")}},
  773. {Topic(4), {GetHashedDomain("bar.com")}},
  774. {Topic(5), {GetHashedDomain("bar.com")}}},
  775. kTime1));
  776. mock_calculator_results.push(
  777. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  778. {Topic(7), {GetHashedDomain("bar.com")}},
  779. {Topic(8), {GetHashedDomain("bar.com")}},
  780. {Topic(9), {GetHashedDomain("bar.com")}},
  781. {Topic(10), {GetHashedDomain("bar.com")}}},
  782. kTime1));
  783. mock_calculator_results.push(
  784. CreateTestEpochTopics({{Topic(11), {GetHashedDomain("bar.com")}},
  785. {Topic(12), {GetHashedDomain("bar.com")}},
  786. {Topic(13), {GetHashedDomain("bar.com")}},
  787. {Topic(14), {GetHashedDomain("bar.com")}},
  788. {Topic(15), {GetHashedDomain("bar.com")}}},
  789. kTime1));
  790. mock_calculator_results.push(
  791. CreateTestEpochTopics({{Topic(16), {GetHashedDomain("bar.com")}},
  792. {Topic(17), {GetHashedDomain("bar.com")}},
  793. {Topic(18), {GetHashedDomain("bar.com")}},
  794. {Topic(19), {GetHashedDomain("bar.com")}},
  795. {Topic(20), {GetHashedDomain("bar.com")}}},
  796. kTime1));
  797. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  798. // Finish all calculations.
  799. task_environment()->FastForwardBy(4 * kCalculatorDelay + 3 * base::Days(7));
  800. EXPECT_EQ(browsing_topics_state().epochs().size(), 4u);
  801. NavigateToPage(GURL("https://www.foo.com"));
  802. // Current time is before the epoch switch time.
  803. std::vector<blink::mojom::EpochTopicPtr> result =
  804. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  805. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  806. web_contents()->GetPrimaryMainFrame());
  807. EXPECT_EQ(result.size(), 3u);
  808. std::set<int> result_set;
  809. result_set.insert(result[0]->topic);
  810. result_set.insert(result[1]->topic);
  811. result_set.insert(result[2]->topic);
  812. EXPECT_EQ(result_set, std::set<int>({2, 7, 12}));
  813. // Advance to the time after the epoch switch time.
  814. task_environment()->AdvanceClock(base::Days(7) - base::Seconds(1));
  815. result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
  816. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  817. web_contents()->GetPrimaryMainFrame());
  818. EXPECT_EQ(result.size(), 3u);
  819. result_set.clear();
  820. result_set.insert(result[0]->topic);
  821. result_set.insert(result[1]->topic);
  822. result_set.insert(result[2]->topic);
  823. EXPECT_EQ(result_set, std::set<int>({7, 12, 17}));
  824. }
  825. TEST_F(BrowsingTopicsServiceImplTest,
  826. GetBrowsingTopicsForJsApi_DuplicateTopicsRemoved) {
  827. base::queue<EpochTopics> mock_calculator_results;
  828. mock_calculator_results.push(
  829. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  830. {Topic(2), {GetHashedDomain("bar.com")}},
  831. {Topic(3), {GetHashedDomain("bar.com")}},
  832. {Topic(4), {GetHashedDomain("bar.com")}},
  833. {Topic(5), {GetHashedDomain("bar.com")}}},
  834. kTime1));
  835. mock_calculator_results.push(
  836. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  837. {Topic(7), {GetHashedDomain("bar.com")}},
  838. {Topic(8), {GetHashedDomain("bar.com")}},
  839. {Topic(9), {GetHashedDomain("bar.com")}},
  840. {Topic(10), {GetHashedDomain("bar.com")}}},
  841. kTime1));
  842. mock_calculator_results.push(
  843. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  844. {Topic(2), {GetHashedDomain("bar.com")}},
  845. {Topic(3), {GetHashedDomain("bar.com")}},
  846. {Topic(4), {GetHashedDomain("bar.com")}},
  847. {Topic(5), {GetHashedDomain("bar.com")}}},
  848. kTime1));
  849. mock_calculator_results.push(
  850. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  851. {Topic(7), {GetHashedDomain("bar.com")}},
  852. {Topic(8), {GetHashedDomain("bar.com")}},
  853. {Topic(9), {GetHashedDomain("bar.com")}},
  854. {Topic(10), {GetHashedDomain("bar.com")}}},
  855. kTime1));
  856. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  857. // Finish all calculations.
  858. task_environment()->FastForwardBy(4 * kCalculatorDelay + 3 * base::Days(7));
  859. EXPECT_EQ(browsing_topics_state().epochs().size(), 4u);
  860. NavigateToPage(GURL("https://www.foo.com"));
  861. // Current time is before the epoch switch time.
  862. std::vector<blink::mojom::EpochTopicPtr> result =
  863. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  864. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  865. web_contents()->GetPrimaryMainFrame());
  866. EXPECT_EQ(result.size(), 2u);
  867. std::set<int> result_set;
  868. result_set.insert(result[0]->topic);
  869. result_set.insert(result[1]->topic);
  870. EXPECT_EQ(result_set, std::set<int>({2, 7}));
  871. // Advance to the time after the epoch switch time.
  872. task_environment()->AdvanceClock(base::Days(7) - base::Seconds(1));
  873. result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
  874. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  875. web_contents()->GetPrimaryMainFrame());
  876. EXPECT_EQ(result.size(), 2u);
  877. result_set.clear();
  878. result_set.insert(result[0]->topic);
  879. result_set.insert(result[1]->topic);
  880. EXPECT_EQ(result_set, std::set<int>({2, 7}));
  881. }
  882. // TODO(yaoxia): Re-enable. This test currently fails solely due to it's
  883. // generating a lot of GMOCK WARNING output.
  884. TEST_F(BrowsingTopicsServiceImplTest,
  885. DISABLED_GetBrowsingTopicsForJsApi_TopicsReturnedInRandomOrder) {
  886. base::queue<EpochTopics> mock_calculator_results;
  887. mock_calculator_results.push(
  888. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  889. {Topic(2), {GetHashedDomain("bar.com")}},
  890. {Topic(3), {GetHashedDomain("bar.com")}},
  891. {Topic(4), {GetHashedDomain("bar.com")}},
  892. {Topic(5), {GetHashedDomain("bar.com")}}},
  893. kTime1));
  894. mock_calculator_results.push(
  895. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  896. {Topic(7), {GetHashedDomain("bar.com")}},
  897. {Topic(8), {GetHashedDomain("bar.com")}},
  898. {Topic(9), {GetHashedDomain("bar.com")}},
  899. {Topic(10), {GetHashedDomain("bar.com")}}},
  900. kTime1));
  901. mock_calculator_results.push(
  902. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  903. {Topic(2), {GetHashedDomain("bar.com")}},
  904. {Topic(3), {GetHashedDomain("bar.com")}},
  905. {Topic(4), {GetHashedDomain("bar.com")}},
  906. {Topic(5), {GetHashedDomain("bar.com")}}},
  907. kTime1));
  908. mock_calculator_results.push(
  909. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  910. {Topic(7), {GetHashedDomain("bar.com")}},
  911. {Topic(8), {GetHashedDomain("bar.com")}},
  912. {Topic(9), {GetHashedDomain("bar.com")}},
  913. {Topic(10), {GetHashedDomain("bar.com")}}},
  914. kTime1));
  915. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  916. // Finish all calculations.
  917. task_environment()->FastForwardBy(4 * kCalculatorDelay + 3 * base::Days(7));
  918. EXPECT_EQ(browsing_topics_state().epochs().size(), 4u);
  919. NavigateToPage(GURL("https://www.foo.com"));
  920. // Current time is before the epoch switch time.
  921. // Expect that in 100 rounds, both Topic(2) and Topic(7) should be seen being
  922. // at the front.
  923. bool seen_topic_2_at_front = false;
  924. bool seen_topic_7_at_front = false;
  925. for (int i = 0; i < 100; ++i) {
  926. std::vector<blink::mojom::EpochTopicPtr> result =
  927. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  928. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  929. web_contents()->GetPrimaryMainFrame());
  930. EXPECT_EQ(result.size(), 2u);
  931. std::set<int> result_set;
  932. result_set.insert(result[0]->topic);
  933. result_set.insert(result[1]->topic);
  934. EXPECT_EQ(result_set, std::set<int>({2, 7}));
  935. seen_topic_2_at_front = seen_topic_2_at_front || (result[0]->topic == 2);
  936. seen_topic_7_at_front = seen_topic_2_at_front || (result[0]->topic == 7);
  937. }
  938. EXPECT_TRUE(seen_topic_2_at_front);
  939. EXPECT_TRUE(seen_topic_7_at_front);
  940. }
  941. TEST_F(BrowsingTopicsServiceImplTest,
  942. GetBrowsingTopicsForJsApi_TrackedUsageContext) {
  943. base::queue<EpochTopics> mock_calculator_results;
  944. mock_calculator_results.push(
  945. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  946. {Topic(2), {GetHashedDomain("bar.com")}},
  947. {Topic(3), {GetHashedDomain("bar.com")}},
  948. {Topic(4), {GetHashedDomain("bar.com")}},
  949. {Topic(5), {GetHashedDomain("bar.com")}}},
  950. kTime1));
  951. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  952. task_environment()->RunUntilIdle();
  953. EXPECT_EQ(
  954. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).size(),
  955. 0u);
  956. NavigateToPage(GURL("https://www.foo.com"));
  957. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  958. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  959. web_contents()->GetPrimaryMainFrame());
  960. std::vector<ApiUsageContext> api_usage_contexts =
  961. content::GetBrowsingTopicsApiUsage(topics_site_data_manager());
  962. EXPECT_EQ(api_usage_contexts.size(), 1u);
  963. EXPECT_EQ(api_usage_contexts[0].hashed_main_frame_host,
  964. HashMainFrameHostForStorage("www.foo.com"));
  965. EXPECT_EQ(api_usage_contexts[0].hashed_context_domain,
  966. GetHashedDomain("bar.com"));
  967. }
  968. TEST_F(BrowsingTopicsServiceImplTest, ApiResultUkm_ZeroAndOneTopic) {
  969. ukm::TestAutoSetUkmRecorder ukm_recorder;
  970. base::queue<EpochTopics> mock_calculator_results;
  971. mock_calculator_results.push(
  972. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  973. {Topic(2), {GetHashedDomain("bar.com")}},
  974. {Topic(3), {GetHashedDomain("bar.com")}},
  975. {Topic(4), {GetHashedDomain("bar.com")}},
  976. {Topic(5), {GetHashedDomain("bar.com")}}},
  977. kTime1));
  978. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  979. // Finish all calculations.
  980. task_environment()->FastForwardBy(kCalculatorDelay);
  981. EXPECT_EQ(browsing_topics_state().epochs().size(), 1u);
  982. NavigateToPage(GURL("https://www.foo.com"));
  983. auto entries = ukm_recorder.GetEntriesByName(
  984. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  985. kEntryName);
  986. EXPECT_EQ(0u, entries.size());
  987. // Current time is before the epoch switch time. Expect one ukm event without
  988. // any metrics.
  989. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  990. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  991. web_contents()->GetPrimaryMainFrame());
  992. entries = ukm_recorder.GetEntriesByName(
  993. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  994. kEntryName);
  995. EXPECT_EQ(1u, entries.size());
  996. ukm_recorder.ExpectEntryMetric(
  997. entries.back(),
  998. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  999. kEmptyReasonName,
  1000. 2 /* kNoCandicateTopics */);
  1001. EXPECT_FALSE(ukm_recorder.GetEntryMetric(
  1002. entries.back(),
  1003. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1004. kReturnedTopic0Name));
  1005. // Advance to the time after the epoch switch time.
  1006. task_environment()->AdvanceClock(base::Days(7) - base::Seconds(1));
  1007. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  1008. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  1009. web_contents()->GetPrimaryMainFrame());
  1010. entries = ukm_recorder.GetEntriesByName(
  1011. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1012. kEntryName);
  1013. EXPECT_EQ(2u, entries.size());
  1014. ukm_recorder.ExpectEntryMetric(
  1015. entries.back(),
  1016. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1017. kReturnedTopic0Name,
  1018. 2);
  1019. ukm_recorder.ExpectEntryMetric(
  1020. entries.back(),
  1021. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1022. kReturnedTopic0IsTrueTopTopicName,
  1023. true);
  1024. ukm_recorder.ExpectEntryMetric(
  1025. entries.back(),
  1026. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1027. kReturnedTopic0ModelVersionName,
  1028. 5000000000ULL);
  1029. ukm_recorder.ExpectEntryMetric(
  1030. entries.back(),
  1031. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1032. kReturnedTopic0TaxonomyVersionName,
  1033. 1);
  1034. EXPECT_FALSE(ukm_recorder.GetEntryMetric(
  1035. entries.back(),
  1036. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1037. kReturnedTopic1Name));
  1038. }
  1039. TEST_F(BrowsingTopicsServiceImplTest,
  1040. ApiResultUkm_ReportTrueStatusIfTheTopicHasMixedTrueAndRandomStatus) {
  1041. ukm::TestAutoSetUkmRecorder ukm_recorder;
  1042. base::queue<EpochTopics> mock_calculator_results;
  1043. mock_calculator_results.push(
  1044. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  1045. {Topic(2), {GetHashedDomain("bar.com")}},
  1046. {Topic(3), {GetHashedDomain("bar.com")}},
  1047. {Topic(4), {GetHashedDomain("bar.com")}},
  1048. {Topic(5), {GetHashedDomain("bar.com")}}},
  1049. kTime1,
  1050. /*padded_top_topics_start_index=*/0));
  1051. mock_calculator_results.push(
  1052. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  1053. {Topic(7), {GetHashedDomain("bar.com")}},
  1054. {Topic(8), {GetHashedDomain("bar.com")}},
  1055. {Topic(9), {GetHashedDomain("bar.com")}},
  1056. {Topic(10), {GetHashedDomain("bar.com")}}},
  1057. kTime1, /*padded_top_topics_start_index=*/0));
  1058. mock_calculator_results.push(
  1059. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  1060. {Topic(2), {GetHashedDomain("bar.com")}},
  1061. {Topic(3), {GetHashedDomain("bar.com")}},
  1062. {Topic(4), {GetHashedDomain("bar.com")}},
  1063. {Topic(5), {GetHashedDomain("bar.com")}}},
  1064. kTime1, /*padded_top_topics_start_index=*/0));
  1065. mock_calculator_results.push(
  1066. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  1067. {Topic(7), {GetHashedDomain("bar.com")}},
  1068. {Topic(8), {GetHashedDomain("bar.com")}},
  1069. {Topic(9), {GetHashedDomain("bar.com")}},
  1070. {Topic(10), {GetHashedDomain("bar.com")}}},
  1071. kTime1));
  1072. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1073. // Finish all calculations.
  1074. task_environment()->FastForwardBy(4 * kCalculatorDelay + 3 * base::Days(7));
  1075. EXPECT_EQ(browsing_topics_state().epochs().size(), 4u);
  1076. NavigateToPage(GURL("https://www.foo.com"));
  1077. // Advance to the time after the epoch switch time.
  1078. task_environment()->AdvanceClock(base::Days(7) - base::Seconds(1));
  1079. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  1080. /*context_origin=*/url::Origin::Create(GURL("https://www.bar.com")),
  1081. web_contents()->GetPrimaryMainFrame());
  1082. auto entries = ukm_recorder.GetEntriesByName(
  1083. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1084. kEntryName);
  1085. EXPECT_EQ(1u, entries.size());
  1086. const int64_t* topic0_metric = ukm_recorder.GetEntryMetric(
  1087. entries.back(),
  1088. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1089. kReturnedTopic0Name);
  1090. const int64_t* topic0_is_true_metric = ukm_recorder.GetEntryMetric(
  1091. entries.back(),
  1092. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1093. kReturnedTopic0IsTrueTopTopicName);
  1094. const int64_t* topic1_metric = ukm_recorder.GetEntryMetric(
  1095. entries.back(),
  1096. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1097. kReturnedTopic1Name);
  1098. const int64_t* topic1_is_true_metric = ukm_recorder.GetEntryMetric(
  1099. entries.back(),
  1100. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1101. kReturnedTopic1IsTrueTopTopicName);
  1102. EXPECT_TRUE(topic0_metric);
  1103. EXPECT_TRUE(topic0_is_true_metric);
  1104. EXPECT_TRUE(topic1_metric);
  1105. EXPECT_TRUE(topic1_is_true_metric);
  1106. EXPECT_TRUE((*topic0_metric == 2 && *topic0_is_true_metric == false &&
  1107. *topic1_metric == 7 && *topic1_is_true_metric == true) ||
  1108. (*topic0_metric == 7 && *topic0_is_true_metric == true &&
  1109. *topic1_metric == 2 && *topic1_is_true_metric == false));
  1110. EXPECT_FALSE(ukm_recorder.GetEntryMetric(
  1111. entries.back(),
  1112. ukm::builders::BrowsingTopics_DocumentBrowsingTopicsApiResult::
  1113. kReturnedTopic2Name));
  1114. }
  1115. TEST_F(BrowsingTopicsServiceImplTest, GetTopicsForSiteForDisplay) {
  1116. base::queue<EpochTopics> mock_calculator_results;
  1117. mock_calculator_results.push(
  1118. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  1119. {Topic(2), {GetHashedDomain("bar.com")}},
  1120. {Topic(3), {GetHashedDomain("bar.com")}},
  1121. {Topic(4), {GetHashedDomain("bar.com")}},
  1122. {Topic(5), {GetHashedDomain("bar.com")}}},
  1123. kTime1));
  1124. mock_calculator_results.push(
  1125. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  1126. {Topic(7), {GetHashedDomain("bar.com")}},
  1127. {Topic(8), {GetHashedDomain("bar.com")}},
  1128. {Topic(9), {GetHashedDomain("bar.com")}},
  1129. {Topic(10), {GetHashedDomain("bar.com")}}},
  1130. kTime1));
  1131. mock_calculator_results.push(
  1132. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  1133. {Topic(2), {GetHashedDomain("bar.com")}},
  1134. {Topic(3), {GetHashedDomain("bar.com")}},
  1135. {Topic(4), {GetHashedDomain("bar.com")}},
  1136. {Topic(5), {GetHashedDomain("bar.com")}}},
  1137. kTime1,
  1138. /*padded_top_topics_start_index=*/0));
  1139. mock_calculator_results.push(
  1140. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  1141. {Topic(7), {GetHashedDomain("bar.com")}},
  1142. {Topic(8), {GetHashedDomain("bar.com")}},
  1143. {Topic(9), {GetHashedDomain("bar.com")}},
  1144. {Topic(10), {GetHashedDomain("bar.com")}}},
  1145. kTime1));
  1146. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1147. // Finish all calculations.
  1148. task_environment()->FastForwardBy(4 * kCalculatorDelay + 3 * base::Days(7));
  1149. EXPECT_EQ(browsing_topics_state().epochs().size(), 4u);
  1150. NavigateToPage(GURL("https://www.foo.com"));
  1151. // Current time is before the epoch switch time.
  1152. std::vector<privacy_sandbox::CanonicalTopic> result =
  1153. browsing_topics_service_->GetTopicsForSiteForDisplay(
  1154. web_contents()->GetPrimaryMainFrame()->GetLastCommittedOrigin());
  1155. EXPECT_EQ(result.size(), 2u);
  1156. EXPECT_EQ(result[0].topic_id(), Topic(2));
  1157. EXPECT_EQ(result[1].topic_id(), Topic(7));
  1158. EXPECT_EQ(result[0].taxonomy_version(), 1);
  1159. EXPECT_EQ(result[1].taxonomy_version(), 1);
  1160. }
  1161. TEST_F(BrowsingTopicsServiceImplTest, GetTopTopicsForDisplay) {
  1162. base::queue<EpochTopics> mock_calculator_results;
  1163. mock_calculator_results.push(
  1164. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  1165. {Topic(2), {GetHashedDomain("bar.com")}},
  1166. {Topic(3), {GetHashedDomain("bar.com")}},
  1167. {Topic(4), {GetHashedDomain("bar.com")}},
  1168. {Topic(5), {GetHashedDomain("bar.com")}}},
  1169. kTime1,
  1170. /*padded_top_topics_start_index=*/1));
  1171. mock_calculator_results.push(
  1172. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  1173. {Topic(7), {GetHashedDomain("bar.com")}},
  1174. {Topic(8), {GetHashedDomain("bar.com")}},
  1175. {Topic(9), {GetHashedDomain("bar.com")}},
  1176. {Topic(10), {GetHashedDomain("bar.com")}}},
  1177. kTime1));
  1178. mock_calculator_results.push(
  1179. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("bar.com")}},
  1180. {Topic(2), {GetHashedDomain("bar.com")}},
  1181. {Topic(3), {GetHashedDomain("bar.com")}},
  1182. {Topic(4), {GetHashedDomain("bar.com")}},
  1183. {Topic(5), {GetHashedDomain("bar.com")}}},
  1184. kTime1,
  1185. /*padded_top_topics_start_index=*/2));
  1186. mock_calculator_results.push(
  1187. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("bar.com")}},
  1188. {Topic(7), {GetHashedDomain("bar.com")}},
  1189. {Topic(8), {GetHashedDomain("bar.com")}},
  1190. {Topic(9), {GetHashedDomain("bar.com")}},
  1191. {Topic(10), {GetHashedDomain("bar.com")}}},
  1192. kTime1));
  1193. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1194. // Finish all calculations.
  1195. task_environment()->FastForwardBy(4 * kCalculatorDelay + 3 * base::Days(7));
  1196. EXPECT_EQ(browsing_topics_state().epochs().size(), 4u);
  1197. NavigateToPage(GURL("https://www.foo.com"));
  1198. // Current time is before the epoch switch time.
  1199. std::vector<privacy_sandbox::CanonicalTopic> result =
  1200. browsing_topics_service_->GetTopTopicsForDisplay();
  1201. EXPECT_EQ(result.size(), 13u);
  1202. EXPECT_EQ(result[0].topic_id(), Topic(1));
  1203. EXPECT_EQ(result[1].topic_id(), Topic(6));
  1204. EXPECT_EQ(result[2].topic_id(), Topic(7));
  1205. EXPECT_EQ(result[3].topic_id(), Topic(8));
  1206. EXPECT_EQ(result[4].topic_id(), Topic(9));
  1207. EXPECT_EQ(result[5].topic_id(), Topic(10));
  1208. EXPECT_EQ(result[6].topic_id(), Topic(1));
  1209. EXPECT_EQ(result[7].topic_id(), Topic(2));
  1210. EXPECT_EQ(result[8].topic_id(), Topic(6));
  1211. EXPECT_EQ(result[9].topic_id(), Topic(7));
  1212. EXPECT_EQ(result[10].topic_id(), Topic(8));
  1213. EXPECT_EQ(result[11].topic_id(), Topic(9));
  1214. EXPECT_EQ(result[12].topic_id(), Topic(10));
  1215. }
  1216. TEST_F(BrowsingTopicsServiceImplTest,
  1217. GetBrowsingTopicsStateForWebUi_CalculationInProgress) {
  1218. base::Time start_time = base::Time::Now();
  1219. base::queue<EpochTopics> mock_calculator_results;
  1220. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  1221. {Topic(2), {}},
  1222. {Topic(3), {}},
  1223. {Topic(4), {}},
  1224. {Topic(5), {}}},
  1225. start_time));
  1226. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1227. task_environment()->RunUntilIdle();
  1228. base::test::TestFuture<mojom::WebUIGetBrowsingTopicsStateResultPtr> future1;
  1229. base::test::TestFuture<mojom::WebUIGetBrowsingTopicsStateResultPtr> future2;
  1230. browsing_topics_service_->GetBrowsingTopicsStateForWebUi(
  1231. /*calculate_now=*/false, future1.GetCallback());
  1232. browsing_topics_service_->GetBrowsingTopicsStateForWebUi(
  1233. /*calculate_now=*/true, future2.GetCallback());
  1234. EXPECT_FALSE(future1.IsReady());
  1235. EXPECT_FALSE(future2.IsReady());
  1236. task_environment()->FastForwardBy(kCalculatorDelay);
  1237. // The callbacks are invoked after the calculation has finished.
  1238. EXPECT_TRUE(future1.IsReady());
  1239. EXPECT_TRUE(future2.IsReady());
  1240. mojom::WebUIGetBrowsingTopicsStateResultPtr result1 = future1.Take();
  1241. mojom::WebUIGetBrowsingTopicsStateResultPtr result2 = future2.Take();
  1242. EXPECT_EQ(result1, result2);
  1243. mojom::WebUIBrowsingTopicsStatePtr& webui_state1 =
  1244. result1->get_browsing_topics_state();
  1245. EXPECT_EQ(webui_state1->epochs.size(), 1u);
  1246. EXPECT_EQ(webui_state1->next_scheduled_calculation_time,
  1247. start_time + kCalculatorDelay + base::Days(7));
  1248. }
  1249. TEST_F(BrowsingTopicsServiceImplTest,
  1250. GetBrowsingTopicsStateForWebUi_CalculationNow) {
  1251. base::Time start_time = base::Time::Now();
  1252. base::queue<EpochTopics> mock_calculator_results;
  1253. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  1254. {Topic(2), {}},
  1255. {Topic(3), {}},
  1256. {Topic(4), {}},
  1257. {Topic(5), {}}},
  1258. start_time));
  1259. mock_calculator_results.push(
  1260. CreateTestEpochTopics({{Topic(1), {}},
  1261. {Topic(2), {}},
  1262. {Topic(3), {}},
  1263. {Topic(4), {}},
  1264. {Topic(5), {}}},
  1265. start_time + kCalculatorDelay + base::Days(1)));
  1266. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1267. task_environment()->FastForwardBy(kCalculatorDelay);
  1268. EXPECT_EQ(browsing_topics_state().epochs().size(), 1u);
  1269. EXPECT_EQ(browsing_topics_state().next_scheduled_calculation_time(),
  1270. start_time + kCalculatorDelay + base::Days(7));
  1271. // Advance by some time smaller than the periodic update interval.
  1272. task_environment()->FastForwardBy(base::Days(1));
  1273. base::test::TestFuture<mojom::WebUIGetBrowsingTopicsStateResultPtr> future;
  1274. browsing_topics_service_->GetBrowsingTopicsStateForWebUi(
  1275. /*calculate_now=*/true, future.GetCallback());
  1276. EXPECT_FALSE(future.IsReady());
  1277. task_environment()->FastForwardBy(kCalculatorDelay);
  1278. EXPECT_TRUE(future.IsReady());
  1279. mojom::WebUIGetBrowsingTopicsStateResultPtr result = future.Take();
  1280. mojom::WebUIBrowsingTopicsStatePtr& webui_state =
  1281. result->get_browsing_topics_state();
  1282. EXPECT_EQ(webui_state->epochs.size(), 2u);
  1283. // The `next_scheduled_calculation_time` is reset to 7 days after.
  1284. EXPECT_EQ(webui_state->next_scheduled_calculation_time,
  1285. start_time + 2 * kCalculatorDelay + base::Days(1) + base::Days(7));
  1286. }
  1287. TEST_F(BrowsingTopicsServiceImplTest, GetBrowsingTopicsStateForWebUi) {
  1288. base::Time start_time = base::Time::Now();
  1289. base::queue<EpochTopics> mock_calculator_results;
  1290. mock_calculator_results.push(
  1291. CreateTestEpochTopics({{Topic(1), {HashedDomain(123), HashedDomain(456)}},
  1292. {Topic(2), {}},
  1293. {Topic(0), {}}, // blocked
  1294. {Topic(4), {}},
  1295. {Topic(5), {}}},
  1296. start_time));
  1297. // Failed calculation.
  1298. mock_calculator_results.push(EpochTopics(start_time + base::Days(7)));
  1299. mock_calculator_results.push(
  1300. CreateTestEpochTopics({{Topic(6), {}},
  1301. {Topic(7), {}},
  1302. {Topic(8), {}},
  1303. {Topic(9), {}},
  1304. {Topic(10), {}}},
  1305. start_time + base::Days(14),
  1306. /*padded_top_topics_start_index=*/2));
  1307. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1308. // Finish file loading and three calculations.
  1309. task_environment()->FastForwardBy(3 * kCalculatorDelay + 2 * base::Days(7));
  1310. base::test::TestFuture<mojom::WebUIGetBrowsingTopicsStateResultPtr> future;
  1311. browsing_topics_service_->GetBrowsingTopicsStateForWebUi(
  1312. /*calculate_now=*/false, future.GetCallback());
  1313. EXPECT_TRUE(future.IsReady());
  1314. mojom::WebUIGetBrowsingTopicsStateResultPtr result = future.Take();
  1315. mojom::WebUIBrowsingTopicsStatePtr& webui_state =
  1316. result->get_browsing_topics_state();
  1317. EXPECT_EQ(webui_state->epochs.size(), 3u);
  1318. EXPECT_EQ(webui_state->next_scheduled_calculation_time,
  1319. start_time + 3 * kCalculatorDelay + 3 * base::Days(7));
  1320. const mojom::WebUIEpochPtr& epoch0 = webui_state->epochs[0];
  1321. const mojom::WebUIEpochPtr& epoch1 = webui_state->epochs[1];
  1322. const mojom::WebUIEpochPtr& epoch2 = webui_state->epochs[2];
  1323. EXPECT_EQ(epoch0->calculation_time, start_time + base::Days(14));
  1324. EXPECT_EQ(epoch0->model_version, "5000000000");
  1325. EXPECT_EQ(epoch0->taxonomy_version, "1");
  1326. EXPECT_EQ(epoch0->topics.size(), 5u);
  1327. EXPECT_EQ(epoch0->topics[0]->topic_id, 6);
  1328. EXPECT_EQ(epoch0->topics[0]->topic_name, u"Entertainment industry");
  1329. EXPECT_TRUE(epoch0->topics[0]->is_real_topic);
  1330. EXPECT_TRUE(epoch0->topics[0]->observed_by_domains.empty());
  1331. EXPECT_EQ(epoch0->topics[1]->topic_id, 7);
  1332. EXPECT_EQ(epoch0->topics[1]->topic_name, u"Humor");
  1333. EXPECT_TRUE(epoch0->topics[1]->is_real_topic);
  1334. EXPECT_TRUE(epoch0->topics[1]->observed_by_domains.empty());
  1335. EXPECT_EQ(epoch0->topics[2]->topic_id, 8);
  1336. EXPECT_EQ(epoch0->topics[2]->topic_name, u"Live comedy");
  1337. EXPECT_FALSE(epoch0->topics[2]->is_real_topic);
  1338. EXPECT_TRUE(epoch0->topics[2]->observed_by_domains.empty());
  1339. EXPECT_EQ(epoch0->topics[3]->topic_id, 9);
  1340. EXPECT_EQ(epoch0->topics[3]->topic_name, u"Live sporting events");
  1341. EXPECT_FALSE(epoch0->topics[3]->is_real_topic);
  1342. EXPECT_TRUE(epoch0->topics[3]->observed_by_domains.empty());
  1343. EXPECT_EQ(epoch0->topics[4]->topic_id, 10);
  1344. EXPECT_EQ(epoch0->topics[4]->topic_name, u"Magic");
  1345. EXPECT_FALSE(epoch0->topics[4]->is_real_topic);
  1346. EXPECT_TRUE(epoch0->topics[4]->observed_by_domains.empty());
  1347. EXPECT_EQ(epoch1->calculation_time, start_time + base::Days(7));
  1348. EXPECT_EQ(epoch1->model_version, "0");
  1349. EXPECT_EQ(epoch1->taxonomy_version, "0");
  1350. EXPECT_EQ(epoch1->topics.size(), 0u);
  1351. EXPECT_EQ(epoch2->calculation_time, start_time);
  1352. EXPECT_EQ(epoch2->model_version, "5000000000");
  1353. EXPECT_EQ(epoch2->taxonomy_version, "1");
  1354. EXPECT_EQ(epoch2->topics.size(), 5u);
  1355. EXPECT_EQ(epoch2->topics[0]->topic_id, 1);
  1356. EXPECT_EQ(epoch2->topics[0]->topic_name, u"Arts & entertainment");
  1357. EXPECT_TRUE(epoch2->topics[0]->is_real_topic);
  1358. EXPECT_EQ(epoch2->topics[0]->observed_by_domains.size(), 2u);
  1359. EXPECT_EQ(epoch2->topics[0]->observed_by_domains[0], "123");
  1360. EXPECT_EQ(epoch2->topics[0]->observed_by_domains[1], "456");
  1361. EXPECT_EQ(epoch2->topics[1]->topic_id, 2);
  1362. EXPECT_EQ(epoch2->topics[1]->topic_name, u"Acting & theater");
  1363. EXPECT_TRUE(epoch2->topics[1]->is_real_topic);
  1364. EXPECT_TRUE(epoch2->topics[1]->observed_by_domains.empty());
  1365. EXPECT_EQ(epoch2->topics[2]->topic_id, 0);
  1366. EXPECT_EQ(epoch2->topics[2]->topic_name, u"Unknown");
  1367. EXPECT_TRUE(epoch2->topics[2]->is_real_topic);
  1368. EXPECT_TRUE(epoch2->topics[2]->observed_by_domains.empty());
  1369. EXPECT_EQ(epoch2->topics[3]->topic_id, 4);
  1370. EXPECT_EQ(epoch2->topics[3]->topic_name, u"Concerts & music festivals");
  1371. EXPECT_TRUE(epoch2->topics[3]->is_real_topic);
  1372. EXPECT_TRUE(epoch2->topics[3]->observed_by_domains.empty());
  1373. EXPECT_EQ(epoch2->topics[4]->topic_id, 5);
  1374. EXPECT_EQ(epoch2->topics[4]->topic_name, u"Dance");
  1375. EXPECT_TRUE(epoch2->topics[4]->is_real_topic);
  1376. EXPECT_TRUE(epoch2->topics[4]->observed_by_domains.empty());
  1377. }
  1378. TEST_F(BrowsingTopicsServiceImplTest, ClearTopic) {
  1379. base::Time start_time = base::Time::Now();
  1380. base::queue<EpochTopics> mock_calculator_results;
  1381. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  1382. {Topic(2), {}},
  1383. {Topic(3), {}},
  1384. {Topic(4), {}},
  1385. {Topic(5), {}}},
  1386. start_time));
  1387. mock_calculator_results.push(
  1388. CreateTestEpochTopics({{Topic(6), {}},
  1389. {Topic(7), {}},
  1390. {Topic(8), {}},
  1391. {Topic(9), {}},
  1392. {Topic(10), {}}},
  1393. start_time + kCalculatorDelay + base::Days(7)));
  1394. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1395. // Finish file loading and two calculations.
  1396. task_environment()->FastForwardBy(2 * kCalculatorDelay + base::Days(7));
  1397. browsing_topics_service_->ClearTopic(
  1398. privacy_sandbox::CanonicalTopic(Topic(3), /*taxonomy_version=*/1));
  1399. std::vector<privacy_sandbox::CanonicalTopic> result =
  1400. browsing_topics_service_->GetTopTopicsForDisplay();
  1401. EXPECT_EQ(result.size(), 9u);
  1402. EXPECT_EQ(result[0].topic_id(), Topic(1));
  1403. EXPECT_EQ(result[1].topic_id(), Topic(2));
  1404. EXPECT_EQ(result[2].topic_id(), Topic(4));
  1405. EXPECT_EQ(result[3].topic_id(), Topic(5));
  1406. EXPECT_EQ(result[4].topic_id(), Topic(6));
  1407. EXPECT_EQ(result[5].topic_id(), Topic(7));
  1408. EXPECT_EQ(result[6].topic_id(), Topic(8));
  1409. EXPECT_EQ(result[7].topic_id(), Topic(9));
  1410. EXPECT_EQ(result[8].topic_id(), Topic(10));
  1411. }
  1412. TEST_F(BrowsingTopicsServiceImplTest, ClearTopicBeforeLoadFinish) {
  1413. base::Time start_time = base::Time::Now();
  1414. std::vector<EpochTopics> preexisting_epochs;
  1415. preexisting_epochs.push_back(
  1416. CreateTestEpochTopics({{Topic(1), {}},
  1417. {Topic(2), {}},
  1418. {Topic(3), {}},
  1419. {Topic(4), {}},
  1420. {Topic(5), {}}},
  1421. start_time - base::Days(1)));
  1422. CreateBrowsingTopicsStateFile(
  1423. std::move(preexisting_epochs),
  1424. /*next_scheduled_calculation_time=*/start_time + base::Days(1));
  1425. base::queue<EpochTopics> mock_calculator_results;
  1426. mock_calculator_results.push(CreateTestEpochTopics({{Topic(6), {}},
  1427. {Topic(7), {}},
  1428. {Topic(8), {}},
  1429. {Topic(9), {}},
  1430. {Topic(10), {}}},
  1431. kTime2));
  1432. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1433. privacy_sandbox_settings_->SetTopicAllowed(
  1434. privacy_sandbox::CanonicalTopic(Topic(3), /*taxonomy_version=*/1), false);
  1435. // Finish file loading.
  1436. task_environment()->RunUntilIdle();
  1437. // If a topic in the settings is cleared before load finish, all pre-existing
  1438. // topics data will be cleared in the `BrowsingTopicsState` after load finish.
  1439. std::vector<privacy_sandbox::CanonicalTopic> result =
  1440. browsing_topics_service_->GetTopTopicsForDisplay();
  1441. EXPECT_EQ(result.size(), 0u);
  1442. }
  1443. TEST_F(BrowsingTopicsServiceImplTest, ClearAllTopicsData) {
  1444. base::Time start_time = base::Time::Now();
  1445. base::queue<EpochTopics> mock_calculator_results;
  1446. mock_calculator_results.push(CreateTestEpochTopics({{Topic(1), {}},
  1447. {Topic(2), {}},
  1448. {Topic(3), {}},
  1449. {Topic(4), {}},
  1450. {Topic(5), {}}},
  1451. start_time));
  1452. mock_calculator_results.push(
  1453. CreateTestEpochTopics({{Topic(6), {}},
  1454. {Topic(7), {}},
  1455. {Topic(8), {}},
  1456. {Topic(9), {}},
  1457. {Topic(10), {}}},
  1458. start_time + kCalculatorDelay + base::Days(7)));
  1459. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1460. // Finish file loading and two calculations.
  1461. task_environment()->FastForwardBy(2 * kCalculatorDelay + base::Days(7));
  1462. // Add some arbitrary data to site data storage. The intent is just to test
  1463. // data deletion.
  1464. topics_site_data_manager()->OnBrowsingTopicsApiUsed(
  1465. HashMainFrameHostForStorage("a.com"), {HashedDomain(1), HashedDomain(2)},
  1466. base::Time::Now());
  1467. EXPECT_EQ(
  1468. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).size(),
  1469. 2u);
  1470. task_environment()->FastForwardBy(base::Seconds(1));
  1471. browsing_topics_service_->ClearAllTopicsData();
  1472. task_environment()->RunUntilIdle();
  1473. std::vector<privacy_sandbox::CanonicalTopic> result =
  1474. browsing_topics_service_->GetTopTopicsForDisplay();
  1475. EXPECT_TRUE(browsing_topics_service_->GetTopTopicsForDisplay().empty());
  1476. EXPECT_TRUE(
  1477. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).empty());
  1478. }
  1479. TEST_F(BrowsingTopicsServiceImplTest, ClearTopicsDataForOrigin) {
  1480. base::queue<EpochTopics> mock_calculator_results;
  1481. mock_calculator_results.push(
  1482. CreateTestEpochTopics({{Topic(1), {GetHashedDomain("b.com")}},
  1483. {Topic(2), {GetHashedDomain("b.com")}},
  1484. {Topic(3), {GetHashedDomain("b.com")}},
  1485. {Topic(4), {GetHashedDomain("b.com")}},
  1486. {Topic(5), {GetHashedDomain("b.com")}}},
  1487. kTime1));
  1488. mock_calculator_results.push(
  1489. CreateTestEpochTopics({{Topic(6), {GetHashedDomain("b.com")}},
  1490. {Topic(7), {GetHashedDomain("b.com")}},
  1491. {Topic(8), {GetHashedDomain("b.com")}},
  1492. {Topic(9), {GetHashedDomain("b.com")}},
  1493. {Topic(10), {GetHashedDomain("b.com")}}},
  1494. kTime1 + kCalculatorDelay + base::Days(7)));
  1495. InitializeBrowsingTopicsService(std::move(mock_calculator_results));
  1496. NavigateToPage(GURL("https://a.com"));
  1497. // Finish file loading and two calculations.
  1498. task_environment()->FastForwardBy(2 * kCalculatorDelay + base::Days(7));
  1499. std::vector<blink::mojom::EpochTopicPtr> api_call_result =
  1500. browsing_topics_service_->GetBrowsingTopicsForJsApi(
  1501. /*context_origin=*/url::Origin::Create(GURL("https://b.com")),
  1502. web_contents()->GetPrimaryMainFrame());
  1503. EXPECT_EQ(api_call_result.size(), 1u);
  1504. EXPECT_EQ(api_call_result[0]->topic, 3);
  1505. // Add some arbitrary data to site data storage. The intent is just to test
  1506. // data deletion.
  1507. topics_site_data_manager()->OnBrowsingTopicsApiUsed(
  1508. HashMainFrameHostForStorage("d.com"),
  1509. {GetHashedDomain("b.com"), GetHashedDomain("c.com")}, base::Time::Now());
  1510. // The data is from both the manual `OnBrowsingTopicsApiUsed` call and the
  1511. // usage tracking due to the previous `GetBrowsingTopicsForJsApi`.
  1512. EXPECT_EQ(
  1513. content::GetBrowsingTopicsApiUsage(topics_site_data_manager()).size(),
  1514. 3u);
  1515. task_environment()->FastForwardBy(base::Seconds(1));
  1516. browsing_topics_service_->ClearTopicsDataForOrigin(
  1517. url::Origin::Create(GURL("https://b.com")));
  1518. task_environment()->RunUntilIdle();
  1519. // Note that this won't trigger another usage storing to the database, because
  1520. // the same context domain was seen in the page before.
  1521. api_call_result = browsing_topics_service_->GetBrowsingTopicsForJsApi(
  1522. /*context_origin=*/url::Origin::Create(GURL("https://b.com")),
  1523. web_contents()->GetPrimaryMainFrame());
  1524. // Since the domain "b.com" is removed. The candidate topic won't be returned.
  1525. EXPECT_TRUE(api_call_result.empty());
  1526. std::vector<browsing_topics::ApiUsageContext> api_usage_contexts =
  1527. content::GetBrowsingTopicsApiUsage(topics_site_data_manager());
  1528. // The context domain "b.com" should have been cleared.
  1529. EXPECT_EQ(api_usage_contexts.size(), 1u);
  1530. EXPECT_EQ(api_usage_contexts[0].hashed_main_frame_host,
  1531. HashMainFrameHostForStorage("d.com"));
  1532. EXPECT_EQ(api_usage_contexts[0].hashed_context_domain,
  1533. GetHashedDomain("c.com"));
  1534. }
  1535. } // namespace browsing_topics