Move Tuple to base namespace.
[chromium-blink-merge.git] / components / autofill / content / browser / request_autocomplete_manager_unittest.cc
blob79b66995543c4f8b9e505fa0bb37cbd06044f8a1
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"
14 namespace autofill {
16 namespace {
18 const char kAppLocale[] = "en-US";
19 const AutofillManager::AutofillDownloadManagerState kDownloadState =
20 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER;
22 class TestAutofillManager : public AutofillManager {
23 public:
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;
35 private:
36 bool autofill_enabled_;
38 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
41 class CustomTestAutofillClient : public TestAutofillClient {
42 public:
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);
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::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);
90 } // namespace
92 class RequestAutocompleteManagerTest :
93 public content::RenderViewHostTestHarness {
94 public:
95 RequestAutocompleteManagerTest() {}
97 void SetUp() override {
98 content::RenderViewHostTestHarness::SetUp();
100 driver_.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(),
101 &autofill_client_));
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.
109 driver_.reset();
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);
123 if (!message)
124 return false;
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();
130 return true;
133 protected:
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