File: prefs_migrator_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (161 lines) | stat: -rw-r--r-- 7,167 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/segmentation_platform/internal/migration/prefs_migrator.h"

#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/segmentation_platform/internal/constants.h"
#include "components/segmentation_platform/internal/database/client_result_prefs.h"
#include "components/segmentation_platform/internal/metadata/metadata_utils.h"
#include "components/segmentation_platform/internal/metadata/metadata_writer.h"
#include "components/segmentation_platform/internal/migration/migration_test_utils.h"
#include "components/segmentation_platform/internal/selection/segmentation_result_prefs.h"
#include "components/segmentation_platform/public/config.h"
#include "components/segmentation_platform/public/constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::Invoke;
using testing::Return;
using testing::SaveArg;

namespace segmentation_platform {

class PrefsMigratorTest : public testing::Test {
 public:
  PrefsMigratorTest() = default;
  ~PrefsMigratorTest() override = default;

  void SetUp() override {
    old_result_prefs_ =
        std::make_unique<SegmentationResultPrefs>(&pref_service_);
    new_result_prefs_ = std::make_unique<ClientResultPrefs>(&pref_service_);
    pref_service_.registry()->RegisterStringPref(kSegmentationClientResultPrefs,
                                                 std::string());
    pref_service_.registry()->RegisterDictionaryPref(kSegmentationResultPref);
    configs_.push_back(migration_test_utils::GetTestConfigForBinaryClassifier(
        kShoppingUserSegmentationKey, kShoppingUserUmaName,
        proto::SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHOPPING_USER));
    configs_.push_back(migration_test_utils::GetTestConfigForAdaptiveToolbar());
  }

 protected:
  TestingPrefServiceSimple pref_service_;
  std::unique_ptr<ClientResultPrefs> new_result_prefs_;
  std::unique_ptr<SegmentationResultPrefs> old_result_prefs_;
  std::unique_ptr<PrefsMigrator> prefs_migrator_;
  std::vector<std::unique_ptr<Config>> configs_;
};

TEST_F(PrefsMigratorTest, PrefsMigratorForBinaryClassifier) {
  // Model with binary classifier when `segment_id` is selected.
  SelectedSegment result(
      SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHOPPING_USER, 1);
  result.selection_time = base::Time::Now() - base::Seconds(1);
  old_result_prefs_->SaveSegmentationResultToPref(kShoppingUserSegmentationKey,
                                                  result);
  prefs_migrator_ = std::make_unique<PrefsMigrator>(
      &pref_service_, new_result_prefs_.get(), configs_);

  prefs_migrator_->MigrateOldPrefsToNewPrefs();

  auto expected_output_config =
      migration_test_utils::GetTestOutputConfigForBinaryClassifier(
          proto::SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHOPPING_USER);
  const auto* result_in_new_prefs =
      new_result_prefs_->ReadClientResultFromPrefs(
          kShoppingUserSegmentationKey);

  EXPECT_TRUE(result_in_new_prefs);
  proto::PredictionResult client_result = result_in_new_prefs->client_result();
  EXPECT_EQ(expected_output_config.SerializeAsString(),
            client_result.output_config().SerializeAsString());
  EXPECT_THAT(client_result.result(), testing::ElementsAre(1));

  new_result_prefs_->SaveClientResultToPrefs(kShoppingUserSegmentationKey,
                                             *result_in_new_prefs);

  // Models with binary classifier when `segment_id` is not selected.
  SelectedSegment result1(SegmentId::OPTIMIZATION_TARGET_UNKNOWN, 0);
  result1.selection_time = base::Time::Now();
  old_result_prefs_->SaveSegmentationResultToPref(kShoppingUserSegmentationKey,
                                                  result1);
  prefs_migrator_ = std::make_unique<PrefsMigrator>(
      &pref_service_, new_result_prefs_.get(), configs_);
  prefs_migrator_->MigrateOldPrefsToNewPrefs();

  result_in_new_prefs = new_result_prefs_->ReadClientResultFromPrefs(
      kShoppingUserSegmentationKey);
  EXPECT_TRUE(result_in_new_prefs);
  client_result = result_in_new_prefs->client_result();
  EXPECT_EQ(expected_output_config.SerializeAsString(),
            client_result.output_config().SerializeAsString());
  EXPECT_THAT(client_result.result(), testing::ElementsAre(0));
}

TEST_F(PrefsMigratorTest, PrefsMigratorForAdaptiveToolbar) {
  // AdpativeToolbar model when new tab is selected.
  SelectedSegment result(SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_NEW_TAB,
                         1);
  result.selection_time = base::Time::Now();
  old_result_prefs_->SaveSegmentationResultToPref(
      kAdaptiveToolbarSegmentationKey, result);
  prefs_migrator_ = std::make_unique<PrefsMigrator>(
      &pref_service_, new_result_prefs_.get(), configs_);

  prefs_migrator_->MigrateOldPrefsToNewPrefs();

  auto expected_output_config =
      migration_test_utils::GetTestOutputConfigForAdaptiveToolbar();
  const auto* result_in_new_prefs =
      new_result_prefs_->ReadClientResultFromPrefs(
          kAdaptiveToolbarSegmentationKey);

  EXPECT_TRUE(result_in_new_prefs);
  proto::PredictionResult client_result = result_in_new_prefs->client_result();
  EXPECT_EQ(expected_output_config.SerializeAsString(),
            client_result.output_config().SerializeAsString());
  EXPECT_THAT(client_result.result(), testing::ElementsAre(1, 0, 0, 0, 0));

  new_result_prefs_->SaveClientResultToPrefs(kAdaptiveToolbarSegmentationKey,
                                             *result_in_new_prefs);

  // AdpativeToolbar model when share is selected.
  SelectedSegment result1(SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHARE, 1);
  result1.selection_time = base::Time::Now();
  old_result_prefs_->SaveSegmentationResultToPref(
      kAdaptiveToolbarSegmentationKey, result1);
  prefs_migrator_ = std::make_unique<PrefsMigrator>(
      &pref_service_, new_result_prefs_.get(), configs_);

  prefs_migrator_->MigrateOldPrefsToNewPrefs();

  result_in_new_prefs = new_result_prefs_->ReadClientResultFromPrefs(
      kAdaptiveToolbarSegmentationKey);

  EXPECT_TRUE(result_in_new_prefs);
  client_result = result_in_new_prefs->client_result();
  EXPECT_EQ(expected_output_config.SerializeAsString(),
            client_result.output_config().SerializeAsString());
  EXPECT_THAT(client_result.result(), testing::ElementsAre(0, 1, 0, 0, 0));
}

TEST_F(PrefsMigratorTest, PrefsMigratorForOtherConfig) {
  SelectedSegment result(
      SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SEARCH_USER, 1);
  result.selection_time = base::Time::Now();
  old_result_prefs_->SaveSegmentationResultToPref(kSearchUserKey, result);
  prefs_migrator_ = std::make_unique<PrefsMigrator>(
      &pref_service_, new_result_prefs_.get(), configs_);

  prefs_migrator_->MigrateOldPrefsToNewPrefs();

  const auto* result_in_new_prefs =
      new_result_prefs_->ReadClientResultFromPrefs(kSearchUserKey);
  EXPECT_FALSE(result_in_new_prefs);
}

}  // namespace segmentation_platform