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
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/browsing_data/core/browsing_data_utils.h"
#include <string>
#include <vector>
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/autofill/core/browser/data_manager/test_personal_data_manager.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/browsing_data/core/counters/autofill_counter.h"
#include "components/browsing_data/core/counters/history_counter.h"
#include "components/browsing_data/core/counters/passwords_counter.h"
#include "components/browsing_data/core/features.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/password_manager/core/browser/password_store/test_password_store.h"
#include "components/prefs/pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/webdata/common/web_database_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace browsing_data {
namespace {
class FakeWebDataService : public autofill::AutofillWebDataService {
public:
FakeWebDataService()
: AutofillWebDataService(
base::MakeRefCounted<WebDatabaseService>(
base::FilePath(),
base::SingleThreadTaskRunner::GetCurrentDefault(),
base::SingleThreadTaskRunner::GetCurrentDefault()),
base::SingleThreadTaskRunner::GetCurrentDefault()) {}
protected:
~FakeWebDataService() override = default;
};
} // namespace
class BrowsingDataUtilsTest : public testing::Test {
public:
~BrowsingDataUtilsTest() override = default;
void SetUp() override {
browsing_data::prefs::RegisterBrowserUserPrefs(prefs_.registry());
}
PrefService* prefs() { return &prefs_; }
private:
base::test::SingleThreadTaskEnvironment task_environment_;
sync_preferences::TestingPrefServiceSyncable prefs_;
};
// Tests the complex output of the Autofill counter.
TEST_F(BrowsingDataUtilsTest, AutofillCounterResult) {
autofill::TestPersonalDataManager test_personal_data_manager;
AutofillCounter counter(&test_personal_data_manager,
base::MakeRefCounted<FakeWebDataService>(),
/*entity_data_manager=*/nullptr, nullptr);
// Test all configurations of zero and nonzero partial results for datatypes.
// Test singular and plural for each datatype.
const struct TestCase {
int num_credit_cards;
int num_addresses;
int num_suggestions;
int num_entities;
bool sync_enabled;
std::string expected_output;
} kTestCases[] = {
{0, 0, 0, 0, false, "None"},
{0, 0, 0, 0, true, "None"},
{1, 0, 0, 0, false, "1 payment method"},
{0, 5, 0, 0, false, "5 addresses"},
{0, 0, 1, 0, false, "1 suggestion"},
{0, 0, 2, 0, false, "2 suggestions"},
{0, 0, 2, 1, false, "3 suggestions"},
{0, 0, 0, 2, false, "2 suggestions"},
{0, 0, 2, 0, true, "2 suggestions (synced)"},
{4, 7, 0, 0, false, "4 payment methods, 7 addresses"},
{4, 7, 0, 0, true, "4 payment methods, 7 addresses (synced)"},
{3, 0, 9, 0, false, "3 payment methods, 9 other suggestions"},
{0, 1, 1, 0, false, "1 address, 1 other suggestion"},
{9, 6, 3, 0, false, "9 payment methods, 6 addresses, 3 others"},
{9, 6, 3, 5, false, "9 payment methods, 6 addresses, 8 others"},
{4, 2, 1, 0, false, "4 payment methods, 2 addresses, 1 other"},
{4, 2, 1, 0, true, "4 payment methods, 2 addresses, 1 other (synced)"},
};
for (const TestCase& test_case : kTestCases) {
AutofillCounter::AutofillResult result(
&counter, test_case.num_suggestions, test_case.num_credit_cards,
test_case.num_addresses, test_case.num_entities,
test_case.sync_enabled);
SCOPED_TRACE(
base::StringPrintf("Test params: %d payment method(s), "
"%d address(es), %d suggestion(s), %d entities",
test_case.num_credit_cards, test_case.num_addresses,
test_case.num_suggestions, test_case.num_entities));
std::u16string output = browsing_data::GetCounterTextFromResult(&result);
EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output));
}
}
// Tests the output of the Passwords counter.
TEST_F(BrowsingDataUtilsTest, PasswordsCounterResult) {
auto store = base::MakeRefCounted<password_manager::TestPasswordStore>();
store->Init(prefs(), /*affiliated_match_helper=*/nullptr);
PasswordsCounter counter(
/*profile_store=*/scoped_refptr<password_manager::PasswordStoreInterface>(
store),
/*account_store=*/nullptr,
/*pref_service=*/nullptr,
/*sync_service=*/nullptr);
// Use a separate struct for input to make test cases easier to read after
// auto formatting.
struct TestInput {
int num_passwords;
int num_account_passwords;
int is_synced;
std::vector<std::string> domain_examples;
std::vector<std::string> account_domain_examples;
};
const struct TestCase {
TestInput input;
std::string expected_output;
} kTestCases[] = {
{{0, 0, false, {}, {}}, "None"},
{{0, 0, true, {}, {}}, "None"},
{{1, 0, false, {"a.com"}, {}}, "1 password (for a.com)"},
{{1, 0, true, {"a.com"}, {}}, "1 password (for a.com, synced)"},
{{5, 0, false, {"a.com", "b.com", "c.com", "d.com"}, {}},
"5 passwords (for a.com, b.com, and 3 more)"},
{{2, 0, false, {"a.com", "b.com"}, {}}, "2 passwords (for a.com, b.com)"},
{{5, 0, true, {"a.com", "b.com", "c.com", "d.com", "e.com"}, {}},
"5 passwords (for a.com, b.com, and 3 more, synced)"},
{{0, 1, false, {}, {"a.com"}}, "1 password in your account (for a.com)"},
{{0, 2, false, {}, {"a.com", "b.com"}},
"2 passwords in your account (for a.com, b.com)"},
{{0, 3, false, {}, {"a.com", "b.com", "c.com"}},
"3 passwords in your account (for a.com, b.com, and 1 more)"},
{{2, 1, false, {"a.com", "b.com"}, {"c.com"}},
"2 passwords (for a.com, b.com); 1 password in your account (for "
"c.com)"},
};
for (const TestCase& test_case : kTestCases) {
auto& input = test_case.input;
PasswordsCounter::PasswordsResult result(
&counter, input.num_passwords, input.num_account_passwords,
input.is_synced, input.domain_examples, input.account_domain_examples);
SCOPED_TRACE(base::StringPrintf(
"Test params: %d password(s), %d account password(s), %d is_synced",
input.num_passwords, input.num_account_passwords, input.is_synced));
std::u16string output = browsing_data::GetCounterTextFromResult(&result);
EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output));
}
store->ShutdownOnUIThread();
}
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
// Tests the output of the History counter.
TEST_F(BrowsingDataUtilsTest, HistoryCounterResult) {
history::HistoryService history_service;
HistoryCounter counter(&history_service,
HistoryCounter::GetUpdatedWebHistoryServiceCallback(),
nullptr);
counter.Init(prefs(), ClearBrowsingDataTab::ADVANCED, base::DoNothing());
const struct TestCase {
int num_history;
int is_sync_enabled;
int has_sync_visits;
std::string last_visited_domain;
int unique_domains;
std::string expected_output;
} kTestCases[] = {
// No sync, no synced visits:
{0, false, false, "", 0, "None"},
{0, false, false, "google.com", 1, "From google.com"},
{1, false, false, "google.com", 1, "From google.com"},
{5, false, false, "google.com", 5, "From google.com + 4 sites"},
// Sync but not synced visits:
{0, true, false, "", 0, "None"},
{1, true, false, "google.com", 1, "From google.com"},
{0, true, false, "google.com", 1, "From google.com"},
{5, true, false, "google.com", 5, "From google.com + 4 sites"},
// Sync and synced visits:
{0, true, true, "", 0, "From at least 1 site on synced devices"},
{1, true, true, "google.com", 1,
"From google.com (more on synced devices)"},
{0, true, true, "google.com", 1,
"From google.com (more on synced devices)"},
{5, true, true, "google.com", 5,
"From google.com + 4 sites (more on synced devices)"},
};
for (const TestCase& test_case : kTestCases) {
HistoryCounter::HistoryResult result(
&counter, test_case.num_history, test_case.is_sync_enabled,
test_case.has_sync_visits, test_case.last_visited_domain,
test_case.unique_domains);
SCOPED_TRACE(
base::StringPrintf("Test params: %d history, %d has_synced_visits, "
"last visted domain is %s",
test_case.num_history, test_case.has_sync_visits,
test_case.last_visited_domain));
std::u16string output = browsing_data::GetCounterTextFromResult(&result);
EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output));
}
}
#else // !(BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS))
// Tests the output of the History counter.
// TODO(crbug.com/397187800): Clean up once kDbdRevampDesktop is launched.
TEST_F(BrowsingDataUtilsTest, HistoryCounterResult_RevampDisabled) {
base::test::ScopedFeatureList feature_list;
feature_list.InitAndDisableFeature(features::kDbdRevampDesktop);
history::HistoryService history_service;
HistoryCounter counter(&history_service,
HistoryCounter::GetUpdatedWebHistoryServiceCallback(),
nullptr);
counter.Init(prefs(), ClearBrowsingDataTab::ADVANCED, base::DoNothing());
const struct TestCase {
int num_history;
int is_sync_enabled;
int has_sync_visits;
std::string expected_output;
} kTestCases[] = {
// No sync, no synced visits:
{0, false, false, "None"},
{1, false, false, "1 item"},
{5, false, false, "5 items"},
// Sync but not synced visits:
{0, true, false, "None"},
{1, true, false, "1 item"},
{5, true, false, "5 items"},
// Sync and synced visits:
{0, true, true, "At least 1 item on synced devices"},
{1, true, true, "1 item (and more on synced devices)"},
{5, true, true, "5 items (and more on synced devices)"},
};
for (const TestCase& test_case : kTestCases) {
HistoryCounter::HistoryResult result(&counter, test_case.num_history,
test_case.is_sync_enabled,
test_case.has_sync_visits, "", 0);
SCOPED_TRACE(
base::StringPrintf("Test params: %d history, %d has_synced_visits",
test_case.num_history, test_case.has_sync_visits));
std::u16string output = browsing_data::GetCounterTextFromResult(&result);
EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output));
}
}
// Tests the output of the History counter with kDbdRevampDesktop, the history
// counter should count the unique domains.
TEST_F(BrowsingDataUtilsTest, HistoryCounterResult_RevampEnabled) {
base::test::ScopedFeatureList feature(features::kDbdRevampDesktop);
history::HistoryService history_service;
HistoryCounter counter(&history_service,
HistoryCounter::GetUpdatedWebHistoryServiceCallback(),
nullptr);
counter.Init(prefs(), ClearBrowsingDataTab::ADVANCED, base::DoNothing());
const struct TestCase {
int num_history;
int is_sync_enabled;
int has_sync_visits;
std::string last_visited_domain;
int unique_domains;
std::string expected_output;
} kTestCases[] = {
// No sync, no synced visits:
{0, false, false, "", 0, "None"},
{0, false, false, "google.com", 1, "From google.com"},
{1, false, false, "google.com", 1, "From google.com"},
{5, false, false, "google.com", 5, "From google.com + 4 sites"},
// Sync but not synced visits:
{0, true, false, "", 0, "None"},
{1, true, false, "google.com", 1, "From google.com"},
{0, true, false, "google.com", 1, "From google.com"},
{5, true, false, "google.com", 5, "From google.com + 4 sites"},
// Sync and synced visits:
{0, true, true, "", 0, "From at least 1 site on synced devices"},
{1, true, true, "google.com", 1,
"From google.com (more on synced devices)"},
{0, true, true, "google.com", 1,
"From google.com (more on synced devices)"},
{5, true, true, "google.com", 5,
"From google.com + 4 sites (more on synced devices)"},
};
for (const TestCase& test_case : kTestCases) {
HistoryCounter::HistoryResult result(
&counter, test_case.num_history, test_case.is_sync_enabled,
test_case.has_sync_visits, test_case.last_visited_domain,
test_case.unique_domains);
SCOPED_TRACE(
base::StringPrintf("Test params: %d history, %d has_synced_visits, "
"last visted domain is %s",
test_case.num_history, test_case.has_sync_visits,
test_case.last_visited_domain));
std::u16string output = browsing_data::GetCounterTextFromResult(&result);
EXPECT_EQ(output, base::ASCIIToUTF16(test_case.expected_output));
}
}
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
} // namespace browsing_data
|