blob: f238d78dd293614e3c330d2c15d4bcac17acf081 [file] [log] [blame]
Danyao Wang25f72dc2019-10-18 05:11:321// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/macros.h"
Danyao Wang25f72dc2019-10-18 05:11:326#include "base/test/metrics/histogram_tester.h"
Sahel Sharify6bfc6192020-02-21 20:33:357#include "chrome/test/payments/payment_request_platform_browsertest_base.h"
Danyao Wang25f72dc2019-10-18 05:11:328#include "components/payments/core/journey_logger.h"
Sahel Sharifydab66602020-01-30 20:35:309#include "components/ukm/test_ukm_recorder.h"
Sahel Sharifydab66602020-01-30 20:35:3010#include "services/metrics/public/cpp/ukm_builders.h"
Danyao Wang25f72dc2019-10-18 05:11:3211
12namespace payments {
Danyao Wang25f72dc2019-10-18 05:11:3213
Sahel Sharify6bfc6192020-02-21 20:33:3514class JourneyLoggerTest : public PaymentRequestPlatformBrowserTestBase {
Danyao Wang25f72dc2019-10-18 05:11:3215 public:
Sahel Sharify6bfc6192020-02-21 20:33:3516 JourneyLoggerTest() : gpay_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}
Danyao Wang25f72dc2019-10-18 05:11:3217
Sahel Sharify6bfc6192020-02-21 20:33:3518 ~JourneyLoggerTest() override = default;
Danyao Wang25f72dc2019-10-18 05:11:3219
Sahel Sharifydab66602020-01-30 20:35:3020 void PreRunTestOnMainThread() override {
Sahel Sharify6bfc6192020-02-21 20:33:3521 PaymentRequestPlatformBrowserTestBase::PreRunTestOnMainThread();
Sahel Sharifydab66602020-01-30 20:35:3022 test_ukm_recorder_ = std::make_unique<ukm::TestAutoSetUkmRecorder>();
23 }
24
Danyao Wang25f72dc2019-10-18 05:11:3225 void SetUpOnMainThread() override {
Sahel Sharify6bfc6192020-02-21 20:33:3526 PaymentRequestPlatformBrowserTestBase::SetUpOnMainThread();
Danyao Wang25f72dc2019-10-18 05:11:3227 gpay_server_.ServeFilesFromSourceDirectory(
28 "components/test/data/payments/google.com/");
29 ASSERT_TRUE(gpay_server_.Start());
30
31 // Set up test manifest downloader that knows how to fake origin.
Sahel Sharify6bfc6192020-02-21 20:33:3532 const std::string method_name = "google.com";
33 SetDownloaderAndIgnorePortInOriginComparisonForTesting(
34 {{method_name, &gpay_server_}});
Danyao Wang25f72dc2019-10-18 05:11:3235
Sahel Sharify6bfc6192020-02-21 20:33:3536 main_frame_url_ = https_server()->GetURL("/journey_logger_test.html");
Sahel Sharifydab66602020-01-30 20:35:3037 ASSERT_TRUE(
38 content::NavigateToURL(GetActiveWebContents(), main_frame_url_));
Danyao Wang25f72dc2019-10-18 05:11:3239 }
40
Sahel Sharifydab66602020-01-30 20:35:3041 const GURL& main_frame_url() const { return main_frame_url_; }
42
43 ukm::TestAutoSetUkmRecorder* test_ukm_recorder() {
44 return test_ukm_recorder_.get();
45 }
46
Danyao Wang25f72dc2019-10-18 05:11:3247 private:
Danyao Wang25f72dc2019-10-18 05:11:3248 net::EmbeddedTestServer gpay_server_;
Sahel Sharifydab66602020-01-30 20:35:3049 GURL main_frame_url_;
50 std::unique_ptr<ukm::TestAutoSetUkmRecorder> test_ukm_recorder_;
Danyao Wang25f72dc2019-10-18 05:11:3251
52 DISALLOW_COPY_AND_ASSIGN(JourneyLoggerTest);
53};
54
55IN_PROC_BROWSER_TEST_F(JourneyLoggerTest, NoPaymentMethodSupported) {
56 base::HistogramTester histogram_tester;
57
Sahel Sharify6bfc6192020-02-21 20:33:3558 ResetEventWaiterForSingleEvent(TestEvent::kShowAppsReady);
Danyao Wang25f72dc2019-10-18 05:11:3259 EXPECT_TRUE(content::ExecJs(GetActiveWebContents(), "testBasicCard()"));
60 WaitForObservedEvent();
61
62 EXPECT_TRUE(content::ExecJs(GetActiveWebContents(), "abort()"));
63
64 std::vector<base::Bucket> buckets =
65 histogram_tester.GetAllSamples("PaymentRequest.Events");
66 ASSERT_EQ(1U, buckets.size());
67 EXPECT_FALSE(buckets[0].min &
68 JourneyLogger::EVENT_AVAILABLE_METHOD_BASIC_CARD);
69 EXPECT_FALSE(buckets[0].min & JourneyLogger::EVENT_AVAILABLE_METHOD_GOOGLE);
70 EXPECT_FALSE(buckets[0].min & JourneyLogger::EVENT_AVAILABLE_METHOD_OTHER);
71}
72
73IN_PROC_BROWSER_TEST_F(JourneyLoggerTest, BasicCardOnly) {
Sahel Sharify6bfc6192020-02-21 20:33:3574 CreateAndAddCreditCardForProfile(CreateAndAddAutofillProfile());
Danyao Wang25f72dc2019-10-18 05:11:3275 base::HistogramTester histogram_tester;
76
Sahel Sharify6bfc6192020-02-21 20:33:3577 ResetEventWaiterForSingleEvent(TestEvent::kShowAppsReady);
Danyao Wang25f72dc2019-10-18 05:11:3278 EXPECT_TRUE(content::ExecJs(GetActiveWebContents(), "testBasicCard()"));
79 WaitForObservedEvent();
80
81 EXPECT_EQ(true, content::EvalJs(GetActiveWebContents(), "abort()"));
82
83 std::vector<base::Bucket> buckets =
84 histogram_tester.GetAllSamples("PaymentRequest.Events");
85 ASSERT_EQ(1U, buckets.size());
86 EXPECT_TRUE(buckets[0].min &
87 JourneyLogger::EVENT_AVAILABLE_METHOD_BASIC_CARD);
88 EXPECT_FALSE(buckets[0].min & JourneyLogger::EVENT_AVAILABLE_METHOD_GOOGLE);
89 EXPECT_FALSE(buckets[0].min & JourneyLogger::EVENT_AVAILABLE_METHOD_OTHER);
90}
91
92IN_PROC_BROWSER_TEST_F(JourneyLoggerTest, GooglePaymentApp) {
93 base::HistogramTester histogram_tester;
94
95 EXPECT_EQ("{\"apiVersion\":1}",
96 content::EvalJs(GetActiveWebContents(), "testGPay()"));
97
98 std::vector<base::Bucket> buckets =
99 histogram_tester.GetAllSamples("PaymentRequest.Events");
100 ASSERT_EQ(1U, buckets.size());
101 EXPECT_FALSE(buckets[0].min &
102 JourneyLogger::EVENT_AVAILABLE_METHOD_BASIC_CARD);
103 EXPECT_TRUE(buckets[0].min & JourneyLogger::EVENT_AVAILABLE_METHOD_GOOGLE);
104 EXPECT_FALSE(buckets[0].min & JourneyLogger::EVENT_AVAILABLE_METHOD_OTHER);
105}
106
Sahel Sharifydab66602020-01-30 20:35:30107// Make sure the UKM was logged correctly.
108IN_PROC_BROWSER_TEST_F(JourneyLoggerTest, UKMTransactionAmountRecorded) {
109 EXPECT_EQ("{\"apiVersion\":1}",
110 content::EvalJs(GetActiveWebContents(), "testGPay()"));
111
112 auto entries = test_ukm_recorder()->GetEntriesByName(
113 ukm::builders::PaymentRequest_TransactionAmount::kEntryName);
114 size_t num_entries = entries.size();
115 EXPECT_EQ(2u, num_entries);
116 for (size_t i = 0; i < num_entries; i++) {
117 test_ukm_recorder()->ExpectEntrySourceHasUrl(entries[i], main_frame_url());
118 EXPECT_EQ(2U, entries[i]->metrics.size());
119 test_ukm_recorder()->ExpectEntryMetric(
120 entries[i],
121 ukm::builders::PaymentRequest_TransactionAmount::kCompletionStatusName,
122 i != 0 /* completed */);
123 test_ukm_recorder()->ExpectEntryMetric(
124 entries[i],
125 ukm::builders::PaymentRequest_TransactionAmount::kCategoryName,
126 static_cast<int64_t>(
127 JourneyLogger::TransactionSize::kRegularTransaction));
128 }
129}
130
Danyao Wang25f72dc2019-10-18 05:11:32131} // namespace payments