Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / autofill / autofill_server_browsertest.cc
blob163dd2d33ce70fb8fba2f2b5eaaf272a009125f3
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/command_line.h"
6 #include "base/macros.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/autofill/personal_data_manager_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/autofill/core/browser/autofill_profile.h"
19 #include "components/autofill/core/browser/autofill_test_utils.h"
20 #include "components/autofill/core/browser/personal_data_manager.h"
21 #include "components/autofill/core/browser/personal_data_manager_observer.h"
22 #include "components/autofill/core/common/autofill_pref_names.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/test_utils.h"
25 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 namespace autofill {
29 namespace {
31 // TODO(bondd): PdmChangeWaiter in autofill_uitest_util.cc is a replacement for
32 // this class. Remove this class and use helper functions in that file instead.
33 class WindowedPersonalDataManagerObserver : public PersonalDataManagerObserver {
34 public:
35 explicit WindowedPersonalDataManagerObserver(Profile* profile)
36 : profile_(profile),
37 message_loop_runner_(new content::MessageLoopRunner){
38 PersonalDataManagerFactory::GetForProfile(profile_)->AddObserver(this);
40 ~WindowedPersonalDataManagerObserver() override {}
42 // Waits for the PersonalDataManager's list of profiles to be updated.
43 void Wait() {
44 message_loop_runner_->Run();
45 PersonalDataManagerFactory::GetForProfile(profile_)->RemoveObserver(this);
48 // PersonalDataManagerObserver:
49 void OnPersonalDataChanged() override { message_loop_runner_->Quit(); }
51 private:
52 Profile* profile_;
53 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
56 class WindowedNetworkObserver : public net::TestURLFetcher::DelegateForTests {
57 public:
58 explicit WindowedNetworkObserver(const std::string& expected_upload_data)
59 : factory_(new net::TestURLFetcherFactory),
60 expected_upload_data_(expected_upload_data),
61 message_loop_runner_(new content::MessageLoopRunner) {
62 factory_->SetDelegateForTests(this);
64 ~WindowedNetworkObserver() {}
66 // Waits for a network request with the |expected_upload_data_|.
67 void Wait() {
68 message_loop_runner_->Run();
69 factory_.reset();
72 // net::TestURLFetcher::DelegateForTests:
73 void OnRequestStart(int fetcher_id) override {
74 net::TestURLFetcher* fetcher = factory_->GetFetcherByID(fetcher_id);
75 if (fetcher->upload_data() == expected_upload_data_)
76 message_loop_runner_->Quit();
78 // Not interested in any further status updates from this fetcher.
79 fetcher->SetDelegateForTests(NULL);
81 void OnChunkUpload(int fetcher_id) override {}
82 void OnRequestEnd(int fetcher_id) override {}
84 private:
85 // Mocks out network requests.
86 scoped_ptr<net::TestURLFetcherFactory> factory_;
88 const std::string expected_upload_data_;
89 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
91 DISALLOW_COPY_AND_ASSIGN(WindowedNetworkObserver);
94 } // namespace
96 class AutofillServerTest : public InProcessBrowserTest {
97 public:
98 void SetUpCommandLine(base::CommandLine* command_line) override {
99 // Enable finch experiment for sending field metadata.
100 command_line->AppendSwitchASCII(
101 ::switches::kForceFieldTrials, "AutofillFieldMetadata/Enabled/");
104 void SetUpOnMainThread() override {
105 // Disable interactions with the Mac Keychain.
106 PrefService* pref_service = browser()->profile()->GetPrefs();
107 test::DisableSystemServices(pref_service);
109 // Enable uploads, and load a new tab to force the AutofillDownloadManager
110 // to update its cached view of the prefs.
111 pref_service->SetDouble(prefs::kAutofillPositiveUploadRate, 1.0);
112 pref_service->SetDouble(prefs::kAutofillNegativeUploadRate, 1.0);
113 AddBlankTabAndShow(browser());
117 // Regression test for http://crbug.com/177419
118 IN_PROC_BROWSER_TEST_F(AutofillServerTest,
119 QueryAndUploadBothIncludeFieldsWithAutocompleteOff) {
120 // Seed some test Autofill profile data, as upload requests are only made when
121 // there is local data available to use as a baseline.
122 WindowedPersonalDataManagerObserver personal_data_observer(
123 browser()->profile());
124 PersonalDataManagerFactory::GetForProfile(browser()->profile())
125 ->AddProfile(test::GetFullProfile());
126 personal_data_observer.Wait();
128 // Load the test page. Expect a query request upon loading the page.
129 const char kDataURIPrefix[] = "data:text/html;charset=utf-8,";
130 const char kFormHtml[] =
131 "<form id='test_form'>"
132 " <input id='one'>"
133 " <input id='two' autocomplete='off'>"
134 " <input id='three'>"
135 " <input id='four' autocomplete='off'>"
136 " <input type='submit'>"
137 "</form>"
138 "<script>"
139 " document.onclick = function() {"
140 " document.getElementById('test_form').submit();"
141 " };"
142 "</script>";
143 const char kQueryRequest[] =
144 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
145 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
146 "<form signature=\"15916856893790176210\">"
147 "<field signature=\"2594484045\" name=\"one\" type=\"text\"/>"
148 "<field signature=\"2750915947\" name=\"two\" type=\"text\"/>"
149 "<field signature=\"3494787134\" name=\"three\" type=\"text\"/>"
150 "<field signature=\"1236501728\" name=\"four\" type=\"text\"/></form>"
151 "</autofillquery>";
152 WindowedNetworkObserver query_network_observer(kQueryRequest);
153 ui_test_utils::NavigateToURL(
154 browser(), GURL(std::string(kDataURIPrefix) + kFormHtml));
155 query_network_observer.Wait();
157 // Submit the form, using a simulated mouse click because form submissions not
158 // triggered by user gestures are ignored. Expect an upload request upon form
159 // submission, with form fields matching those from the query request.
160 const char kUploadRequest[] =
161 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
162 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
163 " formsignature=\"15916856893790176210\""
164 " autofillused=\"false\""
165 " datapresent=\"1f7e0003780000080004\">"
166 "<field signature=\"2594484045\" name=\"one\" type=\"text\""
167 " autofilltype=\"2\"/>"
168 "<field signature=\"2750915947\" name=\"two\" type=\"text\""
169 " autocomplete=\"off\" autofilltype=\"2\"/>"
170 "<field signature=\"3494787134\" name=\"three\" type=\"text\""
171 " autofilltype=\"2\"/>"
172 "<field signature=\"1236501728\" name=\"four\" type=\"text\""
173 " autocomplete=\"off\" autofilltype=\"2\"/>"
174 "</autofillupload>";
176 WindowedNetworkObserver upload_network_observer(kUploadRequest);
177 content::WebContents* web_contents =
178 browser()->tab_strip_model()->GetActiveWebContents();
179 content::SimulateMouseClick(
180 web_contents, 0, blink::WebMouseEvent::ButtonLeft);
181 upload_network_observer.Wait();
184 // Verify that a site with password fields will query even in the presence
185 // of user defined autocomplete types.
186 IN_PROC_BROWSER_TEST_F(AutofillServerTest,
187 AlwaysQueryForPasswordFields) {
188 // Load the test page. Expect a query request upon loading the page.
189 const char kDataURIPrefix[] = "data:text/html;charset=utf-8,";
190 const char kFormHtml[] =
191 "<form id='test_form'>"
192 " <input type='text' id='one' autocomplete='username'>"
193 " <input type='text' id='two' autocomplete='off'>"
194 " <input type='password' id='three'>"
195 " <input type='submit'>"
196 "</form>";
197 const char kQueryRequest[] =
198 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
199 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
200 "<form signature=\"8900697631820480876\">"
201 "<field signature=\"2594484045\" name=\"one\" type=\"text\"/>"
202 "<field signature=\"2750915947\" name=\"two\" type=\"text\"/>"
203 "<field signature=\"116843943\" name=\"three\" type=\"password\"/>"
204 "</form></autofillquery>";
205 WindowedNetworkObserver query_network_observer(kQueryRequest);
206 ui_test_utils::NavigateToURL(
207 browser(), GURL(std::string(kDataURIPrefix) + kFormHtml));
208 query_network_observer.Wait();
211 } // namespace autofill