1 // Copyright 2013 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.
5 #include "base/string16.h"
6 #include "base/utf_string_conversions.h"
7 #include "content/public/common/password_form.h"
8 #include "content/public/renderer/password_form_conversion_utils.h"
9 #include "content/public/test/render_view_test.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPasswordFormData.h"
16 using WebKit::WebFormElement
;
17 using WebKit::WebFrame
;
18 using WebKit::WebPasswordFormData
;
19 using WebKit::WebVector
;
24 class PasswordFormConversionUtilsTest
: public RenderViewTest
{
26 PasswordFormConversionUtilsTest() : RenderViewTest() {}
27 virtual ~PasswordFormConversionUtilsTest() {}
30 DISALLOW_COPY_AND_ASSIGN(PasswordFormConversionUtilsTest
);
35 TEST_F(PasswordFormConversionUtilsTest
, ValidWebFormElementToPasswordForm
) {
36 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
37 " <INPUT type=\"text\" name=\"username\" "
38 " id=\"username\" value=\"johnsmith\"/>"
39 " <INPUT type=\"submit\" name=\"submit\" value=\"Submit\"/>"
40 " <INPUT type=\"password\" name=\"password\" id=\"password\" "
44 WebFrame
* frame
= GetMainFrame();
45 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
47 WebVector
<WebFormElement
> forms
;
48 frame
->document().forms(forms
);
49 ASSERT_EQ(1U, forms
.size());
50 WebPasswordFormData
web_password_form(forms
[0]);
51 ASSERT_TRUE(web_password_form
.isValid());
53 scoped_ptr
<PasswordForm
> password_form
= CreatePasswordForm(forms
[0]);
54 ASSERT_NE(static_cast<PasswordForm
*>(NULL
), password_form
.get());
56 EXPECT_EQ("data:", password_form
->signon_realm
);
57 EXPECT_EQ(GURL("http://cnn.com"), password_form
->action
);
58 EXPECT_EQ(UTF8ToUTF16("username"), password_form
->username_element
);
59 EXPECT_EQ(UTF8ToUTF16("johnsmith"), password_form
->username_value
);
60 EXPECT_EQ(UTF8ToUTF16("password"), password_form
->password_element
);
61 EXPECT_EQ(UTF8ToUTF16("secret"), password_form
->password_value
);
62 EXPECT_EQ(PasswordForm::SCHEME_HTML
, password_form
->scheme
);
63 EXPECT_FALSE(password_form
->ssl_valid
);
64 EXPECT_FALSE(password_form
->preferred
);
65 EXPECT_FALSE(password_form
->blacklisted_by_user
);
66 EXPECT_EQ(PasswordForm::TYPE_MANUAL
, password_form
->type
);
69 TEST_F(PasswordFormConversionUtilsTest
, InvalidWebFormElementToPasswordForm
) {
70 LoadHTML("<FORM name=\"TestForm\" action=\"invalid\" method=\"post\">"
71 " <INPUT type=\"text\" name=\"username\" "
72 " id=\"username\" value=\"johnsmith\"/>"
73 " <INPUT type=\"submit\" name=\"submit\" value=\"Submit\"/>"
74 " <INPUT type=\"password\" name=\"password\" id=\"password\" "
78 WebFrame
* frame
= GetMainFrame();
79 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
81 WebVector
<WebFormElement
> forms
;
82 frame
->document().forms(forms
);
83 ASSERT_EQ(1U, forms
.size());
84 WebPasswordFormData
web_password_form(forms
[0]);
85 ASSERT_FALSE(web_password_form
.isValid());
87 scoped_ptr
<PasswordForm
> password_form
= CreatePasswordForm(forms
[0]);
88 EXPECT_EQ(static_cast<PasswordForm
*>(NULL
), password_form
.get());
91 } // namespace content