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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/keyboard_accessory/android/manual_filling_controller_impl.h"
#include <string>
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/autofill/manual_filling_view_interface.h"
#include "chrome/browser/autofill/mock_manual_filling_view.h"
#include "chrome/browser/keyboard_accessory/android/accessory_controller.h"
#include "chrome/browser/keyboard_accessory/android/accessory_sheet_data.h"
#include "chrome/browser/keyboard_accessory/android/accessory_sheet_enums.h"
#include "chrome/browser/keyboard_accessory/android/affiliated_plus_profiles_cache.h"
#include "chrome/browser/keyboard_accessory/test_utils/android/mock_address_accessory_controller.h"
#include "chrome/browser/keyboard_accessory/test_utils/android/mock_password_accessory_controller.h"
#include "chrome/browser/keyboard_accessory/test_utils/android/mock_payment_method_accessory_controller.h"
#include "chrome/browser/plus_addresses/plus_address_service_factory.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/autofill/content/browser/test_autofill_client_injector.h"
#include "components/autofill/content/browser/test_content_autofill_client.h"
#include "components/plus_addresses/fake_plus_address_service.h"
#include "components/plus_addresses/features.h"
#include "components/plus_addresses/plus_address_types.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using autofill::AccessoryAction;
using autofill::AccessorySheetData;
using autofill::AccessorySuggestionType;
using autofill::AccessoryTabType;
using autofill::TestAutofillClientInjector;
using autofill::TestContentAutofillClient;
using autofill::mojom::FocusedFieldType;
using plus_addresses::FakePlusAddressService;
using testing::_;
using testing::AnyNumber;
using testing::AtLeast;
using testing::Eq;
using testing::NiceMock;
using testing::Return;
using testing::SaveArg;
using FillingSource = ManualFillingController::FillingSource;
using IsFillingSourceAvailable = AccessoryController::IsFillingSourceAvailable;
using WaitForKeyboard = ManualFillingViewInterface::WaitForKeyboard;
AccessorySheetData filled_passwords_sheet() {
return AccessorySheetData::Builder(AccessoryTabType::PASSWORDS, u"Pwds",
/*plus_address_title=*/std::u16string())
.AddUserInfo("example.com", autofill::UserInfo::IsExactMatch(true))
.AppendField(AccessorySuggestionType::kCredentialUsername, u"Ben", u"Ben",
false, true)
.AppendField(AccessorySuggestionType::kCredentialPassword, u"S3cur3",
u"Ben's PW", true, false)
.Build();
}
AccessorySheetData populate_sheet(AccessoryTabType type) {
constexpr char16_t kTitle[] = u"Suggestions available!";
return AccessorySheetData::Builder(type, kTitle,
/*plus_address_title=*/std::u16string())
.AddUserInfo()
.Build();
}
std::vector<uint8_t> test_passkey_id() {
return {23, 24, 25, 26, 27};
}
std::unique_ptr<KeyedService> BuildFakePlusAddressService(
content::BrowserContext* context) {
return std::make_unique<FakePlusAddressService>();
}
constexpr autofill::FieldRendererId kFocusedFieldId(123);
} // namespace
// Fixture that tests the manual filling experience with the most recent version
// of the keyboard accessory and all its fallback sheets.
class ManualFillingControllerTest : public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
PlusAddressServiceFactory::GetInstance()->SetTestingFactory(
GetBrowserContext(), base::BindRepeating(&BuildFakePlusAddressService));
EXPECT_CALL(mock_pwd_controller_, RegisterFillingSourceObserver)
.WillOnce(SaveArg<0>(&pwd_source_observer_));
EXPECT_CALL(mock_pwd_controller_, RegisterPlusProfilesProvider);
EXPECT_CALL(mock_payment_method_controller_, RegisterFillingSourceObserver)
.WillOnce(SaveArg<0>(&cc_source_observer_));
EXPECT_CALL(mock_address_controller_, RegisterFillingSourceObserver)
.WillOnce(SaveArg<0>(&address_source_observer_));
EXPECT_CALL(mock_address_controller_, RegisterPlusProfilesProvider);
ManualFillingControllerImpl::CreateForWebContentsForTesting(
web_contents(), mock_pwd_controller_.AsWeakPtr(),
mock_address_controller_.AsWeakPtr(),
mock_payment_method_controller_.AsWeakPtr(),
std::make_unique<NiceMock<MockManualFillingView>>());
}
void FocusFieldAndClearExpectations(FocusedFieldType fieldType) {
// Depending on |fieldType|, different calls can be expected. All of them
// are irrelevant during setup.
controller()->NotifyFocusedInputChanged(kFocusedFieldId, fieldType);
testing::Mock::VerifyAndClearExpectations(view());
}
ManualFillingControllerImpl* controller() {
return ManualFillingControllerImpl::FromWebContents(web_contents());
}
MockManualFillingView* view() {
return static_cast<MockManualFillingView*>(controller()->view());
}
void NotifyPasswordSourceObserver(IsFillingSourceAvailable source_available) {
pwd_source_observer_.Run(&mock_pwd_controller_, source_available);
}
void NotifyCreditCardSourceObserver(
IsFillingSourceAvailable source_available) {
cc_source_observer_.Run(&mock_payment_method_controller_, source_available);
}
void NotifyAddressSourceObserver(IsFillingSourceAvailable source_available) {
address_source_observer_.Run(&mock_address_controller_, source_available);
}
protected:
base::test::ScopedFeatureList features_{
plus_addresses::features::kPlusAddressesEnabled};
NiceMock<MockPasswordAccessoryController> mock_pwd_controller_;
NiceMock<MockAddressAccessoryController> mock_address_controller_;
NiceMock<MockPaymentMethodAccessoryController>
mock_payment_method_controller_;
AccessoryController::FillingSourceObserver pwd_source_observer_;
AccessoryController::FillingSourceObserver cc_source_observer_;
AccessoryController::FillingSourceObserver address_source_observer_;
TestAutofillClientInjector<TestContentAutofillClient>
autofill_client_injector_;
};
TEST_F(ManualFillingControllerTest, ShowsAccessoryForAutofillOnSearchField) {
FocusFieldAndClearExpectations(FocusedFieldType::kFillableSearchField);
EXPECT_CALL(*view(), Show(WaitForKeyboard(true)));
controller()->UpdateSourceAvailability(FillingSource::PASSWORD_FALLBACKS,
/*has_suggestions=*/true);
controller()->UpdateSourceAvailability(FillingSource::AUTOFILL,
/*has_suggestions=*/true);
testing::Mock::VerifyAndClearExpectations(view());
// Hiding autofill hides the accessory because fallbacks alone don't provide
// sufficient value and might be confusing.
EXPECT_CALL(*view(), Hide());
controller()->UpdateSourceAvailability(FillingSource::AUTOFILL,
/*has_suggestions=*/false);
}
TEST_F(ManualFillingControllerTest, PasswordAccessoryControllerReturnsNoData) {
EXPECT_CALL(mock_pwd_controller_, GetSheetData)
.Times(AtLeast(1))
.WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(*view(), OnItemsAvailable).Times(0);
EXPECT_CALL(*view(), Hide());
NotifyPasswordSourceObserver(IsFillingSourceAvailable(true));
FocusFieldAndClearExpectations(FocusedFieldType::kFillableUsernameField);
}
TEST_F(ManualFillingControllerTest,
ShowsAccessoryForPasswordsTriggeredByObserver) {
// TODO(crbug.com/40165275): Because the data isn't cached, test that only one
// call to `GetSheetData()` happens.
EXPECT_CALL(mock_pwd_controller_, GetSheetData)
.Times(AtLeast(1))
.WillRepeatedly(Return(filled_passwords_sheet()));
EXPECT_CALL(*view(), OnItemsAvailable(filled_passwords_sheet()))
.Times(AnyNumber());
EXPECT_CALL(*view(), Show(WaitForKeyboard(true)));
NotifyPasswordSourceObserver(IsFillingSourceAvailable(true));
FocusFieldAndClearExpectations(FocusedFieldType::kFillableUsernameField);
EXPECT_CALL(*view(), Hide());
NotifyPasswordSourceObserver(IsFillingSourceAvailable(false));
}
TEST_F(ManualFillingControllerTest, AddressAccessoryControllerReturnsNoData) {
EXPECT_CALL(mock_address_controller_, GetSheetData)
.Times(AtLeast(1))
.WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(*view(), OnItemsAvailable).Times(0);
EXPECT_CALL(*view(), Hide());
NotifyAddressSourceObserver(IsFillingSourceAvailable(true));
FocusFieldAndClearExpectations(FocusedFieldType::kFillableNonSearchField);
}
TEST_F(ManualFillingControllerTest,
ShowsAccessoryForAddressesTriggeredByObserver) {
const AccessorySheetData kTestAddressSheet =
populate_sheet(AccessoryTabType::ADDRESSES);
// TODO(crbug.com/40165275): Because the data isn't cached, test that only one
// call to `GetSheetData()` happens.
EXPECT_CALL(mock_address_controller_, GetSheetData)
.Times(AtLeast(1))
.WillRepeatedly(Return(kTestAddressSheet));
EXPECT_CALL(*view(), OnItemsAvailable(kTestAddressSheet)).Times(AnyNumber());
EXPECT_CALL(*view(), Show(WaitForKeyboard(true)));
NotifyAddressSourceObserver(IsFillingSourceAvailable(true));
FocusFieldAndClearExpectations(FocusedFieldType::kFillableNonSearchField);
EXPECT_CALL(*view(), Hide());
NotifyAddressSourceObserver(IsFillingSourceAvailable(false));
}
TEST_F(ManualFillingControllerTest,
CreditCardAccessoryControllerReturnsNoData) {
EXPECT_CALL(mock_payment_method_controller_, GetSheetData)
.Times(AtLeast(1))
.WillRepeatedly(Return(std::nullopt));
EXPECT_CALL(*view(), OnItemsAvailable).Times(0);
EXPECT_CALL(*view(), Hide());
NotifyCreditCardSourceObserver(IsFillingSourceAvailable(true));
FocusFieldAndClearExpectations(FocusedFieldType::kFillableNonSearchField);
}
TEST_F(ManualFillingControllerTest,
ShowsAccessoryForCreditCardsTriggeredByObserver) {
const AccessorySheetData kTestCreditCardSheet =
populate_sheet(AccessoryTabType::CREDIT_CARDS);
// TODO(crbug.com/40165275): Because the data isn't cached, test that only one
// call to `GetSheetData()` happens.
EXPECT_CALL(mock_payment_method_controller_, GetSheetData)
.Times(AtLeast(1))
.WillRepeatedly(Return(kTestCreditCardSheet));
EXPECT_CALL(*view(), OnItemsAvailable(kTestCreditCardSheet))
.Times(AnyNumber());
EXPECT_CALL(*view(), Show(WaitForKeyboard(true)));
NotifyCreditCardSourceObserver(IsFillingSourceAvailable(true));
FocusFieldAndClearExpectations(FocusedFieldType::kFillableNonSearchField);
EXPECT_CALL(*view(), Hide());
NotifyCreditCardSourceObserver(IsFillingSourceAvailable(false));
}
TEST_F(ManualFillingControllerTest, HidesAccessoryWithoutAvailableSources) {
FocusFieldAndClearExpectations(FocusedFieldType::kFillableNonSearchField);
EXPECT_CALL(*view(), Show(WaitForKeyboard(true))).Times(2);
controller()->UpdateSourceAvailability(FillingSource::PASSWORD_FALLBACKS,
/*has_suggestions=*/true);
controller()->UpdateSourceAvailability(FillingSource::AUTOFILL,
/*has_suggestions=*/true);
// This duplicate call is a noop.
controller()->UpdateSourceAvailability(FillingSource::PASSWORD_FALLBACKS,
/*has_suggestions=*/true);
testing::Mock::VerifyAndClearExpectations(view());
// Hiding just one of two active filling sources won't have any effect at all.
EXPECT_CALL(*view(), Hide()).Times(0);
EXPECT_CALL(*view(), Show(WaitForKeyboard(true))).Times(0);
controller()->UpdateSourceAvailability(FillingSource::PASSWORD_FALLBACKS,
/*has_suggestions=*/false);
testing::Mock::VerifyAndClearExpectations(view());
EXPECT_CALL(*view(), Hide());
controller()->UpdateSourceAvailability(FillingSource::AUTOFILL,
/*has_suggestions=*/false);
}
TEST_F(ManualFillingControllerTest, FetchesAffiliatedPlusProfilesWhenShown) {
FocusFieldAndClearExpectations(FocusedFieldType::kFillableNonSearchField);
// Not plus profiles should be fetched before the first call to `Show()`.
EXPECT_TRUE(controller()->plus_profiles_cache());
EXPECT_EQ(
controller()->plus_profiles_cache()->GetAffiliatedPlusProfiles().size(),
0u);
EXPECT_CALL(*view(), Show(WaitForKeyboard(true)));
controller()->UpdateSourceAvailability(FillingSource::ADDRESS_FALLBACKS,
/*has_suggestions=*/true);
EXPECT_EQ(
controller()->plus_profiles_cache()->GetAffiliatedPlusProfiles().size(),
1u);
EXPECT_CALL(*view(), Hide());
controller()->UpdateSourceAvailability(FillingSource::ADDRESS_FALLBACKS,
/*has_suggestions=*/false);
EXPECT_EQ(
controller()->plus_profiles_cache()->GetAffiliatedPlusProfiles().size(),
0u);
}
TEST_F(ManualFillingControllerTest, ForwardsCredManActionToPasswordController) {
EXPECT_CALL(
mock_pwd_controller_,
OnOptionSelected(AccessoryAction::CREDMAN_CONDITIONAL_UI_REENTRY));
controller()->OnOptionSelected(
AccessoryAction::CREDMAN_CONDITIONAL_UI_REENTRY);
}
TEST_F(ManualFillingControllerTest,
ForwardsPasskeySelectionToPasswordController) {
EXPECT_CALL(mock_pwd_controller_, OnPasskeySelected(Eq(test_passkey_id())));
EXPECT_CALL(*view(), Hide()); // Make room for passkey sheet!
controller()->OnPasskeySelected(AccessoryTabType::PASSWORDS,
test_passkey_id());
}
TEST_F(ManualFillingControllerTest,
ShowsAccessoryWhenAutofillSourceAvailableOnUnknownField) {
FocusFieldAndClearExpectations(FocusedFieldType::kUnknown);
EXPECT_CALL(*view(), Show(WaitForKeyboard(false)));
controller()->UpdateSourceAvailability(FillingSource::AUTOFILL,
/*has_suggestions=*/true);
// Noop duplicate call.
controller()->UpdateSourceAvailability(FillingSource::AUTOFILL,
/*has_suggestions=*/true);
testing::Mock::VerifyAndClearExpectations(view());
EXPECT_CALL(*view(), Hide());
controller()->UpdateSourceAvailability(FillingSource::AUTOFILL,
/*has_suggestions=*/false);
}
|