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 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/mahi/mahi_panel_view.h"
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/url_constants.h"
#include "ash/public/cpp/image_util.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/test/test_new_window_delegate.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/icon_button.h"
#include "ash/style/system_textfield.h"
#include "ash/system/mahi/mahi_constants.h"
#include "ash/system/mahi/mahi_content_source_button.h"
#include "ash/system/mahi/mahi_question_answer_view.h"
#include "ash/system/mahi/mahi_ui_controller.h"
#include "ash/system/mahi/mahi_utils.h"
#include "ash/system/mahi/summary_outlines_elucidation_section.h"
#include "ash/system/mahi/test/mahi_test_util.h"
#include "ash/system/mahi/test/mock_mahi_manager.h"
#include "ash/test/ash_test_base.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "chromeos/components/mahi/public/cpp/mahi_manager.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/layer.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/scrollbar/scroll_bar.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/highlight_border.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"
namespace ash {
namespace {
// Aliases ---------------------------------------------------------------------
using chromeos::MahiResponseStatus;
using ::testing::_;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::Return;
// MockNewWindowDelegate -------------------------------------------------------
class MockNewWindowDelegate : public NiceMock<TestNewWindowDelegate> {
public:
// TestNewWindowDelegate:
MOCK_METHOD(void,
OpenUrl,
(const GURL& url, OpenUrlFrom from, Disposition disposition),
(override));
};
// Helpers ---------------------------------------------------------------------
// Returns a comprehensive list of potential errors from Mahi backend.
// NOTE: `MahiResponseStatus::kLowQuota` is a warning instead of an error.
std::vector<MahiResponseStatus> GetMahiErrors() {
std::vector<MahiResponseStatus> errors;
for (size_t status_value = 0;
status_value <= static_cast<size_t>(MahiResponseStatus::kMaxValue);
++status_value) {
MahiResponseStatus status = static_cast<MahiResponseStatus>(status_value);
if (status != MahiResponseStatus::kSuccess &&
status != MahiResponseStatus::kLowQuota) {
errors.push_back(status);
}
}
return errors;
}
void PressEnter() {
ui::test::EventGenerator(Shell::GetPrimaryRootWindow())
.PressKey(ui::KeyboardCode::VKEY_RETURN, ui::EF_NONE);
}
// Returns an answer asyncly with the specified `status`. Use `waiter` to wait
// for the response.
void ReturnDefaultAnswerAsyncly(
base::test::TestFuture<void>& waiter,
MahiResponseStatus status,
chromeos::MahiManager::MahiAnswerQuestionCallback callback,
base::TimeDelta delay = base::TimeDelta()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::OnceClosure unblock_closure, MahiResponseStatus status,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(u"fake answer", status);
std::move(unblock_closure).Run();
},
waiter.GetCallback(), status, std::move(callback)),
delay);
}
// Returns `kFakeOutlines` asyncly with the specified `status`. Use `waiter` to
// wait for the response.
void ReturnDefaultOutlinesAsyncly(
base::test::TestFuture<void>& waiter,
MahiResponseStatus status,
chromeos::MahiManager::MahiOutlinesCallback callback) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(
[](base::OnceClosure unblock_closure, MahiResponseStatus status,
chromeos::MahiManager::MahiOutlinesCallback callback) {
std::move(callback).Run(mahi_test_util::GetDefaultFakeOutlines(),
status);
std::move(unblock_closure).Run();
},
waiter.GetCallback(), status, std::move(callback)));
}
// Returns a fake summary asyncly with the specified `status`. Use `waiter` to
// wait for the response.
void ReturnDefaultSummaryAsyncly(
base::test::TestFuture<void>& waiter,
MahiResponseStatus status,
chromeos::MahiManager::MahiSummaryCallback callback,
base::TimeDelta delay = base::TimeDelta()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(
[](base::OnceClosure unblock_closure, MahiResponseStatus status,
chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(u"fake summary", status);
std::move(unblock_closure).Run();
},
waiter.GetCallback(), status, std::move(callback)),
delay);
}
// Returns a long summary.
void ReturnLongSummary(chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(
base::StrCat(std::vector<std::u16string>(100, u"Long Summary\n")),
MahiResponseStatus::kSuccess);
}
std::u16string_view GetContentSourceTitle(views::View* mahi_view) {
return views::AsViewClass<MahiContentSourceButton>(
mahi_view->GetViewByID(
mahi_constants::ViewId::kContentSourceButton))
->GetText();
}
gfx::ImageSkia GetContentSourceIcon(views::View* mahi_view) {
return views::AsViewClass<MahiContentSourceButton>(
mahi_view->GetViewByID(
mahi_constants::ViewId::kContentSourceButton))
->GetImage(views::Button::STATE_NORMAL);
}
views::Label* GetSummaryLabel(views::View* mahi_view) {
return views::AsViewClass<views::Label>(
mahi_view->GetViewByID(mahi_constants::ViewId::kSummaryLabel));
}
// Generates a random string, given the maximum amount of words the string can
// have.
std::u16string GetRandomString(int max_words_count) {
int string_length = base::RandInt(1, max_words_count);
std::vector<char> random_chars;
for (int string_index = 0; string_index < string_length; string_index++) {
int word_length = base::RandInt(1, 10);
for (int word_index = 0; word_index < word_length; word_index++) {
// Add a random character from 'a' to 'z' to the string.
random_chars.push_back(base::RandInt('a', 'z'));
}
// Add a space between each word.
random_chars.push_back(0x20);
}
return std::u16string(random_chars.begin(), random_chars.end());
}
} // namespace
class MahiPanelViewTest : public AshTestBase {
public:
MahiPanelViewTest()
: AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
MockMahiManager& mock_mahi_manager() { return mock_mahi_manager_; }
MahiUiController* ui_controller() { return &ui_controller_; }
MockNewWindowDelegate& new_window_delegate() { return new_window_delegate_; }
MahiPanelView* panel_view() { return panel_view_; }
views::Widget* widget() { return widget_.get(); }
protected:
// AshTestBase:
void SetUp() override {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{chromeos::features::kMahi,
chromeos::features::kFeatureManagementMahi},
/*disabled_features=*/{});
AshTestBase::SetUp();
scoped_setter_ = std::make_unique<chromeos::ScopedMahiManagerSetter>(
&mock_mahi_manager_);
CreatePanelWidget();
}
void TearDown() override {
panel_view_ = nullptr;
widget_.reset();
scoped_setter_.reset();
AshTestBase::TearDown();
}
// Creates a widget hosting `MahiPanelView`. Recreates if there is one.
void CreatePanelWidget() {
ResetPanelWidget();
widget_ = CreateFramelessTestWidget();
widget_->SetBounds(
gfx::Rect(/*x=*/0, /*y=*/0,
/*width=*/mahi_constants::kPanelDefaultWidth,
/*height=*/mahi_constants::kPanelDefaultHeight));
panel_view_ = widget_->SetContentsView(
std::make_unique<MahiPanelView>(&ui_controller_));
}
void ResetPanelWidget() {
// Avoid creating a dangling pointer.
panel_view_ = nullptr;
widget_.reset();
}
// Submit a test question by setting the text in the textfield and click send
// button.
void SubmitTestQuestion(const std::u16string& question = u"fake question") {
auto* const question_textfield = views::AsViewClass<views::Textfield>(
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionTextfield));
ASSERT_TRUE(question_textfield);
question_textfield->SetText(question);
auto* const send_button = panel_view()->GetViewByID(
mahi_constants::ViewId::kAskQuestionSendButton);
ASSERT_TRUE(send_button);
// Send button enable state doesn't get updated on calling question
// textfield `SetText()`, set it manually.
send_button->SetEnabled(true);
LeftClickOn(send_button);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
NiceMock<MockMahiManager> mock_mahi_manager_;
std::unique_ptr<chromeos::ScopedMahiManagerSetter> scoped_setter_;
MahiUiController ui_controller_;
raw_ptr<MahiPanelView> panel_view_ = nullptr;
std::unique_ptr<views::Widget> widget_;
MockNewWindowDelegate new_window_delegate_;
};
// Checks that the summary text is set correctly in ctor with different texts,
// and the indicator label is set to properly, too.
TEST_F(MahiPanelViewTest, SummaryTextAndIndicatorLabel) {
const std::u16string summary_text1(u"test summary text 1");
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault([&summary_text1](
chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(summary_text1, MahiResponseStatus::kSuccess);
});
MahiPanelView mahi_view1(ui_controller());
const auto* const summary_label1 = views::AsViewClass<views::Label>(
mahi_view1.GetViewByID(mahi_constants::ViewId::kSummaryLabel));
const auto* const indicator_label =
views::AsViewClass<views::Label>(mahi_view1.GetViewByID(
mahi_constants::ViewId::kSummaryElucidationIndicator));
EXPECT_EQ(summary_text1, summary_label1->GetText());
EXPECT_TRUE(indicator_label->GetVisible());
EXPECT_EQ(indicator_label->GetText(),
l10n_util::GetStringUTF16(IDS_MAHI_SUMMARIZE_INDICATOR_LABEL));
const std::u16string summary_text2(u"test summary text 2");
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault([&summary_text2](
chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(summary_text2, MahiResponseStatus::kSuccess);
});
MahiPanelView mahi_view2(ui_controller());
const auto* const summary_label2 = views::AsViewClass<views::Label>(
mahi_view2.GetViewByID(mahi_constants::ViewId::kSummaryLabel));
EXPECT_EQ(summary_text2, summary_label2->GetText());
// Make sure the text is multiline and aligned correctly.
EXPECT_TRUE(summary_label2->GetMultiLine());
EXPECT_EQ(summary_label2->GetHorizontalAlignment(),
gfx::HorizontalAlignment::ALIGN_LEFT);
}
// Checks that the text is set correctly in ctor with elucidation result when
// the ui controller has `elucidation_in_use_ = true`, and the indicator label
// is set properly, too.
TEST_F(MahiPanelViewTest, SimplifiedTextAndIndicatorLabel) {
const std::u16string simplified_text(u"test simplified text");
ON_CALL(mock_mahi_manager(), GetElucidation)
.WillByDefault(
[&simplified_text](
chromeos::MahiManager::MahiElucidationCallback callback) {
std::move(callback).Run(simplified_text,
MahiResponseStatus::kSuccess);
});
ui_controller()->set_elucidation_in_use_for_testing(true);
MahiPanelView mahi_view(ui_controller());
const auto* const result_label = views::AsViewClass<views::Label>(
mahi_view.GetViewByID(mahi_constants::ViewId::kSummaryLabel));
const auto* const indicator_label =
views::AsViewClass<views::Label>(mahi_view.GetViewByID(
mahi_constants::ViewId::kSummaryElucidationIndicator));
EXPECT_EQ(simplified_text, result_label->GetText());
EXPECT_TRUE(indicator_label->GetVisible());
EXPECT_EQ(indicator_label->GetText(),
l10n_util::GetStringUTF16(IDS_MAHI_SIMPLIFY_INDICATOR_LABEL));
}
TEST_F(MahiPanelViewTest, ThumbsUpFeedbackButton) {
base::HistogramTester histogram_tester;
// Pressing thumbs up should toggle the button on and update the feedback
// histogram.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(0);
IconButton* thumbs_up_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsUpButton));
LeftClickOn(thumbs_up_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_TRUE(thumbs_up_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 1);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 0);
// Pressing thumbs up again should just toggle the button off.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(0);
LeftClickOn(thumbs_up_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_FALSE(thumbs_up_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 1);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 0);
// Pressing thumbs up should toggle the button on and update the histogram
// again.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(0);
LeftClickOn(thumbs_up_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_TRUE(thumbs_up_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 2);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 0);
}
TEST_F(MahiPanelViewTest, ThumbsDownFeedbackButton) {
base::HistogramTester histogram_tester;
// Pressing thumbs down the first time should open the feedback dialog, toggle
// the button off and update the feedback histogram.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(1);
IconButton* thumbs_down_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsDownButton));
LeftClickOn(thumbs_down_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_TRUE(thumbs_down_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 0);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 1);
// Pressing thumbs down again should just toggle the button off.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(0);
LeftClickOn(thumbs_down_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_FALSE(thumbs_down_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 0);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 1);
// Pressing thumbs down should toggle the button on and update the histogram
// again.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(0);
LeftClickOn(thumbs_down_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_TRUE(thumbs_down_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 0);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 2);
}
TEST_F(MahiPanelViewTest, CloseButton) {
EXPECT_FALSE(widget()->IsClosed());
LeftClickOn(panel_view()->GetViewByID(mahi_constants::ViewId::kCloseButton));
EXPECT_TRUE(widget()->IsClosed());
}
TEST_F(MahiPanelViewTest, LearnMoreLink) {
auto* disclaimer_text = views::AsViewClass<views::StyledLabel>(
panel_view()->GetViewByID(mahi_constants::ViewId::kFooterLabel));
// Run layout so the link updates its size and becomes clickable.
views::test::RunScheduledLayout(widget());
EXPECT_CALL(new_window_delegate(),
OpenUrl(GURL(chrome::kHelpMeReadWriteLearnMoreURL),
NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab));
disclaimer_text->ClickFirstLinkForTesting();
Mock::VerifyAndClearExpectations(&new_window_delegate());
}
// TODO(b/330643995): Remove this test after outlines can be shown by default.
TEST_F(MahiPanelViewTest, OutlinesHiddenByDefault) {
EXPECT_FALSE(
panel_view()
->GetViewByID(mahi_constants::ViewId::kOutlinesSectionContainer)
->GetVisible());
}
// Make sure the `PanelContentsContainer` is larger than its contents when the
// contents are short.
TEST_F(MahiPanelViewTest, PanelContentsViewBoundsWithShortSummary) {
ON_CALL(mock_mahi_manager(), GetContentTitle)
.WillByDefault(Return(u"fake content title"));
ON_CALL(mock_mahi_manager(), GetOutlines)
.WillByDefault(mahi_test_util::ReturnDefaultOutlines);
// Configure the mock manager to return a short summary.
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault([](chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(/*summary=*/u"Short summary",
MahiResponseStatus::kSuccess);
});
MahiPanelView mahi_view(ui_controller());
// TODO(b/330643995): After outlines are shown by default, remove this since
// we won't need to explicitly show the outlines section anymore.
mahi_view.GetViewByID(mahi_constants::ViewId::kOutlinesSectionContainer)
->SetVisible(true);
mahi_view.SetPreferredSize(gfx::Size(300, 400));
mahi_view.SizeToPreferredSize();
const int short_content_height =
mahi_view.GetViewByID(mahi_constants::kSummaryOutlinesSection)
->bounds()
.height();
const int short_contents_container_height =
mahi_view.GetViewByID(mahi_constants::kPanelContentsContainer)
->bounds()
.height();
// The container should be larger than the contents when the summary is short.
EXPECT_GT(short_contents_container_height, short_content_height);
}
// Make sure the `PanelContentsContainer` is smaller than its contents when the
// contents are long.
TEST_F(MahiPanelViewTest, PanelContentsViewBoundsWithLongSummary) {
ON_CALL(mock_mahi_manager(), GetContentTitle)
.WillByDefault(Return(u"fake content title"));
ON_CALL(mock_mahi_manager(), GetOutlines)
.WillByDefault(mahi_test_util::ReturnDefaultOutlines);
// Configure the mock manager to return a long summary.
ON_CALL(mock_mahi_manager(), GetSummary).WillByDefault(ReturnLongSummary);
MahiPanelView mahi_view(ui_controller());
// TODO(b/330643995): After outlines are shown by default, remove this since
// we won't need to explicitly show the outlines section anymore.
mahi_view.GetViewByID(mahi_constants::ViewId::kOutlinesSectionContainer)
->SetVisible(true);
mahi_view.SetPreferredSize(gfx::Size(300, 400));
mahi_view.SizeToPreferredSize();
const int long_content_height =
mahi_view.GetViewByID(mahi_constants::kSummaryOutlinesSection)
->bounds()
.height();
const int long_contents_container_height =
mahi_view.GetViewByID(mahi_constants::kPanelContentsContainer)
->bounds()
.height();
// The container should be smaller than the contents when the summary is long.
EXPECT_LT(long_contents_container_height, long_content_height);
}
// Make sure the `PanelContentsContainer` is always sized to occupy the same
// amount of space in the `MahiPanelView` irrespective of its contents size.
TEST_F(MahiPanelViewTest, PanelContentsViewBoundsStayConstant) {
// Create a panel with a short summary.
constexpr gfx::Size kPanelBounds(/*width=*/300, /*height=*/400);
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault([](chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(u"Short summary", MahiResponseStatus::kSuccess);
});
MahiPanelView mahi_view1(ui_controller());
mahi_view1.SetPreferredSize(kPanelBounds);
mahi_view1.SizeToPreferredSize();
// Create another panel with a long summary.
ON_CALL(mock_mahi_manager(), GetSummary).WillByDefault(ReturnLongSummary);
MahiPanelView mahi_view2(ui_controller());
mahi_view2.SetPreferredSize(kPanelBounds);
mahi_view2.SizeToPreferredSize();
const int short_contents_container_height =
mahi_view1.GetViewByID(mahi_constants::kPanelContentsContainer)
->bounds()
.height();
const int long_contents_container_height =
mahi_view2.GetViewByID(mahi_constants::kPanelContentsContainer)
->bounds()
.height();
// The container size should stay constant irrespective of summary length.
EXPECT_EQ(short_contents_container_height, long_contents_container_height);
}
TEST_F(MahiPanelViewTest, LoadingAnimations) {
ResetPanelWidget();
// Config the mock mahi manager to return a summary asyncly.
base::test::TestFuture<void> summary_waiter;
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault([&summary_waiter](
chromeos::MahiManager::MahiSummaryCallback callback) {
ReturnDefaultSummaryAsyncly(
summary_waiter, MahiResponseStatus::kSuccess, std::move(callback));
});
// Config the mock mahi manager to return outlines asyncly.
base::test::TestFuture<void> outlines_waiter;
ON_CALL(mock_mahi_manager(), GetOutlines)
.WillByDefault([&outlines_waiter](
chromeos::MahiManager::MahiOutlinesCallback callback) {
ReturnDefaultOutlinesAsyncly(
outlines_waiter, MahiResponseStatus::kSuccess, std::move(callback));
});
MahiPanelView mahi_view(ui_controller());
// TODO(b/330643995): After outlines are shown by default, remove this since
// we won't need to explicitly show the outlines section anymore.
mahi_view.GetViewByID(mahi_constants::ViewId::kOutlinesSectionContainer)
->SetVisible(true);
const auto* const summary_loading_animated_image = mahi_view.GetViewByID(
mahi_constants::ViewId::kSummaryLoadingAnimatedImage);
const auto* const outlines_loading_animated_image = mahi_view.GetViewByID(
mahi_constants::ViewId::kOutlinesLoadingAnimatedImage);
const auto* const summary_label =
mahi_view.GetViewByID(mahi_constants::ViewId::kSummaryLabel);
const auto* const outlines_container =
mahi_view.GetViewByID(mahi_constants::ViewId::kOutlinesContainer);
// Since the APIs that return summary & outlines are blocked, loading
// animations should play.
EXPECT_TRUE(summary_loading_animated_image->GetVisible());
EXPECT_TRUE(outlines_loading_animated_image->GetVisible());
EXPECT_FALSE(summary_label->GetVisible());
EXPECT_FALSE(outlines_container->GetVisible());
// Wait until summary is returned. Then check:
// 1. The outlines loading animation is still playing.
// 2. `summary_label` is visible.
ASSERT_TRUE(summary_waiter.Wait());
EXPECT_FALSE(summary_loading_animated_image->GetVisible());
EXPECT_TRUE(outlines_loading_animated_image->GetVisible());
EXPECT_TRUE(summary_label->GetVisible());
EXPECT_FALSE(outlines_container->GetVisible());
// Wait until outlines are returned. Both labels should be visible.
ASSERT_TRUE(outlines_waiter.Wait());
EXPECT_FALSE(summary_loading_animated_image->GetVisible());
EXPECT_FALSE(outlines_loading_animated_image->GetVisible());
EXPECT_TRUE(summary_label->GetVisible());
// TODO(b/330643995): Expect TRUE after outlines are shown by default.
EXPECT_FALSE(outlines_container->GetVisible());
}
TEST_F(MahiPanelViewTest, SummaryLoadingAnimationsMetricsRecord) {
// Reset the default panel to avoid unnecessary histogram record.
ResetPanelWidget();
base::HistogramTester histogram_tester;
auto delay_time = base::Milliseconds(100);
// Config the mock mahi manager to return a summary asyncly.
base::test::TestFuture<void> summary_waiter;
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault([&summary_waiter, delay_time](
chromeos::MahiManager::MahiSummaryCallback callback) {
ReturnDefaultSummaryAsyncly(
summary_waiter, MahiResponseStatus::kSuccess, std::move(callback),
/*delay=*/delay_time);
});
MahiPanelView mahi_view(ui_controller());
histogram_tester.ExpectTimeBucketCount(
mahi_constants::kSummaryLoadingTimeHistogramName, delay_time, 0);
// Test that loading time metrics is recorded when summary is loaded.
ASSERT_TRUE(summary_waiter.WaitAndClear());
histogram_tester.ExpectTimeBucketCount(
mahi_constants::kSummaryLoadingTimeHistogramName, delay_time, 1);
// Test that loading time metrics is recorded when the content is refreshed.
ui_controller()->RefreshContents();
ASSERT_TRUE(summary_waiter.Wait());
histogram_tester.ExpectTimeBucketCount(
mahi_constants::kSummaryLoadingTimeHistogramName, delay_time, 2);
}
TEST_F(MahiPanelViewTest, AnswerLoadingAnimationsMetricsRecord) {
base::HistogramTester histogram_tester;
auto delay_time = base::Milliseconds(100);
// Config the mock mahi manager to return an answer asyncly.
base::test::TestFuture<void> answer_waiter;
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion)
.WillOnce(
[&answer_waiter, delay_time](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
ReturnDefaultAnswerAsyncly(
answer_waiter, MahiResponseStatus::kSuccess,
std::move(callback), /*delay=*/delay_time);
});
SubmitTestQuestion();
histogram_tester.ExpectTimeBucketCount(
mahi_constants::kAnswerLoadingTimeHistogramName, delay_time, 0);
// Test that loading time metrics is recorded when a question is answered.
ASSERT_TRUE(answer_waiter.Wait());
histogram_tester.ExpectTimeBucketCount(
mahi_constants::kAnswerLoadingTimeHistogramName, delay_time, 1);
}
// Tests that the correct behaviour occurs when the panel is resized.
TEST_F(MahiPanelViewTest, ResizePanel) {
MahiPanelView mahi_view(ui_controller());
const gfx::Rect resized_bounds = gfx::Rect(20, 20, 150, 140);
panel_view()->SetBounds(resized_bounds.x(), resized_bounds.y(),
resized_bounds.width(), resized_bounds.height());
// Check that the panel clip rect has been resized.
// X and Y positions should be 0 as they are relative to the panel.
EXPECT_EQ(panel_view()->layer()->GetTargetClipRect(),
gfx::Rect(0, 0, resized_bounds.width(), resized_bounds.height()));
// Check that the text in the summary outlines elucidation section bounds has
// been resized.
const SummaryOutlinesElucidationSection*
summary_outlines_elucidation_section =
views::AsViewClass<SummaryOutlinesElucidationSection>(
panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection));
const views::Label* summary_text = views::AsViewClass<views::Label>(
summary_outlines_elucidation_section->GetViewByID(
mahi_constants::ViewId::kSummaryLabel));
EXPECT_EQ(
summary_text->GetMaximumWidth(),
resized_bounds.width() - mahi_constants::kPanelBorderAndPadding -
mahi_constants::kSummaryOutlinesElucidationSectionPadding.width());
}
// Tests that pressing on the send button with a valid textfield takes the user
// to the Q&A View and the back to summary outlines button that appears on top
// takes the user back to the main view.
TEST_F(MahiPanelViewTest, TransitionToQuestionAnswerView) {
// Initially the Summary Outlines section is visible.
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
ASSERT_TRUE(summary_outlines_section);
EXPECT_TRUE(summary_outlines_section->GetVisible());
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
ASSERT_TRUE(question_answer_view);
EXPECT_FALSE(question_answer_view->GetVisible());
const auto* const send_button =
panel_view()->GetViewByID(mahi_constants::ViewId::kAskQuestionSendButton);
ASSERT_TRUE(send_button);
EXPECT_TRUE(send_button->GetVisible());
const auto* const go_to_summary_outlines_button = panel_view()->GetViewByID(
mahi_constants::ViewId::kGoToSummaryOutlinesButton);
ASSERT_TRUE(go_to_summary_outlines_button);
EXPECT_FALSE(go_to_summary_outlines_button->GetVisible());
auto* const question_textfield = views::AsViewClass<views::Textfield>(
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionTextfield));
ASSERT_TRUE(question_textfield);
EXPECT_TRUE(question_textfield->GetVisible());
const auto* const go_to_question_answer_button = panel_view()->GetViewByID(
mahi_constants::ViewId::kGoToQuestionAndAnswerButton);
ASSERT_TRUE(go_to_question_answer_button);
EXPECT_FALSE(go_to_question_answer_button->GetVisible());
// Pressing the send button with a valid input in the textfield should take
// the user to the Q&A view.
SubmitTestQuestion(u"input");
EXPECT_FALSE(summary_outlines_section->GetVisible());
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_TRUE(go_to_summary_outlines_button->GetVisible());
EXPECT_TRUE(send_button->GetVisible());
// Run layout so the back to summary outlines button updates its size and
// becomes clickable.
views::test::RunScheduledLayout(widget());
// Pressing the back to summary outlines button should take the user back to
// the main view.
LeftClickOn(go_to_summary_outlines_button);
EXPECT_TRUE(summary_outlines_section->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_FALSE(go_to_summary_outlines_button->GetVisible());
EXPECT_TRUE(send_button->GetVisible());
views::test::RunScheduledLayout(widget());
// "Back to QA" button should now be visible and clickable.
EXPECT_TRUE(go_to_question_answer_button->GetVisible());
LeftClickOn(go_to_question_answer_button);
EXPECT_FALSE(summary_outlines_section->GetVisible());
EXPECT_TRUE(question_answer_view->GetVisible());
// Refreshing the summary contents should clear the Q&A view and make the
// "Back to QA button" invisible.
ui_controller()->RefreshContents();
EXPECT_TRUE(summary_outlines_section->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_FALSE(go_to_question_answer_button->GetVisible());
}
TEST_F(MahiPanelViewTest, ScrollViewContentsDynamicSize) {
auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
auto* const scroll_view_contents =
panel_view()->GetViewByID(mahi_constants::ViewId::kScrollViewContents);
// Make sure the views have different size and their heights exceed the
// visible rect's height of the scroll view.
summary_outlines_section->SetPreferredSize(gfx::Size(80, 300));
question_answer_view->SetPreferredSize(gfx::Size(80, 600));
views::test::RunScheduledLayout(widget());
EXPECT_TRUE(summary_outlines_section->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_EQ(summary_outlines_section->height() +
mahi_constants::kScrollContentsViewBottomPadding,
scroll_view_contents->GetPreferredSize().height());
// Transition to Q&A view. Scroll view should change its preferred height (the
// height that the view will take when it is not constrained by `ScrollView`).
SubmitTestQuestion(u"input");
// Run layout so the views update their size.
views::test::RunScheduledLayout(widget());
EXPECT_FALSE(summary_outlines_section->GetVisible());
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_EQ(question_answer_view->height() +
mahi_constants::kScrollContentsViewBottomPadding,
scroll_view_contents->GetPreferredSize().height());
// Transition back to summary outlines view. Scroll view should change its
// preferred height.(the height that the view will take when it is not
// constrained by `ScrollView`).
LeftClickOn(panel_view()->GetViewByID(
mahi_constants::ViewId::kGoToSummaryOutlinesButton));
views::test::RunScheduledLayout(widget());
EXPECT_TRUE(summary_outlines_section->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_EQ(summary_outlines_section->height() +
mahi_constants::kScrollContentsViewBottomPadding,
scroll_view_contents->GetPreferredSize().height());
}
// Tests that the question textfield accepts user input and creates a text
// bubble with the provided text by pressing the send button or enter.
TEST_F(MahiPanelViewTest, QuestionTextfield_CreateQuestion) {
auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
auto* const send_button =
panel_view()->GetViewByID(mahi_constants::ViewId::kAskQuestionSendButton);
auto* const question_textfield = views::AsViewClass<views::Textfield>(
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionTextfield));
const std::u16string answer1(u"test answer1");
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[&answer1](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(answer1, MahiResponseStatus::kSuccess);
});
// Pressing the send button with a valid question should create a question and
// answer text bubble.
const std::u16string question1(u"question 1");
SubmitTestQuestion(question1);
ASSERT_EQ(2u, question_answer_view->children().size());
EXPECT_EQ(views::AsViewClass<views::Label>(
question_answer_view->children()[0]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText(),
question1);
EXPECT_EQ(views::AsViewClass<views::Label>(
question_answer_view->children()[1]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText(),
answer1);
// Textfield contents should be cleared after processing input.
EXPECT_TRUE(question_textfield->GetText().empty());
const std::u16string answer2(u"test answer2");
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[&answer2](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(answer2, MahiResponseStatus::kSuccess);
});
// Set another valid text in the question textfield.
const std::u16string question2(u"question 2");
question_textfield->SetText(question2);
send_button->SetEnabled(true);
// Pressing the "Enter" key while the textfield is focused should create a
// question and answer text bubble.
question_textfield->RequestFocus();
PressEnter();
ASSERT_EQ(question_answer_view->children().size(), 4u);
EXPECT_EQ(views::AsViewClass<views::Label>(
question_answer_view->children()[2]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText(),
question2);
EXPECT_EQ(views::AsViewClass<views::Label>(
question_answer_view->children()[3]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText(),
answer2);
// Textfield contents should be cleared after processing input.
EXPECT_TRUE(question_textfield->GetText().empty());
}
// Tests that the question send button state is controlled by both pending QA
// request and question textfield content.
TEST_F(MahiPanelViewTest, QuestionTextfield_SendButtonState) {
// Config the mock mahi manager to return an answer asyncly.
base::test::TestFuture<void> answer_waiter;
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion)
.WillOnce(
[&answer_waiter](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
ReturnDefaultAnswerAsyncly(answer_waiter,
MahiResponseStatus::kSuccess,
std::move(callback));
});
// Question textfield is initially empty therefore send button is disabled.
auto* const question_textfield = views::AsViewClass<views::Textfield>(
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionTextfield));
auto* const send_button =
panel_view()->GetViewByID(mahi_constants::ViewId::kAskQuestionSendButton);
ASSERT_TRUE(question_textfield);
ASSERT_TRUE(send_button);
EXPECT_TRUE(question_textfield->GetText().empty());
EXPECT_FALSE(send_button->GetEnabled());
question_textfield->RequestFocus();
ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
// Types something in the question textfield, the send button is enabled.
generator.PressKey(ui::KeyboardCode::VKEY_A, ui::EF_NONE);
EXPECT_EQ(question_textfield->GetText(), u"a");
EXPECT_TRUE(send_button->GetEnabled());
// Presses backspace to make the textfield empty, the send button is disabled.
generator.PressKey(ui::KeyboardCode::VKEY_BACK, ui::EF_NONE);
EXPECT_TRUE(question_textfield->GetText().empty());
EXPECT_FALSE(send_button->GetEnabled());
// Types something again to enable the send button.
generator.PressKey(ui::KeyboardCode::VKEY_A, ui::EF_NONE);
EXPECT_TRUE(send_button->GetEnabled());
// Sends the question, pending QA request is present.
LeftClickOn(send_button);
// Sending question empties the textfield and disables the send button.
EXPECT_TRUE(question_textfield->GetText().empty());
EXPECT_FALSE(send_button->GetEnabled());
// When pending QA request, send button is disabled even if user types in the
// question text field.
generator.PressKey(ui::KeyboardCode::VKEY_B, ui::EF_NONE);
EXPECT_EQ(question_textfield->GetText(), u"b");
EXPECT_FALSE(send_button->GetEnabled());
// Attempt sending a question while loading. It should not be processed either
// by attempting to press the send button or by pressing "Enter" key.
LeftClickOn(send_button);
question_textfield->RequestFocus();
PressEnter();
// Wait until an answer is loaded. Send button should be enabled again because
// the question textfield is not empty.
ASSERT_TRUE(answer_waiter.Wait());
EXPECT_EQ(question_textfield->GetText(), u"b");
EXPECT_TRUE(send_button->GetEnabled());
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
// Attempting to send with "Enter" key should process the new input.
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion);
PressEnter();
EXPECT_TRUE(question_textfield->GetText().empty());
EXPECT_FALSE(send_button->GetEnabled());
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
}
// Tests that the question textfield does not process empty or blank inputs.
TEST_F(MahiPanelViewTest, QuestionTextfield_EmptyInput) {
// Question textfield is initially empty.
auto* const question_textfield = views::AsViewClass<views::Textfield>(
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionTextfield));
ASSERT_TRUE(question_textfield);
EXPECT_TRUE(question_textfield->GetText().empty());
// Send button is disabled when text field is empty.
auto* const send_button =
panel_view()->GetViewByID(mahi_constants::ViewId::kAskQuestionSendButton);
ASSERT_TRUE(send_button);
EXPECT_FALSE(send_button->GetEnabled());
// Even forcing the button enabled and attempting to send an empty input
// should not process the text.
send_button->SetEnabled(true);
LeftClickOn(send_button);
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
ASSERT_TRUE(question_answer_view);
EXPECT_TRUE(question_answer_view->children().empty());
// Attempting to send only whitespace should not process the text.
SubmitTestQuestion(u" ");
EXPECT_TRUE(question_answer_view->children().empty());
}
// Tests that the question textfield trims whitespace from the front and back of
// the provided text.
TEST_F(MahiPanelViewTest, QuestionTextfield_TrimWhitespace) {
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[](const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(/*answer=*/u"fake answer",
MahiResponseStatus::kSuccess);
});
// Send a question with leading and trailing whitespace.
SubmitTestQuestion(u" leading and trailing ");
// Sending the text should create a question and answer text bubble.
// The whitespace should be trimmed from the sides.
auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
ASSERT_TRUE(question_answer_view);
// TODO(b/334117521): Create a test API and use it instead of using
// `children()`.
ASSERT_EQ(question_answer_view->children().size(), 2u);
EXPECT_EQ(u"leading and trailing",
views::AsViewClass<views::Label>(
question_answer_view->children()[0]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText());
// Send a question with whitespace between the string.
const std::u16string question_text(u"whitespace between");
SubmitTestQuestion(question_text);
// Sending the text should create a question and answer text bubble.
// The whitespace should not be trimmed if it's not on the sides.
ASSERT_EQ(question_answer_view->children().size(), 4u);
EXPECT_EQ(views::AsViewClass<views::Label>(
question_answer_view->children()[2]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText(),
question_text);
}
// Tests the animated image showing when answer is loading.
TEST_F(MahiPanelViewTest, AnswerLoadingAnimation) {
// Config the mock mahi manager to answer a question asyncly.
base::test::TestFuture<void> answer_waiter;
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[&answer_waiter](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
ReturnDefaultAnswerAsyncly(answer_waiter,
MahiResponseStatus::kSuccess,
std::move(callback));
});
SubmitTestQuestion();
// After a question is posted and before an answer is loaded, the Q&A view
// should show.
auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
ASSERT_TRUE(question_answer_view);
// When the answer is loading, the view should show the question and the
// loading image.
EXPECT_EQ(question_answer_view->children().size(), 2u);
EXPECT_TRUE(question_answer_view->children()[0]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_TRUE(question_answer_view->children()[1]->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
// After the answer is loaded, the view should show the question and answer
// labels, without the loading image.
ASSERT_TRUE(answer_waiter.WaitAndClear());
EXPECT_EQ(question_answer_view->children().size(), 2u);
EXPECT_TRUE(question_answer_view->children()[0]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_TRUE(question_answer_view->children()[1]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_FALSE(panel_view()->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
// Test submitting another question.
SubmitTestQuestion();
EXPECT_EQ(question_answer_view->children().size(), 4u);
EXPECT_TRUE(question_answer_view->children()[2]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_TRUE(question_answer_view->children()[3]->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
// After 2nd answer is loaded.
ASSERT_TRUE(answer_waiter.WaitAndClear());
EXPECT_EQ(question_answer_view->children().size(), 4u);
EXPECT_TRUE(question_answer_view->children()[2]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_TRUE(question_answer_view->children()[3]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_FALSE(panel_view()->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
}
// Tests that the content scroll view scrolls to the bottom or top when being
// laid out based on the following:
// 1. Scroll to bottom when switching to the Q&A view, or when a new
// question/answer is added.
// 2. Scroll to top when switching to the summary & outlines section, or
// when refreshing the summary contents.
TEST_F(MahiPanelViewTest, ScrollViewScrollsAfterLayout) {
ON_CALL(mock_mahi_manager(), GetSummary).WillByDefault(ReturnLongSummary);
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[](const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(/*answer=*/u"answer",
MahiResponseStatus::kSuccess);
});
// Recreate panel widget so it can return a long summary.
CreatePanelWidget();
// Check that the Summary view with a long summary has a scroll bar.
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
ASSERT_TRUE(summary_outlines_section);
EXPECT_TRUE(summary_outlines_section->GetVisible());
auto* const scroll_view = views::AsViewClass<views::ScrollView>(
panel_view()->GetViewByID(mahi_constants::ViewId::kScrollView));
ASSERT_TRUE(scroll_view);
auto* const scroll_bar = scroll_view->vertical_scroll_bar();
ASSERT_TRUE(scroll_bar);
EXPECT_TRUE(scroll_bar->GetVisible());
// Switch to the Q&A view, which should initially not be scrollable.
SubmitTestQuestion(u"question");
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
ASSERT_TRUE(question_answer_view);
ASSERT_TRUE(question_answer_view->GetVisible());
views::test::RunScheduledLayout(widget());
EXPECT_FALSE(scroll_bar->GetVisible());
// Add enough questions to make the view scrollable.
while (!scroll_bar->GetVisible()) {
SubmitTestQuestion(u"question");
views::test::RunScheduledLayout(widget());
}
// Ensure that the view scrolls down after a new question is added.
int previous_scroll_bar_position = scroll_bar->GetPosition();
SubmitTestQuestion(u"question");
views::test::RunScheduledLayout(widget());
EXPECT_GT(scroll_bar->GetPosition(), previous_scroll_bar_position);
// Ensure that the scroll bar is at the end position.
previous_scroll_bar_position = scroll_bar->GetPosition();
scroll_bar->ScrollByAmount(views::ScrollBar::ScrollAmount::kEnd);
EXPECT_EQ(scroll_bar->GetPosition(), previous_scroll_bar_position);
// The view scrolls up after switching to the summary section.
const auto* const go_to_summary_outlines_button = panel_view()->GetViewByID(
mahi_constants::ViewId::kGoToSummaryOutlinesButton);
ASSERT_TRUE(go_to_summary_outlines_button);
LeftClickOn(go_to_summary_outlines_button);
views::test::RunScheduledLayout(widget());
EXPECT_TRUE(summary_outlines_section->GetVisible());
// Ensure that the scroll bar is visible.
ASSERT_TRUE(scroll_bar->GetVisible());
EXPECT_LT(scroll_bar->GetPosition(), previous_scroll_bar_position);
// Ensure that the scroll bar is at the start position.
previous_scroll_bar_position = scroll_bar->GetPosition();
scroll_bar->ScrollByAmount(views::ScrollBar::ScrollAmount::kStart);
EXPECT_EQ(scroll_bar->GetPosition(), previous_scroll_bar_position);
// Scroll down to the end position to test that refreshing the summary
// contents scrolls to the start position.
scroll_bar->ScrollByAmount(views::ScrollBar::ScrollAmount::kEnd);
EXPECT_GT(scroll_bar->GetPosition(), previous_scroll_bar_position);
previous_scroll_bar_position = scroll_bar->GetPosition();
ui_controller()->RefreshContents();
views::test::RunScheduledLayout(widget());
EXPECT_LT(scroll_bar->GetPosition(), previous_scroll_bar_position);
// Ensure again that the scroll bar is at the start position.
previous_scroll_bar_position = scroll_bar->GetPosition();
scroll_bar->ScrollByAmount(views::ScrollBar::ScrollAmount::kStart);
EXPECT_EQ(scroll_bar->GetPosition(), previous_scroll_bar_position);
}
// Verifies the mahi panel view when loading an answer with an error by
// iterating all possible errors.
TEST_F(MahiPanelViewTest, FailToGetAnswer) {
for (MahiResponseStatus error : GetMahiErrors()) {
// Configs the mock mahi manager to return answer with an `error` asyncly.
base::test::TestFuture<void> answer_waiter;
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion)
.WillOnce(
[&answer_waiter, error](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
ReturnDefaultAnswerAsyncly(answer_waiter, error,
std::move(callback));
});
base::HistogramTester histogram_tester;
const std::u16string question(u"A question that brings errors");
SubmitTestQuestion(question);
histogram_tester.ExpectBucketCount(
mahi_constants::kMahiQuestionSourceHistogramName,
MahiUiController::QuestionSource::kPanel, 1);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
// After a question is posted and before an answer is loaded, the Q&A view
// should show.
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
CHECK(question_answer_view);
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_TRUE(question_answer_view->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
CHECK(summary_outlines_section);
EXPECT_FALSE(summary_outlines_section->GetVisible());
auto* error_label_view =
views::AsViewClass<views::Label>(panel_view()->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerErrorLabel));
EXPECT_EQ(nullptr, error_label_view);
auto* const question_textfield = views::AsViewClass<views::Textfield>(
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionTextfield));
auto* const send_button = panel_view()->GetViewByID(
mahi_constants::ViewId::kAskQuestionSendButton);
// When pending answer / error, send button is disabled even if user types
// in the question text field.
question_textfield->RequestFocus();
ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
generator.PressKey(ui::KeyboardCode::VKEY_A, ui::EF_NONE);
EXPECT_EQ(question_textfield->GetText(), u"a");
EXPECT_FALSE(send_button->GetEnabled());
// Waits until an answer is loaded with an error.
ASSERT_TRUE(answer_waiter.WaitAndClear());
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_FALSE(summary_outlines_section->GetVisible());
// Checks the contents of `error_status_label`. The error should show
// inline.
error_label_view =
views::AsViewClass<views::Label>(panel_view()->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerErrorLabel));
EXPECT_TRUE(error_label_view->GetVisible());
EXPECT_EQ(
error_label_view->GetText(),
l10n_util::GetStringUTF16(mahi_utils::GetErrorStatusViewTextId(error)));
// After the pending QA is complete by the error, send button is enabled
// because the question text field is not empty.
EXPECT_TRUE(send_button->GetEnabled());
EXPECT_FALSE(question_answer_view->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
// Configs the mock mahi manager to return an answer in success.
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion)
.WillOnce(
[&answer_waiter](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
ReturnDefaultAnswerAsyncly(answer_waiter,
MahiResponseStatus::kSuccess,
std::move(callback));
});
// Asks another question.
SubmitTestQuestion(u"A new question");
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
// Loading animated image should show again.
EXPECT_TRUE(question_answer_view->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
// The error image view and the error label view should still exist.
EXPECT_TRUE(panel_view()->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerErrorImage));
EXPECT_TRUE(panel_view()->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerErrorLabel));
// Waits for the answer to load. Both the error image view and the error
// label view should still exist.
ASSERT_TRUE(answer_waiter.WaitAndClear());
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_TRUE(panel_view()->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerErrorImage));
EXPECT_TRUE(panel_view()->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerErrorLabel));
EXPECT_EQ(question_answer_view->children().size(), 4u);
EXPECT_EQ(views::AsViewClass<views::Label>(
question_answer_view->children()[3]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText(),
u"fake answer");
// Configs the mock mahi manager to return answer with an `error` again.
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion)
.WillOnce(
[&answer_waiter, error](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
ReturnDefaultAnswerAsyncly(answer_waiter, error,
std::move(callback));
});
SubmitTestQuestion(u"A new question that brings errors");
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
// Shows the new error message inline.
ASSERT_TRUE(answer_waiter.WaitAndClear());
EXPECT_EQ(question_answer_view->children().size(), 6u);
EXPECT_EQ(question_answer_view->children()[5]->children()[0]->GetID(),
mahi_constants::ViewId::kQuestionAnswerErrorImage);
EXPECT_EQ(question_answer_view->children()[5]->children()[1]->GetID(),
mahi_constants::ViewId::kQuestionAnswerErrorLabel);
EXPECT_EQ(
views::AsViewClass<views::Label>(
question_answer_view->children()[5]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerErrorLabel))
->GetText(),
l10n_util::GetStringUTF16(mahi_utils::GetErrorStatusViewTextId(error)));
CreatePanelWidget();
}
}
// Verifies the mahi panel view when loading an answer with a low quota warning.
TEST_F(MahiPanelViewTest, GetAnswerWithLowQuotaWarning) {
// Config the mock mahi manager to return an answer with a low quota warning.
base::test::TestFuture<void> answer_waiter;
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion)
.WillOnce(
[&answer_waiter](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
ReturnDefaultAnswerAsyncly(answer_waiter,
MahiResponseStatus::kLowQuota,
std::move(callback));
});
SubmitTestQuestion();
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
// After a question is posted and before an answer is loaded, the Q&A view
// should show.
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
CHECK(question_answer_view);
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_TRUE(question_answer_view->GetViewByID(
mahi_constants::ViewId::kAnswerLoadingAnimatedImage));
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
CHECK(summary_outlines_section);
EXPECT_FALSE(summary_outlines_section->GetVisible());
const auto* const error_status_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kErrorStatusView);
CHECK(error_status_view);
EXPECT_FALSE(error_status_view->GetVisible());
// Wait until an answer is loaded.
ASSERT_TRUE(answer_waiter.Wait());
// `question_answer_view` should still be visible because
// `MahiResponseStatus::kLowQuota` should not block the answer.
EXPECT_FALSE(error_status_view->GetVisible());
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_FALSE(summary_outlines_section->GetVisible());
// Check the answer bubble.
// TODO(http://b/334117521): Add a test API instead of using `children()`.
ASSERT_EQ(question_answer_view->children().size(), 2u);
EXPECT_EQ(views::AsViewClass<views::Label>(
question_answer_view->children()[1]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel))
->GetText(),
u"fake answer");
}
// Verifies the mahi panel view when loading outlines with an error by
// iterating all possible errors.
TEST_F(MahiPanelViewTest, FailToGetOutlines) {
for (MahiResponseStatus error : GetMahiErrors()) {
// Config the mock mahi manager to return outlines with an `error` asyncly.
base::test::TestFuture<void> outlines_waiter;
EXPECT_CALL(mock_mahi_manager(), GetOutlines)
.WillOnce([&outlines_waiter, error](
chromeos::MahiManager::MahiOutlinesCallback callback) {
ReturnDefaultOutlinesAsyncly(outlines_waiter, error,
std::move(callback));
});
CreatePanelWidget();
Mock::VerifyAndClear(&mock_mahi_manager());
// Before outlines are loaded with an error, the summary & outlines section
// should show.
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
CHECK(question_answer_view);
EXPECT_FALSE(question_answer_view->GetVisible());
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
CHECK(summary_outlines_section);
EXPECT_TRUE(summary_outlines_section->GetVisible());
const auto* const error_status_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kErrorStatusView);
CHECK(error_status_view);
EXPECT_FALSE(error_status_view->GetVisible());
const auto* const error_status_label = views::AsViewClass<views::Label>(
panel_view()->GetViewByID(mahi_constants::ViewId::kErrorStatusLabel));
CHECK(error_status_label);
EXPECT_TRUE(error_status_label->GetText().empty());
// Wait until outlines are loaded with an error.
ASSERT_TRUE(outlines_waiter.Wait());
EXPECT_TRUE(error_status_view->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_FALSE(summary_outlines_section->GetVisible());
// Check the contents of `error_status_label`.
EXPECT_EQ(
error_status_label->GetText(),
l10n_util::GetStringUTF16(mahi_utils::GetErrorStatusViewTextId(error)));
const auto* const retry_link =
panel_view()->GetViewByID(mahi_constants::kErrorStatusRetryLink);
ASSERT_TRUE(retry_link);
EXPECT_EQ(retry_link->GetVisible(),
mahi_utils::CalculateRetryLinkVisible(error));
if (retry_link->GetVisible()) {
// Click the `retry_link`. The mock mahi manager should be requested for a
// summary and outlines again.
views::test::RunScheduledLayout(widget());
GetEventGenerator()->MoveMouseTo(
retry_link->GetBoundsInScreen().CenterPoint());
EXPECT_CALL(mock_mahi_manager(), GetSummary);
EXPECT_CALL(mock_mahi_manager(), GetOutlines);
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion).Times(0);
GetEventGenerator()->ClickLeftButton();
Mock::VerifyAndClear(&mock_mahi_manager());
}
}
}
// Verifies the mahi panel view when loading outlines with a low quota warning.
TEST_F(MahiPanelViewTest, GetOutlinesWithLowQuotaWarning) {
// Config the mock mahi manager to return outlines with a low quota warning.
base::test::TestFuture<void> outlines_waiter;
EXPECT_CALL(mock_mahi_manager(), GetOutlines)
.WillOnce([&outlines_waiter](
chromeos::MahiManager::MahiOutlinesCallback callback) {
ReturnDefaultOutlinesAsyncly(outlines_waiter,
MahiResponseStatus::kLowQuota,
std::move(callback));
});
CreatePanelWidget();
Mock::VerifyAndClear(&mock_mahi_manager());
// Before outlines are loaded, the summary & outlines section should show.
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
CHECK(question_answer_view);
EXPECT_FALSE(question_answer_view->GetVisible());
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
CHECK(summary_outlines_section);
EXPECT_TRUE(summary_outlines_section->GetVisible());
const auto* const error_status_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kErrorStatusView);
CHECK(error_status_view);
EXPECT_FALSE(error_status_view->GetVisible());
// Wait until outlines are loaded.
ASSERT_TRUE(outlines_waiter.Wait());
// `summary_outlines_section` should still be visible because
// `MahiResponseStatus::kLowQuota` should not block the outlines.
// TODO(http://b/330643995): Check the outlines container is visible.
EXPECT_FALSE(error_status_view->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_TRUE(summary_outlines_section->GetVisible());
}
// Verifies the mahi panel view when loading summary with an error by iterating
// all possible errors.
TEST_F(MahiPanelViewTest, FailToGetSummary) {
for (MahiResponseStatus error : GetMahiErrors()) {
// Config the mock mahi manager to return a summary with an `error` asyncly.
base::test::TestFuture<void> summary_waiter;
EXPECT_CALL(mock_mahi_manager(), GetSummary)
.WillOnce([&summary_waiter,
error](chromeos::MahiManager::MahiSummaryCallback callback) {
ReturnDefaultSummaryAsyncly(summary_waiter, error,
std::move(callback));
});
CreatePanelWidget();
Mock::VerifyAndClear(&mock_mahi_manager());
// Before the summary is loaded with an error, the summary & outlines
// section should show.
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
CHECK(question_answer_view);
EXPECT_FALSE(question_answer_view->GetVisible());
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
CHECK(summary_outlines_section);
EXPECT_TRUE(summary_outlines_section->GetVisible());
const auto* const error_status_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kErrorStatusView);
CHECK(error_status_view);
EXPECT_FALSE(error_status_view->GetVisible());
const auto* const error_status_label = views::AsViewClass<views::Label>(
panel_view()->GetViewByID(mahi_constants::ViewId::kErrorStatusLabel));
CHECK(error_status_label);
EXPECT_TRUE(error_status_label->GetText().empty());
// Wait until the summary is loaded with an error.
ASSERT_TRUE(summary_waiter.Wait());
EXPECT_TRUE(error_status_view->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_FALSE(summary_outlines_section->GetVisible());
// Check the contents of `error_status_label`.
EXPECT_EQ(
error_status_label->GetText(),
l10n_util::GetStringUTF16(mahi_utils::GetErrorStatusViewTextId(error)));
const auto* const retry_link =
panel_view()->GetViewByID(mahi_constants::kErrorStatusRetryLink);
ASSERT_TRUE(retry_link);
EXPECT_EQ(retry_link->GetVisible(),
mahi_utils::CalculateRetryLinkVisible(error));
if (retry_link->GetVisible()) {
// Click the `retry_link`. The mock mahi manager should be requested for a
// summary and outlines again.
views::test::RunScheduledLayout(widget());
GetEventGenerator()->MoveMouseTo(
retry_link->GetBoundsInScreen().CenterPoint());
EXPECT_CALL(mock_mahi_manager(), GetSummary);
EXPECT_CALL(mock_mahi_manager(), GetOutlines);
EXPECT_CALL(mock_mahi_manager(), AnswerQuestion).Times(0);
GetEventGenerator()->ClickLeftButton();
Mock::VerifyAndClear(&mock_mahi_manager());
}
}
}
// Verifies the mahi panel view when loading a summary with a low quota warning.
TEST_F(MahiPanelViewTest, GetSummaryWithLowQuotaWarning) {
// Config the mock mahi manager to return a summary with a low quota warning.
base::test::TestFuture<void> summary_waiter;
EXPECT_CALL(mock_mahi_manager(), GetSummary)
.WillOnce([&summary_waiter](
chromeos::MahiManager::MahiSummaryCallback callback) {
ReturnDefaultSummaryAsyncly(
summary_waiter, MahiResponseStatus::kLowQuota, std::move(callback));
});
CreatePanelWidget();
Mock::VerifyAndClear(&mock_mahi_manager());
// Before the summary is loaded, the summary & outlines section should show.
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
CHECK(question_answer_view);
EXPECT_FALSE(question_answer_view->GetVisible());
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
CHECK(summary_outlines_section);
EXPECT_TRUE(summary_outlines_section->GetVisible());
const auto* const error_status_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kErrorStatusView);
CHECK(error_status_view);
EXPECT_FALSE(error_status_view->GetVisible());
// Wait until the summary is loaded.
ASSERT_TRUE(summary_waiter.Wait());
// `summary_outlines_section` should still be visible because
// `MahiResponseStatus::kLowQuota` should not block the summary.
EXPECT_FALSE(error_status_view->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_TRUE(summary_outlines_section->GetVisible());
const auto* const summary_label = GetSummaryLabel(panel_view());
ASSERT_TRUE(summary_label);
EXPECT_TRUE(summary_label->GetVisible());
}
// Tests that calling `RefreshSummaryContents` will update the panel's contents
// with the new data from the manager.
TEST_F(MahiPanelViewTest, RefreshSummaryContents) {
const std::u16string title1(u"Test content title");
const std::u16string summary1(u"Short summary");
const auto icon1(gfx::test::CreateImageSkia(/*size=*/128, SK_ColorBLUE));
ON_CALL(mock_mahi_manager(), GetContentTitle).WillByDefault(Return(title1));
ON_CALL(mock_mahi_manager(), GetContentIcon).WillByDefault(Return(icon1));
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault(
[&summary1](chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(summary1,
chromeos::MahiResponseStatus::kSuccess);
});
MahiPanelView mahi_view(ui_controller());
EXPECT_EQ(GetContentSourceTitle(&mahi_view), title1);
EXPECT_TRUE(gfx::test::AreBitmapsEqual(
*GetContentSourceIcon(&mahi_view).bitmap(),
*image_util::ResizeAndCropImage(icon1, mahi_constants::kContentIconSize)
.bitmap()));
EXPECT_EQ(GetSummaryLabel(&mahi_view)->GetText(), summary1);
const std::u16string title2(u"Test content title 2");
const std::u16string summary2(u"Short summary 2");
const auto icon2(gfx::test::CreateImageSkia(/*size=*/128, SK_ColorRED));
ON_CALL(mock_mahi_manager(), GetContentTitle).WillByDefault(Return(title2));
ON_CALL(mock_mahi_manager(), GetContentIcon).WillByDefault(Return(icon2));
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault(
[&summary2](chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(summary2,
chromeos::MahiResponseStatus::kSuccess);
});
ui_controller()->RefreshContents();
EXPECT_EQ(GetContentSourceTitle(&mahi_view), title2);
EXPECT_TRUE(gfx::test::AreBitmapsEqual(
*GetContentSourceIcon(&mahi_view).bitmap(),
*image_util::ResizeAndCropImage(icon2, mahi_constants::kContentIconSize)
.bitmap()));
EXPECT_EQ(GetSummaryLabel(&mahi_view)->GetText(), summary2);
}
// Tests that clicking the content source button opens the source url
// corresponding to the refreshed content shown on the Mahi panel.
TEST_F(MahiPanelViewTest, ContentSourceButtonUrlAfterRefresh) {
const GURL test_url1("https://www.google.com");
ON_CALL(mock_mahi_manager(), GetContentUrl).WillByDefault(Return(test_url1));
CreatePanelWidget();
EXPECT_CALL(
new_window_delegate(),
OpenUrl(test_url1, NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kSwitchToTab));
LeftClickOn(
panel_view()->GetViewByID(mahi_constants::ViewId::kContentSourceButton));
Mock::VerifyAndClearExpectations(&new_window_delegate());
const GURL test_url2("https://en.wikipedia.org");
ON_CALL(mock_mahi_manager(), GetContentUrl).WillByDefault(Return(test_url2));
ui_controller()->RefreshContents();
EXPECT_CALL(
new_window_delegate(),
OpenUrl(test_url2, NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kSwitchToTab));
LeftClickOn(
panel_view()->GetViewByID(mahi_constants::ViewId::kContentSourceButton));
Mock::VerifyAndClearExpectations(&new_window_delegate());
}
// Tests that refreshing Summary contents will bring the user to the Summary
// View and clear all previously added Q&A text bubbles.
TEST_F(MahiPanelViewTest, RefreshSummaryContents_TransitionToSummaryView) {
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[](const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(u"answer",
chromeos::MahiResponseStatus::kSuccess);
});
const auto* const summary_outlines_section = panel_view()->GetViewByID(
mahi_constants::ViewId::kSummaryOutlinesSection);
const auto* const question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
// Transition to Q&A view by asking a question.
SubmitTestQuestion();
EXPECT_FALSE(summary_outlines_section->GetVisible());
EXPECT_TRUE(question_answer_view->GetVisible());
EXPECT_EQ(question_answer_view->children().size(), 2u);
// Refreshing summary contents should clear all Q&A contents and transition to
// the summary view.
ui_controller()->RefreshContents();
EXPECT_TRUE(summary_outlines_section->GetVisible());
EXPECT_FALSE(question_answer_view->GetVisible());
EXPECT_TRUE(question_answer_view->children().empty());
}
TEST_F(MahiPanelViewTest, ClickMetrics) {
base::HistogramTester histogram;
// Learn more button.
histogram.ExpectBucketCount(mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kLearnMoreLink, 0);
views::test::RunScheduledLayout(widget());
views::AsViewClass<views::StyledLabel>(
panel_view()->GetViewByID(mahi_constants::ViewId::kFooterLabel))
->ClickFirstLinkForTesting();
histogram.ExpectBucketCount(mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kLearnMoreLink, 1);
histogram.ExpectTotalCount(mahi_constants::kMahiButtonClickHistogramName, 1);
auto* const send_button =
panel_view()->GetViewByID(mahi_constants::ViewId::kAskQuestionSendButton);
auto* const back_to_summary_outlines_button = panel_view()->GetViewByID(
mahi_constants::ViewId::kGoToSummaryOutlinesButton);
auto* const back_to_question_answer_button = panel_view()->GetViewByID(
mahi_constants::ViewId::kGoToQuestionAndAnswerButton);
// Send question button.
// Should not send question when the question text is empty.
views::test::RunScheduledLayout(widget());
LeftClickOn(send_button);
histogram.ExpectBucketCount(
mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kAskQuestionSendButton, 0);
histogram.ExpectTotalCount(mahi_constants::kMahiButtonClickHistogramName, 1);
// Should send question when the question text is not empty.
EXPECT_FALSE(back_to_summary_outlines_button->GetVisible());
SubmitTestQuestion(u"question text");
histogram.ExpectBucketCount(
mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kAskQuestionSendButton, 1);
histogram.ExpectTotalCount(mahi_constants::kMahiButtonClickHistogramName, 2);
// Now the back to summary outlines button is visible.
EXPECT_TRUE(back_to_summary_outlines_button->GetVisible());
// Back to summary outlines button.
views::test::RunScheduledLayout(widget());
histogram.ExpectBucketCount(
mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kGoToSummaryOutlinesButton, 0);
LeftClickOn(back_to_summary_outlines_button);
histogram.ExpectBucketCount(
mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kGoToSummaryOutlinesButton, 1);
histogram.ExpectTotalCount(mahi_constants::kMahiButtonClickHistogramName, 3);
// Back to Q&A button.
views::test::RunScheduledLayout(widget());
EXPECT_TRUE(back_to_question_answer_button->GetVisible());
histogram.ExpectBucketCount(
mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kGoToQuestionAndAnswerButton, 0);
LeftClickOn(back_to_question_answer_button);
histogram.ExpectBucketCount(
mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kGoToQuestionAndAnswerButton, 1);
histogram.ExpectTotalCount(mahi_constants::kMahiButtonClickHistogramName, 4);
// Close button.
views::test::RunScheduledLayout(widget());
histogram.ExpectBucketCount(mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kCloseButton, 0);
LeftClickOn(panel_view()->GetViewByID(mahi_constants::ViewId::kCloseButton));
histogram.ExpectBucketCount(mahi_constants::kMahiButtonClickHistogramName,
mahi_constants::PanelButton::kCloseButton, 1);
histogram.ExpectTotalCount(mahi_constants::kMahiButtonClickHistogramName, 5);
}
TEST_F(MahiPanelViewTest, UserJourneyTimeMetrics) {
base::HistogramTester histogram;
histogram.ExpectTimeBucketCount(
mahi_constants::kMahiUserJourneyTimeHistogramName, base::Seconds(3),
/*expected_count=*/0);
task_environment()->AdvanceClock(base::Seconds(3));
CreatePanelWidget();
histogram.ExpectTimeBucketCount(
mahi_constants::kMahiUserJourneyTimeHistogramName, base::Seconds(3),
/*expected_count=*/1);
task_environment()->AdvanceClock(base::Minutes(3));
CreatePanelWidget();
histogram.ExpectTimeBucketCount(
mahi_constants::kMahiUserJourneyTimeHistogramName, base::Minutes(3),
/*expected_count=*/1);
task_environment()->AdvanceClock(base::Minutes(10));
CreatePanelWidget();
histogram.ExpectTimeBucketCount(
mahi_constants::kMahiUserJourneyTimeHistogramName, base::Minutes(10),
/*expected_count=*/1);
}
TEST_F(MahiPanelViewTest, ReportQuestionCountWhenRefresh) {
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[](const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(u"answer",
chromeos::MahiResponseStatus::kSuccess);
});
// Ask one question then refresh. Verify that the recorded question count
// in this Mahi session should be one.
base::HistogramTester histogram_tester;
SubmitTestQuestion();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName, /*sample=*/1,
/*expected_count=*/0);
ui_controller()->RefreshContents();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName, /*sample=*/1,
/*expected_count=*/1);
// Ask two questions then refresh. Verify that the recorded question count
// in this Mahi session should be two.
SubmitTestQuestion();
SubmitTestQuestion();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName, /*sample=*/1,
/*expected_count=*/1);
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName, /*sample=*/2,
/*expected_count=*/0);
ui_controller()->RefreshContents();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName, /*sample=*/2,
/*expected_count=*/1);
}
TEST_F(MahiPanelViewTest, ReportQuestionCountWhenMahiPanelDestroyed) {
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[](const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(u"answer",
chromeos::MahiResponseStatus::kSuccess);
});
// Ask one question then destroy the Mahi panel. Verify that the recorded
// question count in this Mahi session should be one.
base::HistogramTester histogram_tester;
SubmitTestQuestion();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName, /*sample=*/1,
/*expected_count=*/0);
ResetPanelWidget();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName, /*sample=*/1,
/*expected_count=*/1);
// Ask two questions then destroy the Mahi panel. Verify that the recorded
// question count in this Mahi session should be two.
CreatePanelWidget();
SubmitTestQuestion();
SubmitTestQuestion();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName,
/*sample=*/1,
/*expected_count=*/1);
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName,
/*sample=*/2,
/*expected_count=*/0);
ResetPanelWidget();
histogram_tester.ExpectBucketCount(
mahi_constants::kQuestionCountPerMahiSessionHistogramName,
/*sample=*/2,
/*expected_count=*/1);
}
// Make sure that summary label is displayed correctly given any kind of text.
TEST_F(MahiPanelViewTest, RandomizedTextSummaryLabel) {
auto random_string = GetRandomString(/*max_words_count=*/500);
ON_CALL(mock_mahi_manager(), GetSummary)
.WillByDefault(
[random_string](chromeos::MahiManager::MahiSummaryCallback callback) {
std::move(callback).Run(random_string,
chromeos::MahiResponseStatus::kSuccess);
});
ui_controller()->RefreshContents();
views::test::RunScheduledLayout(widget());
auto* summary_label = views::AsViewClass<views::Label>(
panel_view()->GetViewByID(mahi_constants::ViewId::kSummaryLabel));
// Make sure the summary label is not clipped.
EXPECT_FALSE(summary_label->IsDisplayTextClipped())
<< "Summary label is clipped with the text: " << random_string;
// Make sure the label is within the bounds of its parent view.
auto* scroll_view = views::AsViewClass<views::ScrollView>(
panel_view()->GetViewByID(mahi_constants::ViewId::kScrollView));
EXPECT_LE(summary_label->width(), scroll_view->GetVisibleRect().width())
<< "Summary label width surpasses scroll view visible width: "
<< random_string;
}
// Make sure the question and answer labels are displayed correctly given any
// kind of texts.
TEST_F(MahiPanelViewTest, RandomizedTextQuestionAnswerLabels) {
auto random_answer = GetRandomString(/*max_words_count=*/100);
ON_CALL(mock_mahi_manager(), AnswerQuestion)
.WillByDefault(
[&random_answer](
const std::u16string& question, bool current_panel_content,
chromeos::MahiManager::MahiAnswerQuestionCallback callback) {
std::move(callback).Run(random_answer,
chromeos::MahiResponseStatus::kSuccess);
});
auto random_question = GetRandomString(/*max_words_count=*/100);
SubmitTestQuestion(random_question);
views::test::RunScheduledLayout(widget());
auto* question_answer_view =
panel_view()->GetViewByID(mahi_constants::ViewId::kQuestionAnswerView);
auto* question_label = views::AsViewClass<views::Label>(
question_answer_view->children()[0]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_FALSE(question_label->IsDisplayTextClipped())
<< "Question label is clipped with the text: " << random_question;
auto* scroll_view = views::AsViewClass<views::ScrollView>(
panel_view()->GetViewByID(mahi_constants::ViewId::kScrollView));
EXPECT_LE(question_label->width(), scroll_view->GetVisibleRect().width())
<< "Question label width surpasses scroll view visible width: "
<< random_answer;
auto* answer_label = views::AsViewClass<views::Label>(
question_answer_view->children()[1]->GetViewByID(
mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel));
EXPECT_FALSE(answer_label->IsDisplayTextClipped())
<< "Answer label is clipped with the text: " << random_answer;
EXPECT_LE(answer_label->width(), scroll_view->GetVisibleRect().width())
<< "Answer label width surpasses scroll view visible width: "
<< random_answer;
}
TEST_F(MahiPanelViewTest, OnlyOneFeedbackButtonCanKeepToggled) {
IconButton* thumbs_up_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsUpButton));
IconButton* thumbs_down_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsDownButton));
EXPECT_FALSE(thumbs_up_button->toggled());
EXPECT_FALSE(thumbs_down_button->toggled());
// Pressing thumbs up should toggle the button.
LeftClickOn(thumbs_up_button);
EXPECT_TRUE(thumbs_up_button->toggled());
EXPECT_FALSE(thumbs_down_button->toggled());
// Pressing thumbs down should just toggle down button on and up button off.
LeftClickOn(thumbs_down_button);
EXPECT_TRUE(thumbs_down_button->toggled());
EXPECT_FALSE(thumbs_up_button->toggled());
// Pressing thumbs up should just toggle up button on and down button off.
LeftClickOn(thumbs_up_button);
EXPECT_TRUE(thumbs_up_button->toggled());
EXPECT_FALSE(thumbs_down_button->toggled());
}
TEST_F(MahiPanelViewTest, FeedbackButtonsAllowed) {
PrefService* prefs =
Shell::Get()->session_controller()->GetActivePrefService();
prefs->SetInteger(
prefs::kHmrManagedSettings,
static_cast<int>(
mahi_utils::HmrEnterprisePolicy::kAllowedWithoutModelImprovement));
CreatePanelWidget();
EXPECT_FALSE(
panel_view()
->GetViewByID(mahi_constants::ViewId::kFeedbackButtonsContainer)
->GetVisible());
EXPECT_EQ(
l10n_util::GetStringFUTF16(
IDS_ASH_MAHI_PANEL_DISCLAIMER_FEEDBACK_DISABLED,
l10n_util::GetStringUTF16(IDS_ASH_MAHI_LEARN_MORE_LINK_LABEL_TEXT)),
static_cast<views::StyledLabel*>(
panel_view()->GetViewByID(mahi_constants::ViewId::kFooterLabel))
->GetText());
prefs->SetInteger(
prefs::kHmrManagedSettings,
static_cast<int>(
mahi_utils::HmrEnterprisePolicy::kAllowedWithModelImprovement));
CreatePanelWidget();
EXPECT_TRUE(
panel_view()
->GetViewByID(mahi_constants::ViewId::kFeedbackButtonsContainer)
->GetVisible());
EXPECT_EQ(
l10n_util::GetStringFUTF16(
IDS_ASH_MAHI_PANEL_DISCLAIMER,
l10n_util::GetStringUTF16(IDS_ASH_MAHI_LEARN_MORE_LINK_LABEL_TEXT)),
static_cast<views::StyledLabel*>(
panel_view()->GetViewByID(mahi_constants::ViewId::kFooterLabel))
->GetText());
}
TEST_F(MahiPanelViewTest, FeedbackButtonResetWhenRefresh) {
IconButton* thumbs_up_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsUpButton));
IconButton* thumbs_down_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsDownButton));
EXPECT_FALSE(thumbs_up_button->toggled());
EXPECT_FALSE(thumbs_down_button->toggled());
LeftClickOn(thumbs_up_button);
EXPECT_TRUE(thumbs_up_button->toggled());
EXPECT_FALSE(thumbs_down_button->toggled());
// Test that the feedback button is reset when content is refreshed.
ui_controller()->RefreshContents();
EXPECT_FALSE(thumbs_up_button->toggled());
EXPECT_FALSE(thumbs_down_button->toggled());
LeftClickOn(thumbs_down_button);
EXPECT_FALSE(thumbs_up_button->toggled());
EXPECT_TRUE(thumbs_down_button->toggled());
ui_controller()->RefreshContents();
EXPECT_FALSE(thumbs_up_button->toggled());
EXPECT_FALSE(thumbs_down_button->toggled());
}
TEST_F(MahiPanelViewTest, FeedbackButtonsOnError) {
base::HistogramTester histogram_tester;
base::test::TestFuture<void> summary_waiter;
EXPECT_CALL(mock_mahi_manager(), GetSummary)
.WillOnce([&summary_waiter](
chromeos::MahiManager::MahiSummaryCallback callback) {
ReturnDefaultSummaryAsyncly(summary_waiter,
MahiResponseStatus::kUnknownError,
std::move(callback));
});
CreatePanelWidget();
// Wait until the summary is loaded with an error.
ASSERT_TRUE(summary_waiter.Wait());
// Pressing thumbs up should toggle the button on and update the feedback
// histogram.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(0);
IconButton* thumbs_up_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsUpButton));
LeftClickOn(thumbs_up_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_TRUE(thumbs_up_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 1);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 0);
// Pressing thumbs down the first time should open the feedback dialog, toggle
// the button off and update the feedback histogram.
EXPECT_CALL(mock_mahi_manager(), OpenFeedbackDialog).Times(1);
IconButton* thumbs_down_button = views::AsViewClass<IconButton>(
panel_view()->GetViewByID(mahi_constants::ViewId::kThumbsDownButton));
LeftClickOn(thumbs_down_button);
Mock::VerifyAndClearExpectations(&mock_mahi_manager());
EXPECT_TRUE(thumbs_down_button->toggled());
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
true, 1);
histogram_tester.ExpectBucketCount(mahi_constants::kMahiFeedbackHistogramName,
false, 1);
}
} // namespace ash
|