autofill_browsertest.cc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "weblayer/test/weblayer_browser_test.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "build/build_config.h"
  7. #include "components/autofill/core/common/form_data.h"
  8. #include "components/autofill/core/common/form_field_data.h"
  9. #include "net/test/embedded_test_server/embedded_test_server.h"
  10. #include "weblayer/browser/tab_impl.h"
  11. #include "weblayer/shell/browser/shell.h"
  12. #include "weblayer/test/weblayer_browser_test_utils.h"
  13. namespace weblayer {
  14. namespace {
  15. // Method that is passed to the autofill system to be invoked on detection of
  16. // autofill forms.
  17. // NOTE: This method can currently be invoked only once within the context of
  18. // a given test. If that restriction ever needs to be relaxed, it could be
  19. // done by changing |quit_closure| to a global that could be reset between
  20. // expected invocations of the method.
  21. void OnReceivedFormDataFromRenderer(base::OnceClosure quit_closure,
  22. autofill::FormData* output,
  23. const autofill::FormData& form) {
  24. ASSERT_TRUE(quit_closure);
  25. *output = form;
  26. std::move(quit_closure).Run();
  27. }
  28. } // namespace
  29. // Cross-platform tests of autofill parsing in the renderer and communication
  30. // to the browser. Does not test integration with any platform's underlying
  31. // system autofill mechanisms.
  32. class AutofillBrowserTest : public WebLayerBrowserTest {
  33. public:
  34. AutofillBrowserTest() = default;
  35. AutofillBrowserTest(const AutofillBrowserTest&) = delete;
  36. AutofillBrowserTest& operator=(const AutofillBrowserTest&) = delete;
  37. ~AutofillBrowserTest() override = default;
  38. void SetUp() override {
  39. #if BUILDFLAG(IS_ANDROID)
  40. TabImpl::DisableAutofillSystemIntegrationForTesting();
  41. #endif
  42. WebLayerBrowserTest::SetUp();
  43. }
  44. };
  45. // Tests that the renderer detects a password form and passes the appropriate
  46. // data to the browser.
  47. IN_PROC_BROWSER_TEST_F(AutofillBrowserTest, TestPasswordFormDetection) {
  48. ASSERT_TRUE(embedded_test_server()->Start());
  49. base::RunLoop run_loop;
  50. autofill::FormData observed_form;
  51. InitializeAutofillWithEventForwarding(
  52. shell(), base::BindRepeating(&OnReceivedFormDataFromRenderer,
  53. run_loop.QuitClosure(), &observed_form));
  54. GURL password_form_url =
  55. embedded_test_server()->GetURL("/simple_password_form.html");
  56. NavigateAndWaitForCompletion(password_form_url, shell());
  57. // Focus the username field (note that a user gesture is necessary for
  58. // autofill to trigger) ...
  59. ExecuteScriptWithUserGesture(
  60. shell(), "document.getElementById('username_field').focus();");
  61. // ... and wait for the parsed data to be passed to the browser.
  62. run_loop.Run();
  63. // Verify that that the form data matches that of the document.
  64. EXPECT_EQ(u"testform", observed_form.name);
  65. EXPECT_EQ(password_form_url.spec(), observed_form.url);
  66. auto fields = observed_form.fields;
  67. EXPECT_EQ(2u, fields.size());
  68. autofill::FormFieldData username_field = fields[0];
  69. EXPECT_EQ(u"username_field", username_field.name);
  70. autofill::FormFieldData password_field = fields[1];
  71. EXPECT_EQ(u"password_field", password_field.name);
  72. }
  73. } // namespace weblayer