File: password_form_helper_unittest.mm

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 (989 lines) | stat: -rw-r--r-- 38,985 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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
// 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.

#import "components/password_manager/ios/password_form_helper.h"

#import <stddef.h>

#import <string>

#import "base/apple/bundle_locations.h"
#import "base/strings/sys_string_conversions.h"
#import "base/strings/utf_string_conversions.h"
#import "base/test/ios/wait_util.h"
#import "base/test/metrics/histogram_tester.h"
#import "base/values.h"
#import "components/autofill/core/browser/logging/log_manager.h"
#import "components/autofill/core/common/field_data_manager.h"
#import "components/autofill/core/common/form_data.h"
#import "components/autofill/core/common/password_form_fill_data.h"
#import "components/autofill/core/common/unique_ids.h"
#import "components/autofill/ios/browser/autofill_java_script_feature.h"
#import "components/autofill/ios/common/field_data_manager_factory_ios.h"
#import "components/autofill/ios/form_util/autofill_test_with_web_state.h"
#import "components/autofill/ios/form_util/form_handlers_java_script_feature.h"
#import "components/autofill/ios/form_util/form_util_java_script_feature.h"
#import "components/password_manager/core/browser/mock_password_manager.h"
#import "components/password_manager/core/browser/password_manager_driver.h"
#import "components/password_manager/core/browser/password_manager_interface.h"
#import "components/password_manager/ios/account_select_fill_data.h"
#import "components/password_manager/ios/ios_password_manager_driver_factory.h"
#import "components/password_manager/ios/password_manager_java_script_feature.h"
#import "components/password_manager/ios/shared_password_controller.h"
#import "components/password_manager/ios/test_helpers.h"
#import "components/ukm/ios/ukm_url_recorder.h"
#import "components/ukm/test_ukm_recorder.h"
#import "ios/web/public/js_messaging/content_world.h"
#import "ios/web/public/js_messaging/script_message.h"
#import "ios/web/public/js_messaging/web_frame.h"
#import "ios/web/public/js_messaging/web_frames_manager.h"
#import "ios/web/public/test/fakes/fake_navigation_context.h"
#import "ios/web/public/test/fakes/fake_web_client.h"
#import "ios/web/public/test/fakes/fake_web_frame.h"
#import "ios/web/public/test/fakes/fake_web_frames_manager.h"
#import "ios/web/public/test/fakes/fake_web_state.h"
#import "ios/web/public/test/js_test_util.h"
#import "ios/web/public/test/web_test_with_web_state.h"
#import "ios/web/public/web_client.h"
#import "ios/web/public/web_state.h"
#import "services/metrics/public/cpp/ukm_builders.h"
#import "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/gtest_support.h"

NS_ASSUME_NONNULL_BEGIN

using autofill::FieldRendererId;
using autofill::FormData;
using autofill::FormRendererId;
using autofill::PasswordFormFillData;
using base::test::ios::kWaitForJSCompletionTimeout;
using base::test::ios::WaitUntilConditionOrTimeout;
using password_manager::FillData;
using test_helpers::SetPasswordFormFillData;
using test_helpers::SetFillData;
using test_helpers::SetFormData;

namespace {

// A FakeWebState that returns nullopt as the last trusted committed URL.
class FakeWebStateWithoutTrustedCommittedUrl : public web::FakeWebState {
 public:
  ~FakeWebStateWithoutTrustedCommittedUrl() override = default;

  // WebState implementation.
  std::optional<GURL> GetLastCommittedURLIfTrusted() const override {
    return std::nullopt;
  }
};

class PasswordFormHelperTest : public AutofillTestWithWebState {
 public:
  PasswordFormHelperTest()
      : AutofillTestWithWebState(std::make_unique<web::FakeWebClient>()) {
    web::FakeWebClient* web_client =
        static_cast<web::FakeWebClient*>(GetWebClient());
    web_client->SetJavaScriptFeatures(
        {autofill::FormHandlersJavaScriptFeature::GetInstance(),
         autofill::FormUtilJavaScriptFeature::GetInstance(),
         password_manager::PasswordManagerJavaScriptFeature::GetInstance()});
  }

  PasswordFormHelperTest(const PasswordFormHelperTest&) = delete;
  PasswordFormHelperTest& operator=(const PasswordFormHelperTest&) = delete;

  ~PasswordFormHelperTest() override = default;

  void SetUp() override {
    WebTestWithWebState::SetUp();

    IOSPasswordManagerDriverFactory::CreateForWebState(
        web_state(), OCMStrictClassMock([SharedPasswordController class]),
        &password_manager_);

    helper_ = [[PasswordFormHelper alloc] initWithWebState:web_state()];
    ukm::InitializeSourceUrlRecorderForWebState(web_state());
  }

  void TearDown() override {
    WaitForBackgroundTasks();
    helper_ = nil;
    web::WebTestWithWebState::TearDown();
  }

  web::WebFrame* GetMainFrame() {
    password_manager::PasswordManagerJavaScriptFeature* feature =
        password_manager::PasswordManagerJavaScriptFeature::GetInstance();
    return feature->GetWebFramesManager(web_state())->GetMainWebFrame();
  }

  // Sets up unique form ids and returns true if successful.
  bool SetUpUniqueIDs() {
    __block web::WebFrame* main_frame = nullptr;
    bool success =
        WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^bool {
          main_frame = GetMainFrame();
          return main_frame != nullptr;
        });
    if (!success) {
      return false;
    }
    DCHECK(main_frame);

    // Run password forms search to set up unique IDs.
    __block bool complete = false;
    password_manager::PasswordManagerJavaScriptFeature::GetInstance()
        ->FindPasswordFormsInFrame(main_frame,
                                   base::BindOnce(^(NSString* forms) {
                                     complete = true;
                                   }));

    return WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^bool {
      return complete;
    });
  }

  // Returns a valid form submitted message body.
  std::unique_ptr<base::Value> ValidFormSubmittedMessageBody(
      std::string frame_id) {
    return std::make_unique<base::Value>(
        base::Value::Dict()
            .Set("name", "test_form")
            .Set("origin", BaseUrl())
            .Set("fields", base::Value::List().Append(
                               base::Value::Dict()
                                   .Set("name", "test_field")
                                   .Set("form_control_type", "password")))
            .Set("host_frame", frame_id));
  }

  // Returns a script message that can represent a form submission.
  web::ScriptMessage ScriptMessageForSubmit(std::unique_ptr<base::Value> body) {
    return web::ScriptMessage(std::move(body),
                              /*is_user_interacting=*/true,
                              /*is_main_frame=*/true,
                              /*request_url=*/std::nullopt);
  }

 protected:
  // PasswordFormHelper for testing.
  PasswordFormHelper* helper_;
  password_manager::MockPasswordManager password_manager_;
};

struct FindPasswordFormTestData {
  // HTML String of the form.
  NSString* html_string;
  // True if expected to find the form.
  const bool expected_form_found;
  // Expected number of fields in found form.
  const size_t expected_number_of_fields;
  // Expected form name.
  const char* expected_form_name;
};

// Check that HTML forms are converted correctly into PasswordForms.
TEST_F(PasswordFormHelperTest, FindPasswordFormsInView) {
  // clang-format off
  const FindPasswordFormTestData test_data[] = {
    // Normal form: a username and a password element.
    {
      @"<form name='form1'>"
      "<input type='text' name='user0'>"
      "<input type='password' name='pass0'>"
      "</form>",
      true, 2, "form1"
    },
    // User name is captured as an email address (HTML5).
    {
      @"<form name='form1'>"
      "<input type='email' name='email1'>"
      "<input type='password' name='pass1'>"
      "</form>",
      true, 2, "form1"
    },
    // No form found.
    {
      @"<div>",
      false, 0, nullptr
    },
    // Disabled username element.
    {
      @"<form name='form1'>"
      "<input type='text' name='user2' disabled='disabled'>"
      "<input type='password' name='pass2'>"
      "</form>",
      true, 2, "form1"
    },
    // No password element.
    {
      @"<form name='form1'>"
      "<input type='text' name='user3'>"
      "</form>",
      false, 0, nullptr
    },
    // No <form> tag.
    {
      @"<input type='email' name='email1'>"
      "<input type='password' name='pass1'>",
      true, 2, ""
    },
  };
  // clang-format on

  for (const FindPasswordFormTestData& data : test_data) {
    SCOPED_TRACE(testing::Message()
                 << "for html_string="
                 << base::SysNSStringToUTF8(data.html_string));
    LoadHtml(data.html_string);
    __block std::vector<FormData> forms;
    __block BOOL block_was_called = NO;
    [helper_ findPasswordFormsInFrame:GetMainFrame()
                    completionHandler:^(const std::vector<FormData>& result) {
                      block_was_called = YES;
                      forms = result;
                    }];
    EXPECT_TRUE(
        WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^bool() {
          return block_was_called;
        }));
    if (data.expected_form_found) {
      ASSERT_EQ(1U, forms.size());
      EXPECT_EQ(data.expected_number_of_fields, forms[0].fields().size());
      EXPECT_EQ(data.expected_form_name, base::UTF16ToUTF8(forms[0].name()));
    } else {
      ASSERT_TRUE(forms.empty());
    }
  }
}

// A script that resets all text fields, including those in iframes.
static NSString* kClearInputFieldsScript =
    @"function clearInputFields(win) {"
     "  var inputs = win.document.getElementsByTagName('input');"
     "  for (var i = 0; i < inputs.length; i++) {"
     "    inputs[i].value = '';"
     "  }"
     "  var frames = win.frames;"
     "  for (var i = 0; i < frames.length; i++) {"
     "    clearInputFields(frames[i]);"
     "  }"
     "}"
     "clearInputFields(window);";

// A script that runs after autofilling forms.  It returns ids and values of all
// non-empty fields, including those in iframes.
static NSString* kInputFieldValueVerificationScript =
    @"function findAllInputsInFrame(win, prefix) {"
     "  var result = '';"
     "  var inputs = win.document.getElementsByTagName('input');"
     "  for (var i = 0; i < inputs.length; i++) {"
     "    var input = inputs[i];"
     "    if (input.value) {"
     "      result += prefix + input.id + '=' + input.value + ';';"
     "    }"
     "  }"
     "  var frames = win.frames;"
     "  for (var i = 0; i < frames.length; i++) {"
     "    result += findAllInputsInFrame("
     "        frames[i], prefix + frames[i].name +'.');"
     "  }"
     "  return result;"
     "};"
     "function findAllInputs(win) {"
     "  return findAllInputsInFrame(win, '');"
     "};"
     "findAllInputs(window);";

// Tests that filling password forms with fill data works correctly.
TEST_F(PasswordFormHelperTest, FillPasswordFormWithFillData_Success) {
  ukm::TestAutoSetUkmRecorder test_recorder;
  base::HistogramTester histogram_tester;
  LoadHtml(
      @"<form><input id='u1' type='text' name='un1'>"
       "<input id='p1' type='password' name='pw1'></form>");

  ASSERT_TRUE(SetUpUniqueIDs());
  const std::string base_url = BaseUrl();
  FormRendererId form_id(1);
  FieldRendererId username_field_id(2);
  const std::u16string username_value = u"john.doe@gmail.com";
  FieldRendererId password_field_id(3);
  const std::u16string password_value = u"super!secret";
  FillData fill_data;
  SetFillData(base_url, form_id.value(), username_field_id.value(),
              base::UTF16ToUTF8(username_value).c_str(),
              password_field_id.value(),
              base::UTF16ToUTF8(password_value).c_str(), &fill_data);

  web::WebFrame* frame = GetMainFrame();

  // Expect calls to the PasswordManager to update its state from the filled
  // inputs.
  IOSPasswordManagerDriver* driver =
      IOSPasswordManagerDriverFactory::FromWebStateAndWebFrame(web_state(),
                                                               frame);
  auto* field_data_manager =
      autofill::FieldDataManagerFactoryIOS::FromWebFrame(frame);
  EXPECT_CALL(
      password_manager_,
      UpdateStateOnUserInput(driver, ::testing::Ref(*field_data_manager),
                             std::make_optional<FormRendererId>(form_id),
                             username_field_id, username_value));
  EXPECT_CALL(
      password_manager_,
      UpdateStateOnUserInput(driver, ::testing::Ref(*field_data_manager),
                             std::make_optional<FormRendererId>(form_id),
                             password_field_id, password_value));

  __block bool called = false;
  __block BOOL succeeded = false;
  [helper_ fillPasswordFormWithFillData:fill_data
                                inFrame:frame
                       triggeredOnField:username_field_id
                      completionHandler:^(BOOL success) {
                        called = true;
                        succeeded = success;
                      }];

  // Wait on the JS call to be completed.
  EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
    return called;
  }));

  // Verify that the completion callback is called with success as a result.
  EXPECT_TRUE(succeeded);

  // Verify that the username and password inputs were filled with their
  // respective value.
  id result = ExecuteJavaScript(kInputFieldValueVerificationScript);
  EXPECT_NSEQ(@"u1=john.doe@gmail.com;p1=super!secret;", result);

  // Check that username and password fields were updated as filled in the
  // FieldDataManager.
  autofill::FieldDataManager* fieldDataManager =
      autofill::FieldDataManagerFactoryIOS::FromWebFrame(GetMainFrame());
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(username_field_id));
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(password_field_id));

  // Verify that the fill operation was recorded as a success.
  histogram_tester.ExpectUniqueSample("PasswordManager.FillingSuccessIOS", true,
                                      1);

  // Check recorded UKM.
  auto entries = test_recorder.GetEntriesByName(
      ukm::builders::PasswordManager_PasswordFillingIOS::kEntryName);
  // Expect one recorded metric.
  ASSERT_EQ(1u, entries.size());
  test_recorder.ExpectEntryMetric(entries[0], "FillingSuccess", true);
}

// Tests that filling password form data can succeeds while not filling
// anything.
TEST_F(PasswordFormHelperTest, FillPasswordFormWithFillData_Success_NoFill) {
  ukm::TestAutoSetUkmRecorder test_recorder;
  base::HistogramTester histogram_tester;
  // Set the username field as disabled and set a value for the password so it
  // is possible to have a succesful fill operation that returns success but
  // that doesn't fill anything in reality.
  LoadHtml(@"<form><input id='u1' type='text' name='un1' value='test-username' "
           @"disabled>"
            "<input id='p1' type='password' name='pw1' "
            "value='test-password'></form>");

  ASSERT_TRUE(SetUpUniqueIDs());

  // Don't expect calls to the PasswordManager to update its state when failure
  // to fill.
  EXPECT_CALL(password_manager_, UpdateStateOnUserInput).Times(0);

  const std::string base_url = BaseUrl();
  FieldRendererId username_field_id(2);
  FieldRendererId password_field_id(3);
  FillData fill_data;
  // Set fill data with a password that is the same as the one that is already
  // in the password field.
  SetFillData(base_url, 1, username_field_id.value(), "test-username",
              password_field_id.value(), "test-password", &fill_data);

  __block bool called = false;
  __block BOOL succeeded = false;
  [helper_ fillPasswordFormWithFillData:fill_data
                                inFrame:GetMainFrame()
                       triggeredOnField:username_field_id
                      completionHandler:^(BOOL success) {
                        called = true;
                        succeeded = success;
                      }];

  // Wait on the JS call to be completed.
  EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
    return called;
  }));

  // Verify that the completion callback is called with success as the result.
  EXPECT_TRUE(succeeded);

  // Verify that the username and password inputs still hold their value.
  id result = ExecuteJavaScript(kInputFieldValueVerificationScript);
  EXPECT_NSEQ(@"u1=test-username;p1=test-password;", result);

  // Check that username and password fields were still updated even if they
  // were not filled. This odd behavior is kept to not skew metrics downstream
  // (e.g. PasswordManager.FillingAssistance).
  autofill::FieldDataManager* fieldDataManager =
      autofill::FieldDataManagerFactoryIOS::FromWebFrame(GetMainFrame());
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(username_field_id));
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(password_field_id));

  // Verify that the fill operation was recorded as a success.
  histogram_tester.ExpectUniqueSample("PasswordManager.FillingSuccessIOS", true,
                                      1);

  // Check recorded UKM.
  auto entries = test_recorder.GetEntriesByName(
      ukm::builders::PasswordManager_PasswordFillingIOS::kEntryName);
  // Expect one recorded metric.
  ASSERT_EQ(1u, entries.size());
  test_recorder.ExpectEntryMetric(entries[0], "FillingSuccess", true);
}

// Tests that failure in filling password forms with fill data is correctly
// handled.
TEST_F(PasswordFormHelperTest, FillPasswordFormWithFillData_Failure) {
  ukm::TestAutoSetUkmRecorder test_recorder;
  base::HistogramTester histogram_tester;

  LoadHtml(@"<form><input id='p1' type='password' name='pw1'></form>");
  web::WebFrame* frame = GetMainFrame();

  ASSERT_TRUE(SetUpUniqueIDs());

  // Don't expect calls to the PasswordManager to update its state when failure
  // to fill.
  EXPECT_CALL(password_manager_, UpdateStateOnUserInput).Times(0);

  const std::string base_url = BaseUrl();
  FieldRendererId username_field_id(0);
  // The password renderer id does not exist, that's why the filling will fail
  FieldRendererId password_field_id(404);
  FillData fill_data;
  SetFillData(base_url, 1, username_field_id.value(), "",
              password_field_id.value(), "super!secret", &fill_data);

  __block bool called = false;
  __block BOOL succeeded = false;
  [helper_ fillPasswordFormWithFillData:fill_data
                                inFrame:frame
                       triggeredOnField:username_field_id
                      completionHandler:^(BOOL success) {
                        called = true;
                        succeeded = success;
                      }];

  // Wait on the JS call to be completed.
  EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
    return called;
  }));

  // Verify that the completion callback is called with failure.
  EXPECT_FALSE(succeeded);

  // Check that username and password fields were NOT updated as filled in the
  // FieldDataManager.
  autofill::FieldDataManager* fieldDataManager =
      autofill::FieldDataManagerFactoryIOS::FromWebFrame(frame);
  EXPECT_FALSE(fieldDataManager->WasAutofilledOnUserTrigger(username_field_id));
  EXPECT_FALSE(fieldDataManager->WasAutofilledOnUserTrigger(password_field_id));

  // Verify that the fill operation was recorded as a failure.
  histogram_tester.ExpectUniqueSample("PasswordManager.FillingSuccessIOS",
                                      false, 1);
  // Check recorded UKM.
  auto entries = test_recorder.GetEntriesByName(
      ukm::builders::PasswordManager_PasswordFillingIOS::kEntryName);
  // Expect one recorded metric.
  ASSERT_EQ(1u, entries.size());
  test_recorder.ExpectEntryMetric(entries[0], "FillingSuccess", false);
}

// Tests that a form with username typed by user is not refilled when
// the user selects a filling suggestion on password field.
TEST_F(PasswordFormHelperTest,
       FillPasswordIntoForm_UserTypedUsername_FillFromPassword) {
  LoadHtml(@"<form><input id='u1' type='text' name='un1'>"
            "<input id='p1' type='password' name='pw1'></form>");

  ASSERT_TRUE(SetUpUniqueIDs());

  FormRendererId form_id(1);
  FieldRendererId username_field_id(2);
  const std::u16string username_value = u"john.doe@gmail.com";
  FieldRendererId password_field_id(3);
  const std::u16string password_value = u"store!pw";

  web::WebFrame* frame = GetMainFrame();

  // Type on username field.
  ExecuteJavaScript(
      @"document.getElementById('u1').value = 'typed@typed.com';");
  [helper_ updateFieldDataOnUserInput:username_field_id
                              inFrame:GetMainFrame()
                           inputValue:@"typed@typed.com"];

  // Try to autofill the form.
  FillData fill_data;
  SetFillData(BaseUrl(), form_id.value(), username_field_id.value(),
              base::UTF16ToUTF8(username_value).c_str(),
              password_field_id.value(),
              base::UTF16ToUTF8(password_value).c_str(), &fill_data);

  IOSPasswordManagerDriver* driver =
      IOSPasswordManagerDriverFactory::FromWebStateAndWebFrame(web_state(),
                                                               frame);
  auto* field_data_manager =
      autofill::FieldDataManagerFactoryIOS::FromWebFrame(frame);
  // Don't expect to update the state for the username field because it was
  // skipped.
  EXPECT_CALL(
      password_manager_,
      UpdateStateOnUserInput(driver, ::testing::Ref(*field_data_manager),
                             std::make_optional<FormRendererId>(form_id),
                             username_field_id, username_value))
      .Times(0);
  // Expect a state update on the password field.
  EXPECT_CALL(
      password_manager_,
      UpdateStateOnUserInput(driver, ::testing::Ref(*field_data_manager),
                             std::make_optional<FormRendererId>(form_id),
                             password_field_id, password_value));

  __block bool called = NO;
  __block bool succeeded = NO;
  [helper_ fillPasswordFormWithFillData:fill_data
                                inFrame:GetMainFrame()
                       triggeredOnField:password_field_id
                      completionHandler:^(BOOL success) {
                        called = YES;
                        succeeded = success;
                      }];

  // Wait on the JS call to be completed.
  EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
    return called;
  }));

  // Verify that the completion callback is called with success as the result.
  EXPECT_TRUE(succeeded);

  // Check that the password field was updated as filled in the
  // FieldDataManager but not the username as it wasn't filled.
  autofill::FieldDataManager* fieldDataManager =
      autofill::FieldDataManagerFactoryIOS::FromWebFrame(GetMainFrame());
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(username_field_id));
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(password_field_id));

  // Verify that the password was filled.
  id result = ExecuteJavaScript(kInputFieldValueVerificationScript);
  EXPECT_NSEQ(@"u1=typed@typed.com;p1=store!pw;", result);
}

// Tests that a form with username typed by user is overriden by fill when
// the user selects a filling suggestion on usernmae field.
TEST_F(PasswordFormHelperTest,
       FillPasswordIntoForm_UserTypedUsername_FillFromUsername) {
  LoadHtml(@"<form><input id='u1' type='text' name='un1'>"
            "<input id='p1' type='password' name='pw1'></form>");
  ASSERT_TRUE(SetUpUniqueIDs());

  const std::string base_url = BaseUrl();
  FieldRendererId username_field_id(2);
  FieldRendererId password_field_id(3);

  // Type on username field.
  ExecuteJavaScript(
      @"document.getElementById('u1').value = 'typed@typed.com';");
  [helper_ updateFieldDataOnUserInput:username_field_id
                              inFrame:GetMainFrame()
                           inputValue:@"typed@typed.com"];

  FillData fill_data;
  SetFillData(base_url, 1, username_field_id.value(), "john.doe@gmail.com",
              password_field_id.value(), "super!secret", &fill_data);
  __block bool called = false;
  __block BOOL succeeded = false;
  [helper_ fillPasswordFormWithFillData:fill_data
                                inFrame:GetMainFrame()
                       triggeredOnField:username_field_id
                      completionHandler:^(BOOL success) {
                        called = true;
                        succeeded = success;
                      }];

  // Wait on the JS call to be completed.
  EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
    return called;
  }));

  // Verify that the completion callback is called with success as a result.
  EXPECT_TRUE(succeeded);

  // Verify that the username and password inputs were filled with their
  // respective value.
  id result = ExecuteJavaScript(kInputFieldValueVerificationScript);
  EXPECT_NSEQ(@"u1=john.doe@gmail.com;p1=super!secret;", result);

  // Check that username and password fields were updated as filled in the
  // FieldDataManager.
  autofill::FieldDataManager* fieldDataManager =
      autofill::FieldDataManagerFactoryIOS::FromWebFrame(GetMainFrame());
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(username_field_id));
  EXPECT_TRUE(fieldDataManager->WasAutofilledOnUserTrigger(password_field_id));
}

//

// Tests that filling is considered as a failure if the returned result blob
// from the js call is missing a field or is of the wrong type. Tests all
// possibilities in the same test to avoid over boilerplating.
TEST_F(PasswordFormHelperTest, FillUsernameAndPassword_MissingFillResultField) {
  const std::string base_url = BaseUrl();
  web::FakeWebState fake_web_state;
  auto* feature =
      password_manager::PasswordManagerJavaScriptFeature::GetInstance();
  web::ContentWorld content_world = feature->GetSupportedContentWorld();

  // Add feature to support the filling of password form data.
  web::test::OverrideJavaScriptFeatures(GetBrowserState(), {feature});

  auto web_frames_manager = std::make_unique<web::FakeWebFramesManager>();
  auto* web_frames_manager_ptr = web_frames_manager.get();
  fake_web_state.SetWebFramesManager(content_world,
                                     std::move(web_frames_manager));
  fake_web_state.SetBrowserState(GetBrowserState());
  fake_web_state.SetContentIsHTML(true);
  fake_web_state.SetCurrentURL(GURL(base_url));

  std::unique_ptr<web::FakeWebFrame> main_frame =
      web::FakeWebFrame::Create("frameID", true, GURL(base_url));
  main_frame->set_browser_state(GetBrowserState());
  auto* main_frame_ptr = main_frame.get();
  web_frames_manager_ptr->AddWebFrame(std::move(main_frame));

  IOSPasswordManagerDriverFactory::CreateForWebState(
      &fake_web_state, OCMStrictClassMock([SharedPasswordController class]),
      &password_manager_);
  // Don't expect calls to the PasswordManager to update its state when failure
  // to fill.
  EXPECT_CALL(password_manager_, UpdateStateOnUserInput).Times(0);

  FieldRendererId username_field_id(2);
  FieldRendererId password_field_id(3);
  FillData fill_data;
  SetFillData(base_url, 1, username_field_id.value(), "test-username",
              password_field_id.value(), "super!secret", &fill_data);

  PasswordFormHelper* helper =
      [[PasswordFormHelper alloc] initWithWebState:&fake_web_state];

  // Test the missing did_fill_username field.
  {
    auto result = base::Value(base::Value::Dict()
                                  .Set("did_fill_password", base::Value(true))
                                  .Set("did_attempt_fill", base::Value(true)));
    main_frame_ptr->AddJsResultForFunctionCall(&result,
                                               "passwords.fillPasswordForm");
    __block bool called = false;
    __block BOOL succeeded = false;
    [helper fillPasswordFormWithFillData:fill_data
                                 inFrame:main_frame_ptr
                        triggeredOnField:username_field_id
                       completionHandler:^(BOOL success) {
                         called = true;
                         succeeded = success;
                       }];
    WaitForBackgroundTasks();
    ASSERT_TRUE(called);
    EXPECT_FALSE(succeeded);
  }

  // Test the missing did_fill_password field.
  {
    auto result = base::Value(base::Value::Dict()
                                  .Set("did_fill_username", base::Value(true))
                                  .Set("did_attempt_fill", base::Value(true)));
    main_frame_ptr->AddJsResultForFunctionCall(&result,
                                               "passwords.fillPasswordForm");
    __block bool called = false;
    __block BOOL succeeded = false;
    [helper fillPasswordFormWithFillData:fill_data
                                 inFrame:main_frame_ptr
                        triggeredOnField:username_field_id
                       completionHandler:^(BOOL success) {
                         called = true;
                         succeeded = success;
                       }];
    WaitForBackgroundTasks();
    ASSERT_TRUE(called);
    EXPECT_FALSE(succeeded);
  }

  // Test the missing did_attempt_fill field.
  {
    auto result = base::Value(base::Value::Dict()
                                  .Set("did_fill_username", base::Value(true))
                                  .Set("did_fill_password", base::Value(true)));
    main_frame_ptr->AddJsResultForFunctionCall(&result,
                                               "passwords.fillPasswordForm");
    __block bool called = false;
    __block BOOL succeeded = false;
    [helper fillPasswordFormWithFillData:fill_data
                                 inFrame:main_frame_ptr
                        triggeredOnField:username_field_id
                       completionHandler:^(BOOL success) {
                         called = true;
                         succeeded = success;
                       }];
    WaitForBackgroundTasks();
    ASSERT_TRUE(called);
    EXPECT_FALSE(succeeded);
  }

  // Test the wrong type of returned result.
  {
    base::Value result("string-result");
    main_frame_ptr->AddJsResultForFunctionCall(&result,
                                               "passwords.fillPasswordForm");
    __block bool called = false;
    __block BOOL succeeded = false;
    [helper fillPasswordFormWithFillData:fill_data
                                 inFrame:main_frame_ptr
                        triggeredOnField:username_field_id
                       completionHandler:^(BOOL success) {
                         called = true;
                         succeeded = success;
                       }];
    WaitForBackgroundTasks();
    ASSERT_TRUE(called);
    EXPECT_FALSE(succeeded);
  }

  // Test a nullptr result.
  {
    main_frame_ptr->AddJsResultForFunctionCall(nullptr,
                                               "passwords.fillPasswordForm");
    __block bool called = false;
    __block BOOL succeeded = false;
    [helper fillPasswordFormWithFillData:fill_data
                                 inFrame:main_frame_ptr
                        triggeredOnField:username_field_id
                       completionHandler:^(BOOL success) {
                         called = true;
                         succeeded = success;
                       }];
    WaitForBackgroundTasks();
    ASSERT_TRUE(called);
    EXPECT_FALSE(succeeded);
  }
}

// Tests that extractPasswordFormData extracts wanted form on page with mutiple
// forms.
TEST_F(PasswordFormHelperTest, ExtractPasswordFormData) {
  LoadHtml(@"<form><input id='u1' type='text' name='un1'>"
            "<input id='p1' type='password' name='pw1'></form>"
            "<form><input id='u2' type='text' name='un2'>"
            "<input id='p2' type='password' name='pw2'></form>"
            "<form><input id='u3' type='text' name='un3'>"
            "<input id='p3' type='password' name='pw3'></form>");

  ASSERT_TRUE(SetUpUniqueIDs());

  __block int call_counter = 0;
  __block int success_counter = 0;
  __block FormData result = FormData();
  [helper_ extractPasswordFormData:FormRendererId(1)
                           inFrame:GetMainFrame()
                 completionHandler:^(BOOL complete, const FormData& form) {
                   ++call_counter;
                   if (complete) {
                     ++success_counter;
                     result = form;
                   }
                 }];
  EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
    return call_counter == 1;
  }));
  EXPECT_EQ(1, success_counter);
  EXPECT_EQ(result.renderer_id(), FormRendererId(1));

  call_counter = 0;
  success_counter = 0;
  result = FormData();

  [helper_ extractPasswordFormData:FormRendererId(404)
                           inFrame:GetMainFrame()
                 completionHandler:^(BOOL complete, const FormData& form) {
                   ++call_counter;
                   if (complete) {
                     ++success_counter;
                     result = form;
                   }
                 }];
  EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
    return call_counter == 1;
  }));
  EXPECT_EQ(0, success_counter);
}

// Tests that the form submit message is fully handled when in the correct
// format and with the minimal viable content.
TEST_F(PasswordFormHelperTest, HandleFormSubmittedMessage) {
  id delegate = OCMStrictProtocolMock(@protocol(PasswordFormHelperDelegate));
  OCMExpect([[delegate ignoringNonObjectArgs] formHelper:helper_
                                           didSubmitForm:FormData()
                                                 inFrame:GetMainFrame()]);
  helper_.delegate = delegate;

  LoadHtml(@"<p>");

  // Set a message with the minimal viable body to succeed in form data
  // extraction.
  web::ScriptMessage submit_message = ScriptMessageForSubmit(
      ValidFormSubmittedMessageBody(GetMainFrame()->GetFrameId()));

  HandleSubmittedFormStatus status =
      [helper_ handleFormSubmittedMessage:submit_message];

  EXPECT_EQ(HandleSubmittedFormStatus::kHandled, status);

  EXPECT_OCMOCK_VERIFY(delegate);
}

// Tests that the form submit message isn't handled when the message isn't in
// the correct format.
TEST_F(PasswordFormHelperTest, HandleFormSubmittedMessage_InvalidFormat) {
  // Set the delegate mock in a way that the test will crash if there is any
  // delegate call to handle the message.
  id delegate = OCMStrictProtocolMock(@protocol(PasswordFormHelperDelegate));
  helper_.delegate = delegate;

  LoadHtml(@"<p>");

  // Set the message value content as a string which is an invalid format
  // because a dictionary is expected.
  auto invalid_body =
      std::make_unique<base::Value>(base::Value("invalid_because_expect_dict"));

  web::ScriptMessage submit_message =
      ScriptMessageForSubmit(std::move(invalid_body));

  HandleSubmittedFormStatus status =
      [helper_ handleFormSubmittedMessage:submit_message];

  EXPECT_EQ(HandleSubmittedFormStatus::kRejectedMessageBodyNotADict, status);

  // Verify that the delegate is never called because the message isn't handled
  // because of the early return.
  EXPECT_OCMOCK_VERIFY(delegate);
}

// Tests that the form submit message isn't handled when there is no trusted URL
// loaded in the webstate.
TEST_F(PasswordFormHelperTest, HandleFormSubmittedMessage_NoTrustedUrl) {
  FakeWebStateWithoutTrustedCommittedUrl web_state;
  PasswordFormHelper* helper =
      [[PasswordFormHelper alloc] initWithWebState:&web_state];

  // Set the delegate mock in a way that the test will crash if there is any
  // delegate call to handle the message.
  id delegate = OCMStrictProtocolMock(@protocol(PasswordFormHelperDelegate));
  helper.delegate = delegate;

  // Set a dummy message.
  web::ScriptMessage submit_message =
      ScriptMessageForSubmit(std::make_unique<base::Value>("whatever"));

  HandleSubmittedFormStatus status =
      [helper handleFormSubmittedMessage:submit_message];

  EXPECT_EQ(HandleSubmittedFormStatus::kRejectedNoTrustedUrl, status);

  // Verify that the delegate is never called because the message isn't handled
  // because of the early return.
  EXPECT_OCMOCK_VERIFY(delegate);
}

// Tests that the form submit message isn't handled when there is no webstate.
TEST_F(PasswordFormHelperTest, HandleFormSubmittedMessage_NoWebState) {
  // Set the delegate mock in a way that the test will crash if there is any
  // delegate call to handle the message.
  id delegate = OCMStrictProtocolMock(@protocol(PasswordFormHelperDelegate));
  helper_.delegate = delegate;

  // Set a dummy message.
  web::ScriptMessage submit_message =
      ScriptMessageForSubmit(std::make_unique<base::Value>("whatever"));

  // Destroying the webstate will nullify the webstate pointer in the helper.
  DestroyWebState();

  HandleSubmittedFormStatus status =
      [helper_ handleFormSubmittedMessage:submit_message];

  EXPECT_EQ(HandleSubmittedFormStatus::kRejectedNoWebState, status);

  // Verify that the delegate is never called because the message isn't handled
  // because of the early return.
  EXPECT_OCMOCK_VERIFY(delegate);
}

// Tests that the form submit message isn't handled when there is no frame
// matching the provided frame id.
TEST_F(PasswordFormHelperTest, HandleFormSubmittedMessage_NoFormMatchingId) {
  id delegate = OCMStrictProtocolMock(@protocol(PasswordFormHelperDelegate));
  helper_.delegate = delegate;

  LoadHtml(@"<p>");

  // Set a message with an nonexisting frame id.
  web::ScriptMessage submit_message = ScriptMessageForSubmit(
      ValidFormSubmittedMessageBody("nonexisting_frame_id"));

  HandleSubmittedFormStatus status =
      [helper_ handleFormSubmittedMessage:submit_message];

  EXPECT_EQ(HandleSubmittedFormStatus::kRejectedNoFrameMatchingId, status);

  EXPECT_OCMOCK_VERIFY(delegate);
}

// Tests that the form submit message isn't handled when form data can't be
// extracted from the message's body.
TEST_F(PasswordFormHelperTest, HandleFormSubmittedMessage_CantExtractFormData) {
  id delegate = OCMStrictProtocolMock(@protocol(PasswordFormHelperDelegate));
  helper_.delegate = delegate;

  LoadHtml(@"<p>");

  auto incomplete_message_body = std::make_unique<base::Value>(
      base::Value::Dict().Set("host_frame", GetMainFrame()->GetFrameId()));

  // Set a message with an incomplete body that misses the required keys to be
  // parsed to form data.
  web::ScriptMessage submit_message =
      ScriptMessageForSubmit(std::move(incomplete_message_body));

  HandleSubmittedFormStatus status =
      [helper_ handleFormSubmittedMessage:submit_message];

  EXPECT_EQ(HandleSubmittedFormStatus::kRejectedCantExtractFormData, status);

  EXPECT_OCMOCK_VERIFY(delegate);
}

}  // namespace

NS_ASSUME_NONNULL_END