1 // Copyright 2014 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/macros.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/autofill/personal_data_manager_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/autofill/core/browser/autofill_profile.h"
17 #include "components/autofill/core/browser/autofill_test_utils.h"
18 #include "components/autofill/core/browser/personal_data_manager.h"
19 #include "components/autofill/core/browser/personal_data_manager_observer.h"
20 #include "components/autofill/core/common/autofill_pref_names.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "content/public/test/test_utils.h"
23 #include "net/url_request/test_url_fetcher_factory.h"
24 #include "testing/gtest/include/gtest/gtest.h"
29 // TODO(bondd): PdmChangeWaiter in autofill_uitest_util.cc is a replacement for
30 // this class. Remove this class and use helper functions in that file instead.
31 class WindowedPersonalDataManagerObserver
: public PersonalDataManagerObserver
{
33 explicit WindowedPersonalDataManagerObserver(Profile
* profile
)
35 message_loop_runner_(new content::MessageLoopRunner
){
36 PersonalDataManagerFactory::GetForProfile(profile_
)->AddObserver(this);
38 ~WindowedPersonalDataManagerObserver() override
{}
40 // Waits for the PersonalDataManager's list of profiles to be updated.
42 message_loop_runner_
->Run();
43 PersonalDataManagerFactory::GetForProfile(profile_
)->RemoveObserver(this);
46 // PersonalDataManagerObserver:
47 void OnPersonalDataChanged() override
{ message_loop_runner_
->Quit(); }
51 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
54 class WindowedNetworkObserver
: public net::TestURLFetcher::DelegateForTests
{
56 explicit WindowedNetworkObserver(const std::string
& expected_upload_data
)
57 : factory_(new net::TestURLFetcherFactory
),
58 expected_upload_data_(expected_upload_data
),
59 message_loop_runner_(new content::MessageLoopRunner
) {
60 factory_
->SetDelegateForTests(this);
62 ~WindowedNetworkObserver() {}
64 // Waits for a network request with the |expected_upload_data_|.
66 message_loop_runner_
->Run();
70 // net::TestURLFetcher::DelegateForTests:
71 void OnRequestStart(int fetcher_id
) override
{
72 net::TestURLFetcher
* fetcher
= factory_
->GetFetcherByID(fetcher_id
);
73 if (fetcher
->upload_data() == expected_upload_data_
)
74 message_loop_runner_
->Quit();
76 // Not interested in any further status updates from this fetcher.
77 fetcher
->SetDelegateForTests(NULL
);
79 void OnChunkUpload(int fetcher_id
) override
{}
80 void OnRequestEnd(int fetcher_id
) override
{}
83 // Mocks out network requests.
84 scoped_ptr
<net::TestURLFetcherFactory
> factory_
;
86 const std::string expected_upload_data_
;
87 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
89 DISALLOW_COPY_AND_ASSIGN(WindowedNetworkObserver
);
94 class AutofillServerTest
: public InProcessBrowserTest
{
96 void SetUpOnMainThread() override
{
97 // Disable interactions with the Mac Keychain.
98 PrefService
* pref_service
= browser()->profile()->GetPrefs();
99 test::DisableSystemServices(pref_service
);
101 // Enable uploads, and load a new tab to force the AutofillDownloadManager
102 // to update its cached view of the prefs.
103 pref_service
->SetDouble(prefs::kAutofillPositiveUploadRate
, 1.0);
104 pref_service
->SetDouble(prefs::kAutofillNegativeUploadRate
, 1.0);
105 AddBlankTabAndShow(browser());
109 // Regression test for http://crbug.com/177419
110 IN_PROC_BROWSER_TEST_F(AutofillServerTest
,
111 QueryAndUploadBothIncludeFieldsWithAutocompleteOff
) {
112 // Seed some test Autofill profile data, as upload requests are only made when
113 // there is local data available to use as a baseline.
114 WindowedPersonalDataManagerObserver
personal_data_observer(
115 browser()->profile());
116 PersonalDataManagerFactory::GetForProfile(browser()->profile())
117 ->AddProfile(test::GetFullProfile());
118 personal_data_observer
.Wait();
120 // Load the test page. Expect a query request upon loading the page.
121 const char kDataURIPrefix
[] = "data:text/html;charset=utf-8,";
122 const char kFormHtml
[] =
123 "<form id='test_form'>"
125 " <input id='two' autocomplete='off'>"
126 " <input id='three'>"
127 " <input id='four' autocomplete='off'>"
128 " <input type='submit'>"
131 " document.onclick = function() {"
132 " document.getElementById('test_form').submit();"
135 const char kQueryRequest
[] =
136 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
137 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
138 "<form signature=\"15916856893790176210\">"
139 "<field signature=\"2594484045\"/>"
140 "<field signature=\"2750915947\"/>"
141 "<field signature=\"3494787134\"/>"
142 "<field signature=\"1236501728\"/>"
145 WindowedNetworkObserver
query_network_observer(kQueryRequest
);
146 ui_test_utils::NavigateToURL(
147 browser(), GURL(std::string(kDataURIPrefix
) + kFormHtml
));
148 query_network_observer
.Wait();
150 // Submit the form, using a simulated mouse click because form submissions not
151 // triggered by user gestures are ignored. Expect an upload request upon form
152 // submission, with form fields matching those from the query request.
153 const char kUploadRequest
[] =
154 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
155 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
156 " formsignature=\"15916856893790176210\""
157 " autofillused=\"false\""
158 " datapresent=\"1f7e0003780000080004\">"
159 "<field signature=\"2594484045\" autofilltype=\"2\"/>"
160 "<field signature=\"2750915947\" autofilltype=\"2\"/>"
161 "<field signature=\"3494787134\" autofilltype=\"2\"/>"
162 "<field signature=\"1236501728\" autofilltype=\"2\"/>"
164 WindowedNetworkObserver
upload_network_observer(kUploadRequest
);
165 content::WebContents
* web_contents
=
166 browser()->tab_strip_model()->GetActiveWebContents();
167 content::SimulateMouseClick(
168 web_contents
, 0, blink::WebMouseEvent::ButtonLeft
);
169 upload_network_observer
.Wait();
172 // Verify that a site with password fields will query even in the presence
173 // of user defined autocomplete types.
174 IN_PROC_BROWSER_TEST_F(AutofillServerTest
,
175 AlwaysQueryForPasswordFields
) {
176 // Load the test page. Expect a query request upon loading the page.
177 const char kDataURIPrefix
[] = "data:text/html;charset=utf-8,";
178 const char kFormHtml
[] =
179 "<form id='test_form'>"
180 " <input type='text' id='one' autocomplete='username'>"
181 " <input type='text' id='two' autocomplete='off'>"
182 " <input type='password' id='three'>"
183 " <input type='submit'>"
185 const char kQueryRequest
[] =
186 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
187 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
188 "<form signature=\"8900697631820480876\">"
189 "<field signature=\"2594484045\"/>"
190 "<field signature=\"2750915947\"/>"
191 "<field signature=\"116843943\"/>"
194 WindowedNetworkObserver
query_network_observer(kQueryRequest
);
195 ui_test_utils::NavigateToURL(
196 browser(), GURL(std::string(kDataURIPrefix
) + kFormHtml
));
197 query_network_observer
.Wait();
200 } // namespace autofill