File: segmentation_platform_service_test_base.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (171 lines) | stat: -rw-r--r-- 6,878 bytes parent folder | download | duplicates (9)
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
162
163
164
165
166
167
168
169
170
171
// Copyright 2022 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/segmentation_platform_service_test_base.h"

#include <string_view>

#include "base/test/test_simple_task_runner.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/segmentation_platform/internal/constants.h"
#include "components/segmentation_platform/internal/database/segment_info_database.h"
#include "components/segmentation_platform/internal/execution/mock_model_provider.h"
#include "components/segmentation_platform/internal/segmentation_platform_service_impl.h"
#include "components/segmentation_platform/internal/ukm_data_manager.h"
#include "components/segmentation_platform/public/config.h"
#include "components/segmentation_platform/public/field_trial_register.h"
#include "components/sync_device_info/device_info_tracker.h"
#include "components/sync_device_info/fake_device_info_tracker.h"

namespace segmentation_platform {

namespace {

using syncer::DeviceInfoTracker;

class MockFieldTrialRegister : public FieldTrialRegister {
 public:
  MOCK_METHOD2(RegisterFieldTrial,
               void(std::string_view trial_name, std::string_view group_name));
  MOCK_METHOD3(RegisterSubsegmentFieldTrialIfNeeded,
               void(std::string_view trial_name,
                    proto::SegmentId segment_id,
                    int subsegment_rank));
};

std::vector<std::unique_ptr<Config>> CreateTestConfigs() {
  std::vector<std::unique_ptr<Config>> configs;
  {
    std::unique_ptr<Config> config = std::make_unique<Config>();
    config->segmentation_key = kTestSegmentationKey1;
    config->segment_selection_ttl = base::Days(28);
    config->AddSegmentId(SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_NEW_TAB);
    config->AddSegmentId(SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHARE);
    configs.push_back(std::move(config));
  }
  {
    std::unique_ptr<Config> config = std::make_unique<Config>();
    config->segmentation_key = kTestSegmentationKey2;
    config->segment_selection_ttl = base::Days(10);
    config->AddSegmentId(SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHARE);
    config->AddSegmentId(SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_VOICE);
    configs.push_back(std::move(config));
  }
  {
    std::unique_ptr<Config> config = std::make_unique<Config>();
    config->segmentation_key = kTestSegmentationKey3;
    config->segment_selection_ttl = base::Days(14);
    config->AddSegmentId(SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_NEW_TAB);
    configs.push_back(std::move(config));
  }
  {
    std::unique_ptr<Config> config = std::make_unique<Config>();
    config->segmentation_key = kTestSegmentationKey4;
    config->segment_selection_ttl = base::Days(14);
    config->AddSegmentId(
        SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHOPPING_USER);
    config->auto_execute_and_cache = false;
    configs.push_back(std::move(config));
  }
  {
    // Empty config.
    std::unique_ptr<Config> config = std::make_unique<Config>();
    config->segmentation_key = "test_key";
    configs.push_back(std::move(config));
  }

  return configs;
}

}  // namespace

constexpr char kTestSegmentationKey1[] = "test_key1";
constexpr char kTestSegmentationKey2[] = "test_key2";
constexpr char kTestSegmentationKey3[] = "test_key3";
constexpr char kTestSegmentationKey4[] = "test_key4";
constexpr char kTestProfileId[] = "test_id";

SegmentationPlatformServiceTestBase::SegmentationPlatformServiceTestBase() {
  device_info_tracker_ = std::make_unique<syncer::FakeDeviceInfoTracker>();
}

SegmentationPlatformServiceTestBase::~SegmentationPlatformServiceTestBase() =
    default;

void SegmentationPlatformServiceTestBase::InitPlatform(
    UkmDataManager* ukm_data_manager,
    history::HistoryService* history_service) {
  task_runner_ = base::MakeRefCounted<base::TestSimpleTaskRunner>();

  auto segment_db =
      std::make_unique<leveldb_proto::test::FakeDB<proto::SegmentInfo>>(
          &segment_db_entries_);
  auto signal_db =
      std::make_unique<leveldb_proto::test::FakeDB<proto::SignalData>>(
          &signal_db_entries_);
  auto segment_storage_config_db = std::make_unique<
      leveldb_proto::test::FakeDB<proto::SignalStorageConfigs>>(
      &segment_storage_config_db_entries_);
  segment_db_ = segment_db.get();
  signal_db_ = signal_db.get();
  segment_storage_config_db_ = segment_storage_config_db.get();
  auto model_provider_factory =
      std::make_unique<TestModelProviderFactory>(&model_provider_data_);

  SegmentationPlatformService::RegisterProfilePrefs(pref_service_.registry());
  SetUpPrefs();

  std::vector<std::unique_ptr<Config>> configs = CreateTestConfigs();
  base::flat_set<SegmentId> all_segment_ids;
  for (const auto& config : configs) {
    for (const auto& segment_id : config->segments)
      all_segment_ids.insert(segment_id.first);
  }
  auto storage_service = std::make_unique<StorageService>(
      std::move(segment_db), std::move(signal_db),
      std::move(segment_storage_config_db), task_runner_, &test_clock_,
      ukm_data_manager, std::move(configs), model_provider_factory.get(),
      &pref_service_, "profile_id", base::DoNothing());

  auto params = std::make_unique<SegmentationPlatformServiceImpl::InitParams>();
  params->profile_id = kTestProfileId;
  params->storage_service = std::move(storage_service);
  params->model_provider = std::move(model_provider_factory);
  params->profile_prefs = &pref_service_;
  params->history_service = history_service;
  params->task_runner = task_runner_;
  params->clock = &test_clock_;
  params->field_trial_register = std::make_unique<MockFieldTrialRegister>();
  params->device_info_tracker = device_info_tracker_.get();
  params->input_delegate_holder =
      std::make_unique<processing::InputDelegateHolder>();
  segmentation_platform_service_impl_ =
      std::make_unique<SegmentationPlatformServiceImpl>(std::move(params));
}

void SegmentationPlatformServiceTestBase::DestroyPlatform() {
  segmentation_platform_service_impl_.reset();
  // Allow for the SegmentationModelExecutor owned by SegmentationModelHandler
  // to be destroyed.
  task_runner_->RunUntilIdle();
}

void SegmentationPlatformServiceTestBase::SetUpPrefs() {
  ScopedDictPrefUpdate update(&pref_service_, kSegmentationResultPref);
  base::Value::Dict& dictionary = update.Get();

  base::Value::Dict segmentation_result;
  segmentation_result.Set("segment_id",
                          SegmentId::OPTIMIZATION_TARGET_SEGMENTATION_SHARE);
  dictionary.Set(kTestSegmentationKey1, std::move(segmentation_result));
}

std::vector<std::unique_ptr<Config>>
SegmentationPlatformServiceTestBase::CreateConfigs() {
  return CreateTestConfigs();
}

}  // namespace segmentation_platform