File: omnibox_action_in_suggest_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (252 lines) | stat: -rw-r--r-- 10,206 bytes parent folder | download | duplicates (6)
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
245
246
247
248
249
250
251
252
// 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/omnibox/browser/actions/omnibox_action_in_suggest.h"

#include "base/memory/scoped_refptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "components/omnibox/browser/actions/omnibox_pedal_provider.h"
#include "components/omnibox/browser/mock_autocomplete_provider_client.h"
#include "components/strings/grit/components_strings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/omnibox_proto/entity_info.pb.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/window_open_disposition.h"
#include "url/gurl.h"

using omnibox::ActionInfo;
using ActionType = omnibox::ActionInfo_ActionType;

namespace {
class FakeOmniboxAction : public OmniboxAction {
 public:
  explicit FakeOmniboxAction(OmniboxActionId id)
      : OmniboxAction(LabelStrings(u"", u"", u"", u""), GURL{}), id_(id) {}
  OmniboxActionId ActionId() const override { return id_; }

 private:
  ~FakeOmniboxAction() override = default;
  OmniboxActionId id_{};
};

// Note: can't use operator<<, because ActionType is a plain old enum.
const char* ToString(ActionType type) {
  switch (type) {
    case omnibox::ActionInfo_ActionType_CALL:
      return "Call";
    case omnibox::ActionInfo_ActionType_DIRECTIONS:
      return "Directions";
    case omnibox::ActionInfo_ActionType_REVIEWS:
      return "Reviews";
    default:
      NOTREACHED();
  }
}
}  // namespace

class OmniboxActionInSuggestTest : public testing::Test {
 public:
  OmniboxActionInSuggestTest();

 protected:
  MockAutocompleteProviderClient client_;
  OmniboxAction::ExecutionContext context_;
};

OmniboxActionInSuggestTest::OmniboxActionInSuggestTest()
    : context_(client_,
               OmniboxAction::ExecutionContext::OpenUrlCallback(),
               base::TimeTicks(),
               WindowOpenDisposition::IGNORE_ACTION) {}

TEST_F(OmniboxActionInSuggestTest, CheckLabelsArePresentForKnownTypes) {
  struct {
    ActionType action_type;
    int want_hint;
    int want_contents;
    int want_accessibility_focus_hint;
    int want_accessibility_activate_hint;
  } test_cases[] = {{
                        omnibox::ActionInfo_ActionType_CALL,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_CALL_HINT,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_CALL_CONTENTS,
                        IDS_ACC_OMNIBOX_ACTION_IN_SUGGEST_SUFFIX,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_CALL_CONTENTS,
                    },
                    {
                        omnibox::ActionInfo_ActionType_DIRECTIONS,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_DIRECTIONS_HINT,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_DIRECTIONS_CONTENTS,
                        IDS_ACC_OMNIBOX_ACTION_IN_SUGGEST_SUFFIX,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_DIRECTIONS_CONTENTS,
                    },
                    {
                        omnibox::ActionInfo_ActionType_REVIEWS,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_REVIEWS_HINT,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_REVIEWS_CONTENTS,
                        IDS_ACC_OMNIBOX_ACTION_IN_SUGGEST_SUFFIX,
                        IDS_OMNIBOX_ACTION_IN_SUGGEST_REVIEWS_CONTENTS,
                    }};

  for (const auto& test_case : test_cases) {
    ActionInfo action_info;
    action_info.set_action_type(test_case.action_type);

    auto action = base::MakeRefCounted<OmniboxActionInSuggest>(
        std::move(action_info), std::nullopt);
    EXPECT_EQ(OmniboxActionId::ACTION_IN_SUGGEST, action->ActionId())
        << "while evaluatin action " << ToString(test_case.action_type);
    EXPECT_EQ(test_case.action_type, action->Type());

    const auto& labels = action->GetLabelStrings();
    EXPECT_EQ(labels.hint, l10n_util::GetStringUTF16(test_case.want_hint))
        << "while evaluatin action " << ToString(test_case.action_type);
    EXPECT_EQ(labels.suggestion_contents,
              l10n_util::GetStringUTF16(test_case.want_contents))
        << "while evaluatin action " << ToString(test_case.action_type);
    EXPECT_EQ(
        labels.accessibility_suffix,
        l10n_util::GetStringUTF16(test_case.want_accessibility_focus_hint))
        << "while evaluatin action " << ToString(test_case.action_type);
    EXPECT_EQ(
        labels.accessibility_hint,
        l10n_util::GetStringUTF16(test_case.want_accessibility_activate_hint))
        << "while evaluatin action " << ToString(test_case.action_type);
  }
}

TEST_F(OmniboxActionInSuggestTest, ConversionFromAction) {
  const ActionType test_cases[]{omnibox::ActionInfo_ActionType_CALL,
                                omnibox::ActionInfo_ActionType_DIRECTIONS,
                                omnibox::ActionInfo_ActionType_REVIEWS};

  for (auto test_case : test_cases) {
    ActionInfo action_info;
    action_info.set_action_type(test_case);

    scoped_refptr<OmniboxAction> upcasted_action =
        base::MakeRefCounted<OmniboxActionInSuggest>(std::move(action_info),
                                                     std::nullopt);

    auto* downcasted_action =
        OmniboxActionInSuggest::FromAction(upcasted_action.get());

    EXPECT_EQ(upcasted_action.get(), downcasted_action)
        << "while evaluatin action " << ToString(test_case);
    EXPECT_EQ(test_case, downcasted_action->Type())
        << "while evaluatin action " << ToString(test_case);
  }
}

TEST_F(OmniboxActionInSuggestTest, ConversionFromNonAction) {
  const OmniboxActionId test_cases[]{OmniboxActionId::HISTORY_CLUSTERS,
                                     OmniboxActionId::PEDAL,
                                     OmniboxActionId::TAB_SWITCH};

  for (auto test_case : test_cases) {
    auto fake_action = base::MakeRefCounted<FakeOmniboxAction>(test_case);
    EXPECT_EQ(nullptr, OmniboxActionInSuggest::FromAction(fake_action.get()));
  }
}

TEST_F(OmniboxActionInSuggestTest, AllDeclaredActionTypesAreProperlyReflected) {
  // This test verifies that we're not quietly migrating new action types, and
  // failing to recognize the need for appropriate coverage, both in terms of
  // labels (hints, accessibility) but also UMA metrics.
  for (int type = ActionInfo::ActionType_MIN;
       type <= ActionInfo::ActionType_MAX; type++) {
    if (omnibox::ActionInfo_ActionType_IsValid(type)) {
      ActionInfo action_info;
      action_info.set_action_type(ActionType(type));

      // This is a valid action type. Object MUST build.
      auto action = base::MakeRefCounted<OmniboxActionInSuggest>(
          std::move(action_info), std::nullopt);
      // This is a valid action type. Object MUST be able to report metrics.
      {
        base::HistogramTester histograms;
        action->RecordActionShown(1, false);
        // NO actions report to the 'Unknown' bucket.
        histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Shown", 0, 0);
        histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Used", 0, 0);
        histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Shown", 1);
        histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Used", 0);
      }

      {
        base::HistogramTester histograms;
        action->RecordActionShown(1, true);
        // NO actions report to the 'Unknown' bucket.
        histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Shown", 0, 0);
        histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Used", 0, 0);
        histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Shown", 1);
        histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Used", 1);
      }
    }
  }
}

TEST_F(OmniboxActionInSuggestTest, HistogramsRecording) {
  enum class UmaTypeForTest {
    kUnknown = 0,
    kCall,
    kDirections,
    kWebsite,
    kReviews,
  };

  // Correlation between ActionType and UMA-recorded bucket.
  struct {
    omnibox::ActionInfo::ActionType type;
    UmaTypeForTest recordedUmaType;
    const char* dedicatedHistogram;
  } test_cases[]{
      {omnibox::ActionInfo_ActionType_CALL, UmaTypeForTest::kCall,
       "Omnibox.ActionInSuggest.UsageByType.Call"},
      {omnibox::ActionInfo_ActionType_DIRECTIONS, UmaTypeForTest::kDirections,
       "Omnibox.ActionInSuggest.UsageByType.Directions"},
      {omnibox::ActionInfo_ActionType_REVIEWS, UmaTypeForTest::kReviews,
       "Omnibox.ActionInSuggest.UsageByType.Reviews"},
  };

  for (const auto& test_case : test_cases) {
    ActionInfo action_info;
    action_info.set_action_type(test_case.type);
    scoped_refptr<OmniboxAction> action =
        base::MakeRefCounted<OmniboxActionInSuggest>(std::move(action_info),
                                                     std::nullopt);

    {
      // Just show.
      base::HistogramTester histograms;
      action->RecordActionShown(1, false);
      histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Shown",
                                   test_case.recordedUmaType, 1);
      histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Used",
                                   test_case.recordedUmaType, 0);
      histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Shown", 1);
      histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Used", 0);

      histograms.ExpectBucketCount(test_case.dedicatedHistogram, false, 1);
      histograms.ExpectTotalCount(test_case.dedicatedHistogram, 1);
    }

    {
      // Show and execute.
      base::HistogramTester histograms;
      action->RecordActionShown(1, true);
      histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Shown",
                                   test_case.recordedUmaType, 1);
      histograms.ExpectBucketCount("Omnibox.ActionInSuggest.Used",
                                   test_case.recordedUmaType, 1);
      histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Shown", 1);
      histograms.ExpectTotalCount("Omnibox.ActionInSuggest.Used", 1);

      histograms.ExpectBucketCount(test_case.dedicatedHistogram, true, 1);
      histograms.ExpectTotalCount(test_case.dedicatedHistogram, 1);
    }
  }
}