OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "base/macros.h" |
| 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/test/base/chrome_render_view_test.h" |
| 8 #include "components/autofill/content/renderer/form_classifier.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/WebKit/public/web/WebDocument.h" |
| 11 #include "third_party/WebKit/public/web/WebFormElement.h" |
| 12 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 13 |
| 14 namespace autofill { |
| 15 |
| 16 class FormClassifierTest : public ChromeRenderViewTest { |
| 17 public: |
| 18 FormClassifierTest() {} |
| 19 |
| 20 void TearDown() override { |
| 21 LoadHTML(""); |
| 22 ChromeRenderViewTest::TearDown(); |
| 23 } |
| 24 |
| 25 bool GetGenerationField(std::string* generation_field) { |
| 26 blink::WebDocument document = GetMainFrame()->document(); |
| 27 blink::WebFormElement form = |
| 28 document.getElementById("test_form").to<blink::WebFormElement>(); |
| 29 base::string16 generation_field16; |
| 30 bool generation_availalbe = |
| 31 ClassifyFormAndFindGenerationField(form, &generation_field16); |
| 32 *generation_field = base::UTF16ToUTF8(generation_field16); |
| 33 return generation_availalbe; |
| 34 } |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(FormClassifierTest); |
| 38 }; |
| 39 |
| 40 const char kSigninFormHTML[] = |
| 41 "<FORM id = 'test_form'> " |
| 42 " <SELECT id='account_type'>" |
| 43 " <OPTION value = 'personal'>" |
| 44 " <OPTION value = 'corporate'>" |
| 45 " </SELECT>" |
| 46 " <INPUT type = 'text' id = 'username'/>" |
| 47 " <INPUT type = 'password' id = 'password'/>" |
| 48 " <INPUT type = 'checkbox' id = 'remember_me'/>" |
| 49 " <INPUT type = 'checkbox' id = 'secure_login'/>" |
| 50 " <INPUT type = 'submit' id = 'signin' />" |
| 51 " <INPUT type = 'hidden' id = 'ignore_this' />" |
| 52 " <INPUT type = 'hidden' id = 'ignore_this_too' />" |
| 53 " <INPUT type = 'submit' id = 'submit' />" |
| 54 "</FORM>"; |
| 55 |
| 56 const char kSignupFormWithSeveralTextFieldsFormHTML[] = |
| 57 "<FORM id = 'test_form'> " |
| 58 " <INPUT type = 'text' id = 'full_name'/>" |
| 59 " <INPUT type = 'text' id = 'username'/>" |
| 60 " <INPUT type = 'password' id = 'password'/>" |
| 61 " <INPUT type = 'submit' id = 'submit' />" |
| 62 "</FORM>"; |
| 63 |
| 64 const char kSignupFormWithSeveralPasswordFieldsHTML[] = |
| 65 "<FORM id = 'test_form'> " |
| 66 " <INPUT type = 'text' id = 'username'/>" |
| 67 " <INPUT type = 'password' id = 'password'/>" |
| 68 " <INPUT type = 'password' id = 'confirm_password'/>" |
| 69 " <INPUT type = 'submit' id = 'submit' />" |
| 70 "</FORM>"; |
| 71 |
| 72 const char kSignupFormWithManyCheckboxesHTML[] = |
| 73 "<FORM id = 'test_form'> " |
| 74 " </SELECT>" |
| 75 " <INPUT type = 'text' id = 'username' />" |
| 76 " <INPUT type = 'password' id = 'password' />" |
| 77 " <INPUT type = 'checkbox' id = 'subscribe_science' />" |
| 78 " <INPUT type = 'checkbox' id = 'subscribe_music' />" |
| 79 " <INPUT type = 'checkbox' id = 'subscribe_sport' />" |
| 80 " <INPUT type = 'submit' id = 'submit' />" |
| 81 "</FORM>"; |
| 82 |
| 83 const char kSignupFormWithOtherFieldsHTML[] = |
| 84 "<FORM id = 'test_form'> " |
| 85 " <INPUT type = 'text' id = 'username' />" |
| 86 " <INPUT type = 'password' id = 'password' />" |
| 87 " <INPUT type = 'color' id = 'account_color' />" |
| 88 " <INPUT type = 'date' id = 'date_of_birth' />" |
| 89 " <INPUT type = 'submit' id = 'submit' />" |
| 90 "</FORM>"; |
| 91 |
| 92 const char kSignupFormWithTextFeatureInInputElementHTML[] = |
| 93 "<FORM id = 'test_form'> " |
| 94 " <INPUT type = 'text' id = 'username' class = 'sign-up_field' />" |
| 95 " <INPUT type = 'password' id = 'password' class = 'sign-up_field' />" |
| 96 " <INPUT type = 'submit' id = 'submit' />" |
| 97 "</FORM>"; |
| 98 |
| 99 const char kSignupFormWithTextFeatureInFormTagHTML[] = |
| 100 "<FORM id = 'test_form' some_attribute='sign_up_form' > " |
| 101 " <INPUT type = 'text' id = 'username' />" |
| 102 " <INPUT type = 'password' id = 'password' />" |
| 103 " <INPUT type = 'submit' id = 'submit' />" |
| 104 "</FORM>"; |
| 105 |
| 106 const char kSigninAndSignupFormsInOneDivHTML[] = |
| 107 "<DIV class = 'signup_signin_container'>" |
| 108 " <FORM id = 'test_form' class = 'unknown_form_type' > " |
| 109 " <INPUT type = 'text' id = 'username' />" |
| 110 " <INPUT type = 'password' id = 'password' />" |
| 111 " <INPUT type = 'submit' id = 'submit' />" |
| 112 " </FORM>" |
| 113 " <FORM id = 'another_form' class = 'unknown_form_type' > " |
| 114 " <INPUT type = 'text' id = 'username' />" |
| 115 " <INPUT type = 'password' id = 'password' />" |
| 116 " <INPUT type = 'submit' id = 'submit' />" |
| 117 " </FORM>" |
| 118 "</DIV>"; |
| 119 |
| 120 const char kSigninFormWithTextFeatureInEnclosingParentHTML[] = |
| 121 "<DIV id = 'signup_signin_container'>" |
| 122 " <DIV id = 'sign_in_wraper'>" |
| 123 " <FORM id = 'test_form' > " |
| 124 " <INPUT type = 'text' id = 'username' />" |
| 125 " <INPUT type = 'password' id = 'password' />" |
| 126 " <INPUT type = 'submit' id = 'submit' />" |
| 127 " </FORM>" |
| 128 " </DIV>" |
| 129 "</DIV>"; |
| 130 |
| 131 const char kSigninFormWithInvisibleFieldsHTML[] = |
| 132 "<FORM id = 'test_form'> " |
| 133 " <INPUT type = 'text' id = 'username' />" |
| 134 " <INPUT type = 'password' id = 'password' />" |
| 135 " <INPUT type = 'input' hidden id = 'hidden_field1' />" |
| 136 " <INPUT type = 'password' hidden id = 'hidden_field2'/>" |
| 137 " <INPUT type = 'submit' id = 'submit' />" |
| 138 "</FORM>"; |
| 139 |
| 140 const char kSignupFormWithSigninButtonHTML[] = |
| 141 "<FORM id = 'test_form' >" |
| 142 " <INPUT type = 'text' id = 'username' />" |
| 143 " <INPUT type = 'password' id = 'password' />" |
| 144 " <INPUT type = 'password' id = 'confirm_password' />" |
| 145 " <INPUT type = 'submit' id = 'submit' />" |
| 146 " <INPUT type = 'button' id = 'goto_signin_form' />" |
| 147 " <INPUT type = 'image' id = 'goto_auth_form' />" |
| 148 "</FORM>"; |
| 149 |
| 150 const char kSomeFormWithoutPasswordFields[] = |
| 151 "<FORM id = 'test_form' >" |
| 152 " <INPUT type = 'text' id = 'username' />" |
| 153 " <INPUT type = 'text' id = 'fullname' />" |
| 154 " <INPUT type = 'text' id = 'address' />" |
| 155 " <INPUT type = 'text' id = 'phone' />" |
| 156 " <INPUT type = 'submit' id = 'submit' />" |
| 157 "</FORM>"; |
| 158 |
| 159 const char kSignupFormWithSigninTextFeatureAndManyFieldsHTML[] = |
| 160 "<FORM id = 'test_form' class = 'log-on_container'> " |
| 161 " <INPUT type = 'text' id = 'fullname' />" |
| 162 " <INPUT type = 'text' id = 'username' />" |
| 163 " <INPUT type = 'password' id = 'password' />" |
| 164 " <INPUT type = 'submit' id = 'submit' />" |
| 165 "</FORM>"; |
| 166 |
| 167 const char kChangeFormWithTreePasswordFieldsHTML[] = |
| 168 "<FORM id = 'test_form' >" |
| 169 " <INPUT type = 'password' id = 'old_password' />" |
| 170 " <INPUT type = 'password' id = 'password' />" |
| 171 " <INPUT type = 'password' id = 'confirm_password' />" |
| 172 " <INPUT type = 'submit' id = 'submit' />" |
| 173 "</FORM>"; |
| 174 |
| 175 TEST_F(FormClassifierTest, SigninForm) { |
| 176 // Signin form with as many as possible visible elements, |
| 177 // i.e. if one more text/password/checkbox/other field is added, the form |
| 178 // will be recognized as a signup form. |
| 179 LoadHTML(kSigninFormHTML); |
| 180 std::string generation_field; |
| 181 EXPECT_FALSE(GetGenerationField(&generation_field)); |
| 182 } |
| 183 |
| 184 TEST_F(FormClassifierTest, SignupFormWithSeveralTextFields) { |
| 185 LoadHTML(kSignupFormWithSeveralTextFieldsFormHTML); |
| 186 std::string generation_field; |
| 187 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 188 EXPECT_EQ("password", generation_field); |
| 189 } |
| 190 |
| 191 TEST_F(FormClassifierTest, SignupFormWithSeveralPasswordFieldsHTML) { |
| 192 LoadHTML(kSignupFormWithSeveralPasswordFieldsHTML); |
| 193 std::string generation_field; |
| 194 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 195 EXPECT_EQ("password", generation_field); |
| 196 } |
| 197 |
| 198 TEST_F(FormClassifierTest, SignupFormWithManyCheckboxesHTML) { |
| 199 LoadHTML(kSignupFormWithManyCheckboxesHTML); |
| 200 std::string generation_field; |
| 201 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 202 EXPECT_EQ("password", generation_field); |
| 203 } |
| 204 |
| 205 TEST_F(FormClassifierTest, SignupFormWithOtherFieldsHTML) { |
| 206 LoadHTML(kSignupFormWithOtherFieldsHTML); |
| 207 std::string generation_field; |
| 208 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 209 EXPECT_EQ("password", generation_field); |
| 210 } |
| 211 |
| 212 TEST_F(FormClassifierTest, SignupFormWithTextFeatureInInputElementHTML) { |
| 213 LoadHTML(kSignupFormWithTextFeatureInInputElementHTML); |
| 214 std::string generation_field; |
| 215 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 216 EXPECT_EQ("password", generation_field); |
| 217 } |
| 218 |
| 219 TEST_F(FormClassifierTest, SigninFormWithTextFeatureInFormTagHTML) { |
| 220 LoadHTML(kSignupFormWithTextFeatureInFormTagHTML); |
| 221 std::string generation_field; |
| 222 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 223 EXPECT_EQ("password", generation_field); |
| 224 } |
| 225 |
| 226 TEST_F(FormClassifierTest, SigninAndSignupFormsInOneDivHTML) { |
| 227 LoadHTML(kSigninAndSignupFormsInOneDivHTML); |
| 228 std::string generation_field; |
| 229 EXPECT_FALSE(GetGenerationField(&generation_field)); |
| 230 } |
| 231 |
| 232 TEST_F(FormClassifierTest, SigninFormWithTextFeatureInEnclosingParentHTML) { |
| 233 LoadHTML(kSigninFormWithTextFeatureInEnclosingParentHTML); |
| 234 std::string generation_field; |
| 235 EXPECT_FALSE(GetGenerationField(&generation_field)); |
| 236 } |
| 237 |
| 238 TEST_F(FormClassifierTest, SigninFormWithInvisibleFieldsHTML) { |
| 239 LoadHTML(kSigninFormWithInvisibleFieldsHTML); |
| 240 std::string generation_field; |
| 241 EXPECT_FALSE(GetGenerationField(&generation_field)); |
| 242 } |
| 243 |
| 244 TEST_F(FormClassifierTest, SignupFormWithSigninButtonHTML) { |
| 245 LoadHTML(kSignupFormWithSigninButtonHTML); |
| 246 std::string generation_field; |
| 247 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 248 EXPECT_EQ("password", generation_field); |
| 249 } |
| 250 |
| 251 TEST_F(FormClassifierTest, SomeFormWithoutPasswordFields) { |
| 252 LoadHTML(kSomeFormWithoutPasswordFields); |
| 253 std::string generation_field; |
| 254 EXPECT_FALSE(GetGenerationField(&generation_field)); |
| 255 } |
| 256 |
| 257 TEST_F(FormClassifierTest, SignupFormWithSigninTextFeatureAndManyFieldsHTML) { |
| 258 // Even if there is signin text feature, the number of fields is more reliable |
| 259 // signal of signup form. So, this form should be classified as signup. |
| 260 LoadHTML(kSignupFormWithSigninTextFeatureAndManyFieldsHTML); |
| 261 std::string generation_field; |
| 262 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 263 EXPECT_EQ("password", generation_field); |
| 264 } |
| 265 |
| 266 TEST_F(FormClassifierTest, kChangeFormWithTreePasswordFieldsHTML) { |
| 267 LoadHTML(kChangeFormWithTreePasswordFieldsHTML); |
| 268 std::string generation_field; |
| 269 EXPECT_TRUE(GetGenerationField(&generation_field)); |
| 270 EXPECT_EQ("password", generation_field); |
| 271 } |
| 272 |
| 273 } // namespace autofill |
OLD | NEW |