Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / autofill / content / browser / request_autocomplete_manager_unittest.cc
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_manager_delegate.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:
23   TestAutofillManager(AutofillDriver* driver,
24                       AutofillManagerDelegate* delegate)
25       : AutofillManager(driver, delegate, kAppLocale, kDownloadState),
26         autofill_enabled_(true) {
27   }
28   virtual ~TestAutofillManager() {}
29
30   virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
31
32   void set_autofill_enabled(bool autofill_enabled) {
33     autofill_enabled_ = autofill_enabled;
34   }
35
36  private:
37   bool autofill_enabled_;
38
39   DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
40 };
41
42 class CustomTestAutofillManagerDelegate : public TestAutofillManagerDelegate {
43   public:
44     CustomTestAutofillManagerDelegate() : should_simulate_success_(true) {}
45
46     virtual ~CustomTestAutofillManagerDelegate() {}
47
48     virtual void ShowRequestAutocompleteDialog(
49         const FormData& form,
50         const GURL& source_url,
51         const ResultCallback& callback) OVERRIDE {
52       if (should_simulate_success_) {
53         FormStructure form_structure(form);
54         callback.Run(AutocompleteResultSuccess,
55                      base::string16(),
56                      &form_structure);
57       } else {
58         callback.Run(AutofillManagerDelegate::AutocompleteResultErrorDisabled,
59                      base::string16(),
60                      NULL);
61       }
62     }
63
64     void set_should_simulate_success(bool should_simulate_success) {
65       should_simulate_success_ = should_simulate_success;
66     }
67
68   private:
69     // Enable testing the path where a callback is called without a
70     // valid FormStructure.
71     bool should_simulate_success_;
72
73     DISALLOW_COPY_AND_ASSIGN(CustomTestAutofillManagerDelegate);
74 };
75
76 class TestContentAutofillDriver : public ContentAutofillDriver {
77  public:
78   TestContentAutofillDriver(content::WebContents* contents,
79                             AutofillManagerDelegate* delegate)
80       : ContentAutofillDriver(contents, delegate, kAppLocale, kDownloadState) {
81     SetAutofillManager(make_scoped_ptr<AutofillManager>(
82         new TestAutofillManager(this, delegate)));
83   }
84   virtual ~TestContentAutofillDriver() {}
85
86   TestAutofillManager* mock_autofill_manager() {
87     return static_cast<TestAutofillManager*>(autofill_manager());
88   }
89
90   using ContentAutofillDriver::DidNavigateMainFrame;
91
92   DISALLOW_COPY_AND_ASSIGN(TestContentAutofillDriver);
93 };
94
95 }  // namespace
96
97 class RequestAutocompleteManagerTest :
98     public content::RenderViewHostTestHarness {
99  public:
100   RequestAutocompleteManagerTest() {}
101
102   virtual void SetUp() OVERRIDE {
103     content::RenderViewHostTestHarness::SetUp();
104
105     driver_.reset(
106         new TestContentAutofillDriver(web_contents(), &manager_delegate_));
107     request_autocomplete_manager_.reset(
108         new RequestAutocompleteManager(driver_.get()));
109   }
110
111   virtual void TearDown() OVERRIDE {
112     // Reset the driver now to cause all pref observers to be removed and avoid
113     // crashes that otherwise occur in the destructor.
114     driver_.reset();
115     content::RenderViewHostTestHarness::TearDown();
116   }
117
118   // Searches for an |AutofillMsg_RequestAutocompleteResult| message in the
119   // queue of sent IPC messages. If none is present, returns false. Otherwise,
120   // extracts the first |AutofillMsg_RequestAutocompleteResult| message, fills
121   // the output parameter with the value of the message's parameter, and
122   // clears the queue of sent messages.
123   bool GetAutocompleteResultMessage(
124       blink::WebFormElement::AutocompleteResult* result) {
125     const uint32 kMsgID = AutofillMsg_RequestAutocompleteResult::ID;
126     const IPC::Message* message =
127         process()->sink().GetFirstMessageMatching(kMsgID);
128     if (!message)
129       return false;
130     Tuple3<blink::WebFormElement::AutocompleteResult, base::string16, FormData>
131         autofill_param;
132     AutofillMsg_RequestAutocompleteResult::Read(message, &autofill_param);
133     *result = autofill_param.a;
134     process()->sink().ClearMessages();
135     return true;
136   }
137
138  protected:
139   CustomTestAutofillManagerDelegate manager_delegate_;
140   scoped_ptr<TestContentAutofillDriver> driver_;
141   scoped_ptr<RequestAutocompleteManager> request_autocomplete_manager_;
142
143   DISALLOW_COPY_AND_ASSIGN(RequestAutocompleteManagerTest);
144 };
145
146 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteSuccess) {
147   blink::WebFormElement::AutocompleteResult result;
148   request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL());
149   EXPECT_TRUE(GetAutocompleteResultMessage(&result));
150   EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result);
151 }
152
153 TEST_F(RequestAutocompleteManagerTest, OnRequestAutocompleteCancel) {
154   blink::WebFormElement::AutocompleteResult result;
155   manager_delegate_.set_should_simulate_success(false);
156   request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL());
157   EXPECT_TRUE(GetAutocompleteResultMessage(&result));
158   EXPECT_EQ(blink::WebFormElement::AutocompleteResultErrorDisabled, result);
159 }
160
161 // Disabling autofill doesn't disable the dialog (it just disables the use of
162 // autofill in the dialog).
163 TEST_F(RequestAutocompleteManagerTest,
164        OnRequestAutocompleteWithAutofillDisabled) {
165   blink::WebFormElement::AutocompleteResult result;
166   driver_->mock_autofill_manager()->set_autofill_enabled(false);
167   request_autocomplete_manager_->OnRequestAutocomplete(FormData(), GURL());
168   EXPECT_TRUE(GetAutocompleteResultMessage(&result));
169   EXPECT_EQ(blink::WebFormElement::AutocompleteResultSuccess, result);
170 }
171
172 }  // namespace autofill