This source file includes following definitions.
- SetUp
- TearDown
- IssueOnQuery
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <vector>
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/popup_item_ids.h"
#include "components/autofill/core/browser/test_autofill_driver.h"
#include "components/autofill/core/browser/test_autofill_external_delegate.h"
#include "components/autofill/core/browser/test_autofill_manager_delegate.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/password_form_fill_data.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect.h"
using base::ASCIIToUTF16;
using testing::_;
namespace autofill {
namespace {
const int kQueryId = 5;
const int kAutofillProfileId = 1;
class MockAutofillDriver : public TestAutofillDriver {
public:
MockAutofillDriver() {}
MOCK_METHOD1(RendererShouldAcceptDataListSuggestion,
void(const base::string16&));
MOCK_METHOD0(RendererShouldClearFilledForm, void());
MOCK_METHOD0(RendererShouldClearPreviewedForm, void());
MOCK_METHOD1(RendererShouldFillFieldWithValue, void(const base::string16&));
MOCK_METHOD1(RendererShouldPreviewFieldWithValue,
void(const base::string16&));
private:
DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
};
class MockAutofillManagerDelegate
: public autofill::TestAutofillManagerDelegate {
public:
MockAutofillManagerDelegate() {}
MOCK_METHOD7(ShowAutofillPopup,
void(const gfx::RectF& element_bounds,
base::i18n::TextDirection text_direction,
const std::vector<base::string16>& values,
const std::vector<base::string16>& labels,
const std::vector<base::string16>& icons,
const std::vector<int>& identifiers,
base::WeakPtr<AutofillPopupDelegate> delegate));
MOCK_METHOD2(UpdateAutofillPopupDataListValues,
void(const std::vector<base::string16>& values,
const std::vector<base::string16>& lables));
MOCK_METHOD0(HideAutofillPopup, void());
private:
DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate);
};
class MockAutofillManager : public AutofillManager {
public:
MockAutofillManager(AutofillDriver* driver,
MockAutofillManagerDelegate* delegate)
: AutofillManager(driver, delegate, NULL) {
}
virtual ~MockAutofillManager() {}
MOCK_METHOD5(FillOrPreviewForm,
void(AutofillDriver::RendererFormDataAction action,
int query_id,
const FormData& form,
const FormFieldData& field,
int unique_id));
private:
DISALLOW_COPY_AND_ASSIGN(MockAutofillManager);
};
}
class AutofillExternalDelegateUnitTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE {
autofill_driver_.reset(new MockAutofillDriver());
autofill_manager_.reset(
new MockAutofillManager(autofill_driver_.get(),
&manager_delegate_));
external_delegate_.reset(
new AutofillExternalDelegate(
autofill_manager_.get(), autofill_driver_.get()));
}
virtual void TearDown() OVERRIDE {
autofill_manager_.reset();
external_delegate_.reset();
autofill_driver_.reset();
}
void IssueOnQuery(int query_id) {
const FormData form;
FormFieldData field;
field.is_focusable = true;
field.should_autocomplete = true;
const gfx::RectF element_bounds;
external_delegate_->OnQuery(query_id, form, field, element_bounds, true);
}
MockAutofillManagerDelegate manager_delegate_;
scoped_ptr<MockAutofillDriver> autofill_driver_;
scoped_ptr<MockAutofillManager> autofill_manager_;
scoped_ptr<AutofillExternalDelegate> external_delegate_;
base::MessageLoop message_loop_;
};
TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) {
IssueOnQuery(kQueryId);
EXPECT_CALL(
manager_delegate_,
ShowAutofillPopup(_,
_,
_,
_,
_,
testing::ElementsAre(
kAutofillProfileId,
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS)),
_));
std::vector<base::string16> autofill_item;
autofill_item.push_back(base::string16());
std::vector<int> autofill_ids;
autofill_ids.push_back(kAutofillProfileId);
external_delegate_->OnSuggestionsReturned(kQueryId,
autofill_item,
autofill_item,
autofill_item,
autofill_ids);
EXPECT_CALL(*autofill_manager_,
FillOrPreviewForm(
AutofillDriver::FORM_DATA_ACTION_FILL, _, _, _, _));
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
external_delegate_->DidAcceptSuggestion(autofill_item[0], autofill_ids[0]);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateDataList) {
IssueOnQuery(kQueryId);
std::vector<base::string16> data_list_items;
data_list_items.push_back(base::string16());
external_delegate_->SetCurrentDataListValues(data_list_items,
data_list_items);
EXPECT_CALL(
manager_delegate_,
ShowAutofillPopup(_,
_,
_,
_,
_,
testing::ElementsAre(
static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY),
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
kAutofillProfileId,
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS)),
_));
std::vector<base::string16> autofill_item;
autofill_item.push_back(base::string16());
std::vector<int> autofill_ids;
autofill_ids.push_back(kAutofillProfileId);
external_delegate_->OnSuggestionsReturned(kQueryId,
autofill_item,
autofill_item,
autofill_item,
autofill_ids);
EXPECT_CALL(
manager_delegate_,
ShowAutofillPopup(
_,
_,
_,
_,
_,
testing::ElementsAre(static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY)),
_));
autofill_item = std::vector<base::string16>();
autofill_ids = std::vector<int>();
external_delegate_->OnSuggestionsReturned(kQueryId,
autofill_item,
autofill_item,
autofill_item,
autofill_ids);
}
TEST_F(AutofillExternalDelegateUnitTest, UpdateDataListWhileShowingPopup) {
IssueOnQuery(kQueryId);
EXPECT_CALL(manager_delegate_,
ShowAutofillPopup(_, _, _, _, _, _, _)).Times(0);
std::vector<base::string16> data_list_items;
data_list_items.push_back(base::string16());
external_delegate_->SetCurrentDataListValues(data_list_items,
data_list_items);
EXPECT_CALL(
manager_delegate_,
ShowAutofillPopup(_,
_,
_,
_,
_,
testing::ElementsAre(
static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY),
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
kAutofillProfileId,
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS)),
_));
std::vector<base::string16> autofill_item;
autofill_item.push_back(base::string16());
std::vector<int> autofill_ids;
autofill_ids.push_back(kAutofillProfileId);
external_delegate_->OnSuggestionsReturned(kQueryId,
autofill_item,
autofill_item,
autofill_item,
autofill_ids);
external_delegate_->OnPopupShown();
data_list_items.push_back(base::string16());
EXPECT_CALL(manager_delegate_,
UpdateAutofillPopupDataListValues(data_list_items,
data_list_items));
external_delegate_->SetCurrentDataListValues(data_list_items,
data_list_items);
}
TEST_F(AutofillExternalDelegateUnitTest, AutofillWarnings) {
IssueOnQuery(kQueryId);
EXPECT_CALL(
manager_delegate_,
ShowAutofillPopup(
_,
_,
_,
_,
_,
testing::ElementsAre(static_cast<int>(POPUP_ITEM_ID_WARNING_MESSAGE)),
_));
std::vector<base::string16> autofill_item;
autofill_item.push_back(base::string16());
std::vector<int> autofill_ids;
autofill_ids.push_back(POPUP_ITEM_ID_WARNING_MESSAGE);
external_delegate_->OnSuggestionsReturned(kQueryId,
autofill_item,
autofill_item,
autofill_item,
autofill_ids);
}
TEST_F(AutofillExternalDelegateUnitTest, NoAutofillWarningsWithoutSuggestions) {
const FormData form;
FormFieldData field;
field.is_focusable = true;
field.should_autocomplete = false;
const gfx::RectF element_bounds;
external_delegate_->OnQuery(kQueryId, form, field, element_bounds, true);
EXPECT_CALL(manager_delegate_,
ShowAutofillPopup(_, _, _, _, _, _, _)).Times(0);
EXPECT_CALL(manager_delegate_, HideAutofillPopup()).Times(1);
std::vector<base::string16> autofill_item;
autofill_item.push_back(base::string16());
std::vector<int> autofill_ids;
autofill_ids.push_back(POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
external_delegate_->OnSuggestionsReturned(kQueryId,
autofill_item,
autofill_item,
autofill_item,
autofill_ids);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) {
EXPECT_CALL(*autofill_manager_, FillOrPreviewForm(_, _, _, _, _)).Times(0);
EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
external_delegate_->DidSelectSuggestion(base::string16(), -1);
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
EXPECT_CALL(*autofill_manager_, FillOrPreviewForm(_, _, _, _, _)).Times(0);
external_delegate_->DidAcceptSuggestion(base::string16(), -1);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) {
EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
external_delegate_->DidSelectSuggestion(ASCIIToUTF16("baz foo"),
POPUP_ITEM_ID_PASSWORD_ENTRY);
EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
EXPECT_CALL(*autofill_manager_,
FillOrPreviewForm(
AutofillDriver::FORM_DATA_ACTION_PREVIEW, _, _, _, _));
external_delegate_->DidSelectSuggestion(ASCIIToUTF16("baz foo"), 1);
EXPECT_CALL(*autofill_driver_, RendererShouldClearPreviewedForm()).Times(1);
EXPECT_CALL(*autofill_driver_, RendererShouldPreviewFieldWithValue(
ASCIIToUTF16("baz foo")));
external_delegate_->DidSelectSuggestion(ASCIIToUTF16("baz foo"),
POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
}
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateHidePopupAfterEditing) {
EXPECT_CALL(manager_delegate_, ShowAutofillPopup(_, _, _, _, _, _, _));
autofill::GenerateTestAutofillPopup(external_delegate_.get());
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
external_delegate_->DidEndTextFieldEditing();
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegatePasswordSuggestions) {
static const base::string16 kUsername = ASCIIToUTF16("username");
static const base::string16 kSignonRealm = ASCIIToUTF16("http://foo.com/");
std::vector<base::string16> suggestions;
suggestions.push_back(kUsername);
std::vector<base::string16> realms;
realms.push_back(kSignonRealm);
FormFieldData field;
field.is_focusable = true;
field.should_autocomplete = true;
const gfx::RectF element_bounds;
FormFieldData username_field_data;
username_field_data.value = kUsername;
PasswordFormFillData password_form_fill_data;
password_form_fill_data.basic_data.fields.push_back(username_field_data);
external_delegate_->AddPasswordFormMapping(field, password_form_fill_data);
EXPECT_CALL(
manager_delegate_,
ShowAutofillPopup(
_,
_,
_,
_,
_,
testing::ElementsAre(static_cast<int>(POPUP_ITEM_ID_PASSWORD_ENTRY)),
_));
external_delegate_->OnShowPasswordSuggestions(suggestions,
realms,
field,
element_bounds);
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
external_delegate_->DidAcceptSuggestion(suggestions[0],
POPUP_ITEM_ID_PASSWORD_ENTRY);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateAcceptSuggestion) {
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
base::string16 dummy_string(ASCIIToUTF16("baz qux"));
EXPECT_CALL(*autofill_driver_,
RendererShouldAcceptDataListSuggestion(dummy_string));
external_delegate_->DidAcceptSuggestion(dummy_string,
POPUP_ITEM_ID_DATALIST_ENTRY);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearForm) {
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
EXPECT_CALL(*autofill_driver_, RendererShouldClearFilledForm());
external_delegate_->DidAcceptSuggestion(base::string16(),
POPUP_ITEM_ID_CLEAR_FORM);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateHideWarning) {
const FormData form;
FormFieldData field;
field.is_focusable = true;
field.should_autocomplete = false;
const gfx::RectF element_bounds;
external_delegate_->OnQuery(kQueryId, form, field, element_bounds, false);
std::vector<base::string16> autofill_items;
autofill_items.push_back(base::string16());
std::vector<int> autofill_ids;
autofill_ids.push_back(POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
external_delegate_->OnSuggestionsReturned(kQueryId,
autofill_items,
autofill_items,
autofill_items,
autofill_ids);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateFillFieldWithValue) {
EXPECT_CALL(manager_delegate_, HideAutofillPopup());
base::string16 dummy_string(ASCIIToUTF16("baz foo"));
EXPECT_CALL(*autofill_driver_,
RendererShouldFillFieldWithValue(dummy_string));
external_delegate_->DidAcceptSuggestion(dummy_string,
POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
}
}