File: account_select_fill_data_unittest.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 (655 lines) | stat: -rw-r--r-- 28,429 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO: crbug.com/352295124 - Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "components/password_manager/ios/account_select_fill_data.h"

#include "base/feature_list.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/password_manager/core/browser/features/password_features.h"
#include "components/password_manager/ios/features.h"
#include "components/password_manager/ios/test_helpers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

using autofill::FieldRendererId;
using autofill::FormRendererId;
using autofill::PasswordFormFillData;
using password_manager::AccountSelectFillData;
using password_manager::FillData;
using password_manager::FillDataRetrievalResult;
using password_manager::FillDataRetrievalStatus;
using password_manager::FormInfo;
using password_manager::FormInfoRetrievalError;
using password_manager::FormInfoRetrievalResult;
using password_manager::UsernameAndRealm;
using test_helpers::SetPasswordFormFillData;
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Field;

namespace {
// Test data.
const char* kUrl = "http://example.com/";
const char* kFormNames[] = {"form_name1", "form_name2"};
const uint32_t kFormUniqueIDs[] = {0, 3};
const char* kUsernameElements[] = {"username1", "username2"};
const uint32_t kUsernameUniqueIDs[] = {1, 4};
const char* kUsernames[] = {"user0", "u_s_e_r"};
const char* kPasswordElements[] = {"password1", "password2"};
const uint32_t kPasswordUniqueIDs[] = {2, 5};
const char* kPasswords[] = {"password0", "secret"};
const char16_t* kBackupPassword = u"backup_password";
const char* kAdditionalUsernames[] = {"u$er2", nullptr};
const char* kAdditionalPasswords[] = {"secret", nullptr};

// Returns a field renderer ID that isn't used in any testing data, which
// represents an unexisting field renderer ID.
autofill::FieldRendererId UnexistingFieldRendererId() {
  return autofill::FieldRendererId(1000);
}

// Returns form data for a single username form with credentials eligible for
// filling that has at least one non-empty username.
PasswordFormFillData EligibleSingleUsernameFormData() {
  // Set fill data with 1 non-empty username and 1 empty username.
  PasswordFormFillData form_data;
  test_helpers::SetPasswordFormFillData(
      kUrl, /*form_name=*/"", /*renderer_id=*/1, /*username_field=*/"",
      /*username_field_id=*/1,
      /*username_value=*/"", /*password_field=*/"", /*password_field_id=*/0,
      /*password_value=*/"secret1", /*additional_username=*/"username",
      /*addition_password=*/"secret2", &form_data);
  return form_data;
}

// Returns form data with only empty usernames.
PasswordFormFillData FormDataWithEmptyUsernamesOnly() {
  // Set fill data with 2 empty usernames.
  PasswordFormFillData form_data;
  test_helpers::SetPasswordFormFillData(
      kUrl, /*form_name=*/"", /*renderer_id=*/1, /*username_field=*/"",
      /*username_field_id=*/2,
      /*username_value=*/"", /*password_field=*/"", /*password_field_id=*/3,
      /*password_value=*/"secret1", /*additional_username=*/"",
      /*addition_password=*/"secret2", &form_data);
  return form_data;
}

// Returns form data for a single username form with credentials ineligible for
// filling that only consist of empty usernames.
PasswordFormFillData IneligibleSingleUsernameFormData() {
  PasswordFormFillData form_data = FormDataWithEmptyUsernamesOnly();
  // Set the default field id for the password field to make the form a single
  // username form.
  form_data.password_element_renderer_id = autofill::FieldRendererId(0);
  return form_data;
}

class AccountSelectFillDataTest : public PlatformTest {
 public:
  AccountSelectFillDataTest() {
    for (size_t i = 0; i < std::size(form_data_); ++i) {
      SetPasswordFormFillData(
          kUrl, kFormNames[i], kFormUniqueIDs[i], kUsernameElements[i],
          kUsernameUniqueIDs[i], kUsernames[i], kPasswordElements[i],
          kPasswordUniqueIDs[i], kPasswords[i], kAdditionalUsernames[i],
          kAdditionalPasswords[i], &form_data_[i]);
    }
  }

 protected:
  PasswordFormFillData form_data_[2];
};

TEST_F(AccountSelectFillDataTest, EmptyReset) {
  AccountSelectFillData account_select_fill_data;
  EXPECT_TRUE(account_select_fill_data.Empty());

  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);
  EXPECT_FALSE(account_select_fill_data.Empty());

  account_select_fill_data.Reset();
  EXPECT_TRUE(account_select_fill_data.Empty());
}

TEST_F(AccountSelectFillDataTest, IsSuggestionsAvailableOneForm) {
  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);

  // Suggestions are available for the correct form and field ids.
  EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
      form_data_[0].form_renderer_id,
      form_data_[0].username_element_renderer_id, false));
  // Suggestion should be available to any password field.
  EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
      form_data_[0].form_renderer_id, FieldRendererId(404), true));

  // Suggestions are not available for different form renderer_id.
  EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
      FormRendererId(404), form_data_[0].username_element_renderer_id, false));
  EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
      FormRendererId(404), form_data_[0].password_element_renderer_id, true));

  // Suggestions are not available for different field id.
  EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
      form_data_[0].form_renderer_id, FieldRendererId(404), false));
}

TEST_F(AccountSelectFillDataTest, IsSuggestionsAvailableTwoForms) {
  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);
  account_select_fill_data.Add(form_data_[1],
                               /*always_populate_realm=*/false);

  // Suggestions are available for the correct form and field names.
  EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
      form_data_[0].form_renderer_id,
      form_data_[0].username_element_renderer_id, false));
  // Suggestions are available for the correct form and field names.
  EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
      form_data_[1].form_renderer_id,
      form_data_[1].username_element_renderer_id, false));
  // Suggestions are not available for different form id.
  EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
      FormRendererId(404), form_data_[0].username_element_renderer_id, false));
}

// Test that, when sign-in uff is disabled, IsSuggestionsAvailable() returns
// true on password forms when there are only empty usernames, as the
// suggestions with empty usernames still hold passwords that can be filled.
// This test makes sure that there is no regression in password filling with
// sign-in uff disabled.
TEST_F(AccountSelectFillDataTest, IsSuggestionsAvailable_EmptyUsernames) {
  PasswordFormFillData form_data = FormDataWithEmptyUsernamesOnly();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
      form_data.form_renderer_id, form_data.username_element_renderer_id,
      false));
}

// Test that, when sign-in uff is enabled, IsSuggestionsAvailable() still
// returns true on password forms when there are only empty usernames, as the
// suggestions with empty usernames still hold passwords that can be filled.
// Sign-in uff should only target single username forms.
TEST_F(AccountSelectFillDataTest,
       IsSuggestionsAvailable_EmptyUsernames_WhenSigninUffEnabled) {
  PasswordFormFillData form_data = FormDataWithEmptyUsernamesOnly();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
      form_data.form_renderer_id, form_data.username_element_renderer_id,
      false));
}

TEST_F(AccountSelectFillDataTest,
       IsSuggestionsAvailable_OnSingleUsernameForm_WhenEligible) {
  PasswordFormFillData form_data = EligibleSingleUsernameFormData();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_TRUE(account_select_fill_data.IsSuggestionsAvailable(
      form_data.form_renderer_id, form_data.username_element_renderer_id,
      false));
}

TEST_F(AccountSelectFillDataTest,
       IsSuggestionsAvailable_OnSingleUsernameForm_WhenIneligible) {
  PasswordFormFillData form_data = IneligibleSingleUsernameFormData();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_FALSE(account_select_fill_data.IsSuggestionsAvailable(
      form_data.form_renderer_id, form_data.username_element_renderer_id,
      false));
}

TEST_F(AccountSelectFillDataTest, RetrieveSuggestionsOneForm) {
  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);

  for (bool is_password_field : {false, true}) {
    const FieldRendererId field_id =
        is_password_field ? form_data_[0].password_element_renderer_id
                          : form_data_[0].username_element_renderer_id;
    std::vector<UsernameAndRealm> suggestions =
        account_select_fill_data.RetrieveSuggestions(
            form_data_[0].form_renderer_id, field_id, is_password_field);
    EXPECT_EQ(2u, suggestions.size());
    EXPECT_EQ(base::ASCIIToUTF16(kUsernames[0]), suggestions[0].username);
    EXPECT_EQ(std::string(), suggestions[0].realm);
    EXPECT_EQ(base::ASCIIToUTF16(kAdditionalUsernames[0]),
              suggestions[1].username);
    EXPECT_EQ(std::string(), suggestions[1].realm);
  }
}

TEST_F(AccountSelectFillDataTest, RetrieveSuggestionsTwoForm) {
  // Test that after adding two PasswordFormFillData, suggestions for both forms
  // are shown, with credentials from the second PasswordFormFillData. That
  // emulates the case when credentials in the Password Store were changed
  // between load the first and the second forms.
  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);
  account_select_fill_data.Add(form_data_[1],
                               /*always_populate_realm=*/false);

  std::vector<UsernameAndRealm> suggestions =
      account_select_fill_data.RetrieveSuggestions(
          form_data_[0].form_renderer_id,
          form_data_[0].username_element_renderer_id, false);
  EXPECT_EQ(1u, suggestions.size());
  EXPECT_EQ(base::ASCIIToUTF16(kUsernames[1]), suggestions[0].username);

  suggestions = account_select_fill_data.RetrieveSuggestions(
      form_data_[1].form_renderer_id,
      form_data_[1].username_element_renderer_id, false);
  EXPECT_EQ(1u, suggestions.size());
  EXPECT_EQ(base::ASCIIToUTF16(kUsernames[1]), suggestions[0].username);
}

// Test that, when sign-in uff is disabled, RetrieveSuggestions() returns all
// suggestions on password forms when there are only empty usernames, as the
// suggestions with empty usernames still hold passwords that can be filled.
// This test makes sure that there is no regression in password filling with
// sign-in uff disabled.
TEST_F(AccountSelectFillDataTest, RetrieveSuggestions_EmptyUsernames) {
  PasswordFormFillData form_data = FormDataWithEmptyUsernamesOnly();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_THAT(account_select_fill_data.RetrieveSuggestions(
                  form_data.form_renderer_id,
                  form_data.username_element_renderer_id, false),
              testing::SizeIs(2));
}

// Test that, when sign-in uff is enabled, RetrieveSuggestions() still returns
// all suggestions on password forms when there are only empty usernames, as the
// suggestions with empty usernames still hold passwords that can be filled.
// This test makes sure that there is no regression in password filling with
// sign-in uff disabled. Sign-in uff should only target single username forms.
TEST_F(AccountSelectFillDataTest,
       RetrieveSuggestions_EmptyUsernames_WhenSigninUffEnabled) {
  PasswordFormFillData form_data = FormDataWithEmptyUsernamesOnly();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_THAT(account_select_fill_data.RetrieveSuggestions(
                  form_data.form_renderer_id,
                  form_data.username_element_renderer_id, false),
              testing::SizeIs(2));
}

TEST_F(AccountSelectFillDataTest,
       RetrieveSuggestions_OnSingleUsernameForm_WhenEligible) {
  PasswordFormFillData form_data = EligibleSingleUsernameFormData();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_THAT(account_select_fill_data.RetrieveSuggestions(
                  form_data.form_renderer_id,
                  form_data.username_element_renderer_id, false),
              testing::SizeIs(2));
}

TEST_F(AccountSelectFillDataTest,
       RetrieveSuggestions_OnSingleUsernameForm_WhenIneligible) {
  PasswordFormFillData form_data = IneligibleSingleUsernameFormData();

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  EXPECT_THAT(account_select_fill_data.RetrieveSuggestions(
                  form_data.form_renderer_id,
                  form_data.username_element_renderer_id, false),
              testing::IsEmpty());
}

// Tests that the right number of suggestions is created when there's a
// credential that comes with a backup password.
TEST_F(AccountSelectFillDataTest, RetrieveSuggestions_WithBackupPasswords) {
  PasswordFormFillData form_data = form_data_[0];
  form_data.preferred_login.backup_password_value = kBackupPassword;

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  auto retrieve_suggestions = [&]() {
    return account_select_fill_data.RetrieveSuggestions(
        form_data.form_renderer_id, form_data.username_element_renderer_id,
        /*is_password_field=*/false);
  };

  {
    // Enable the iOS backup password feature.
    base::test::ScopedFeatureList scoped_feature_list{
        password_manager::features::kIOSFillRecoveryPassword};

    // There should be three suggestions:
    //   * 1 for the preferred login.
    //   * 1 for the preferred login's backup password.
    //   * 1 for the additional login.
    EXPECT_THAT(
        retrieve_suggestions(),
        ElementsAre(
            AllOf(Field(&UsernameAndRealm::username,
                        base::ASCIIToUTF16(kUsernames[0])),
                  Field(&UsernameAndRealm::is_backup_credential, false)),
            AllOf(Field(&UsernameAndRealm::username,
                        base::ASCIIToUTF16(kUsernames[0])),
                  Field(&UsernameAndRealm::is_backup_credential, true)),
            AllOf(Field(&UsernameAndRealm::username,
                        base::ASCIIToUTF16(kAdditionalUsernames[0])),
                  Field(&UsernameAndRealm::is_backup_credential, false))));
  }
  {
    // Disable the iOS backup password feature.
    base::test::ScopedFeatureList scoped_feature_list;
    scoped_feature_list.InitAndDisableFeature(
        password_manager::features::kIOSFillRecoveryPassword);

    // An additional suggestion shouldn't have been created for the backup
    // password.
    EXPECT_THAT(
        retrieve_suggestions(),
        ElementsAre(
            AllOf(Field(&UsernameAndRealm::username,
                        base::ASCIIToUTF16(kUsernames[0])),
                  Field(&UsernameAndRealm::is_backup_credential, false)),
            AllOf(Field(&UsernameAndRealm::username,
                        base::ASCIIToUTF16(kAdditionalUsernames[0])),
                  Field(&UsernameAndRealm::is_backup_credential, false))));
  }
}

TEST_F(AccountSelectFillDataTest, RetrievePSLMatchedSuggestions) {
  AccountSelectFillData account_select_fill_data;
  const char* kRealm = "http://a.example.com/";
  std::string kAdditionalRealm = "http://b.example.com/";

  // Make logins to be PSL matched by setting non-empy realm.
  form_data_[0].preferred_login.realm = kRealm;
  form_data_[0].additional_logins.begin()->realm = kAdditionalRealm;

  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);
  std::vector<UsernameAndRealm> suggestions =
      account_select_fill_data.RetrieveSuggestions(
          form_data_[0].form_renderer_id,
          form_data_[0].username_element_renderer_id, false);
  EXPECT_EQ(2u, suggestions.size());
  EXPECT_EQ(base::ASCIIToUTF16(kUsernames[0]), suggestions[0].username);
  EXPECT_EQ(kRealm, suggestions[0].realm);
  EXPECT_EQ(base::ASCIIToUTF16(kAdditionalUsernames[0]),
            suggestions[1].username);
  EXPECT_EQ(kAdditionalRealm, suggestions[1].realm);
}

TEST_F(AccountSelectFillDataTest, GetFillData) {
  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);
  account_select_fill_data.Add(form_data_[1],
                               /*always_populate_realm=*/false);

  for (bool is_password_field : {false, true}) {
    for (size_t form_i = 0; form_i < std::size(form_data_); ++form_i) {
      const auto& form_data = form_data_[form_i];
      // Suggestions should be shown on any password field on the form. So in
      // case of clicking on a password field it is taken an id different from
      // existing field ids.
      const FieldRendererId password_field_id =
          is_password_field ? FieldRendererId(1000)
                            : form_data.password_element_renderer_id;
      const FieldRendererId clicked_field =
          is_password_field ? password_field_id
                            : form_data.username_element_renderer_id;

      // GetFillData() doesn't have form identifier in arguments, it should be
      // provided in RetrieveSuggestions().
      account_select_fill_data.RetrieveSuggestions(
          form_data.form_renderer_id, clicked_field, is_password_field);
      FillDataRetrievalResult result = account_select_fill_data.GetFillData(
          base::ASCIIToUTF16(kUsernames[1]));
      ASSERT_TRUE(result.has_value());
      const FillData* fill_data = result.value().get();

      ASSERT_TRUE(fill_data);
      EXPECT_EQ(form_data.url, fill_data->origin);
      EXPECT_EQ(form_data.form_renderer_id.value(), fill_data->form_id.value());
      EXPECT_EQ(kUsernameUniqueIDs[form_i],
                fill_data->username_element_id.value());
      EXPECT_EQ(base::ASCIIToUTF16(kUsernames[1]), fill_data->username_value);
      EXPECT_EQ(password_field_id, fill_data->password_element_id);
      EXPECT_EQ(base::ASCIIToUTF16(kPasswords[1]), fill_data->password_value);
    }
  }
}

TEST_F(AccountSelectFillDataTest, GetFillDataOldCredentials) {
  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);
  account_select_fill_data.Add(form_data_[1],
                               /*always_populate_realm=*/false);

  // GetFillData() doesn't have form identifier in arguments, it should be
  // provided in RetrieveSuggestions().
  account_select_fill_data.RetrieveSuggestions(
      form_data_[0].form_renderer_id,
      form_data_[0].username_element_renderer_id, false);

  // AccountSelectFillData should keep only last credentials. Check that in
  // request of old credentials nothing is returned.
  FillDataRetrievalResult result =
      account_select_fill_data.GetFillData(base::ASCIIToUTF16(kUsernames[0]));
  EXPECT_FALSE(result.has_value());
  EXPECT_EQ(FillDataRetrievalStatus::kNoCredentials, result.error());
}

// Tests that the GetFillData() interface to be used when in stateless mode
// works correctly.
TEST_F(AccountSelectFillDataTest, GetFillData_WhenStateless) {
  base::test::ScopedFeatureList scoped_feature_list{
      password_manager::features::kIOSStatelessFillDataFlow};

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/false);
  account_select_fill_data.Add(form_data_[1],
                               /*always_populate_realm=*/false);

  for (bool is_password_field : {false, true}) {
    for (size_t form_i = 0; form_i < std::size(form_data_); ++form_i) {
      const auto& form_data = form_data_[form_i];
      // Suggestions should be shown on any password field on the form. So in
      // case of clicking on a password field it is taken an id different from
      // existing field ids.
      const FieldRendererId password_field_id =
          is_password_field ? FieldRendererId(1000)
                            : form_data.password_element_renderer_id;
      const FieldRendererId clicked_field =
          is_password_field ? password_field_id
                            : form_data.username_element_renderer_id;

      // GetFillData() when in stateless mode doesn't need to call
      // RetrieveSuggestions() first.
      FillDataRetrievalResult result = account_select_fill_data.GetFillData(
          base::ASCIIToUTF16(kUsernames[1]), form_data.form_renderer_id,
          clicked_field, is_password_field);
      ASSERT_TRUE(result.has_value());
      const FillData* fill_data = result.value().get();
      ASSERT_TRUE(fill_data);
      EXPECT_EQ(form_data.url, fill_data->origin);
      EXPECT_EQ(form_data.form_renderer_id.value(), fill_data->form_id.value());
      EXPECT_EQ(kUsernameUniqueIDs[form_i],
                fill_data->username_element_id.value());
      EXPECT_EQ(base::ASCIIToUTF16(kUsernames[1]), fill_data->username_value);
      EXPECT_EQ(password_field_id, fill_data->password_element_id);
      EXPECT_EQ(base::ASCIIToUTF16(kPasswords[1]), fill_data->password_value);
    }
  }
}

// Tests that the right status will be returned when there is no form with fill
// data matching the queried form.
TEST_F(AccountSelectFillDataTest,
       GetFillData_WhenStateless_NoResult_BecauseNoForm) {
  base::test::ScopedFeatureList scoped_feature_list{
      password_manager::features::kIOSStatelessFillDataFlow};

  AccountSelectFillData account_select_fill_data;

  // GetFillData() when in stateless mode doesn't need to call
  // RetrieveSuggestions() first.
  FillDataRetrievalResult result = account_select_fill_data.GetFillData(
      u"test-user", form_data_[0].form_renderer_id,
      form_data_[0].username_element_renderer_id,
      /*is_password_field=*/false);

  EXPECT_FALSE(result.has_value());
  EXPECT_EQ(FillDataRetrievalStatus::kNoFormMatch, result.error());
}

// Tests that the right status will be returned when there is no field matching
// the queried field.
TEST_F(AccountSelectFillDataTest,
       GetFillData_WhenStateless_NoResult_BecauseNoField) {
  base::test::ScopedFeatureList scoped_feature_list{
      password_manager::features::kIOSStatelessFillDataFlow};

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0], /*always_populate_realm=*/false);

  // GetFillData() when in stateless mode doesn't need to call
  // RetrieveSuggestions() first.
  FillDataRetrievalResult result = account_select_fill_data.GetFillData(
      u"test-user", form_data_[0].form_renderer_id, UnexistingFieldRendererId(),
      /*is_password_field=*/false);

  EXPECT_FALSE(result.has_value());
  EXPECT_EQ(FillDataRetrievalStatus::kNoFieldMatch, result.error());
}

TEST_F(AccountSelectFillDataTest, CrossOriginSuggestionHasRealm) {
  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data_[0],
                               /*always_populate_realm=*/true);

  for (bool is_password_field : {false, true}) {
    const FieldRendererId field_id =
        is_password_field ? form_data_[0].password_element_renderer_id
                          : form_data_[0].username_element_renderer_id;
    std::vector<UsernameAndRealm> suggestions =
        account_select_fill_data.RetrieveSuggestions(
            form_data_[0].form_renderer_id, field_id, is_password_field);
    EXPECT_EQ(2u, suggestions.size());
    EXPECT_EQ(kUrl, suggestions[0].realm);
    EXPECT_EQ(kUrl, suggestions[1].realm);
  }
}

// Tests getting existing form info for an existing username field.
TEST_F(AccountSelectFillDataTest, GetFormInfo_FocusedOnExistingUsernameField) {
  const auto& form_data = form_data_[0];

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  FormInfoRetrievalResult result = account_select_fill_data.GetFormInfo(
      form_data.form_renderer_id, form_data.username_element_renderer_id,
      /*is_password_field=*/false);

  ASSERT_TRUE(result.has_value());
  const FormInfo* form_info = result.value();

  EXPECT_EQ(form_data.url, form_info->origin);
  EXPECT_EQ(form_data.form_renderer_id, form_info->form_id);
  EXPECT_EQ(form_data.username_element_renderer_id,
            form_info->username_element_id);
  EXPECT_EQ(form_data.password_element_renderer_id,
            form_info->password_element_id);
}

// Tests getting form info for an unexisting username field that was no added to
// the data.
TEST_F(AccountSelectFillDataTest,
       GetFormInfo_FocusedOnUnexistingUsernameField) {
  const auto& form_data = form_data_[0];

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  FormInfoRetrievalResult result = account_select_fill_data.GetFormInfo(
      form_data.form_renderer_id, UnexistingFieldRendererId(),
      /*is_password_field=*/false);

  EXPECT_FALSE(result.has_value());
  EXPECT_EQ(FormInfoRetrievalError::kNoFieldMatch, result.error());
}

// Tests getting existing form info when focus on a random password field.
TEST_F(AccountSelectFillDataTest, GetFormInfo_FocusedOnPasswordField) {
  const auto& form_data = form_data_[0];

  AccountSelectFillData account_select_fill_data;
  account_select_fill_data.Add(form_data, /*always_populate_realm=*/false);

  // Get form info for a password field with a unexisting field renderer ID,
  // which should still give a non-null result because any password field should
  // get form info.
  FormInfoRetrievalResult result = account_select_fill_data.GetFormInfo(
      form_data.form_renderer_id, UnexistingFieldRendererId(),
      /*is_password_field=*/true);

  EXPECT_TRUE(result.has_value());
  const FormInfo* form_info = result.value();

  EXPECT_EQ(form_data.url, form_info->origin);
  EXPECT_EQ(form_data.form_renderer_id, form_info->form_id);
  EXPECT_EQ(form_data.username_element_renderer_id,
            form_info->username_element_id);
  EXPECT_EQ(form_data.password_element_renderer_id,
            form_info->password_element_id);
}

// Test getting form info when there is no data for the form.
TEST_F(AccountSelectFillDataTest, GetFormInfo_NoMatch) {
  const auto& form_data = form_data_[0];

  AccountSelectFillData account_select_fill_data;

  FormInfoRetrievalResult result = account_select_fill_data.GetFormInfo(
      form_data.form_renderer_id, form_data.username_element_renderer_id,
      /*is_password_field=*/false);

  EXPECT_FALSE(result.has_value());
  EXPECT_EQ(FormInfoRetrievalError::kNoFormMatch, result.error());
}

}  // namespace