OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/memory/scoped_vector.h" | 6 #include "base/memory/scoped_vector.h" |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/common/autofill_messages.h" | 8 #include "chrome/common/autofill_messages.h" |
9 #include "chrome/renderer/autofill/password_generation_manager.h" | 9 #include "chrome/renderer/autofill/password_generation_manager.h" |
10 #include "chrome/test/base/chrome_render_view_test.h" | 10 #include "chrome/test/base/chrome_render_view_test.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 const char kSigninFormHTML[] = | 87 const char kSigninFormHTML[] = |
88 "<FORM name = 'blah' action = 'http://www.random.com/'> " | 88 "<FORM name = 'blah' action = 'http://www.random.com/'> " |
89 " <INPUT type = 'text' id = 'username'/> " | 89 " <INPUT type = 'text' id = 'username'/> " |
90 " <INPUT type = 'password' id = 'password'/> " | 90 " <INPUT type = 'password' id = 'password'/> " |
91 " <INPUT type = 'submit' value = 'LOGIN' />" | 91 " <INPUT type = 'submit' value = 'LOGIN' />" |
92 "</FORM>"; | 92 "</FORM>"; |
93 | 93 |
94 const char kAccountCreationFormHTML[] = | 94 const char kAccountCreationFormHTML[] = |
95 "<FORM name = 'blah' action = 'http://www.random.com/'> " | 95 "<FORM name = 'blah' action = 'http://www.random.com/'> " |
96 " <INPUT type = 'text' id = 'username'/> " | 96 " <INPUT type = 'text' id = 'username'/> " |
| 97 " <INPUT type = 'password' id = 'first_password' size=5/> " |
| 98 " <INPUT type = 'password' id = 'second_password' size=5/> " |
| 99 " <INPUT type = 'submit' value = 'LOGIN' />" |
| 100 "</FORM>"; |
| 101 |
| 102 const char kHiddenPasswordAccountCreationFormHTML[] = |
| 103 "<FORM name = 'blah' action = 'http://www.random.com/'> " |
| 104 " <INPUT type = 'text' id = 'username'/> " |
| 105 " <INPUT type = 'password' id = 'first_password'/> " |
| 106 " <INPUT type = 'password' id = 'second_password' style='display:none'/> " |
| 107 " <INPUT type = 'submit' value = 'LOGIN' />" |
| 108 "</FORM>"; |
| 109 |
| 110 const char kInvalidActionAccountCreationFormHTML[] = |
| 111 "<FORM name = 'blah' action = 'invalid'> " |
| 112 " <INPUT type = 'text' id = 'username'/> " |
97 " <INPUT type = 'password' id = 'first_password'/> " | 113 " <INPUT type = 'password' id = 'first_password'/> " |
98 " <INPUT type = 'password' id = 'second_password'/> " | 114 " <INPUT type = 'password' id = 'second_password'/> " |
99 " <INPUT type = 'submit' value = 'LOGIN' />" | 115 " <INPUT type = 'submit' value = 'LOGIN' />" |
100 "</FORM>"; | 116 "</FORM>"; |
101 | 117 |
102 TEST_F(PasswordGenerationManagerTest, DetectionTest) { | 118 TEST_F(PasswordGenerationManagerTest, DetectionTest) { |
103 LoadHTML(kSigninFormHTML); | 119 LoadHTML(kSigninFormHTML); |
104 | 120 |
105 WebDocument document = GetMainFrame()->document(); | 121 WebDocument document = GetMainFrame()->document(); |
106 WebElement element = | 122 WebElement element = |
107 document.getElementById(WebString::fromUTF8("password")); | 123 document.getElementById(WebString::fromUTF8("password")); |
108 ASSERT_FALSE(element.isNull()); | 124 ASSERT_FALSE(element.isNull()); |
109 WebInputElement password_element = element.to<WebInputElement>(); | 125 WebInputElement password_element = element.to<WebInputElement>(); |
110 EXPECT_FALSE(DecorationIsVisible(&password_element)); | 126 EXPECT_FALSE(DecorationIsVisible(&password_element)); |
111 | 127 |
112 LoadHTML(kAccountCreationFormHTML); | 128 LoadHTML(kAccountCreationFormHTML); |
113 | 129 |
114 // We don't show the decoration yet because the feature isn't enabled. | 130 // We don't show the decoration yet because the feature isn't enabled. |
115 document = GetMainFrame()->document(); | 131 document = GetMainFrame()->document(); |
116 element = document.getElementById(WebString::fromUTF8("first_password")); | 132 element = document.getElementById(WebString::fromUTF8("first_password")); |
117 ASSERT_FALSE(element.isNull()); | 133 ASSERT_FALSE(element.isNull()); |
118 WebInputElement first_password_element = element.to<WebInputElement>(); | 134 WebInputElement first_password_element = element.to<WebInputElement>(); |
119 element = document.getElementById(WebString::fromUTF8("second_password")); | |
120 ASSERT_FALSE(element.isNull()); | |
121 WebInputElement second_password_element = element.to<WebInputElement>(); | |
122 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); | 135 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); |
123 | 136 |
124 // Pretend like password generation was enabled. | 137 // Pretend like password generation was enabled. |
125 AutofillMsg_PasswordGenerationEnabled msg(0, true); | 138 AutofillMsg_PasswordGenerationEnabled msg(0, true); |
126 generation_manager_->OnMessageReceived(msg); | 139 generation_manager_->OnMessageReceived(msg); |
127 | 140 |
128 // Now check that the decoration is visible on the first password field and | 141 // Now check that the decoration is visible on the first password field and |
129 // that we send a message after the decoration is clicked. | 142 // that we send a message after the decoration is clicked. |
130 LoadHTML(kAccountCreationFormHTML); | 143 LoadHTML(kAccountCreationFormHTML); |
131 document = GetMainFrame()->document(); | 144 document = GetMainFrame()->document(); |
132 element = document.getElementById(WebString::fromUTF8("first_password")); | 145 element = document.getElementById(WebString::fromUTF8("first_password")); |
133 ASSERT_FALSE(element.isNull()); | 146 ASSERT_FALSE(element.isNull()); |
134 first_password_element = element.to<WebInputElement>(); | 147 first_password_element = element.to<WebInputElement>(); |
135 element = document.getElementById(WebString::fromUTF8("second_password")); | |
136 ASSERT_FALSE(element.isNull()); | |
137 second_password_element = element.to<WebInputElement>(); | |
138 EXPECT_TRUE(DecorationIsVisible(&first_password_element)); | 148 EXPECT_TRUE(DecorationIsVisible(&first_password_element)); |
139 SimulateClickOnDecoration(&first_password_element); | 149 SimulateClickOnDecoration(&first_password_element); |
140 EXPECT_EQ(1u, generation_manager_->messages().size()); | 150 EXPECT_EQ(1u, generation_manager_->messages().size()); |
141 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, | 151 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, |
142 generation_manager_->messages()[0]->type()); | 152 generation_manager_->messages()[0]->type()); |
| 153 |
| 154 // This doesn't trigger because hidden password fields are ignored. |
| 155 LoadHTML(kHiddenPasswordAccountCreationFormHTML); |
| 156 document = GetMainFrame()->document(); |
| 157 element = document.getElementById(WebString::fromUTF8("first_password")); |
| 158 ASSERT_FALSE(element.isNull()); |
| 159 first_password_element = element.to<WebInputElement>(); |
| 160 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); |
| 161 |
| 162 // This doesn't trigger because the form action is invalid. |
| 163 LoadHTML(kInvalidActionAccountCreationFormHTML); |
| 164 document = GetMainFrame()->document(); |
| 165 element = document.getElementById(WebString::fromUTF8("first_password")); |
| 166 ASSERT_FALSE(element.isNull()); |
| 167 first_password_element = element.to<WebInputElement>(); |
| 168 EXPECT_FALSE(DecorationIsVisible(&first_password_element)); |
143 } | 169 } |
144 | 170 |
145 TEST_F(PasswordGenerationManagerTest, FillTest) { | 171 TEST_F(PasswordGenerationManagerTest, FillTest) { |
146 // Make sure that we are enabled before loading HTML. | 172 // Make sure that we are enabled before loading HTML. |
147 AutofillMsg_PasswordGenerationEnabled enabled_msg(0, true); | 173 AutofillMsg_PasswordGenerationEnabled enabled_msg(0, true); |
148 generation_manager_->OnMessageReceived(enabled_msg); | 174 generation_manager_->OnMessageReceived(enabled_msg); |
149 LoadHTML(kAccountCreationFormHTML); | 175 LoadHTML(kAccountCreationFormHTML); |
150 | 176 |
151 WebDocument document = GetMainFrame()->document(); | 177 WebDocument document = GetMainFrame()->document(); |
152 WebElement element = | 178 WebElement element = |
(...skipping 13 matching lines...) Expand all Loading... |
166 generation_manager_->OnMessageReceived(msg); | 192 generation_manager_->OnMessageReceived(msg); |
167 | 193 |
168 // Password fields are filled out and set as being autofilled. | 194 // Password fields are filled out and set as being autofilled. |
169 EXPECT_EQ(password, first_password_element.value()); | 195 EXPECT_EQ(password, first_password_element.value()); |
170 EXPECT_EQ(password, second_password_element.value()); | 196 EXPECT_EQ(password, second_password_element.value()); |
171 EXPECT_TRUE(first_password_element.isAutofilled()); | 197 EXPECT_TRUE(first_password_element.isAutofilled()); |
172 EXPECT_TRUE(second_password_element.isAutofilled()); | 198 EXPECT_TRUE(second_password_element.isAutofilled()); |
173 } | 199 } |
174 | 200 |
175 } // namespace autofill | 201 } // namespace autofill |
OLD | NEW |