- add sources.
[platform/framework/web/crosswalk.git] / src / components / autofill / core / common / password_form_fill_data_unittest.cc
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.
4
5 #include "components/autofill/core/common/password_form_fill_data.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "components/autofill/core/common/password_form.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace autofill {
13
14 // Tests that the when there is a single preferred match, and no extra
15 // matches, the PasswordFormFillData is filled in correctly.
16 TEST(PasswordFormFillDataTest, TestSinglePreferredMatch) {
17   // Create the current form on the page.
18   PasswordForm form_on_page;
19   form_on_page.origin = GURL("https://foo.com/");
20   form_on_page.action = GURL("https://foo.com/login");
21   form_on_page.username_element = ASCIIToUTF16("username");
22   form_on_page.username_value = ASCIIToUTF16("test@gmail.com");
23   form_on_page.password_element = ASCIIToUTF16("password");
24   form_on_page.password_value = ASCIIToUTF16("test");
25   form_on_page.submit_element = ASCIIToUTF16("");
26   form_on_page.signon_realm = "https://foo.com/";
27   form_on_page.ssl_valid = true;
28   form_on_page.preferred = false;
29   form_on_page.scheme = PasswordForm::SCHEME_HTML;
30
31   // Create an exact match in the database.
32   PasswordForm preferred_match;
33   preferred_match.origin = GURL("https://foo.com/");
34   preferred_match.action = GURL("https://foo.com/login");
35   preferred_match.username_element = ASCIIToUTF16("username");
36   preferred_match.username_value = ASCIIToUTF16("test@gmail.com");
37   preferred_match.password_element = ASCIIToUTF16("password");
38   preferred_match.password_value = ASCIIToUTF16("test");
39   preferred_match.submit_element = ASCIIToUTF16("");
40   preferred_match.signon_realm = "https://foo.com/";
41   preferred_match.ssl_valid = true;
42   preferred_match.preferred = true;
43   preferred_match.scheme = PasswordForm::SCHEME_HTML;
44
45   PasswordFormMap matches;
46
47   PasswordFormFillData result;
48   InitPasswordFormFillData(form_on_page,
49                            matches,
50                            &preferred_match,
51                            true,
52                            false,
53                            &result);
54
55   // |wait_for_username| should reflect the |wait_for_username_before_autofill|
56   // argument of InitPasswordFormFillData which in this case is true.
57   EXPECT_TRUE(result.wait_for_username);
58   // The preferred realm should be empty since it's the same as the realm of
59   // the form.
60   EXPECT_EQ(result.preferred_realm, "");
61
62   PasswordFormFillData result2;
63   InitPasswordFormFillData(form_on_page,
64                            matches,
65                            &preferred_match,
66                            false,
67                            false,
68                            &result2);
69
70   // |wait_for_username| should reflect the |wait_for_username_before_autofill|
71   // argument of InitPasswordFormFillData which in this case is false.
72   EXPECT_FALSE(result2.wait_for_username);
73 }
74
75 // Tests that the InitPasswordFormFillData behaves correctly when there is a
76 // preferred match that was found using public suffix matching, an additional
77 // result that also used public suffix matching, and a third result that was
78 // found without using public suffix matching.
79 TEST(PasswordFormFillDataTest, TestPublicSuffixDomainMatching) {
80   // Create the current form on the page.
81   PasswordForm form_on_page;
82   form_on_page.origin = GURL("https://foo.com/");
83   form_on_page.action = GURL("https://foo.com/login");
84   form_on_page.username_element = ASCIIToUTF16("username");
85   form_on_page.username_value = ASCIIToUTF16("test@gmail.com");
86   form_on_page.password_element = ASCIIToUTF16("password");
87   form_on_page.password_value = ASCIIToUTF16("test");
88   form_on_page.submit_element = ASCIIToUTF16("");
89   form_on_page.signon_realm = "https://foo.com/";
90   form_on_page.ssl_valid = true;
91   form_on_page.preferred = false;
92   form_on_page.scheme = PasswordForm::SCHEME_HTML;
93
94   // Create a match from the database that matches using public suffix.
95   PasswordForm preferred_match;
96   preferred_match.origin = GURL("https://mobile.foo.com/");
97   preferred_match.action = GURL("https://mobile.foo.com/login");
98   preferred_match.username_element = ASCIIToUTF16("username");
99   preferred_match.username_value = ASCIIToUTF16("test@gmail.com");
100   preferred_match.password_element = ASCIIToUTF16("password");
101   preferred_match.password_value = ASCIIToUTF16("test");
102   preferred_match.submit_element = ASCIIToUTF16("");
103   preferred_match.signon_realm = "https://mobile.foo.com/";
104   preferred_match.original_signon_realm = "https://foo.com/";
105   preferred_match.ssl_valid = true;
106   preferred_match.preferred = true;
107   preferred_match.scheme = PasswordForm::SCHEME_HTML;
108
109   // Create a match that matches exactly, so |original_signon_realm| is not set.
110   PasswordForm exact_match;
111   exact_match.origin = GURL("https://foo.com/");
112   exact_match.action = GURL("https://foo.com/login");
113   exact_match.username_element = ASCIIToUTF16("username");
114   exact_match.username_value = ASCIIToUTF16("test1@gmail.com");
115   exact_match.password_element = ASCIIToUTF16("password");
116   exact_match.password_value = ASCIIToUTF16("test");
117   exact_match.submit_element = ASCIIToUTF16("");
118   exact_match.signon_realm = "https://foo.com/";
119   exact_match.ssl_valid = true;
120   exact_match.preferred = false;
121   exact_match.scheme = PasswordForm::SCHEME_HTML;
122
123   // Create a match that was matched using public suffix, so
124   // |original_signon_realm| is set to where the result came from.
125   PasswordForm public_suffix_match;
126   public_suffix_match.origin = GURL("https://foo.com/");
127   public_suffix_match.action = GURL("https://foo.com/login");
128   public_suffix_match.username_element = ASCIIToUTF16("username");
129   public_suffix_match.username_value = ASCIIToUTF16("test2@gmail.com");
130   public_suffix_match.password_element = ASCIIToUTF16("password");
131   public_suffix_match.password_value = ASCIIToUTF16("test");
132   public_suffix_match.submit_element = ASCIIToUTF16("");
133   public_suffix_match.original_signon_realm = "https://subdomain.foo.com/";
134   public_suffix_match.signon_realm = "https://foo.com/";
135   public_suffix_match.ssl_valid = true;
136   public_suffix_match.preferred = false;
137   public_suffix_match.scheme = PasswordForm::SCHEME_HTML;
138
139   // Add one exact match and one public suffix match.
140   PasswordFormMap matches;
141   matches[exact_match.username_value] = &exact_match;
142   matches[public_suffix_match.username_value] = &public_suffix_match;
143
144   PasswordFormFillData result;
145   InitPasswordFormFillData(form_on_page,
146                            matches,
147                            &preferred_match,
148                            true,
149                            false,
150                            &result);
151   EXPECT_TRUE(result.wait_for_username);
152   // The preferred realm should match the original signon realm from the
153   // preferred match so the user can see where the result came from.
154   EXPECT_EQ(result.preferred_realm,
155             preferred_match.original_signon_realm);
156
157   // The realm of the exact match should be empty.
158   PasswordFormFillData::LoginCollection::const_iterator iter =
159       result.additional_logins.find(exact_match.username_value);
160   EXPECT_EQ(iter->second.realm, "");
161
162   // The realm of the public suffix match should be set to the original signon
163   // realm so the user can see where the result came from.
164   iter = result.additional_logins.find(public_suffix_match.username_value);
165   EXPECT_EQ(iter->second.realm, public_suffix_match.original_signon_realm);
166 }
167
168 }  // namespace autofill