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 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/feature_list.h"
#include "base/memory/ref_counted.h"
#include "base/strings/stringprintf.h"
#include "base/strings/to_string.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/run_until.h"
#include "chrome/browser/extensions/api/side_panel/side_panel_api.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_context_menu_model.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/extensions/extension_action_test_helper.h"
#include "chrome/browser/ui/extensions/extensions_container.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/tabs/tab_model.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
#include "chrome/browser/ui/views/extensions/extensions_toolbar_container.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/side_panel/extensions/extension_side_panel_coordinator.h"
#include "chrome/browser/ui/views/side_panel/extensions/extension_side_panel_manager.h"
#include "chrome/browser/ui/views/side_panel/side_panel.h"
#include "chrome/browser/ui/views/side_panel/side_panel_coordinator.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry_observer.h"
#include "chrome/browser/ui/views/side_panel/side_panel_registry.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/crx_file/id_util.h"
#include "components/sessions/content/session_tab_helper.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/test_image_loader.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_builder.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/test_extension_dir.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/actions/actions.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/image/image_unittest_util.h"
namespace extensions {
namespace {
enum class CommandState {
kAbsent, // The command is not present in the menu.
kEnabled, // The command is present, and enabled.
kDisabled, // The command is present, and disabled.
};
CommandState GetCommandState(const ExtensionContextMenuModel& menu,
int command_id) {
bool is_present = menu.GetIndexOfCommandId(command_id).has_value();
bool is_visible = menu.IsCommandIdVisible(command_id);
// The command is absent if the menu entry is not present, or the entry is
// not visible.
if (!is_present || !is_visible) {
return CommandState::kAbsent;
}
bool is_enabled = menu.IsCommandIdEnabled(command_id);
if (!is_enabled) {
return CommandState::kDisabled;
}
return CommandState::kEnabled;
}
SidePanelEntry::Key GetKey(const ExtensionId& id) {
return SidePanelEntry::Key(SidePanelEntry::Id::kExtension, id);
}
// A class which waits on various SidePanelEntryObserver events.
class TestSidePanelEntryWaiter : public SidePanelEntryObserver {
public:
explicit TestSidePanelEntryWaiter(SidePanelEntry* entry) {
side_panel_entry_observation_.Observe(entry);
}
~TestSidePanelEntryWaiter() override = default;
TestSidePanelEntryWaiter(const TestSidePanelEntryWaiter& other) = delete;
TestSidePanelEntryWaiter& operator=(const TestSidePanelEntryWaiter& other) =
delete;
void WaitForEntryShown() { entry_shown_run_loop_.Run(); }
void WaitForEntryHidden() { entry_hidden_run_loop_.Run(); }
private:
void OnEntryShown(SidePanelEntry* entry) override {
entry_shown_run_loop_.QuitWhenIdle();
}
void OnEntryHidden(SidePanelEntry* entry) override {
entry_hidden_run_loop_.QuitWhenIdle();
}
base::RunLoop entry_shown_run_loop_;
base::RunLoop entry_hidden_run_loop_;
base::ScopedObservation<SidePanelEntry, SidePanelEntryObserver>
side_panel_entry_observation_{this};
};
// A class which waits for an extension's SidePanelEntry to be registered and/or
// deregistered.
class ExtensionSidePanelRegistryWaiter {
public:
ExtensionSidePanelRegistryWaiter(SidePanelRegistry* registry,
const ExtensionId& extension_id)
: registry_(registry), extension_id_(extension_id) {}
~ExtensionSidePanelRegistryWaiter() = default;
ExtensionSidePanelRegistryWaiter(
const ExtensionSidePanelRegistryWaiter& other) = delete;
ExtensionSidePanelRegistryWaiter& operator=(
const ExtensionSidePanelRegistryWaiter& other) = delete;
SidePanelEntry::Key GetKey() {
return SidePanelEntry::Key(SidePanelEntry::Id::kExtension, extension_id_);
}
// Waits until the entry for `extension_id_` is registered.
void WaitForRegistration() {
ASSERT_TRUE(base::test::RunUntil(
[&]() { return registry_->GetEntryForKey(GetKey()); }));
}
// Waits until the entry for `extension_id_` is deregistered.
void WaitForDeregistration() {
ASSERT_TRUE(base::test::RunUntil(
[&]() { return !registry_->GetEntryForKey(GetKey()); }));
}
private:
raw_ptr<SidePanelRegistry> registry_;
ExtensionId extension_id_;
};
class ExtensionSidePanelBrowserTest : public ExtensionBrowserTest {
protected:
int GetCurrentTabId() {
return ExtensionTabUtil::GetTabId(
browser()->tab_strip_model()->GetActiveWebContents());
}
SidePanelRegistry* GetCurrentTabRegistry() {
return SidePanelRegistry::GetDeprecated(
browser()->tab_strip_model()->GetActiveWebContents());
}
void OpenNewForegroundTab() {
int tab_count = browser()->tab_strip_model()->count();
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL("http://example.com"),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
ASSERT_EQ(tab_count + 1, browser()->tab_strip_model()->count());
}
// Calls chrome.sidePanel.setOptions() for the given `extension`, `path` and
// `enabled` and returns when the API call is complete.
void RunSetOptions(const Extension& extension,
std::optional<int> tab_id,
std::optional<std::string> path,
bool enabled) {
auto function = base::MakeRefCounted<SidePanelSetOptionsFunction>();
function->set_extension(&extension);
std::string tab_id_arg =
tab_id.has_value() ? base::StringPrintf(R"("tabId":%d,)", *tab_id) : "";
std::string path_arg =
path.has_value() ? base::StringPrintf(R"("path":"%s",)", path->c_str())
: "";
std::string args =
base::StringPrintf(R"([{%s%s"enabled":%s}])", tab_id_arg.c_str(),
path_arg.c_str(), base::ToString(enabled));
EXPECT_TRUE(api_test_utils::RunFunction(function.get(), args, profile()))
<< function->GetError();
}
// Calls chrome.sidePanel.setPanelBehavior() for the given `extension` and
// `openPanelOnActionClick`, and returns when the API call is complete.
void RunSetPanelBehavior(const Extension& extension,
bool openPanelOnActionClick) {
auto function = base::MakeRefCounted<SidePanelSetPanelBehaviorFunction>();
function->set_extension(&extension);
std::string args =
base::StringPrintf(R"([{"openPanelOnActionClick":%s}])",
base::ToString(openPanelOnActionClick));
EXPECT_TRUE(api_test_utils::RunFunction(function.get(), args, profile()))
<< function->GetError();
}
// Shows a side panel entry and waits for the entry to be shown.
void ShowEntryAndWait(SidePanelRegistry& registry,
const SidePanelEntry::Key& key) {
TestSidePanelEntryWaiter extension_entry_waiter(
registry.GetEntryForKey(key));
side_panel_coordinator()->Show(key);
extension_entry_waiter.WaitForEntryShown();
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
void ShowEntryAndWait(const SidePanelEntry::Key& key) {
ShowEntryAndWait(*global_registry(), key);
}
// Displays the contextual entry correspodning to `key` in the currently-
// active tab.
void ShowContextualEntryAndWait(const SidePanelEntry::Key& key) {
ShowEntryAndWait(*SidePanelRegistry::GetDeprecated(
browser()->tab_strip_model()->GetActiveWebContents()),
key);
}
actions::ActionItem* GetActionItemForExtension(
const extensions::Extension* extension,
BrowserActions* browser_actions) {
std::optional<actions::ActionId> extension_action_id =
actions::ActionIdMap::StringToActionId(
GetKey(extension->id()).ToString());
EXPECT_TRUE(extension_action_id.has_value());
actions::ActionItem* action_item = actions::ActionManager::Get().FindAction(
extension_action_id.value(), browser_actions->root_action_item());
return action_item;
}
ExtensionsToolbarContainer* GetExtensionsToolbarContainer() const {
return browser()->GetBrowserView().toolbar()->extensions_container();
}
void WaitForSidePanelToolbarCloseButtonVisibility(bool visible) {
auto* container = GetExtensionsToolbarContainer();
auto* button = container->GetCloseSidePanelButtonForTesting();
if (visible == false && !container->GetVisible()) {
return;
}
if (button->GetVisible() == visible) {
return;
}
base::RunLoop run_loop;
auto button_subscription =
button->AddVisibleChangedCallback(run_loop.QuitClosure());
auto container_subscription =
container->AddVisibleChangedCallback(run_loop.QuitClosure());
run_loop.Run();
bool is_visible = button->GetVisible() && container->GetVisible();
EXPECT_EQ(visible, is_visible);
}
void WaitForSidePanelClose() {
ASSERT_TRUE(base::test::RunUntil([&]() {
return browser()->GetBrowserView().unified_side_panel()->state() ==
SidePanel::State::kClosed;
}));
}
extensions::ExtensionContextMenuModel* GetContextMenuForExtension(
const ExtensionId& extension_id) {
return static_cast<extensions::ExtensionContextMenuModel*>(
GetExtensionsToolbarContainer()
->GetActionForId(extension_id)
->GetContextMenu(extensions::ExtensionContextMenuModel::
ContextMenuSource::kMenuItem));
}
ExtensionSidePanelCoordinator* GetCoordinator(
const ExtensionId& extension_id,
content::WebContents* web_contents) {
auto* manager =
web_contents ? tabs::TabInterface::GetFromContents(web_contents)
->GetTabFeatures()
->extension_side_panel_manager()
: browser()->GetFeatures().extension_side_panel_manager();
return manager->GetExtensionCoordinatorForTesting(extension_id);
}
// Runs a script in the extension's side panel WebContents to retrieve the
// value of document.sidePanelTemp.
std::string GetGlobalVariableInExtensionSidePanel(
const ExtensionId& extension_id,
content::WebContents* web_contents) {
auto* extension_coordinator = GetCoordinator(extension_id, web_contents);
static constexpr char kScript[] = R"(
document.sidePanelTemp ? document.sidePanelTemp : 'undefined';
)";
return content::EvalJs(
extension_coordinator->GetHostWebContentsForTesting(), kScript)
.ExtractString();
}
// Runs a script in the extension's side panel WebContents to set the value of
// document.sidePanelTemp to `value`.
void SetGlobalVariableInExtensionSidePanel(const ExtensionId& extension_id,
content::WebContents* web_contents,
const std::string& value) {
auto* extension_coordinator = GetCoordinator(extension_id, web_contents);
std::string script =
base::StringPrintf(R"(document.sidePanelTemp = "%s";)", value.c_str());
ASSERT_TRUE(content::ExecJs(
extension_coordinator->GetHostWebContentsForTesting(), script.c_str()));
}
SidePanelRegistry* global_registry() {
return browser()
->GetFeatures()
.side_panel_coordinator()
->GetWindowRegistry();
}
SidePanelCoordinator* side_panel_coordinator() {
return browser()->GetFeatures().side_panel_coordinator();
}
SidePanelCoordinator* side_panel_coordinator(Browser* browser) {
return browser->GetFeatures().side_panel_coordinator();
}
};
// Test that only extensions with side panel content will have a SidePanelEntry
// registered.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
ExtensionEntryVisibleInSidePanel) {
// Load two extensions: one with a side panel entry in its manifest and one
// without.
scoped_refptr<const extensions::Extension> no_side_panel_extension =
LoadExtension(test_data_dir_.AppendASCII("common/background_script"));
ASSERT_TRUE(no_side_panel_extension);
scoped_refptr<const extensions::Extension> side_panel_extension =
LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(side_panel_extension);
// Check if ActionItem is created.
BrowserActions* browser_actions = browser()->browser_actions();
actions::ActionItem* action_item =
GetActionItemForExtension(side_panel_extension.get(), browser_actions);
EXPECT_EQ(action_item->GetText(),
base::UTF8ToUTF16(side_panel_extension->short_name()));
EXPECT_FALSE(action_item->GetImage().IsEmpty());
std::optional<actions::ActionId> no_side_panel_extension_action_id =
actions::ActionIdMap::StringToActionId(
GetKey(no_side_panel_extension->id()).ToString());
EXPECT_FALSE(no_side_panel_extension_action_id.has_value());
// Check that only the extension with the side panel entry in its manifest is
// shown as an entry in the global side panel registry.
EXPECT_TRUE(global_registry()->GetEntryForKey(SidePanelEntry::Key(
SidePanelEntry::Id::kExtension, side_panel_extension->id())));
EXPECT_FALSE(global_registry()->GetEntryForKey(SidePanelEntry::Key(
SidePanelEntry::Id::kExtension, no_side_panel_extension->id())));
// Unloading the extension should remove it from the registry.
UnloadExtension(side_panel_extension->id());
EXPECT_FALSE(global_registry()->GetEntryForKey(SidePanelEntry::Key(
SidePanelEntry::Id::kExtension, side_panel_extension->id())));
// Check if ActionItem is deleted.
action_item =
GetActionItemForExtension(side_panel_extension.get(), browser_actions);
EXPECT_FALSE(action_item);
// The other ActionItems should not be deleted.
EXPECT_GE(
browser_actions->root_action_item()->GetChildren().children().size(),
1UL);
}
// Test that an extension's view is shown/behaves correctly in the side panel.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
ExtensionViewVisibleInsideSidePanel) {
ExtensionTestMessageListener default_path_listener("default_path");
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
SidePanelEntry* extension_entry =
global_registry()->GetEntryForKey(extension_key);
ASSERT_TRUE(extension_entry);
// The key for the extension should be registered, but the side panel isn't
// shown yet.
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
side_panel_coordinator()->Show(extension_key);
// Wait until the view in the side panel is active by listening for the
// message sent from the view's script.
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
// Reset the `default_path_listener`.
default_path_listener.Reset();
// Close and reopen the side panel. The extension's view should be recreated.
side_panel_coordinator()->Close();
WaitForSidePanelClose();
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
// Now unload the extension. The key should no longer exist in the global
// registry and the side panel should close as a result.
UnloadExtension(extension->id());
WaitForSidePanelClose();
EXPECT_FALSE(global_registry()->GetEntryForKey(extension_key));
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
}
// Test that an extension's SidePanelEntry is registered for new browser
// windows.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest, MultipleBrowsers) {
// Load an extension and verify that its SidePanelEntry is registered.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
BrowserActions* browser_actions = browser()->browser_actions();
actions::ActionItem* browser_one_action_item =
GetActionItemForExtension(extension.get(), browser_actions);
EXPECT_EQ(browser_one_action_item->GetText(),
base::UTF8ToUTF16(extension->short_name()));
// Open a new browser window. The extension's SidePanelEntry should also be
// registered for the new window's global SidePanelRegistry.
Browser* second_browser = CreateBrowser(browser()->profile());
BrowserActions* browser_actions_second_browser =
second_browser->browser_actions();
SidePanelRegistry* second_global_registry = second_browser->GetFeatures()
.side_panel_coordinator()
->GetWindowRegistry();
EXPECT_TRUE(second_global_registry->GetEntryForKey(extension_key));
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
actions::ActionItem* browser_two_action_item = GetActionItemForExtension(
extension.get(), browser_actions_second_browser);
// Validate the state of the action items are still correct.
EXPECT_EQ(browser_one_action_item->GetText(),
base::UTF8ToUTF16(extension->short_name()));
EXPECT_EQ(browser_two_action_item->GetText(),
base::UTF8ToUTF16(extension->short_name()));
// Unloading the extension should remove it from the registry.
UnloadExtension(extension->id());
EXPECT_FALSE(global_registry()->GetEntryForKey(
SidePanelEntry::Key(SidePanelEntry::Id::kExtension, extension->id())));
EXPECT_FALSE(second_browser->GetFeatures()
.side_panel_coordinator()
->GetWindowRegistry()
->GetEntryForKey(SidePanelEntry::Key(
SidePanelEntry::Id::kExtension, extension->id())));
browser_one_action_item =
GetActionItemForExtension(extension.get(), browser_actions);
browser_two_action_item = GetActionItemForExtension(
extension.get(), browser_actions_second_browser);
EXPECT_FALSE(browser_one_action_item);
EXPECT_FALSE(browser_two_action_item);
}
// Test that if the side panel is closed while the extension's side panel view
// is still loading, there will not be a crash. Regression for
// crbug.com/1403168.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest, SidePanelQuicklyClosed) {
// Load an extension and verify that its SidePanelEntry is registered.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Quickly open the side panel showing the extension's side panel entry then
// close it. The test should not cause any crashes after it is complete.
side_panel_coordinator()->Show(extension_key);
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
side_panel_coordinator()->Close();
}
// Test that the extension's side panel entry shows the extension's icon.
// TODO(crbug.com/40915500): Re-enable this test
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#define MAYBE_EntryShowsExtensionIcon DISABLED_EntryShowsExtensionIcon
#else
#define MAYBE_EntryShowsExtensionIcon EntryShowsExtensionIcon
#endif
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
MAYBE_EntryShowsExtensionIcon) {
// Load an extension and verify that its SidePanelEntry is registered.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
BrowserActions* browser_actions = browser()->browser_actions();
actions::ActionItem* action_item =
GetActionItemForExtension(extension.get(), browser_actions);
// Check that the entry's icon bitmap is identical to the bitmap of the
// extension's icon scaled down to `extension_misc::EXTENSION_ICON_BITTY`.
SkBitmap expected_icon_bitmap = TestImageLoader::LoadAndGetExtensionBitmap(
extension.get(), "icon.png", extension_misc::EXTENSION_ICON_BITTY);
const SkBitmap& actual_icon_bitmap =
*action_item->GetImage().GetImage().ToSkBitmap();
EXPECT_TRUE(
gfx::test::AreBitmapsEqual(expected_icon_bitmap, actual_icon_bitmap));
}
// Test that sidePanel.setOptions() will register and deregister the extension's
// SidePanelEntry when called with enabled: true/false.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest, SetOptions_Enabled) {
ExtensionTestMessageListener panel_2_listener("panel_2");
// Load an extension without a default side panel path.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/setoptions"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_FALSE(global_registry()->GetEntryForKey(extension_key));
{
// Call setOptions({enabled: true}) and wait for the extension's
// SidePanelEntry to be registered.
ExtensionSidePanelRegistryWaiter waiter(global_registry(), extension->id());
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel_1.html",
/*enabled=*/true);
waiter.WaitForRegistration();
}
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
{
// Call setOptions({enabled: false}) and wait for the extension's
// SidePanelEntry to be deregistered.
ExtensionSidePanelRegistryWaiter waiter(global_registry(), extension->id());
RunSetOptions(*extension, /*tab_id=*/std::nullopt, /*path=*/std::nullopt,
/*enabled=*/false);
waiter.WaitForDeregistration();
}
EXPECT_FALSE(global_registry()->GetEntryForKey(extension_key));
{
// Sanity check that re-enabling the side panel will register the entry
// again and a view with the new side panel path can be shown.
ExtensionSidePanelRegistryWaiter waiter(global_registry(), extension->id());
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel_2.html",
/*enabled=*/true);
waiter.WaitForRegistration();
}
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
side_panel_coordinator()->Show(extension_key);
// Wait until the view in the side panel is active by listening for the
// message sent from the view's script.
ASSERT_TRUE(panel_2_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
{
// Calling setOptions({enabled: false}) when the extension's SidePanelEntry
// is shown should close the side panel.
ExtensionSidePanelRegistryWaiter waiter(global_registry(), extension->id());
RunSetOptions(*extension, /*tab_id=*/std::nullopt, /*path=*/std::nullopt,
/*enabled=*/false);
waiter.WaitForDeregistration();
WaitForSidePanelClose();
}
EXPECT_FALSE(global_registry()->GetEntryForKey(extension_key));
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
}
// Test that sidePanel.setOptions() will change what is shown in the extension's
// SidePanelEntry's view when called with different paths.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest, SetOptions_Path) {
ExtensionTestMessageListener default_path_listener("default_path");
ExtensionTestMessageListener panel_1_listener("panel_1");
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
auto* extension_coordinator =
GetCoordinator(extension->id(), /*web_contents=*/nullptr);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
// Check that the extension's side panel view shows the most recently set
// path.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel_1.html",
/*enabled=*/true);
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(panel_1_listener.WaitUntilSatisfied());
EXPECT_FALSE(default_path_listener.was_satisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
// Check that changing the path while the view is active will cause the view
// to navigate to the new path.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "default_path.html",
/*enabled=*/true);
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
// Switch to the reading list in the side panel and check that the extension
// view is cached (i.e. the view exists but is not shown, and its web contents
// still exists).
ShowEntryAndWait(SidePanelEntry::Key(SidePanelEntry::Id::kReadingList));
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key)->CachedView());
panel_1_listener.Reset();
content::WebContentsDestroyedWatcher destroyed_watcher(
extension_coordinator->GetHostWebContentsForTesting());
// Test calling setOptions with a different path when the extension's view is
// cached. The cached view should then be invalidated and its web contents are
// destroyed.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel_1.html",
/*enabled=*/true);
destroyed_watcher.Wait();
// When the extension's entry is shown again, the view with the updated path
// should be active.
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(panel_1_listener.WaitUntilSatisfied());
}
// Test that sidePanel.setOptions() can be called with an HTTP/HTTPS URL.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest, SetOptions_Url) {
const auto kExtensionDir =
test_data_dir_.AppendASCII("api_test/side_panel/simple_default");
embedded_test_server()->ServeFilesFromDirectory(kExtensionDir);
auto test_server_handle = embedded_test_server()->StartAndReturnHandle();
ASSERT_TRUE(test_server_handle);
const GURL kPanelUrl = embedded_test_server()->GetURL("/panel_dom.html");
scoped_refptr<const extensions::Extension> extension =
LoadExtension(kExtensionDir);
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
// Test calling setOptions with an HTTP/HTTPS URL works.
content::DOMMessageQueue message_queue;
RunSetOptions(*extension, /*tab_id=*/std::nullopt, kPanelUrl.spec(),
/*enabled=*/true);
side_panel_coordinator()->Show(extension_key);
// Note: We use DOMMessageQueue here because since this isn't an extension
// page, it doesn't have access to any chrome.* APIs, including chrome.test.
std::string message;
ASSERT_TRUE(message_queue.WaitForMessage(&message));
EXPECT_EQ("\"panel_dom\"", message);
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
// Test that calling window.close() from an extension side panel deletes the
// panel's web contents and closes the extension's side panel if it's also
// shown.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest, WindowCloseCalled) {
// Install an extension and show its side panel.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
{
ExtensionTestMessageListener default_path_listener("default_path");
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
auto* extension_coordinator =
GetCoordinator(extension->id(), /*web_contents=*/nullptr);
// Call window.close() from the extension's side panel page and wait for the
// web contents to be destroyed.
{
content::WebContentsDestroyedWatcher destroyed_watcher(
extension_coordinator->GetHostWebContentsForTesting());
ASSERT_TRUE(
content::ExecJs(extension_coordinator->GetHostWebContentsForTesting(),
"window.close();"));
destroyed_watcher.Wait();
}
// The side panel should now be closed.
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Show the extension's side panel again.
ExtensionTestMessageListener default_path_listener("default_path");
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
// Show another side panel type so the extension's panel's view gets cached.
ShowEntryAndWait(SidePanelEntry::Key(SidePanelEntry::Id::kReadingList));
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key)->CachedView());
// Calling window.close() from within the panel should invalidate the cached
// view when the extension panel is not shown.
content::WebContentsDestroyedWatcher destroyed_watcher(
extension_coordinator->GetHostWebContentsForTesting());
ASSERT_TRUE(
content::ExecJs(extension_coordinator->GetHostWebContentsForTesting(),
"window.close();"));
destroyed_watcher.Wait();
// The side panel should be open because the reading list entry is still
// shown.
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
// Test that calling window.close() from an extension's side panel deletes the
// panel's web contents and closes the extension's side panel if it's also
// shown.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
WindowCloseCalledFromTabSpecificPanel) {
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/setoptions"));
ASSERT_TRUE(extension);
content::WebContents* active_web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
SidePanelEntry::Key extension_key = GetKey(extension->id());
// Call setOptions({enabled: true}) with a tab ID and new path, and wait for
// the extension's SidePanelEntry to be registered.
ExtensionSidePanelRegistryWaiter waiter(
SidePanelRegistry::GetDeprecated(active_web_contents), extension->id());
RunSetOptions(*extension, GetCurrentTabId(), "panel_2.html",
/*enabled=*/true);
waiter.WaitForRegistration();
ExtensionTestMessageListener panel_2_listener("panel_2");
side_panel_coordinator()->Show(extension_key);
// Wait until the view in the side panel is active by listening for the
// message sent from the view's script.
ASSERT_TRUE(panel_2_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
auto* extension_coordinator =
GetCoordinator(extension->id(), active_web_contents);
content::WebContentsDestroyedWatcher destroyed_watcher(
extension_coordinator->GetHostWebContentsForTesting());
ASSERT_TRUE(
content::ExecJs(extension_coordinator->GetHostWebContentsForTesting(),
"window.close();"));
destroyed_watcher.Wait();
}
// Test that calling window.close() from an extension side panel when it is
// shown closes the side panel even if another entry is loading and will be
// shown.
// TODO(crbug.com/347643170) Test is flaky.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
DISABLED_WindowCloseCalledWhenLoading) {
// Install an extension and show its side panel.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
{
ExtensionTestMessageListener default_path_listener("default_path");
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
auto* extension_coordinator =
GetCoordinator(extension->id(), /*web_contents=*/nullptr);
// Start showing another entry and call window.close() from the extension's
// side panel page while the other entry is still loading but not shown. The
// extension's side panel web content should still be destroyed and the side
// panel will close.
{
side_panel_coordinator()->Show(SidePanelEntry::Id::kReadingList);
content::WebContentsDestroyedWatcher destroyed_watcher(
extension_coordinator->GetHostWebContentsForTesting());
ASSERT_TRUE(
content::ExecJs(extension_coordinator->GetHostWebContentsForTesting(),
"window.close();"));
destroyed_watcher.Wait();
}
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
}
// Tests that global options are not affected by tab options.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
GlobalOptionsUnaffectedByTabOptions) {
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
// Global side panel is enabled by default.
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
// Register a tab side panel.
int tab_id = GetCurrentTabId();
ExtensionSidePanelRegistryWaiter waiter(GetCurrentTabRegistry(),
extension->id());
RunSetOptions(*extension, tab_id, "default_path.html",
/*enabled=*/true);
waiter.WaitForRegistration();
EXPECT_TRUE(GetCurrentTabRegistry()->GetEntryForKey(extension_key));
// Show the extension side panel. This should show the tab-scoped side panel.
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return side_panel_coordinator()->IsSidePanelShowing(); }));
EXPECT_TRUE(side_panel_coordinator()->current_key()->tab_handle);
// Disable the extension's side panel for the current tab.
RunSetOptions(*extension, tab_id, /*path=*/std::nullopt,
/*enabled=*/false);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return !side_panel_coordinator()->IsSidePanelShowing(); }));
// The global panel should still be registered, even though the side panel
// isn't showing.
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
// The global side panel can be shown even though the tab-side panel is
// disabled.
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return side_panel_coordinator()->IsSidePanelShowing(); }));
// Calling setOptions({enabled: false}) should deregister the global entry and
// hide the side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, /*path=*/std::nullopt,
/*enabled=*/false);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return !global_registry()->GetEntryForKey(extension_key); }));
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
}
// Test that when switching tabs, the new tab shows the extension's contextual
// entry if one exists, or the global entry if there is no tab-specific entry
// specified for that tab.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
ShowTabSpecificPaneOnTabSwitch) {
// Open a second tab and switch back to the first tab.
OpenNewForegroundTab();
ASSERT_TRUE(browser()->tab_strip_model()->IsTabSelected(1));
int second_tab_id = GetCurrentTabId();
browser()->tab_strip_model()->ActivateTabAt(0);
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
// Call setOptions for the second tab.
ExtensionSidePanelRegistryWaiter waiter(
SidePanelRegistry::GetDeprecated(
browser()->tab_strip_model()->GetWebContentsAt(1)),
extension->id());
RunSetOptions(*extension, second_tab_id, "panel_1.html",
/*enabled=*/true);
waiter.WaitForRegistration();
// Show the extension's side panel on the first tab.
ShowEntryAndWait(extension_key);
// Switch to the second tab: this should cause the extension's entry for that
// tab to be shown.
ExtensionTestMessageListener panel_1_listener("panel_1");
browser()->tab_strip_model()->ActivateTabAt(1);
ASSERT_TRUE(panel_1_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
// Switch back to the first tab: the global entry should be shown.
TestSidePanelEntryWaiter entry_shown_waiter(
global_registry()->GetEntryForKey(extension_key));
browser()->tab_strip_model()->ActivateTabAt(0);
entry_shown_waiter.WaitForEntryShown();
}
// Test that the view state between the extension's global side panel entry and
// all of its tab-specific side panel entries are independent of each other.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
TabSpecificPanelsOwnViewState) {
// Open a second tab and switch back to the first tab.
OpenNewForegroundTab();
ASSERT_TRUE(browser()->tab_strip_model()->IsTabSelected(1));
int second_tab_id = GetCurrentTabId();
browser()->tab_strip_model()->ActivateTabAt(0);
int first_tab_id = GetCurrentTabId();
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
// Set a local variable's value to "GLOBAL" for the extension's global side
// panel's WebContents.
SidePanelEntry::Key extension_key = GetKey(extension->id());
{
ExtensionTestMessageListener default_path_listener("default_path");
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
EXPECT_EQ("undefined", GetGlobalVariableInExtensionSidePanel(
extension->id(), /*web_contents=*/nullptr));
SetGlobalVariableInExtensionSidePanel(extension->id(),
/*web_contents=*/nullptr, "GLOBAL");
EXPECT_EQ("GLOBAL", GetGlobalVariableInExtensionSidePanel(
extension->id(), /*web_contents=*/nullptr));
auto* first_tab_contents = browser()->tab_strip_model()->GetWebContentsAt(0);
auto* second_tab_contents = browser()->tab_strip_model()->GetWebContentsAt(1);
{
// Set a local variable's value to "TAB 1" for the extension's side panel's
// view on the first tab.
ExtensionTestMessageListener default_path_listener("default_path");
SidePanelRegistry* first_tab_registry =
SidePanelRegistry::GetDeprecated(first_tab_contents);
ExtensionSidePanelRegistryWaiter waiter(first_tab_registry,
extension->id());
// Register and show the tab-scoped side panel.
RunSetOptions(*extension, first_tab_id, "default_path.html",
/*enabled=*/true);
waiter.WaitForRegistration();
side_panel_coordinator()->Show(
{browser()->GetActiveTabInterface()->GetHandle(), extension_key},
/*open_trigger=*/std::nullopt, /*suppress_animations=*/true);
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
// Despite sharing the same path, the instance of default_path.html that is
// specifically registered for `first_tab_id` is a different entry/view than
// default_path.html registered for all tabs.
EXPECT_EQ("undefined", GetGlobalVariableInExtensionSidePanel(
extension->id(), first_tab_contents));
SetGlobalVariableInExtensionSidePanel(extension->id(), first_tab_contents,
"TAB 1");
}
{
// Set a local variable's value to "TAB 2" for the extension's side panel's
// view on the second tab.
SidePanelRegistry* second_tab_registry =
SidePanelRegistry::GetDeprecated(second_tab_contents);
ExtensionSidePanelRegistryWaiter waiter(second_tab_registry,
extension->id());
RunSetOptions(*extension, second_tab_id, "default_path.html",
/*enabled=*/true);
waiter.WaitForRegistration();
TestSidePanelEntryWaiter entry_shown_waiter(
second_tab_registry->GetEntryForKey(extension_key));
browser()->tab_strip_model()->ActivateTabAt(1);
entry_shown_waiter.WaitForEntryShown();
EXPECT_EQ("undefined", GetGlobalVariableInExtensionSidePanel(
extension->id(), second_tab_contents));
SetGlobalVariableInExtensionSidePanel(extension->id(), second_tab_contents,
"TAB 2");
}
// Check that the global variable's value for the extension's global and
// contextual (first tab) entries are not affected.
EXPECT_EQ("GLOBAL", GetGlobalVariableInExtensionSidePanel(
extension->id(), /*web_contents=*/nullptr));
EXPECT_EQ("TAB 1", GetGlobalVariableInExtensionSidePanel(extension->id(),
first_tab_contents));
}
// Test that unloading an extension after its tab-specific side panel is moved
// to another browser does not crash. This tests a rare use case where the
// extension's contextual SidePanelEntry is deregistered before its global one,
// all while the extension itself is being unloaded. See
// ExtensionSidePanelCoordinator::CreateVIew for more details.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
UnloadExtensionAfterMovingTab) {
OpenNewForegroundTab();
ASSERT_TRUE(browser()->tab_strip_model()->IsTabSelected(1));
const tabs::TabInterface* second_tab =
browser()->tab_strip_model()->GetTabAtIndex(1);
ASSERT_TRUE(second_tab);
int second_tab_id = GetCurrentTabId();
// Load an extension and verify that its SidePanelEntry is registered.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
SidePanelEntry::Key extension_key = GetKey(extension->id());
EXPECT_TRUE(global_registry()->GetEntryForKey(extension_key));
{
// Register a SidePanelEntry for the extension for the second tab.
SidePanelRegistry* second_tab_registry =
SidePanelRegistry::GetDeprecated(second_tab->GetContents());
ExtensionSidePanelRegistryWaiter waiter(second_tab_registry,
extension->id());
RunSetOptions(*extension, second_tab_id, "panel_1.html",
/*enabled=*/true);
waiter.WaitForRegistration();
ExtensionTestMessageListener panel_1_listener("panel_1");
side_panel_coordinator()->Show(extension_key);
ASSERT_TRUE(panel_1_listener.WaitUntilSatisfied());
}
// Detach the second tab from `browser()`
std::unique_ptr<tabs::TabModel> detached_tab =
browser()->tab_strip_model()->DetachTabAtForInsertion(
/*index=*/1);
ASSERT_EQ(second_tab, detached_tab.get());
// Open a new browser window and add `detached_tab`.
Browser* second_browser = CreateBrowser(browser()->profile());
TabStripModel* target_tab_strip =
ExtensionTabUtil::GetEditableTabStripModel(second_browser);
target_tab_strip->InsertDetachedTabAt(
/*index=*/1, std::move(detached_tab), AddTabTypes::ADD_NONE);
// Switch to the newly moved tab.
ASSERT_EQ(2, second_browser->tab_strip_model()->count());
second_browser->tab_strip_model()->ActivateTabAt(1);
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
// Unloading the extension at this point should not crash the browser.
UnloadExtension(extension->id());
WaitForSidePanelClose();
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
}
// Test that when the openSidePanelOnClick pref is true, clicking the extension
// icon will show the extension's entry if it's not shown, or close
// the side panel if the extension's entry is shown.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
ToggleExtensionEntryOnUserAction) {
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
// Create a helper that will click the extension's icon from the menu to
// trigger an extension action.
std::unique_ptr<ExtensionActionTestHelper> action_helper =
ExtensionActionTestHelper::Create(browser());
SidePanelEntry::Key extension_key = GetKey(extension->id());
RunSetPanelBehavior(*extension, /*openPanelOnActionClick=*/true);
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
{
ExtensionTestMessageListener default_path_listener("default_path");
// Clicking the icon should show the extension's entry.
action_helper->Press(extension->id());
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
// Switch over to another side panel entry.
ShowEntryAndWait(SidePanelEntry::Key(SidePanelEntry::Id::kReadingList));
{
TestSidePanelEntryWaiter entry_shown_waiter(
global_registry()->GetEntryForKey(extension_key));
// Since the extension's entry is not shown, clicking the icon should show
// it.
action_helper->Press(extension->id());
entry_shown_waiter.WaitForEntryShown();
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
{
TestSidePanelEntryWaiter entry_hidden_waiter(
global_registry()->GetEntryForKey(extension_key));
// Clicking the icon when the extension's entry is shown should close the
// side panel.
action_helper->Press(extension->id());
entry_hidden_waiter.WaitForEntryHidden();
}
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
}
// Test that extension action behavior falls back to defaults if the extension
// has no side panel panel for the current tab (global or contextual) or if the
// openSidePanelOnClick pref is false.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
FallbackActionWithoutSidePanel) {
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/with_action_onclick"));
ASSERT_TRUE(extension);
// Create a helper that will click the extension's icon from the menu to
// trigger an extension action.
std::unique_ptr<ExtensionActionTestHelper> action_helper =
ExtensionActionTestHelper::Create(browser());
SidePanelEntry::Key extension_key = GetKey(extension->id());
RunSetPanelBehavior(*extension, /*openPanelOnActionClick=*/true);
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
{
ExtensionTestMessageListener default_path_listener("default_path");
// Clicking the icon should show the extension's entry.
action_helper->Press(extension->id());
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
// Set the pref to false.
RunSetPanelBehavior(*extension, /*openPanelOnActionClick=*/false);
{
ExtensionTestMessageListener action_clicked_listener("action_clicked");
// Since the pref is false, clicking the icon will fall back to triggering
// chrome.action.onClicked, which satisfies `action_clicked_listener`.
action_helper->Press(extension->id());
ASSERT_TRUE(action_clicked_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
}
// Set the pref to true but disable the extension's side panel.
RunSetPanelBehavior(*extension, /*openPanelOnActionClick=*/true);
RunSetOptions(*extension, /*tab_id=*/std::nullopt, /*path=*/std::nullopt,
/*enabled=*/false);
ASSERT_TRUE(base::test::RunUntil(
[&]() { return !global_registry()->GetEntryForKey(extension_key); }));
{
ExtensionTestMessageListener default_path_listener("default_path");
ExtensionTestMessageListener action_clicked_listener("action_clicked");
// Clicking the icon will fall back to triggering chrome.action.onClicked,
action_helper->Press(extension->id());
ASSERT_TRUE(action_clicked_listener.WaitUntilSatisfied());
EXPECT_FALSE(default_path_listener.was_satisfied());
}
}
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelBrowserTest,
CloseSidePanelButtonVisibleWhenExtensionsSidePanelOpen) {
ExtensionTestMessageListener default_path_listener("default_path");
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
// Check if ActionItem is created.
BrowserActions* browser_actions = browser()->browser_actions();
actions::ActionItem* action_item =
GetActionItemForExtension(extension.get(), browser_actions);
EXPECT_EQ(action_item->GetText(), base::UTF8ToUTF16(extension->short_name()));
EXPECT_FALSE(action_item->GetImage().IsEmpty());
SidePanelEntry::Key extension_key = GetKey(extension->id());
SidePanelEntry* extension_entry =
global_registry()->GetEntryForKey(extension_key);
ASSERT_TRUE(extension_entry);
// The key for the extension should be registered, but the side panel isn't
// shown yet and the close side panel button is not visible.
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
EXPECT_FALSE(GetExtensionsToolbarContainer()
->GetCloseSidePanelButtonForTesting()
->GetVisible());
side_panel_coordinator()->Show(extension_key);
// Wait until the view in the side panel is active by listening for the
// message sent from the view's script. Verify the close side panel button is
// visible.
ASSERT_TRUE(default_path_listener.WaitUntilSatisfied());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
EXPECT_TRUE(GetExtensionsToolbarContainer()
->GetCloseSidePanelButtonForTesting()
->GetVisible());
// Now unload the extension. The key should no longer exist in the global
// registry and the side panel should close as a result and the close side
// panel button should not be visible.
UnloadExtension(extension->id());
WaitForSidePanelClose();
EXPECT_FALSE(global_registry()->GetEntryForKey(extension_key));
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
WaitForSidePanelToolbarCloseButtonVisibility(false);
}
class ExtensionSidePanelPWABrowserTest : public ExtensionSidePanelBrowserTest {
public:
ExtensionSidePanelPWABrowserTest() = default;
~ExtensionSidePanelPWABrowserTest() override = default;
private:
web_app::OsIntegrationTestOverrideBlockingRegistration override_registration_;
};
// Tests that moving an extension side panel in a PWA app window to a normal
// browser window does not crash.
IN_PROC_BROWSER_TEST_F(ExtensionSidePanelPWABrowserTest, OpenInChrome) {
// Load side-panel extension.
scoped_refptr<const extensions::Extension> extension = LoadExtension(
test_data_dir_.AppendASCII("api_test/side_panel/simple_default"));
ASSERT_TRUE(extension);
// Make a PWA app window.
GURL example_url("https://example.com");
auto web_app_info =
web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(example_url);
web_app_info->user_display_mode =
web_app::mojom::UserDisplayMode::kStandalone;
web_app_info->title = u"A Web App";
webapps::AppId app_id =
web_app::test::InstallWebApp(profile(), std::move(web_app_info));
Browser* browser = web_app::LaunchWebAppBrowserAndWait(profile(), app_id);
SidePanelEntry::Key extension_key = GetKey(extension->id());
// Call setOptions({enabled: true}) with a tab ID and new path, and wait for
// the extension's SidePanelEntry to be registered. This simulates the
// extension registering a tab-specific side panel.
// This logic needs to be scoped so that waiter leaves scope before the call
// to chrome::OpenInChrome. This avoids a dangling reference to the
// side_panel_registry.
// Note that side-panel is not supported in PWAs. However, that does not
// prevent extensions from running and setting state, which will be carried
// over when the WebContents is moved to a normal browser window.
{
content::WebContents* web_contents =
browser->tab_strip_model()->GetActiveWebContents();
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
ExtensionSidePanelRegistryWaiter waiter(browser->GetActiveTabInterface()
->GetTabFeatures()
->side_panel_registry(),
extension->id());
RunSetOptions(*extension, tab_id, "panel_1.html",
/*enabled=*/true);
waiter.WaitForRegistration();
}
// Should not crash.
chrome::OpenInChrome(browser);
}
class ExtensionOpenSidePanelBrowserTest : public ExtensionSidePanelBrowserTest {
public:
ExtensionOpenSidePanelBrowserTest() = default;
~ExtensionOpenSidePanelBrowserTest() override = default;
protected:
// Loads up a stub side panel extension.
const Extension* LoadSidePanelExtension(bool allow_in_incognito = false,
bool split_mode = false) {
TestExtensionDir test_dir;
static constexpr char kManifest[] =
R"({
"name": "Side Panel Extension",
"manifest_version": 3,
"version": "0.1",
"permissions": ["sidePanel"],
"incognito" : "%s"
})";
test_dir.WriteManifest(
base::StringPrintf(kManifest, split_mode ? "split" : "spanning"));
test_dir.WriteFile(FILE_PATH_LITERAL("panel.html"), "<html>hello</html>");
const Extension* extension = LoadExtension(
test_dir.UnpackedPath(), {.allow_in_incognito = allow_in_incognito});
test_dirs_.push_back(std::move(test_dir));
return extension;
}
// Loads up a stub extension.
const Extension* LoadNoSidePanelExtension() {
TestExtensionDir test_dir;
static constexpr char kManifest[] =
R"({
"name": "No Side Panel Extension",
"manifest_version": 3,
"version": "0.1"
})";
test_dir.WriteManifest(kManifest);
const Extension* extension = LoadExtension(test_dir.UnpackedPath());
test_dirs_.push_back(std::move(test_dir));
return extension;
}
void RunOpenPanelForTab(const Extension& extension, int tab_id) {
RunOpenPanel(extension, tab_id, /*window_id=*/std::nullopt, profile());
}
void RunOpenPanelForWindow(const Extension& extension, int window_id) {
RunOpenPanel(extension, /*tab_id=*/std::nullopt, window_id, profile());
}
void RunOpenPanelForTabAndProfile(const Extension& extension,
int tab_id,
Profile* profile) {
RunOpenPanel(extension, tab_id, /*window_id=*/std::nullopt, profile);
}
void RunOpenPanelForWindowAndProfile(const Extension& extension,
int window_id,
Profile* profile) {
RunOpenPanel(extension, /*tab_id=*/std::nullopt, window_id, profile);
}
int GetCurrentWindowId() { return ExtensionTabUtil::GetWindowId(browser()); }
private:
void RunOpenPanel(const Extension& extension,
std::optional<int> tab_id,
std::optional<int> window_id,
Profile* profile) {
auto function = base::MakeRefCounted<SidePanelOpenFunction>();
function->set_extension(&extension);
base::Value::Dict options;
if (tab_id) {
options.Set("tabId", *tab_id);
}
if (window_id) {
options.Set("windowId", *window_id);
}
std::string args_str;
base::JSONWriter::Write(base::Value::List().Append(std::move(options)),
&args_str);
function->set_user_gesture(true);
EXPECT_TRUE(api_test_utils::RunFunction(function.get(), args_str, profile))
<< function->GetError();
}
std::vector<TestExtensionDir> test_dirs_;
};
// Tests that calling `sidePanel.open()` for an extension with a global panel
// registered opens the panel on the specified tab.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OpenGlobalPanelOnActiveTab) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Run `sidePanel.open()`. The panel should open.
RunOpenPanelForTab(*extension, GetCurrentTabId());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
}
// Tests that calling `sidePanel.open()` for an extension with a global panel
// registered opens the panel on the specified tab when using an incognito
// window. Regression test for https://crbug.com/329211590.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OpenGlobalPanelOnActiveTab_Incognito) {
const Extension* extension =
LoadSidePanelExtension(/*allow_in_incognito=*/true, /*split_mode=*/true);
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// For clarity sake, use a named reference to the non-incognito browser.
Browser* non_incognito_browser = browser();
// Open a tab in an incognito browser window to use.
Browser* incognito_browser =
OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
ASSERT_TRUE(incognito_browser);
int incognito_tab_id = ExtensionTabUtil::GetTabId(
incognito_browser->tab_strip_model()->GetActiveWebContents());
EXPECT_FALSE(side_panel_coordinator(incognito_browser)->IsSidePanelShowing());
EXPECT_FALSE(
side_panel_coordinator(non_incognito_browser)->IsSidePanelShowing());
// Run `sidePanel.open()` for the incognito profile. The panel should only
// open in the incognito browser and not the non-incognito browser.
RunOpenPanelForTabAndProfile(*extension, incognito_tab_id,
incognito_browser->profile());
EXPECT_TRUE(side_panel_coordinator(incognito_browser)
->IsSidePanelEntryShowing(GetKey(extension->id())));
EXPECT_FALSE(side_panel_coordinator(non_incognito_browser)
->IsSidePanelEntryShowing(GetKey(extension->id())));
}
// Tests that calling `sidePanel.open()` for an extension with a global panel
// registered opens the panel on all tabs (since the registration is global,
// rather than contextual).
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OpenGlobalPanelOnInactiveTab) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
int tab_id = GetCurrentTabId();
// Open a new tab.
OpenNewForegroundTab();
int new_tab_id = GetCurrentTabId();
ASSERT_NE(new_tab_id, tab_id);
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Open the side panel on the original tab.
RunOpenPanelForTab(*extension, tab_id);
// Because it's a global side panel, it should be displaying in both the
// original and the new tab.
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
}
// Tests that calling `sidePanel.open()` will override a different, active
// global side panel in the tab when the active tab's tab ID is provided.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OverridesGlobalPanelWithActiveTabIdProvided) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// Open a different global side panel (reading list).
ShowEntryAndWait(SidePanelEntry::Key(SidePanelEntry::Id::kReadingList));
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
EXPECT_EQ(SidePanelEntry::Id::kReadingList,
side_panel_coordinator()->GetCurrentEntryId());
// Call `sidePanel.open()` on the current tab.
RunOpenPanelForTab(*extension, GetCurrentTabId());
// The extension side panel should be able to override the currently-open
// side panel.
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
}
// Tests that calling `sidePanel.open()` will override a different, active
// global side panel in the tab when an inactive tab ID is provided.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OverridesGlobalPanelWithInactiveTabIdProvided) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
int first_tab_id = GetCurrentTabId();
OpenNewForegroundTab();
// Open a different global side panel (reading list).
ShowEntryAndWait(SidePanelEntry::Key(SidePanelEntry::Id::kReadingList));
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
EXPECT_EQ(SidePanelEntry::Id::kReadingList,
side_panel_coordinator()->GetCurrentEntryId());
// Call `sidePanel.open()` on the inactive tab.
RunOpenPanelForTab(*extension, first_tab_id);
// Even though the tab ID provided was for an inactive tab, the extension side
// panel should be able to override the currently-open side panel in the
// active tab since they are both global entries.
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
}
// Tests that calling `sidePanel.open()` with a contextual panel on the active
// tab will open that contextual panel and will not override a global panel
// that's open in a different tab.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OpenContextualPanelInActiveTab) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
// Open a global side panel (reading list) on the first tab.
ShowEntryAndWait(SidePanelEntry::Key(SidePanelEntry::Id::kReadingList));
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
EXPECT_EQ(SidePanelEntry::Id::kReadingList,
side_panel_coordinator()->GetCurrentEntryId());
// Open a new tab.
OpenNewForegroundTab();
int new_tab_id = GetCurrentTabId();
// Register a contextual side panel in the new tab.
RunSetOptions(*extension, new_tab_id, "panel.html", /*enabled=*/true);
// Call `sidePanel.open()` on the current tab.
RunOpenPanelForTab(*extension, GetCurrentTabId());
// The contextual side panel should show on the current tab.
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
// Switching back to the first tab, the global side panel (reading list)
// should be active.
browser()->tab_strip_model()->ActivateTabAt(0);
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelShowing());
EXPECT_EQ(SidePanelEntry::Id::kReadingList,
side_panel_coordinator()->GetCurrentEntryId());
}
// Tests that calling `sidePanel.open()` for a different tab will not override
// an active contextual panel.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_DoesNotOverrideActiveContextualPanelIfOtherTabIdProvided) {
// Load two side panel extensions.
const Extension* extension1 = LoadSidePanelExtension();
ASSERT_TRUE(extension1);
const Extension* extension2 = LoadSidePanelExtension();
ASSERT_TRUE(extension2);
// Create three tabs (the initial tab + two more).
int first_tab_id = GetCurrentTabId();
OpenNewForegroundTab();
OpenNewForegroundTab();
int third_tab_id = GetCurrentTabId();
// Register a global side panel in the first extension.
RunSetOptions(*extension1, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// Register a contextual side panel in the second extension.
RunSetOptions(*extension2, third_tab_id, "panel.html", /*enabled=*/true);
SidePanelEntry::Key extension1_key = GetKey(extension1->id());
SidePanelEntry::Key extension2_key = GetKey(extension2->id());
// Show the contextual entry for the second extension on the active (third)
// tab.
ShowContextualEntryAndWait(extension2_key);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension2_key));
// Now, run `sidePanel.open()` from the first extension. This is a global
// panel, and shouldn't override the current tab's contextual panel.
RunOpenPanelForTab(*extension1, first_tab_id);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension2_key));
// However, the global panel of the first extension should be displayed in the
// other two tabs (both the one explicitly specified and the second tab).
browser()->tab_strip_model()->ActivateTabAt(0);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension1_key));
browser()->tab_strip_model()->ActivateTabAt(1);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension1_key));
}
// Tests that calling `sidePanel.open()` will override an open contextual panel
// in an inactive tab if the tab ID provided matches.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OverridesContextualEntryInInactiveTabIfTabIdMatches) {
// Load two side panel extensions.
const Extension* extension1 = LoadSidePanelExtension();
ASSERT_TRUE(extension1);
const Extension* extension2 = LoadSidePanelExtension();
ASSERT_TRUE(extension2);
int first_tab_id = GetCurrentTabId();
OpenNewForegroundTab();
// Register a global side panel in the first extension.
RunSetOptions(*extension1, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// Register a contextual side panel in the second extension.
RunSetOptions(*extension2, first_tab_id, "panel.html", /*enabled=*/true);
SidePanelEntry::Key extension1_key = GetKey(extension1->id());
SidePanelEntry::Key extension2_key = GetKey(extension2->id());
// Show the contextual entry for the second extension on the inactive (first)
// tab. The panel shouldn't be displayed on the active tab since it's
// contextual.
RunOpenPanelForTab(*extension2, first_tab_id);
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Now, run `sidePanel.open()` from the first extension on the inactive
// (first) tab. Even though this is a global side panel, in this case, it
// *should* override the contextual panel because the tab ID was explicitly
// specified.
RunOpenPanelForTab(*extension1, first_tab_id);
// As a result, the panel should be showing on the active tab (since it's
// global)...
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension1_key));
// ... As well as on the inactive (first) tab.
browser()->tab_strip_model()->ActivateTabAt(0);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension1_key));
}
// Tests that calling `sidePanel.open()` can override an active contextual
// panel if the `tabId` of that tab is specified.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OverridesActiveContextualPanelOnSameTab) {
// Load two side panel extensions.
const Extension* extension1 = LoadSidePanelExtension();
ASSERT_TRUE(extension1);
const Extension* extension2 = LoadSidePanelExtension();
ASSERT_TRUE(extension2);
int current_tab_id = GetCurrentTabId();
// Register a global side panel in the first extension.
RunSetOptions(*extension1, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// Register a contextual side panel in the second extension.
RunSetOptions(*extension2, current_tab_id, "panel.html", /*enabled=*/true);
SidePanelEntry::Key extension1_key = GetKey(extension1->id());
SidePanelEntry::Key extension2_key = GetKey(extension2->id());
// Show the contextual entry for the second extension on the active (third)
// tab.
ShowContextualEntryAndWait(extension2_key);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension2_key));
// Now, run `sidePanel.open()` from the first extension. This should override
// the current (contextual) panel since the active tab ID was provided.
RunOpenPanelForTab(*extension1, current_tab_id);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension1_key));
}
// Tests that calling `sidePanel.open()` on an inactive tab with a contextual
// side panel sets that panel as the active entry for that tab, but does not
// open the side panel in the active tab.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_OpenContextualPanelInInactiveTab) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
int first_tab_id = GetCurrentTabId();
// Open a new tab.
OpenNewForegroundTab();
// Register a contextual side panel in the first tab.
RunSetOptions(*extension, first_tab_id, "panel.html", true);
// Call `sidePanel.open()` on the first tab.
RunOpenPanelForTab(*extension, first_tab_id);
// The contextual side panel should not show on the current tab.
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Switch to the first tab; the contextual panel should be shown.
browser()->tab_strip_model()->ActivateTabAt(0);
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
}
// Tests calling `sidePanel.open()` with a given window ID will open the
// side panel in that window when there is no active side panel.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_WindowId_OpenWithNoActivePanel) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Run `sidePanel.open()`. The panel should open.
RunOpenPanelForWindow(*extension, GetCurrentWindowId());
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
}
// Tests calling `sidePanel.open()` with a given window ID for an incognito
// window will open the side panel in that window when there is no active side
// panel.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_WindowId_OpenWithNoActivePanel_Incognito) {
const Extension* extension =
LoadSidePanelExtension(/*allow_in_incognito=*/true, /*split_mode=*/true);
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// For clarity sake, use a named reference to the non-incognito browser.
Browser* non_incognito_browser = browser();
// Open an incognito browser window to use and get the window id.
Browser* incognito_browser =
OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
ASSERT_TRUE(incognito_browser);
int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser);
EXPECT_FALSE(side_panel_coordinator(incognito_browser)->IsSidePanelShowing());
EXPECT_FALSE(
side_panel_coordinator(non_incognito_browser)->IsSidePanelShowing());
// Run `sidePanel.open()`. The panel should open in the active tab of the
// incognito browser.
RunOpenPanelForWindowAndProfile(*extension, incognito_window_id,
incognito_browser->profile());
EXPECT_TRUE(side_panel_coordinator(incognito_browser)
->IsSidePanelEntryShowing(GetKey(extension->id())));
EXPECT_FALSE(side_panel_coordinator(non_incognito_browser)
->IsSidePanelEntryShowing(GetKey(extension->id())));
}
// Tests calling `sidePanel.open()` with a given window ID will override an
// active global side panel in that window.
IN_PROC_BROWSER_TEST_F(ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_WindowId_OverridesActiveGlobalPanel) {
const Extension* extension = LoadSidePanelExtension();
ASSERT_TRUE(extension);
// Register a global side panel.
RunSetOptions(*extension, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// Open a different global side panel (reading list).
ShowEntryAndWait(SidePanelEntry::Key(SidePanelEntry::Id::kReadingList));
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
SidePanelEntryKey(SidePanelEntry::Id::kReadingList)));
// Call `sidePanel.open()` on the current tab.
RunOpenPanelForWindow(*extension, GetCurrentWindowId());
// The extension side panel should be able to override the currently-open
// side panel.
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(extension->id())));
}
// Tests calling `sidePanel.open()` with a given window ID will not override an
// active contextual panel.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_WindowId_DoesNotOverrideActiveContextualPanel) {
// Load two side panel extensions.
const Extension* extension1 = LoadSidePanelExtension();
ASSERT_TRUE(extension1);
const Extension* extension2 = LoadSidePanelExtension();
ASSERT_TRUE(extension2);
OpenNewForegroundTab();
int second_tab_id = GetCurrentTabId();
// Register a global side panel in the first extension.
RunSetOptions(*extension1, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// Register a contextual side panel in the second extension.
RunSetOptions(*extension2, second_tab_id, "panel.html", /*enabled=*/true);
SidePanelEntry::Key extension1_key = GetKey(extension1->id());
SidePanelEntry::Key extension2_key = GetKey(extension2->id());
// Show the contextual entry for the second extension on the active tab.
ShowContextualEntryAndWait(extension2_key);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension2_key));
// Now, run `sidePanel.open()` from the first extension. This is a global
// panel, and shouldn't override the current tab's contextual panel.
RunOpenPanelForWindow(*extension1, GetCurrentWindowId());
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension2_key));
// However, the global panel of the first extension should be displayed in
// the other tab.
browser()->tab_strip_model()->ActivateTabAt(0);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension1_key));
}
// Tests calling `sidePanel.open()` with a given window ID will not override an
// inactive contextual panel.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_WindowId_DoesNotOverrideInactiveContextualPanel) {
// Load two side panel extensions.
const Extension* extension1 = LoadSidePanelExtension();
ASSERT_TRUE(extension1);
const Extension* extension2 = LoadSidePanelExtension();
ASSERT_TRUE(extension2);
int first_tab_id = GetCurrentTabId();
OpenNewForegroundTab();
// Register a global side panel in the first extension.
RunSetOptions(*extension1, /*tab_id=*/std::nullopt, "panel.html",
/*enabled=*/true);
// Register a contextual side panel in the second extension.
RunSetOptions(*extension2, first_tab_id, "panel.html", /*enabled=*/true);
SidePanelEntry::Key extension1_key = GetKey(extension1->id());
SidePanelEntry::Key extension2_key = GetKey(extension2->id());
// Show the contextual entry for the second extension on the inactive tab.
RunOpenPanelForTab(*extension2, first_tab_id);
// Now, run `sidePanel.open()` from the first extension. This is a global
// panel, and shouldn't override the inactive tab's contextual panel, but
// it should be displayed in the active tab.
RunOpenPanelForWindow(*extension1, GetCurrentWindowId());
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension1_key));
// The first tab should still show the contextual panel.
browser()->tab_strip_model()->ActivateTabAt(0);
EXPECT_TRUE(
side_panel_coordinator()->IsSidePanelEntryShowing(extension2_key));
}
// Tests that extension context menus show the "(Open / Close) side panel" menu
// item when appropriate, and that the menu item toggles the global side panel.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_ContextMenu_GlobalPanel_ToggleSidePanelVisibility) {
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
// Intentionally navigate and commit a new tab. The first tab in the browser
// does not do this, this causes a failure in the origin_ CHECK since a tuple
// origin and an opaque origin are never the same. More info in url/origin.h.
OpenNewForegroundTab();
{
// Verify the "Open side panel" entry is absent if the extension does not
// have the side panel permission.
const Extension* no_side_panel_extension = LoadNoSidePanelExtension();
ASSERT_TRUE(no_side_panel_extension);
auto* menu = GetContextMenuForExtension(no_side_panel_extension->id());
EXPECT_EQ(
GetCommandState(
*menu, ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY),
CommandState::kAbsent);
}
const Extension* side_panel_extension = LoadSidePanelExtension();
ASSERT_TRUE(side_panel_extension);
{
// Verify the "Open side panel" entry is absent if the extension has the
// side panel permission but hasn't set a global panel for the tab.
auto* menu = GetContextMenuForExtension(side_panel_extension->id());
EXPECT_EQ(
GetCommandState(
*menu, ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY),
CommandState::kAbsent);
}
{
// Verify the "Open side panel" entry is present if the extension has the
// side panel permission and sets a global panel.
RunSetOptions(*side_panel_extension, /*tab_id=*/std::nullopt,
/*path=*/"panel_1.html",
/*enabled=*/true);
auto* menu = GetContextMenuForExtension(side_panel_extension->id());
EXPECT_EQ(
GetCommandState(
*menu, ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY),
CommandState::kEnabled);
// Simulate clicking on the "Open side panel" menu item. This should open
// the side panel.
menu->ExecuteCommand(
extensions::ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY, 0);
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(side_panel_extension->id())));
// Clicking on the menu item again should close the side panel.
menu->ExecuteCommand(
extensions::ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY, 0);
WaitForSidePanelClose();
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(side_panel_extension->id())));
}
}
// Tests that extension context menus show the "(Open / Close) side panel" menu
// item when appropriate, and that the menu item toggles the contextual side
// panel.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_ContextMenu_ContextualPanel_ToggleSidePanelVisibility) {
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
const Extension* side_panel_extension = LoadSidePanelExtension();
ASSERT_TRUE(side_panel_extension);
// Add a second tab to the browser and set a contextual panel for it.
OpenNewForegroundTab();
int new_tab_id = GetCurrentTabId();
RunSetOptions(*side_panel_extension, /*tab_id=*/new_tab_id,
/*path=*/"panel_1.html",
/*enabled=*/true);
{
// Verify the "Open side panel" entry is present if the extension has the
// side panel permission and a contextual panel is set for the tab.
auto* menu = GetContextMenuForExtension(side_panel_extension->id());
EXPECT_EQ(
GetCommandState(
*menu, ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY),
CommandState::kEnabled);
// Simulate clicking on the "Open side panel" menu item. This should open
// the side panel.
menu->ExecuteCommand(
extensions::ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY, 0);
EXPECT_TRUE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(side_panel_extension->id())));
// Clicking on the menu item again should close the side panel.
menu->ExecuteCommand(
extensions::ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY, 0);
WaitForSidePanelClose();
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(side_panel_extension->id())));
}
// Activate the first tab which does not have a contextual panel.
browser()->tab_strip_model()->ActivateTabAt(0);
{
// Verify the "Open side panel" entry is absent if the extension has the
// side panel permission but no contextual panel set on the tab.
auto* menu = GetContextMenuForExtension(side_panel_extension->id());
EXPECT_EQ(
GetCommandState(
*menu, ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY),
CommandState::kAbsent);
}
}
// Tests that the extension context menus "(Open / Close) side panel" menu item
// does nothing if the page navigated while the menu is open.
IN_PROC_BROWSER_TEST_F(
ExtensionOpenSidePanelBrowserTest,
OpenSidePanel_ContextMenu_ContextualPanel_PageNavigations) {
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelShowing());
scoped_refptr<const extensions::Extension> side_panel_extension =
LoadSidePanelExtension();
// Intentionally add a new tab in order to update the origin url in the
// context menus.
OpenNewForegroundTab();
int new_tab_id = GetCurrentTabId();
RunSetOptions(*side_panel_extension, /*tab_id=*/new_tab_id,
/*path=*/"panel_1.html",
/*enabled=*/true);
{
// Verify the "Open side panel" entry is present if the extension has the
// side panel permission and a contextual panel is set for the tab.
auto* menu = GetContextMenuForExtension(side_panel_extension->id());
EXPECT_EQ(GetCommandState(*menu, extensions::ExtensionContextMenuModel::
TOGGLE_SIDE_PANEL_VISIBILITY),
CommandState::kEnabled);
// Navigate to another page while the menu is open.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("https://2.com")));
// Ensure that the menu item does not open the side panel.
menu->ExecuteCommand(
extensions::ExtensionContextMenuModel::TOGGLE_SIDE_PANEL_VISIBILITY, 0);
EXPECT_FALSE(side_panel_coordinator()->IsSidePanelEntryShowing(
GetKey(side_panel_extension->id())));
}
}
// TODO(crbug.com/40243760): Add a test here which requires a browser in
// ExtensionViewHost for both global and contextual extension entries. One
// example of this is having a link in the page that the user can open in a new
// tab.
} // namespace
} // namespace extensions
|