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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// Copyright 2014 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 "base/strings/utf_string_conversions.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/common/autofill_messages.h"
#include "components/autofill/core/browser/autofill_external_delegate.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data_predictions.h"
#include "content/public/browser/browser_context.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 "content/public/test/test_renderer_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, AutofillClient* client)
: AutofillManager(driver, client, kAppLocale, kDownloadState) {}
virtual ~MockAutofillManager() {}
MOCK_METHOD0(Reset, void());
};
class TestContentAutofillDriver : public ContentAutofillDriver {
public:
TestContentAutofillDriver(content::RenderFrameHost* rfh,
AutofillClient* client)
: ContentAutofillDriver(rfh, client, kAppLocale, kDownloadState) {
scoped_ptr<AutofillManager> autofill_manager(
new MockAutofillManager(this, client));
SetAutofillManager(autofill_manager.Pass());
}
~TestContentAutofillDriver() override {}
virtual MockAutofillManager* mock_autofill_manager() {
return static_cast<MockAutofillManager*>(autofill_manager());
}
using ContentAutofillDriver::DidNavigateFrame;
};
class ContentAutofillDriverTest : public content::RenderViewHostTestHarness {
public:
void SetUp() override {
content::RenderViewHostTestHarness::SetUp();
test_autofill_client_.reset(new TestAutofillClient());
driver_.reset(new TestContentAutofillDriver(web_contents()->GetMainFrame(),
test_autofill_client_.get()));
}
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();
content::RenderViewHostTestHarness::TearDown();
}
protected:
// Searches for an |AutofillMsg_FillForm| message in the queue of sent IPC
// messages. If none is present, returns false. Otherwise, extracts the first
// |AutofillMsg_FillForm| message, fills the output parameters with the values
// of the message's parameters, and clears the queue of sent messages.
bool GetAutofillFillFormMessage(int* page_id, FormData* results) {
const uint32 kMsgID = AutofillMsg_FillForm::ID;
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(kMsgID);
if (!message)
return false;
Tuple<int, FormData> autofill_param;
if (!AutofillMsg_FillForm::Read(message, &autofill_param))
return false;
if (page_id)
*page_id = get<0>(autofill_param);
if (results)
*results = get<1>(autofill_param);
process()->sink().ClearMessages();
return true;
}
// Searches for an |AutofillMsg_PreviewForm| message in the queue of sent IPC
// messages. If none is present, returns false. Otherwise, extracts the first
// |AutofillMsg_PreviewForm| message, fills the output parameters with the
// values of the message's parameters, and clears the queue of sent messages.
bool GetAutofillPreviewFormMessage(int* page_id, FormData* results) {
const uint32 kMsgID = AutofillMsg_PreviewForm::ID;
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(kMsgID);
if (!message)
return false;
Tuple<int, FormData> autofill_param;
if (!AutofillMsg_PreviewForm::Read(message, &autofill_param))
return false;
if (page_id)
*page_id = get<0>(autofill_param);
if (results)
*results = get<1>(autofill_param);
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;
Tuple<std::vector<FormDataPredictions> > autofill_param;
if (!AutofillMsg_FieldTypePredictionsAvailable::Read(message,
&autofill_param))
return false;
if (predictions)
*predictions = get<0>(autofill_param);
process()->sink().ClearMessages();
return true;
}
// Searches for a message matching |messageID| in the queue of sent IPC
// messages. If none is present, returns false. Otherwise, extracts the first
// matching message, fills the output parameter with the string16 from the
// message's parameter, and clears the queue of sent messages.
bool GetString16FromMessageWithID(uint32 messageID, base::string16* value) {
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(messageID);
if (!message)
return false;
Tuple<base::string16> autofill_param;
switch (messageID) {
case AutofillMsg_FillFieldWithValue::ID:
if (!AutofillMsg_FillFieldWithValue::Read(message, &autofill_param))
return false;
break;
case AutofillMsg_PreviewFieldWithValue::ID:
if (!AutofillMsg_PreviewFieldWithValue::Read(message, &autofill_param))
return false;
break;
case AutofillMsg_AcceptDataListSuggestion::ID:
if (!AutofillMsg_AcceptDataListSuggestion::Read(message,
&autofill_param))
return false;
break;
default:
NOTREACHED();
}
if (value)
*value = get<0>(autofill_param);
process()->sink().ClearMessages();
return true;
}
// Searches for a message matching |messageID| in the queue of sent IPC
// messages. If none is present, returns false. Otherwise, clears the queue
// of sent messages and returns true.
bool HasMessageMatchingID(uint32 messageID) {
const IPC::Message* message =
process()->sink().GetFirstMessageMatching(messageID);
if (!message)
return false;
process()->sink().ClearMessages();
return true;
}
scoped_ptr<TestAutofillClient> test_autofill_client_;
scoped_ptr<TestContentAutofillDriver> driver_;
};
TEST_F(ContentAutofillDriverTest, GetURLRequestContext) {
net::URLRequestContextGetter* request_context =
driver_->GetURLRequestContext();
net::URLRequestContextGetter* expected_request_context =
web_contents()->GetBrowserContext()->GetRequestContext();
EXPECT_EQ(request_context, expected_request_context);
}
TEST_F(ContentAutofillDriverTest, 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_->DidNavigateFrame(details, params);
}
TEST_F(ContentAutofillDriverTest, 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_->DidNavigateFrame(details, params);
}
TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_FillForm) {
int input_page_id = 42;
FormData input_form_data;
test::CreateTestAddressFormData(&input_form_data);
driver_->SendFormDataToRenderer(
input_page_id, AutofillDriver::FORM_DATA_ACTION_FILL, input_form_data);
int output_page_id = 0;
FormData output_form_data;
EXPECT_FALSE(
GetAutofillPreviewFormMessage(&output_page_id, &output_form_data));
EXPECT_TRUE(GetAutofillFillFormMessage(&output_page_id, &output_form_data));
EXPECT_EQ(input_page_id, output_page_id);
EXPECT_TRUE(input_form_data.SameFormAs(output_form_data));
}
TEST_F(ContentAutofillDriverTest, FormDataSentToRenderer_PreviewForm) {
int input_page_id = 42;
FormData input_form_data;
test::CreateTestAddressFormData(&input_form_data);
driver_->SendFormDataToRenderer(
input_page_id, AutofillDriver::FORM_DATA_ACTION_PREVIEW, input_form_data);
int output_page_id = 0;
FormData output_form_data;
EXPECT_FALSE(GetAutofillFillFormMessage(&output_page_id, &output_form_data));
EXPECT_TRUE(
GetAutofillPreviewFormMessage(&output_page_id, &output_form_data));
EXPECT_EQ(input_page_id, output_page_id);
EXPECT_TRUE(input_form_data.SameFormAs(output_form_data));
}
TEST_F(ContentAutofillDriverTest,
TypePredictionsNotSentToRendererWhenDisabled) {
FormData form;
test::CreateTestAddressFormData(&form);
FormStructure form_structure(form);
std::vector<FormStructure*> forms(1, &form_structure);
driver_->SendAutofillTypePredictionsToRenderer(forms);
EXPECT_FALSE(GetFieldTypePredictionsAvailable(NULL));
}
TEST_F(ContentAutofillDriverTest, TypePredictionsSentToRendererWhenEnabled) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kShowAutofillTypePredictions);
FormData form;
test::CreateTestAddressFormData(&form);
FormStructure form_structure(form);
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(ContentAutofillDriverTest, AcceptDataListSuggestion) {
base::string16 input_value(base::ASCIIToUTF16("barfoo"));
base::string16 output_value;
driver_->RendererShouldAcceptDataListSuggestion(input_value);
EXPECT_TRUE(GetString16FromMessageWithID(
AutofillMsg_AcceptDataListSuggestion::ID, &output_value));
EXPECT_EQ(input_value, output_value);
}
TEST_F(ContentAutofillDriverTest, ClearFilledFormSentToRenderer) {
driver_->RendererShouldClearFilledForm();
EXPECT_TRUE(HasMessageMatchingID(AutofillMsg_ClearForm::ID));
}
TEST_F(ContentAutofillDriverTest, ClearPreviewedFormSentToRenderer) {
driver_->RendererShouldClearPreviewedForm();
EXPECT_TRUE(HasMessageMatchingID(AutofillMsg_ClearPreviewedForm::ID));
}
TEST_F(ContentAutofillDriverTest, FillFieldWithValue) {
base::string16 input_value(base::ASCIIToUTF16("barqux"));
base::string16 output_value;
driver_->RendererShouldFillFieldWithValue(input_value);
EXPECT_TRUE(GetString16FromMessageWithID(AutofillMsg_FillFieldWithValue::ID,
&output_value));
EXPECT_EQ(input_value, output_value);
}
TEST_F(ContentAutofillDriverTest, PreviewFieldWithValue) {
base::string16 input_value(base::ASCIIToUTF16("barqux"));
base::string16 output_value;
driver_->RendererShouldPreviewFieldWithValue(input_value);
EXPECT_TRUE(GetString16FromMessageWithID(
AutofillMsg_PreviewFieldWithValue::ID,
&output_value));
EXPECT_EQ(input_value, output_value);
}
} // namespace autofill
|