// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "build/build_config.h"
#include "chrome/browser/ui/views/payments/payment_request_browsertest_base.h"
#include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h"
#include "components/autofill/core/browser/data_manager/addresses/address_data_manager.h"
#include "components/autofill/core/browser/data_manager/addresses/address_data_manager_test_utils.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/browser/test_utils/test_autofill_clock.h"
#include "content/public/test/browser_test.h"

namespace payments {

namespace {

const base::Time kSomeDate = base::Time::FromSecondsSinceUnixEpoch(1484505871);
const base::Time kSomeLaterDate =
    base::Time::FromSecondsSinceUnixEpoch(1497552271);

}  // namespace

using PaymentRequestShippingAddressUseStatsTest = PaymentRequestBrowserTestBase;

// Tests that use stats for the shipping address used in a Payment Request are
// properly updated upon completion.
// Flaky. https://crbug.com/40937744.
IN_PROC_BROWSER_TEST_F(PaymentRequestShippingAddressUseStatsTest,
                       DISABLED_RecordUse) {
  std::string payment_method_name;
  InstallPaymentApp("a.com", "/payment_request_success_responder.js",
                    &payment_method_name);

  NavigateTo("/payment_request_free_shipping_test.html");
  autofill::TestAutofillClock test_clock;
  test_clock.SetNow(kSomeDate);

  // Create two addresses, one with a higher frequency score so that it is
  // selected as the default shipping address.
  autofill::AutofillProfile shipping_address1 =
      autofill::test::GetFullProfile();
  AddAutofillProfile(shipping_address1);
  autofill::AutofillProfile shipping_address2 =
      autofill::test::GetFullProfile2();
  shipping_address2.usage_history().set_use_count(3);
  AddAutofillProfile(shipping_address2);

  // Check that the initial use stats were set correctly.
  const autofill::AutofillProfile* initial_shipping =
      address_data_manager()->GetProfileByGUID(shipping_address2.guid());
  EXPECT_EQ(3U, initial_shipping->usage_history().use_count());
  EXPECT_EQ(kSomeDate, initial_shipping->usage_history().use_date());

  // Complete the Payment Request.
  test_clock.SetNow(kSomeLaterDate);
  InvokePaymentRequestUIWithJs("buyWithMethods([{supportedMethods:'" +
                               payment_method_name + "'}]);");
  ResetEventWaiterForSequence(
      {DialogEvent::PROCESSING_SPINNER_SHOWN, DialogEvent::DIALOG_CLOSED});
  ClickOnDialogViewAndWait(DialogViewID::PAY_BUTTON, dialog_view());
  autofill::AddressDataChangedWaiter(address_data_manager()).Wait();

  // Check that the usage of the profile was recorded.
  const autofill::AutofillProfile* updated_shipping =
      address_data_manager()->GetProfileByGUID(shipping_address2.guid());
  EXPECT_EQ(4U, updated_shipping->usage_history().use_count());
  EXPECT_EQ(kSomeLaterDate, updated_shipping->usage_history().use_date());
}

using PaymentRequestContactAddressUseStatsTest = PaymentRequestBrowserTestBase;

// Tests that use stats for the contact address used in a Payment Request are
// properly updated upon completion.
// Flaky. https://crbug.com/40937744.
IN_PROC_BROWSER_TEST_F(PaymentRequestContactAddressUseStatsTest,
                       DISABLED_RecordUse) {
  std::string payment_method_name;
  InstallPaymentApp("a.com", "/payment_request_success_responder.js",
                    &payment_method_name);

  NavigateTo("/payment_request_name_test.html");
  autofill::TestAutofillClock test_clock;
  test_clock.SetNow(kSomeDate);

  // Create two addresses, one with a higher frequency score so that it is
  // selected as the default contact address.
  autofill::AutofillProfile contact_address1 = autofill::test::GetFullProfile();
  AddAutofillProfile(contact_address1);
  autofill::AutofillProfile contact_address2 =
      autofill::test::GetFullProfile2();
  contact_address2.usage_history().set_use_count(3);
  AddAutofillProfile(contact_address2);

  // Check that the initial use stats were set correctly.
  const autofill::AutofillProfile* initial_contact =
      address_data_manager()->GetProfileByGUID(contact_address2.guid());
  EXPECT_EQ(3U, initial_contact->usage_history().use_count());
  EXPECT_EQ(kSomeDate, initial_contact->usage_history().use_date());

  // Complete the Payment Request.
  test_clock.SetNow(kSomeLaterDate);
  InvokePaymentRequestUIWithJs("buyWithMethods([{supportedMethods:'" +
                               payment_method_name + "'}]);");
  ResetEventWaiterForSequence(
      {DialogEvent::PROCESSING_SPINNER_SHOWN, DialogEvent::DIALOG_CLOSED});
  ClickOnDialogViewAndWait(DialogViewID::PAY_BUTTON, dialog_view());
  autofill::AddressDataChangedWaiter(address_data_manager()).Wait();

  // Check that the usage of the profile was recorded.
  const autofill::AutofillProfile* updated_contact =
      address_data_manager()->GetProfileByGUID(contact_address2.guid());
  EXPECT_EQ(4U, updated_contact->usage_history().use_count());
  EXPECT_EQ(kSomeLaterDate, updated_contact->usage_history().use_date());
}

using PaymentRequestSameShippingAndContactAddressUseStatsTest =
    PaymentRequestBrowserTestBase;

// Tests that use stats for an address that was used both as a shipping and
// contact address in a Payment Request are properly updated upon completion.
// Flaky. https://crbug.com/40937744.
IN_PROC_BROWSER_TEST_F(PaymentRequestSameShippingAndContactAddressUseStatsTest,
                       DISABLED_RecordUse) {
  std::string payment_method_name;
  InstallPaymentApp("a.com", "/payment_request_success_responder.js",
                    &payment_method_name);

  NavigateTo("/payment_request_contact_details_and_free_shipping_test.html");
  autofill::TestAutofillClock test_clock;
  test_clock.SetNow(kSomeDate);

  // Create two addresses, one with a higher frequency score so that it is
  // selected as the default shipping and contact address.
  autofill::AutofillProfile multi_address1 = autofill::test::GetFullProfile();
  AddAutofillProfile(multi_address1);
  autofill::AutofillProfile multi_address2 = autofill::test::GetFullProfile2();
  multi_address2.usage_history().set_use_count(3);
  AddAutofillProfile(multi_address2);

  // Check that the initial use stats were set correctly.
  const autofill::AutofillProfile* initial_multi =
      address_data_manager()->GetProfileByGUID(multi_address2.guid());
  EXPECT_EQ(3U, initial_multi->usage_history().use_count());
  EXPECT_EQ(kSomeDate, initial_multi->usage_history().use_date());

  // Complete the Payment Request.
  test_clock.SetNow(kSomeLaterDate);
  InvokePaymentRequestUIWithJs("buyWithMethods([{supportedMethods:'" +
                               payment_method_name + "'}]);");
  ResetEventWaiterForSequence(
      {DialogEvent::PROCESSING_SPINNER_SHOWN, DialogEvent::DIALOG_CLOSED});
  ClickOnDialogViewAndWait(DialogViewID::PAY_BUTTON, dialog_view());
  autofill::AddressDataChangedWaiter(address_data_manager()).Wait();

  // Check that the usage of the profile was only recorded once.
  const autofill::AutofillProfile* updated_multi =
      address_data_manager()->GetProfileByGUID(multi_address2.guid());
  EXPECT_EQ(4U, updated_multi->usage_history().use_count());
  EXPECT_EQ(kSomeLaterDate, updated_multi->usage_history().use_date());
}

}  // namespace payments
