File: offer_notification_icon_view_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (165 lines) | stat: -rw-r--r-- 5,910 bytes parent folder | download | duplicates (4)
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
// 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 "chrome/browser/ui/views/autofill/payments/offer_notification_icon_view.h"

#include <optional>

#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/page_action/page_action_icon_type.h"
#include "chrome/browser/ui/test/test_browser_ui.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_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/core/browser/data_model/payments/autofill_offer_data.h"
#include "components/autofill/core/browser/payments/offer_notification_options.h"
#include "components/autofill/core/browser/payments/payments_autofill_client.h"
#include "components/commerce/core/test_utils.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/test/browser_test.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/views/interaction/element_tracker_views.h"

namespace autofill {
namespace {

constexpr char kTestURL[] = "https://www.example.com";
constexpr char kTestPromoCode[] = "FREEFALL1234";

struct UiTestData {
  std::string name;
  std::optional<std::vector<base::test::FeatureRefAndParams>> enabled_features;
};

std::string GetTestName(const ::testing::TestParamInfo<UiTestData>& info) {
  return info.param.name;
}

AutofillOfferData CreateTestOffer(const std::vector<GURL>& merchant_origins,
                                  const std::string& promo_code) {
  int64_t offer_id = 2468;
  base::Time expiry = base::Time::Now() + base::Days(2);
  autofill::DisplayStrings display_strings;
  display_strings.value_prop_text = "5% off on shoes. Up to $50";
  display_strings.see_details_text = "See details";
  display_strings.usage_instructions_text =
      "Click the promo code field at checkout to autofill it.";
  return autofill::AutofillOfferData::GPayPromoCodeOffer(
      offer_id, expiry, merchant_origins, /*offer_details_url=*/GURL(),
      display_strings, promo_code);
}

class OfferNotificationIconViewBrowserTest
    : public UiBrowserTest,
      public testing::WithParamInterface<UiTestData> {
 public:
  OfferNotificationIconViewBrowserTest() {
    if (GetParam().enabled_features.has_value()) {
      feature_list_.InitWithFeaturesAndParameters(
          GetParam().enabled_features.value(),
          /*disabled_features=*/{});
    }
  }
  // UiBrowserTest:
  void ShowUi(const std::string& name) override {
    AutofillOfferData offer = CreateTestOffer(
        /*merchant_origins=*/{GURL(kTestURL)}, kTestPromoCode);
    auto* autofill_client =
        ChromeAutofillClient::FromWebContentsForTesting(GetWebContents());

    if (name.find("show_offer_notification_icon_only") != std::string::npos) {
      autofill_client->GetPaymentsAutofillClient()->UpdateOfferNotification(
          offer, {});
    }
  }

  bool VerifyUi() override {
    auto* offer_notification_icon_view = GetIcon();
    if (!offer_notification_icon_view) {
      return false;
    }

    EXPECT_EQ(
        offer_notification_icon_view->GetViewAccessibility().GetCachedName(),
        l10n_util::GetStringUTF16(
            IDS_AUTOFILL_OFFERS_REMINDER_ICON_TOOLTIP_TEXT));

    std::string test_name =
        testing::UnitTest::GetInstance()->current_test_info()->name();

    if (test_name.find("InvokeUi_show_offer_notification_icon_only") !=
        std::string::npos) {
      EXPECT_FALSE(offer_notification_icon_view->ShouldShowLabel());
    }

    return true;
  }

  void WaitForUserDismissal() override {
    // Consider closing the browser to be dismissal. This is useful when using
    // the test-launcher-interactive option.
    ui_test_utils::WaitForBrowserToClose();
  }

 protected:
  content::WebContents* GetWebContents() {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

  IconLabelBubbleView* GetIcon() {
    const ui::ElementContext context =
        views::ElementTrackerViews::GetContextForView(GetLocationBarView());
    views::ElementTrackerViews::ViewList matched_views =
        views::ElementTrackerViews::GetInstance()->GetAllMatchingViews(
            kOfferNotificationChipElementId, context);

    for (auto* matched_view : matched_views) {
      // During the migration, there will be two views with the same element
      // id. If the migration is enabled, we want the view that is a
      // PageActionView.
      if (IsPageActionMigrated(
              PageActionIconType::kPaymentsOfferNotification) ==
          views::IsViewClass<page_actions::PageActionView>(matched_view)) {
        return views::AsViewClass<IconLabelBubbleView>(matched_view);
      }
    }

    return nullptr;
  }

  void WaitForIconToFinishAnimating(OfferNotificationIconView* icon_view) {
    while (icon_view->is_animating_label()) {
      base::RunLoop().RunUntilIdle();
    }
  }

 private:
  BrowserView* GetBrowserView() {
    return BrowserView::GetBrowserViewForBrowser(browser());
  }

  LocationBarView* GetLocationBarView() {
    return GetBrowserView()->toolbar()->location_bar();
  }

  base::test::ScopedFeatureList feature_list_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         OfferNotificationIconViewBrowserTest,
                         testing::Values(UiTestData{"Default"}),
                         GetTestName);

IN_PROC_BROWSER_TEST_P(OfferNotificationIconViewBrowserTest,
                       InvokeUi_show_offer_notification_icon_only) {
  ShowAndVerifyUi();
}

}  // namespace
}  // namespace autofill