File: account_selection_view_test_base.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (313 lines) | stat: -rw-r--r-- 12,194 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 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/webid/account_selection_view_test_base.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/views/webid/account_selection_view_base.h"
#include "chrome/browser/ui/webid/identity_ui_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/test/test_event.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/throbber.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/test/views_test_utils.h"

namespace webid {

const std::vector<content::IdentityRequestDialogDisclosureField>
    kDefaultDisclosureFields = {
        content::IdentityRequestDialogDisclosureField::kName,
        content::IdentityRequestDialogDisclosureField::kEmail,
        content::IdentityRequestDialogDisclosureField::kPicture};

AccountSelectionViewTestBase::AccountSelectionViewTestBase() = default;

AccountSelectionViewTestBase::~AccountSelectionViewTestBase() = default;

std::u16string_view AccountSelectionViewTestBase::GetHoverButtonTitle(
    HoverButton* account) {
  return account->title()->GetText();
}

views::Label* AccountSelectionViewTestBase::GetHoverButtonSubtitle(
    HoverButton* account) {
  return account->subtitle();
}

views::View* AccountSelectionViewTestBase::GetHoverButtonIconView(
    HoverButton* account) {
  return account->icon_view();
}

views::Label* AccountSelectionViewTestBase::GetHoverButtonFooter(
    HoverButton* account) {
  return account->footer();
}

views::View* AccountSelectionViewTestBase::GetHoverButtonSecondaryView(
    HoverButton* account) {
  return account->secondary_view();
}

IdentityRequestAccountPtr
AccountSelectionViewTestBase::CreateTestIdentityRequestAccount(
    const std::string& account_suffix,
    IdentityProviderDataPtr idp,
    content::IdentityRequestAccount::LoginState login_state,
    std::optional<base::Time> last_used_timestamp) {
  std::string display_identifier =
      std::string(kDisplayIdentifierBase) + account_suffix;
  std::string display_name = std::string(kDisplayNameBase) + account_suffix;
  std::string name = std::string(kNameBase) + account_suffix;
  std::string email = std::string(kEmailBase) + account_suffix;
  IdentityRequestAccountPtr account =
      base::MakeRefCounted<content::IdentityRequestAccount>(
          std::string(kIdBase) + account_suffix, display_identifier,
          display_name, email, name,
          std::string(kGivenNameBase) + account_suffix, GURL(), "", "",
          /*login_hints=*/std::vector<std::string>(),
          /*domain_hints=*/std::vector<std::string>(),
          /*labels=*/std::vector<std::string>(), login_state,
          /*browser_trusted_login_state=*/
          content::IdentityRequestAccount::LoginState::kSignUp,
          last_used_timestamp);
  if (login_state == content::IdentityRequestAccount::LoginState::kSignUp) {
    account->fields = idp->disclosure_fields;
  }
  account->identity_provider = std::move(idp);
  return account;
}

std::vector<IdentityRequestAccountPtr>
AccountSelectionViewTestBase::CreateTestIdentityRequestAccounts(
    const std::vector<std::string>& account_suffixes,
    IdentityProviderDataPtr idp,
    const std::vector<content::IdentityRequestAccount::LoginState>&
        login_states,
    const std::vector<std::optional<base::Time>>& last_used_timestamps) {
  if (!login_states.empty()) {
    CHECK_EQ(account_suffixes.size(), login_states.size());
  }
  if (!last_used_timestamps.empty()) {
    CHECK_EQ(account_suffixes.size(), last_used_timestamps.size());
  }
  std::vector<IdentityRequestAccountPtr> accounts;
  size_t idx = 0;
  for (const std::string& account_suffix : account_suffixes) {
    content::IdentityRequestAccount::LoginState login_state =
        login_states.empty()
            ? content::IdentityRequestAccount::LoginState::kSignUp
            : login_states[idx];
    std::optional<base::Time> last_used_timestamp =
        last_used_timestamps.empty() ? std::nullopt : last_used_timestamps[idx];
    accounts.push_back(CreateTestIdentityRequestAccount(
        account_suffix, idp, login_state, last_used_timestamp));
    ++idx;
  }
  return accounts;
}

content::ClientMetadata AccountSelectionViewTestBase::CreateTestClientMetadata(
    const std::string& terms_of_service_url,
    const std::string& privacy_policy_url,
    const std::string& rp_brand_icon_url) {
  return content::ClientMetadata(GURL(terms_of_service_url),
                                 GURL(privacy_policy_url),
                                 GURL(rp_brand_icon_url), gfx::Image());
}

std::vector<std::string> AccountSelectionViewTestBase::GetChildClassNames(
    views::View* parent) {
  std::vector<std::string> child_class_names;
  for (views::View* child_view : parent->children()) {
    child_class_names.emplace_back(child_view->GetClassName());
  }
  return child_class_names;
}

views::View* AccountSelectionViewTestBase::GetViewWithClassName(
    views::View* parent,
    const std::string& class_name) {
  for (views::View* child_view : parent->children()) {
    if (child_view->GetClassName() == class_name) {
      return child_view;
    }
  }
  return nullptr;
}

void AccountSelectionViewTestBase::CheckNonHoverableAccountRow(
    views::View* row,
    const std::string& account_suffix,
    bool has_display_identifier) {
  std::vector<raw_ptr<views::View, VectorExperimental>> row_children =
      row->children();
  ASSERT_EQ(row_children.size(), 2u);

  // Check the image.
  views::ImageView* image_view =
      static_cast<views::ImageView*>(row_children[0]);
  EXPECT_TRUE(image_view);

  // Check the text shown.
  views::View* text_view = row_children[1];
  views::BoxLayout* layout_manager =
      static_cast<views::BoxLayout*>(text_view->GetLayoutManager());
  ASSERT_TRUE(layout_manager);
  EXPECT_EQ(layout_manager->GetOrientation(),
            views::BoxLayout::Orientation::kVertical);
  std::vector<raw_ptr<views::View, VectorExperimental>> text_view_children =
      text_view->children();
  unsigned int expected_children = has_display_identifier ? 2u : 1u;
  ASSERT_EQ(text_view_children.size(), expected_children);

  std::string expected_display_name(std::string(kDisplayNameBase) +
                                    account_suffix);
  views::StyledLabel* name_view =
      static_cast<views::StyledLabel*>(text_view_children[0]);
  ASSERT_TRUE(name_view);
  EXPECT_EQ(name_view->GetText(), base::UTF8ToUTF16(expected_display_name));

  if (has_display_identifier) {
    std::string expected_display_identifier(
        std::string(kDisplayIdentifierBase) + account_suffix);
    views::Label* display_identifier_view =
        static_cast<views::Label*>(text_view_children[1]);
    ASSERT_TRUE(display_identifier_view);
    EXPECT_EQ(display_identifier_view->GetText(),
              base::UTF8ToUTF16(expected_display_identifier));
  }
}

void AccountSelectionViewTestBase::CheckHoverableAccountRows(
    const std::vector<raw_ptr<views::View, VectorExperimental>>& accounts,
    const std::vector<std::string>& account_suffixes,
    size_t& accounts_index,
    bool expect_idp,
    bool is_modal_dialog) {
  ASSERT_GE(accounts.size(), account_suffixes.size() + accounts_index);
  // Checks the account rows starting at `accounts[accounts_index]`. Updates
  // `accounts_index` to the first unused index in `accounts`, or to
  // `accounts.size()` if done.
  for (const auto& account_suffix : account_suffixes) {
    if (accounts[accounts_index]->GetClassName() == "Separator") {
      ++accounts_index;
    }
    CheckHoverableAccountRow(accounts[accounts_index++], account_suffix,
                             /*has_display_identifier=*/true, expect_idp,
                             is_modal_dialog);
  }
}

void AccountSelectionViewTestBase::CheckHoverableAccountRow(
    views::View* account,
    const std::string& account_suffix,
    bool has_display_identifier,
    bool expect_idp,
    bool is_modal_dialog,
    bool is_disabled) {
  // RunScheduledLayout() is needed due to widget auto-resize.
  views::test::RunScheduledLayout(account);

  ASSERT_EQ("HoverButton", account->GetClassName());
  HoverButton* account_row = static_cast<HoverButton*>(account);
  ASSERT_TRUE(account_row);

  // Check for the title, which is the display name if the account is not
  // filtered out and the display identifier otherwise.
  EXPECT_EQ(GetHoverButtonTitle(account_row),
            (is_disabled && has_display_identifier)
                ? base::UTF8ToUTF16(kDisplayIdentifierBase + account_suffix)
                : base::UTF8ToUTF16(kDisplayNameBase + account_suffix));

  if (!is_disabled) {
    if (has_display_identifier) {
      // Check for account display identifier in subtitle.
      EXPECT_EQ(GetHoverButtonSubtitle(account_row)->GetText(),
                base::UTF8ToUTF16(std::string(kDisplayIdentifierBase) +
                                  account_suffix));
    } else {
      EXPECT_EQ(nullptr, GetHoverButtonSubtitle(account_row));
    }
    EXPECT_TRUE(account_row->GetEnabled());
  } else {
    // Check that the subtitle says that the account is disabled.
    EXPECT_EQ(GetHoverButtonSubtitle(account_row)->GetText(),
              u"You can't sign in using this account");
    EXPECT_FALSE(account_row->GetEnabled());
  }
  if (is_disabled || has_display_identifier) {
    // The subtitle has changed style, so AutoColorReadabilityEnabled should be
    // set.
    EXPECT_TRUE(
        GetHoverButtonSubtitle(account_row)->GetAutoColorReadabilityEnabled());
  }

  // Check for account icon.
  views::View* icon_view = GetHoverButtonIconView(account_row);
  EXPECT_TRUE(icon_view);
  EXPECT_EQ(icon_view->GetClassName(), "AccountImageView");

  // Check for the IDP eTLD+1 in footer. This is not passed to the method but
  // in our tests they all start with 'idp'.
  if (expect_idp) {
    EXPECT_TRUE(
        GetHoverButtonFooter(account_row)->GetText().starts_with(u"idp"));
  } else {
    EXPECT_FALSE(GetHoverButtonFooter(account_row));
  }
  EXPECT_EQ(
      icon_view->size(),
      is_modal_dialog
          ? gfx::Size(webid::kModalAvatarSize, webid::kModalAvatarSize)
      // Height is increased by 2 * offset so that the account icon is centered.
      : expect_idp
          ? gfx::Size(webid::kDesiredAvatarSize + webid::kIdpBadgeOffset,
                      webid::kDesiredAvatarSize + 2 * webid::kIdpBadgeOffset)
          : gfx::Size(webid::kDesiredAvatarSize, webid::kDesiredAvatarSize));

  if (is_modal_dialog) {
    // Check for arrow icon in secondary view.
    AccountHoverButtonSecondaryView* secondary_view =
        static_cast<AccountHoverButtonSecondaryView*>(
            GetHoverButtonSecondaryView(account_row));
    EXPECT_TRUE(secondary_view);

    // Check that arrow icon can be replaced with a spinner.
    secondary_view->ReplaceWithSpinner();
    views::Throbber* spinner_view =
        static_cast<views::Throbber*>(secondary_view->children()[0]);
    EXPECT_TRUE(spinner_view);
  } else {
    EXPECT_FALSE(GetHoverButtonSecondaryView(account_row));
  }
}

void AccountSelectionViewTestBase::CheckDisclosureText(
    views::View* disclosure_text,
    bool expect_terms_of_service,
    bool expect_privacy_policy) {
  views::StyledLabel* disclosure_label =
      static_cast<views::StyledLabel*>(disclosure_text);
  ASSERT_TRUE(disclosure_label);

  std::u16string expected_disclosure_text =
      u"To continue, idp-example.com will share your name, email address, and "
      u"profile picture with this site.";
  if (expect_privacy_policy && expect_terms_of_service) {
    expected_disclosure_text +=
        u" See this site's privacy policy and terms of service.";
  } else if (expect_privacy_policy) {
    expected_disclosure_text += u" See this site's privacy policy.";
  } else if (expect_terms_of_service) {
    expected_disclosure_text += u" See this site's terms of service.";
  }

  EXPECT_EQ(disclosure_label->GetText(), expected_disclosure_text);
}

}  // namespace webid