Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: components/autofill/content/browser/content_autofill_driver_unittest.cc

Issue 2007473004: [Autofill] Migrate ContentAutofillDriver<-->AutofillAgent IPCs to mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/message_loop/message_loop.h"
17 #include "base/run_loop.h"
16 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
17 #include "components/autofill/content/common/autofill_messages.h" 19 #include "components/autofill/content/common/autofill_messages.h"
18 #include "components/autofill/core/browser/autofill_external_delegate.h" 20 #include "components/autofill/core/browser/autofill_external_delegate.h"
19 #include "components/autofill/core/browser/autofill_manager.h" 21 #include "components/autofill/core/browser/autofill_manager.h"
20 #include "components/autofill/core/browser/autofill_test_utils.h" 22 #include "components/autofill/core/browser/autofill_test_utils.h"
21 #include "components/autofill/core/browser/test_autofill_client.h" 23 #include "components/autofill/core/browser/test_autofill_client.h"
22 #include "components/autofill/core/common/autofill_switches.h" 24 #include "components/autofill/core/common/autofill_switches.h"
23 #include "components/autofill/core/common/form_data_predictions.h" 25 #include "components/autofill/core/common/form_data_predictions.h"
24 #include "content/public/browser/browser_context.h" 26 #include "content/public/browser/browser_context.h"
25 #include "content/public/browser/navigation_details.h" 27 #include "content/public/browser/navigation_details.h"
26 #include "content/public/browser/storage_partition.h" 28 #include "content/public/browser/storage_partition.h"
27 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
28 #include "content/public/common/frame_navigate_params.h" 30 #include "content/public/common/frame_navigate_params.h"
31 #include "content/public/common/service_registry.h"
29 #include "content/public/test/mock_render_process_host.h" 32 #include "content/public/test/mock_render_process_host.h"
30 #include "content/public/test/test_renderer_host.h" 33 #include "content/public/test/test_renderer_host.h"
31 #include "ipc/ipc_test_sink.h" 34 #include "ipc/ipc_test_sink.h"
35 #include "mojo/public/cpp/bindings/binding_set.h"
32 #include "testing/gmock/include/gmock/gmock.h" 36 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
34 38
35 namespace autofill { 39 namespace autofill {
36 40
37 namespace { 41 namespace {
38 42
39 const char kAppLocale[] = "en-US"; 43 const char kAppLocale[] = "en-US";
40 const AutofillManager::AutofillDownloadManagerState kDownloadState = 44 const AutofillManager::AutofillDownloadManagerState kDownloadState =
41 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER; 45 AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER;
42 46
47 void RunAllPendingTasks() {
48 base::RunLoop run_loop;
49 base::MessageLoop::current()->PostTask(
50 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
yzshen1 2016/05/24 16:12:10 QuitWhenIdleClosure is deprecated according to the
leonhsl(Using Gerrit) 2016/06/23 10:00:12 Done.
51 run_loop.Run();
52 }
53
43 } // namespace 54 } // namespace
44 55
45 class MockAutofillManager : public AutofillManager { 56 class MockAutofillManager : public AutofillManager {
46 public: 57 public:
47 MockAutofillManager(AutofillDriver* driver, AutofillClient* client) 58 MockAutofillManager(AutofillDriver* driver, AutofillClient* client)
48 : AutofillManager(driver, client, kAppLocale, kDownloadState) {} 59 : AutofillManager(driver, client, kAppLocale, kDownloadState) {}
49 virtual ~MockAutofillManager() {} 60 virtual ~MockAutofillManager() {}
50 61
51 MOCK_METHOD0(Reset, void()); 62 MOCK_METHOD0(Reset, void());
52 }; 63 };
53 64
54 class TestContentAutofillDriver : public ContentAutofillDriver { 65 class TestContentAutofillDriver : public ContentAutofillDriver {
55 public: 66 public:
56 TestContentAutofillDriver(content::RenderFrameHost* rfh, 67 TestContentAutofillDriver(content::RenderFrameHost* rfh,
57 AutofillClient* client) 68 AutofillClient* client)
58 : ContentAutofillDriver(rfh, client, kAppLocale, kDownloadState) { 69 : ContentAutofillDriver(rfh, client, kAppLocale, kDownloadState) {
59 std::unique_ptr<AutofillManager> autofill_manager( 70 std::unique_ptr<AutofillManager> autofill_manager(
60 new MockAutofillManager(this, client)); 71 new MockAutofillManager(this, client));
61 SetAutofillManager(std::move(autofill_manager)); 72 SetAutofillManager(std::move(autofill_manager));
62 } 73 }
63 ~TestContentAutofillDriver() override {} 74 ~TestContentAutofillDriver() override {}
64 75
65 virtual MockAutofillManager* mock_autofill_manager() { 76 virtual MockAutofillManager* mock_autofill_manager() {
66 return static_cast<MockAutofillManager*>(autofill_manager()); 77 return static_cast<MockAutofillManager*>(autofill_manager());
67 } 78 }
68 79
69 using ContentAutofillDriver::DidNavigateFrame; 80 using ContentAutofillDriver::DidNavigateFrame;
70 }; 81 };
71 82
72 class ContentAutofillDriverTest : public content::RenderViewHostTestHarness { 83 class ContentAutofillDriverTest : public content::RenderViewHostTestHarness,
84 public mojom::AutofillAgent {
vabr (Chromium) 2016/05/24 08:08:20 Please aggregate a test version of an mojom::Autof
leonhsl(Using Gerrit) 2016/06/23 10:00:12 Done.
73 public: 85 public:
74 void SetUp() override { 86 void SetUp() override {
75 content::RenderViewHostTestHarness::SetUp(); 87 content::RenderViewHostTestHarness::SetUp();
76 88
77 test_autofill_client_.reset(new TestAutofillClient()); 89 test_autofill_client_.reset(new TestAutofillClient());
78 driver_.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(), 90 driver_.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(),
79 test_autofill_client_.get())); 91 test_autofill_client_.get()));
92
93 id_sent_ = -1;
vabr (Chromium) 2016/05/24 08:08:20 Please initialise this in the constructor or via a
leonhsl(Using Gerrit) 2016/06/23 10:00:12 Done.
94 form_sent_.reset();
vabr (Chromium) 2016/05/24 08:08:20 No need for this, it is null since construction, a
leonhsl(Using Gerrit) 2016/06/23 10:00:12 Done.
95
96 content::ServiceRegistry* registry =
97 web_contents()->GetMainFrame()->GetServiceRegistry();
98 registry->AddServiceOverrideForTesting(
99 mojom::AutofillAgent::Name_,
100 base::Bind(&ContentAutofillDriverTest::BindFakeAutofillAgent,
101 base::Unretained(this)));
80 } 102 }
81 103
82 void TearDown() override { 104 void TearDown() override {
83 // Reset the driver now to cause all pref observers to be removed and avoid 105 // Reset the driver now to cause all pref observers to be removed and avoid
84 // crashes that otherwise occur in the destructor. 106 // crashes that otherwise occur in the destructor.
85 driver_.reset(); 107 driver_.reset();
86 content::RenderViewHostTestHarness::TearDown(); 108 content::RenderViewHostTestHarness::TearDown();
87 } 109 }
88 110
111 void BindFakeAutofillAgent(mojo::ScopedMessagePipeHandle handle) {
112 bindings_.AddBinding(
113 this, mojo::MakeRequest<mojom::AutofillAgent>(std::move(handle)));
114 }
115
89 protected: 116 protected:
90 // Searches for an |AutofillMsg_FillForm| message in the queue of sent IPC 117 // mojom::AutofillAgent methods:
91 // messages. If none is present, returns false. Otherwise, extracts the first 118 void FirstUserGestureObservedInTab() override {}
92 // |AutofillMsg_FillForm| message, fills the output parameters with the values 119
93 // of the message's parameters, and clears the queue of sent messages. 120 void FillForm(int32_t id,
121 const FormData& form,
122 const FillFormCallback& callback) override {
123 id_sent_ = id;
124 form_sent_.reset(new FormData(form));
125 callback.Run(form, 0);
126 }
127
128 // Returns the id and formdata received via
129 // mojo interface method mojom::AutofillAgent::FillForm().
94 bool GetAutofillFillFormMessage(int* page_id, FormData* results) { 130 bool GetAutofillFillFormMessage(int* page_id, FormData* results) {
95 const uint32_t kMsgID = AutofillMsg_FillForm::ID; 131 if (id_sent_ == -1)
96 const IPC::Message* message =
97 process()->sink().GetFirstMessageMatching(kMsgID);
98 if (!message)
99 return false; 132 return false;
100 std::tuple<int, FormData> autofill_param; 133 if (!form_sent_)
101 if (!AutofillMsg_FillForm::Read(message, &autofill_param))
102 return false; 134 return false;
135
103 if (page_id) 136 if (page_id)
104 *page_id = std::get<0>(autofill_param); 137 *page_id = id_sent_;
105 if (results) 138 if (results)
106 *results = std::get<1>(autofill_param); 139 *results = *form_sent_;
107 process()->sink().ClearMessages();
108 return true; 140 return true;
109 } 141 }
110 142
111 // Searches for an |AutofillMsg_PreviewForm| message in the queue of sent IPC 143 // Searches for an |AutofillMsg_PreviewForm| message in the queue of sent IPC
112 // messages. If none is present, returns false. Otherwise, extracts the first 144 // messages. If none is present, returns false. Otherwise, extracts the first
113 // |AutofillMsg_PreviewForm| message, fills the output parameters with the 145 // |AutofillMsg_PreviewForm| message, fills the output parameters with the
114 // values of the message's parameters, and clears the queue of sent messages. 146 // values of the message's parameters, and clears the queue of sent messages.
115 bool GetAutofillPreviewFormMessage(int* page_id, FormData* results) { 147 bool GetAutofillPreviewFormMessage(int* page_id, FormData* results) {
116 const uint32_t kMsgID = AutofillMsg_PreviewForm::ID; 148 const uint32_t kMsgID = AutofillMsg_PreviewForm::ID;
117 const IPC::Message* message = 149 const IPC::Message* message =
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 const IPC::Message* message = 224 const IPC::Message* message =
193 process()->sink().GetFirstMessageMatching(messageID); 225 process()->sink().GetFirstMessageMatching(messageID);
194 if (!message) 226 if (!message)
195 return false; 227 return false;
196 process()->sink().ClearMessages(); 228 process()->sink().ClearMessages();
197 return true; 229 return true;
198 } 230 }
199 231
200 std::unique_ptr<TestAutofillClient> test_autofill_client_; 232 std::unique_ptr<TestAutofillClient> test_autofill_client_;
201 std::unique_ptr<TestContentAutofillDriver> driver_; 233 std::unique_ptr<TestContentAutofillDriver> driver_;
234 mojo::BindingSet<mojom::AutofillAgent> bindings_;
235
236 int32_t id_sent_;
vabr (Chromium) 2016/05/24 08:08:20 nit: Please comment about what |id_sent_| and |for
leonhsl(Using Gerrit) 2016/06/23 10:00:12 Done.
237 std::unique_ptr<FormData> form_sent_;
202 }; 238 };
203 239
204 TEST_F(ContentAutofillDriverTest, GetURLRequestContext) { 240 TEST_F(ContentAutofillDriverTest, GetURLRequestContext) {
205 net::URLRequestContextGetter* request_context = 241 net::URLRequestContextGetter* request_context =
206 driver_->GetURLRequestContext(); 242 driver_->GetURLRequestContext();
207 net::URLRequestContextGetter* expected_request_context = 243 net::URLRequestContextGetter* expected_request_context =
208 content::BrowserContext::GetDefaultStoragePartition( 244 content::BrowserContext::GetDefaultStoragePartition(
209 web_contents()->GetBrowserContext())->GetURLRequestContext(); 245 web_contents()->GetBrowserContext())->GetURLRequestContext();
210 EXPECT_EQ(request_context, expected_request_context); 246 EXPECT_EQ(request_context, expected_request_context);
211 } 247 }
(...skipping 14 matching lines...) Expand all
226 details.is_main_frame = false; 262 details.is_main_frame = false;
227 ASSERT_TRUE(!details.is_navigation_to_different_page()); 263 ASSERT_TRUE(!details.is_navigation_to_different_page());
228 content::FrameNavigateParams params = content::FrameNavigateParams(); 264 content::FrameNavigateParams params = content::FrameNavigateParams();
229 driver_->DidNavigateFrame(details, params); 265 driver_->DidNavigateFrame(details, params);
230 } 266 }
231 267
232 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_FillForm) { 268 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_FillForm) {
233 int input_page_id = 42; 269 int input_page_id = 42;
234 FormData input_form_data; 270 FormData input_form_data;
235 test::CreateTestAddressFormData(&input_form_data); 271 test::CreateTestAddressFormData(&input_form_data);
236 driver_->SendFormDataToRenderer( 272 driver_->SendFormDataToRenderer(
yzshen1 2016/05/24 16:12:10 I am not familiar with auto fill code. Does this c
leonhsl(Using Gerrit) 2016/06/23 10:00:12 Done.
237 input_page_id, AutofillDriver::FORM_DATA_ACTION_FILL, input_form_data); 273 input_page_id, AutofillDriver::FORM_DATA_ACTION_FILL, input_form_data);
238 274
275 RunAllPendingTasks();
276
239 int output_page_id = 0; 277 int output_page_id = 0;
240 FormData output_form_data; 278 FormData output_form_data;
241 EXPECT_FALSE( 279 EXPECT_FALSE(
242 GetAutofillPreviewFormMessage(&output_page_id, &output_form_data)); 280 GetAutofillPreviewFormMessage(&output_page_id, &output_form_data));
243 EXPECT_TRUE(GetAutofillFillFormMessage(&output_page_id, &output_form_data)); 281 EXPECT_TRUE(GetAutofillFillFormMessage(&output_page_id, &output_form_data));
244 EXPECT_EQ(input_page_id, output_page_id); 282 EXPECT_EQ(input_page_id, output_page_id);
245 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data)); 283 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data));
246 } 284 }
247 285
248 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_PreviewForm) { 286 TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_PreviewForm) {
249 int input_page_id = 42; 287 int input_page_id = 42;
250 FormData input_form_data; 288 FormData input_form_data;
251 test::CreateTestAddressFormData(&input_form_data); 289 test::CreateTestAddressFormData(&input_form_data);
252 driver_->SendFormDataToRenderer( 290 driver_->SendFormDataToRenderer(
253 input_page_id, AutofillDriver::FORM_DATA_ACTION_PREVIEW, input_form_data); 291 input_page_id, AutofillDriver::FORM_DATA_ACTION_PREVIEW, input_form_data);
254 292
293 RunAllPendingTasks();
294
255 int output_page_id = 0; 295 int output_page_id = 0;
256 FormData output_form_data; 296 FormData output_form_data;
257 EXPECT_FALSE(GetAutofillFillFormMessage(&output_page_id, &output_form_data)); 297 EXPECT_FALSE(GetAutofillFillFormMessage(&output_page_id, &output_form_data));
258 EXPECT_TRUE( 298 EXPECT_TRUE(
259 GetAutofillPreviewFormMessage(&output_page_id, &output_form_data)); 299 GetAutofillPreviewFormMessage(&output_page_id, &output_form_data));
260 EXPECT_EQ(input_page_id, output_page_id); 300 EXPECT_EQ(input_page_id, output_page_id);
261 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data)); 301 EXPECT_TRUE(input_form_data.SameFormAs(output_form_data));
262 } 302 }
263 303
264 TEST_F(ContentAutofillDriverTest, 304 TEST_F(ContentAutofillDriverTest,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 base::string16 input_value(base::ASCIIToUTF16("barqux")); 361 base::string16 input_value(base::ASCIIToUTF16("barqux"));
322 base::string16 output_value; 362 base::string16 output_value;
323 driver_->RendererShouldPreviewFieldWithValue(input_value); 363 driver_->RendererShouldPreviewFieldWithValue(input_value);
324 EXPECT_TRUE(GetString16FromMessageWithID( 364 EXPECT_TRUE(GetString16FromMessageWithID(
325 AutofillMsg_PreviewFieldWithValue::ID, 365 AutofillMsg_PreviewFieldWithValue::ID,
326 &output_value)); 366 &output_value));
327 EXPECT_EQ(input_value, output_value); 367 EXPECT_EQ(input_value, output_value);
328 } 368 }
329 369
330 } // namespace autofill 370 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698