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 "components/autofill/content/browser/content_autofill_driver.h"
6 #include "components/autofill/content/browser/request_autocomplete_manager.h"
7 #include "components/autofill/content/common/autofill_messages.h"
8 #include "components/autofill/core/browser/test_autofill_client.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/test/mock_render_process_host.h"
11 #include "content/public/test/test_renderer_host.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 const char kAppLocale
[] = "en-US";
19 const AutofillManager::AutofillDownloadManagerState kDownloadState
=
20 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER
;
22 class TestAutofillManager
: public AutofillManager
{
24 TestAutofillManager(AutofillDriver
* driver
, AutofillClient
* client
)
25 : AutofillManager(driver
, client
, kAppLocale
, kDownloadState
),
26 autofill_enabled_(true) {}
27 ~TestAutofillManager() override
{}
29 bool IsAutofillEnabled() const override
{ return autofill_enabled_
; }
31 void set_autofill_enabled(bool autofill_enabled
) {
32 autofill_enabled_
= autofill_enabled
;
36 bool autofill_enabled_
;
38 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager
);
41 class CustomTestAutofillClient
: public TestAutofillClient
{
43 CustomTestAutofillClient() : should_simulate_success_(true) {}
45 ~CustomTestAutofillClient() override
{}
47 void ShowRequestAutocompleteDialog(const FormData
& form
,
48 content::RenderFrameHost
* rfh
,
49 const ResultCallback
& callback
) override
{
50 if (should_simulate_success_
) {
51 FormStructure
form_structure(form
);
53 AutocompleteResultSuccess
, base::string16(), &form_structure
);
55 callback
.Run(AutofillClient::AutocompleteResultErrorDisabled
,
61 void set_should_simulate_success(bool should_simulate_success
) {
62 should_simulate_success_
= should_simulate_success
;
66 // Enable testing the path where a callback is called without a
67 // valid FormStructure.
68 bool should_simulate_success_
;
70 DISALLOW_COPY_AND_ASSIGN(CustomTestAutofillClient
);
73 class TestContentAutofillDriver
: public ContentAutofillDriver
{
75 TestContentAutofillDriver(content::RenderFrameHost
* rfh
,
76 AutofillClient
* client
)
77 : ContentAutofillDriver(rfh
, client
, kAppLocale
, kDownloadState
) {
78 SetAutofillManager(make_scoped_ptr
<AutofillManager
>(
79 new TestAutofillManager(this, client
)));
81 ~TestContentAutofillDriver() override
{}
83 TestAutofillManager
* mock_autofill_manager() {
84 return static_cast<TestAutofillManager
*>(autofill_manager());
87 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver
);
92 class RequestAutocompleteManagerTest
:
93 public content::RenderViewHostTestHarness
{
95 RequestAutocompleteManagerTest() {}
97 void SetUp() override
{
98 content::RenderViewHostTestHarness::SetUp();
100 driver_
.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(),
102 request_autocomplete_manager_
.reset(
103 new RequestAutocompleteManager(driver_
.get()));
106 void TearDown() override
{
107 // Reset the driver now to cause all pref observers to be removed and avoid
108 // crashes that otherwise occur in the destructor.
110 content::RenderViewHostTestHarness::TearDown();
113 // Searches for an |AutofillMsg_RequestAutocompleteResult| message in the
114 // queue of sent IPC messages. If none is present, returns false. Otherwise,
115 // extracts the first |AutofillMsg_RequestAutocompleteResult| message, fills
116 // the output parameter with the value of the message's parameter, and
117 // clears the queue of sent messages.
118 bool GetAutocompleteResultMessage(
119 blink::WebFormElement::AutocompleteResult
* result
) {
120 const uint32 kMsgID
= AutofillMsg_RequestAutocompleteResult::ID
;
121 const IPC::Message
* message
=
122 process()->sink().GetFirstMessageMatching(kMsgID
);
125 base::Tuple
<blink::WebFormElement::AutocompleteResult
, base::string16
,
126 FormData
> autofill_param
;
127 AutofillMsg_RequestAutocompleteResult::Read(message
, &autofill_param
);
128 *result
= base::get
<0>(autofill_param
);
129 process()->sink().ClearMessages();
134 CustomTestAutofillClient autofill_client_
;
135 scoped_ptr
<TestContentAutofillDriver
> driver_
;
136 scoped_ptr
<RequestAutocompleteManager
> request_autocomplete_manager_
;
138 DISALLOW_COPY_AND_ASSIGN(RequestAutocompleteManagerTest
);
141 TEST_F(RequestAutocompleteManagerTest
, OnRequestAutocompleteSuccess
) {
142 blink::WebFormElement::AutocompleteResult result
;
143 request_autocomplete_manager_
->OnRequestAutocomplete(FormData());
144 EXPECT_TRUE(GetAutocompleteResultMessage(&result
));
145 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess
, result
);
148 TEST_F(RequestAutocompleteManagerTest
, OnRequestAutocompleteCancel
) {
149 blink::WebFormElement::AutocompleteResult result
;
150 autofill_client_
.set_should_simulate_success(false);
151 request_autocomplete_manager_
->OnRequestAutocomplete(FormData());
152 EXPECT_TRUE(GetAutocompleteResultMessage(&result
));
153 EXPECT_EQ(blink::WebFormElement::AutocompleteResultErrorDisabled
, result
);
156 // Disabling autofill doesn't disable the dialog (it just disables the use of
157 // autofill in the dialog).
158 TEST_F(RequestAutocompleteManagerTest
,
159 OnRequestAutocompleteWithAutofillDisabled
) {
160 blink::WebFormElement::AutocompleteResult result
;
161 driver_
->mock_autofill_manager()->set_autofill_enabled(false);
162 request_autocomplete_manager_
->OnRequestAutocomplete(FormData());
163 EXPECT_TRUE(GetAutocompleteResultMessage(&result
));
164 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess
, result
);
167 } // namespace autofill