Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(656)

Side by Side Diff: chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller_unittest.mm

Issue 1987893002: Reland of [Mac][Material Design] Update bookmarks bar to Material Design. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 #include <stddef.h> 6 #include <stddef.h>
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/mac/mac_util.h" 10 #include "base/mac/mac_util.h"
(...skipping 21 matching lines...) Expand all
32 #include "components/bookmarks/browser/bookmark_utils.h" 32 #include "components/bookmarks/browser/bookmark_utils.h"
33 #include "components/bookmarks/common/bookmark_pref_names.h" 33 #include "components/bookmarks/common/bookmark_pref_names.h"
34 #include "components/bookmarks/test/bookmark_test_helpers.h" 34 #include "components/bookmarks/test/bookmark_test_helpers.h"
35 #include "components/syncable_prefs/testing_pref_service_syncable.h" 35 #include "components/syncable_prefs/testing_pref_service_syncable.h"
36 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
37 #import "testing/gtest_mac.h" 37 #import "testing/gtest_mac.h"
38 #include "testing/platform_test.h" 38 #include "testing/platform_test.h"
39 #import "third_party/ocmock/OCMock/OCMock.h" 39 #import "third_party/ocmock/OCMock/OCMock.h"
40 #include "third_party/ocmock/gtest_support.h" 40 #include "third_party/ocmock/gtest_support.h"
41 #include "ui/base/cocoa/animation_utils.h" 41 #include "ui/base/cocoa/animation_utils.h"
42 #include "ui/base/material_design/material_design_controller.h"
42 #include "ui/base/theme_provider.h" 43 #include "ui/base/theme_provider.h"
43 #include "ui/events/test/cocoa_test_event_utils.h" 44 #include "ui/events/test/cocoa_test_event_utils.h"
44 #include "ui/gfx/image/image_skia.h" 45 #include "ui/gfx/image/image_skia.h"
45 46
46 using base::ASCIIToUTF16; 47 using base::ASCIIToUTF16;
47 using bookmarks::BookmarkModel; 48 using bookmarks::BookmarkModel;
48 using bookmarks::BookmarkNode; 49 using bookmarks::BookmarkNode;
49 50
50 // Unit tests don't need time-consuming asynchronous animations. 51 // Unit tests don't need time-consuming asynchronous animations.
51 @interface BookmarkBarControllerTestable : BookmarkBarController { 52 @interface BookmarkBarControllerTestable : BookmarkBarController {
(...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after
1007 1008
1008 const BookmarkNode* parent = model->bookmark_bar_node(); 1009 const BookmarkNode* parent = model->bookmark_bar_node();
1009 model->AddURL(parent, parent->child_count(), 1010 model->AddURL(parent, parent->child_count(),
1010 ASCIIToUTF16("title"), GURL("http://one.com")); 1011 ASCIIToUTF16("title"), GURL("http://one.com"));
1011 1012
1012 [bar_ loaded:model]; 1013 [bar_ loaded:model];
1013 1014
1014 // Make sure the internal bookmark button also is the correct height. 1015 // Make sure the internal bookmark button also is the correct height.
1015 NSArray* buttons = [bar_ buttons]; 1016 NSArray* buttons = [bar_ buttons];
1016 EXPECT_GT([buttons count], 0u); 1017 EXPECT_GT([buttons count], 0u);
1018 const bool kIsModeMaterial = ui::MaterialDesignController::IsModeMaterial();
1019
1017 for (NSButton* button in buttons) { 1020 for (NSButton* button in buttons) {
1018 EXPECT_FLOAT_EQ( 1021 if (kIsModeMaterial) {
1019 (chrome::kBookmarkBarHeight + bookmarks::kVisualHeightOffset) - 1022 EXPECT_FLOAT_EQ(
1020 2 * bookmarks::kBookmarkVerticalPadding, 1023 (chrome::kBookmarkBarHeight +
1021 [button frame].size.height); 1024 bookmarks::kMaterialVisualHeightOffset) -
1025 2 * bookmarks::BookmarkVerticalPadding(),
1026 [button frame].size.height);
1027 } else {
1028 EXPECT_FLOAT_EQ(
1029 (chrome::kBookmarkBarHeight + bookmarks::kVisualHeightOffset) -
1030 2 * bookmarks::BookmarkVerticalPadding(),
1031 [button frame].size.height);
1032 }
1022 } 1033 }
1023 } 1034 }
1024 1035
1025 TEST_F(BookmarkBarControllerTest, DropBookmarks) { 1036 TEST_F(BookmarkBarControllerTest, DropBookmarks) {
1026 const char* urls[] = { 1037 const char* urls[] = {
1027 "http://qwantz.com", 1038 "http://qwantz.com",
1028 "http://xkcd.com", 1039 "http://xkcd.com",
1029 "javascript:alert('lolwut')", 1040 "javascript:alert('lolwut')",
1030 "file://localhost/tmp/local-file.txt" // As if dragged from the desktop. 1041 "file://localhost/tmp/local-file.txt" // As if dragged from the desktop.
1031 }; 1042 };
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after
1556 [bar_ shrinkOrHideView:view forMaxX:40.0]; 1567 [bar_ shrinkOrHideView:view forMaxX:40.0];
1557 EXPECT_EQ(40.0, NSWidth([view frame])); 1568 EXPECT_EQ(40.0, NSWidth([view frame]));
1558 EXPECT_FALSE([view isHidden]); 1569 EXPECT_FALSE([view isHidden]);
1559 [bar_ shrinkOrHideView:view forMaxX:31.0]; 1570 [bar_ shrinkOrHideView:view forMaxX:31.0];
1560 EXPECT_EQ(31.0, NSWidth([view frame])); 1571 EXPECT_EQ(31.0, NSWidth([view frame]));
1561 EXPECT_FALSE([view isHidden]); 1572 EXPECT_FALSE([view isHidden]);
1562 [bar_ shrinkOrHideView:view forMaxX:29.0]; 1573 [bar_ shrinkOrHideView:view forMaxX:29.0];
1563 EXPECT_TRUE([view isHidden]); 1574 EXPECT_TRUE([view isHidden]);
1564 } 1575 }
1565 1576
1577 // Simiulate browser window width change and ensure that the bookmark buttons
1578 // that should be visible are visible.
1566 TEST_F(BookmarkBarControllerTest, LastBookmarkResizeBehavior) { 1579 TEST_F(BookmarkBarControllerTest, LastBookmarkResizeBehavior) {
1567 // Hide the apps shortcut. 1580 // Hide the apps shortcut.
1568 profile()->GetPrefs()->SetBoolean( 1581 profile()->GetPrefs()->SetBoolean(
1569 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false); 1582 bookmarks::prefs::kShowAppsShortcutInBookmarkBar, false);
1570 ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]); 1583 ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
1571 1584
1585 // Add three buttons to the bookmark bar.
1572 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); 1586 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1573 const BookmarkNode* root = model->bookmark_bar_node(); 1587 const BookmarkNode* root = model->bookmark_bar_node();
1574 const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b "); 1588 const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
1575 bookmarks::test::AddNodesFromModelString(model, root, model_string); 1589 bookmarks::test::AddNodesFromModelString(model, root, model_string);
1576 [bar_ frameDidChange]; 1590 [bar_ frameDidChange];
1577 1591
1592 // Step through simulated window resizings. In resizing from the first width
1593 // to the second, the bookmark bar should transition from displaying one
1594 // button to two. Of the next 5 widths, the third transitions the bar from
1595 // displaying two buttons to three. The next width (200.0) resizes the bar to
1596 // a large width that does not change the number of visible buttons, and the
1597 // remaining widths step through all the previous resizings in reverse, which
1598 // should correspond to the previous number of visible buttons.
1599 //
1600 // To recalibrate this test for new OS releases, etc., determine values for
1601 // |view_widths[1]| and |view_widths[4]| which will cause a visible button
1602 // transition from 1 -> 2 and 2 -> 3, respectively. With those two widths you
1603 // can easily compute all widths from 0 through 6. |view_widths[7]| is always
1604 // 200.0, and the remainder are the reverse of widths 0 through 6. When all
1605 // three buttons are visible, be sure to sanity check with frames (i.e. under
1606 // MD the first button should start at x=10, and there should be 16pt of space
1607 // between the buttons).
1608 //
1578 // The default font changed between OSX Mavericks, OSX Yosemite, and 1609 // The default font changed between OSX Mavericks, OSX Yosemite, and
1579 // OSX El Capitan, so this test requires different widths to trigger the 1610 // OSX El Capitan, so this test requires different widths to trigger the
1580 // appropriate results. The Mavericks and El Capitan font widths are close 1611 // appropriate results. Button widths and locations also changed with
1581 // enough to use the same sizes. 1612 // Material Design.
1582 CGFloat viewWidthsYosemite[] = { 121.0, 122.0, 148.0, 149.0, 150.0, 151.0, 1613 CGFloat view_widths_el_capitan[] =
1583 152.0, 200.0, 152.0, 151.0, 150.0, 149.0, 1614 { 121.0, 122.0, 149.0, 150.0, 151.0, 152.0,
1584 148.0, 122.0, 121.0 }; 1615 153.0, 200.0, 153.0, 152.0, 151.0, 150.0,
1585 CGFloat viewWidthsRest[] = { 123.0, 124.0, 151.0, 152.0, 153.0, 154.0, 1616 149.0, 122.0, 121.0 };
1586 155.0, 200.0, 155.0, 154.0, 153.0, 152.0, 1617 CGFloat view_widths_yosemite[] =
1587 151.0, 124.0, 123.0 }; 1618 { 121.0, 122.0, 148.0, 149.0, 150.0, 151.0,
1588 CGFloat* viewWidths = base::mac::IsOSYosemite() ? viewWidthsYosemite : 1619 152.0, 200.0, 152.0, 151.0, 150.0, 149.0,
1589 viewWidthsRest; 1620 148.0, 122.0, 121.0 };
1621 CGFloat view_widths_rest[] =
1622 { 123.0, 124.0, 151.0, 152.0, 153.0, 154.0,
1623 155.0, 200.0, 155.0, 154.0, 153.0, 152.0,
1624 151.0, 124.0, 123.0 };
1625 CGFloat material_view_widths_el_capitan[] =
1626 { 139.0, 140.0, 150.0, 151.0, 152.0, 153.0,
1627 154.0, 200.0, 154.0, 153.0, 152.0, 151.0,
1628 150.0, 140.0, 139.0 };
1629 CGFloat material_view_widths_yosemite[] =
1630 { 140.0, 141.0, 150.0, 151.0, 152.0, 153.0,
1631 154.0, 200.0, 154.0, 153.0, 152.0, 151.0,
1632 150.0, 141.0, 140.0 };
1633 CGFloat material_view_widths_rest[] =
1634 { 142.0, 143.0, 153.0, 154.0, 155.0, 156.0,
1635 157.0, 200.0, 157.0, 156.0, 155.0, 154.0,
1636 153.0, 143.0, 142.0 };
1637 CGFloat* view_widths = NULL;
1638 bool is_mode_material = ui::MaterialDesignController::IsModeMaterial();
1639 if (base::mac::IsOSElCapitan()) {
1640 view_widths = is_mode_material ? material_view_widths_el_capitan
1641 : view_widths_el_capitan;
1642 } else if (base::mac::IsOSYosemite()) {
1643 view_widths = is_mode_material ? material_view_widths_yosemite
1644 : view_widths_yosemite;
1645 } else {
1646 view_widths = is_mode_material ? material_view_widths_rest
1647 : view_widths_rest;
1648 }
1590 1649
1591 BOOL offTheSideButtonIsHiddenResults[] = { NO, NO, NO, NO, YES, YES, YES, YES, 1650 BOOL off_the_side_button_is_hidden_results[] =
1592 YES, YES, YES, NO, NO, NO, NO}; 1651 { NO, NO, NO, NO, YES, YES, YES, YES, YES, YES, YES, NO, NO, NO, NO};
1593 int displayedButtonCountResults[] = { 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 2, 1652 int displayed_button_count_results[] =
1594 2, 1 }; 1653 { 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1 };
1595 for (unsigned int i = 0; i < arraysize(viewWidthsYosemite); ++i) { 1654 for (unsigned int i = 0; i < arraysize(view_widths_yosemite); ++i) {
1596 NSRect frame = [[bar_ view] frame]; 1655 NSRect frame = [[bar_ view] frame];
1597 frame.size.width = viewWidths[i] + bookmarks::kBookmarkRightMargin; 1656 frame.size.width = view_widths[i] + bookmarks::BookmarkRightMargin();
1598 [[bar_ view] setFrame:frame]; 1657 [[bar_ view] setFrame:frame];
1599 EXPECT_EQ(offTheSideButtonIsHiddenResults[i], 1658 EXPECT_EQ(off_the_side_button_is_hidden_results[i],
1600 [bar_ offTheSideButtonIsHidden]); 1659 [bar_ offTheSideButtonIsHidden]);
1601 EXPECT_EQ(displayedButtonCountResults[i], [bar_ displayedButtonCount]); 1660 EXPECT_EQ(displayed_button_count_results[i], [bar_ displayedButtonCount]);
1602 } 1661 }
1603 } 1662 }
1604 1663
1605 TEST_F(BookmarkBarControllerTest, BookmarksWithAppsPageShortcut) { 1664 TEST_F(BookmarkBarControllerTest, BookmarksWithAppsPageShortcut) {
1606 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); 1665 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1607 const BookmarkNode* root = model->bookmark_bar_node(); 1666 const BookmarkNode* root = model->bookmark_bar_node();
1608 const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b "); 1667 const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
1609 bookmarks::test::AddNodesFromModelString(model, root, model_string); 1668 bookmarks::test::AddNodesFromModelString(model, root, model_string);
1610 [bar_ frameDidChange]; 1669 [bar_ frameDidChange];
1611 1670
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
2043 ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]); 2102 ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
2044 2103
2045 // Validate initial model. 2104 // Validate initial model.
2046 std::string actualModel = bookmarks::test::ModelStringFromNode(root); 2105 std::string actualModel = bookmarks::test::ModelStringFromNode(root);
2047 EXPECT_EQ(model_string, actualModel); 2106 EXPECT_EQ(model_string, actualModel);
2048 2107
2049 // Test a series of points starting at the right edge of the bar. 2108 // Test a series of points starting at the right edge of the bar.
2050 BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"1b"]; 2109 BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"1b"];
2051 ASSERT_TRUE(targetButton); 2110 ASSERT_TRUE(targetButton);
2052 NSPoint targetPoint = [targetButton left]; 2111 NSPoint targetPoint = [targetButton left];
2053 CGFloat leftMarginIndicatorPosition = bookmarks::kBookmarkLeftMargin - 0.5 * 2112 CGFloat leftMarginIndicatorPosition = bookmarks::BookmarkLeftMargin() - 0.5 *
2054 bookmarks::kBookmarkHorizontalPadding; 2113 bookmarks::BookmarkHorizontalPadding();
2055 const CGFloat baseOffset = targetPoint.x; 2114 const CGFloat baseOffset = targetPoint.x;
2056 CGFloat expected = leftMarginIndicatorPosition; 2115 CGFloat expected = leftMarginIndicatorPosition;
2057 CGFloat actual = [bar_ indicatorPosForDragToPoint:targetPoint]; 2116 CGFloat actual = [bar_ indicatorPosForDragToPoint:targetPoint];
2058 EXPECT_CGFLOAT_EQ(expected, actual); 2117 EXPECT_CGFLOAT_EQ(expected, actual);
2059 targetButton = [bar_ buttonWithTitleEqualTo:@"2f"]; 2118 targetButton = [bar_ buttonWithTitleEqualTo:@"2f"];
2060 actual = [bar_ indicatorPosForDragToPoint:[targetButton right]]; 2119 actual = [bar_ indicatorPosForDragToPoint:[targetButton right]];
2061 targetButton = [bar_ buttonWithTitleEqualTo:@"3b"]; 2120 targetButton = [bar_ buttonWithTitleEqualTo:@"3b"];
2062 expected = [targetButton left].x - baseOffset + leftMarginIndicatorPosition; 2121 expected = [targetButton left].x - baseOffset + leftMarginIndicatorPosition;
2063 EXPECT_CGFLOAT_EQ(expected, actual); 2122 EXPECT_CGFLOAT_EQ(expected, actual);
2064 targetButton = [bar_ buttonWithTitleEqualTo:@"4b"]; 2123 targetButton = [bar_ buttonWithTitleEqualTo:@"4b"];
2065 targetPoint = [targetButton right]; 2124 targetPoint = [targetButton right];
2066 targetPoint.x += 100; // Somewhere off to the right. 2125 targetPoint.x += 100; // Somewhere off to the right.
2067 CGFloat xDelta = 0.5 * bookmarks::kBookmarkHorizontalPadding; 2126 CGFloat xDelta = 0.5 * bookmarks::BookmarkHorizontalPadding();
2068 expected = NSMaxX([targetButton frame]) + xDelta; 2127 expected = NSMaxX([targetButton frame]) + xDelta;
2069 actual = [bar_ indicatorPosForDragToPoint:targetPoint]; 2128 actual = [bar_ indicatorPosForDragToPoint:targetPoint];
2070 EXPECT_CGFLOAT_EQ(expected, actual); 2129 EXPECT_CGFLOAT_EQ(expected, actual);
2071 } 2130 }
2072 2131
2073 TEST_F(BookmarkBarControllerDragDropTest, PulseButton) { 2132 TEST_F(BookmarkBarControllerDragDropTest, PulseButton) {
2074 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); 2133 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
2075 const BookmarkNode* root = model->bookmark_bar_node(); 2134 const BookmarkNode* root = model->bookmark_bar_node();
2076 GURL gurl("http://www.google.com"); 2135 GURL gurl("http://www.google.com");
2077 const BookmarkNode* node = model->AddURL(root, root->child_count(), 2136 const BookmarkNode* node = model->AddURL(root, root->child_count(),
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2141 "2f3b ] 4b "); 2200 "2f3b ] 4b ");
2142 actual = bookmarks::test::ModelStringFromNode(root); 2201 actual = bookmarks::test::ModelStringFromNode(root);
2143 EXPECT_EQ(expected, actual); 2202 EXPECT_EQ(expected, actual);
2144 2203
2145 // Verify that the other bookmark folder can't be deleted. 2204 // Verify that the other bookmark folder can't be deleted.
2146 BookmarkButton *otherButton = [bar_ otherBookmarksButton]; 2205 BookmarkButton *otherButton = [bar_ otherBookmarksButton];
2147 EXPECT_FALSE([bar_ canDragBookmarkButtonToTrash:otherButton]); 2206 EXPECT_FALSE([bar_ canDragBookmarkButtonToTrash:otherButton]);
2148 } 2207 }
2149 2208
2150 } // namespace 2209 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698