1 // Copyright 2017 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 
5 #include "chrome/browser/media/media_engagement_score.h"
6 
7 #include <utility>
8 
9 #include "base/macros.h"
10 #include "base/metrics/field_trial_param_associator.h"
11 #include "base/metrics/field_trial_params.h"
12 #include "base/test/scoped_feature_list.h"
13 #include "base/test/simple_test_clock.h"
14 #include "base/values.h"
15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
16 #include "chrome/browser/engagement/site_engagement_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "components/content_settings/core/browser/host_content_settings_map.h"
21 #include "media/base/media_switches.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "url/gurl.h"
24 
25 namespace {
26 
GetReferenceTime()27 base::Time GetReferenceTime() {
28   base::Time::Exploded exploded_reference_time;
29   exploded_reference_time.year = 2015;
30   exploded_reference_time.month = 1;
31   exploded_reference_time.day_of_month = 30;
32   exploded_reference_time.day_of_week = 5;
33   exploded_reference_time.hour = 11;
34   exploded_reference_time.minute = 0;
35   exploded_reference_time.second = 0;
36   exploded_reference_time.millisecond = 0;
37 
38   base::Time out_time;
39   EXPECT_TRUE(
40       base::Time::FromLocalExploded(exploded_reference_time, &out_time));
41   return out_time;
42 }
43 
44 }  // namespace
45 
46 class MediaEngagementScoreTest : public ChromeRenderViewHostTestHarness {
47  public:
SetUp()48   void SetUp() override {
49     ChromeRenderViewHostTestHarness::SetUp();
50     test_clock.SetNow(GetReferenceTime());
51     score_ = new MediaEngagementScore(&test_clock, url::Origin(), nullptr);
52   }
53 
TearDown()54   void TearDown() override {
55     delete score_;
56     ChromeRenderViewHostTestHarness::TearDown();
57   }
58 
59   base::SimpleTestClock test_clock;
60 
61  protected:
62   MediaEngagementScore* score_;
63 
VerifyScore(MediaEngagementScore * score,int expected_visits,int expected_media_playbacks,base::Time expected_last_media_playback_time,bool has_high_score)64   void VerifyScore(MediaEngagementScore* score,
65                    int expected_visits,
66                    int expected_media_playbacks,
67                    base::Time expected_last_media_playback_time,
68                    bool has_high_score) {
69     EXPECT_EQ(expected_visits, score->visits());
70     EXPECT_EQ(expected_media_playbacks, score->media_playbacks());
71     EXPECT_EQ(expected_last_media_playback_time,
72               score->last_media_playback_time());
73     EXPECT_EQ(has_high_score, score->high_score());
74   }
75 
UpdateScore(MediaEngagementScore * score)76   void UpdateScore(MediaEngagementScore* score) {
77     test_clock.SetNow(test_clock.Now() + base::TimeDelta::FromHours(1));
78 
79     score->IncrementVisits();
80     score->IncrementMediaPlaybacks();
81   }
82 
TestScoreInitializesAndUpdates(std::unique_ptr<base::DictionaryValue> score_dict,int expected_visits,int expected_media_playbacks,base::Time expected_last_media_playback_time,bool has_high_score,bool update_score_expectation)83   void TestScoreInitializesAndUpdates(
84       std::unique_ptr<base::DictionaryValue> score_dict,
85       int expected_visits,
86       int expected_media_playbacks,
87       base::Time expected_last_media_playback_time,
88       bool has_high_score,
89       bool update_score_expectation) {
90     MediaEngagementScore* initial_score =
91         new MediaEngagementScore(&test_clock, url::Origin(),
92                                  std::move(score_dict), nullptr /* settings */);
93     VerifyScore(initial_score, expected_visits, expected_media_playbacks,
94                 expected_last_media_playback_time, has_high_score);
95 
96     // Updating the score dict should return false, as the score shouldn't
97     // have changed at this point.
98     EXPECT_FALSE(initial_score->UpdateScoreDict());
99 
100     // Increment the scores and check that the values were stored correctly.
101     UpdateScore(initial_score);
102     EXPECT_EQ(update_score_expectation, initial_score->UpdateScoreDict());
103     delete initial_score;
104   }
105 
SetScore(MediaEngagementScore * score,int visits,int media_playbacks)106   static void SetScore(MediaEngagementScore* score,
107                        int visits,
108                        int media_playbacks) {
109     score->SetVisits(visits);
110     score->SetMediaPlaybacks(media_playbacks);
111   }
112 
SetScore(int visits,int media_playbacks)113   void SetScore(int visits, int media_playbacks) {
114     SetScore(score_, visits, media_playbacks);
115   }
116 
VerifyGetScoreDetails(MediaEngagementScore * score)117   void VerifyGetScoreDetails(MediaEngagementScore* score) {
118     media::mojom::MediaEngagementScoreDetailsPtr details =
119         score->GetScoreDetails();
120     EXPECT_EQ(details->origin, score->origin_);
121     EXPECT_EQ(details->total_score, score->actual_score());
122     EXPECT_EQ(details->visits, score->visits());
123     EXPECT_EQ(details->media_playbacks, score->media_playbacks());
124     EXPECT_EQ(details->last_media_playback_time,
125               score->last_media_playback_time().ToJsTime());
126   }
127 
OverrideFieldTrial(int min_visits,double lower_threshold,double upper_threshold)128   void OverrideFieldTrial(int min_visits,
129                           double lower_threshold,
130                           double upper_threshold) {
131     base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
132 
133     std::map<std::string, std::string> params;
134     params[MediaEngagementScore::kScoreMinVisitsParamName] =
135         std::to_string(min_visits);
136     params[MediaEngagementScore::kHighScoreLowerThresholdParamName] =
137         std::to_string(lower_threshold);
138     params[MediaEngagementScore::kHighScoreUpperThresholdParamName] =
139         std::to_string(upper_threshold);
140 
141     scoped_feature_list_ = std::make_unique<base::test::ScopedFeatureList>();
142     scoped_feature_list_->InitAndEnableFeatureWithParameters(
143         media::kMediaEngagementBypassAutoplayPolicies, params);
144 
145     std::map<std::string, std::string> actual_params;
146     EXPECT_TRUE(base::GetFieldTrialParamsByFeature(
147         media::kMediaEngagementBypassAutoplayPolicies, &actual_params));
148     EXPECT_EQ(params, actual_params);
149 
150     score_->Recalculate();
151   }
152 
153  private:
154   std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
155 };
156 
157 // Test Mojo serialization.
TEST_F(MediaEngagementScoreTest,MojoSerialization)158 TEST_F(MediaEngagementScoreTest, MojoSerialization) {
159   VerifyGetScoreDetails(score_);
160   UpdateScore(score_);
161   VerifyGetScoreDetails(score_);
162 }
163 
164 // Test that scores are read / written correctly from / to empty score
165 // dictionaries.
TEST_F(MediaEngagementScoreTest,EmptyDictionary)166 TEST_F(MediaEngagementScoreTest, EmptyDictionary) {
167   std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
168   TestScoreInitializesAndUpdates(std::move(dict), 0, 0, base::Time(), false,
169                                  true);
170 }
171 
172 // Test that scores are read / written correctly from / to partially empty
173 // score dictionaries.
TEST_F(MediaEngagementScoreTest,PartiallyEmptyDictionary)174 TEST_F(MediaEngagementScoreTest, PartiallyEmptyDictionary) {
175   std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
176   dict->SetInteger(MediaEngagementScore::kVisitsKey, 2);
177 
178   TestScoreInitializesAndUpdates(std::move(dict), 2, 0, base::Time(), false,
179                                  true);
180 }
181 
182 // Test that scores are read / written correctly from / to populated score
183 // dictionaries.
TEST_F(MediaEngagementScoreTest,PopulatedDictionary)184 TEST_F(MediaEngagementScoreTest, PopulatedDictionary) {
185   std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
186   dict->SetInteger(MediaEngagementScore::kVisitsKey, 20);
187   dict->SetInteger(MediaEngagementScore::kMediaPlaybacksKey, 12);
188   dict->SetDouble(MediaEngagementScore::kLastMediaPlaybackTimeKey,
189                   test_clock.Now().ToInternalValue());
190   dict->SetBoolean(MediaEngagementScore::kHasHighScoreKey, true);
191 
192   TestScoreInitializesAndUpdates(std::move(dict), 20, 12, test_clock.Now(),
193                                  true, true);
194 }
195 
196 // Test getting and commiting the score works correctly with different
197 // origins.
TEST_F(MediaEngagementScoreTest,ContentSettingsMultiOrigin)198 TEST_F(MediaEngagementScoreTest, ContentSettingsMultiOrigin) {
199   url::Origin origin = url::Origin::Create(GURL("https://www.google.com"));
200 
201   // Replace |score_| with one with an actual URL, and with a settings map.
202   HostContentSettingsMap* settings_map =
203       HostContentSettingsMapFactory::GetForProfile(profile());
204   MediaEngagementScore* score =
205       new MediaEngagementScore(&test_clock, origin, settings_map);
206 
207   // Verify the score is originally zero, try incrementing and storing
208   // the score.
209   VerifyScore(score, 0, 0, base::Time(), false);
210   score->IncrementVisits();
211   UpdateScore(score);
212   score->Commit();
213 
214   // Now confirm the correct score is present on the same origin,
215   // but zero for a different origin.
216   url::Origin same_origin = url::Origin::Create(GURL("https://www.google.com"));
217   url::Origin different_origin =
218       url::Origin::Create(GURL("https://www.google.co.uk"));
219   MediaEngagementScore* new_score =
220       new MediaEngagementScore(&test_clock, origin, settings_map);
221   MediaEngagementScore* same_origin_score =
222       new MediaEngagementScore(&test_clock, same_origin, settings_map);
223   MediaEngagementScore* different_origin_score =
224       new MediaEngagementScore(&test_clock, different_origin, settings_map);
225   VerifyScore(new_score, 2, 1, test_clock.Now(), false);
226   VerifyScore(same_origin_score, 2, 1, test_clock.Now(), false);
227   VerifyScore(different_origin_score, 0, 0, base::Time(), false);
228 
229   delete score;
230   delete new_score;
231   delete same_origin_score;
232   delete different_origin_score;
233 }
234 
235 // Tests content settings read/write.
TEST_F(MediaEngagementScoreTest,ContentSettings)236 TEST_F(MediaEngagementScoreTest, ContentSettings) {
237   HostContentSettingsMap* settings_map =
238       HostContentSettingsMapFactory::GetForProfile(profile());
239   int example_num_visits = 20;
240   int example_media_playbacks = 5;
241 
242   // Store some example data in content settings.
243   url::Origin origin = url::Origin::Create(GURL("https://www.google.com"));
244   std::unique_ptr<base::DictionaryValue> score_dict =
245       std::make_unique<base::DictionaryValue>();
246   score_dict->SetInteger(MediaEngagementScore::kVisitsKey, example_num_visits);
247   score_dict->SetInteger(MediaEngagementScore::kMediaPlaybacksKey,
248                          example_media_playbacks);
249   score_dict->SetDouble(MediaEngagementScore::kLastMediaPlaybackTimeKey,
250                         test_clock.Now().ToInternalValue());
251   score_dict->SetBoolean(MediaEngagementScore::kHasHighScoreKey, false);
252   settings_map->SetWebsiteSettingDefaultScope(
253       origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
254       std::move(score_dict));
255 
256   // Make sure we read that data back correctly.
257   MediaEngagementScore* score =
258       new MediaEngagementScore(&test_clock, origin, settings_map);
259   EXPECT_EQ(score->visits(), example_num_visits);
260   EXPECT_EQ(score->media_playbacks(), example_media_playbacks);
261   EXPECT_EQ(score->last_media_playback_time(), test_clock.Now());
262   EXPECT_FALSE(score->high_score());
263 
264   UpdateScore(score);
265   score->IncrementMediaPlaybacks();
266   EXPECT_TRUE(score->high_score());
267   score->Commit();
268 
269   // Now read back content settings and make sure we have the right values.
270   int stored_visits;
271   int stored_media_playbacks;
272   double stored_last_media_playback_time;
273   bool stored_has_high_score;
274   std::unique_ptr<base::DictionaryValue> values =
275       base::DictionaryValue::From(settings_map->GetWebsiteSetting(
276           origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
277           nullptr));
278   values->GetInteger(MediaEngagementScore::kVisitsKey, &stored_visits);
279   values->GetInteger(MediaEngagementScore::kMediaPlaybacksKey,
280                      &stored_media_playbacks);
281   values->GetDouble(MediaEngagementScore::kLastMediaPlaybackTimeKey,
282                     &stored_last_media_playback_time);
283   values->GetBoolean(MediaEngagementScore::kHasHighScoreKey,
284                      &stored_has_high_score);
285   EXPECT_EQ(stored_visits, example_num_visits + 1);
286   EXPECT_EQ(stored_media_playbacks, example_media_playbacks + 2);
287   EXPECT_EQ(stored_last_media_playback_time,
288             test_clock.Now().ToInternalValue());
289 
290   delete score;
291 }
292 
293 // Test that the engagement score is calculated correctly.
TEST_F(MediaEngagementScoreTest,EngagementScoreCalculation)294 TEST_F(MediaEngagementScoreTest, EngagementScoreCalculation) {
295   EXPECT_EQ(0, score_->actual_score());
296   UpdateScore(score_);
297 
298   // Check that the score increases when there is one visit.
299   EXPECT_EQ(0.05, score_->actual_score());
300 
301   SetScore(20, 8);
302   EXPECT_EQ(0.4, score_->actual_score());
303 
304   UpdateScore(score_);
305   EXPECT_EQ(9.0 / 21.0, score_->actual_score());
306 }
307 
308 // Test that a score without the high_score bit uses the correct bounds.
TEST_F(MediaEngagementScoreTest,HighScoreLegacy_High)309 TEST_F(MediaEngagementScoreTest, HighScoreLegacy_High) {
310   const url::Origin origin =
311       url::Origin::Create(GURL("https://www.example.com"));
312   HostContentSettingsMap* settings_map =
313       HostContentSettingsMapFactory::GetForProfile(profile());
314 
315   {
316     std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
317     dict->SetInteger(MediaEngagementScore::kVisitsKey, 20);
318     dict->SetInteger(MediaEngagementScore::kMediaPlaybacksKey, 6);
319     settings_map->SetWebsiteSettingDefaultScope(
320         origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
321         std::move(dict));
322   }
323 
324   {
325     std::unique_ptr<MediaEngagementScore> score(
326         new MediaEngagementScore(&test_clock, origin, settings_map));
327     VerifyScore(score.get(), 20, 6, base::Time(), true);
328   }
329 }
330 
331 // Test that a score without the high_score bit uses the correct bounds.
TEST_F(MediaEngagementScoreTest,HighScoreLegacy_Low)332 TEST_F(MediaEngagementScoreTest, HighScoreLegacy_Low) {
333   const url::Origin origin =
334       url::Origin::Create(GURL("https://www.example.com"));
335   HostContentSettingsMap* settings_map =
336       HostContentSettingsMapFactory::GetForProfile(profile());
337 
338   {
339     std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
340     dict->SetInteger(MediaEngagementScore::kVisitsKey, 20);
341     dict->SetInteger(MediaEngagementScore::kMediaPlaybacksKey, 4);
342     settings_map->SetWebsiteSettingDefaultScope(
343         origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
344         std::move(dict));
345   }
346 
347   {
348     std::unique_ptr<MediaEngagementScore> score(
349         new MediaEngagementScore(&test_clock, origin, settings_map));
350     VerifyScore(score.get(), 20, 4, base::Time(), false);
351   }
352 }
353 
354 // Test that if we changed the boundaries the high_score bit is updated
355 // when the score is loaded.
TEST_F(MediaEngagementScoreTest,HighScoreUpdated)356 TEST_F(MediaEngagementScoreTest, HighScoreUpdated) {
357   const url::Origin origin =
358       url::Origin::Create(GURL("https://www.example.com"));
359   HostContentSettingsMap* settings_map =
360       HostContentSettingsMapFactory::GetForProfile(profile());
361 
362   {
363     std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
364     dict->SetInteger(MediaEngagementScore::kVisitsKey, 10);
365     dict->SetInteger(MediaEngagementScore::kMediaPlaybacksKey, 1);
366     dict->SetDouble(MediaEngagementScore::kLastMediaPlaybackTimeKey,
367                     test_clock.Now().ToInternalValue());
368     dict->SetBoolean(MediaEngagementScore::kHasHighScoreKey, true);
369 
370     settings_map->SetWebsiteSettingDefaultScope(
371         origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
372         std::move(dict));
373   }
374 
375   {
376     std::unique_ptr<MediaEngagementScore> score(
377         new MediaEngagementScore(&test_clock, origin, settings_map));
378     EXPECT_FALSE(score->high_score());
379     base::RunLoop().RunUntilIdle();
380   }
381 
382   {
383     std::unique_ptr<base::DictionaryValue> dict =
384         base::DictionaryValue::From(settings_map->GetWebsiteSetting(
385             origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
386             nullptr));
387 
388     bool stored_high_score = false;
389     dict->GetBoolean(MediaEngagementScore::kHasHighScoreKey,
390                      &stored_high_score);
391     EXPECT_FALSE(stored_high_score);
392   }
393 }
394 
395 // Test that the has high score upper and lower thresholds work.
TEST_F(MediaEngagementScoreTest,HighScoreThreshold)396 TEST_F(MediaEngagementScoreTest, HighScoreThreshold) {
397   EXPECT_FALSE(score_->high_score());
398 
399   // Test that a total score of 0.1 is not high.
400   SetScore(20, 2);
401   EXPECT_FALSE(score_->high_score());
402 
403   // Test that a total score of 0.25 is not high but above zero.
404   SetScore(20, 5);
405   EXPECT_FALSE(score_->high_score());
406 
407   // Test that a total score of 0.3 is high.
408   SetScore(20, 6);
409   EXPECT_TRUE(score_->high_score());
410 
411   // Test that a total score of 0.25 is high because of the lower boundary.
412   SetScore(20, 5);
413   EXPECT_TRUE(score_->high_score());
414 
415   // Test that a total score of 0.1 is not high.
416   SetScore(20, 2);
417   EXPECT_FALSE(score_->high_score());
418 }
419 
TEST_F(MediaEngagementScoreTest,OverrideFieldTrial)420 TEST_F(MediaEngagementScoreTest, OverrideFieldTrial) {
421   EXPECT_EQ(20, MediaEngagementScore::GetScoreMinVisits());
422   EXPECT_EQ(0.2, MediaEngagementScore::GetHighScoreLowerThreshold());
423   EXPECT_EQ(0.3, MediaEngagementScore::GetHighScoreUpperThreshold());
424 
425   SetScore(20, 16);
426   EXPECT_EQ(0.8, score_->actual_score());
427   EXPECT_TRUE(score_->high_score());
428 
429   // Raise the upper threshold, since the score was already considered high we
430   // should still be high.
431   OverrideFieldTrial(5, 0.7, 0.9);
432   EXPECT_TRUE(score_->high_score());
433   EXPECT_EQ(0.7, MediaEngagementScore::GetHighScoreLowerThreshold());
434   EXPECT_EQ(0.9, MediaEngagementScore::GetHighScoreUpperThreshold());
435 
436   // Raise the lower threshold, the score will no longer be high.
437   OverrideFieldTrial(5, 0.85, 0.9);
438   EXPECT_FALSE(score_->high_score());
439   EXPECT_EQ(0.85, MediaEngagementScore::GetHighScoreLowerThreshold());
440 
441   // Raise the minimum visits, the score will now be relative to this new
442   // visits requirements.
443   OverrideFieldTrial(25, 0.85, 0.9);
444   EXPECT_EQ(0.64, score_->actual_score());
445   EXPECT_EQ(25, MediaEngagementScore::GetScoreMinVisits());
446 }
447 
448 // Test that scores are read / written correctly from / to populated score
449 // dictionaries.
TEST_F(MediaEngagementScoreTest,PopulatedDictionary_HTTPSOnly)450 TEST_F(MediaEngagementScoreTest, PopulatedDictionary_HTTPSOnly) {
451   base::test::ScopedFeatureList feature_list;
452   feature_list.InitAndEnableFeature(media::kMediaEngagementHTTPSOnly);
453 
454   std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
455   dict->SetInteger(MediaEngagementScore::kVisitsKey, 20);
456   dict->SetInteger(MediaEngagementScore::kMediaPlaybacksKey, 12);
457   dict->SetDouble(MediaEngagementScore::kLastMediaPlaybackTimeKey,
458                   test_clock.Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
459   dict->SetBoolean(MediaEngagementScore::kHasHighScoreKey, true);
460 
461   TestScoreInitializesAndUpdates(std::move(dict), 0, 0, base::Time(), false,
462                                  false);
463 }
464 
TEST_F(MediaEngagementScoreTest,DoNotStoreDeprecatedFields)465 TEST_F(MediaEngagementScoreTest, DoNotStoreDeprecatedFields) {
466   constexpr char kVisitsWithMediaTag[] = "visitsWithMediaTag";
467   constexpr char kAudiblePlaybacks[] = "audiblePlaybacks";
468   constexpr char kSignificantPlaybacks[] = "significantPlaybacks";
469   constexpr char kHighScoreChanges[] = "highScoreChanges";
470   constexpr char kMediaElementPlaybacks[] = "mediaElementPlaybacks";
471   constexpr char kAudioContextPlaybacks[] = "audioContextPlaybacks";
472 
473   // This field is not deprecated by it is not used by media engagement so it
474   // should not be deleted.
475   constexpr char kNotDeprectedUnknown[] = "unknown";
476 
477   url::Origin origin = url::Origin::Create(GURL("https://www.google.com"));
478   HostContentSettingsMap* settings_map =
479       HostContentSettingsMapFactory::GetForProfile(profile());
480   std::unique_ptr<base::DictionaryValue> score_dict =
481       std::make_unique<base::DictionaryValue>();
482 
483   // Store data with deprecated fields in content settings.
484   score_dict->SetInteger(kVisitsWithMediaTag, 10);
485   score_dict->SetInteger(kAudiblePlaybacks, 10);
486   score_dict->SetInteger(kSignificantPlaybacks, 10);
487   score_dict->SetInteger(kHighScoreChanges, 10);
488   score_dict->SetInteger(kMediaElementPlaybacks, 10);
489   score_dict->SetInteger(kAudioContextPlaybacks, 10);
490 
491   // These fields are not deprecated and should not be removed.
492   score_dict->SetInteger(MediaEngagementScore::kVisitsKey, 20);
493   score_dict->SetInteger(MediaEngagementScore::kMediaPlaybacksKey, 12);
494   score_dict->SetDouble(MediaEngagementScore::kLastMediaPlaybackTimeKey,
495                         test_clock.Now().ToInternalValue());
496   score_dict->SetBoolean(MediaEngagementScore::kHasHighScoreKey, true);
497   score_dict->SetInteger(kNotDeprectedUnknown, 10);
498   settings_map->SetWebsiteSettingDefaultScope(
499       origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
500       std::move(score_dict));
501 
502   // Run the data through media engagement score.
503   auto score =
504       std::make_unique<MediaEngagementScore>(&test_clock, origin, settings_map);
505   UpdateScore(score.get());
506   score->Commit();
507 
508   // Check the deprecated fields have been dropped.
509   std::unique_ptr<base::DictionaryValue> values =
510       base::DictionaryValue::From(settings_map->GetWebsiteSetting(
511           origin.GetURL(), GURL(), ContentSettingsType::MEDIA_ENGAGEMENT,
512           nullptr));
513   EXPECT_FALSE(values->HasKey(kVisitsWithMediaTag));
514   EXPECT_FALSE(values->HasKey(kAudiblePlaybacks));
515   EXPECT_FALSE(values->HasKey(kSignificantPlaybacks));
516   EXPECT_FALSE(values->HasKey(kHighScoreChanges));
517   EXPECT_FALSE(values->HasKey(kMediaElementPlaybacks));
518   EXPECT_FALSE(values->HasKey(kAudioContextPlaybacks));
519 
520   // Check the non-deprecated fields are still present.
521   EXPECT_TRUE(values->HasKey(MediaEngagementScore::kVisitsKey));
522   EXPECT_TRUE(values->HasKey(MediaEngagementScore::kMediaPlaybacksKey));
523   EXPECT_TRUE(values->HasKey(MediaEngagementScore::kLastMediaPlaybackTimeKey));
524   EXPECT_TRUE(values->HasKey(MediaEngagementScore::kHasHighScoreKey));
525   EXPECT_TRUE(values->HasKey(kNotDeprectedUnknown));
526 }
527