File: autocomplete_match_test_util.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 (244 lines) | stat: -rw-r--r-- 9,902 bytes parent folder | download | duplicates (5)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2025 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/omnibox/browser/autocomplete_match_test_util.h"

#include "base/json/json_reader.h"
#include "base/strings/utf_string_conversions.h"
#include "components/omnibox/browser/actions/contextual_search_action.h"
#include "components/omnibox/browser/actions/omnibox_action_in_suggest.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/omnibox_proto/rich_answer_template.pb.h"
#include "url/gurl.h"

namespace {

bool ParseAnswer(const std::string& answer_json,
                 omnibox::AnswerType answer_type,
                 omnibox::RichAnswerTemplate* answer) {
  std::optional<base::Value::Dict> value =
      base::JSONReader::ReadDict(answer_json);
  if (!value) {
    return false;
  }

  return omnibox::answer_data_parser::ParseJsonToAnswerData(*value, answer);
}

}  // namespace

AutocompleteMatch CreateHistoryURLMatch(std::string destination_url,
                                        bool is_zero_prefix) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::HISTORY_URL;
  match.destination_url = GURL(destination_url);
  if (is_zero_prefix) {
    match.subtypes.insert(omnibox::SUBTYPE_ZERO_PREFIX);
  }
  return match;
}

AutocompleteMatch CreateCompanyEntityMatch(std::string website_uri) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::SEARCH_SUGGEST_ENTITY;
  match.website_uri = website_uri;
  match.image_url = GURL("https://url");
  match.image_dominant_color = "#000000";
  return match;
}

AutocompleteMatch CreateSearchMatch(std::u16string contents) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::SEARCH_SUGGEST;
  match.contents = contents;
  return match;
}

AutocompleteMatch CreateContextualSearchMatch(std::u16string contents) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::SEARCH_SUGGEST;
  match.contents = contents;
  match.relevance = 195;
  match.contents_class = {{0, 1}};
  match.keyword = u"contextual";
  match.suggestion_group_id = omnibox::GroupId::GROUP_CONTEXTUAL_SEARCH;
  match.subtypes.insert(omnibox::SUBTYPE_CONTEXTUAL_SEARCH);
  return match;
}

AutocompleteMatch CreateLensActionMatch(std::u16string contents) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::PEDAL;
  match.contents = contents;
  match.contents_class = {{0, 1}};
  match.keyword = u"lens";
  match.relevance = omnibox::kContextualActionZeroSuggestRelevance;
  match.suggestion_group_id = omnibox::GroupId::GROUP_CONTEXTUAL_SEARCH_ACTION;
  match.takeover_action =
      base::MakeRefCounted<ContextualSearchOpenLensAction>();
  return match;
}

AutocompleteMatch CreateZeroPrefixSearchMatch(std::u16string contents) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::SEARCH_SUGGEST;
  match.contents = contents;
  match.subtypes.insert(omnibox::SUBTYPE_ZERO_PREFIX);
  return match;
}

AutocompleteMatch CreateStarterPackMatch(std::u16string keyword) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::STARTER_PACK;
  match.contents = keyword;
  match.keyword = keyword;
  match.associated_keyword = std::make_unique<AutocompleteMatch>(
      nullptr, 1000, false, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED);
  match.associated_keyword->keyword = keyword;
  return match;
}

AutocompleteMatch CreateFeaturedEnterpriseSearch(std::u16string keyword) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::FEATURED_ENTERPRISE_SEARCH;
  match.contents = keyword;
  match.keyword = keyword;
  match.associated_keyword = std::make_unique<AutocompleteMatch>(
      nullptr, 1000, false, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED);
  match.associated_keyword->keyword = keyword;
  return match;
}

AutocompleteMatch CreateActionInSuggestMatch(
    std::u16string description,
    std::vector<omnibox::ActionInfo::ActionType> action_types) {
  AutocompleteMatch match;
  match.type = AutocompleteMatchType::Type::SEARCH_SUGGEST_ENTITY;
  match.description = description;
  for (auto action_type : action_types) {
    omnibox::ActionInfo action_info;
    action_info.set_action_type(action_type);
    match.actions.push_back(base::MakeRefCounted<OmniboxActionInSuggest>(
        std::move(action_info), std::nullopt));
  }
  return match;
}

AutocompleteMatch CreateSearchMatch(std::string name,
                                    bool allowed_to_be_default_match,
                                    int traditional_relevance) {
  auto match = CreateAutocompleteMatch(
      name, AutocompleteMatchType::SEARCH_SUGGEST, allowed_to_be_default_match,
      false, traditional_relevance, std::nullopt);
  match.keyword = u"keyword";
  match.search_terms_args = std::make_unique<TemplateURLRef::SearchTermsArgs>(
      base::UTF8ToUTF16(name));
  return match;
}

AutocompleteMatch CreatePersonalizedZeroPrefixMatch(std::string name,
                                                    int traditional_relevance) {
  auto match = CreateAutocompleteMatch(
      name, AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED, false, false,
      traditional_relevance, std::nullopt);
  match.keyword = u"keyword";
  match.search_terms_args =
      std::make_unique<TemplateURLRef::SearchTermsArgs>(std::u16string());
  match.suggestion_group_id = omnibox::GROUP_PERSONALIZED_ZERO_SUGGEST;
  match.subtypes.emplace(omnibox::SUBTYPE_PERSONAL);
  match.subtypes.emplace(omnibox::SUBTYPE_ZERO_PREFIX);
  return match;
}

AutocompleteMatch CreateHistoryUrlMlScoredMatch(
    std::string name,
    bool allowed_to_be_default_match,
    int traditional_relevance,
    float ml_output) {
  return CreateMlScoredMatch(name, AutocompleteMatchType::HISTORY_URL,
                             allowed_to_be_default_match, traditional_relevance,
                             ml_output);
}

AutocompleteMatch CreateAnswerMlScoredMatch(std::string name,
                                            omnibox::AnswerType answer_type,
                                            std::string answer_json,
                                            bool allowed_to_be_default_match,
                                            int traditional_relevance,
                                            float ml_output) {
  AutocompleteMatch match = CreateSearchMlScoredMatch(
      name, allowed_to_be_default_match, traditional_relevance, ml_output);
  match.answer_type = answer_type;
  omnibox::RichAnswerTemplate answer;
  EXPECT_TRUE(ParseAnswer(answer_json, match.answer_type, &answer));
  match.answer_template = answer;
  return match;
}

AutocompleteMatch CreateSearchMlScoredMatch(std::string name,
                                            bool allowed_to_be_default_match,
                                            int traditional_relevance,
                                            float ml_output) {
  AutocompleteMatch match = CreateMlScoredMatch(
      name, AutocompleteMatchType::SEARCH_SUGGEST, allowed_to_be_default_match,
      traditional_relevance, ml_output);
  match.keyword = u"keyword";
  match.search_terms_args = std::make_unique<TemplateURLRef::SearchTermsArgs>(
      base::UTF8ToUTF16(name));
  return match;
}

AutocompleteMatch CreateMlScoredMatch(std::string name,
                                      AutocompleteMatchType::Type type,
                                      bool allowed_to_be_default_match,
                                      int traditional_relevance,
                                      float ml_output) {
  return CreateAutocompleteMatch(name, type, allowed_to_be_default_match, false,
                                 traditional_relevance, ml_output);
}

AutocompleteMatch CreateBoostedShortcutMatch(std::string name,
                                             int traditional_relevance,
                                             float ml_output) {
  return CreateAutocompleteMatch(name, AutocompleteMatchType::HISTORY_URL, true,
                                 true, traditional_relevance, ml_output);
}

AutocompleteMatch CreateKeywordHintMatch(std::string name,
                                         int traditional_relevance) {
  auto match = CreateAutocompleteMatch(
      name, AutocompleteMatchType::SEARCH_SUGGEST, false, false,
      traditional_relevance, std::nullopt);
  match.keyword = u"keyword";
  match.associated_keyword = std::make_unique<AutocompleteMatch>(
      nullptr, 1000, false, AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED);
  return match;
}

AutocompleteMatch CreateHistoryClusterMatch(std::string name,
                                            int traditional_relevance) {
  return CreateAutocompleteMatch(name, AutocompleteMatchType::HISTORY_CLUSTER,
                                 false, false, traditional_relevance,
                                 std::nullopt);
}

AutocompleteMatch CreateAutocompleteMatch(std::string name,
                                          AutocompleteMatchType::Type type,
                                          bool allowed_to_be_default_match,
                                          bool shortcut_boosted,
                                          int traditional_relevance,
                                          std::optional<float> ml_output) {
  AutocompleteMatch match{nullptr, traditional_relevance, false, type};
  match.shortcut_boosted = shortcut_boosted;
  match.allowed_to_be_default_match = allowed_to_be_default_match;
  match.destination_url = GURL{"https://google.com/" + name};
  match.stripped_destination_url = GURL{"https://google.com/" + name};
  match.contents = base::UTF8ToUTF16(name);
  match.contents_class = {{0, 1}};
  if (ml_output.has_value()) {
    match.scoring_signals = {{}};
    match.scoring_signals->set_site_engagement(ml_output.value());
  }
  return match;
}