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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/extensions/test_extension_system.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_constants.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_window.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_unittest_helper.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_view_cocoa.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#import "chrome/browser/ui/cocoa/view_resizer_pong.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#include "ui/base/cocoa/animation_utils.h"
#include "ui/base/theme_provider.h"
#include "ui/events/test/cocoa_test_event_utils.h"
#include "ui/gfx/image/image_skia.h"
using base::ASCIIToUTF16;
// Unit tests don't need time-consuming asynchronous animations.
@interface BookmarkBarControllerTestable : BookmarkBarController {
}
@end
@implementation BookmarkBarControllerTestable
- (id)initWithBrowser:(Browser*)browser
initialWidth:(CGFloat)initialWidth
delegate:(id<BookmarkBarControllerDelegate>)delegate
resizeDelegate:(id<ViewResizer>)resizeDelegate {
if ((self = [super initWithBrowser:browser
initialWidth:initialWidth
delegate:delegate
resizeDelegate:resizeDelegate])) {
[self setStateAnimationsEnabled:NO];
[self setInnerContentAnimationsEnabled:NO];
}
return self;
}
@end
// Just like a BookmarkBarController but openURL: is stubbed out.
@interface BookmarkBarControllerNoOpen : BookmarkBarControllerTestable {
@public
std::vector<GURL> urls_;
std::vector<WindowOpenDisposition> dispositions_;
}
@end
@implementation BookmarkBarControllerNoOpen
- (void)openURL:(GURL)url disposition:(WindowOpenDisposition)disposition {
urls_.push_back(url);
dispositions_.push_back(disposition);
}
- (void)clear {
urls_.clear();
dispositions_.clear();
}
@end
// NSCell that is pre-provided with a desired size that becomes the
// return value for -(NSSize)cellSize:.
@interface CellWithDesiredSize : NSCell {
@private
NSSize cellSize_;
}
@property (nonatomic, readonly) NSSize cellSize;
@end
@implementation CellWithDesiredSize
@synthesize cellSize = cellSize_;
- (id)initTextCell:(NSString*)string desiredSize:(NSSize)size {
if ((self = [super initTextCell:string])) {
cellSize_ = size;
}
return self;
}
@end
// Remember the number of times we've gotten a frameDidChange notification.
@interface BookmarkBarControllerTogglePong : BookmarkBarControllerNoOpen {
@private
int toggles_;
}
@property (nonatomic, readonly) int toggles;
@end
@implementation BookmarkBarControllerTogglePong
@synthesize toggles = toggles_;
- (void)frameDidChange {
toggles_++;
}
@end
// Remembers if a notification callback was called.
@interface BookmarkBarControllerNotificationPong : BookmarkBarControllerNoOpen {
BOOL windowWillCloseReceived_;
BOOL windowDidResignKeyReceived_;
}
@property (nonatomic, readonly) BOOL windowWillCloseReceived;
@property (nonatomic, readonly) BOOL windowDidResignKeyReceived;
@end
@implementation BookmarkBarControllerNotificationPong
@synthesize windowWillCloseReceived = windowWillCloseReceived_;
@synthesize windowDidResignKeyReceived = windowDidResignKeyReceived_;
// Override NSNotificationCenter callback.
- (void)parentWindowWillClose:(NSNotification*)notification {
windowWillCloseReceived_ = YES;
}
// NSNotificationCenter callback.
- (void)parentWindowDidResignKey:(NSNotification*)notification {
windowDidResignKeyReceived_ = YES;
}
@end
// Remembers if and what kind of openAll was performed.
@interface BookmarkBarControllerOpenAllPong : BookmarkBarControllerNoOpen {
WindowOpenDisposition dispositionDetected_;
}
@property (nonatomic) WindowOpenDisposition dispositionDetected;
@end
@implementation BookmarkBarControllerOpenAllPong
@synthesize dispositionDetected = dispositionDetected_;
// Intercede for the openAll:disposition: method.
- (void)openAll:(const BookmarkNode*)node
disposition:(WindowOpenDisposition)disposition {
[self setDispositionDetected:disposition];
}
@end
// Just like a BookmarkBarController but intercedes when providing
// pasteboard drag data.
@interface BookmarkBarControllerDragData : BookmarkBarControllerTestable {
const BookmarkNode* dragDataNode_; // Weak
}
- (void)setDragDataNode:(const BookmarkNode*)node;
@end
@implementation BookmarkBarControllerDragData
- (id)initWithBrowser:(Browser*)browser
initialWidth:(CGFloat)initialWidth
delegate:(id<BookmarkBarControllerDelegate>)delegate
resizeDelegate:(id<ViewResizer>)resizeDelegate {
if ((self = [super initWithBrowser:browser
initialWidth:initialWidth
delegate:delegate
resizeDelegate:resizeDelegate])) {
dragDataNode_ = NULL;
}
return self;
}
- (void)setDragDataNode:(const BookmarkNode*)node {
dragDataNode_ = node;
}
- (std::vector<const BookmarkNode*>)retrieveBookmarkNodeData {
std::vector<const BookmarkNode*> dragDataNodes;
if(dragDataNode_) {
dragDataNodes.push_back(dragDataNode_);
}
return dragDataNodes;
}
@end
class FakeTheme : public ui::ThemeProvider {
public:
FakeTheme(NSColor* color) : color_(color) {}
base::scoped_nsobject<NSColor> color_;
bool UsingSystemTheme() const override { return true; }
gfx::ImageSkia* GetImageSkiaNamed(int id) const override { return NULL; }
SkColor GetColor(int id) const override { return SkColor(); }
int GetDisplayProperty(int id) const override { return -1; }
bool ShouldUseNativeFrame() const override { return false; }
bool HasCustomImage(int id) const override { return false; }
base::RefCountedMemory* GetRawData(int id, ui::ScaleFactor scale_factor)
const override {
return NULL;
}
NSImage* GetNSImageNamed(int id) const override { return nil; }
NSColor* GetNSImageColorNamed(int id) const override { return nil; }
NSColor* GetNSColor(int id) const override { return color_.get(); }
NSColor* GetNSColorTint(int id) const override { return nil; }
NSGradient* GetNSGradient(int id) const override { return nil; }
};
@interface FakeDragInfo : NSObject {
@public
NSPoint dropLocation_;
NSDragOperation sourceMask_;
}
@property (nonatomic, assign) NSPoint dropLocation;
- (void)setDraggingSourceOperationMask:(NSDragOperation)mask;
@end
@implementation FakeDragInfo
@synthesize dropLocation = dropLocation_;
- (id)init {
if ((self = [super init])) {
dropLocation_ = NSZeroPoint;
sourceMask_ = NSDragOperationMove;
}
return self;
}
// NSDraggingInfo protocol functions.
- (id)draggingPasteboard {
return self;
}
- (id)draggingSource {
return self;
}
- (NSDragOperation)draggingSourceOperationMask {
return sourceMask_;
}
- (NSPoint)draggingLocation {
return dropLocation_;
}
// Other functions.
- (void)setDraggingSourceOperationMask:(NSDragOperation)mask {
sourceMask_ = mask;
}
@end
namespace {
class BookmarkBarControllerTestBase : public CocoaProfileTest {
public:
base::scoped_nsobject<NSView> parent_view_;
base::scoped_nsobject<ViewResizerPong> resizeDelegate_;
void SetUp() override {
CocoaProfileTest::SetUp();
ASSERT_TRUE(profile());
base::FilePath extension_dir;
static_cast<extensions::TestExtensionSystem*>(
extensions::ExtensionSystem::Get(profile()))
->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
extension_dir, false);
resizeDelegate_.reset([[ViewResizerPong alloc] init]);
NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]);
[parent_view_ setHidden:YES];
}
void InstallAndToggleBar(BookmarkBarController* bar) {
// Force loading of the nib.
[bar view];
// Awkwardness to look like we've been installed.
for (NSView* subView in [parent_view_ subviews])
[subView removeFromSuperview];
[parent_view_ addSubview:[bar view]];
NSRect frame = [[[bar view] superview] frame];
frame.origin.y = 100;
[[[bar view] superview] setFrame:frame];
// Make sure it's on in a window so viewDidMoveToWindow is called
NSView* contentView = [test_window() contentView];
if (![parent_view_ isDescendantOf:contentView])
[contentView addSubview:parent_view_];
// Make sure it's open so certain things aren't no-ops.
[bar updateState:BookmarkBar::SHOW
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
}
};
class BookmarkBarControllerTest : public BookmarkBarControllerTestBase {
public:
base::scoped_nsobject<BookmarkBarControllerNoOpen> bar_;
void SetUp() override {
BookmarkBarControllerTestBase::SetUp();
ASSERT_TRUE(browser());
AddCommandLineSwitches();
// In OSX 10.10, the owner of a nib file is retain/autoreleased during the
// initialization of the nib. Wrapping the constructor in an
// autoreleasepool ensures that tests can control the destruction timing of
// |bar_|.
@autoreleasepool {
bar_.reset([[BookmarkBarControllerNoOpen alloc]
initWithBrowser:browser()
initialWidth:NSWidth([parent_view_ frame])
delegate:nil
resizeDelegate:resizeDelegate_.get()]);
}
InstallAndToggleBar(bar_.get());
}
virtual void AddCommandLineSwitches() {}
BookmarkBarControllerNoOpen* noOpenBar() {
return (BookmarkBarControllerNoOpen*)bar_.get();
}
};
TEST_F(BookmarkBarControllerTest, ShowWhenShowBookmarkBarTrue) {
[bar_ updateState:BookmarkBar::SHOW
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_TRUE([bar_ isInState:BookmarkBar::SHOW]);
EXPECT_FALSE([bar_ isInState:BookmarkBar::DETACHED]);
EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
EXPECT_FALSE([[bar_ view] isHidden]);
EXPECT_GT([resizeDelegate_ height], 0);
EXPECT_GT([[bar_ view] frame].size.height, 0);
}
TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarFalse) {
[bar_ updateState:BookmarkBar::HIDDEN
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_FALSE([bar_ isInState:BookmarkBar::SHOW]);
EXPECT_FALSE([bar_ isInState:BookmarkBar::DETACHED]);
EXPECT_FALSE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
EXPECT_TRUE([[bar_ view] isHidden]);
EXPECT_EQ(0, [resizeDelegate_ height]);
EXPECT_EQ(0, [[bar_ view] frame].size.height);
}
TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarTrueButDisabled) {
[bar_ setBookmarkBarEnabled:NO];
[bar_ updateState:BookmarkBar::SHOW
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_TRUE([bar_ isInState:BookmarkBar::SHOW]);
EXPECT_FALSE([bar_ isInState:BookmarkBar::DETACHED]);
EXPECT_FALSE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
EXPECT_TRUE([[bar_ view] isHidden]);
EXPECT_EQ(0, [resizeDelegate_ height]);
EXPECT_EQ(0, [[bar_ view] frame].size.height);
}
TEST_F(BookmarkBarControllerTest, ShowOnNewTabPage) {
[bar_ updateState:BookmarkBar::DETACHED
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_FALSE([bar_ isInState:BookmarkBar::SHOW]);
EXPECT_TRUE([bar_ isInState:BookmarkBar::DETACHED]);
EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
EXPECT_FALSE([[bar_ view] isHidden]);
EXPECT_GT([resizeDelegate_ height], 0);
EXPECT_GT([[bar_ view] frame].size.height, 0);
// Make sure no buttons fall off the bar, either now or when resized
// bigger or smaller.
CGFloat sizes[] = { 300.0, -100.0, 200.0, -420.0 };
CGFloat previousX = 0.0;
for (unsigned x = 0; x < arraysize(sizes); x++) {
// Confirm the buttons moved from the last check (which may be
// init but that's fine).
CGFloat newX = [[bar_ offTheSideButton] frame].origin.x;
EXPECT_NE(previousX, newX);
previousX = newX;
// Confirm the buttons have a reasonable bounds. Recall that |-frame|
// returns rectangles in the superview's coordinates.
NSRect buttonViewFrame =
[[bar_ buttonView] convertRect:[[bar_ buttonView] frame]
fromView:[[bar_ buttonView] superview]];
EXPECT_EQ([bar_ buttonView], [[bar_ offTheSideButton] superview]);
EXPECT_TRUE(NSContainsRect(buttonViewFrame,
[[bar_ offTheSideButton] frame]));
EXPECT_EQ([bar_ buttonView], [[bar_ otherBookmarksButton] superview]);
EXPECT_TRUE(NSContainsRect(buttonViewFrame,
[[bar_ otherBookmarksButton] frame]));
// Now move them implicitly.
// We confirm FrameChangeNotification works in the next unit test;
// we simply assume it works here to resize or reposition the
// buttons above.
NSRect frame = [[bar_ view] frame];
frame.size.width += sizes[x];
[[bar_ view] setFrame:frame];
}
}
// Test whether |-updateState:...| sets currentState as expected. Make
// sure things don't crash.
TEST_F(BookmarkBarControllerTest, StateChanges) {
// First, go in one-at-a-time cycle.
[bar_ updateState:BookmarkBar::HIDDEN
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_EQ(BookmarkBar::HIDDEN, [bar_ currentState]);
EXPECT_FALSE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
[bar_ updateState:BookmarkBar::SHOW
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_EQ(BookmarkBar::SHOW, [bar_ currentState]);
EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
[bar_ updateState:BookmarkBar::DETACHED
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_EQ(BookmarkBar::DETACHED, [bar_ currentState]);
EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
// Now try some "jumps".
for (int i = 0; i < 2; i++) {
[bar_ updateState:BookmarkBar::HIDDEN
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_EQ(BookmarkBar::HIDDEN, [bar_ currentState]);
EXPECT_FALSE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
[bar_ updateState:BookmarkBar::SHOW
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_EQ(BookmarkBar::SHOW, [bar_ currentState]);
EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
}
// Now try some "jumps".
for (int i = 0; i < 2; i++) {
[bar_ updateState:BookmarkBar::SHOW
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_EQ(BookmarkBar::SHOW, [bar_ currentState]);
EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
[bar_ updateState:BookmarkBar::DETACHED
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_EQ(BookmarkBar::DETACHED, [bar_ currentState]);
EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([bar_ isAnimationRunning]);
}
}
// Make sure we're watching for frame change notifications.
TEST_F(BookmarkBarControllerTest, FrameChangeNotification) {
base::scoped_nsobject<BookmarkBarControllerTogglePong> bar;
bar.reset(
[[BookmarkBarControllerTogglePong alloc]
initWithBrowser:browser()
initialWidth:100 // arbitrary
delegate:nil
resizeDelegate:resizeDelegate_.get()]);
InstallAndToggleBar(bar.get());
// Send a frame did change notification for the pong's view.
[[NSNotificationCenter defaultCenter]
postNotificationName:NSViewFrameDidChangeNotification
object:[bar view]];
EXPECT_GT([bar toggles], 0);
}
// Confirm our "no items" container goes away when we add the 1st
// bookmark, and comes back when we delete the bookmark.
TEST_F(BookmarkBarControllerTest, NoItemContainerGoesAway) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* bar = model->bookmark_bar_node();
[bar_ loaded:model];
BookmarkBarView* view = [bar_ buttonView];
DCHECK(view);
NSView* noItemContainer = [view noItemContainer];
DCHECK(noItemContainer);
EXPECT_FALSE([noItemContainer isHidden]);
const BookmarkNode* node = model->AddURL(bar, bar->child_count(),
ASCIIToUTF16("title"),
GURL("http://www.google.com"));
EXPECT_TRUE([noItemContainer isHidden]);
model->Remove(bar, bar->GetIndexOf(node));
EXPECT_FALSE([noItemContainer isHidden]);
// Now try it using a bookmark from the Other Bookmarks.
const BookmarkNode* otherBookmarks = model->other_node();
node = model->AddURL(otherBookmarks, otherBookmarks->child_count(),
ASCIIToUTF16("TheOther"),
GURL("http://www.other.com"));
EXPECT_FALSE([noItemContainer isHidden]);
// Move it from Other Bookmarks to the bar.
model->Move(node, bar, 0);
EXPECT_TRUE([noItemContainer isHidden]);
// Move it back to Other Bookmarks from the bar.
model->Move(node, otherBookmarks, 0);
EXPECT_FALSE([noItemContainer isHidden]);
}
// Confirm off the side button only enabled when reasonable.
TEST_F(BookmarkBarControllerTest, OffTheSideButtonHidden) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
[bar_ loaded:model];
EXPECT_TRUE([bar_ offTheSideButtonIsHidden]);
for (int i = 0; i < 2; i++) {
bookmarks::AddIfNotBookmarked(
model, GURL("http://www.foo.com"), ASCIIToUTF16("small"));
EXPECT_TRUE([bar_ offTheSideButtonIsHidden]);
}
const BookmarkNode* parent = model->bookmark_bar_node();
for (int i = 0; i < 20; i++) {
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("super duper wide title"),
GURL("http://superfriends.hall-of-justice.edu"));
}
EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
// Open the "off the side" and start deleting nodes. Make sure
// deletion of the last node in "off the side" causes the folder to
// close.
EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
NSButton* offTheSideButton = [bar_ offTheSideButton];
// Open "off the side" menu.
[bar_ openOffTheSideFolderFromButton:offTheSideButton];
BookmarkBarFolderController* bbfc = [bar_ folderController];
EXPECT_TRUE(bbfc);
[bbfc setIgnoreAnimations:YES];
while (!parent->empty()) {
// We've completed the job so we're done.
if ([bar_ offTheSideButtonIsHidden])
break;
// Delete the last button.
model->Remove(parent, parent->child_count() - 1);
// If last one make sure the menu is closed and the button is hidden.
// Else make sure menu stays open.
if ([bar_ offTheSideButtonIsHidden]) {
EXPECT_FALSE([bar_ folderController]);
} else {
EXPECT_TRUE([bar_ folderController]);
}
}
}
// http://crbug.com/46175 is a crash when deleting bookmarks from the
// off-the-side menu while it is open. This test tries to bang hard
// in this area to reproduce the crash.
TEST_F(BookmarkBarControllerTest, DeleteFromOffTheSideWhileItIsOpen) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
[bar_ loaded:model];
// Add a lot of bookmarks (per the bug).
const BookmarkNode* parent = model->bookmark_bar_node();
for (int i = 0; i < 100; i++) {
std::ostringstream title;
title << "super duper wide title " << i;
model->AddURL(parent, parent->child_count(), ASCIIToUTF16(title.str()),
GURL("http://superfriends.hall-of-justice.edu"));
}
EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
// Open "off the side" menu.
NSButton* offTheSideButton = [bar_ offTheSideButton];
[bar_ openOffTheSideFolderFromButton:offTheSideButton];
BookmarkBarFolderController* bbfc = [bar_ folderController];
EXPECT_TRUE(bbfc);
[bbfc setIgnoreAnimations:YES];
// Start deleting items; try and delete randomish ones in case it
// makes a difference.
int indices[] = { 2, 4, 5, 1, 7, 9, 2, 0, 10, 9 };
while (!parent->empty()) {
for (unsigned int i = 0; i < arraysize(indices); i++) {
if (indices[i] < parent->child_count()) {
// First we mouse-enter the button to make things harder.
NSArray* buttons = [bbfc buttons];
for (BookmarkButton* button in buttons) {
if ([button bookmarkNode] == parent->GetChild(indices[i])) {
[bbfc mouseEnteredButton:button event:nil];
break;
}
}
// Then we remove the node. This triggers the button to get
// deleted.
model->Remove(parent, indices[i]);
// Force visual update which is otherwise delayed.
[[bbfc window] displayIfNeeded];
}
}
}
}
// Test whether |-dragShouldLockBarVisibility| returns NO iff the bar is
// detached.
TEST_F(BookmarkBarControllerTest, TestDragShouldLockBarVisibility) {
[bar_ updateState:BookmarkBar::HIDDEN
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_TRUE([bar_ dragShouldLockBarVisibility]);
[bar_ updateState:BookmarkBar::SHOW
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_TRUE([bar_ dragShouldLockBarVisibility]);
[bar_ updateState:BookmarkBar::DETACHED
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_FALSE([bar_ dragShouldLockBarVisibility]);
}
TEST_F(BookmarkBarControllerTest, TagMap) {
int64 ids[] = { 1, 3, 4, 40, 400, 4000, 800000000, 2, 123456789 };
std::vector<int32> tags;
// Generate some tags
for (unsigned int i = 0; i < arraysize(ids); i++) {
tags.push_back([bar_ menuTagFromNodeId:ids[i]]);
}
// Confirm reverse mapping.
for (unsigned int i = 0; i < arraysize(ids); i++) {
EXPECT_EQ(ids[i], [bar_ nodeIdFromMenuTag:tags[i]]);
}
// Confirm uniqueness.
std::sort(tags.begin(), tags.end());
for (unsigned int i=0; i<(tags.size()-1); i++) {
EXPECT_NE(tags[i], tags[i+1]);
}
}
TEST_F(BookmarkBarControllerTest, MenuForFolderNode) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
// First make sure something (e.g. "(empty)" string) is always present.
NSMenu* menu = [bar_ menuForFolderNode:model->bookmark_bar_node()];
EXPECT_GT([menu numberOfItems], 0);
// Test two bookmarks.
GURL gurl("http://www.foo.com");
bookmarks::AddIfNotBookmarked(model, gurl, ASCIIToUTF16("small"));
bookmarks::AddIfNotBookmarked(
model, GURL("http://www.cnn.com"), ASCIIToUTF16("bigger title"));
menu = [bar_ menuForFolderNode:model->bookmark_bar_node()];
EXPECT_EQ([menu numberOfItems], 2);
NSMenuItem *item = [menu itemWithTitle:@"bigger title"];
EXPECT_TRUE(item);
item = [menu itemWithTitle:@"small"];
EXPECT_TRUE(item);
if (item) {
int64 tag = [bar_ nodeIdFromMenuTag:[item tag]];
const BookmarkNode* node = bookmarks::GetBookmarkNodeByID(model, tag);
EXPECT_TRUE(node);
EXPECT_EQ(gurl, node->url());
}
// Test with an actual folder as well
const BookmarkNode* parent = model->bookmark_bar_node();
const BookmarkNode* folder = model->AddFolder(parent,
parent->child_count(),
ASCIIToUTF16("folder"));
model->AddURL(folder, folder->child_count(),
ASCIIToUTF16("f1"), GURL("http://framma-lamma.com"));
model->AddURL(folder, folder->child_count(),
ASCIIToUTF16("f2"), GURL("http://framma-lamma-ding-dong.com"));
menu = [bar_ menuForFolderNode:model->bookmark_bar_node()];
EXPECT_EQ([menu numberOfItems], 3);
item = [menu itemWithTitle:@"folder"];
EXPECT_TRUE(item);
EXPECT_TRUE([item hasSubmenu]);
NSMenu *submenu = [item submenu];
EXPECT_TRUE(submenu);
EXPECT_EQ(2, [submenu numberOfItems]);
EXPECT_TRUE([submenu itemWithTitle:@"f1"]);
EXPECT_TRUE([submenu itemWithTitle:@"f2"]);
}
// Confirm openBookmark: forwards the request to the controller's delegate
TEST_F(BookmarkBarControllerTest, OpenBookmark) {
GURL gurl("http://walla.walla.ding.dong.com");
scoped_ptr<BookmarkNode> node(new BookmarkNode(gurl));
base::scoped_nsobject<BookmarkButtonCell> cell(
[[BookmarkButtonCell alloc] init]);
[cell setBookmarkNode:node.get()];
base::scoped_nsobject<BookmarkButton> button([[BookmarkButton alloc] init]);
[button setCell:cell.get()];
[cell setRepresentedObject:[NSValue valueWithPointer:node.get()]];
[bar_ openBookmark:button];
EXPECT_EQ(noOpenBar()->urls_[0], node->url());
EXPECT_EQ(noOpenBar()->dispositions_[0], CURRENT_TAB);
}
TEST_F(BookmarkBarControllerTest, TestAddRemoveAndClear) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
NSView* buttonView = [bar_ buttonView];
EXPECT_EQ(0U, [[bar_ buttons] count]);
unsigned int initial_subview_count = [[buttonView subviews] count];
// Make sure a redundant call doesn't choke
[bar_ clearBookmarkBar];
EXPECT_EQ(0U, [[bar_ buttons] count]);
EXPECT_EQ(initial_subview_count, [[buttonView subviews] count]);
GURL gurl1("http://superfriends.hall-of-justice.edu");
// Short titles increase the chances of this test succeeding if the view is
// narrow.
// TODO(viettrungluu): make the test independent of window/view size, font
// metrics, button size and spacing, and everything else.
base::string16 title1(ASCIIToUTF16("x"));
bookmarks::AddIfNotBookmarked(model, gurl1, title1);
EXPECT_EQ(1U, [[bar_ buttons] count]);
EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]);
GURL gurl2("http://legion-of-doom.gov");
base::string16 title2(ASCIIToUTF16("y"));
bookmarks::AddIfNotBookmarked(model, gurl2, title2);
EXPECT_EQ(2U, [[bar_ buttons] count]);
EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]);
for (int i = 0; i < 3; i++) {
bookmarks::RemoveAllBookmarks(model, gurl2);
EXPECT_EQ(1U, [[bar_ buttons] count]);
EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]);
// and bring it back
bookmarks::AddIfNotBookmarked(model, gurl2, title2);
EXPECT_EQ(2U, [[bar_ buttons] count]);
EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]);
}
[bar_ clearBookmarkBar];
EXPECT_EQ(0U, [[bar_ buttons] count]);
EXPECT_EQ(initial_subview_count, [[buttonView subviews] count]);
// Explicit test of loaded: since this is a convenient spot
[bar_ loaded:model];
EXPECT_EQ(2U, [[bar_ buttons] count]);
EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]);
}
// Make sure we don't create too many buttons; we only really need
// ones that will be visible.
TEST_F(BookmarkBarControllerTest, TestButtonLimits) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
EXPECT_EQ(0U, [[bar_ buttons] count]);
// Add one; make sure we see it.
const BookmarkNode* parent = model->bookmark_bar_node();
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("title"), GURL("http://www.google.com"));
EXPECT_EQ(1U, [[bar_ buttons] count]);
// Add 30 which we expect to be 'too many'. Make sure we don't see
// 30 buttons.
model->Remove(parent, 0);
EXPECT_EQ(0U, [[bar_ buttons] count]);
for (int i=0; i<30; i++) {
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("title"), GURL("http://www.google.com"));
}
int count = [[bar_ buttons] count];
EXPECT_LT(count, 30L);
// Add 10 more (to the front of the list so the on-screen buttons
// would change) and make sure the count stays the same.
for (int i=0; i<10; i++) {
model->AddURL(parent, 0, /* index is 0, so front, not end */
ASCIIToUTF16("title"), GURL("http://www.google.com"));
}
// Finally, grow the view and make sure the button count goes up.
NSRect frame = [[bar_ view] frame];
frame.size.width += 600;
[[bar_ view] setFrame:frame];
int finalcount = [[bar_ buttons] count];
EXPECT_GT(finalcount, count);
}
// Make sure that each button we add marches to the right and does not
// overlap with the previous one.
TEST_F(BookmarkBarControllerTest, TestButtonMarch) {
base::scoped_nsobject<NSMutableArray> cells([[NSMutableArray alloc] init]);
CGFloat widths[] = { 10, 10, 100, 10, 500, 500, 80000, 60000, 1, 345 };
for (unsigned int i = 0; i < arraysize(widths); i++) {
NSCell* cell = [[CellWithDesiredSize alloc]
initTextCell:@"foo"
desiredSize:NSMakeSize(widths[i], 30)];
[cells addObject:cell];
[cell release];
}
int x_offset = 0;
CGFloat x_end = x_offset; // end of the previous button
for (unsigned int i = 0; i < arraysize(widths); i++) {
NSRect r = [bar_ frameForBookmarkButtonFromCell:[cells objectAtIndex:i]
xOffset:&x_offset];
EXPECT_GE(r.origin.x, x_end);
x_end = NSMaxX(r);
}
}
TEST_F(BookmarkBarControllerTest, CheckForGrowth) {
WithNoAnimation at_all; // Turn off Cocoa auto animation in this scope.
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
GURL gurl1("http://www.google.com");
base::string16 title1(ASCIIToUTF16("x"));
bookmarks::AddIfNotBookmarked(model, gurl1, title1);
GURL gurl2("http://www.google.com/blah");
base::string16 title2(ASCIIToUTF16("y"));
bookmarks::AddIfNotBookmarked(model, gurl2, title2);
EXPECT_EQ(2U, [[bar_ buttons] count]);
CGFloat width_1 = [[[bar_ buttons] objectAtIndex:0] frame].size.width;
CGFloat x_2 = [[[bar_ buttons] objectAtIndex:1] frame].origin.x;
NSButton* first = [[bar_ buttons] objectAtIndex:0];
[[first cell] setTitle:@"This is a really big title; watch out mom!"];
[bar_ checkForBookmarkButtonGrowth:first];
// Make sure the 1st button is now wider, the 2nd one is moved over,
// and they don't overlap.
NSRect frame_1 = [[[bar_ buttons] objectAtIndex:0] frame];
NSRect frame_2 = [[[bar_ buttons] objectAtIndex:1] frame];
EXPECT_GT(frame_1.size.width, width_1);
EXPECT_GT(frame_2.origin.x, x_2);
EXPECT_GE(frame_2.origin.x, frame_1.origin.x + frame_1.size.width);
}
TEST_F(BookmarkBarControllerTest, DeleteBookmark) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const char* urls[] = { "https://secret.url.com",
"http://super.duper.web.site.for.doodz.gov",
"http://www.foo-bar-baz.com/" };
const BookmarkNode* parent = model->bookmark_bar_node();
for (unsigned int i = 0; i < arraysize(urls); i++) {
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("title"), GURL(urls[i]));
}
EXPECT_EQ(3, parent->child_count());
const BookmarkNode* middle_node = parent->GetChild(1);
model->Remove(middle_node->parent(),
middle_node->parent()->GetIndexOf(middle_node));
EXPECT_EQ(2, parent->child_count());
EXPECT_EQ(parent->GetChild(0)->url(), GURL(urls[0]));
// node 2 moved into spot 1
EXPECT_EQ(parent->GetChild(1)->url(), GURL(urls[2]));
}
// TODO(jrg): write a test to confirm that nodeFaviconLoaded calls
// checkForBookmarkButtonGrowth:.
TEST_F(BookmarkBarControllerTest, Cell) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
[bar_ loaded:model];
const BookmarkNode* parent = model->bookmark_bar_node();
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("supertitle"),
GURL("http://superfriends.hall-of-justice.edu"));
const BookmarkNode* node = parent->GetChild(0);
NSCell* cell = [bar_ cellForBookmarkNode:node];
EXPECT_TRUE(cell);
EXPECT_NSEQ(@"supertitle", [cell title]);
EXPECT_EQ(node, [[cell representedObject] pointerValue]);
EXPECT_TRUE([cell menu]);
// Empty cells still have a menu.
cell = [bar_ cellForBookmarkNode:nil];
EXPECT_TRUE([cell menu]);
// Even empty cells have a title (of "(empty)")
EXPECT_TRUE([cell title]);
// cell is autoreleased; no need to release here
}
// Test drawing, mostly to ensure nothing leaks or crashes.
TEST_F(BookmarkBarControllerTest, Display) {
[[bar_ view] display];
}
// Test that middle clicking on a bookmark button results in an open action,
// except for offTheSideButton, as it just opens its folder menu.
TEST_F(BookmarkBarControllerTest, MiddleClick) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
GURL gurl1("http://www.google.com/");
base::string16 title1(ASCIIToUTF16("x"));
bookmarks::AddIfNotBookmarked(model, gurl1, title1);
EXPECT_EQ(1U, [[bar_ buttons] count]);
NSButton* first = [[bar_ buttons] objectAtIndex:0];
EXPECT_TRUE(first);
[first otherMouseUp:
cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0)];
EXPECT_EQ(noOpenBar()->urls_.size(), 1U);
// Test for offTheSideButton.
// Add more bookmarks so that offTheSideButton is visible.
const BookmarkNode* parent = model->bookmark_bar_node();
for (int i = 0; i < 20; i++) {
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("super duper wide title"),
GURL("http://superfriends.hall-of-justice.edu"));
}
EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
NSButton* offTheSideButton = [bar_ offTheSideButton];
EXPECT_TRUE(offTheSideButton);
[offTheSideButton otherMouseUp:
cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0)];
// Middle click on offTheSideButton should not open any bookmarks under it,
// therefore urls size should still be 1.
EXPECT_EQ(noOpenBar()->urls_.size(), 1U);
// Check that folderController should not be NULL since offTheSideButton
// folder is currently open.
BookmarkBarFolderController* bbfc = [bar_ folderController];
EXPECT_TRUE(bbfc);
EXPECT_TRUE([bbfc parentButton] == offTheSideButton);
// Middle clicking again on it should close the folder.
[offTheSideButton otherMouseUp:
cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0)];
bbfc = [bar_ folderController];
EXPECT_FALSE(bbfc);
}
TEST_F(BookmarkBarControllerTest, DisplaysHelpMessageOnEmpty) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
[bar_ loaded:model];
EXPECT_FALSE([[[bar_ buttonView] noItemContainer] isHidden]);
}
TEST_F(BookmarkBarControllerTest, HidesHelpMessageWithBookmark) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* parent = model->bookmark_bar_node();
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("title"), GURL("http://one.com"));
[bar_ loaded:model];
EXPECT_TRUE([[[bar_ buttonView] noItemContainer] isHidden]);
}
TEST_F(BookmarkBarControllerTest, BookmarkButtonSizing) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* parent = model->bookmark_bar_node();
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("title"), GURL("http://one.com"));
[bar_ loaded:model];
// Make sure the internal bookmark button also is the correct height.
NSArray* buttons = [bar_ buttons];
EXPECT_GT([buttons count], 0u);
for (NSButton* button in buttons) {
EXPECT_FLOAT_EQ(
(chrome::kBookmarkBarHeight + bookmarks::kVisualHeightOffset) -
2 * bookmarks::kBookmarkVerticalPadding,
[button frame].size.height);
}
}
TEST_F(BookmarkBarControllerTest, DropBookmarks) {
const char* urls[] = {
"http://qwantz.com",
"http://xkcd.com",
"javascript:alert('lolwut')",
"file://localhost/tmp/local-file.txt" // As if dragged from the desktop.
};
const char* titles[] = {
"Philosophoraptor",
"Can't draw",
"Inspiration",
"Frum stuf"
};
EXPECT_EQ(arraysize(urls), arraysize(titles));
NSMutableArray* nsurls = [NSMutableArray array];
NSMutableArray* nstitles = [NSMutableArray array];
for (size_t i = 0; i < arraysize(urls); ++i) {
[nsurls addObject:base::SysUTF8ToNSString(urls[i])];
[nstitles addObject:base::SysUTF8ToNSString(titles[i])];
}
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* parent = model->bookmark_bar_node();
[bar_ addURLs:nsurls withTitles:nstitles at:NSZeroPoint];
EXPECT_EQ(4, parent->child_count());
for (int i = 0; i < parent->child_count(); ++i) {
GURL gurl = parent->GetChild(i)->url();
if (gurl.scheme() == "http" ||
gurl.scheme() == "javascript") {
EXPECT_EQ(parent->GetChild(i)->url(), GURL(urls[i]));
} else {
// Be flexible if the scheme needed to be added.
std::string gurl_string = gurl.spec();
std::string my_string = parent->GetChild(i)->url().spec();
EXPECT_NE(gurl_string.find(my_string), std::string::npos);
}
EXPECT_EQ(parent->GetChild(i)->GetTitle(), ASCIIToUTF16(titles[i]));
}
}
TEST_F(BookmarkBarControllerTest, TestDragButton) {
WithNoAnimation at_all;
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
GURL gurls[] = { GURL("http://www.google.com/a"),
GURL("http://www.google.com/b"),
GURL("http://www.google.com/c") };
base::string16 titles[] = { ASCIIToUTF16("a"),
ASCIIToUTF16("b"),
ASCIIToUTF16("c") };
for (unsigned i = 0; i < arraysize(titles); i++)
bookmarks::AddIfNotBookmarked(model, gurls[i], titles[i]);
EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:0] title]);
[bar_ dragButton:[[bar_ buttons] objectAtIndex:2]
to:NSZeroPoint
copy:NO];
EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:0] title]);
// Make sure a 'copy' did not happen.
EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
[bar_ dragButton:[[bar_ buttons] objectAtIndex:1]
to:NSMakePoint(1000, 0)
copy:NO];
EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:0] title]);
EXPECT_NSEQ(@"b", [[[bar_ buttons] objectAtIndex:1] title]);
EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:2] title]);
EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
// A drop of the 1st between the next 2.
CGFloat x = NSMinX([[[bar_ buttons] objectAtIndex:2] frame]);
x += [[bar_ view] frame].origin.x;
[bar_ dragButton:[[bar_ buttons] objectAtIndex:0]
to:NSMakePoint(x, 0)
copy:NO];
EXPECT_NSEQ(@"b", [[[bar_ buttons] objectAtIndex:0] title]);
EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:1] title]);
EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:2] title]);
EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
// A drop on a non-folder button. (Shouldn't try and go in it.)
x = NSMidX([[[bar_ buttons] objectAtIndex:0] frame]);
x += [[bar_ view] frame].origin.x;
[bar_ dragButton:[[bar_ buttons] objectAtIndex:2]
to:NSMakePoint(x, 0)
copy:NO];
EXPECT_EQ(arraysize(titles), [[bar_ buttons] count]);
// A drop on a folder button.
const BookmarkNode* folder = model->AddFolder(
model->bookmark_bar_node(), 0, ASCIIToUTF16("awesome folder"));
DCHECK(folder);
model->AddURL(folder, 0, ASCIIToUTF16("already"),
GURL("http://www.google.com"));
EXPECT_EQ(arraysize(titles) + 1, [[bar_ buttons] count]);
EXPECT_EQ(1, folder->child_count());
x = NSMidX([[[bar_ buttons] objectAtIndex:0] frame]);
x += [[bar_ view] frame].origin.x;
base::string16 title =
[[[bar_ buttons] objectAtIndex:2] bookmarkNode]->GetTitle();
[bar_ dragButton:[[bar_ buttons] objectAtIndex:2]
to:NSMakePoint(x, 0)
copy:NO];
// Gone from the bar
EXPECT_EQ(arraysize(titles), [[bar_ buttons] count]);
// In the folder
EXPECT_EQ(2, folder->child_count());
// At the end
EXPECT_EQ(title, folder->GetChild(1)->GetTitle());
}
TEST_F(BookmarkBarControllerTest, TestCopyButton) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
GURL gurls[] = { GURL("http://www.google.com/a"),
GURL("http://www.google.com/b"),
GURL("http://www.google.com/c") };
base::string16 titles[] = { ASCIIToUTF16("a"),
ASCIIToUTF16("b"),
ASCIIToUTF16("c") };
for (unsigned i = 0; i < arraysize(titles); i++)
bookmarks::AddIfNotBookmarked(model, gurls[i], titles[i]);
EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:0] title]);
// Drag 'a' between 'b' and 'c'.
CGFloat x = NSMinX([[[bar_ buttons] objectAtIndex:2] frame]);
x += [[bar_ view] frame].origin.x;
[bar_ dragButton:[[bar_ buttons] objectAtIndex:0]
to:NSMakePoint(x, 0)
copy:YES];
EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:0] title]);
EXPECT_NSEQ(@"b", [[[bar_ buttons] objectAtIndex:1] title]);
EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:2] title]);
EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:3] title]);
EXPECT_EQ([[bar_ buttons] count], 4U);
}
// Fake a theme with colored text. Apply it and make sure bookmark
// buttons have the same colored text. Repeat more than once.
TEST_F(BookmarkBarControllerTest, TestThemedButton) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
bookmarks::AddIfNotBookmarked(
model, GURL("http://www.foo.com"), ASCIIToUTF16("small"));
BookmarkButton* button = [[bar_ buttons] objectAtIndex:0];
EXPECT_TRUE(button);
NSArray* colors = [NSArray arrayWithObjects:[NSColor redColor],
[NSColor blueColor],
nil];
for (NSColor* color in colors) {
FakeTheme theme(color);
[bar_ updateTheme:&theme];
NSAttributedString* astr = [button attributedTitle];
EXPECT_TRUE(astr);
EXPECT_NSEQ(@"small", [astr string]);
// Pick a char in the middle to test (index 3)
NSDictionary* attributes = [astr attributesAtIndex:3 effectiveRange:NULL];
NSColor* newColor =
[attributes objectForKey:NSForegroundColorAttributeName];
EXPECT_NSEQ(newColor, color);
}
}
// Test that delegates and targets of buttons are cleared on dealloc.
TEST_F(BookmarkBarControllerTest, TestClearOnDealloc) {
// Make some bookmark buttons.
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
GURL gurls[] = { GURL("http://www.foo.com/"),
GURL("http://www.bar.com/"),
GURL("http://www.baz.com/") };
base::string16 titles[] = { ASCIIToUTF16("a"),
ASCIIToUTF16("b"),
ASCIIToUTF16("c") };
for (size_t i = 0; i < arraysize(titles); i++)
bookmarks::AddIfNotBookmarked(model, gurls[i], titles[i]);
// Get and retain the buttons so we can examine them after dealloc.
base::scoped_nsobject<NSArray> buttons([[bar_ buttons] retain]);
EXPECT_EQ([buttons count], arraysize(titles));
// Make sure that everything is set.
for (BookmarkButton* button in buttons.get()) {
ASSERT_TRUE([button isKindOfClass:[BookmarkButton class]]);
EXPECT_TRUE([button delegate]);
EXPECT_TRUE([button target]);
EXPECT_TRUE([button action]);
}
// This will dealloc....
bar_.reset();
// Make sure that everything is cleared.
for (BookmarkButton* button in buttons.get()) {
EXPECT_FALSE([button delegate]);
EXPECT_FALSE([button target]);
EXPECT_FALSE([button action]);
}
}
TEST_F(BookmarkBarControllerTest, TestFolders) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
// Create some folder buttons.
const BookmarkNode* parent = model->bookmark_bar_node();
const BookmarkNode* folder = model->AddFolder(parent,
parent->child_count(),
ASCIIToUTF16("folder"));
model->AddURL(folder, folder->child_count(),
ASCIIToUTF16("f1"), GURL("http://framma-lamma.com"));
folder = model->AddFolder(parent, parent->child_count(),
ASCIIToUTF16("empty"));
EXPECT_EQ([[bar_ buttons] count], 2U);
// First confirm mouseEntered does nothing if "menus" aren't active.
NSEvent* event =
cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0);
[bar_ mouseEnteredButton:[[bar_ buttons] objectAtIndex:0] event:event];
EXPECT_FALSE([bar_ folderController]);
// Make one active. Entering it is now a no-op.
[bar_ openBookmarkFolderFromButton:[[bar_ buttons] objectAtIndex:0]];
BookmarkBarFolderController* bbfc = [bar_ folderController];
EXPECT_TRUE(bbfc);
[bar_ mouseEnteredButton:[[bar_ buttons] objectAtIndex:0] event:event];
EXPECT_EQ(bbfc, [bar_ folderController]);
// Enter a different one; a new folderController is active.
[bar_ mouseEnteredButton:[[bar_ buttons] objectAtIndex:1] event:event];
EXPECT_NE(bbfc, [bar_ folderController]);
// Confirm exited is a no-op.
[bar_ mouseExitedButton:[[bar_ buttons] objectAtIndex:1] event:event];
EXPECT_NE(bbfc, [bar_ folderController]);
// Clean up.
[bar_ closeBookmarkFolder:nil];
}
// Verify that the folder menu presentation properly tracks mouse movements
// over the bar. Until there is a click no folder menus should show. After a
// click on a folder folder menus should show until another click on a folder
// button, and a click outside the bar and its folder menus.
TEST_F(BookmarkBarControllerTest, TestFolderButtons) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b 4f:[ 4f1b 4f2b ] ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
// Validate initial model and that we do not have a folder controller.
std::string actualModelString = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actualModelString);
EXPECT_FALSE([bar_ folderController]);
// Add a real bookmark so we can click on it.
const BookmarkNode* folder = root->GetChild(3);
model->AddURL(folder, folder->child_count(), ASCIIToUTF16("CLICK ME"),
GURL("http://www.google.com/"));
// Click on a folder button.
BookmarkButton* button = [bar_ buttonWithTitleEqualTo:@"4f"];
EXPECT_TRUE(button);
[bar_ openBookmarkFolderFromButton:button];
BookmarkBarFolderController* bbfc = [bar_ folderController];
EXPECT_TRUE(bbfc);
// Make sure a 2nd click on the same button closes things.
[bar_ openBookmarkFolderFromButton:button];
EXPECT_FALSE([bar_ folderController]);
// Next open is a different button.
button = [bar_ buttonWithTitleEqualTo:@"2f"];
EXPECT_TRUE(button);
[bar_ openBookmarkFolderFromButton:button];
EXPECT_TRUE([bar_ folderController]);
// Mouse over a non-folder button and confirm controller has gone away.
button = [bar_ buttonWithTitleEqualTo:@"1b"];
EXPECT_TRUE(button);
NSEvent* event = cocoa_test_event_utils::MouseEventAtPoint([button center],
NSMouseMoved, 0);
[bar_ mouseEnteredButton:button event:event];
EXPECT_FALSE([bar_ folderController]);
// Mouse over the original folder and confirm a new controller.
button = [bar_ buttonWithTitleEqualTo:@"2f"];
EXPECT_TRUE(button);
[bar_ mouseEnteredButton:button event:event];
BookmarkBarFolderController* oldBBFC = [bar_ folderController];
EXPECT_TRUE(oldBBFC);
// 'Jump' over to a different folder and confirm a new controller.
button = [bar_ buttonWithTitleEqualTo:@"4f"];
EXPECT_TRUE(button);
[bar_ mouseEnteredButton:button event:event];
BookmarkBarFolderController* newBBFC = [bar_ folderController];
EXPECT_TRUE(newBBFC);
EXPECT_NE(oldBBFC, newBBFC);
}
// Make sure the "off the side" folder looks like a bookmark folder
// but only contains "off the side" items.
TEST_F(BookmarkBarControllerTest, OffTheSideFolder) {
// It starts hidden.
EXPECT_TRUE([bar_ offTheSideButtonIsHidden]);
// Create some buttons.
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* parent = model->bookmark_bar_node();
for (int x = 0; x < 30; x++) {
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("medium-size-title"),
GURL("http://framma-lamma.com"));
}
// Add a couple more so we can delete one and make sure its button goes away.
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("DELETE_ME"), GURL("http://ashton-tate.com"));
model->AddURL(parent, parent->child_count(),
ASCIIToUTF16("medium-size-title"),
GURL("http://framma-lamma.com"));
// Should no longer be hidden.
EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
// Open it; make sure we have a folder controller.
EXPECT_FALSE([bar_ folderController]);
[bar_ openOffTheSideFolderFromButton:[bar_ offTheSideButton]];
BookmarkBarFolderController* bbfc = [bar_ folderController];
EXPECT_TRUE(bbfc);
// Confirm the contents are only buttons which fell off the side by
// making sure that none of the nodes in the off-the-side folder are
// found in bar buttons. Be careful since not all the bar buttons
// may be currently displayed.
NSArray* folderButtons = [bbfc buttons];
NSArray* barButtons = [bar_ buttons];
for (BookmarkButton* folderButton in folderButtons) {
for (BookmarkButton* barButton in barButtons) {
if ([barButton superview]) {
EXPECT_NE([folderButton bookmarkNode], [barButton bookmarkNode]);
}
}
}
// Delete a bookmark in the off-the-side and verify it's gone.
BookmarkButton* button = [bbfc buttonWithTitleEqualTo:@"DELETE_ME"];
EXPECT_TRUE(button);
model->Remove(parent, parent->child_count() - 2);
button = [bbfc buttonWithTitleEqualTo:@"DELETE_ME"];
EXPECT_FALSE(button);
}
TEST_F(BookmarkBarControllerTest, EventToExitCheck) {
NSEvent* event = cocoa_test_event_utils::MouseEventWithType(NSMouseMoved, 0);
EXPECT_FALSE([bar_ isEventAnExitEvent:event]);
BookmarkBarFolderWindow* folderWindow = [[[BookmarkBarFolderWindow alloc]
init] autorelease];
[[[bar_ view] window] addChildWindow:folderWindow
ordered:NSWindowAbove];
event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(NSMakePoint(1,1),
folderWindow);
EXPECT_FALSE([bar_ isEventAnExitEvent:event]);
event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
NSMakePoint(100,100), test_window());
EXPECT_TRUE([bar_ isEventAnExitEvent:event]);
// Many components are arbitrary (e.g. location, keycode).
event = [NSEvent keyEventWithType:NSKeyDown
location:NSMakePoint(1,1)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
characters:@"x"
charactersIgnoringModifiers:@"x"
isARepeat:NO
keyCode:87];
EXPECT_FALSE([bar_ isEventAnExitEvent:event]);
[[[bar_ view] window] removeChildWindow:folderWindow];
}
TEST_F(BookmarkBarControllerTest, DropDestination) {
// Make some buttons.
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* parent = model->bookmark_bar_node();
model->AddFolder(parent, parent->child_count(), ASCIIToUTF16("folder 1"));
model->AddFolder(parent, parent->child_count(), ASCIIToUTF16("folder 2"));
EXPECT_EQ([[bar_ buttons] count], 2U);
// Confirm "off to left" and "off to right" match nothing.
NSPoint p = NSMakePoint(-1, 2);
EXPECT_FALSE([bar_ buttonForDroppingOnAtPoint:p]);
EXPECT_TRUE([bar_ shouldShowIndicatorShownForPoint:p]);
p = NSMakePoint(50000, 10);
EXPECT_FALSE([bar_ buttonForDroppingOnAtPoint:p]);
EXPECT_TRUE([bar_ shouldShowIndicatorShownForPoint:p]);
// Confirm "right in the center" (give or take a pixel) is a match,
// and confirm "just barely in the button" is not. Anything more
// specific seems likely to be tweaked.
CGFloat viewFrameXOffset = [[bar_ view] frame].origin.x;
for (BookmarkButton* button in [bar_ buttons]) {
CGFloat x = NSMidX([button frame]) + viewFrameXOffset;
// Somewhere near the center: a match
EXPECT_EQ(button,
[bar_ buttonForDroppingOnAtPoint:NSMakePoint(x-1, 10)]);
EXPECT_EQ(button,
[bar_ buttonForDroppingOnAtPoint:NSMakePoint(x+1, 10)]);
EXPECT_FALSE([bar_ shouldShowIndicatorShownForPoint:NSMakePoint(x, 10)]);;
// On the very edges: NOT a match
x = NSMinX([button frame]) + viewFrameXOffset;
EXPECT_NE(button,
[bar_ buttonForDroppingOnAtPoint:NSMakePoint(x, 9)]);
x = NSMaxX([button frame]) + viewFrameXOffset;
EXPECT_NE(button,
[bar_ buttonForDroppingOnAtPoint:NSMakePoint(x, 11)]);
}
}
TEST_F(BookmarkBarControllerTest, CloseFolderOnAnimate) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
[bar_ setStateAnimationsEnabled:YES];
const BookmarkNode* parent = model->bookmark_bar_node();
const BookmarkNode* folder = model->AddFolder(parent,
parent->child_count(),
ASCIIToUTF16("folder"));
model->AddFolder(parent, parent->child_count(),
ASCIIToUTF16("sibbling folder"));
model->AddURL(folder, folder->child_count(), ASCIIToUTF16("title a"),
GURL("http://www.google.com/a"));
model->AddURL(folder, folder->child_count(),
ASCIIToUTF16("title super duper long long whoa momma title you betcha"),
GURL("http://www.google.com/b"));
BookmarkButton* button = [[bar_ buttons] objectAtIndex:0];
EXPECT_FALSE([bar_ folderController]);
[bar_ openBookmarkFolderFromButton:button];
BookmarkBarFolderController* bbfc = [bar_ folderController];
// The following tells us that the folder menu is showing. We want to make
// sure the folder menu goes away if the bookmark bar is hidden.
EXPECT_TRUE(bbfc);
EXPECT_TRUE([bar_ isVisible]);
// Hide the bookmark bar.
[bar_ updateState:BookmarkBar::DETACHED
changeType:BookmarkBar::ANIMATE_STATE_CHANGE];
EXPECT_TRUE([bar_ isAnimationRunning]);
// Now that we've closed the bookmark bar (with animation) the folder menu
// should have been closed thus releasing the folderController.
EXPECT_FALSE([bar_ folderController]);
// Stop the pending animation to tear down cleanly.
[bar_ updateState:BookmarkBar::DETACHED
changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
EXPECT_FALSE([bar_ isAnimationRunning]);
}
TEST_F(BookmarkBarControllerTest, MoveRemoveAddButtons) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
// Validate initial model.
std::string actualModelString = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actualModelString);
// Remember how many buttons are showing.
int oldDisplayedButtons = [bar_ displayedButtonCount];
NSArray* buttons = [bar_ buttons];
// Move a button around a bit.
[bar_ moveButtonFromIndex:0 toIndex:2];
EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:0] title]);
EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:1] title]);
EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:2] title]);
EXPECT_EQ(oldDisplayedButtons, [bar_ displayedButtonCount]);
[bar_ moveButtonFromIndex:2 toIndex:0];
EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:0] title]);
EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:1] title]);
EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:2] title]);
EXPECT_EQ(oldDisplayedButtons, [bar_ displayedButtonCount]);
// Add a couple of buttons.
const BookmarkNode* parent = root->GetChild(1); // Purloin an existing node.
const BookmarkNode* node = parent->GetChild(0);
[bar_ addButtonForNode:node atIndex:0];
EXPECT_NSEQ(@"2f1b", [[buttons objectAtIndex:0] title]);
EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:1] title]);
EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:2] title]);
EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:3] title]);
EXPECT_EQ(oldDisplayedButtons + 1, [bar_ displayedButtonCount]);
node = parent->GetChild(1);
[bar_ addButtonForNode:node atIndex:-1];
EXPECT_NSEQ(@"2f1b", [[buttons objectAtIndex:0] title]);
EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:1] title]);
EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:2] title]);
EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:3] title]);
EXPECT_NSEQ(@"2f2b", [[buttons objectAtIndex:4] title]);
EXPECT_EQ(oldDisplayedButtons + 2, [bar_ displayedButtonCount]);
// Remove a couple of buttons.
[bar_ removeButton:4 animate:NO];
[bar_ removeButton:1 animate:NO];
EXPECT_NSEQ(@"2f1b", [[buttons objectAtIndex:0] title]);
EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:1] title]);
EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:2] title]);
EXPECT_EQ(oldDisplayedButtons, [bar_ displayedButtonCount]);
}
TEST_F(BookmarkBarControllerTest, ShrinkOrHideView) {
NSRect viewFrame = NSMakeRect(0.0, 0.0, 500.0, 50.0);
NSView* view = [[[NSView alloc] initWithFrame:viewFrame] autorelease];
EXPECT_FALSE([view isHidden]);
[bar_ shrinkOrHideView:view forMaxX:500.0];
EXPECT_EQ(500.0, NSWidth([view frame]));
EXPECT_FALSE([view isHidden]);
[bar_ shrinkOrHideView:view forMaxX:450.0];
EXPECT_EQ(450.0, NSWidth([view frame]));
EXPECT_FALSE([view isHidden]);
[bar_ shrinkOrHideView:view forMaxX:40.0];
EXPECT_EQ(40.0, NSWidth([view frame]));
EXPECT_FALSE([view isHidden]);
[bar_ shrinkOrHideView:view forMaxX:31.0];
EXPECT_EQ(31.0, NSWidth([view frame]));
EXPECT_FALSE([view isHidden]);
[bar_ shrinkOrHideView:view forMaxX:29.0];
EXPECT_TRUE([view isHidden]);
}
TEST_F(BookmarkBarControllerTest, LastBookmarkResizeBehavior) {
// Hide the apps shortcut.
profile()->GetPrefs()->SetBoolean(
bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
[bar_ frameDidChange];
// The default font changed between OSX Mavericks and OSX Yosemite, so this
// test requires different widths to trigger the appropriate results.
CGFloat viewWidthsYosemite[] = { 121.0, 122.0, 148.0, 149.0, 150.0, 151.0,
152.0, 200.0, 152.0, 151.0, 150.0, 149.0,
148.0, 122.0, 121.0 };
CGFloat viewWidthsMavericks[] = { 123.0, 124.0, 151.0, 152.0, 153.0, 154.0,
155.0, 200.0, 155.0, 154.0, 153.0, 152.0,
151.0, 124.0, 123.0 };
BOOL offTheSideButtonIsHiddenResults[] = { NO, NO, NO, NO, YES, YES, YES, YES,
YES, YES, YES, NO, NO, NO, NO};
int displayedButtonCountResults[] = { 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 2,
2, 1 };
CGFloat* viewWidths = base::mac::IsOSYosemiteOrLater() ? viewWidthsYosemite
: viewWidthsMavericks;
for (unsigned int i = 0; i < arraysize(viewWidthsYosemite); ++i) {
NSRect frame = [[bar_ view] frame];
frame.size.width = viewWidths[i] + bookmarks::kBookmarkRightMargin;
[[bar_ view] setFrame:frame];
EXPECT_EQ(offTheSideButtonIsHiddenResults[i],
[bar_ offTheSideButtonIsHidden]);
EXPECT_EQ(displayedButtonCountResults[i], [bar_ displayedButtonCount]);
}
}
TEST_F(BookmarkBarControllerTest, BookmarksWithAppsPageShortcut) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
[bar_ frameDidChange];
// Apps page shortcut button should be visible.
ASSERT_FALSE([bar_ appsPageShortcutButtonIsHidden]);
// Bookmarks should be to the right of the Apps page shortcut button.
CGFloat apps_button_right = NSMaxX([[bar_ appsPageShortcutButton] frame]);
CGFloat right = apps_button_right;
NSArray* buttons = [bar_ buttons];
for (size_t i = 0; i < [buttons count]; ++i) {
EXPECT_LE(right, NSMinX([[buttons objectAtIndex:i] frame]));
right = NSMaxX([[buttons objectAtIndex:i] frame]);
}
// Removing the Apps button should move every bookmark to the left.
profile()->GetPrefs()->SetBoolean(
bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
EXPECT_GT(apps_button_right, NSMinX([[buttons objectAtIndex:0] frame]));
for (size_t i = 1; i < [buttons count]; ++i) {
EXPECT_LE(NSMaxX([[buttons objectAtIndex:i - 1] frame]),
NSMinX([[buttons objectAtIndex:i] frame]));
}
}
TEST_F(BookmarkBarControllerTest, BookmarksWithoutAppsPageShortcut) {
// The no item containers should be to the right of the Apps button.
ASSERT_FALSE([bar_ appsPageShortcutButtonIsHidden]);
CGFloat apps_button_right = NSMaxX([[bar_ appsPageShortcutButton] frame]);
EXPECT_LE(apps_button_right,
NSMinX([[[bar_ buttonView] noItemTextfield] frame]));
EXPECT_LE(NSMaxX([[[bar_ buttonView] noItemTextfield] frame]),
NSMinX([[[bar_ buttonView] importBookmarksButton] frame]));
// Removing the Apps button should move the no item containers to the left.
profile()->GetPrefs()->SetBoolean(
bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
EXPECT_GT(apps_button_right,
NSMinX([[[bar_ buttonView] noItemTextfield] frame]));
EXPECT_LE(NSMaxX([[[bar_ buttonView] noItemTextfield] frame]),
NSMinX([[[bar_ buttonView] importBookmarksButton] frame]));
}
TEST_F(BookmarkBarControllerTest, ManagedShowAppsShortcutInBookmarksBar) {
// By default the pref is not managed and the apps shortcut is shown.
TestingPrefServiceSyncable* prefs = profile()->GetTestingPrefService();
EXPECT_FALSE(prefs->IsManagedPreference(
bookmarks::prefs::kShowAppsShortcutInBookmarkBar));
EXPECT_FALSE([bar_ appsPageShortcutButtonIsHidden]);
// Hide the apps shortcut by policy, via the managed pref.
prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
new base::FundamentalValue(false));
EXPECT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
// And try showing it via policy too.
prefs->SetManagedPref(bookmarks::prefs::kShowAppsShortcutInBookmarkBar,
new base::FundamentalValue(true));
EXPECT_FALSE([bar_ appsPageShortcutButtonIsHidden]);
}
class BookmarkBarControllerOpenAllTest : public BookmarkBarControllerTest {
public:
void SetUp() override {
BookmarkBarControllerTest::SetUp();
ASSERT_TRUE(profile());
resizeDelegate_.reset([[ViewResizerPong alloc] init]);
NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
bar_.reset(
[[BookmarkBarControllerOpenAllPong alloc]
initWithBrowser:browser()
initialWidth:NSWidth(parent_frame)
delegate:nil
resizeDelegate:resizeDelegate_.get()]);
[bar_ view];
// Awkwardness to look like we've been installed.
[parent_view_ addSubview:[bar_ view]];
NSRect frame = [[[bar_ view] superview] frame];
frame.origin.y = 100;
[[[bar_ view] superview] setFrame:frame];
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
parent_ = model->bookmark_bar_node();
// { one, { two-one, two-two }, three }
model->AddURL(parent_, parent_->child_count(), ASCIIToUTF16("title"),
GURL("http://one.com"));
folder_ = model->AddFolder(parent_, parent_->child_count(),
ASCIIToUTF16("folder"));
model->AddURL(folder_, folder_->child_count(),
ASCIIToUTF16("title"), GURL("http://two-one.com"));
model->AddURL(folder_, folder_->child_count(),
ASCIIToUTF16("title"), GURL("http://two-two.com"));
model->AddURL(parent_, parent_->child_count(),
ASCIIToUTF16("title"), GURL("https://three.com"));
}
const BookmarkNode* parent_; // Weak
const BookmarkNode* folder_; // Weak
};
// Command-click on a folder should open all the bookmarks in it.
TEST_F(BookmarkBarControllerOpenAllTest, CommandClickOnFolder) {
NSButton* first = [[bar_ buttons] objectAtIndex:0];
EXPECT_TRUE(first);
// Create the right kind of event; mock NSApp so [NSApp
// currentEvent] finds it.
NSEvent* commandClick =
cocoa_test_event_utils::MouseEventAtPoint(NSZeroPoint,
NSLeftMouseDown,
NSCommandKeyMask);
id fakeApp = [OCMockObject partialMockForObject:NSApp];
[[[fakeApp stub] andReturn:commandClick] currentEvent];
id oldApp = NSApp;
NSApp = fakeApp;
size_t originalDispositionCount = noOpenBar()->dispositions_.size();
// Click!
[first performClick:first];
size_t dispositionCount = noOpenBar()->dispositions_.size();
EXPECT_EQ(originalDispositionCount+1, dispositionCount);
EXPECT_EQ(noOpenBar()->dispositions_[dispositionCount-1], NEW_BACKGROUND_TAB);
// Replace NSApp
NSApp = oldApp;
}
class BookmarkBarControllerNotificationTest : public CocoaProfileTest {
public:
void SetUp() override {
CocoaProfileTest::SetUp();
ASSERT_TRUE(browser());
resizeDelegate_.reset([[ViewResizerPong alloc] init]);
NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]);
[parent_view_ setHidden:YES];
bar_.reset(
[[BookmarkBarControllerNotificationPong alloc]
initWithBrowser:browser()
initialWidth:NSWidth(parent_frame)
delegate:nil
resizeDelegate:resizeDelegate_.get()]);
// Force loading of the nib.
[bar_ view];
// Awkwardness to look like we've been installed.
[parent_view_ addSubview:[bar_ view]];
NSRect frame = [[[bar_ view] superview] frame];
frame.origin.y = 100;
[[[bar_ view] superview] setFrame:frame];
// Do not add the bar to a window, yet.
}
base::scoped_nsobject<NSView> parent_view_;
base::scoped_nsobject<ViewResizerPong> resizeDelegate_;
base::scoped_nsobject<BookmarkBarControllerNotificationPong> bar_;
};
TEST_F(BookmarkBarControllerNotificationTest, DeregistersForNotifications) {
NSWindow* window = [[CocoaTestHelperWindow alloc] init];
[window setReleasedWhenClosed:YES];
// First add the bookmark bar to the temp window, then to another window.
[[window contentView] addSubview:parent_view_];
[[test_window() contentView] addSubview:parent_view_];
// Post a fake windowDidResignKey notification for the temp window and make
// sure the bookmark bar controller wasn't listening.
[[NSNotificationCenter defaultCenter]
postNotificationName:NSWindowDidResignKeyNotification
object:window];
EXPECT_FALSE([bar_ windowDidResignKeyReceived]);
// Close the temp window and make sure no notification was received.
[window close];
EXPECT_FALSE([bar_ windowWillCloseReceived]);
}
// TODO(jrg): draggingEntered: and draggingExited: trigger timers so
// they are hard to test. Factor out "fire timers" into routines
// which can be overridden to fire immediately to make behavior
// confirmable.
// TODO(jrg): add unit test to make sure "Other Bookmarks" responds
// properly to a hover open.
// TODO(viettrungluu): figure out how to test animations.
class BookmarkBarControllerDragDropTest : public BookmarkBarControllerTestBase {
public:
base::scoped_nsobject<BookmarkBarControllerDragData> bar_;
void SetUp() override {
BookmarkBarControllerTestBase::SetUp();
ASSERT_TRUE(browser());
bar_.reset(
[[BookmarkBarControllerDragData alloc]
initWithBrowser:browser()
initialWidth:NSWidth([parent_view_ frame])
delegate:nil
resizeDelegate:resizeDelegate_.get()]);
InstallAndToggleBar(bar_.get());
}
};
TEST_F(BookmarkBarControllerDragDropTest, DragMoveBarBookmarkToOffTheSide) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1bWithLongName 2fWithLongName:[ "
"2f1bWithLongName 2f2fWithLongName:[ 2f2f1bWithLongName "
"2f2f2bWithLongName 2f2f3bWithLongName 2f4b ] 2f3bWithLongName ] "
"3bWithLongName 4bWithLongName 5bWithLongName 6bWithLongName "
"7bWithLongName 8bWithLongName 9bWithLongName 10bWithLongName "
"11bWithLongName 12bWithLongName 13b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
// Validate initial model.
std::string actualModelString = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actualModelString);
// Insure that the off-the-side is not showing.
ASSERT_FALSE([bar_ offTheSideButtonIsHidden]);
// Remember how many buttons are showing and are available.
int oldDisplayedButtons = [bar_ displayedButtonCount];
int oldChildCount = root->child_count();
// Pop up the off-the-side menu.
BookmarkButton* otsButton = (BookmarkButton*)[bar_ offTheSideButton];
ASSERT_TRUE(otsButton);
[[otsButton target] performSelector:@selector(openOffTheSideFolderFromButton:)
withObject:otsButton];
BookmarkBarFolderController* otsController = [bar_ folderController];
EXPECT_TRUE(otsController);
NSWindow* toWindow = [otsController window];
EXPECT_TRUE(toWindow);
BookmarkButton* draggedButton =
[bar_ buttonWithTitleEqualTo:@"3bWithLongName"];
ASSERT_TRUE(draggedButton);
int oldOTSCount = (int)[[otsController buttons] count];
EXPECT_EQ(oldOTSCount, oldChildCount - oldDisplayedButtons);
BookmarkButton* targetButton = [[otsController buttons] objectAtIndex:0];
ASSERT_TRUE(targetButton);
[otsController dragButton:draggedButton
to:[targetButton center]
copy:YES];
// There should still be the same number of buttons in the bar
// and off-the-side should have one more.
int newDisplayedButtons = [bar_ displayedButtonCount];
int newChildCount = root->child_count();
int newOTSCount = (int)[[otsController buttons] count];
EXPECT_EQ(oldDisplayedButtons, newDisplayedButtons);
EXPECT_EQ(oldChildCount + 1, newChildCount);
EXPECT_EQ(oldOTSCount + 1, newOTSCount);
EXPECT_EQ(newOTSCount, newChildCount - newDisplayedButtons);
}
TEST_F(BookmarkBarControllerDragDropTest, DragOffTheSideToOther) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1bWithLongName 2bWithLongName "
"3bWithLongName 4bWithLongName 5bWithLongName 6bWithLongName "
"7bWithLongName 8bWithLongName 9bWithLongName 10bWithLongName "
"11bWithLongName 12bWithLongName 13bWithLongName 14bWithLongName "
"15bWithLongName 16bWithLongName 17bWithLongName 18bWithLongName "
"19bWithLongName 20bWithLongName ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
const BookmarkNode* other = model->other_node();
const std::string other_string("1other 2other 3other ");
bookmarks::test::AddNodesFromModelString(model, other, other_string);
// Validate initial model.
std::string actualModelString = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actualModelString);
std::string actualOtherString = bookmarks::test::ModelStringFromNode(other);
EXPECT_EQ(other_string, actualOtherString);
// Insure that the off-the-side is showing.
ASSERT_FALSE([bar_ offTheSideButtonIsHidden]);
// Remember how many buttons are showing and are available.
int oldDisplayedButtons = [bar_ displayedButtonCount];
int oldRootCount = root->child_count();
int oldOtherCount = other->child_count();
// Pop up the off-the-side menu.
BookmarkButton* otsButton = (BookmarkButton*)[bar_ offTheSideButton];
ASSERT_TRUE(otsButton);
[[otsButton target] performSelector:@selector(openOffTheSideFolderFromButton:)
withObject:otsButton];
BookmarkBarFolderController* otsController = [bar_ folderController];
EXPECT_TRUE(otsController);
int oldOTSCount = (int)[[otsController buttons] count];
EXPECT_EQ(oldOTSCount, oldRootCount - oldDisplayedButtons);
// Pick an off-the-side button and drag it to the other bookmarks.
BookmarkButton* draggedButton =
[otsController buttonWithTitleEqualTo:@"20bWithLongName"];
ASSERT_TRUE(draggedButton);
BookmarkButton* targetButton = [bar_ otherBookmarksButton];
ASSERT_TRUE(targetButton);
[bar_ dragButton:draggedButton to:[targetButton center] copy:NO];
// There should one less button in the bar, one less in off-the-side,
// and one more in other bookmarks.
int newRootCount = root->child_count();
int newOTSCount = (int)[[otsController buttons] count];
int newOtherCount = other->child_count();
EXPECT_EQ(oldRootCount - 1, newRootCount);
EXPECT_EQ(oldOTSCount - 1, newOTSCount);
EXPECT_EQ(oldOtherCount + 1, newOtherCount);
}
TEST_F(BookmarkBarControllerDragDropTest, DragBookmarkData) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
"2f3b ] 3b 4b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
const BookmarkNode* other = model->other_node();
const std::string other_string("O1b O2b O3f:[ O3f1b O3f2f ] "
"O4f:[ O4f1b O4f2f ] 05b ");
bookmarks::test::AddNodesFromModelString(model, other, other_string);
// Validate initial model.
std::string actual = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actual);
actual = bookmarks::test::ModelStringFromNode(other);
EXPECT_EQ(other_string, actual);
// Remember the little ones.
int oldChildCount = root->child_count();
BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"3b"];
ASSERT_TRUE(targetButton);
// Gen up some dragging data.
const BookmarkNode* newNode = other->GetChild(2);
[bar_ setDragDataNode:newNode];
base::scoped_nsobject<FakeDragInfo> dragInfo([[FakeDragInfo alloc] init]);
[dragInfo setDropLocation:[targetButton center]];
[bar_ dragBookmarkData:(id<NSDraggingInfo>)dragInfo.get()];
// There should one more button in the bar.
int newChildCount = root->child_count();
EXPECT_EQ(oldChildCount + 1, newChildCount);
// Verify the model.
const std::string expected("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
"2f3b ] O3f:[ O3f1b O3f2f ] 3b 4b ");
actual = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(expected, actual);
oldChildCount = newChildCount;
// Now do it over a folder button.
targetButton = [bar_ buttonWithTitleEqualTo:@"2f"];
ASSERT_TRUE(targetButton);
NSPoint targetPoint = [targetButton center];
newNode = other->GetChild(2); // Should be O4f.
EXPECT_EQ(newNode->GetTitle(), ASCIIToUTF16("O4f"));
[bar_ setDragDataNode:newNode];
[dragInfo setDropLocation:targetPoint];
[bar_ dragBookmarkData:(id<NSDraggingInfo>)dragInfo.get()];
newChildCount = root->child_count();
EXPECT_EQ(oldChildCount, newChildCount);
// Verify the model.
const std::string expected1("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
"2f3b O4f:[ O4f1b O4f2f ] ] O3f:[ O3f1b O3f2f ] "
"3b 4b ");
actual = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(expected1, actual);
}
TEST_F(BookmarkBarControllerDragDropTest, AddURLs) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
"2f3b ] 3b 4b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
// Validate initial model.
std::string actual = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actual);
// Remember the children.
int oldChildCount = root->child_count();
BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"3b"];
ASSERT_TRUE(targetButton);
NSArray* urls = [NSArray arrayWithObjects: @"http://www.a.com/",
@"http://www.b.com/", nil];
NSArray* titles = [NSArray arrayWithObjects: @"SiteA", @"SiteB", nil];
[bar_ addURLs:urls withTitles:titles at:[targetButton center]];
// There should two more nodes in the bar.
int newChildCount = root->child_count();
EXPECT_EQ(oldChildCount + 2, newChildCount);
// Verify the model.
const std::string expected("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
"2f3b ] SiteA SiteB 3b 4b ");
actual = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(expected, actual);
}
TEST_F(BookmarkBarControllerDragDropTest, ControllerForNode) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
// Validate initial model.
std::string actualModelString = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actualModelString);
// Find the main bar controller.
const void* expectedController = bar_;
const void* actualController = [bar_ controllerForNode:root];
EXPECT_EQ(expectedController, actualController);
}
TEST_F(BookmarkBarControllerDragDropTest, DropPositionIndicator) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2b 2f3b ] 3b 4b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
// Hide the apps shortcut.
profile()->GetPrefs()->SetBoolean(
bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
// Validate initial model.
std::string actualModel = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actualModel);
// Test a series of points starting at the right edge of the bar.
BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"1b"];
ASSERT_TRUE(targetButton);
NSPoint targetPoint = [targetButton left];
CGFloat leftMarginIndicatorPosition = bookmarks::kBookmarkLeftMargin - 0.5 *
bookmarks::kBookmarkHorizontalPadding;
const CGFloat baseOffset = targetPoint.x;
CGFloat expected = leftMarginIndicatorPosition;
CGFloat actual = [bar_ indicatorPosForDragToPoint:targetPoint];
EXPECT_CGFLOAT_EQ(expected, actual);
targetButton = [bar_ buttonWithTitleEqualTo:@"2f"];
actual = [bar_ indicatorPosForDragToPoint:[targetButton right]];
targetButton = [bar_ buttonWithTitleEqualTo:@"3b"];
expected = [targetButton left].x - baseOffset + leftMarginIndicatorPosition;
EXPECT_CGFLOAT_EQ(expected, actual);
targetButton = [bar_ buttonWithTitleEqualTo:@"4b"];
targetPoint = [targetButton right];
targetPoint.x += 100; // Somewhere off to the right.
CGFloat xDelta = 0.5 * bookmarks::kBookmarkHorizontalPadding;
expected = NSMaxX([targetButton frame]) + xDelta;
actual = [bar_ indicatorPosForDragToPoint:targetPoint];
EXPECT_CGFLOAT_EQ(expected, actual);
}
TEST_F(BookmarkBarControllerDragDropTest, PulseButton) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
GURL gurl("http://www.google.com");
const BookmarkNode* node = model->AddURL(root, root->child_count(),
ASCIIToUTF16("title"), gurl);
BookmarkButton* button = [[bar_ buttons] objectAtIndex:0];
EXPECT_FALSE([button isContinuousPulsing]);
NSValue *value = [NSValue valueWithPointer:node];
NSDictionary *dict = [NSDictionary
dictionaryWithObjectsAndKeys:value,
bookmark_button::kBookmarkKey,
[NSNumber numberWithBool:YES],
bookmark_button::kBookmarkPulseFlagKey,
nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:bookmark_button::kPulseBookmarkButtonNotification
object:nil
userInfo:dict];
EXPECT_TRUE([button isContinuousPulsing]);
dict = [NSDictionary dictionaryWithObjectsAndKeys:value,
bookmark_button::kBookmarkKey,
[NSNumber numberWithBool:NO],
bookmark_button::kBookmarkPulseFlagKey,
nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:bookmark_button::kPulseBookmarkButtonNotification
object:nil
userInfo:dict];
EXPECT_FALSE([button isContinuousPulsing]);
}
TEST_F(BookmarkBarControllerDragDropTest, DragBookmarkDataToTrash) {
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
const BookmarkNode* root = model->bookmark_bar_node();
const std::string model_string("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
"2f3b ] 3b 4b ");
bookmarks::test::AddNodesFromModelString(model, root, model_string);
// Validate initial model.
std::string actual = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(model_string, actual);
int oldChildCount = root->child_count();
// Drag a button to the trash.
BookmarkButton* buttonToDelete = [bar_ buttonWithTitleEqualTo:@"3b"];
ASSERT_TRUE(buttonToDelete);
EXPECT_TRUE([bar_ canDragBookmarkButtonToTrash:buttonToDelete]);
[bar_ didDragBookmarkToTrash:buttonToDelete];
// There should be one less button in the bar.
int newChildCount = root->child_count();
EXPECT_EQ(oldChildCount - 1, newChildCount);
// Verify the model.
const std::string expected("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
"2f3b ] 4b ");
actual = bookmarks::test::ModelStringFromNode(root);
EXPECT_EQ(expected, actual);
// Verify that the other bookmark folder can't be deleted.
BookmarkButton *otherButton = [bar_ otherBookmarksButton];
EXPECT_FALSE([bar_ canDragBookmarkButtonToTrash:otherButton]);
}
} // namespace
|