1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include <vector>
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/autofill/content/browser/autofill_driver_impl.h"
#include "components/autofill/core/browser/autofill_common_test.h"
#include "components/autofill/core/browser/autofill_external_delegate.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/test_autofill_manager_delegate.h"
#include "components/autofill/core/common/autofill_messages.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data_predictions.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/frame_navigate_params.h"
#include "content/public/test/mock_render_process_host.h"
#include "ipc/ipc_test_sink.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
namespace {
const std::string kAppLocale = "en-US";
const AutofillManager::AutofillDownloadManagerState kDownloadState =
AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER;
} // namespace
class MockAutofillManager : public AutofillManager {
public:
MockAutofillManager(AutofillDriver* driver,
AutofillManagerDelegate* delegate)
: AutofillManager(driver, delegate, kAppLocale, kDownloadState) {
}
virtual ~MockAutofillManager() {}
MOCK_METHOD0(Reset, void());
};
class TestAutofillDriverImpl : public AutofillDriverImpl {
public:
TestAutofillDriverImpl(content::WebContents* contents,
AutofillManagerDelegate* delegate)
: AutofillDriverImpl(contents, delegate, kAppLocale, kDownloadState) {
scoped_ptr<AutofillManager> autofill_manager(
new MockAutofillManager(this, delegate));
SetAutofillManager(autofill_manager.Pass());
}
virtual ~TestAutofillDriverImpl() {}
virtual MockAutofillManager* mock_autofill_manager() {
return static_cast<MockAutofillManager*>(autofill_manager());
}
using AutofillDriverImpl::DidNavigateMainFrame;
};
class AutofillDriverImplTest : public ChromeRenderViewHostTestHarness {
public:
virtual void SetUp() OVERRIDE {
ChromeRenderViewHostTestHarness::SetUp();
test_manager_delegate_.reset(new TestAutofillManagerDelegate());
driver_.reset(new TestAutofillDriverImpl(web_contents(),
test_manager_delegate_.get()));
}
virtual void TearDown() OVERRIDE {
// Reset the driver now to cause all pref observers to be removed and avoid
// crashes that otherwise occur in the destructor.
driver_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
protected:
// Searches for an |AutofillMsg_FormDataFilled| message in the queue of sent
// IPC messages. If none is present, returns false. Otherwise, extracts the
// first |AutofillMsg_FormDataFilled| message, fills the output parameters
// with the values of the message's parameters, and clears the queue of sent
// messages.
bool GetAutofillFormDataFilledMessage(int* page_id, FormData* results) {
const uint32 kMsgID = AutofillMsg_FormDataFilled::ID;
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(kMsgID);
if (!message)
return false;
Tuple2<int, FormData> autofill_param;
AutofillMsg_FormDataFilled::Read(message, &autofill_param);
if (page_id)
*page_id = autofill_param.a;
if (results)
*results = autofill_param.b;
process()->sink().ClearMessages();
return true;
}
// Searches for an |AutofillMsg_FieldTypePredictionsAvailable| message in the
// queue of sent IPC messages. If none is present, returns false. Otherwise,
// extracts the first |AutofillMsg_FieldTypePredictionsAvailable| message,
// fills the output parameter with the values of the message's parameter, and
// clears the queue of sent messages.
bool GetFieldTypePredictionsAvailable(
std::vector<FormDataPredictions>* predictions) {
const uint32 kMsgID = AutofillMsg_FieldTypePredictionsAvailable::ID;
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(kMsgID);
if (!message)
return false;
Tuple1<std::vector<FormDataPredictions> > autofill_param;
AutofillMsg_FieldTypePredictionsAvailable::Read(message, &autofill_param);
if (predictions)
*predictions = autofill_param.a;
process()->sink().ClearMessages();
return true;
}
// Searches for an |AutofillMsg_SetAutofillActionPreview| message in the
// queue of sent IPC messages. If none is present, returns false. Otherwise,
// clears the queue of sent messages and returns true.
bool GetSetAutofillActionPreviewMessage() {
const uint32 kMsgID = AutofillMsg_SetAutofillActionPreview::ID;
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(kMsgID);
if (!message)
return false;
process()->sink().ClearMessages();
return true;
}
// Searches for an |AutofillMsg_SetAutofillActionFill| message in the
// queue of sent IPC messages. If none is present, returns false. Otherwise,
// clears the queue of sent messages and returns true.
bool GetSetAutofillActionFillMessage() {
const uint32 kMsgID = AutofillMsg_SetAutofillActionFill::ID;
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(kMsgID);
if (!message)
return false;
process()->sink().ClearMessages();
return true;
}
scoped_ptr<TestAutofillManagerDelegate> test_manager_delegate_;
scoped_ptr<TestAutofillDriverImpl> driver_;
};
TEST_F(AutofillDriverImplTest, NavigatedToDifferentPage) {
EXPECT_CALL(*driver_->mock_autofill_manager(), Reset());
content::LoadCommittedDetails details = content::LoadCommittedDetails();
details.is_main_frame = true;
details.is_in_page = false;
ASSERT_TRUE(details.is_navigation_to_different_page());
content::FrameNavigateParams params = content::FrameNavigateParams();
driver_->DidNavigateMainFrame(details, params);
}
TEST_F(AutofillDriverImplTest, NavigatedWithinSamePage) {
EXPECT_CALL(*driver_->mock_autofill_manager(), Reset()).Times(0);
content::LoadCommittedDetails details = content::LoadCommittedDetails();
details.is_main_frame = false;
ASSERT_TRUE(!details.is_navigation_to_different_page());
content::FrameNavigateParams params = content::FrameNavigateParams();
driver_->DidNavigateMainFrame(details, params);
}
TEST_F(AutofillDriverImplTest, FormDataSentToRenderer) {
int input_page_id = 42;
FormData input_form_data;
test::CreateTestAddressFormData(&input_form_data);
driver_->SendFormDataToRenderer(input_page_id, input_form_data);
int output_page_id = 0;
FormData output_form_data;
EXPECT_TRUE(GetAutofillFormDataFilledMessage(&output_page_id,
&output_form_data));
EXPECT_EQ(input_page_id, output_page_id);
EXPECT_EQ(input_form_data, output_form_data);
}
TEST_F(AutofillDriverImplTest, TypePredictionsNotSentToRendererWhenDisabled) {
FormData form;
test::CreateTestAddressFormData(&form);
FormStructure form_structure(form, std::string());
std::vector<FormStructure*> forms(1, &form_structure);
driver_->SendAutofillTypePredictionsToRenderer(forms);
EXPECT_FALSE(GetFieldTypePredictionsAvailable(NULL));
}
TEST_F(AutofillDriverImplTest, TypePredictionsSentToRendererWhenEnabled) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kShowAutofillTypePredictions);
FormData form;
test::CreateTestAddressFormData(&form);
FormStructure form_structure(form, std::string());
std::vector<FormStructure*> forms(1, &form_structure);
std::vector<FormDataPredictions> expected_type_predictions;
FormStructure::GetFieldTypePredictions(forms, &expected_type_predictions);
driver_->SendAutofillTypePredictionsToRenderer(forms);
std::vector<FormDataPredictions> output_type_predictions;
EXPECT_TRUE(GetFieldTypePredictionsAvailable(&output_type_predictions));
EXPECT_EQ(expected_type_predictions, output_type_predictions);
}
TEST_F(AutofillDriverImplTest, PreviewActionSentToRenderer) {
driver_->SetRendererActionOnFormDataReception(
AutofillDriver::FORM_DATA_ACTION_PREVIEW);
EXPECT_TRUE(GetSetAutofillActionPreviewMessage());
}
TEST_F(AutofillDriverImplTest, FillActionSentToRenderer) {
driver_->SetRendererActionOnFormDataReception(
AutofillDriver::FORM_DATA_ACTION_FILL);
EXPECT_TRUE(GetSetAutofillActionFillMessage());
}
} // namespace autofill
|