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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
|
// Copyright 2023 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/ash/policy/dlp/files_policy_notification_manager.h"
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include "ash/webui/file_manager/file_manager_ui.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/test_mock_time_task_runner.h"
#include "chrome/browser/ash/extensions/file_manager/event_router.h"
#include "chrome/browser/ash/extensions/file_manager/event_router_factory.h"
#include "chrome/browser/ash/file_manager/file_manager_test_util.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/file_manager/io_task.h"
#include "chrome/browser/ash/policy/dlp/dialogs/files_policy_dialog.h"
#include "chrome/browser/ash/policy/dlp/dialogs/files_policy_error_dialog.h"
#include "chrome/browser/ash/policy/dlp/dialogs/files_policy_warn_dialog.h"
#include "chrome/browser/ash/policy/dlp/files_policy_notification_manager.h"
#include "chrome/browser/ash/policy/dlp/files_policy_notification_manager_factory.h"
#include "chrome/browser/ash/policy/dlp/test/files_policy_notification_manager_test_utils.h"
#include "chrome/browser/ash/policy/dlp/test/mock_dlp_files_controller_ash.h"
#include "chrome/browser/ash/system_web_apps/system_web_app_manager.h"
#include "chrome/browser/chromeos/policy/dlp/dialogs/policy_dialog_base.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_confidential_file.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_file_destination.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_files_utils.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_policy_constants.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager_factory.h"
#include "chrome/browser/chromeos/policy/dlp/test/mock_dlp_rules_manager.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/notifications/notification_display_service_impl.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/notifications/notification_platform_bridge_delegator.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/enterprise/data_controls/core/browser/dlp_histogram_helper.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/test/browser_test.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/message_center/public/cpp/notification.h"
namespace policy {
namespace {
using testing::Field;
using policy::AddCopyOrMoveIOTask;
using policy::kNotificationId;
const file_manager::io_task::IOTaskId kTaskId1 = 1u;
const file_manager::io_task::IOTaskId kTaskId2 = 2u;
constexpr char kNotificationId1[] = "swa-file-operation-1";
constexpr char kNotificationId2[] = "swa-file-operation-2";
constexpr base::TimeDelta kWarningTimeout = base::Minutes(5);
} // namespace
using DialogInfoMap =
std::map<FilesPolicyDialog::BlockReason, FilesPolicyDialog::Info>;
class MockFilesPolicyDialogFactory : public FilesPolicyDialogFactory {
public:
MockFilesPolicyDialogFactory() {
ON_CALL(*this, CreateWarnDialog)
.WillByDefault([](WarningWithJustificationCallback callback,
dlp::FileAction file_action,
gfx::NativeWindow modal_parent,
std::optional<DlpFileDestination> destination,
FilesPolicyDialog::Info dialog_info) {
views::Widget* widget = views::DialogDelegate::CreateDialogWidget(
std::make_unique<FilesPolicyWarnDialog>(
std::move(callback), file_action, modal_parent, destination,
std::move(dialog_info)),
/*context=*/nullptr, modal_parent);
widget->Show();
return widget;
});
ON_CALL(*this, CreateErrorDialog)
.WillByDefault([](const DialogInfoMap& dialog_info_map,
dlp::FileAction action,
gfx::NativeWindow modal_parent) {
views::Widget* widget = views::DialogDelegate::CreateDialogWidget(
std::make_unique<FilesPolicyErrorDialog>(dialog_info_map, action,
modal_parent),
/*context=*/nullptr, modal_parent);
widget->Show();
return widget;
});
}
MOCK_METHOD(views::Widget*,
CreateWarnDialog,
(WarningWithJustificationCallback,
dlp::FileAction,
gfx::NativeWindow,
std::optional<DlpFileDestination>,
FilesPolicyDialog::Info),
(override));
MOCK_METHOD(views::Widget*,
CreateErrorDialog,
(const DialogInfoMap&, dlp::FileAction, gfx::NativeWindow),
(override));
};
// NotificationPlatformBridgeDelegator test implementation. Keeps track of
// displayed notifications and allows clicking on a displayed notification.
class TestNotificationPlatformBridgeDelegator
: public NotificationPlatformBridgeDelegator {
public:
explicit TestNotificationPlatformBridgeDelegator(Profile* profile)
: NotificationPlatformBridgeDelegator(profile, base::DoNothing()) {}
~TestNotificationPlatformBridgeDelegator() override = default;
// NotificationPlatformBridgeDelegator:
void Display(
NotificationHandler::Type notification_type,
const message_center::Notification& notification,
std::unique_ptr<NotificationCommon::Metadata> metadata) override {
notifications_.emplace(notification.id(), notification);
ids_.insert(notification.id());
}
void Close(NotificationHandler::Type notification_type,
const std::string& notification_id) override {
notifications_.erase(notification_id);
ids_.erase(notification_id);
}
void GetDisplayed(GetDisplayedNotificationsCallback callback) const override {
std::move(callback).Run(ids_, /*supports_sync=*/true);
}
std::optional<message_center::Notification> GetDisplayedNotification(
const std::string& notification_id) {
auto it = notifications_.find(notification_id);
if (it != notifications_.end()) {
return it->second;
}
return std::nullopt;
}
// If a notification with `notification_id` is displayed, simulates clicking
// on that notification with `button_index` button.
void Click(const std::string& notification_id,
std::optional<int> button_index) {
auto it = notifications_.find(notification_id);
if (it == notifications_.end()) {
return;
}
it->second.delegate()->Click(button_index, std::nullopt);
}
private:
std::map<std::string, message_center::Notification> notifications_;
std::set<std::string> ids_;
};
class FilesPolicyNotificationManagerBrowserTest : public InProcessBrowserTest {
public:
FilesPolicyNotificationManagerBrowserTest() {
scoped_feature_list_.InitAndEnableFeature(features::kNewFilesPolicyUX);
}
FilesPolicyNotificationManagerBrowserTest(
const FilesPolicyNotificationManagerBrowserTest&) = delete;
FilesPolicyNotificationManagerBrowserTest& operator=(
const FilesPolicyNotificationManagerBrowserTest&) = delete;
~FilesPolicyNotificationManagerBrowserTest() override = default;
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
// Needed to check that Files app was/wasn't opened.
ash::SystemWebAppManager::GetForTest(browser()->profile())
->InstallSystemAppsForTesting();
file_manager::test::AddDefaultComponentExtensionsOnMainThread(
browser()->profile());
display_service_ = static_cast<NotificationDisplayServiceImpl*>(
NotificationDisplayServiceFactory::GetForProfile(browser()->profile()));
auto bridge = std::make_unique<TestNotificationPlatformBridgeDelegator>(
browser()->profile());
bridge_ = bridge.get();
display_service_->SetNotificationPlatformBridgeDelegatorForTesting(
std::move(bridge));
factory_ = std::make_unique<MockFilesPolicyDialogFactory>();
FilesPolicyDialog::SetFactory(factory_.get());
fpnm_ = FilesPolicyNotificationManagerFactory::GetForBrowserContext(
browser()->profile());
ASSERT_TRUE(fpnm_);
}
protected:
FilesPolicyDialogFactory* factory() { return factory_.get(); }
// Returns the last active Files app window, or nullptr when none are found.
Browser* FindFilesApp() {
return FindSystemWebAppBrowser(browser()->profile(),
ash::SystemWebAppType::FILE_MANAGER);
}
base::test::ScopedFeatureList scoped_feature_list_;
base::HistogramTester histogram_tester_;
raw_ptr<NotificationDisplayServiceImpl, DanglingUntriaged> display_service_;
raw_ptr<TestNotificationPlatformBridgeDelegator, DanglingUntriaged> bridge_;
std::unique_ptr<MockFilesPolicyDialogFactory> factory_;
raw_ptr<policy::FilesPolicyNotificationManager, DanglingUntriaged> fpnm_ =
nullptr;
};
class NonIOWarningBrowserTest
: public FilesPolicyNotificationManagerBrowserTest,
public ::testing::WithParamInterface<dlp::FileAction> {};
// Tests that clicking on the warning notification, but no button is ignored.
// Timing out the warning closes the notification and cancels the task.
IN_PROC_BROWSER_TEST_P(NonIOWarningBrowserTest, SingleFileNoButtonIgnored) {
auto action = GetParam();
scoped_refptr<base::TestMockTimeTaskRunner> task_runner =
base::MakeRefCounted<base::TestMockTimeTaskRunner>();
fpnm_->SetTaskRunnerForTesting(task_runner);
// The callback is not invoked.
base::MockCallback<WarningWithJustificationCallback> cb;
EXPECT_CALL(cb, Run).Times(0);
fpnm_->ShowDlpWarning(cb.Get(), /*task_id=*/std::nullopt,
{base::FilePath("file1.txt")}, DlpFileDestination(),
action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, /*button_index=*/std::nullopt);
// The notification shouldn't be closed.
EXPECT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
// Skip the warning timeout. The callback is only invoked when the warning
// times out.
testing::Mock::VerifyAndClearExpectations(&cb);
EXPECT_CALL(cb, Run(/*user_justification=*/std::optional<std::u16string>(),
/*should_proceed=*/false));
task_runner->FastForwardBy(kWarningTimeout);
// The warning notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
// The warning timeout notification should be shown.
EXPECT_TRUE(bridge_->GetDisplayedNotification("dlp_files_1").has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(1, 1)},
/*action_timedout_buckets=*/{base::Bucket(action, 1)});
}
// Tests that closing the warning notification (e.g. by X or Dismiss all)
// invokes the Cancel callback.
IN_PROC_BROWSER_TEST_P(NonIOWarningBrowserTest, SingleFileCloseCancels) {
auto action = GetParam();
// The task is cancelled.
base::MockCallback<WarningWithJustificationCallback> cb;
EXPECT_CALL(cb, Run(/*user_justification=*/std::optional<std::u16string>(),
/*should_proceed=*/false))
.Times(1);
fpnm_->ShowDlpWarning(cb.Get(), /*task_id=*/std::nullopt,
{base::FilePath("file1.txt")}, DlpFileDestination(),
action);
auto notification = bridge_->GetDisplayedNotification(kNotificationId);
ASSERT_TRUE(notification.has_value());
notification->delegate()->Close(
/*by_user=*/true); // parameter doesn't matter
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(1, 1)},
/*action_timedout_buckets=*/{});
}
// Tests that clicking the OK button on a warning notification for a single
// file continues the action without showing the dialog.
IN_PROC_BROWSER_TEST_P(NonIOWarningBrowserTest, SingleFileOKContinues) {
auto action = GetParam();
EXPECT_CALL(*factory_, CreateWarnDialog).Times(0);
// No Files app opened.
ASSERT_FALSE(FindSystemWebAppBrowser(browser()->profile(),
ash::SystemWebAppType::FILE_MANAGER));
// The callback is invoked directly from the notification.
base::MockCallback<WarningWithJustificationCallback> cb;
EXPECT_CALL(cb, Run(/*user_justification=*/std::optional<std::u16string>(),
/*should_proceed=*/true))
.Times(1);
fpnm_->ShowDlpWarning(cb.Get(), /*task_id=*/std::nullopt,
{base::FilePath("file1.txt")}, DlpFileDestination(),
action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, NotificationButton::OK);
// No Files app opened.
ASSERT_FALSE(FindSystemWebAppBrowser(browser()->profile(),
ash::SystemWebAppType::FILE_MANAGER));
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(1, 1)},
/*action_timedout_buckets=*/{});
}
// This is a test for b/277594200. Tests that clicking the OK button on a
// warning notification for multiple files shows a dialog instead of continuing
// the action and always opens the Files app. Timing out the warning closes the
// dialogs and cancels the task.
IN_PROC_BROWSER_TEST_P(NonIOWarningBrowserTest, MultiFileOKShowsDialog) {
auto action = GetParam();
std::vector<base::FilePath> warning_files;
warning_files.emplace_back("file1.txt");
warning_files.emplace_back("file2.txt");
EXPECT_CALL(*factory_,
CreateWarnDialog(
base::test::IsNotNullCallback(), action, testing::NotNull(),
testing::Eq(std::nullopt),
FilesPolicyDialog::Info::Warn(
FilesPolicyDialog::BlockReason::kDlp, warning_files)))
.Times(2)
.WillRepeatedly(
[](WarningWithJustificationCallback callback,
dlp::FileAction file_action, gfx::NativeWindow modal_parent,
std::optional<DlpFileDestination> destination,
FilesPolicyDialog::Info dialog_info) {
views::Widget* widget = views::DialogDelegate::CreateDialogWidget(
std::make_unique<FilesPolicyWarnDialog>(
std::move(callback), file_action, modal_parent, destination,
std::move(dialog_info)),
/*context=*/nullptr, modal_parent);
widget->Show();
return widget;
});
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
scoped_refptr<base::TestMockTimeTaskRunner> task_runner =
base::MakeRefCounted<base::TestMockTimeTaskRunner>();
fpnm_->SetTaskRunnerForTesting(task_runner);
base::MockCallback<WarningWithJustificationCallback> cb;
EXPECT_CALL(cb, Run).Times(0);
fpnm_->ShowDlpWarning(cb.Get(), /*task_id=*/std::nullopt, warning_files,
DlpFileDestination(), action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, NotificationButton::OK);
// Check that a new Files app is opened.
ui_test_utils::WaitForBrowserToOpen();
ASSERT_EQ(ash::file_manager::FileManagerUI::GetNumInstances(), 1);
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
// Show another notification and dialog. Another app should be opened.
fpnm_->ShowDlpWarning(cb.Get(), /*task_id=*/std::nullopt, warning_files,
DlpFileDestination(), action);
const std::string second_notification = "dlp_files_1";
ASSERT_TRUE(
bridge_->GetDisplayedNotification(second_notification).has_value());
bridge_->Click(second_notification, NotificationButton::OK);
// Check that a new Files app is opened.
ui_test_utils::WaitForBrowserToOpen();
ASSERT_EQ(ash::file_manager::FileManagerUI::GetNumInstances(), 2);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 2);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
// Skip the warning timeout. The callback will only be invoked when the
// warnings time out.
testing::Mock::VerifyAndClearExpectations(&cb);
EXPECT_CALL(cb, Run(/*user_justification=*/std::optional<std::u16string>(),
/*should_proceed=*/false))
.Times(2);
task_runner->FastForwardBy(kWarningTimeout);
// The warning timeout notifications should be shown.
ASSERT_TRUE(bridge_->GetDisplayedNotification("dlp_files_2").has_value());
ASSERT_TRUE(bridge_->GetDisplayedNotification("dlp_files_3").has_value());
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 2)},
/*warning_count_buckets=*/{base::Bucket(2, 2)},
/*action_timedout_buckets=*/{base::Bucket(action, 2)});
}
// Tests that clicking the OK button on a warning notification for multiple
// files shows a system modal dialog when Files app doesn't launch before
// timeout. Proceeding the warning from the dialog continues the task and closes
// the dialog, and the warning timeout is then ignored.
IN_PROC_BROWSER_TEST_P(NonIOWarningBrowserTest,
MultiFileOKShowsDialog_Timeout) {
auto action = GetParam();
std::vector<base::FilePath> warning_files;
warning_files.emplace_back("file1.txt");
warning_files.emplace_back("file2.txt");
EXPECT_CALL(*factory_,
CreateWarnDialog(
base::test::IsNotNullCallback(), action, testing::IsNull(),
// Null modal parent means the dialog is a system modal.
testing::Eq(std::nullopt),
FilesPolicyDialog::Info::Warn(
FilesPolicyDialog::BlockReason::kDlp, warning_files)))
.Times(1);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
scoped_refptr<base::TestMockTimeTaskRunner> task_runner =
base::MakeRefCounted<base::TestMockTimeTaskRunner>();
fpnm_->SetTaskRunnerForTesting(task_runner);
base::MockCallback<WarningWithJustificationCallback> cb;
EXPECT_CALL(cb, Run).Times(0);
fpnm_->ShowDlpWarning(cb.Get(), /*task_id=*/std::nullopt, warning_files,
DlpFileDestination(), action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, NotificationButton::OK);
// Skip the timeout.
task_runner->FastForwardBy(base::TimeDelta(base::Milliseconds(3000)));
// Check that a new Files app is still opened.
ASSERT_EQ(ui_test_utils::WaitForBrowserToOpen(), FindFilesApp());
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
// Accept the warning.
testing::Mock::VerifyAndClearExpectations(&cb);
EXPECT_CALL(cb, Run(/*user_justification=*/std::optional<std::u16string>(),
/*should_proceed=*/true));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_RETURN, /*control=*/false,
/*shift=*/false, /*alt=*/false, /*command=*/false));
// Skip the warning timeout. Shouldn't do anything.
testing::Mock::VerifyAndClearExpectations(&cb);
EXPECT_CALL(cb, Run).Times(0);
task_runner->FastForwardBy(kWarningTimeout);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 1);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(2, 1)},
/*action_timedout_buckets=*/{});
}
// Tests that clicking the Cancel button on a warning notification cancels the
// action without showing the dialog.
IN_PROC_BROWSER_TEST_P(NonIOWarningBrowserTest, CancelShowsNoDialog) {
auto action = GetParam();
EXPECT_CALL(*factory_, CreateWarnDialog).Times(0);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
// The callback is invoked directly from the notification.
base::MockCallback<WarningWithJustificationCallback> cb;
EXPECT_CALL(cb, Run(/*user_justification=*/std::optional<std::u16string>(),
/*should_proceed=*/false))
.Times(1);
fpnm_->ShowDlpWarning(
cb.Get(), /*task_id=*/std::nullopt,
{base::FilePath("file1.txt"), base::FilePath("file2.txt")},
DlpFileDestination(), action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, NotificationButton::CANCEL);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(2, 1)},
/*action_timedout_buckets=*/{});
}
INSTANTIATE_TEST_SUITE_P(FPNM,
NonIOWarningBrowserTest,
::testing::Values(dlp::FileAction::kUpload,
dlp::FileAction::kMove));
class NonIOErrorBrowserTest
: public FilesPolicyNotificationManagerBrowserTest,
public ::testing::WithParamInterface<dlp::FileAction> {};
// Tests that clicking the OK button on an error notification for multiple-
// files shows a dialog.
IN_PROC_BROWSER_TEST_P(NonIOErrorBrowserTest, MultiFileOKShowsDialog) {
auto action = GetParam();
DialogInfoMap dialog_info_map;
const std::vector<base::FilePath> paths = {base::FilePath("file1.txt"),
base::FilePath("file2.txt")};
auto dialog_info = FilesPolicyDialog::Info::Error(
FilesPolicyDialog::BlockReason::kDlp, paths);
dialog_info_map.insert({FilesPolicyDialog::BlockReason::kDlp, dialog_info});
EXPECT_CALL(*factory_,
CreateErrorDialog(dialog_info_map, action, testing::NotNull()))
.Times(1);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
std::vector<base::FilePath> blocked_files;
blocked_files.emplace_back("file1.txt");
blocked_files.emplace_back("file2.txt");
fpnm_->ShowDlpBlockedFiles(std::nullopt, std::move(blocked_files), action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, NotificationButton::OK);
// Check that a new Files app is opened.
ASSERT_EQ(ui_test_utils::WaitForBrowserToOpen(), FindFilesApp());
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 1);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(2, 1)));
}
// Tests that clicking on the error notification, but no button is ignored.
IN_PROC_BROWSER_TEST_P(NonIOErrorBrowserTest, MultiFileNoButtonIgnored) {
auto action = GetParam();
std::vector<base::FilePath> blocked_files;
blocked_files.emplace_back("file1.txt");
blocked_files.emplace_back("file2.txt");
fpnm_->ShowDlpBlockedFiles(std::nullopt, std::move(blocked_files), action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, /*button_index=*/std::nullopt);
// The notification shouldn't be closed.
EXPECT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(2, 1)));
}
// Tests that closing the error notification (e.g. by X or Dismiss all)
// correctly closes it.
IN_PROC_BROWSER_TEST_P(NonIOErrorBrowserTest, MultiFileCloseCancels) {
auto action = GetParam();
std::vector<base::FilePath> blocked_files;
blocked_files.emplace_back("file1.txt");
blocked_files.emplace_back("file2.txt");
fpnm_->ShowDlpBlockedFiles(std::nullopt, std::move(blocked_files), action);
auto notification = bridge_->GetDisplayedNotification(kNotificationId);
ASSERT_TRUE(notification.has_value());
notification->delegate()->Close(
/*by_user=*/false); // parameter doesn't matter
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(2, 1)));
}
// Tests that clicking the OK button on an error notification for multiple
// files shows a system modal dialog when Files app doesn't launch before
// timeout.
IN_PROC_BROWSER_TEST_P(NonIOErrorBrowserTest, MultiFileOKShowsDialog_Timeout) {
auto action = GetParam();
DialogInfoMap dialog_info_map;
const std::vector<base::FilePath> paths = {base::FilePath("file1.txt"),
base::FilePath("file2.txt")};
dialog_info_map.insert({FilesPolicyDialog::BlockReason::kDlp,
FilesPolicyDialog::Info::Error(
FilesPolicyDialog::BlockReason::kDlp, paths)});
EXPECT_CALL(
*factory_,
CreateErrorDialog(dialog_info_map, action,
// Null modal parent means the dialog is a system modal.
testing::IsNull()))
.Times(1);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
scoped_refptr<base::TestMockTimeTaskRunner> task_runner =
base::MakeRefCounted<base::TestMockTimeTaskRunner>();
fpnm_->SetTaskRunnerForTesting(task_runner);
std::vector<base::FilePath> blocked_files;
blocked_files.emplace_back("file1.txt");
blocked_files.emplace_back("file2.txt");
fpnm_->ShowDlpBlockedFiles(std::nullopt, std::move(blocked_files), action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, NotificationButton::OK);
// Skip the timeout.
task_runner->FastForwardBy(base::TimeDelta(base::Milliseconds(3000)));
// Check that a new Files app is still opened.
ASSERT_EQ(ui_test_utils::WaitForBrowserToOpen(), FindFilesApp());
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 1);
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockReviewedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(2, 1)));
}
// Tests that clicking the Cancel button on an error notification dismisses
// the notification without showing the dialog.
IN_PROC_BROWSER_TEST_P(NonIOErrorBrowserTest, CancelDismisses) {
auto action = GetParam();
EXPECT_CALL(*factory_, CreateErrorDialog).Times(0);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
std::vector<base::FilePath> blocked_files;
blocked_files.emplace_back("file1.txt");
blocked_files.emplace_back("file2.txt");
fpnm_->ShowDlpBlockedFiles(std::nullopt, std::move(blocked_files), action);
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
bridge_->Click(kNotificationId, NotificationButton::CANCEL);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(2, 1)));
}
INSTANTIATE_TEST_SUITE_P(FPNM,
NonIOErrorBrowserTest,
::testing::Values(dlp::FileAction::kOpen,
dlp::FileAction::kDownload));
class IOTaskBrowserTest
: public FilesPolicyNotificationManagerBrowserTest,
public ::testing::WithParamInterface<
std::tuple<file_manager::io_task::OperationType, dlp::FileAction>> {
protected:
void SetUpOnMainThread() override {
FilesPolicyNotificationManagerBrowserTest::SetUpOnMainThread();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
file_system_context_ = file_manager::util::GetFileManagerFileSystemContext(
browser()->profile());
// DLP Setup.
policy::DlpRulesManagerFactory::GetInstance()->SetTestingFactory(
browser()->profile(),
base::BindRepeating(&IOTaskBrowserTest::SetDlpRulesManager,
base::Unretained(this)));
ASSERT_TRUE(policy::DlpRulesManagerFactory::GetForPrimaryProfile());
ASSERT_NE(policy::DlpRulesManagerFactory::GetForPrimaryProfile()
->GetDlpFilesController(),
nullptr);
}
void TearDownOnMainThread() override {
// The files controller must be destroyed before the profile since it's
// holding a pointer to it.
files_controller_.reset();
mock_rules_manager_ = nullptr;
FilesPolicyNotificationManagerBrowserTest::TearDownOnMainThread();
}
std::unique_ptr<KeyedService> SetDlpRulesManager(
content::BrowserContext* context) {
auto dlp_rules_manager =
std::make_unique<testing::NiceMock<policy::MockDlpRulesManager>>(
Profile::FromBrowserContext(context));
mock_rules_manager_ = dlp_rules_manager.get();
ON_CALL(*mock_rules_manager_, IsFilesPolicyEnabled)
.WillByDefault(testing::Return(true));
files_controller_ =
std::make_unique<testing::NiceMock<policy::MockDlpFilesControllerAsh>>(
*mock_rules_manager_, Profile::FromBrowserContext(context));
ON_CALL(*mock_rules_manager_, GetDlpFilesController())
.WillByDefault(::testing::Return(files_controller_.get()));
return dlp_rules_manager;
}
// Expects CheckIfTransferAllowed to be called. Once called, it calls
// FilesPolicyNotificationManager to show a warning.
void ExpectCheckIfTransferAllowedToWarn(
const file_manager::io_task::IOTaskId task_id,
const dlp::FileAction action,
const bool expected_should_proceed,
const std::vector<base::FilePath>& warning_files,
Policy type = Policy::kDlp) {
bool is_move = action == dlp::FileAction::kMove;
auto warn_on_check =
[=, this](std::optional<file_manager::io_task::IOTaskId> task_id,
const std::vector<storage::FileSystemURL>& transferred_files,
storage::FileSystemURL destination, bool is_move,
DlpFilesControllerAsh::CheckIfTransferAllowedCallback
result_callback) {
auto warn_cb = base::BindOnce(
[](DlpFilesControllerAsh::CheckIfTransferAllowedCallback cb,
const bool expected_should_proceed,
const std::vector<storage::FileSystemURL>& transferred_files,
std::optional<std::u16string> user_justification,
bool should_proceed) {
EXPECT_EQ(should_proceed, expected_should_proceed);
if (should_proceed) {
std::move(cb).Run({});
} else {
std::move(cb).Run({transferred_files});
}
},
std::move(result_callback), expected_should_proceed,
transferred_files);
if (type == Policy::kDlp) {
fpnm_->ShowDlpWarning(std::move(warn_cb), task_id.value(),
warning_files, DlpFileDestination(), action);
} else {
// Enterprise connectors file transfer currently support warning
// mode only for sensitive data.
auto dialog_info = FilesPolicyDialog::Info::Warn(
FilesPolicyDialog::BlockReason::
kEnterpriseConnectorsSensitiveData,
warning_files);
// Override default dialog settings.
dialog_info.SetMessage(u"Custom warning message");
dialog_info.SetLearnMoreURL(GURL("https://learnmore.com"));
dialog_info.SetBypassRequiresJustification(true);
fpnm_->ShowConnectorsWarning(std::move(warn_cb), task_id.value(),
action, std::move(dialog_info));
}
};
EXPECT_CALL(*files_controller_,
CheckIfTransferAllowed(std::make_optional(task_id), testing::_,
testing::_, is_move, testing::_))
.WillOnce(testing::Invoke(warn_on_check));
}
// Expects CheckIfTransferAllowed to be called. Once called, it calls
// FilesPolicyNotificationManager to show the blocked files.
void ExpectCheckIfTransferAllowedToBlock(
const file_manager::io_task::IOTaskId task_id,
const dlp::FileAction action,
const std::vector<base::FilePath>& blocked_files) {
bool is_move = (action == dlp::FileAction::kMove) ? true : false;
auto block_on_check =
[=, this](std::optional<file_manager::io_task::IOTaskId> task_id,
const std::vector<storage::FileSystemURL>& transferred_files,
storage::FileSystemURL destination, bool is_move,
DlpFilesControllerAsh::CheckIfTransferAllowedCallback
result_callback) {
fpnm_->ShowDlpBlockedFiles(task_id.value(), blocked_files, action);
// Return transferred files as blocked.
std::move(result_callback).Run(transferred_files);
};
EXPECT_CALL(*files_controller_,
CheckIfTransferAllowed(std::make_optional(task_id), testing::_,
testing::_, is_move, testing::_))
.WillOnce(testing::Invoke(block_on_check));
}
// Expects CheckIfTransferAllowed to be called. Once called, it calls
// FilesPolicyNotificationManager to show a warning then calls it again to
// show the blocked files.
void ExpectCheckIfTransferAllowedToWarnAndBlock(
const file_manager::io_task::IOTaskId task_id,
const dlp::FileAction action,
const bool expected_should_proceed,
const std::vector<base::FilePath>& warning_files,
const std::vector<base::FilePath>& blocked_files) {
bool is_move = (action == dlp::FileAction::kMove) ? true : false;
auto warn_on_check =
[=, this](std::optional<file_manager::io_task::IOTaskId> task_id,
const std::vector<storage::FileSystemURL>& transferred_files,
storage::FileSystemURL destination, bool is_move,
DlpFilesControllerAsh::CheckIfTransferAllowedCallback
result_callback) {
auto warn_cb = base::BindOnce(
[](DlpFilesControllerAsh::CheckIfTransferAllowedCallback cb,
const std::vector<storage::FileSystemURL>& transferred_files,
const bool expected_should_proceed,
std::optional<std::u16string> user_justification,
bool should_proceed) {
EXPECT_EQ(should_proceed, expected_should_proceed);
// Return transferred files as blocked.
std::move(cb).Run(transferred_files);
},
std::move(result_callback), transferred_files,
expected_should_proceed);
fpnm_->ShowDlpWarning(std::move(warn_cb), task_id.value(),
warning_files, DlpFileDestination(), action);
};
EXPECT_CALL(*files_controller_,
CheckIfTransferAllowed(std::make_optional(task_id), testing::_,
testing::_, is_move, testing::_))
.WillOnce(testing::Invoke(warn_on_check));
fpnm_->ShowDlpBlockedFiles(task_id, blocked_files, action);
}
// Expects CheckIfTransferAllowed to be called. Once called, it calls
// FilesPolicyNotificationManager to show a warning then waits for the user.
void ExpectCheckIfTransferAllowedToWarnAndWait(
const file_manager::io_task::IOTaskId task_id,
const dlp::FileAction action,
const bool expected_should_proceed,
const std::vector<base::FilePath>& warning_files) {
bool is_move = (action == dlp::FileAction::kMove) ? true : false;
auto warn_on_check =
[=, this](std::optional<file_manager::io_task::IOTaskId> task_id,
const std::vector<storage::FileSystemURL>& transferred_files,
storage::FileSystemURL destination, bool is_move,
DlpFilesControllerAsh::CheckIfTransferAllowedCallback
result_callback) {
auto warn_cb = base::BindOnce(
[](DlpFilesControllerAsh::CheckIfTransferAllowedCallback cb,
const std::vector<storage::FileSystemURL>& transferred_files,
const bool expected_should_proceed,
std::optional<std::u16string> user_justification,
bool should_proceed) {
EXPECT_EQ(should_proceed, expected_should_proceed);
},
std::move(result_callback), transferred_files,
expected_should_proceed);
fpnm_->ShowDlpWarning(std::move(warn_cb), task_id.value(),
warning_files, DlpFileDestination(), action);
};
EXPECT_CALL(*files_controller_,
CheckIfTransferAllowed(std::make_optional(task_id), testing::_,
testing::_, is_move, testing::_))
.WillOnce(testing::Invoke(warn_on_check));
}
base::ScopedTempDir temp_dir_;
scoped_refptr<storage::FileSystemContext> file_system_context_;
const blink::StorageKey kTestStorageKey =
blink::StorageKey::CreateFromStringForTesting("chrome://abc");
raw_ptr<policy::MockDlpRulesManager, DanglingUntriaged> mock_rules_manager_ =
nullptr;
std::unique_ptr<policy::MockDlpFilesControllerAsh> files_controller_;
};
// Tests that warning an IO task with multiple warning files shows a desktop
// notification. Skipping the timeout will abort the IO tasks with DLP warning
// timeout error.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest,
MultiFileTimeoutNotification_Warning) {
auto [type, action] = GetParam();
scoped_refptr<base::TestMockTimeTaskRunner> task_runner =
base::MakeRefCounted<base::TestMockTimeTaskRunner>();
fpnm_->SetTaskRunnerForTesting(task_runner);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
// CheckIfTransferAllowed will call FPNM to show the warning which will pause
// the IO task and trigger the notification.
ExpectCheckIfTransferAllowedToWarnAndWait(
kTaskId1, action,
/*expected_should_proceed=*/false,
{base::FilePath("file1.txt"), base::FilePath("file2.txt")});
// Add the tasks.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
auto notification1 = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification1.has_value());
const std::u16string warning_title =
action == dlp::FileAction::kCopy
? l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_COPY_REVIEW_TITLE)
: l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_MOVE_REVIEW_TITLE);
EXPECT_EQ(notification1->title(), warning_title);
// Skip the warning timeout.
task_runner->FastForwardBy(kWarningTimeout);
// Wait till IO task is complete.
base::RunLoop().RunUntilIdle();
const std::u16string timeout_title =
action == dlp::FileAction::kCopy
? l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_COPY_TIMEOUT_TITLE)
: l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_MOVE_TIMEOUT_TITLE);
notification1 = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification1.has_value());
EXPECT_EQ(notification1->title(), timeout_title);
EXPECT_FALSE(fpnm_->HasIOTask(kTaskId1));
// Dismiss the notification.
bridge_->Click(kNotificationId1, std::nullopt);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(2, 1)},
/*action_timedout_buckets=*/{base::Bucket(action, 1)});
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId1).has_value());
}
// Tests that clicking the OK button on a warning notification shown for copy or
// move IO task with multiple warning files shows a dialog instead of continuing
// the action, and opens the Files App only if there's not one opened already.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest,
MultiFileOKShowsDialogOverFilesApp_Warning) {
auto [type, action] = GetParam();
// 2 dialogs should be shown.
std::vector<base::FilePath> warning_files;
warning_files.emplace_back("file1.txt");
warning_files.emplace_back("file2.txt");
auto dialog_info = FilesPolicyDialog::Info::Warn(
FilesPolicyDialog::BlockReason::kDlp, warning_files);
EXPECT_CALL(*factory_,
CreateWarnDialog(base::test::IsNotNullCallback(), action,
testing::NotNull(), testing::Eq(std::nullopt),
std::move(dialog_info)))
.Times(2)
.WillRepeatedly([](WarningWithJustificationCallback callback,
dlp::FileAction file_action,
gfx::NativeWindow modal_parent,
std::optional<DlpFileDestination> destination,
FilesPolicyDialog::Info dialog_info) {
// Cancel the task so it's deleted properly.
std::move(callback).Run(/*user_justification=*/std::nullopt,
/*should_proceed=*/false);
return nullptr;
});
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
// CheckIfTransferAllowed will call FPNM to show the warning which will pause
// the IO task and trigger the notification. Do this before any Files App is
// opened so that we are sure we show system notifications.
ExpectCheckIfTransferAllowedToWarn(kTaskId1, action,
/*expected_should_proceed=*/false,
warning_files);
ExpectCheckIfTransferAllowedToWarn(kTaskId2, action,
/*expected_should_proceed=*/false,
warning_files);
// Add the tasks.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId2, type,
temp_dir_.GetPath(), "test2.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId2));
const std::u16string title =
action == dlp::FileAction::kCopy
? l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_COPY_REVIEW_TITLE)
: l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_MOVE_REVIEW_TITLE);
auto notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
EXPECT_EQ(notification->title(), title);
notification = bridge_->GetDisplayedNotification(kNotificationId2);
ASSERT_TRUE(notification.has_value());
EXPECT_EQ(notification->title(), title);
// Show the first dialog.
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId1).has_value());
bridge_->Click(kNotificationId1, NotificationButton::OK);
// Check that a new Files app is opened.
Browser* first_app = ui_test_utils::WaitForBrowserToOpen();
ASSERT_TRUE(first_app);
ASSERT_EQ(first_app, FindFilesApp());
// The first notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId1).has_value());
// Show the second dialog.
ASSERT_TRUE(bridge_->GetDisplayedNotification(kNotificationId2).has_value());
bridge_->Click(kNotificationId2, NotificationButton::OK);
// Check that the last active Files app is the same as before.
ASSERT_TRUE(first_app);
ASSERT_EQ(first_app, FindFilesApp());
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId2).has_value());
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 1);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 2)},
/*warning_count_buckets=*/{base::Bucket(2, 2)},
/*action_timedout_buckets=*/{});
}
// Tests that clicking the Cancel button on a warning notification shown for
// copy or move IO task with multiple warning files will cancel the task.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest, MultiFileDismissCancels_Warning) {
auto [type, action] = GetParam();
// CheckIfTransferAllowed will call FPNM to show the warning which will pause
// the IO task and trigger the notification.
ExpectCheckIfTransferAllowedToWarn(
kTaskId1, action,
/*expected_should_proceed=*/false,
{base::FilePath("file1.txt"), base::FilePath("file2.txt")});
// Add the task.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
auto notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
const std::u16string title =
action == dlp::FileAction::kCopy
? l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_COPY_REVIEW_TITLE)
: l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_MOVE_REVIEW_TITLE);
// Cancel the warning.
EXPECT_EQ(notification->title(), title);
bridge_->Click(kNotificationId1, NotificationButton::CANCEL);
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId1).has_value());
// Task info is removed when the task is cancelled.
EXPECT_FALSE(fpnm_->HasIOTask(kTaskId1));
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(2, 1)},
/*action_timedout_buckets=*/{});
}
// Tests that clicking the OK button on a warning notification shown for
// copy or move IO task with single warning file will proceed the task.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest, SingleFileOkProceeds_Warning) {
auto [type, action] = GetParam();
// CheckIfTransferAllowed will call FPNM to show the warning which will pause
// the IO task and trigger the notification.
ExpectCheckIfTransferAllowedToWarn(kTaskId1, action,
/*expected_should_proceed=*/true,
{base::FilePath("test1.txt")});
// Add the task.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
auto notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
const std::u16string title =
action == dlp::FileAction::kCopy
? l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_COPY_REVIEW_TITLE)
: l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_MOVE_REVIEW_TITLE);
// Proceed the warning.
EXPECT_EQ(notification->title(), title);
bridge_->Click(kNotificationId1, NotificationButton::OK);
// The warning notification should be closed or replaced by in progress one.
notification = bridge_->GetDisplayedNotification(kNotificationId1);
EXPECT_TRUE(!notification.has_value() || notification->title() != title);
// Wait till IO task is complete.
base::RunLoop().RunUntilIdle();
// Task info should be cleared because there's not any blocked file.
ASSERT_FALSE(fpnm_->HasIOTask(kTaskId1));
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 0);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(1, 1)},
/*action_timedout_buckets=*/{});
}
// Tests that clicking the OK button on an error notification shown for copy or
// move IO task with multiple blocked files shows a dialog, for which it opens
// the Files App only if there's not one opened already.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest,
MultiFileOKShowsDialogOverFilesApp_Error) {
auto [type, action] = GetParam();
DialogInfoMap dialog_info_map;
const std::vector<base::FilePath> paths = {base::FilePath("file1.txt"),
base::FilePath("file2.txt")};
auto dialog_info = FilesPolicyDialog::Info::Error(
FilesPolicyDialog::BlockReason::kDlp, paths);
dialog_info_map.insert({FilesPolicyDialog::BlockReason::kDlp, dialog_info});
EXPECT_CALL(*factory_,
CreateErrorDialog(dialog_info_map, action, testing::NotNull()))
.Times(2);
// No Files app opened.
ASSERT_FALSE(FindFilesApp());
// CheckIfTransferAllowed will call FPNM to save the blocked files. Once we
// complete the tasks with policy error, the file_manager::EventRouter will
// notify FPNM with the error status and trigger the notification. Do this
// before any Files App is opened so that we are sure we show system
// notifications.
ExpectCheckIfTransferAllowedToBlock(kTaskId1, action, paths);
ExpectCheckIfTransferAllowedToBlock(kTaskId2, action, paths);
// Add the tasks.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId2, type,
temp_dir_.GetPath(), "test2.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId2));
// Wait till IO tasks are complete.
base::RunLoop().RunUntilIdle();
// Task Info shouldn't be removed after completion.
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId2));
auto notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
const std::u16string title = action == dlp::FileAction::kCopy
? u"2 files blocked from copying"
: u"2 files blocked from moving";
EXPECT_EQ(notification->title(), title);
notification = bridge_->GetDisplayedNotification(kNotificationId2);
ASSERT_TRUE(notification.has_value());
EXPECT_EQ(notification->title(), title);
// Show the first dialog.
bridge_->Click(kNotificationId1, NotificationButton::OK);
// Check that a new Files app is opened.
Browser* first_app = ui_test_utils::WaitForBrowserToOpen();
ASSERT_TRUE(first_app);
ASSERT_EQ(first_app, FindFilesApp());
// Task info is removed after the dialog is shown.
EXPECT_FALSE(fpnm_->HasIOTask(kTaskId1));
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId1).has_value());
// Show the second dialog. No new app should be opened.
bridge_->Click(kNotificationId2, NotificationButton::OK);
// Check that the last active Files app is the same as before.
ASSERT_TRUE(first_app);
ASSERT_EQ(first_app, FindFilesApp());
// Task info is removed after the dialog is shown.
EXPECT_FALSE(fpnm_->HasIOTask(kTaskId2));
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
false, 1);
histogram_tester_.ExpectBucketCount(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesAppOpenTimedOutUMA,
true, 0);
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 2)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(2, 2)));
}
// Tests that the IO task info for copy or move with multiple blocked files will
// be removed upon clicking the DISMISS button on the error notification.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest, MultiFileDismissRemovesIOInfo_Error) {
auto [type, action] = GetParam();
// CheckIfTransferAllowed will call FPNM to save the blocked files. Once we
// complete the tasks with policy error, the file_manager::EventRouter will
// notify FPNM with the error status and trigger the notification.
ExpectCheckIfTransferAllowedToBlock(
kTaskId1, action,
{base::FilePath("file1.txt"), base::FilePath("file2.txt")});
// Add the task.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
// Wait till IO tasks are complete.
base::RunLoop().RunUntilIdle();
// Task Info shouldn't be removed after completion.
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
auto notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
const std::u16string title = action == dlp::FileAction::kCopy
? u"2 files blocked from copying"
: u"2 files blocked from moving";
EXPECT_EQ(notification->title(), title);
// Dismiss the notification.
bridge_->Click(kNotificationId1, NotificationButton::CANCEL);
// Task info is removed after the notification is dismissed.
EXPECT_FALSE(fpnm_->HasIOTask(kTaskId1));
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId1).has_value());
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(2, 1)));
}
// Tests that the IO task info for copy or move with single blocked file will
// be removed after the error notification is clicked.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest,
SingleFileNotificationRemovesIOInfo_Error) {
auto [type, action] = GetParam();
// CheckIfTransferAllowed will call FPNM to save the blocked files. Once we
// complete the tasks with policy error, the file_manager::EventRouter will
// notify FPNM with the error status and trigger the notification.
ExpectCheckIfTransferAllowedToBlock(kTaskId1, action,
{base::FilePath("test1.txt")});
// Add the task.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
// Wait till IO task is complete.
base::RunLoop().RunUntilIdle();
// Task Info shouldn't be removed after completion.
EXPECT_TRUE(fpnm_->HasIOTask(kTaskId1));
auto notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
const std::u16string title = action == dlp::FileAction::kCopy
? u"File blocked from copying"
: u"File blocked from moving";
EXPECT_EQ(notification->title(), title);
EXPECT_NE(
browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec(),
dlp::kDlpLearnMoreUrl);
// Click Learn more.
bridge_->Click(kNotificationId1, NotificationButton::OK);
EXPECT_EQ(
browser()->tab_strip_model()->GetActiveWebContents()->GetURL().spec(),
dlp::kDlpLearnMoreUrl);
// Task info is removed after the notification is clicked.
EXPECT_FALSE(fpnm_->HasIOTask(kTaskId1));
// The notification should be closed.
EXPECT_FALSE(bridge_->GetDisplayedNotification(kNotificationId1).has_value());
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(1, 1)));
}
// Tests that clicking the OK button on a warning notification shown for
// copy or move IO task with single warning file and a single blocked file will
// proceed the task but a block notification will appear in the end for the
// blocked file.
IN_PROC_BROWSER_TEST_P(IOTaskBrowserTest, SingleFileOkProceeds_Mix) {
auto [type, action] = GetParam();
// CheckIfTransferAllowed will call FPNM to show the warning which will pause
// the IO task and trigger the notification.
ExpectCheckIfTransferAllowedToWarnAndBlock(kTaskId1, action,
/*expected_should_proceed=*/true,
{base::FilePath("file1.txt")},
{base::FilePath("file2.txt")});
// Add the task.
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_FALSE(policy::AddCopyOrMoveIOTask(
browser()->profile(), file_system_context_, kTaskId1, type,
temp_dir_.GetPath(), "test1.txt", kTestStorageKey)
.empty());
}
ASSERT_TRUE(fpnm_->HasIOTask(kTaskId1));
auto notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
const std::u16string title1 =
action == dlp::FileAction::kCopy
? l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_COPY_REVIEW_TITLE)
: l10n_util::GetStringUTF16(IDS_POLICY_DLP_FILES_MOVE_REVIEW_TITLE);
EXPECT_EQ(notification->title(), title1);
// Proceed the warning.
bridge_->Click(kNotificationId1, NotificationButton::OK);
// The warning notification should be closed or replaced by in progress one.
notification = bridge_->GetDisplayedNotification(kNotificationId1);
EXPECT_TRUE(!notification.has_value() || notification->title() != title1);
// Wait till IO task is complete.
base::RunLoop().RunUntilIdle();
// Task info should be cleared because there's one blocked file.
EXPECT_TRUE(fpnm_->HasIOTask(kTaskId1));
// Error notification.
const std::u16string title2 = action == dlp::FileAction::kCopy
? u"File blocked from copying"
: u"File blocked from moving";
notification = bridge_->GetDisplayedNotification(kNotificationId1);
ASSERT_TRUE(notification.has_value());
EXPECT_EQ(notification->title(), title2);
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
std::string(data_controls::dlp::kFileActionBlockedUMA)),
base::BucketsAre(base::Bucket(action, 1)));
EXPECT_THAT(histogram_tester_.GetAllSamples(
data_controls::GetDlpHistogramPrefix() +
data_controls::dlp::kFilesBlockedCountUMA),
testing::ElementsAre(base::Bucket(1, 1)));
VerifyFilesWarningUMAs(histogram_tester_,
/*action_warned_buckets=*/{base::Bucket(action, 1)},
/*warning_count_buckets=*/{base::Bucket(1, 1)},
/*action_timedout_buckets=*/{});
}
INSTANTIATE_TEST_SUITE_P(
FPNM,
IOTaskBrowserTest,
::testing::Values(
std::make_tuple(file_manager::io_task::OperationType::kCopy,
dlp::FileAction::kCopy),
std::make_tuple(file_manager::io_task::OperationType::kMove,
dlp::FileAction::kMove)));
} // namespace policy
|