Chromium Code Reviews| 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 = 'password' id = 'password'/>" | |
| 67 " <INPUT type = 'password' id = 'confirm_password'/>" | |
| 68 " <INPUT type = 'submit' id = 'submit' />" | |
| 69 "</FORM>"; | |
| 70 | |
| 71 const char kSignupFormWithManyCheckboxesHTML[] = | |
| 72 "<FORM id = 'test_form'> " | |
| 73 " </SELECT>" | |
| 74 " <INPUT type = 'text' id = 'username' />" | |
| 75 " <INPUT type = 'password' id = 'password' />" | |
| 76 " <INPUT type = 'checkbox' id = 'subscribe_science' />" | |
| 77 " <INPUT type = 'checkbox' id = 'subscribe_music' />" | |
| 78 " <INPUT type = 'checkbox' id = 'subscribe_sport' />" | |
| 79 " <INPUT type = 'submit' id = 'submit' />" | |
| 80 "</FORM>"; | |
| 81 | |
| 82 const char kSignupFormWithOtherFieldsHTML[] = | |
| 83 "<FORM id = 'test_form'> " | |
| 84 " <INPUT type = 'text' id = 'username' />" | |
| 85 " <INPUT type = 'password' id = 'password' />" | |
| 86 " <INPUT type = 'color' id = 'account_color' />" | |
| 87 " <INPUT type = 'date' id = 'date_of_birth' />" | |
| 88 " <INPUT type = 'submit' id = 'submit' />" | |
| 89 "</FORM>"; | |
| 90 | |
| 91 const char kSigninFormWithTextFeatureInInputElementHTML[] = | |
| 92 "<FORM id = 'test_form'> " | |
| 93 " <INPUT type = 'text' id = 'username' class = 'sign-in_field' />" | |
| 94 " <INPUT type = 'password' id = 'password' class = 'sign-in_field' />" | |
| 95 " <INPUT type = 'submit' id = 'submit' />" | |
| 96 "</FORM>"; | |
| 97 | |
| 98 const char kSigninFormWithTextFeatureInFormTagHTML[] = | |
| 99 "<FORM id = 'test_form' some_attribute='auth_form' > " | |
| 100 " <INPUT type = 'text' id = 'username' />" | |
| 101 " <INPUT type = 'password' id = 'password' />" | |
| 102 " <INPUT type = 'submit' id = 'submit' />" | |
| 103 "</FORM>"; | |
| 104 | |
| 105 const char kSigninAndSignupFormsInOneDivHTML[] = | |
| 106 "<DIV class = 'signup_signin_container'>" | |
| 107 " <FORM id = 'test_form' class = 'unknown_form_type' > " | |
| 108 " <INPUT type = 'text' id = 'username' />" | |
| 109 " <INPUT type = 'password' id = 'password' />" | |
| 110 " <INPUT type = 'submit' id = 'submit' />" | |
| 111 " </FORM>" | |
| 112 " <FORM id = 'another_form' class = 'unknown_form_type' > " | |
| 113 " <INPUT type = 'text' id = 'username' />" | |
| 114 " <INPUT type = 'password' id = 'password' />" | |
| 115 " <INPUT type = 'submit' id = 'submit' />" | |
| 116 " </FORM>" | |
| 117 "</DIV>"; | |
| 118 | |
| 119 const char kSigninFormWithTextFeatureInEnclosingParentHTML[] = | |
| 120 "<DIV id = 'signup_signin_container'>" | |
| 121 " <DIV id = 'signin_wraper'>" | |
| 122 " <FORM id = 'test_form' > " | |
| 123 " <INPUT type = 'text' id = 'username' />" | |
| 124 " <INPUT type = 'password' id = 'password' />" | |
| 125 " <INPUT type = 'submit' id = 'submit' />" | |
| 126 " </FORM>" | |
| 127 " </DIV>" | |
| 128 "</DIV>"; | |
| 129 | |
| 130 const char kSigninFormWithInvisibleFieldsHTML[] = | |
| 131 "<FORM id = 'test_form'> " | |
| 132 " <INPUT type = 'text' id = 'username' />" | |
| 133 " <INPUT type = 'password' id = 'password' />" | |
| 134 " <INPUT type = 'input' hidden id = 'hidden_field1' />" | |
| 135 " <INPUT type = 'password' hidden id = 'hidden_field2'/>" | |
| 136 " <INPUT type = 'submit' id = 'submit' />" | |
| 137 "</FORM>"; | |
| 138 | |
| 139 const char kSignupFormWithSigninButtonHTML[] = | |
| 140 "<FORM id = 'test_form' >" | |
| 141 " <INPUT type = 'text' id = 'username' />" | |
| 142 " <INPUT type = 'password' id = 'password' />" | |
| 143 " <INPUT type = 'password' id = 'confirm_password' />" | |
| 144 " <INPUT type = 'submit' id = 'submit' />" | |
| 145 " <INPUT type = 'button' id = 'goto_signin_form' />" | |
| 146 " <INPUT type = 'image' id = 'goto_auth_form' />" | |
| 147 "</FORM>"; | |
| 148 | |
| 149 const char kSomeFormWithoutPasswordFields[] = | |
| 150 "<FORM id = 'test_form' >" | |
| 151 " <INPUT type = 'text' id = 'username' />" | |
| 152 " <INPUT type = 'text' id = 'fullname' />" | |
| 153 " <INPUT type = 'text' id = 'address' />" | |
| 154 " <INPUT type = 'text' id = 'phone' />" | |
| 155 " <INPUT type = 'submit' id = 'submit' />" | |
| 156 "</FORM>"; | |
| 157 | |
| 158 const char kSignupFormWithSigninTextFeatureAndManyFieldsHTML[] = | |
| 159 "<FORM id = 'test_form' class = 'log-on_container'> " | |
| 160 " <INPUT type = 'text' id = 'fullname' />" | |
| 161 " <INPUT type = 'text' id = 'username' />" | |
| 162 " <INPUT type = 'password' id = 'password' />" | |
| 163 " <INPUT type = 'submit' id = 'submit' />" | |
| 164 "</FORM>"; | |
| 165 | |
| 166 const char kChangeFormWithTreePasswordFieldsHTML[] = | |
| 167 "<FORM id = 'test_form' >" | |
| 168 " <INPUT type = 'password' id = 'old_password' />" | |
| 169 " <INPUT type = 'password' id = 'password' />" | |
| 170 " <INPUT type = 'password' id = 'confirm_password' />" | |
| 171 " <INPUT type = 'submit' id = 'submit' />" | |
| 172 "</FORM>"; | |
| 173 | |
| 174 TEST_F(FormClassifierTest, SigninForm) { | |
| 175 // Signin form with as many as possible visible elements, | |
| 176 // i.e. if one more text/password/checkbox/other field is added, the form | |
|
vabr (Chromium)
2016/06/10 13:22:11
nit: Please keep horizontal spaces to one characte
kolos1
2016/06/13 14:27:34
Removed double space.
Did I exceed 80 character
vabr (Chromium)
2016/06/13 16:01:57
No, you did not, quite the opposite :), which was
| |
| 177 // will be recognized as a signup form. | |
| 178 LoadHTML(kSigninFormHTML); | |
| 179 std::string generation_field; | |
| 180 EXPECT_FALSE(GetGenerationField(&generation_field)); | |
| 181 } | |
| 182 | |
| 183 TEST_F(FormClassifierTest, SignupFormWithSeveralTextFields) { | |
| 184 LoadHTML(kSignupFormWithSeveralTextFieldsFormHTML); | |
| 185 std::string generation_field; | |
| 186 EXPECT_TRUE(GetGenerationField(&generation_field)); | |
| 187 EXPECT_EQ("password", generation_field); | |
| 188 } | |
| 189 | |
| 190 TEST_F(FormClassifierTest, SignupFormWithSeveralPasswordFieldsHTML) { | |
| 191 LoadHTML(kSignupFormWithSeveralPasswordFieldsHTML); | |
| 192 std::string generation_field; | |
| 193 EXPECT_TRUE(GetGenerationField(&generation_field)); | |
| 194 EXPECT_EQ("password", generation_field); | |
| 195 } | |
| 196 | |
| 197 TEST_F(FormClassifierTest, SignupFormWithManyCheckboxesHTML) { | |
| 198 LoadHTML(kSignupFormWithManyCheckboxesHTML); | |
| 199 std::string generation_field; | |
| 200 EXPECT_TRUE(GetGenerationField(&generation_field)); | |
| 201 EXPECT_EQ("password", generation_field); | |
| 202 } | |
| 203 | |
| 204 TEST_F(FormClassifierTest, SignupFormWithOtherFieldsHTML) { | |
| 205 LoadHTML(kSignupFormWithOtherFieldsHTML); | |
| 206 std::string generation_field; | |
| 207 EXPECT_TRUE(GetGenerationField(&generation_field)); | |
| 208 EXPECT_EQ("password", generation_field); | |
| 209 } | |
| 210 | |
| 211 TEST_F(FormClassifierTest, SigninFormWithTextFeatureInInputElementHTML) { | |
| 212 LoadHTML(kSigninFormWithTextFeatureInInputElementHTML); | |
| 213 std::string generation_field; | |
| 214 EXPECT_FALSE(GetGenerationField(&generation_field)); | |
| 215 } | |
| 216 | |
| 217 TEST_F(FormClassifierTest, SigninFormWithTextFeatureInFormTagHTML) { | |
| 218 LoadHTML(kSigninFormWithTextFeatureInFormTagHTML); | |
| 219 std::string generation_field; | |
| 220 EXPECT_FALSE(GetGenerationField(&generation_field)); | |
| 221 } | |
| 222 | |
| 223 TEST_F(FormClassifierTest, SigninAndSignupFormsInOneDivHTML) { | |
| 224 LoadHTML(kSigninAndSignupFormsInOneDivHTML); | |
| 225 std::string generation_field; | |
| 226 EXPECT_FALSE(GetGenerationField(&generation_field)); | |
| 227 } | |
| 228 | |
| 229 TEST_F(FormClassifierTest, SigninFormWithTextFeatureInEnclosingParentHTML) { | |
| 230 LoadHTML(kSigninFormWithTextFeatureInEnclosingParentHTML); | |
| 231 std::string generation_field; | |
| 232 EXPECT_FALSE(GetGenerationField(&generation_field)); | |
| 233 } | |
| 234 | |
| 235 TEST_F(FormClassifierTest, SigninFormWithInvisibleFieldsHTML) { | |
| 236 LoadHTML(kSigninFormWithInvisibleFieldsHTML); | |
| 237 std::string generation_field; | |
| 238 EXPECT_FALSE(GetGenerationField(&generation_field)); | |
| 239 } | |
| 240 | |
| 241 TEST_F(FormClassifierTest, SignupFormWithSigninButtonHTML) { | |
| 242 LoadHTML(kSignupFormWithSigninButtonHTML); | |
| 243 std::string generation_field; | |
| 244 EXPECT_TRUE(GetGenerationField(&generation_field)); | |
| 245 EXPECT_EQ("password", generation_field); | |
| 246 } | |
| 247 | |
| 248 TEST_F(FormClassifierTest, SomeFormWithoutPasswordFields) { | |
| 249 LoadHTML(kSomeFormWithoutPasswordFields); | |
| 250 std::string generation_field; | |
| 251 EXPECT_FALSE(GetGenerationField(&generation_field)); | |
| 252 } | |
| 253 | |
| 254 TEST_F(FormClassifierTest, SignupFormWithSigninTextFeatureAndManyFieldsHTML) { | |
| 255 // Even if there is signin text feature, the number of fields is more reliable | |
| 256 // signal of signup form. So, this form should classified as signup. | |
|
vabr (Chromium)
2016/06/10 13:22:11
typo: should classified -> should be classified
kolos1
2016/06/13 14:27:34
Done.
| |
| 257 LoadHTML(kSignupFormWithSigninTextFeatureAndManyFieldsHTML); | |
| 258 std::string generation_field; | |
| 259 EXPECT_TRUE(GetGenerationField(&generation_field)); | |
| 260 EXPECT_EQ("password", generation_field); | |
| 261 } | |
| 262 | |
| 263 TEST_F(FormClassifierTest, kChangeFormWithTreePasswordFieldsHTML) { | |
| 264 LoadHTML(kChangeFormWithTreePasswordFieldsHTML); | |
| 265 std::string generation_field; | |
| 266 EXPECT_TRUE(GetGenerationField(&generation_field)); | |
| 267 EXPECT_EQ("password", generation_field); | |
| 268 } | |
| 269 | |
| 270 } // namespace autofill | |
| OLD | NEW |