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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cstddef>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/types/cxx23_to_underlying.h"
#include "build/build_config.h"
#include "chrome/browser/autofill/mock_autofill_popup_controller.h"
#include "chrome/browser/ui/autofill/autofill_keyboard_accessory_adapter.h"
#include "chrome/browser/ui/autofill/autofill_popup_view.h"
#include "components/autofill/core/browser/ui/popup_item_ids.h"
#include "components/autofill/core/browser/ui/suggestion.h"
#include "components/autofill/core/common/aliases.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect_f.h"
using base::ASCIIToUTF16;
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::StrictMock;
using ::testing::WithArg;
namespace autofill {
namespace {
class MockAccessoryView
: public AutofillKeyboardAccessoryAdapter::AccessoryView {
public:
MockAccessoryView() {}
MockAccessoryView(const MockAccessoryView&) = delete;
MockAccessoryView& operator=(const MockAccessoryView&) = delete;
MOCK_METHOD(bool, Initialize, ());
MOCK_METHOD(void, Hide, ());
MOCK_METHOD(void, Show, ());
MOCK_METHOD(void, AxAnnounce, (const std::u16string&));
MOCK_METHOD(void,
ConfirmDeletion,
(const std::u16string&,
const std::u16string&,
base::OnceClosure));
};
Suggestion createPasswordEntry(std::string password,
std::string username,
std::string psl_origin) {
Suggestion s(/*main_text=*/username, /*label=*/psl_origin, /*icon=*/"",
PopupItemId::kAutocompleteEntry);
s.additional_label = ASCIIToUTF16(password);
return s;
}
std::vector<Suggestion> createSuggestions() {
std::vector<Suggestion> suggestions = {
createPasswordEntry("****************", "Alf", ""),
createPasswordEntry("****************", "Berta", "psl.origin.eg"),
createPasswordEntry("***", "Carl", "")};
return suggestions;
}
std::vector<Suggestion> createSuggestions(int clearItemOffset) {
std::vector<Suggestion> suggestions = createSuggestions();
suggestions.emplace(suggestions.begin() + clearItemOffset, "Clear", "", "",
PopupItemId::kClearForm);
return suggestions;
}
// Convert the Suggestion::labels into one string of format "[[a, b],[c]]".
std::string SuggestionLabelsToString(
const std::vector<std::vector<Suggestion::Text>>& labels) {
std::string result;
for (const std::vector<Suggestion::Text>& texts : labels) {
if (!result.empty())
result.append(",");
std::string row;
for (const Suggestion::Text& text : texts) {
if (!row.empty())
row.append(",");
row.append(base::UTF16ToUTF8(text.value));
}
result.append("[" + row + "]");
}
return "[" + result + "]";
}
// Matcher returning true if suggestions have equal members.
MATCHER_P(equalsSuggestion, other, "") {
if (arg.popup_item_id != other.popup_item_id) {
*result_listener << "has popup_item_id "
<< base::to_underlying(arg.popup_item_id);
return false;
}
if (arg.main_text != other.main_text) {
*result_listener << "has main_text " << arg.main_text.value;
return false;
}
if (arg.labels != other.labels) {
*result_listener << "has labels " << SuggestionLabelsToString(arg.labels);
return false;
}
if (arg.icon != other.icon) {
*result_listener << "has icon " << arg.icon;
return false;
}
return true;
}
} // namespace
class AutofillKeyboardAccessoryAdapterTest : public testing::Test {
public:
AutofillKeyboardAccessoryAdapterTest()
: popup_controller_(
std::make_unique<StrictMock<MockAutofillPopupController>>()) {
auto view = std::make_unique<StrictMock<MockAccessoryView>>();
accessory_view_ = view.get();
autofill_accessory_adapter_ =
std::make_unique<AutofillKeyboardAccessoryAdapter>(
popup_controller_->GetWeakPtr());
autofill_accessory_adapter_->SetAccessoryView(std::move(view));
}
void NotifyAboutSuggestions() {
EXPECT_CALL(*view(), Show());
adapter_as_view()->OnSuggestionsChanged();
testing::Mock::VerifyAndClearExpectations(view());
}
const Suggestion& suggestion(int i) {
return controller()->GetSuggestionAt(i);
}
AutofillPopupController* adapter_as_controller() {
return autofill_accessory_adapter_.get();
}
AutofillPopupView* adapter_as_view() {
return autofill_accessory_adapter_.get();
}
MockAutofillPopupController* controller() { return popup_controller_.get(); }
MockAccessoryView* view() { return accessory_view_; }
private:
raw_ptr<StrictMock<MockAccessoryView>> accessory_view_;
std::unique_ptr<StrictMock<MockAutofillPopupController>> popup_controller_;
std::unique_ptr<AutofillKeyboardAccessoryAdapter> autofill_accessory_adapter_;
};
TEST_F(AutofillKeyboardAccessoryAdapterTest, ShowingInitializesAndUpdatesView) {
EXPECT_CALL(*view(), Show());
adapter_as_view()->Show(AutoselectFirstSuggestion(false));
}
TEST_F(AutofillKeyboardAccessoryAdapterTest, HidingAdapterHidesView) {
EXPECT_CALL(*view(), Hide());
adapter_as_view()->Hide();
}
TEST_F(AutofillKeyboardAccessoryAdapterTest, ReorderUpdatedSuggestions) {
controller()->set_suggestions(createSuggestions(/*clearItemOffset=*/2));
EXPECT_CALL(*view(), Show());
adapter_as_view()->OnSuggestionsChanged();
EXPECT_THAT(adapter_as_controller()->GetSuggestionAt(0),
equalsSuggestion(suggestion(2)));
EXPECT_THAT(adapter_as_controller()->GetSuggestionAt(1),
equalsSuggestion(suggestion(0)));
EXPECT_THAT(adapter_as_controller()->GetSuggestionAt(2),
equalsSuggestion(suggestion(1)));
EXPECT_THAT(adapter_as_controller()->GetSuggestionAt(3),
equalsSuggestion(suggestion(3)));
}
TEST_F(AutofillKeyboardAccessoryAdapterTest, UseAdditionalLabelForElidedLabel) {
controller()->set_suggestions(createSuggestions(/*clearItemOffset=*/1));
NotifyAboutSuggestions();
// The 1st item is usually not visible (something like clear form) and has an
// empty label. But it needs to be handled since UI might ask for it anyway.
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(0).size(), 1U);
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(0)[0].size(), 1U);
EXPECT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(0)[0][0].value,
std::u16string());
// If there is a label, use it but cap at 8 bullets.
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(1).size(), 1U);
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(1)[0].size(), 1U);
EXPECT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(1)[0][0].value,
u"********");
// If the label is empty, use the additional label:
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(2).size(), 1U);
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(2)[0].size(), 1U);
EXPECT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(2)[0][0].value,
u"psl.origin.eg ********");
// If the password has less than 8 bullets, show the exact amount.
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(3).size(), 1U);
ASSERT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(3)[0].size(), 1U);
EXPECT_EQ(adapter_as_controller()->GetSuggestionLabelsAt(3)[0][0].value,
u"***");
}
TEST_F(AutofillKeyboardAccessoryAdapterTest, ProvideReorderedSuggestions) {
controller()->set_suggestions(createSuggestions(/*clearItemOffset=*/2));
NotifyAboutSuggestions();
EXPECT_THAT(adapter_as_controller()->GetSuggestions(),
testing::ElementsAre(equalsSuggestion(suggestion(2)),
equalsSuggestion(suggestion(0)),
equalsSuggestion(suggestion(1)),
equalsSuggestion(suggestion(3))));
}
TEST_F(AutofillKeyboardAccessoryAdapterTest, RemoveAfterConfirmation) {
controller()->set_suggestions(createSuggestions());
NotifyAboutSuggestions();
base::OnceClosure confirm;
EXPECT_CALL(*controller(), GetRemovalConfirmationText(0, _, _))
.WillOnce(Return(true));
EXPECT_CALL(*view(), ConfirmDeletion(_, _, _))
.WillOnce(WithArg<2>(Invoke([&](base::OnceClosure closure) -> void {
confirm = std::move(closure);
})));
EXPECT_TRUE(adapter_as_controller()->RemoveSuggestion(0));
EXPECT_CALL(*controller(), RemoveSuggestion(0)).WillOnce(Return(true));
std::move(confirm).Run();
}
} // namespace autofill
|