| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/autofill/content/browser/request_autocomplete_manager.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <tuple> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "components/autofill/content/browser/content_autofill_driver.h" | |
| 14 #include "components/autofill/content/common/autofill_messages.h" | |
| 15 #include "components/autofill/core/browser/test_autofill_client.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/test/mock_render_process_host.h" | |
| 18 #include "content/public/test/test_renderer_host.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace autofill { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char kAppLocale[] = "en-US"; | |
| 26 const AutofillManager::AutofillDownloadManagerState kDownloadState = | |
| 27 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER; | |
| 28 | |
| 29 class TestAutofillManager : public AutofillManager { | |
| 30 public: | |
| 31 TestAutofillManager(AutofillDriver* driver, AutofillClient* client) | |
| 32 : AutofillManager(driver, client, kAppLocale, kDownloadState), | |
| 33 autofill_enabled_(true) {} | |
| 34 ~TestAutofillManager() override {} | |
| 35 | |
| 36 bool IsAutofillEnabled() const override { return autofill_enabled_; } | |
| 37 | |
| 38 void set_autofill_enabled(bool autofill_enabled) { | |
| 39 autofill_enabled_ = autofill_enabled; | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 bool autofill_enabled_; | |
| 44 | |
| 45 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); | |
| 46 }; | |
| 47 | |
| 48 class CustomTestAutofillClient : public TestAutofillClient { | |
| 49 public: | |
| 50 CustomTestAutofillClient() : should_simulate_success_(true) {} | |
| 51 | |
| 52 ~CustomTestAutofillClient() override {} | |
| 53 | |
| 54 void ShowRequestAutocompleteDialog(const FormData& form, | |
| 55 content::RenderFrameHost* rfh, | |
| 56 const ResultCallback& callback) override { | |
| 57 if (should_simulate_success_) { | |
| 58 FormStructure form_structure(form); | |
| 59 callback.Run( | |
| 60 AutocompleteResultSuccess, base::string16(), &form_structure); | |
| 61 } else { | |
| 62 callback.Run(AutofillClient::AutocompleteResultErrorDisabled, | |
| 63 base::string16(), | |
| 64 NULL); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 void set_should_simulate_success(bool should_simulate_success) { | |
| 69 should_simulate_success_ = should_simulate_success; | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 // Enable testing the path where a callback is called without a | |
| 74 // valid FormStructure. | |
| 75 bool should_simulate_success_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(CustomTestAutofillClient); | |
| 78 }; | |
| 79 | |
| 80 class TestContentAutofillDriver : public ContentAutofillDriver { | |
| 81 public: | |
| 82 TestContentAutofillDriver(content::RenderFrameHost* rfh, | |
| 83 AutofillClient* client) | |
| 84 : ContentAutofillDriver(rfh, client, kAppLocale, kDownloadState) { | |
| 85 SetAutofillManager(base::WrapUnique<AutofillManager>( | |
| 86 new TestAutofillManager(this, client))); | |
| 87 } | |
| 88 ~TestContentAutofillDriver() override {} | |
| 89 | |
| 90 TestAutofillManager* mock_autofill_manager() { | |
| 91 return static_cast<TestAutofillManager*>(autofill_manager()); | |
| 92 } | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver); | |
| 95 }; | |
| 96 | |
| 97 } // namespace | |
| 98 | |
| 99 class RequestAutocompleteManagerTest : | |
| 100 public content::RenderViewHostTestHarness { | |
| 101 public: | |
| 102 RequestAutocompleteManagerTest() {} | |
| 103 | |
| 104 void SetUp() override { | |
| 105 content::RenderViewHostTestHarness::SetUp(); | |
| 106 | |
| 107 driver_.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(), | |
| 108 &autofill_client_)); | |
| 109 request_autocomplete_manager_.reset( | |
| 110 new RequestAutocompleteManager(driver_.get())); | |
| 111 } | |
| 112 | |
| 113 void TearDown() override { | |
| 114 // Reset the driver now to cause all pref observers to be removed and avoid | |
| 115 // crashes that otherwise occur in the destructor. | |
| 116 driver_.reset(); | |
| 117 content::RenderViewHostTestHarness::TearDown(); | |
| 118 } | |
| 119 | |
| 120 // Searches for an |AutofillMsg_RequestAutocompleteResult| message in the | |
| 121 // queue of sent IPC messages. If none is present, returns false. Otherwise, | |
| 122 // extracts the first |AutofillMsg_RequestAutocompleteResult| message, fills | |
| 123 // the output parameter with the value of the message's parameter, and | |
| 124 // clears the queue of sent messages. | |
| 125 bool GetAutocompleteResultMessage( | |
| 126 blink::WebFormElement::AutocompleteResult* result) { | |
| 127 const uint32_t kMsgID = AutofillMsg_RequestAutocompleteResult::ID; | |
| 128 const IPC::Message* message = | |
| 129 process()->sink().GetFirstMessageMatching(kMsgID); | |
| 130 if (!message) | |
| 131 return false; | |
| 132 std::tuple<blink::WebFormElement::AutocompleteResult, base::string16, | |
| 133 FormData> autofill_param; | |
| 134 AutofillMsg_RequestAutocompleteResult::Read(message, &autofill_param); | |
| 135 *result = std::get<0>(autofill_param); | |
| 136 process()->sink().ClearMessages(); | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 protected: | |
| 141 CustomTestAutofillClient autofill_client_; | |
| 142 std::unique_ptr<TestContentAutofillDriver> driver_; | |
| 143 std::unique_ptr<RequestAutocompleteManager> request_autocomplete_manager_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(RequestAutocompleteManagerTest); | |
| 146 }; | |
| 147 | |
| 148 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteSuccess) { | |
| 149 blink::WebFormElement::AutocompleteResult result; | |
| 150 request_autocomplete_manager_->OnRequestAutocomplete(FormData()); | |
| 151 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); | |
| 152 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result); | |
| 153 } | |
| 154 | |
| 155 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteCancel) { | |
| 156 blink::WebFormElement::AutocompleteResult result; | |
| 157 autofill_client_.set_should_simulate_success(false); | |
| 158 request_autocomplete_manager_->OnRequestAutocomplete(FormData()); | |
| 159 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); | |
| 160 EXPECT_EQ(blink::WebFormElement::AutocompleteResultErrorDisabled, result); | |
| 161 } | |
| 162 | |
| 163 // Disabling autofill doesn't disable the dialog (it just disables the use of | |
| 164 // autofill in the dialog). | |
| 165 TEST_F(RequestAutocompleteManagerTest, | |
| 166 OnRequestAutocompleteWithAutofillDisabled) { | |
| 167 blink::WebFormElement::AutocompleteResult result; | |
| 168 driver_->mock_autofill_manager()->set_autofill_enabled(false); | |
| 169 request_autocomplete_manager_->OnRequestAutocomplete(FormData()); | |
| 170 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); | |
| 171 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result); | |
| 172 } | |
| 173 | |
| 174 } // namespace autofill | |
| OLD | NEW |