chromium/chrome/browser/ui/views/autofill/payments/local_card_migration_uitest.cc

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

#include <ctime>
#include <list>
#include <memory>
#include <string>
#include <utility>

#include "base/callback_list.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/autofill/autofill_uitest_util.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/ui/autofill/payments/local_card_migration_bubble_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/local_card_migration_dialog_controller_impl.h"
#include "chrome/browser/ui/autofill/payments/save_card_bubble_controller_impl.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/views/autofill/payments/dialog_view_ids.h"
#include "chrome/browser/ui/views/autofill/payments/local_card_migration_bubble_views.h"
#include "chrome/browser/ui/views/autofill/payments/local_card_migration_dialog_view.h"
#include "chrome/browser/ui/views/autofill/payments/local_card_migration_icon_view.h"
#include "chrome/browser/ui/views/autofill/payments/migratable_card_view.h"
#include "chrome/browser/ui/views/autofill/payments/save_card_bubble_views.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_container.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_controller.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_loading_indicator_view.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/webdata_services/web_data_service_factory.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/content/browser/content_autofill_client.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/test_autofill_manager_injector.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/browser_autofill_manager.h"
#include "components/autofill/core/browser/form_data_importer.h"
#include "components/autofill/core/browser/metrics/payments/local_card_migration_metrics.h"
#include "components/autofill/core/browser/payments/credit_card_save_manager.h"
#include "components/autofill/core/browser/payments/local_card_migration_manager.h"
#include "components/autofill/core/browser/payments/payments_util.h"
#include "components/autofill/core/browser/payments_data_manager.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/personal_data_manager_observer.h"
#include "components/autofill/core/browser/personal_data_manager_test_utils.h"
#include "components/autofill/core/browser/test_autofill_manager_waiter.h"
#include "components/autofill/core/browser/test_event_waiter.h"
#include "components/autofill/core/browser/webdata/payments/payments_autofill_table.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/autofill/core/common/credit_card_network_identifiers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync/test/fake_server.h"
#include "components/sync/test/fake_server_network_resources.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/device/public/cpp/test/scoped_geolocation_overrider.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/base_event_utils.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/layout/animating_layout_manager.h"
#include "ui/views/layout/animating_layout_manager_test_util.h"
#include "ui/views/test/widget_test.h"

Bucket;
ElementsAre;

namespace autofill {

namespace {

ACTION_P(QuitMessageLoop, loop) {}

constexpr char kURLGetUploadDetailsRequest[] =;
constexpr char kURLMigrateCardRequest[] =;

constexpr char kResponseGetUploadDetailsSuccess[] =;
constexpr char kResponseGetUploadDetailsSuccessLong[] =;
constexpr char kResponseGetUploadDetailsFailure[] =;

constexpr char kResponseMigrateCardSuccess[] =;

constexpr char kCreditCardFormURL[] =;

constexpr char kFirstCardNumber[] =;   // Mastercard
constexpr char kSecondCardNumber[] =;  // Visa
constexpr char kThirdCardNumber[] =;   // Visa
constexpr char kInvalidCardNumber[] =;
constexpr char kMaskedCardNumber[] =;

constexpr double kFakeGeolocationLatitude =;
constexpr double kFakeGeolocationLongitude =;

}  // namespace

class LocalCardMigrationBrowserTest
    : public SyncTest,
      public LocalCardMigrationManager::ObserverForTest {};

class LocalCardMigrationBrowserUiTest
    : public SupportsTestDialog<LocalCardMigrationBrowserTest> {};

// Ensures that migration is not offered when user saves a new card.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_UsingNewCardDoesNotShowIntermediateMigrationOffer) {}

// Ensures that migration is not offered when payments declines the cards.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_IntermediateMigrationOfferDoesNotShowWhenPaymentsDeclines) {}

// Ensures that the intermediate migration bubble is not shown after reusing
// a saved server card, if there are no other cards to migrate.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ReusingServerCardDoesNotShowIntermediateMigrationOffer) {}

// Ensures that the intermediate migration bubble is shown after reusing
// a saved server card, if there is at least one card to migrate.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ReusingServerCardWithMigratableLocalCardShowIntermediateMigrationOffer) {}

// Ensures that the intermediate migration bubble is not shown after reusing
// a previously saved local card, if there are no other cards to migrate.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ReusingLocalCardDoesNotShowIntermediateMigrationOffer) {}

// Ensures that the intermediate migration bubble is triggered after reusing
// a saved local card, if there are multiple local cards available to migrate.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ReusingLocalCardShowsIntermediateMigrationOffer) {}

// Ensures that clicking [X] on the offer bubble makes the bubble disappear.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClickingCloseClosesBubble) {}

// Ensures that the credit card icon will show in location bar.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_CreditCardIconShownInLocationBar) {}

// Ensures that clicking on the credit card icon in the omnibox reopens the
// offer bubble after closing it.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClickingOmniboxIconReshowsBubble) {}

// Ensures that accepting the intermediate migration offer opens up the main
// migration dialog.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClickingContinueOpensDialog) {}

// Ensures that the migration dialog contains all the valid card stored in
// Chrome browser local storage.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_DialogContainsAllValidMigratableCard) {}

// Ensures that rejecting the main migration dialog closes the dialog.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClickingCancelClosesDialog) {}

// Ensures that accepting the main migration dialog closes the dialog.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClickingSaveClosesDialog) {}

// Ensures local cards will be deleted from browser local storage after being
// successfully migrated.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_DeleteSuccessfullyMigratedCardsFromLocal) {}

// Ensures that accepting the main migration dialog adds strikes.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_AcceptingDialogAddsLocalCardMigrationStrikes) {}

// Ensures that rejecting the main migration dialog adds strikes.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_RejectingDialogAddsLocalCardMigrationStrikes) {}

// Ensures that rejecting the migration bubble adds strikes.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClosingBubbleAddsLocalCardMigrationStrikes) {}

// Ensures that rejecting the migration bubble repeatedly adds strikes every
// time, even for the same tab. Currently, it adds 3 strikes (out of 6), so this
// test can reliably test it being added twice.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClosingBubbleAgainAddsLocalCardMigrationStrikes) {}

// Ensures that reshowing and closing bubble after previously closing it does
// not add strikes.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ReshowingBubbleDoesNotAddStrikes) {}

IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClosedReason_BubbleAccepted) {}

IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClosedReason_BubbleClosed) {}

IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClosedReason_BubbleNotInteracted) {}

IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_ClosedReason_BubbleLostFocus) {}

// Tests to ensure the card nickname is shown correctly in the local card
// migration dialog.
IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_CardIdentifierString) {}

IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_IconViewAccessibleName) {}

IN_PROC_BROWSER_TEST_F(
    LocalCardMigrationBrowserUiTest,
    // TODO(crbug.com/40649134): Flaky, but feature should soon be removed.
    DISABLED_InvokeUi_default) {}

// TODO(crbug.com/41422186):
// - Add more tests for feedback dialog.

}  // namespace autofill