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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
// Copyright 2025 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/ntp_tiles/enterprise/ntp_shortcuts_policy_handler.h"
#include <optional>
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/ntp_tiles/enterprise/enterprise_shortcuts_store.h"
#include "components/ntp_tiles/features.h"
#include "components/ntp_tiles/pref_names.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/field_validation_test_utils.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/strings/grit/components_strings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
using ntp_tiles::EnterpriseShortcut;
using ntp_tiles::EnterpriseShortcutsStore;
using testing::AllOf;
using testing::ElementsAre;
namespace policy {
namespace {
// Represents field values for NTPShortcuts policy, used for generating
// policy value entries.
struct TestShortcut {
std::optional<std::string> name;
std::optional<std::string> url;
std::optional<bool> allow_user_edit;
std::optional<bool> allow_user_delete;
};
// Used for tests that require a list of valid shortcuts.
TestShortcut kValidTestShortcuts[] = {
{.name = "work name",
.url = "https://work.com/",
.allow_user_edit = true,
.allow_user_delete = true},
{.name = "docs name",
.url = "https://docs.com/",
.allow_user_edit = false,
.allow_user_delete = false},
{.name = "mail name", .url = "https://mail.com/"},
};
// Used for tests that require shortcuts with missing required fields.
TestShortcut kMissingRequiredFieldsTestShortcuts[] = {
{.url = "https://missing_name.com/"},
{.name = "missing_url name"},
};
// Used for tests that require shortcuts with empty required fields.
TestShortcut kEmptyFieldTestShortcuts[] = {
{.name = "", .url = "https://empty_name.com/"},
{.name = "empty_url name", .url = ""},
};
// Used for tests that require a shortcut with unknown field.
TestShortcut kUnknownFieldTestShortcuts[] = {
{.name = "work name", .url = "https://work.com/"},
};
// Used for tests that require a list of shortcuts with a duplicated url,
// but at least one valid entry.
TestShortcut kUrlNotUniqueTestShortcuts[] = {
{.name = "work name", .url = "https://work.com"},
{.name = "also work name", .url = "https://work.com/"},
{.name = "docs name", .url = "https://docs.com/"},
{.name = "another work name", .url = "https://work.com/"},
};
// Used for tests that require a list of shortcuts with a duplicated url
// and no valid entry.
TestShortcut kNoUniqueUrlTestShortcuts[] = {
{.name = "work name", .url = "https://work.com/"},
{.name = "also work name", .url = "https://work.com/"},
{.name = "another work name", .url = "https://work.com"},
};
// Used for tests that require a shortcut with invalid non-empty URLs.
TestShortcut kInvalidUrlTestShortcuts[] = {
{.name = "invalid1 name", .url = "work"},
{.name = "invalid2 name", .url = "www.notvalidurl"},
{.name = "invalid3 name", .url = "invalid/url"},
};
base::Value::Dict GenerateNTPShortcutPolicyEntry(TestShortcut test_case) {
base::Value::Dict entry;
if (test_case.name.has_value()) {
entry.Set(NTPShortcutsPolicyHandler::kName, test_case.name.value());
}
if (test_case.url.has_value()) {
entry.Set(NTPShortcutsPolicyHandler::kUrl, test_case.url.value());
}
if (test_case.allow_user_edit.has_value()) {
entry.Set(NTPShortcutsPolicyHandler::kAllowUserEdit,
test_case.allow_user_edit.value());
}
if (test_case.allow_user_delete.has_value()) {
entry.Set(NTPShortcutsPolicyHandler::kAllowUserDelete,
test_case.allow_user_delete.value());
}
return entry;
}
// Returns a matcher that accepts entries for the pref corresponding to the
// NTP shortcuts policy. Field values are obtained from |test_case|.
testing::Matcher<const base::Value&> IsNTPShortcutEntry(
TestShortcut test_case) {
return AllOf(
HasStringField(EnterpriseShortcutsStore::kDictionaryKeyTitle,
test_case.name.value()),
HasStringField(EnterpriseShortcutsStore::kDictionaryKeyUrl,
test_case.url.value()),
HasIntegerField(
EnterpriseShortcutsStore::kDictionaryKeyPolicyOrigin,
static_cast<int>(EnterpriseShortcut::PolicyOrigin::kNtpShortcuts)),
HasBooleanField(EnterpriseShortcutsStore::kDictionaryKeyIsHiddenByUser,
false),
HasBooleanField(EnterpriseShortcutsStore::kDictionaryKeyAllowUserEdit,
test_case.allow_user_edit.value_or(false)),
HasBooleanField(EnterpriseShortcutsStore::kDictionaryKeyAllowUserDelete,
test_case.allow_user_delete.value_or(false)));
}
MATCHER_P(HasValidationError,
expected_str,
base::StringPrintf("%s error message `%s` for `NTPShortcuts`",
negation ? "does not contain" : "contains",
base::UTF16ToUTF8(expected_str).c_str())) {
return arg->HasError(key::kNTPShortcuts) &&
arg->GetErrorMessages(key::kNTPShortcuts).find(expected_str) !=
std::wstring::npos;
}
MATCHER_P(HasValidationWarning,
expected_str,
base::StringPrintf("%s warning message `%s` for `NTPShortcuts`",
negation ? "does not contain" : "contains",
base::UTF16ToUTF8(expected_str).c_str())) {
return arg->HasError(key::kNTPShortcuts) &&
arg->GetErrorMessages(key::kNTPShortcuts,
PolicyMap::MessageType::kWarning)
.find(expected_str) != std::wstring::npos;
}
} // namespace
class NTPShortcutsPolicyHandlerTest : public testing::Test {
public:
NTPShortcutsPolicyHandlerTest() = default;
void SetUp() override {
feature_list_.InitAndEnableFeature(ntp_tiles::kNtpEnterpriseShortcuts);
}
protected:
base::test::ScopedFeatureList feature_list_;
NTPShortcutsPolicyHandler handler_{
Schema::Wrap(policy::GetChromeSchemaData())};
policy::PolicyMap policies_;
PolicyErrorMap errors_;
PrefValueMap prefs_;
};
TEST_F(NTPShortcutsPolicyHandlerTest, PolicyNotSet) {
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_TRUE(errors_.empty());
handler_.ApplyPolicySettings(policies_, &prefs_);
EXPECT_FALSE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
nullptr));
}
TEST_F(NTPShortcutsPolicyHandlerTest, ValidNTPShortcuts_FeatureDisabled) {
feature_list_.Reset();
feature_list_.InitAndDisableFeature(ntp_tiles::kNtpEnterpriseShortcuts);
base::Value::List policy_value;
for (const auto& test_case : kValidTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_TRUE(errors_.empty());
handler_.ApplyPolicySettings(policies_, &prefs_);
EXPECT_FALSE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
nullptr));
}
TEST_F(NTPShortcutsPolicyHandlerTest, ValidNTPShortcuts) {
base::Value::List policy_value;
for (const auto& test_case : kValidTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_TRUE(errors_.empty());
handler_.ApplyPolicySettings(policies_, &prefs_);
base::Value* shortcuts = nullptr;
ASSERT_TRUE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
&shortcuts));
ASSERT_NE(shortcuts, nullptr);
ASSERT_TRUE(shortcuts->is_list());
EXPECT_THAT(shortcuts->GetList(),
ElementsAre(IsNTPShortcutEntry(kValidTestShortcuts[0]),
IsNTPShortcutEntry(kValidTestShortcuts[1]),
IsNTPShortcutEntry(kValidTestShortcuts[2])));
}
TEST_F(NTPShortcutsPolicyHandlerTest, InvalidFormat) {
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(false), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_TRUE(errors_.HasError(key::kNTPShortcuts));
}
TEST_F(NTPShortcutsPolicyHandlerTest, TooManyNTPShortcuts) {
// Policy value has one list entry over the max allowed.
base::Value::List policy_value;
for (int i = 0; i <= NTPShortcutsPolicyHandler::kMaxNtpShortcuts; ++i) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(
{.name = base::StringPrintf("name %d", i),
.url = base::StringPrintf("https://site_%d.com/", i)}));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationError(l10n_util::GetStringFUTF16(
IDS_POLICY_NTP_SHORTCUTS_MAX_SHORTCUTS_LIMIT_ERROR,
base::NumberToString16(
NTPShortcutsPolicyHandler::kMaxNtpShortcuts))));
}
TEST_F(NTPShortcutsPolicyHandlerTest, MissingRequiredFieldWithValidShortcuts) {
base::Value::List policy_value;
for (const auto& test_case : kMissingRequiredFieldsTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policy_value.Append(GenerateNTPShortcutPolicyEntry(kValidTestShortcuts[0]));
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
// CheckPolicySettings should return true because there are valid shortcuts.
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
// There should be warnings for the invalid shortcuts.
EXPECT_THAT(&errors_,
HasValidationWarning(
u"Error at NTPShortcuts[1]: Schema validation error: Missing "
u"or invalid required property: url"));
handler_.ApplyPolicySettings(policies_, &prefs_);
base::Value* shortcuts = nullptr;
ASSERT_TRUE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
&shortcuts));
ASSERT_NE(shortcuts, nullptr);
ASSERT_TRUE(shortcuts->is_list());
// Only the valid shortcuts should be in the prefs.
EXPECT_THAT(shortcuts->GetList(),
ElementsAre(IsNTPShortcutEntry(kValidTestShortcuts[0])));
}
TEST_F(NTPShortcutsPolicyHandlerTest, MissingRequiredField) {
base::Value::List policy_value;
for (const auto& test_case : kMissingRequiredFieldsTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_TRUE(errors_.HasError(key::kNTPShortcuts));
}
TEST_F(NTPShortcutsPolicyHandlerTest, UrlNotUnique) {
base::Value::List policy_value;
for (const auto& test_case : kUrlNotUniqueTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationWarning(l10n_util::GetStringFUTF16(
IDS_POLICY_NTP_SHORTCUTS_DUPLICATED_URL,
u"https://work.com/")));
handler_.ApplyPolicySettings(policies_, &prefs_);
base::Value* shortcuts = nullptr;
ASSERT_TRUE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
&shortcuts));
ASSERT_NE(shortcuts, nullptr);
ASSERT_TRUE(shortcuts->is_list());
EXPECT_THAT(shortcuts->GetList(),
ElementsAre(IsNTPShortcutEntry(kUrlNotUniqueTestShortcuts[2])));
}
TEST_F(NTPShortcutsPolicyHandlerTest, NoUniqueUrl) {
base::Value::List policy_value;
for (const auto& test_case : kNoUniqueUrlTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationWarning(l10n_util::GetStringFUTF16(
IDS_POLICY_NTP_SHORTCUTS_DUPLICATED_URL,
u"https://work.com/")));
EXPECT_THAT(&errors_, HasValidationError(l10n_util::GetStringUTF16(
IDS_POLICY_NTP_SHORTCUTS_NO_VALID_PROVIDER)));
}
TEST_F(NTPShortcutsPolicyHandlerTest, EmptyRequiredField) {
base::Value::List policy_value;
for (const auto& test_case : kEmptyFieldTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationWarning(l10n_util::GetStringUTF16(
IDS_SEARCH_POLICY_SETTINGS_NAME_IS_EMPTY)));
EXPECT_THAT(&errors_, HasValidationWarning(l10n_util::GetStringUTF16(
IDS_SEARCH_POLICY_SETTINGS_URL_IS_EMPTY)));
}
TEST_F(NTPShortcutsPolicyHandlerTest, UnknownField) {
constexpr char kUnknownFieldName[] = "unknown_field";
base::Value::Dict entry =
GenerateNTPShortcutPolicyEntry(kUnknownFieldTestShortcuts[0]);
entry.Set(kUnknownFieldName, true);
base::Value::List policy_value;
policy_value.Append(std::move(entry));
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
// A warning is registered during policy validation, but valid fields are
// still used for building a new shortcut.
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_FALSE(errors_.empty());
handler_.ApplyPolicySettings(policies_, &prefs_);
base::Value* shortcuts = nullptr;
ASSERT_TRUE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
&shortcuts));
ASSERT_NE(shortcuts, nullptr);
ASSERT_TRUE(shortcuts->is_list());
EXPECT_THAT(shortcuts->GetList(),
ElementsAre(IsNTPShortcutEntry(kUnknownFieldTestShortcuts[0])));
}
TEST_F(NTPShortcutsPolicyHandlerTest, InvalidUrlError) {
base::Value::List policy_value;
for (const auto& test_case : kInvalidUrlTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationWarning(l10n_util::GetStringUTF16(
IDS_POLICY_INVALID_URL_ERROR)));
EXPECT_THAT(&errors_, HasValidationError(l10n_util::GetStringUTF16(
IDS_POLICY_NTP_SHORTCUTS_NO_VALID_PROVIDER)));
}
TEST_F(NTPShortcutsPolicyHandlerTest, InvalidUrlWarning) {
base::Value::List policy_value;
for (const auto& test_case : kInvalidUrlTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policy_value.Append(GenerateNTPShortcutPolicyEntry(kValidTestShortcuts[0]));
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationWarning(l10n_util::GetStringUTF16(
IDS_POLICY_INVALID_URL_ERROR)));
handler_.ApplyPolicySettings(policies_, &prefs_);
base::Value* shortcuts = nullptr;
ASSERT_TRUE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
&shortcuts));
ASSERT_NE(shortcuts, nullptr);
ASSERT_TRUE(shortcuts->is_list());
EXPECT_THAT(shortcuts->GetList(),
ElementsAre(IsNTPShortcutEntry(kValidTestShortcuts[0])));
}
TEST_F(NTPShortcutsPolicyHandlerTest, NoValidEntry) {
base::Value::List policy_value;
for (const auto& test_case : kInvalidUrlTestShortcuts) {
policy_value.Append(GenerateNTPShortcutPolicyEntry(test_case));
}
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationError(l10n_util::GetStringUTF16(
IDS_POLICY_NTP_SHORTCUTS_NO_VALID_PROVIDER)));
}
TEST_F(NTPShortcutsPolicyHandlerTest, EmptyList) {
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(base::Value::List()), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_, HasValidationError(l10n_util::GetStringUTF16(
IDS_POLICY_NTP_SHORTCUTS_NO_VALID_PROVIDER)));
}
TEST_F(NTPShortcutsPolicyHandlerTest, NameTooLong) {
base::Value::List policy_value;
policy_value.Append(GenerateNTPShortcutPolicyEntry(
{.name = std::string(
NTPShortcutsPolicyHandler::kMaxNtpShortcutTextLength + 1, 'a'),
.url = "https://work.com/"}));
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_,
HasValidationWarning(l10n_util::GetStringFUTF16(
IDS_POLICY_NTP_SHORTCUTS_NAME_TOO_LONG,
base::NumberToString16(
NTPShortcutsPolicyHandler::kMaxNtpShortcutTextLength))));
}
TEST_F(NTPShortcutsPolicyHandlerTest, UrlTooLong) {
base::Value::List policy_value;
policy_value.Append(GenerateNTPShortcutPolicyEntry(
{.name = "work",
.url = base::StringPrintf(
"https://%s.com/",
std::string(NTPShortcutsPolicyHandler::kMaxNtpShortcutTextLength,
'a')
.c_str())}));
policies_.Set(key::kNTPShortcuts, policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
base::Value(std::move(policy_value)), nullptr);
ASSERT_FALSE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_THAT(&errors_,
HasValidationWarning(l10n_util::GetStringFUTF16(
IDS_POLICY_NTP_SHORTCUTS_URL_TOO_LONG,
base::NumberToString16(
NTPShortcutsPolicyHandler::kMaxNtpShortcutTextLength))));
}
TEST_F(NTPShortcutsPolicyHandlerTest, WithFakeDataEnabled) {
feature_list_.Reset();
feature_list_.InitAndEnableFeatureWithParameters(
ntp_tiles::kNtpEnterpriseShortcuts,
{{ntp_tiles::kNtpEnterpriseShortcutsUseFakeDataParam.name, "true"}});
// Expected shortcuts from
// NTPShortcutsPolicyHandler::ApplyFakeDataForManualTesting().
const TestShortcut kExpectedTestShortcuts[] = {
{.name = "Google Search", .url = "https://www.google.com/"},
{.name = "Workday", .url = "https://workday.com"},
{.name = "Concur", .url = "https://www.concur.com"},
{.name = "JIRA", .url = "https://www.jira.com"},
{.name = "Google Drive (E/D)",
.url = "https://drive.google.com",
.allow_user_edit = true,
.allow_user_delete = true},
{.name = "Zoom (E/D)",
.url = "https://zoom.com",
.allow_user_edit = true,
.allow_user_delete = true},
{.name = "Google Calendar (E)",
.url = "https://calendar.google.com",
.allow_user_edit = true,
.allow_user_delete = false},
{.name = "Gmail (D)",
.url = "https://mail.google.com",
.allow_user_edit = false,
.allow_user_delete = true},
};
ASSERT_TRUE(handler_.CheckPolicySettings(policies_, &errors_));
EXPECT_TRUE(errors_.empty());
handler_.ApplyPolicySettings(policies_, &prefs_);
base::Value* shortcuts = nullptr;
ASSERT_TRUE(prefs_.GetValue(ntp_tiles::prefs::kEnterpriseShortcutsPolicyList,
&shortcuts));
ASSERT_NE(shortcuts, nullptr);
ASSERT_TRUE(shortcuts->is_list());
EXPECT_EQ(shortcuts->GetList().size(), 8u);
EXPECT_THAT(shortcuts->GetList(),
ElementsAre(IsNTPShortcutEntry(kExpectedTestShortcuts[0]),
IsNTPShortcutEntry(kExpectedTestShortcuts[1]),
IsNTPShortcutEntry(kExpectedTestShortcuts[2]),
IsNTPShortcutEntry(kExpectedTestShortcuts[3]),
IsNTPShortcutEntry(kExpectedTestShortcuts[4]),
IsNTPShortcutEntry(kExpectedTestShortcuts[5]),
IsNTPShortcutEntry(kExpectedTestShortcuts[6]),
IsNTPShortcutEntry(kExpectedTestShortcuts[7])));
}
} // namespace policy
|