Add diagnostics_writer.cc to the list of files allowed to printf.
[chromium-blink-merge.git] / components / autofill / content / browser / request_autocomplete_manager_unittest.cc
blobd9a9fb3d1fb858bf0c689c65104f6370d11b7fb4
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/test/mock_render_process_host.h"
10 #include "content/public/test/test_renderer_host.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace autofill {
15 namespace {
17 const char kAppLocale[] = "en-US";
18 const AutofillManager::AutofillDownloadManagerState kDownloadState =
19 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER;
21 class TestAutofillManager : public AutofillManager {
22 public:
23 TestAutofillManager(AutofillDriver* driver, AutofillClient* client)
24 : AutofillManager(driver, client, kAppLocale, kDownloadState),
25 autofill_enabled_(true) {}
26 virtual ~TestAutofillManager() {}
28 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
30 void set_autofill_enabled(bool autofill_enabled) {
31 autofill_enabled_ = autofill_enabled;
34 private:
35 bool autofill_enabled_;
37 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
40 class CustomTestAutofillClient : public TestAutofillClient {
41 public:
42 CustomTestAutofillClient() : should_simulate_success_(true) {}
44 virtual ~CustomTestAutofillClient() {}
46 virtual void ShowRequestAutocompleteDialog(
47 const FormData& form,
48 const GURL& source_url,
49 const ResultCallback& callback) OVERRIDE {
50 if (should_simulate_success_) {
51 FormStructure form_structure(form);
52 callback.Run(
53 AutocompleteResultSuccess, base::string16(), &form_structure);
54 } else {
55 callback.Run(AutofillClient::AutocompleteResultErrorDisabled,
56 base::string16(),
57 NULL);
61 void set_should_simulate_success(bool should_simulate_success) {
62 should_simulate_success_ = should_simulate_success;
65 private:
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 {
74 public:
75 TestContentAutofillDriver(content::WebContents* contents,
76 AutofillClient* client)
77 : ContentAutofillDriver(contents, client, kAppLocale, kDownloadState) {
78 SetAutofillManager(make_scoped_ptr<AutofillManager>(
79 new TestAutofillManager(this, client)));
81 virtual ~TestContentAutofillDriver() {}
83 TestAutofillManager* mock_autofill_manager() {
84 return static_cast<TestAutofillManager*>(autofill_manager());
87 using ContentAutofillDriver::DidNavigateMainFrame;
89 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver);
92 } // namespace
94 class RequestAutocompleteManagerTest :
95 public content::RenderViewHostTestHarness {
96 public:
97 RequestAutocompleteManagerTest() {}
99 virtual void SetUp() OVERRIDE {
100 content::RenderViewHostTestHarness::SetUp();
102 driver_.reset(
103 new TestContentAutofillDriver(web_contents(), &autofill_client_));
104 request_autocomplete_manager_.reset(
105 new RequestAutocompleteManager(driver_.get()));
108 virtual void TearDown() OVERRIDE {
109 // Reset the driver now to cause all pref observers to be removed and avoid
110 // crashes that otherwise occur in the destructor.
111 driver_.reset();
112 content::RenderViewHostTestHarness::TearDown();
115 // Searches for an |AutofillMsg_RequestAutocompleteResult| message in the
116 // queue of sent IPC messages. If none is present, returns false. Otherwise,
117 // extracts the first |AutofillMsg_RequestAutocompleteResult| message, fills
118 // the output parameter with the value of the message's parameter, and
119 // clears the queue of sent messages.
120 bool GetAutocompleteResultMessage(
121 blink::WebFormElement::AutocompleteResult* result) {
122 const uint32 kMsgID = AutofillMsg_RequestAutocompleteResult::ID;
123 const IPC::Message* message =
124 process()->sink().GetFirstMessageMatching(kMsgID);
125 if (!message)
126 return false;
127 Tuple3<blink::WebFormElement::AutocompleteResult, base::string16, FormData>
128 autofill_param;
129 AutofillMsg_RequestAutocompleteResult::Read(message, &autofill_param);
130 *result = autofill_param.a;
131 process()->sink().ClearMessages();
132 return true;
135 protected:
136 CustomTestAutofillClient autofill_client_;
137 scoped_ptr<TestContentAutofillDriver> driver_;
138 scoped_ptr<RequestAutocompleteManager> request_autocomplete_manager_;
140 DISALLOW_COPY_AND_ASSIGN(RequestAutocompleteManagerTest);
143 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteSuccess) {
144 blink::WebFormElement::AutocompleteResult result;
145 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL());
146 EXPECT_TRUE(GetAutocompleteResultMessage(&result));
147 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result);
150 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteCancel) {
151 blink::WebFormElement::AutocompleteResult result;
152 autofill_client_.set_should_simulate_success(false);
153 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL());
154 EXPECT_TRUE(GetAutocompleteResultMessage(&result));
155 EXPECT_EQ(blink::WebFormElement::AutocompleteResultErrorDisabled, result);
158 // Disabling autofill doesn't disable the dialog (it just disables the use of
159 // autofill in the dialog).
160 TEST_F(RequestAutocompleteManagerTest,
161 OnRequestAutocompleteWithAutofillDisabled) {
162 blink::WebFormElement::AutocompleteResult result;
163 driver_->mock_autofill_manager()->set_autofill_enabled(false);
164 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL());
165 EXPECT_TRUE(GetAutocompleteResultMessage(&result));
166 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result);
169 } // namespace autofill