• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
12 
13 namespace autofill {
14 
15 namespace {
16 
17 const char kAppLocale[] = "en-US";
18 const AutofillManager::AutofillDownloadManagerState kDownloadState =
19     AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER;
20 
21 class TestAutofillManager : public AutofillManager {
22  public:
TestAutofillManager(AutofillDriver * driver,AutofillClient * client)23   TestAutofillManager(AutofillDriver* driver, AutofillClient* client)
24       : AutofillManager(driver, client, kAppLocale, kDownloadState),
25         autofill_enabled_(true) {}
~TestAutofillManager()26   virtual ~TestAutofillManager() {}
27 
IsAutofillEnabled() const28   virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
29 
set_autofill_enabled(bool autofill_enabled)30   void set_autofill_enabled(bool autofill_enabled) {
31     autofill_enabled_ = autofill_enabled;
32   }
33 
34  private:
35   bool autofill_enabled_;
36 
37   DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
38 };
39 
40 class CustomTestAutofillClient : public TestAutofillClient {
41  public:
CustomTestAutofillClient()42   CustomTestAutofillClient() : should_simulate_success_(true) {}
43 
~CustomTestAutofillClient()44   virtual ~CustomTestAutofillClient() {}
45 
ShowRequestAutocompleteDialog(const FormData & form,const GURL & source_url,const ResultCallback & callback)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);
58     }
59   }
60 
set_should_simulate_success(bool should_simulate_success)61   void set_should_simulate_success(bool should_simulate_success) {
62     should_simulate_success_ = should_simulate_success;
63   }
64 
65  private:
66   // Enable testing the path where a callback is called without a
67   // valid FormStructure.
68   bool should_simulate_success_;
69 
70   DISALLOW_COPY_AND_ASSIGN(CustomTestAutofillClient);
71 };
72 
73 class TestContentAutofillDriver : public ContentAutofillDriver {
74  public:
TestContentAutofillDriver(content::WebContents * contents,AutofillClient * client)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)));
80   }
~TestContentAutofillDriver()81   virtual ~TestContentAutofillDriver() {}
82 
mock_autofill_manager()83   TestAutofillManager* mock_autofill_manager() {
84     return static_cast<TestAutofillManager*>(autofill_manager());
85   }
86 
87   using ContentAutofillDriver::DidNavigateMainFrame;
88 
89   DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver);
90 };
91 
92 }  // namespace
93 
94 class RequestAutocompleteManagerTest :
95     public content::RenderViewHostTestHarness {
96  public:
RequestAutocompleteManagerTest()97   RequestAutocompleteManagerTest() {}
98 
SetUp()99   virtual void SetUp() OVERRIDE {
100     content::RenderViewHostTestHarness::SetUp();
101 
102     driver_.reset(
103         new TestContentAutofillDriver(web_contents(), &autofill_client_));
104     request_autocomplete_manager_.reset(
105         new RequestAutocompleteManager(driver_.get()));
106   }
107 
TearDown()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();
113   }
114 
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.
GetAutocompleteResultMessage(blink::WebFormElement::AutocompleteResult * result)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;
133   }
134 
135  protected:
136   CustomTestAutofillClient autofill_client_;
137   scoped_ptr<TestContentAutofillDriver> driver_;
138   scoped_ptr<RequestAutocompleteManager> request_autocomplete_manager_;
139 
140   DISALLOW_COPY_AND_ASSIGN(RequestAutocompleteManagerTest);
141 };
142 
TEST_F(RequestAutocompleteManagerTest,OnRequestAutocompleteSuccess)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);
148 }
149 
TEST_F(RequestAutocompleteManagerTest,OnRequestAutocompleteCancel)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);
156 }
157 
158 // Disabling autofill doesn't disable the dialog (it just disables the use of
159 // autofill in the dialog).
TEST_F(RequestAutocompleteManagerTest,OnRequestAutocompleteWithAutofillDisabled)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);
167 }
168 
169 }  // namespace autofill
170