OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
11 #include <tuple> | 11 #include <tuple> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/run_loop.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "components/autofill/content/common/autofill_messages.h" | |
18 #include "components/autofill/core/browser/autofill_external_delegate.h" | 18 #include "components/autofill/core/browser/autofill_external_delegate.h" |
19 #include "components/autofill/core/browser/autofill_manager.h" | 19 #include "components/autofill/core/browser/autofill_manager.h" |
20 #include "components/autofill/core/browser/autofill_test_utils.h" | 20 #include "components/autofill/core/browser/autofill_test_utils.h" |
21 #include "components/autofill/core/browser/test_autofill_client.h" | 21 #include "components/autofill/core/browser/test_autofill_client.h" |
22 #include "components/autofill/core/common/autofill_switches.h" | 22 #include "components/autofill/core/common/autofill_switches.h" |
23 #include "components/autofill/core/common/form_data_predictions.h" | 23 #include "components/autofill/core/common/form_data_predictions.h" |
24 #include "content/public/browser/browser_context.h" | 24 #include "content/public/browser/browser_context.h" |
25 #include "content/public/browser/navigation_details.h" | 25 #include "content/public/browser/navigation_details.h" |
26 #include "content/public/browser/storage_partition.h" | 26 #include "content/public/browser/storage_partition.h" |
27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
28 #include "content/public/common/frame_navigate_params.h" | 28 #include "content/public/common/frame_navigate_params.h" |
29 #include "content/public/test/mock_render_process_host.h" | 29 #include "content/public/test/mock_render_process_host.h" |
30 #include "content/public/test/test_renderer_host.h" | 30 #include "content/public/test/test_renderer_host.h" |
31 #include "ipc/ipc_test_sink.h" | 31 #include "mojo/common/common_type_converters.h" |
| 32 #include "mojo/public/cpp/bindings/binding_set.h" |
| 33 #include "services/shell/public/cpp/interface_provider.h" |
32 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
34 | 36 |
35 namespace autofill { | 37 namespace autofill { |
36 | 38 |
37 namespace { | 39 namespace { |
38 | 40 |
39 const char kAppLocale[] = "en-US"; | 41 const char kAppLocale[] = "en-US"; |
40 const AutofillManager::AutofillDownloadManagerState kDownloadState = | 42 const AutofillManager::AutofillDownloadManagerState kDownloadState = |
41 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER; | 43 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER; |
42 | 44 |
| 45 class FakeAutofillAgent : public mojom::AutofillAgent { |
| 46 public: |
| 47 FakeAutofillAgent() |
| 48 : fill_form_id_(-1), |
| 49 preview_form_id_(-1), |
| 50 called_clear_form_(false), |
| 51 called_clear_previewed_form_(false) {} |
| 52 |
| 53 ~FakeAutofillAgent() override {} |
| 54 |
| 55 void BindRequest(mojo::ScopedMessagePipeHandle handle) { |
| 56 bindings_.AddBinding( |
| 57 this, mojo::MakeRequest<mojom::AutofillAgent>(std::move(handle))); |
| 58 } |
| 59 |
| 60 void SetQuitLoopClosure(base::Closure closure) { quit_closure_ = closure; } |
| 61 |
| 62 // Returns the id and formdata received via |
| 63 // mojo interface method mojom::AutofillAgent::FillForm(). |
| 64 bool GetAutofillFillFormMessage(int* page_id, FormData* results) { |
| 65 if (fill_form_id_ == -1) |
| 66 return false; |
| 67 if (!fill_form_form_) |
| 68 return false; |
| 69 |
| 70 if (page_id) |
| 71 *page_id = fill_form_id_; |
| 72 if (results) |
| 73 *results = *fill_form_form_; |
| 74 return true; |
| 75 } |
| 76 |
| 77 // Returns the id and formdata received via |
| 78 // mojo interface method mojom::AutofillAgent::PreviewForm(). |
| 79 bool GetAutofillPreviewFormMessage(int* page_id, FormData* results) { |
| 80 if (preview_form_id_ == -1) |
| 81 return false; |
| 82 if (!preview_form_form_) |
| 83 return false; |
| 84 |
| 85 if (page_id) |
| 86 *page_id = preview_form_id_; |
| 87 if (results) |
| 88 *results = *preview_form_form_; |
| 89 return true; |
| 90 } |
| 91 |
| 92 // Returns data received via mojo interface method |
| 93 // mojom::AutofillAent::FieldTypePredictionsAvailable(). |
| 94 bool GetFieldTypePredictionsAvailable( |
| 95 std::vector<FormDataPredictions>* predictions) { |
| 96 if (!predictions_) |
| 97 return false; |
| 98 if (predictions) |
| 99 *predictions = *predictions_; |
| 100 return true; |
| 101 } |
| 102 |
| 103 // Returns whether mojo interface method mojom::AutofillAgent::ClearForm() got |
| 104 // called. |
| 105 bool GetCalledClearForm() { return called_clear_form_; } |
| 106 |
| 107 // Returns whether mojo interface method |
| 108 // mojom::AutofillAgent::ClearPreviewedForm() got called. |
| 109 bool GetCalledClearPreviewedForm() { return called_clear_previewed_form_; } |
| 110 |
| 111 // Returns data received via mojo interface method |
| 112 // mojom::AutofillAent::FillFieldWithValue(). |
| 113 bool GetString16FillFieldWithValue(base::string16* value) { |
| 114 if (!value_fill_field_) |
| 115 return false; |
| 116 if (value) |
| 117 *value = *value_fill_field_; |
| 118 return true; |
| 119 } |
| 120 |
| 121 // Returns data received via mojo interface method |
| 122 // mojom::AutofillAent::PreviewFieldWithValue(). |
| 123 bool GetString16PreviewFieldWithValue(base::string16* value) { |
| 124 if (!value_preview_field_) |
| 125 return false; |
| 126 if (value) |
| 127 *value = *value_preview_field_; |
| 128 return true; |
| 129 } |
| 130 |
| 131 // Returns data received via mojo interface method |
| 132 // mojom::AutofillAent::AcceptDataListSuggestion(). |
| 133 bool GetString16AcceptDataListSuggestion(base::string16* value) { |
| 134 if (!value_accept_data_) |
| 135 return false; |
| 136 if (value) |
| 137 *value = *value_accept_data_; |
| 138 return true; |
| 139 } |
| 140 |
| 141 private: |
| 142 void CallDone() { |
| 143 if (!quit_closure_.is_null()) { |
| 144 quit_closure_.Run(); |
| 145 quit_closure_.Reset(); |
| 146 } |
| 147 } |
| 148 |
| 149 // mojom::AutofillAgent methods: |
| 150 void FirstUserGestureObservedInTab() override {} |
| 151 |
| 152 void FillForm(int32_t id, const FormData& form) override { |
| 153 fill_form_id_ = id; |
| 154 fill_form_form_ = form; |
| 155 CallDone(); |
| 156 } |
| 157 |
| 158 void PreviewForm(int32_t id, const FormData& form) override { |
| 159 preview_form_id_ = id; |
| 160 preview_form_form_ = form; |
| 161 CallDone(); |
| 162 } |
| 163 |
| 164 void FieldTypePredictionsAvailable( |
| 165 mojo::Array<FormDataPredictions> forms) override { |
| 166 predictions_ = forms.PassStorage(); |
| 167 CallDone(); |
| 168 } |
| 169 |
| 170 void ClearForm() override { |
| 171 called_clear_form_ = true; |
| 172 CallDone(); |
| 173 } |
| 174 |
| 175 void ClearPreviewedForm() override { |
| 176 called_clear_previewed_form_ = true; |
| 177 CallDone(); |
| 178 } |
| 179 |
| 180 void FillFieldWithValue(const mojo::String& value) override { |
| 181 value_fill_field_ = value.To<base::string16>(); |
| 182 CallDone(); |
| 183 } |
| 184 |
| 185 void PreviewFieldWithValue(const mojo::String& value) override { |
| 186 value_preview_field_ = value.To<base::string16>(); |
| 187 CallDone(); |
| 188 } |
| 189 |
| 190 void AcceptDataListSuggestion(const mojo::String& value) override { |
| 191 value_accept_data_ = value.To<base::string16>(); |
| 192 CallDone(); |
| 193 } |
| 194 |
| 195 void FillPasswordSuggestion(const mojo::String& username, |
| 196 const mojo::String& password) override {} |
| 197 |
| 198 void PreviewPasswordSuggestion(const mojo::String& username, |
| 199 const mojo::String& password) override {} |
| 200 |
| 201 void ShowInitialPasswordAccountSuggestions( |
| 202 int32_t key, |
| 203 const PasswordFormFillData& form_data) override {} |
| 204 |
| 205 mojo::BindingSet<mojom::AutofillAgent> bindings_; |
| 206 |
| 207 base::Closure quit_closure_; |
| 208 |
| 209 // Records data received from FillForm() call. |
| 210 int32_t fill_form_id_; |
| 211 base::Optional<FormData> fill_form_form_; |
| 212 // Records data received from PreviewForm() call. |
| 213 int32_t preview_form_id_; |
| 214 base::Optional<FormData> preview_form_form_; |
| 215 // Records data received from FieldTypePredictionsAvailable() call. |
| 216 base::Optional<std::vector<FormDataPredictions>> predictions_; |
| 217 // Records whether ClearForm() got called. |
| 218 bool called_clear_form_; |
| 219 // Records whether ClearPreviewedForm() got called. |
| 220 bool called_clear_previewed_form_; |
| 221 // Records string received from FillFieldWithValue() call. |
| 222 base::Optional<base::string16> value_fill_field_; |
| 223 // Records string received from PreviewFieldWithValue() call. |
| 224 base::Optional<base::string16> value_preview_field_; |
| 225 // Records string received from AcceptDataListSuggestion() call. |
| 226 base::Optional<base::string16> value_accept_data_; |
| 227 }; |
| 228 |
43 } // namespace | 229 } // namespace |
44 | 230 |
45 class MockAutofillManager : public AutofillManager { | 231 class MockAutofillManager : public AutofillManager { |
46 public: | 232 public: |
47 MockAutofillManager(AutofillDriver* driver, AutofillClient* client) | 233 MockAutofillManager(AutofillDriver* driver, AutofillClient* client) |
48 : AutofillManager(driver, client, kAppLocale, kDownloadState) {} | 234 : AutofillManager(driver, client, kAppLocale, kDownloadState) {} |
49 virtual ~MockAutofillManager() {} | 235 virtual ~MockAutofillManager() {} |
50 | 236 |
51 MOCK_METHOD0(Reset, void()); | 237 MOCK_METHOD0(Reset, void()); |
52 }; | 238 }; |
(...skipping 17 matching lines...) Expand all Loading... |
70 }; | 256 }; |
71 | 257 |
72 class ContentAutofillDriverTest : public content::RenderViewHostTestHarness { | 258 class ContentAutofillDriverTest : public content::RenderViewHostTestHarness { |
73 public: | 259 public: |
74 void SetUp() override { | 260 void SetUp() override { |
75 content::RenderViewHostTestHarness::SetUp(); | 261 content::RenderViewHostTestHarness::SetUp(); |
76 | 262 |
77 test_autofill_client_.reset(new TestAutofillClient()); | 263 test_autofill_client_.reset(new TestAutofillClient()); |
78 driver_.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(), | 264 driver_.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(), |
79 test_autofill_client_.get())); | 265 test_autofill_client_.get())); |
| 266 |
| 267 shell::InterfaceProvider* remote_interfaces = |
| 268 web_contents()->GetMainFrame()->GetRemoteInterfaces(); |
| 269 shell::InterfaceProvider::TestApi test_api(remote_interfaces); |
| 270 test_api.SetBinderForName(mojom::AutofillAgent::Name_, |
| 271 base::Bind(&FakeAutofillAgent::BindRequest, |
| 272 base::Unretained(&fake_agent_))); |
80 } | 273 } |
81 | 274 |
82 void TearDown() override { | 275 void TearDown() override { |
83 // Reset the driver now to cause all pref observers to be removed and avoid | 276 // Reset the driver now to cause all pref observers to be removed and avoid |
84 // crashes that otherwise occur in the destructor. | 277 // crashes that otherwise occur in the destructor. |
85 driver_.reset(); | 278 driver_.reset(); |
86 content::RenderViewHostTestHarness::TearDown(); | 279 content::RenderViewHostTestHarness::TearDown(); |
87 } | 280 } |
88 | 281 |
89 protected: | 282 protected: |
90 // Searches for an |AutofillMsg_FillForm| message in the queue of sent IPC | |
91 // messages. If none is present, returns false. Otherwise, extracts the first | |
92 // |AutofillMsg_FillForm| message, fills the output parameters with the values | |
93 // of the message's parameters, and clears the queue of sent messages. | |
94 bool GetAutofillFillFormMessage(int* page_id, FormData* results) { | |
95 const uint32_t kMsgID = AutofillMsg_FillForm::ID; | |
96 const IPC::Message* message = | |
97 process()->sink().GetFirstMessageMatching(kMsgID); | |
98 if (!message) | |
99 return false; | |
100 std::tuple<int, FormData> autofill_param; | |
101 if (!AutofillMsg_FillForm::Read(message, &autofill_param)) | |
102 return false; | |
103 if (page_id) | |
104 *page_id = std::get<0>(autofill_param); | |
105 if (results) | |
106 *results = std::get<1>(autofill_param); | |
107 process()->sink().ClearMessages(); | |
108 return true; | |
109 } | |
110 | |
111 // Searches for an |AutofillMsg_PreviewForm| message in the queue of sent IPC | |
112 // messages. If none is present, returns false. Otherwise, extracts the first | |
113 // |AutofillMsg_PreviewForm| message, fills the output parameters with the | |
114 // values of the message's parameters, and clears the queue of sent messages. | |
115 bool GetAutofillPreviewFormMessage(int* page_id, FormData* results) { | |
116 const uint32_t kMsgID = AutofillMsg_PreviewForm::ID; | |
117 const IPC::Message* message = | |
118 process()->sink().GetFirstMessageMatching(kMsgID); | |
119 if (!message) | |
120 return false; | |
121 std::tuple<int, FormData> autofill_param; | |
122 if (!AutofillMsg_PreviewForm::Read(message, &autofill_param)) | |
123 return false; | |
124 if (page_id) | |
125 *page_id = std::get<0>(autofill_param); | |
126 if (results) | |
127 *results = std::get<1>(autofill_param); | |
128 process()->sink().ClearMessages(); | |
129 return true; | |
130 } | |
131 | |
132 // Searches for an |AutofillMsg_FieldTypePredictionsAvailable| message in the | |
133 // queue of sent IPC messages. If none is present, returns false. Otherwise, | |
134 // extracts the first |AutofillMsg_FieldTypePredictionsAvailable| message, | |
135 // fills the output parameter with the values of the message's parameter, and | |
136 // clears the queue of sent messages. | |
137 bool GetFieldTypePredictionsAvailable( | |
138 std::vector<FormDataPredictions>* predictions) { | |
139 const uint32_t kMsgID = AutofillMsg_FieldTypePredictionsAvailable::ID; | |
140 const IPC::Message* message = | |
141 process()->sink().GetFirstMessageMatching(kMsgID); | |
142 if (!message) | |
143 return false; | |
144 std::tuple<std::vector<FormDataPredictions> > autofill_param; | |
145 if (!AutofillMsg_FieldTypePredictionsAvailable::Read(message, | |
146 &autofill_param)) | |
147 return false; | |
148 if (predictions) | |
149 *predictions = std::get<0>(autofill_param); | |
150 | |
151 process()->sink().ClearMessages(); | |
152 return true; | |
153 } | |
154 | |
155 // Searches for a message matching |messageID| in the queue of sent IPC | |
156 // messages. If none is present, returns false. Otherwise, extracts the first | |
157 // matching message, fills the output parameter with the string16 from the | |
158 // message's parameter, and clears the queue of sent messages. | |
159 bool GetString16FromMessageWithID(uint32_t messageID, base::string16* value) { | |
160 const IPC::Message* message = | |
161 process()->sink().GetFirstMessageMatching(messageID); | |
162 if (!message) | |
163 return false; | |
164 std::tuple<base::string16> autofill_param; | |
165 switch (messageID) { | |
166 case AutofillMsg_FillFieldWithValue::ID: | |
167 if (!AutofillMsg_FillFieldWithValue::Read(message, &autofill_param)) | |
168 return false; | |
169 break; | |
170 case AutofillMsg_PreviewFieldWithValue::ID: | |
171 if (!AutofillMsg_PreviewFieldWithValue::Read(message, &autofill_param)) | |
172 return false; | |
173 break; | |
174 case AutofillMsg_AcceptDataListSuggestion::ID: | |
175 if (!AutofillMsg_AcceptDataListSuggestion::Read(message, | |
176 &autofill_param)) | |
177 return false; | |
178 break; | |
179 default: | |
180 NOTREACHED(); | |
181 } | |
182 if (value) | |
183 *value = std::get<0>(autofill_param); | |
184 process()->sink().ClearMessages(); | |
185 return true; | |
186 } | |
187 | |
188 // Searches for a message matching |messageID| in the queue of sent IPC | |
189 // messages. If none is present, returns false. Otherwise, clears the queue | |
190 // of sent messages and returns true. | |
191 bool HasMessageMatchingID(uint32_t messageID) { | |
192 const IPC::Message* message = | |
193 process()->sink().GetFirstMessageMatching(messageID); | |
194 if (!message) | |
195 return false; | |
196 process()->sink().ClearMessages(); | |
197 return true; | |
198 } | |
199 | |
200 std::unique_ptr<TestAutofillClient> test_autofill_client_; | 283 std::unique_ptr<TestAutofillClient> test_autofill_client_; |
201 std::unique_ptr<TestContentAutofillDriver> driver_; | 284 std::unique_ptr<TestContentAutofillDriver> driver_; |
| 285 |
| 286 FakeAutofillAgent fake_agent_; |
202 }; | 287 }; |
203 | 288 |
204 TEST_F(ContentAutofillDriverTest, GetURLRequestContext) { | 289 TEST_F(ContentAutofillDriverTest, GetURLRequestContext) { |
205 net::URLRequestContextGetter* request_context = | 290 net::URLRequestContextGetter* request_context = |
206 driver_->GetURLRequestContext(); | 291 driver_->GetURLRequestContext(); |
207 net::URLRequestContextGetter* expected_request_context = | 292 net::URLRequestContextGetter* expected_request_context = |
208 content::BrowserContext::GetDefaultStoragePartition( | 293 content::BrowserContext::GetDefaultStoragePartition( |
209 web_contents()->GetBrowserContext())->GetURLRequestContext(); | 294 web_contents()->GetBrowserContext())->GetURLRequestContext(); |
210 EXPECT_EQ(request_context, expected_request_context); | 295 EXPECT_EQ(request_context, expected_request_context); |
211 } | 296 } |
(...skipping 14 matching lines...) Expand all Loading... |
226 details.is_main_frame = false; | 311 details.is_main_frame = false; |
227 ASSERT_TRUE(!details.is_navigation_to_different_page()); | 312 ASSERT_TRUE(!details.is_navigation_to_different_page()); |
228 content::FrameNavigateParams params = content::FrameNavigateParams(); | 313 content::FrameNavigateParams params = content::FrameNavigateParams(); |
229 driver_->DidNavigateFrame(details, params); | 314 driver_->DidNavigateFrame(details, params); |
230 } | 315 } |
231 | 316 |
232 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_FillForm) { | 317 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_FillForm) { |
233 int input_page_id = 42; | 318 int input_page_id = 42; |
234 FormData input_form_data; | 319 FormData input_form_data; |
235 test::CreateTestAddressFormData(&input_form_data); | 320 test::CreateTestAddressFormData(&input_form_data); |
| 321 base::RunLoop run_loop; |
| 322 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
236 driver_->SendFormDataToRenderer( | 323 driver_->SendFormDataToRenderer( |
237 input_page_id, AutofillDriver::FORM_DATA_ACTION_FILL, input_form_data); | 324 input_page_id, AutofillDriver::FORM_DATA_ACTION_FILL, input_form_data); |
238 | 325 |
| 326 run_loop.RunUntilIdle(); |
| 327 |
239 int output_page_id = 0; | 328 int output_page_id = 0; |
240 FormData output_form_data; | 329 FormData output_form_data; |
241 EXPECT_FALSE( | 330 EXPECT_FALSE(fake_agent_.GetAutofillPreviewFormMessage(&output_page_id, |
242 GetAutofillPreviewFormMessage(&output_page_id, &output_form_data)); | 331 &output_form_data)); |
243 EXPECT_TRUE(GetAutofillFillFormMessage(&output_page_id, &output_form_data)); | 332 EXPECT_TRUE(fake_agent_.GetAutofillFillFormMessage(&output_page_id, |
| 333 &output_form_data)); |
244 EXPECT_EQ(input_page_id, output_page_id); | 334 EXPECT_EQ(input_page_id, output_page_id); |
245 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data)); | 335 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data)); |
246 } | 336 } |
247 | 337 |
248 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_PreviewForm) { | 338 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_PreviewForm) { |
249 int input_page_id = 42; | 339 int input_page_id = 42; |
250 FormData input_form_data; | 340 FormData input_form_data; |
251 test::CreateTestAddressFormData(&input_form_data); | 341 test::CreateTestAddressFormData(&input_form_data); |
| 342 base::RunLoop run_loop; |
| 343 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
252 driver_->SendFormDataToRenderer( | 344 driver_->SendFormDataToRenderer( |
253 input_page_id, AutofillDriver::FORM_DATA_ACTION_PREVIEW, input_form_data); | 345 input_page_id, AutofillDriver::FORM_DATA_ACTION_PREVIEW, input_form_data); |
254 | 346 |
| 347 run_loop.RunUntilIdle(); |
| 348 |
255 int output_page_id = 0; | 349 int output_page_id = 0; |
256 FormData output_form_data; | 350 FormData output_form_data; |
257 EXPECT_FALSE(GetAutofillFillFormMessage(&output_page_id, &output_form_data)); | 351 EXPECT_FALSE(fake_agent_.GetAutofillFillFormMessage(&output_page_id, |
258 EXPECT_TRUE( | 352 &output_form_data)); |
259 GetAutofillPreviewFormMessage(&output_page_id, &output_form_data)); | 353 EXPECT_TRUE(fake_agent_.GetAutofillPreviewFormMessage(&output_page_id, |
| 354 &output_form_data)); |
260 EXPECT_EQ(input_page_id, output_page_id); | 355 EXPECT_EQ(input_page_id, output_page_id); |
261 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data)); | 356 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data)); |
262 } | 357 } |
263 | 358 |
264 TEST_F(ContentAutofillDriverTest, | 359 TEST_F(ContentAutofillDriverTest, |
265 TypePredictionsNotSentToRendererWhenDisabled) { | 360 TypePredictionsNotSentToRendererWhenDisabled) { |
266 FormData form; | 361 FormData form; |
267 test::CreateTestAddressFormData(&form); | 362 test::CreateTestAddressFormData(&form); |
268 FormStructure form_structure(form); | 363 FormStructure form_structure(form); |
269 std::vector<FormStructure*> forms(1, &form_structure); | 364 std::vector<FormStructure*> forms(1, &form_structure); |
| 365 |
| 366 base::RunLoop run_loop; |
| 367 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
270 driver_->SendAutofillTypePredictionsToRenderer(forms); | 368 driver_->SendAutofillTypePredictionsToRenderer(forms); |
271 EXPECT_FALSE(GetFieldTypePredictionsAvailable(NULL)); | 369 run_loop.RunUntilIdle(); |
| 370 |
| 371 EXPECT_FALSE(fake_agent_.GetFieldTypePredictionsAvailable(NULL)); |
272 } | 372 } |
273 | 373 |
274 TEST_F(ContentAutofillDriverTest, TypePredictionsSentToRendererWhenEnabled) { | 374 TEST_F(ContentAutofillDriverTest, TypePredictionsSentToRendererWhenEnabled) { |
275 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 375 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
276 switches::kShowAutofillTypePredictions); | 376 switches::kShowAutofillTypePredictions); |
277 | 377 |
278 FormData form; | 378 FormData form; |
279 test::CreateTestAddressFormData(&form); | 379 test::CreateTestAddressFormData(&form); |
280 FormStructure form_structure(form); | 380 FormStructure form_structure(form); |
281 std::vector<FormStructure*> forms(1, &form_structure); | 381 std::vector<FormStructure*> forms(1, &form_structure); |
282 std::vector<FormDataPredictions> expected_type_predictions = | 382 std::vector<FormDataPredictions> expected_type_predictions = |
283 FormStructure::GetFieldTypePredictions(forms); | 383 FormStructure::GetFieldTypePredictions(forms); |
| 384 |
| 385 base::RunLoop run_loop; |
| 386 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
284 driver_->SendAutofillTypePredictionsToRenderer(forms); | 387 driver_->SendAutofillTypePredictionsToRenderer(forms); |
| 388 run_loop.RunUntilIdle(); |
285 | 389 |
286 std::vector<FormDataPredictions> output_type_predictions; | 390 std::vector<FormDataPredictions> output_type_predictions; |
287 EXPECT_TRUE(GetFieldTypePredictionsAvailable(&output_type_predictions)); | 391 EXPECT_TRUE( |
| 392 fake_agent_.GetFieldTypePredictionsAvailable(&output_type_predictions)); |
288 EXPECT_EQ(expected_type_predictions, output_type_predictions); | 393 EXPECT_EQ(expected_type_predictions, output_type_predictions); |
289 } | 394 } |
290 | 395 |
291 TEST_F(ContentAutofillDriverTest, AcceptDataListSuggestion) { | 396 TEST_F(ContentAutofillDriverTest, AcceptDataListSuggestion) { |
292 base::string16 input_value(base::ASCIIToUTF16("barfoo")); | 397 base::string16 input_value(base::ASCIIToUTF16("barfoo")); |
293 base::string16 output_value; | 398 base::string16 output_value; |
| 399 |
| 400 base::RunLoop run_loop; |
| 401 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
294 driver_->RendererShouldAcceptDataListSuggestion(input_value); | 402 driver_->RendererShouldAcceptDataListSuggestion(input_value); |
295 EXPECT_TRUE(GetString16FromMessageWithID( | 403 run_loop.RunUntilIdle(); |
296 AutofillMsg_AcceptDataListSuggestion::ID, &output_value)); | 404 |
| 405 EXPECT_TRUE(fake_agent_.GetString16AcceptDataListSuggestion(&output_value)); |
297 EXPECT_EQ(input_value, output_value); | 406 EXPECT_EQ(input_value, output_value); |
298 } | 407 } |
299 | 408 |
300 TEST_F(ContentAutofillDriverTest, ClearFilledFormSentToRenderer) { | 409 TEST_F(ContentAutofillDriverTest, ClearFilledFormSentToRenderer) { |
| 410 base::RunLoop run_loop; |
| 411 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
301 driver_->RendererShouldClearFilledForm(); | 412 driver_->RendererShouldClearFilledForm(); |
302 EXPECT_TRUE(HasMessageMatchingID(AutofillMsg_ClearForm::ID)); | 413 run_loop.RunUntilIdle(); |
| 414 |
| 415 EXPECT_TRUE(fake_agent_.GetCalledClearForm()); |
303 } | 416 } |
304 | 417 |
305 TEST_F(ContentAutofillDriverTest, ClearPreviewedFormSentToRenderer) { | 418 TEST_F(ContentAutofillDriverTest, ClearPreviewedFormSentToRenderer) { |
| 419 base::RunLoop run_loop; |
| 420 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
306 driver_->RendererShouldClearPreviewedForm(); | 421 driver_->RendererShouldClearPreviewedForm(); |
307 EXPECT_TRUE(HasMessageMatchingID(AutofillMsg_ClearPreviewedForm::ID)); | 422 run_loop.RunUntilIdle(); |
| 423 |
| 424 EXPECT_TRUE(fake_agent_.GetCalledClearPreviewedForm()); |
308 } | 425 } |
309 | 426 |
310 TEST_F(ContentAutofillDriverTest, FillFieldWithValue) { | 427 TEST_F(ContentAutofillDriverTest, FillFieldWithValue) { |
311 base::string16 input_value(base::ASCIIToUTF16("barqux")); | 428 base::string16 input_value(base::ASCIIToUTF16("barqux")); |
312 base::string16 output_value; | 429 base::string16 output_value; |
313 | 430 |
| 431 base::RunLoop run_loop; |
| 432 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
314 driver_->RendererShouldFillFieldWithValue(input_value); | 433 driver_->RendererShouldFillFieldWithValue(input_value); |
315 EXPECT_TRUE(GetString16FromMessageWithID(AutofillMsg_FillFieldWithValue::ID, | 434 run_loop.RunUntilIdle(); |
316 &output_value)); | 435 |
| 436 EXPECT_TRUE(fake_agent_.GetString16FillFieldWithValue(&output_value)); |
317 EXPECT_EQ(input_value, output_value); | 437 EXPECT_EQ(input_value, output_value); |
318 } | 438 } |
319 | 439 |
320 TEST_F(ContentAutofillDriverTest, PreviewFieldWithValue) { | 440 TEST_F(ContentAutofillDriverTest, PreviewFieldWithValue) { |
321 base::string16 input_value(base::ASCIIToUTF16("barqux")); | 441 base::string16 input_value(base::ASCIIToUTF16("barqux")); |
322 base::string16 output_value; | 442 base::string16 output_value; |
| 443 |
| 444 base::RunLoop run_loop; |
| 445 fake_agent_.SetQuitLoopClosure(run_loop.QuitClosure()); |
323 driver_->RendererShouldPreviewFieldWithValue(input_value); | 446 driver_->RendererShouldPreviewFieldWithValue(input_value); |
324 EXPECT_TRUE(GetString16FromMessageWithID( | 447 run_loop.RunUntilIdle(); |
325 AutofillMsg_PreviewFieldWithValue::ID, | 448 |
326 &output_value)); | 449 EXPECT_TRUE(fake_agent_.GetString16PreviewFieldWithValue(&output_value)); |
327 EXPECT_EQ(input_value, output_value); | 450 EXPECT_EQ(input_value, output_value); |
328 } | 451 } |
329 | 452 |
330 } // namespace autofill | 453 } // namespace autofill |
OLD | NEW |