File: merchant_trust_service_unittest.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 (477 lines) | stat: -rw-r--r-- 18,511 bytes parent folder | download | duplicates (3)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2024 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/page_info/core/merchant_trust_service.h"

#include <string_view>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "base/test/task_environment.h"
#include "components/commerce/core/proto/merchant_trust.pb.h"
#include "components/optimization_guide/core/hints/mock_optimization_guide_decider.h"
#include "components/optimization_guide/core/hints/optimization_guide_decision.h"
#include "components/optimization_guide/core/hints/optimization_metadata.h"
#include "components/optimization_guide/core/optimization_guide_proto_util.h"
#include "components/optimization_guide/proto/common_types.pb.h"
#include "components/page_info/core/features.h"
#include "components/page_info/core/merchant_trust_validation.h"
#include "components/page_info/core/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace page_info {
using testing::_;
using testing::An;
using testing::Invoke;
using testing::Return;

using DecisionWithMetadata = MerchantTrustService::DecisionAndMetadata;
using optimization_guide::AnyWrapProto;
using optimization_guide::MockOptimizationGuideDecider;
using optimization_guide::OptimizationGuideDecision;
using optimization_guide::OptimizationGuideDecisionCallback;
using optimization_guide::OptimizationMetadata;
using optimization_guide::proto::OptimizationType;
using MerchantTrustStatus = merchant_trust_validation::MerchantTrustStatus;

namespace {
const char kTestSummary[] = "This is a test summary.";

commerce::MerchantTrustSignalsV2 CreateValidProto() {
  commerce::MerchantTrustSignalsV2 proto;
  proto.set_merchant_star_rating(3.8);
  proto.set_merchant_count_rating(45);
  proto.set_merchant_details_page_url("https://page_url.com");
  proto.set_shopper_voice_summary(kTestSummary);
  return proto;
}

OptimizationMetadata BuildMerchantTrustResponse() {
  OptimizationMetadata meta;
  meta.set_any_metadata(AnyWrapProto(CreateValidProto()));
  return meta;
}

}  // namespace

class MockMerchantTrustServiceDelegate : public MerchantTrustService::Delegate {
 public:
  MOCK_METHOD(void, ShowEvaluationSurvey, (), (override));
  MOCK_METHOD(double, GetSiteEngagementScore, (const GURL url), (override));
};

class MockMerchantTrustService : public MerchantTrustService {
 public:
  explicit MockMerchantTrustService(
      optimization_guide::OptimizationGuideDecider* optimization_guide_decider,
      std::unique_ptr<MockMerchantTrustServiceDelegate> delegate,
      PrefService* prefs)
      : MerchantTrustService(std::move(delegate),
                             optimization_guide_decider,
                             /*is_off_the_record=*/false,
                             prefs) {}

  MOCK_METHOD(bool, IsOptimizationGuideAllowed, (), (const, override));
};

class MerchantTrustServiceTest : public ::testing::Test {
 public:
  MerchantTrustServiceTest() {
    page_info::MerchantTrustService::RegisterProfilePrefs(prefs()->registry());
  }

  void SetUp() override {
    auto delegate = std::make_unique<MockMerchantTrustServiceDelegate>();
    delegate_ = delegate.get();
    service_ = std::make_unique<MockMerchantTrustService>(
        &opt_guide(), std::move(delegate), prefs());
    SetOptimizationGuideAllowed(true);
    SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kUnknown,
                BuildMerchantTrustResponse());
    clock_.SetNow(base::Time::Now());
    service_->SetClockForTesting(&clock_);
  }

  // Setup optimization guide to return the given decision and metadata for the
  // given URL and optimization type.
  void SetResponse(const GURL& url,
                   OptimizationGuideDecision decision,
                   const OptimizationMetadata& metadata) {
    ON_CALL(
        opt_guide(),
        CanApplyOptimization(
            _, _, An<optimization_guide::OptimizationGuideDecisionCallback>()))
        .WillByDefault(
            Invoke([decision, metadata](
                       const GURL& url, OptimizationType optimization_type,
                       optimization_guide::OptimizationGuideDecisionCallback
                           callback) {
              std::move(callback).Run(decision, metadata);
            }));
  }

  void SetOptimizationGuideAllowed(bool allowed) {
    EXPECT_CALL(*service(), IsOptimizationGuideAllowed())
        .WillRepeatedly(Return(allowed));
  }

  MockMerchantTrustService* service() { return service_.get(); }
  optimization_guide::MockOptimizationGuideDecider& opt_guide() {
    return opt_guide_;
  }
  MockMerchantTrustServiceDelegate* delegate() { return delegate_; }

  sync_preferences::TestingPrefServiceSyncable* prefs() { return &prefs_; }

  base::SimpleTestClock* clock() { return &clock_; }

 private:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  testing::NiceMock<optimization_guide::MockOptimizationGuideDecider>
      opt_guide_;
  std::unique_ptr<MockMerchantTrustService> service_;
  raw_ptr<MockMerchantTrustServiceDelegate> delegate_;
  sync_preferences::TestingPrefServiceSyncable prefs_;
  base::SimpleTestClock clock_;
};

// Tests that proto are returned correctly when optimization guide decision is
// true
TEST_F(MerchantTrustServiceTest, OptimizationGuideDecisionTrue) {
  SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kTrue,
              BuildMerchantTrustResponse());

  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_TRUE(info.has_value());
            ASSERT_EQ(info->page_url, GURL("https://page_url.com"));
            ASSERT_EQ(info->reviews_summary, kTestSummary);
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
}

// Tests that proto are not returned correctly when optimization guide decision
// is unknown
TEST_F(MerchantTrustServiceTest, OptimizationGuideDecisionUnknown) {
  SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kUnknown,
              BuildMerchantTrustResponse());

  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_FALSE(info.has_value());
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
}

// Tests with optimization guide not allowed
TEST_F(MerchantTrustServiceTest, NoOptimizationGuideNotAllowed) {
  SetOptimizationGuideAllowed(false);
  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_FALSE(info.has_value());
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
}

// Tests that sample data is returned when optimization guide decision is
// unknown and sample data is enabled.
TEST_F(MerchantTrustServiceTest, SampleData) {
  base::test::ScopedFeatureList features;
  features.InitAndEnableFeatureWithParameters(
      kMerchantTrust, {{kMerchantTrustEnabledWithSampleData.name, "true"}});

  SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kUnknown,
              BuildMerchantTrustResponse());

  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_TRUE(info.has_value());
            ASSERT_EQ(info->page_url,
                      GURL("https://customerreviews.google.com/v/"
                           "merchant?q=amazon.com&c=US&gl=US"));
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
}

// Tests that if the proto is empty, no data is returned.
TEST_F(MerchantTrustServiceTest, NoResult) {
  base::HistogramTester t;
  OptimizationMetadata metadata;
  metadata.set_any_metadata({});
  SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kTrue,
              metadata);

  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_FALSE(info.has_value());
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
  t.ExpectUniqueSample("Security.PageInfo.MerchantTrustStatus",
                       MerchantTrustStatus::kNoResult, 1);
}

// Tests that status is recorded as not valid when a proto is missing a field
// and no data is returned.
TEST_F(MerchantTrustServiceTest, InvalidProto) {
  base::HistogramTester t;

  commerce::MerchantTrustSignalsV2 proto = CreateValidProto();
  proto.clear_merchant_details_page_url();
  OptimizationMetadata metadata;
  metadata.set_any_metadata(AnyWrapProto(proto));

  SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kTrue,
              metadata);

  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_FALSE(info.has_value());
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
  t.ExpectUniqueSample("Security.PageInfo.MerchantTrustStatus",
                       MerchantTrustStatus::kMissingReviewsPageUrl, 1);
}

// Tests that status is recorded as valid with missing reviews summary when a
// proto is missing the reviews summary field and the feature param is enabled.
TEST_F(MerchantTrustServiceTest, ValidProtoMissingReviewsSummary) {
  base::test::ScopedFeatureList features;
  features.InitAndEnableFeatureWithParameters(
      kMerchantTrust, {{kMerchantTrustWithoutSummaryName, "true"}});
  base::HistogramTester t;

  commerce::MerchantTrustSignalsV2 proto = CreateValidProto();
  proto.clear_shopper_voice_summary();
  OptimizationMetadata metadata;
  metadata.set_any_metadata(AnyWrapProto(proto));

  SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kTrue,
              metadata);

  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_TRUE(info.has_value());
            ASSERT_EQ(info->page_url, GURL("https://page_url.com"));
            ASSERT_EQ(info->reviews_summary, "");
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
  t.ExpectUniqueSample("Security.PageInfo.MerchantTrustStatus",
                       MerchantTrustStatus::kValidWithMissingReviewsSummary, 1);
}

// Tests that status is recorded as valid with missing reviews summary but not
// returning any data when a proto is missing the reviews summary field and the
// feature param is disabled.
TEST_F(MerchantTrustServiceTest, ValidProtoMissingReviewsSummaryNoData) {
  base::test::ScopedFeatureList features;
  features.InitAndEnableFeatureWithParameters(
      kMerchantTrust, {{kMerchantTrustWithoutSummaryName, "false"}});
  base::HistogramTester t;

  commerce::MerchantTrustSignalsV2 proto = CreateValidProto();
  proto.clear_shopper_voice_summary();
  OptimizationMetadata metadata;
  metadata.set_any_metadata(AnyWrapProto(proto));

  SetResponse(GURL("https://foo.com"), OptimizationGuideDecision::kTrue,
              metadata);

  base::RunLoop run_loop;
  service()->GetMerchantTrustInfo(
      GURL("https://foo.com"),
      base::BindOnce(
          [](base::RunLoop* run_loop, const GURL& url,
             std::optional<page_info::MerchantData> info) {
            ASSERT_FALSE(info.has_value());
            run_loop->Quit();
          },
          &run_loop));
  run_loop.Run();
  t.ExpectUniqueSample("Security.PageInfo.MerchantTrustStatus",
                       MerchantTrustStatus::kValidWithMissingReviewsSummary, 1);
}
// Tests for control evaluation survey.
TEST_F(MerchantTrustServiceTest, ControlSurvey) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(1);
  feature_list.InitWithFeatureState(kMerchantTrustEvaluationControlSurvey,
                                    true);

  prefs()->SetTime(prefs::kMerchantTrustPageInfoLastOpenTime, clock()->Now());
  clock()->Advance(kMerchantTrustEvaluationControlMinTimeToShowSurvey.Get());
  service()->MaybeShowEvaluationSurvey();
}

TEST_F(MerchantTrustServiceTest, ControlSurveyInteractionTooEarly) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(0);
  feature_list.InitWithFeatureState(kMerchantTrustEvaluationControlSurvey,
                                    true);

  prefs()->SetTime(prefs::kMerchantTrustPageInfoLastOpenTime, clock()->Now());
  clock()->Advance(kMerchantTrustEvaluationControlMinTimeToShowSurvey.Get() -
                   base::Seconds(1));
  service()->MaybeShowEvaluationSurvey();
}

TEST_F(MerchantTrustServiceTest, ControlSurveyInteractionExpired) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(0);
  feature_list.InitWithFeatureState(kMerchantTrustEvaluationControlSurvey,
                                    true);

  prefs()->SetTime(prefs::kMerchantTrustPageInfoLastOpenTime, clock()->Now());
  clock()->Advance(kMerchantTrustEvaluationControlMaxTimeToShowSurvey.Get() +
                   base::Seconds(1));
  service()->MaybeShowEvaluationSurvey();
}

// Test for control evaluation survey disabled.
TEST_F(MerchantTrustServiceTest, ControlSurveyDisabled) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(0);

  feature_list.InitWithFeatureState(kMerchantTrustEvaluationControlSurvey,
                                    false);
  service()->MaybeShowEvaluationSurvey();
}

// Tests for experiment evaluation survey.
TEST_F(MerchantTrustServiceTest, ExperimentSurvey) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(1);
  feature_list.InitWithFeatureState(kMerchantTrustEvaluationExperimentSurvey,
                                    true);

  prefs()->SetTime(prefs::kMerchantTrustUiLastInteractionTime, clock()->Now());
  clock()->Advance(kMerchantTrustEvaluationExperimentMinTimeToShowSurvey.Get());
  service()->MaybeShowEvaluationSurvey();
}

TEST_F(MerchantTrustServiceTest, ExperimentSurveyInteractionTooEarly) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(0);
  feature_list.InitWithFeatureState(kMerchantTrustEvaluationExperimentSurvey,
                                    true);

  prefs()->SetTime(prefs::kMerchantTrustUiLastInteractionTime, clock()->Now());
  clock()->Advance(kMerchantTrustEvaluationExperimentMinTimeToShowSurvey.Get() -
                   base::Seconds(1));
  service()->MaybeShowEvaluationSurvey();
}

TEST_F(MerchantTrustServiceTest, ExperimentSurveyInteractionExpired) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(0);
  feature_list.InitWithFeatureState(kMerchantTrustEvaluationExperimentSurvey,
                                    true);

  prefs()->SetTime(prefs::kMerchantTrustUiLastInteractionTime, clock()->Now());
  clock()->Advance(kMerchantTrustEvaluationExperimentMaxTimeToShowSurvey.Get() +
                   base::Seconds(1));
  service()->MaybeShowEvaluationSurvey();
}

// Test for experiment evaluation survey disabled.
TEST_F(MerchantTrustServiceTest, ExperimentSurveyDisabled) {
  base::test::ScopedFeatureList feature_list;
  EXPECT_CALL(*delegate(), ShowEvaluationSurvey()).Times(0);

  feature_list.InitWithFeatureState(kMerchantTrustEvaluationExperimentSurvey,
                                    false);
  service()->MaybeShowEvaluationSurvey();
}

TEST_F(MerchantTrustServiceTest, RecordMerchantTrustInteractionFamiliarSite) {
  base::HistogramTester t;
  base::UserActionTester user_action_tester;

  EXPECT_CALL(*delegate(), GetSiteEngagementScore(_))
      .WillRepeatedly(Return(kMerchantFamiliarityThreshold));
  service()->RecordMerchantTrustInteraction(
      GURL("https://foo.com"), MerchantTrustInteraction::kPageInfoRowShown);

  EXPECT_EQ(1, user_action_tester.GetActionCount(
                   "MerchantTrust.PageInfoRowSeen.FamiliarSite"));
  t.ExpectUniqueSample(
      "Security.PageInfo.MerchantTrustInteraction.FamiliarSite",
      MerchantTrustInteraction::kPageInfoRowShown, 1);
  t.ExpectBucketCount(
      "Security.PageInfo.MerchantTrustEngagement.PageInfoRowShown",
      kMerchantFamiliarityThreshold, 1);
}

TEST_F(MerchantTrustServiceTest, RecordMerchantTrustInteractionUnfamiliarSite) {
  base::HistogramTester t;
  base::UserActionTester user_action_tester;

  EXPECT_CALL(*delegate(), GetSiteEngagementScore(_))
      .WillRepeatedly(Return(kMerchantFamiliarityThreshold - 0.1));
  service()->RecordMerchantTrustInteraction(
      GURL("https://foo.com"),
      MerchantTrustInteraction::kBubbleOpenedFromPageInfo);

  EXPECT_EQ(1, user_action_tester.GetActionCount(
                   "MerchantTrust.BubbleOpenedFromPageInfo.UnfamiliarSite"));
  t.ExpectUniqueSample(
      "Security.PageInfo.MerchantTrustInteraction.UnfamiliarSite",
      MerchantTrustInteraction::kBubbleOpenedFromPageInfo, 1);
  t.ExpectBucketCount("Security.PageInfo.MerchantTrustEngagement.BubbleOpened",
                      kMerchantFamiliarityThreshold - 1, 1);
}

}  // namespace page_info