File: popup_cell_utils_unittest.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (164 lines) | stat: -rw-r--r-- 6,325 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/ui/views/autofill/popup/popup_cell_utils.h"

#include <memory>

#include "base/test/task_environment.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/views/autofill/popup/popup_row_content_view.h"
#include "chrome/browser/ui/views/autofill/popup/popup_view_utils.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "components/autofill/core/browser/suggestions/suggestion.h"
#include "components/autofill/core/browser/suggestions/suggestion_type.h"
#include "components/strings/grit/components_strings.h"
#include "components/vector_icons/vector_icons.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/throbber.h"

namespace autofill {
namespace {

constexpr char16_t kVirtualCardBadgeLabel[] = u"Virtual card";
constexpr char16_t kIbanBadgeLabel[] = u"IBAN";

struct VoiceOverTestParam {
  Suggestion suggestion;
  std::u16string expected_voice_over;
  std::string test_name;
};

class GetVoiceOverStringFromSuggestionTest
    : public testing::Test,
      public testing::WithParamInterface<VoiceOverTestParam> {
 public:
  GetVoiceOverStringFromSuggestionTest() = default;
  ~GetVoiceOverStringFromSuggestionTest() override = default;
};

TEST_P(GetVoiceOverStringFromSuggestionTest, Test) {
  const VoiceOverTestParam& param = GetParam();
  EXPECT_EQ(
      popup_cell_utils::GetVoiceOverStringFromSuggestion(param.suggestion),
      param.expected_voice_over)
      << "Test case: " << param.test_name;
}

const char* GetExpandableMenuIconNameFromSuggestionType(SuggestionType type) {
  return popup_cell_utils::GetExpandableMenuIcon(type).name;
}

TEST(PopupCellUtilsTest,
     GetExpandableMenuIcon_ComposeSuggestions_ReturnThreeDotsMenuIcon) {
  EXPECT_EQ(GetExpandableMenuIconNameFromSuggestionType(
                SuggestionType::kComposeProactiveNudge),
            kBrowserToolsChromeRefreshIcon.name);
  // No other Compose type should allow an expandable menu.
  EXPECT_FALSE(IsExpandableSuggestionType(SuggestionType::kComposeResumeNudge));
  EXPECT_FALSE(IsExpandableSuggestionType(
      SuggestionType::kComposeSavedStateNotification));
}

TEST(PopupCellUtilsTest,
     GetExpandableMenuIcon_NonComposeSuggestions_ReturnSubMenuArrowIcon) {
  EXPECT_EQ(GetExpandableMenuIconNameFromSuggestionType(
                SuggestionType::kAddressEntry),
            vector_icons::kSubmenuArrowChromeRefreshIcon.name);
}

// Tests that if a throbber is used instead of an icon the preferred size of the
// `PopupRowContentView` does not change.
TEST(PopupCellUtilsTest, SettingIsLoadingMaintainsPreferredSize) {
  // Needed for the throbber.
  base::test::TaskEnvironment task_environment;
  // Needed to construct a `PopupRowContentView`.
  ChromeLayoutProvider layout_provider;
  Suggestion suggestion(SuggestionType::kCreateNewPlusAddressInline);
  suggestion.icon = Suggestion::Icon::kPlusAddress;

  auto make_main_label = []() {
    return std::make_unique<views::Label>(u"Create new plus address");
  };
  auto make_icon = [&]() {
    return popup_cell_utils::GetIconImageView(suggestion);
  };

  // Ensure that the test is meaningful: The throbber and the icon should have
  // different minimum sizes.
  ASSERT_NE(std::make_unique<views::Throbber>()->GetMinimumSize(),
            make_icon()->GetMinimumSize());

  auto first_content_view = std::make_unique<PopupRowContentView>();
  popup_cell_utils::AddSuggestionContentToView(
      suggestion, make_main_label(), /*minor_text_labels=*/{},
      /*description_label=*/nullptr, /*subtext_views=*/{}, make_icon(),
      *first_content_view);

  suggestion.is_loading = Suggestion::IsLoading(true);
  auto second_content_view = std::make_unique<PopupRowContentView>();
  popup_cell_utils::AddSuggestionContentToView(
      suggestion, make_main_label(), /*minor_text_labels=*/{},
      /*description_label=*/nullptr, /*subtext_views=*/{}, make_icon(),
      *second_content_view);

  EXPECT_EQ(first_content_view->GetPreferredSize(),
            second_content_view->GetPreferredSize());
}

const VoiceOverTestParam kVoiceOverTestCases[] = {
    // This is a VCN suggestion without either product description nor
    // card nickname.
    {.suggestion =
         [] {
           Suggestion suggestion(u"Amex ••1234",
                                 SuggestionType::kVirtualCreditCardEntry);
           suggestion.minor_texts = {Suggestion::Text(u"Expires 01/25")};
           return suggestion;
         }(),
     .expected_voice_over =
         u"Amex ••1234 " + std::u16string(kVirtualCardBadgeLabel),
     .test_name = "VCNWithMinorText"},
    // This is a VCN suggestion with a product description.
    {.suggestion =
         [] {
           Suggestion suggestion(u"American Express Gold card",
                                 SuggestionType::kVirtualCreditCardEntry);
           suggestion.labels = {{Suggestion::Text(u"Amex ••1234")}};
           return suggestion;
         }(),
     .expected_voice_over = u"American Express Gold card Amex ••1234 " +
                            std::u16string(kVirtualCardBadgeLabel),
     .test_name = "VCNWithLabels"},
    {.suggestion =
         [] {
           Suggestion suggestion(u"DE ••6199", SuggestionType::kIbanEntry);
           return suggestion;
         }(),
     .expected_voice_over = u"DE ••6199 " + std::u16string(kIbanBadgeLabel),
     .test_name = "IBANWithNoLabels"},
    {.suggestion =
         [] {
           Suggestion suggestion(u"My IBAN", SuggestionType::kIbanEntry);
           suggestion.labels = {{Suggestion::Text(u"DE ••6199")}};
           return suggestion;
         }(),
     .expected_voice_over =
         u"My IBAN DE ••6199 " + std::u16string(kIbanBadgeLabel),
     .test_name = "IBANWithLabels"},
};

INSTANTIATE_TEST_SUITE_P(
    All,
    GetVoiceOverStringFromSuggestionTest,
    testing::ValuesIn(kVoiceOverTestCases),
    [](const testing::TestParamInfo<VoiceOverTestParam>& info) {
      return info.param.test_name;
    });

}  // namespace
}  // namespace autofill