| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/autofill/content/browser/content_autofill_driver.h" | 5 #include "components/autofill/content/browser/content_autofill_driver.h" |
| 6 #include "components/autofill/content/browser/request_autocomplete_manager.h" | 6 #include "components/autofill/content/browser/request_autocomplete_manager.h" |
| 7 #include "components/autofill/content/common/autofill_messages.h" | 7 #include "components/autofill/content/common/autofill_messages.h" |
| 8 #include "components/autofill/core/browser/test_autofill_manager_delegate.h" | 8 #include "components/autofill/core/browser/test_autofill_client.h" |
| 9 #include "content/public/test/mock_render_process_host.h" | 9 #include "content/public/test/mock_render_process_host.h" |
| 10 #include "content/public/test/test_renderer_host.h" | 10 #include "content/public/test/test_renderer_host.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace autofill { | 13 namespace autofill { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const char kAppLocale[] = "en-US"; | 17 const char kAppLocale[] = "en-US"; |
| 18 const AutofillManager::AutofillDownloadManagerState kDownloadState = | 18 const AutofillManager::AutofillDownloadManagerState kDownloadState = |
| 19 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER; | 19 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER; |
| 20 | 20 |
| 21 class TestAutofillManager : public AutofillManager { | 21 class TestAutofillManager : public AutofillManager { |
| 22 public: | 22 public: |
| 23 TestAutofillManager(AutofillDriver* driver, | 23 TestAutofillManager(AutofillDriver* driver, AutofillClient* client) |
| 24 AutofillManagerDelegate* delegate) | 24 : AutofillManager(driver, client, kAppLocale, kDownloadState), |
| 25 : AutofillManager(driver, delegate, kAppLocale, kDownloadState), | 25 autofill_enabled_(true) {} |
| 26 autofill_enabled_(true) { | |
| 27 } | |
| 28 virtual ~TestAutofillManager() {} | 26 virtual ~TestAutofillManager() {} |
| 29 | 27 |
| 30 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } | 28 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } |
| 31 | 29 |
| 32 void set_autofill_enabled(bool autofill_enabled) { | 30 void set_autofill_enabled(bool autofill_enabled) { |
| 33 autofill_enabled_ = autofill_enabled; | 31 autofill_enabled_ = autofill_enabled; |
| 34 } | 32 } |
| 35 | 33 |
| 36 private: | 34 private: |
| 37 bool autofill_enabled_; | 35 bool autofill_enabled_; |
| 38 | 36 |
| 39 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); | 37 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); |
| 40 }; | 38 }; |
| 41 | 39 |
| 42 class CustomTestAutofillManagerDelegate : public TestAutofillManagerDelegate { | 40 class CustomTestAutofillClient : public TestAutofillClient { |
| 43 public: | 41 public: |
| 44 CustomTestAutofillManagerDelegate() : should_simulate_success_(true) {} | 42 CustomTestAutofillClient() : should_simulate_success_(true) {} |
| 45 | 43 |
| 46 virtual ~CustomTestAutofillManagerDelegate() {} | 44 virtual ~CustomTestAutofillClient() {} |
| 47 | 45 |
| 48 virtual void ShowRequestAutocompleteDialog( | 46 virtual void ShowRequestAutocompleteDialog( |
| 49 const FormData& form, | 47 const FormData& form, |
| 50 const GURL& source_url, | 48 const GURL& source_url, |
| 51 const ResultCallback& callback) OVERRIDE { | 49 const ResultCallback& callback) OVERRIDE { |
| 52 if (should_simulate_success_) { | 50 if (should_simulate_success_) { |
| 53 FormStructure form_structure(form); | 51 FormStructure form_structure(form); |
| 54 callback.Run(AutocompleteResultSuccess, | 52 callback.Run( |
| 55 base::string16(), | 53 AutocompleteResultSuccess, base::string16(), &form_structure); |
| 56 &form_structure); | 54 } else { |
| 57 } else { | 55 callback.Run(AutofillClient::AutocompleteResultErrorDisabled, |
| 58 callback.Run(AutofillManagerDelegate::AutocompleteResultErrorDisabled, | 56 base::string16(), |
| 59 base::string16(), | 57 NULL); |
| 60 NULL); | |
| 61 } | |
| 62 } | 58 } |
| 59 } |
| 63 | 60 |
| 64 void set_should_simulate_success(bool should_simulate_success) { | 61 void set_should_simulate_success(bool should_simulate_success) { |
| 65 should_simulate_success_ = should_simulate_success; | 62 should_simulate_success_ = should_simulate_success; |
| 66 } | 63 } |
| 67 | 64 |
| 68 private: | 65 private: |
| 69 // Enable testing the path where a callback is called without a | 66 // Enable testing the path where a callback is called without a |
| 70 // valid FormStructure. | 67 // valid FormStructure. |
| 71 bool should_simulate_success_; | 68 bool should_simulate_success_; |
| 72 | 69 |
| 73 DISALLOW_COPY_AND_ASSIGN(CustomTestAutofillManagerDelegate); | 70 DISALLOW_COPY_AND_ASSIGN(CustomTestAutofillClient); |
| 74 }; | 71 }; |
| 75 | 72 |
| 76 class TestContentAutofillDriver : public ContentAutofillDriver { | 73 class TestContentAutofillDriver : public ContentAutofillDriver { |
| 77 public: | 74 public: |
| 78 TestContentAutofillDriver(content::WebContents* contents, | 75 TestContentAutofillDriver(content::WebContents* contents, |
| 79 AutofillManagerDelegate* delegate) | 76 AutofillClient* client) |
| 80 : ContentAutofillDriver(contents, delegate, kAppLocale, kDownloadState) { | 77 : ContentAutofillDriver(contents, client, kAppLocale, kDownloadState) { |
| 81 SetAutofillManager(make_scoped_ptr<AutofillManager>( | 78 SetAutofillManager(make_scoped_ptr<AutofillManager>( |
| 82 new TestAutofillManager(this, delegate))); | 79 new TestAutofillManager(this, client))); |
| 83 } | 80 } |
| 84 virtual ~TestContentAutofillDriver() {} | 81 virtual ~TestContentAutofillDriver() {} |
| 85 | 82 |
| 86 TestAutofillManager* mock_autofill_manager() { | 83 TestAutofillManager* mock_autofill_manager() { |
| 87 return static_cast<TestAutofillManager*>(autofill_manager()); | 84 return static_cast<TestAutofillManager*>(autofill_manager()); |
| 88 } | 85 } |
| 89 | 86 |
| 90 using ContentAutofillDriver::DidNavigateMainFrame; | 87 using ContentAutofillDriver::DidNavigateMainFrame; |
| 91 | 88 |
| 92 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver); | 89 DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver); |
| 93 }; | 90 }; |
| 94 | 91 |
| 95 } // namespace | 92 } // namespace |
| 96 | 93 |
| 97 class RequestAutocompleteManagerTest : | 94 class RequestAutocompleteManagerTest : |
| 98 public content::RenderViewHostTestHarness { | 95 public content::RenderViewHostTestHarness { |
| 99 public: | 96 public: |
| 100 RequestAutocompleteManagerTest() {} | 97 RequestAutocompleteManagerTest() {} |
| 101 | 98 |
| 102 virtual void SetUp() OVERRIDE { | 99 virtual void SetUp() OVERRIDE { |
| 103 content::RenderViewHostTestHarness::SetUp(); | 100 content::RenderViewHostTestHarness::SetUp(); |
| 104 | 101 |
| 105 driver_.reset( | 102 driver_.reset( |
| 106 new TestContentAutofillDriver(web_contents(), &manager_delegate_)); | 103 new TestContentAutofillDriver(web_contents(), &autofill_client_)); |
| 107 request_autocomplete_manager_.reset( | 104 request_autocomplete_manager_.reset( |
| 108 new RequestAutocompleteManager(driver_.get())); | 105 new RequestAutocompleteManager(driver_.get())); |
| 109 } | 106 } |
| 110 | 107 |
| 111 virtual void TearDown() OVERRIDE { | 108 virtual void TearDown() OVERRIDE { |
| 112 // Reset the driver now to cause all pref observers to be removed and avoid | 109 // Reset the driver now to cause all pref observers to be removed and avoid |
| 113 // crashes that otherwise occur in the destructor. | 110 // crashes that otherwise occur in the destructor. |
| 114 driver_.reset(); | 111 driver_.reset(); |
| 115 content::RenderViewHostTestHarness::TearDown(); | 112 content::RenderViewHostTestHarness::TearDown(); |
| 116 } | 113 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 129 return false; | 126 return false; |
| 130 Tuple3<blink::WebFormElement::AutocompleteResult, base::string16, FormData> | 127 Tuple3<blink::WebFormElement::AutocompleteResult, base::string16, FormData> |
| 131 autofill_param; | 128 autofill_param; |
| 132 AutofillMsg_RequestAutocompleteResult::Read(message, &autofill_param); | 129 AutofillMsg_RequestAutocompleteResult::Read(message, &autofill_param); |
| 133 *result = autofill_param.a; | 130 *result = autofill_param.a; |
| 134 process()->sink().ClearMessages(); | 131 process()->sink().ClearMessages(); |
| 135 return true; | 132 return true; |
| 136 } | 133 } |
| 137 | 134 |
| 138 protected: | 135 protected: |
| 139 CustomTestAutofillManagerDelegate manager_delegate_; | 136 CustomTestAutofillClient autofill_client_; |
| 140 scoped_ptr<TestContentAutofillDriver> driver_; | 137 scoped_ptr<TestContentAutofillDriver> driver_; |
| 141 scoped_ptr<RequestAutocompleteManager> request_autocomplete_manager_; | 138 scoped_ptr<RequestAutocompleteManager> request_autocomplete_manager_; |
| 142 | 139 |
| 143 DISALLOW_COPY_AND_ASSIGN(RequestAutocompleteManagerTest); | 140 DISALLOW_COPY_AND_ASSIGN(RequestAutocompleteManagerTest); |
| 144 }; | 141 }; |
| 145 | 142 |
| 146 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteSuccess) { | 143 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteSuccess) { |
| 147 blink::WebFormElement::AutocompleteResult result; | 144 blink::WebFormElement::AutocompleteResult result; |
| 148 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL()); | 145 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL()); |
| 149 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); | 146 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); |
| 150 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result); | 147 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result); |
| 151 } | 148 } |
| 152 | 149 |
| 153 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteCancel) { | 150 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteCancel) { |
| 154 blink::WebFormElement::AutocompleteResult result; | 151 blink::WebFormElement::AutocompleteResult result; |
| 155 manager_delegate_.set_should_simulate_success(false); | 152 autofill_client_.set_should_simulate_success(false); |
| 156 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL()); | 153 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL()); |
| 157 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); | 154 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); |
| 158 EXPECT_EQ(blink::WebFormElement::AutocompleteResultErrorDisabled, result); | 155 EXPECT_EQ(blink::WebFormElement::AutocompleteResultErrorDisabled, result); |
| 159 } | 156 } |
| 160 | 157 |
| 161 // Disabling autofill doesn't disable the dialog (it just disables the use of | 158 // Disabling autofill doesn't disable the dialog (it just disables the use of |
| 162 // autofill in the dialog). | 159 // autofill in the dialog). |
| 163 TEST_F(RequestAutocompleteManagerTest, | 160 TEST_F(RequestAutocompleteManagerTest, |
| 164 OnRequestAutocompleteWithAutofillDisabled) { | 161 OnRequestAutocompleteWithAutofillDisabled) { |
| 165 blink::WebFormElement::AutocompleteResult result; | 162 blink::WebFormElement::AutocompleteResult result; |
| 166 driver_->mock_autofill_manager()->set_autofill_enabled(false); | 163 driver_->mock_autofill_manager()->set_autofill_enabled(false); |
| 167 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL()); | 164 request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL()); |
| 168 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); | 165 EXPECT_TRUE(GetAutocompleteResultMessage(&result)); |
| 169 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result); | 166 EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result); |
| 170 } | 167 } |
| 171 | 168 |
| 172 } // namespace autofill | 169 } // namespace autofill |
| OLD | NEW |