summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/autofill_browsertest.cc
blob: 15cb638d0f1f3b1181e017588a1fa923e447091e (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "weblayer/test/weblayer_browser_test.h"

#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/weblayer_browser_test_utils.h"

namespace weblayer {

namespace {

// Method that is passed to the autofill system to be invoked on detection of
// autofill forms.
// NOTE: This method can currently be invoked only once within the context of
// a given test. If that restriction ever needs to be relaxed, it could be
// done by changing |quit_closure| to a global that could be reset between
// expected invocations of the method.
void OnReceivedFormDataFromRenderer(base::OnceClosure quit_closure,
                                    autofill::FormData* output,
                                    const autofill::FormData& form) {
  ASSERT_TRUE(quit_closure);

  *output = form;
  std::move(quit_closure).Run();
}

}  // namespace

// Cross-platform tests of autofill parsing in the renderer and communication
// to the browser. Does not test integration with any platform's underlying
// system autofill mechanisms.
class AutofillBrowserTest : public WebLayerBrowserTest {
 public:
  AutofillBrowserTest() = default;
  ~AutofillBrowserTest() override = default;

  void SetUp() override {
#if defined(OS_ANDROID)
    TabImpl::DisableAutofillSystemIntegrationForTesting();
#endif

    WebLayerBrowserTest::SetUp();
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(AutofillBrowserTest);
};

// Tests that the renderer detects a password form and passes the appropriate
// data to the browser.
IN_PROC_BROWSER_TEST_F(AutofillBrowserTest, TestPasswordFormDetection) {
  ASSERT_TRUE(embedded_test_server()->Start());

  base::RunLoop run_loop;
  autofill::FormData observed_form;

  InitializeAutofillWithEventForwarding(
      shell(), base::BindRepeating(&OnReceivedFormDataFromRenderer,
                                   run_loop.QuitClosure(), &observed_form));

  GURL password_form_url =
      embedded_test_server()->GetURL("/simple_password_form.html");
  NavigateAndWaitForCompletion(password_form_url, shell());

  // Focus the username field (note that a user gesture is necessary for
  // autofill to trigger) ...
  ExecuteScriptWithUserGesture(
      shell(), "document.getElementById('username_field').focus();");

  // ... and wait for the parsed data to be passed to the browser.
  run_loop.Run();

  // Verify that that the form data matches that of the document.
  EXPECT_EQ(base::ASCIIToUTF16("testform"), observed_form.name);
  EXPECT_EQ(password_form_url.spec(), observed_form.url);

  auto fields = observed_form.fields;
  EXPECT_EQ(2u, fields.size());
  autofill::FormFieldData username_field = fields[0];
  EXPECT_EQ(base::ASCIIToUTF16("username_field"), username_field.name);
  autofill::FormFieldData password_field = fields[1];
  EXPECT_EQ(base::ASCIIToUTF16("password_field"), password_field.name);
}

}  // namespace weblayer