[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_bar_controller_unittest.mm
blob0810bf67b94331b8db105f9d2ccb6bae6458a419
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
3 // found in the LICENSE file.
5 #import <Cocoa/Cocoa.h>
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/extensions/test_extension_system.h"
16 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_constants.h"
17 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
18 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_window.h"
19 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_unittest_helper.h"
20 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_view.h"
21 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
22 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h"
23 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
24 #import "chrome/browser/ui/cocoa/view_resizer_pong.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "components/bookmarks/browser/bookmark_model.h"
29 #include "components/bookmarks/browser/bookmark_utils.h"
30 #include "components/bookmarks/test/bookmark_test_helpers.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #import "testing/gtest_mac.h"
33 #include "testing/platform_test.h"
34 #import "third_party/ocmock/OCMock/OCMock.h"
35 #include "third_party/ocmock/gtest_support.h"
36 #include "ui/base/cocoa/animation_utils.h"
37 #include "ui/base/theme_provider.h"
38 #include "ui/events/test/cocoa_test_event_utils.h"
39 #include "ui/gfx/image/image_skia.h"
41 using base::ASCIIToUTF16;
43 // Unit tests don't need time-consuming asynchronous animations.
44 @interface BookmarkBarControllerTestable : BookmarkBarController {
47 @end
49 @implementation BookmarkBarControllerTestable
51 - (id)initWithBrowser:(Browser*)browser
52          initialWidth:(CGFloat)initialWidth
53              delegate:(id<BookmarkBarControllerDelegate>)delegate
54        resizeDelegate:(id<ViewResizer>)resizeDelegate {
55   if ((self = [super initWithBrowser:browser
56                         initialWidth:initialWidth
57                             delegate:delegate
58                       resizeDelegate:resizeDelegate])) {
59     [self setStateAnimationsEnabled:NO];
60     [self setInnerContentAnimationsEnabled:NO];
61   }
62   return self;
65 @end
67 // Just like a BookmarkBarController but openURL: is stubbed out.
68 @interface BookmarkBarControllerNoOpen : BookmarkBarControllerTestable {
69  @public
70   std::vector<GURL> urls_;
71   std::vector<WindowOpenDisposition> dispositions_;
73 @end
75 @implementation BookmarkBarControllerNoOpen
76 - (void)openURL:(GURL)url disposition:(WindowOpenDisposition)disposition {
77   urls_.push_back(url);
78   dispositions_.push_back(disposition);
80 - (void)clear {
81   urls_.clear();
82   dispositions_.clear();
84 @end
87 // NSCell that is pre-provided with a desired size that becomes the
88 // return value for -(NSSize)cellSize:.
89 @interface CellWithDesiredSize : NSCell {
90  @private
91   NSSize cellSize_;
93 @property (nonatomic, readonly) NSSize cellSize;
94 @end
96 @implementation CellWithDesiredSize
98 @synthesize cellSize = cellSize_;
100 - (id)initTextCell:(NSString*)string desiredSize:(NSSize)size {
101   if ((self = [super initTextCell:string])) {
102     cellSize_ = size;
103   }
104   return self;
107 @end
109 // Remember the number of times we've gotten a frameDidChange notification.
110 @interface BookmarkBarControllerTogglePong : BookmarkBarControllerNoOpen {
111  @private
112   int toggles_;
114 @property (nonatomic, readonly) int toggles;
115 @end
117 @implementation BookmarkBarControllerTogglePong
119 @synthesize toggles = toggles_;
121 - (void)frameDidChange {
122   toggles_++;
125 @end
127 // Remembers if a notification callback was called.
128 @interface BookmarkBarControllerNotificationPong : BookmarkBarControllerNoOpen {
129   BOOL windowWillCloseReceived_;
130   BOOL windowDidResignKeyReceived_;
132 @property (nonatomic, readonly) BOOL windowWillCloseReceived;
133 @property (nonatomic, readonly) BOOL windowDidResignKeyReceived;
134 @end
136 @implementation BookmarkBarControllerNotificationPong
137 @synthesize windowWillCloseReceived = windowWillCloseReceived_;
138 @synthesize windowDidResignKeyReceived = windowDidResignKeyReceived_;
140 // Override NSNotificationCenter callback.
141 - (void)parentWindowWillClose:(NSNotification*)notification {
142   windowWillCloseReceived_ = YES;
145 // NSNotificationCenter callback.
146 - (void)parentWindowDidResignKey:(NSNotification*)notification {
147   windowDidResignKeyReceived_ = YES;
149 @end
151 // Remembers if and what kind of openAll was performed.
152 @interface BookmarkBarControllerOpenAllPong : BookmarkBarControllerNoOpen {
153   WindowOpenDisposition dispositionDetected_;
155 @property (nonatomic) WindowOpenDisposition dispositionDetected;
156 @end
158 @implementation BookmarkBarControllerOpenAllPong
159 @synthesize dispositionDetected = dispositionDetected_;
161 // Intercede for the openAll:disposition: method.
162 - (void)openAll:(const BookmarkNode*)node
163     disposition:(WindowOpenDisposition)disposition {
164   [self setDispositionDetected:disposition];
167 @end
169 // Just like a BookmarkBarController but intercedes when providing
170 // pasteboard drag data.
171 @interface BookmarkBarControllerDragData : BookmarkBarControllerTestable {
172   const BookmarkNode* dragDataNode_;  // Weak
174 - (void)setDragDataNode:(const BookmarkNode*)node;
175 @end
177 @implementation BookmarkBarControllerDragData
179 - (id)initWithBrowser:(Browser*)browser
180          initialWidth:(CGFloat)initialWidth
181              delegate:(id<BookmarkBarControllerDelegate>)delegate
182        resizeDelegate:(id<ViewResizer>)resizeDelegate {
183   if ((self = [super initWithBrowser:browser
184                         initialWidth:initialWidth
185                             delegate:delegate
186                       resizeDelegate:resizeDelegate])) {
187     dragDataNode_ = NULL;
188   }
189   return self;
192 - (void)setDragDataNode:(const BookmarkNode*)node {
193   dragDataNode_ = node;
196 - (std::vector<const BookmarkNode*>)retrieveBookmarkNodeData {
197   std::vector<const BookmarkNode*> dragDataNodes;
198   if(dragDataNode_) {
199     dragDataNodes.push_back(dragDataNode_);
200   }
201   return dragDataNodes;
204 @end
207 class FakeTheme : public ui::ThemeProvider {
208  public:
209   FakeTheme(NSColor* color) : color_(color) {}
210   base::scoped_nsobject<NSColor> color_;
212   virtual bool UsingSystemTheme() const OVERRIDE {
213     return true;
214   }
215   virtual gfx::ImageSkia* GetImageSkiaNamed(int id) const OVERRIDE {
216     return NULL;
217   }
218   virtual SkColor GetColor(int id) const OVERRIDE { return SkColor(); }
219   virtual int GetDisplayProperty(int id) const OVERRIDE {
220     return -1;
221   }
222   virtual bool ShouldUseNativeFrame() const OVERRIDE { return false; }
223   virtual bool HasCustomImage(int id) const OVERRIDE { return false; }
224   virtual base::RefCountedMemory* GetRawData(
225       int id,
226       ui::ScaleFactor scale_factor) const OVERRIDE {
227     return NULL;
228   }
229   virtual NSImage* GetNSImageNamed(int id) const OVERRIDE {
230     return nil;
231   }
232   virtual NSColor* GetNSImageColorNamed(int id) const OVERRIDE {
233     return nil;
234   }
235   virtual NSColor* GetNSColor(int id) const OVERRIDE {
236     return color_.get();
237   }
238   virtual NSColor* GetNSColorTint(int id) const OVERRIDE {
239     return nil;
240   }
241   virtual NSGradient* GetNSGradient(int id) const OVERRIDE {
242     return nil;
243   }
247 @interface FakeDragInfo : NSObject {
248  @public
249   NSPoint dropLocation_;
250   NSDragOperation sourceMask_;
252 @property (nonatomic, assign) NSPoint dropLocation;
253 - (void)setDraggingSourceOperationMask:(NSDragOperation)mask;
254 @end
256 @implementation FakeDragInfo
258 @synthesize dropLocation = dropLocation_;
260 - (id)init {
261   if ((self = [super init])) {
262     dropLocation_ = NSZeroPoint;
263     sourceMask_ = NSDragOperationMove;
264   }
265   return self;
268 // NSDraggingInfo protocol functions.
270 - (id)draggingPasteboard {
271   return self;
274 - (id)draggingSource {
275   return self;
278 - (NSDragOperation)draggingSourceOperationMask {
279   return sourceMask_;
282 - (NSPoint)draggingLocation {
283   return dropLocation_;
286 // Other functions.
288 - (void)setDraggingSourceOperationMask:(NSDragOperation)mask {
289   sourceMask_ = mask;
292 @end
295 namespace {
297 class BookmarkBarControllerTestBase : public CocoaProfileTest {
298  public:
299   base::scoped_nsobject<NSView> parent_view_;
300   base::scoped_nsobject<ViewResizerPong> resizeDelegate_;
302   virtual void SetUp() {
303     CocoaProfileTest::SetUp();
304     ASSERT_TRUE(profile());
306     base::FilePath extension_dir;
307     static_cast<extensions::TestExtensionSystem*>(
308         extensions::ExtensionSystem::Get(profile()))->
309         CreateExtensionService(
310             CommandLine::ForCurrentProcess(),
311             extension_dir, false);
312     resizeDelegate_.reset([[ViewResizerPong alloc] init]);
313     NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
314     parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]);
315     [parent_view_ setHidden:YES];
316   }
318   void InstallAndToggleBar(BookmarkBarController* bar) {
319     // Force loading of the nib.
320     [bar view];
321     // Awkwardness to look like we've been installed.
322     for (NSView* subView in [parent_view_ subviews])
323       [subView removeFromSuperview];
324     [parent_view_ addSubview:[bar view]];
325     NSRect frame = [[[bar view] superview] frame];
326     frame.origin.y = 100;
327     [[[bar view] superview] setFrame:frame];
329     // Make sure it's on in a window so viewDidMoveToWindow is called
330     NSView* contentView = [test_window() contentView];
331     if (![parent_view_ isDescendantOf:contentView])
332       [contentView addSubview:parent_view_];
334     // Make sure it's open so certain things aren't no-ops.
335     [bar updateState:BookmarkBar::SHOW
336           changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
337   }
340 class BookmarkBarControllerTest : public BookmarkBarControllerTestBase {
341  public:
342   base::scoped_nsobject<NSButtonCell> cell_;
343   base::scoped_nsobject<BookmarkBarControllerNoOpen> bar_;
345   virtual void SetUp() {
346     BookmarkBarControllerTestBase::SetUp();
347     ASSERT_TRUE(browser());
348     AddCommandLineSwitches();
350     bar_.reset(
351       [[BookmarkBarControllerNoOpen alloc]
352           initWithBrowser:browser()
353              initialWidth:NSWidth([parent_view_ frame])
354                  delegate:nil
355            resizeDelegate:resizeDelegate_.get()]);
357     InstallAndToggleBar(bar_.get());
358   }
360   virtual void AddCommandLineSwitches() {}
362   BookmarkBarControllerNoOpen* noOpenBar() {
363     return (BookmarkBarControllerNoOpen*)bar_.get();
364   }
367 TEST_F(BookmarkBarControllerTest, ShowWhenShowBookmarkBarTrue) {
368   [bar_ updateState:BookmarkBar::SHOW
369          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
370   EXPECT_TRUE([bar_ isInState:BookmarkBar::SHOW]);
371   EXPECT_FALSE([bar_ isInState:BookmarkBar::DETACHED]);
372   EXPECT_TRUE([bar_ isVisible]);
373   EXPECT_FALSE([bar_ isAnimationRunning]);
374   EXPECT_FALSE([[bar_ view] isHidden]);
375   EXPECT_GT([resizeDelegate_ height], 0);
376   EXPECT_GT([[bar_ view] frame].size.height, 0);
379 TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarFalse) {
380   [bar_ updateState:BookmarkBar::HIDDEN
381          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
382   EXPECT_FALSE([bar_ isInState:BookmarkBar::SHOW]);
383   EXPECT_FALSE([bar_ isInState:BookmarkBar::DETACHED]);
384   EXPECT_FALSE([bar_ isVisible]);
385   EXPECT_FALSE([bar_ isAnimationRunning]);
386   EXPECT_TRUE([[bar_ view] isHidden]);
387   EXPECT_EQ(0, [resizeDelegate_ height]);
388   EXPECT_EQ(0, [[bar_ view] frame].size.height);
391 TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarTrueButDisabled) {
392   [bar_ setBookmarkBarEnabled:NO];
393   [bar_ updateState:BookmarkBar::SHOW
394          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
395   EXPECT_TRUE([bar_ isInState:BookmarkBar::SHOW]);
396   EXPECT_FALSE([bar_ isInState:BookmarkBar::DETACHED]);
397   EXPECT_FALSE([bar_ isVisible]);
398   EXPECT_FALSE([bar_ isAnimationRunning]);
399   EXPECT_TRUE([[bar_ view] isHidden]);
400   EXPECT_EQ(0, [resizeDelegate_ height]);
401   EXPECT_EQ(0, [[bar_ view] frame].size.height);
404 TEST_F(BookmarkBarControllerTest, ShowOnNewTabPage) {
405   [bar_ updateState:BookmarkBar::DETACHED
406          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
407   EXPECT_FALSE([bar_ isInState:BookmarkBar::SHOW]);
408   EXPECT_TRUE([bar_ isInState:BookmarkBar::DETACHED]);
409   EXPECT_TRUE([bar_ isVisible]);
410   EXPECT_FALSE([bar_ isAnimationRunning]);
411   EXPECT_FALSE([[bar_ view] isHidden]);
412   EXPECT_GT([resizeDelegate_ height], 0);
413   EXPECT_GT([[bar_ view] frame].size.height, 0);
415   // Make sure no buttons fall off the bar, either now or when resized
416   // bigger or smaller.
417   CGFloat sizes[] = { 300.0, -100.0, 200.0, -420.0 };
418   CGFloat previousX = 0.0;
419   for (unsigned x = 0; x < arraysize(sizes); x++) {
420     // Confirm the buttons moved from the last check (which may be
421     // init but that's fine).
422     CGFloat newX = [[bar_ offTheSideButton] frame].origin.x;
423     EXPECT_NE(previousX, newX);
424     previousX = newX;
426     // Confirm the buttons have a reasonable bounds. Recall that |-frame|
427     // returns rectangles in the superview's coordinates.
428     NSRect buttonViewFrame =
429         [[bar_ buttonView] convertRect:[[bar_ buttonView] frame]
430                               fromView:[[bar_ buttonView] superview]];
431     EXPECT_EQ([bar_ buttonView], [[bar_ offTheSideButton] superview]);
432     EXPECT_TRUE(NSContainsRect(buttonViewFrame,
433                                [[bar_ offTheSideButton] frame]));
434     EXPECT_EQ([bar_ buttonView], [[bar_ otherBookmarksButton] superview]);
435     EXPECT_TRUE(NSContainsRect(buttonViewFrame,
436                                [[bar_ otherBookmarksButton] frame]));
438     // Now move them implicitly.
439     // We confirm FrameChangeNotification works in the next unit test;
440     // we simply assume it works here to resize or reposition the
441     // buttons above.
442     NSRect frame = [[bar_ view] frame];
443     frame.size.width += sizes[x];
444     [[bar_ view] setFrame:frame];
445   }
448 // Test whether |-updateState:...| sets currentState as expected. Make
449 // sure things don't crash.
450 TEST_F(BookmarkBarControllerTest, StateChanges) {
451   // First, go in one-at-a-time cycle.
452   [bar_ updateState:BookmarkBar::HIDDEN
453          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
454   EXPECT_EQ(BookmarkBar::HIDDEN, [bar_ currentState]);
455   EXPECT_FALSE([bar_ isVisible]);
456   EXPECT_FALSE([bar_ isAnimationRunning]);
458   [bar_ updateState:BookmarkBar::SHOW
459          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
460   EXPECT_EQ(BookmarkBar::SHOW, [bar_ currentState]);
461   EXPECT_TRUE([bar_ isVisible]);
462   EXPECT_FALSE([bar_ isAnimationRunning]);
464   [bar_ updateState:BookmarkBar::DETACHED
465          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
466   EXPECT_EQ(BookmarkBar::DETACHED, [bar_ currentState]);
467   EXPECT_TRUE([bar_ isVisible]);
468   EXPECT_FALSE([bar_ isAnimationRunning]);
470   // Now try some "jumps".
471   for (int i = 0; i < 2; i++) {
472   [bar_ updateState:BookmarkBar::HIDDEN
473          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
474     EXPECT_EQ(BookmarkBar::HIDDEN, [bar_ currentState]);
475     EXPECT_FALSE([bar_ isVisible]);
476     EXPECT_FALSE([bar_ isAnimationRunning]);
478     [bar_ updateState:BookmarkBar::SHOW
479            changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
480     EXPECT_EQ(BookmarkBar::SHOW, [bar_ currentState]);
481     EXPECT_TRUE([bar_ isVisible]);
482     EXPECT_FALSE([bar_ isAnimationRunning]);
483   }
485   // Now try some "jumps".
486   for (int i = 0; i < 2; i++) {
487     [bar_ updateState:BookmarkBar::SHOW
488            changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
489     EXPECT_EQ(BookmarkBar::SHOW, [bar_ currentState]);
490     EXPECT_TRUE([bar_ isVisible]);
491     EXPECT_FALSE([bar_ isAnimationRunning]);
493     [bar_ updateState:BookmarkBar::DETACHED
494            changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
495     EXPECT_EQ(BookmarkBar::DETACHED, [bar_ currentState]);
496     EXPECT_TRUE([bar_ isVisible]);
497     EXPECT_FALSE([bar_ isAnimationRunning]);
498   }
501 // Make sure we're watching for frame change notifications.
502 TEST_F(BookmarkBarControllerTest, FrameChangeNotification) {
503   base::scoped_nsobject<BookmarkBarControllerTogglePong> bar;
504   bar.reset(
505     [[BookmarkBarControllerTogglePong alloc]
506           initWithBrowser:browser()
507              initialWidth:100  // arbitrary
508                  delegate:nil
509            resizeDelegate:resizeDelegate_.get()]);
510   InstallAndToggleBar(bar.get());
512   // Send a frame did change notification for the pong's view.
513   [[NSNotificationCenter defaultCenter]
514     postNotificationName:NSViewFrameDidChangeNotification
515                   object:[bar view]];
517   EXPECT_GT([bar toggles], 0);
520 // Confirm our "no items" container goes away when we add the 1st
521 // bookmark, and comes back when we delete the bookmark.
522 TEST_F(BookmarkBarControllerTest, NoItemContainerGoesAway) {
523   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
524   const BookmarkNode* bar = model->bookmark_bar_node();
526   [bar_ loaded:model];
527   BookmarkBarView* view = [bar_ buttonView];
528   DCHECK(view);
529   NSView* noItemContainer = [view noItemContainer];
530   DCHECK(noItemContainer);
532   EXPECT_FALSE([noItemContainer isHidden]);
533   const BookmarkNode* node = model->AddURL(bar, bar->child_count(),
534                                            ASCIIToUTF16("title"),
535                                            GURL("http://www.google.com"));
536   EXPECT_TRUE([noItemContainer isHidden]);
537   model->Remove(bar, bar->GetIndexOf(node));
538   EXPECT_FALSE([noItemContainer isHidden]);
540   // Now try it using a bookmark from the Other Bookmarks.
541   const BookmarkNode* otherBookmarks = model->other_node();
542   node = model->AddURL(otherBookmarks, otherBookmarks->child_count(),
543                        ASCIIToUTF16("TheOther"),
544                        GURL("http://www.other.com"));
545   EXPECT_FALSE([noItemContainer isHidden]);
546   // Move it from Other Bookmarks to the bar.
547   model->Move(node, bar, 0);
548   EXPECT_TRUE([noItemContainer isHidden]);
549   // Move it back to Other Bookmarks from the bar.
550   model->Move(node, otherBookmarks, 0);
551   EXPECT_FALSE([noItemContainer isHidden]);
554 // Confirm off the side button only enabled when reasonable.
555 TEST_F(BookmarkBarControllerTest, OffTheSideButtonHidden) {
556   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
558   [bar_ loaded:model];
559   EXPECT_TRUE([bar_ offTheSideButtonIsHidden]);
561   for (int i = 0; i < 2; i++) {
562     bookmark_utils::AddIfNotBookmarked(
563         model, GURL("http://www.foo.com"), ASCIIToUTF16("small"));
564     EXPECT_TRUE([bar_ offTheSideButtonIsHidden]);
565   }
567   const BookmarkNode* parent = model->bookmark_bar_node();
568   for (int i = 0; i < 20; i++) {
569     model->AddURL(parent, parent->child_count(),
570                   ASCIIToUTF16("super duper wide title"),
571                   GURL("http://superfriends.hall-of-justice.edu"));
572   }
573   EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
575   // Open the "off the side" and start deleting nodes.  Make sure
576   // deletion of the last node in "off the side" causes the folder to
577   // close.
578   EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
579   NSButton* offTheSideButton = [bar_ offTheSideButton];
580   // Open "off the side" menu.
581   [bar_ openOffTheSideFolderFromButton:offTheSideButton];
582   BookmarkBarFolderController* bbfc = [bar_ folderController];
583   EXPECT_TRUE(bbfc);
584   [bbfc setIgnoreAnimations:YES];
585   while (!parent->empty()) {
586     // We've completed the job so we're done.
587     if ([bar_ offTheSideButtonIsHidden])
588       break;
589     // Delete the last button.
590     model->Remove(parent, parent->child_count() - 1);
591     // If last one make sure the menu is closed and the button is hidden.
592     // Else make sure menu stays open.
593     if ([bar_ offTheSideButtonIsHidden]) {
594       EXPECT_FALSE([bar_ folderController]);
595     } else {
596       EXPECT_TRUE([bar_ folderController]);
597     }
598   }
601 // http://crbug.com/46175 is a crash when deleting bookmarks from the
602 // off-the-side menu while it is open.  This test tries to bang hard
603 // in this area to reproduce the crash.
604 TEST_F(BookmarkBarControllerTest, DeleteFromOffTheSideWhileItIsOpen) {
605   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
606   [bar_ loaded:model];
608   // Add a lot of bookmarks (per the bug).
609   const BookmarkNode* parent = model->bookmark_bar_node();
610   for (int i = 0; i < 100; i++) {
611     std::ostringstream title;
612     title << "super duper wide title " << i;
613     model->AddURL(parent, parent->child_count(), ASCIIToUTF16(title.str()),
614                   GURL("http://superfriends.hall-of-justice.edu"));
615   }
616   EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
618   // Open "off the side" menu.
619   NSButton* offTheSideButton = [bar_ offTheSideButton];
620   [bar_ openOffTheSideFolderFromButton:offTheSideButton];
621   BookmarkBarFolderController* bbfc = [bar_ folderController];
622   EXPECT_TRUE(bbfc);
623   [bbfc setIgnoreAnimations:YES];
625   // Start deleting items; try and delete randomish ones in case it
626   // makes a difference.
627   int indices[] = { 2, 4, 5, 1, 7, 9, 2, 0, 10, 9 };
628   while (!parent->empty()) {
629     for (unsigned int i = 0; i < arraysize(indices); i++) {
630       if (indices[i] < parent->child_count()) {
631         // First we mouse-enter the button to make things harder.
632         NSArray* buttons = [bbfc buttons];
633         for (BookmarkButton* button in buttons) {
634           if ([button bookmarkNode] == parent->GetChild(indices[i])) {
635             [bbfc mouseEnteredButton:button event:nil];
636             break;
637           }
638         }
639         // Then we remove the node.  This triggers the button to get
640         // deleted.
641         model->Remove(parent, indices[i]);
642         // Force visual update which is otherwise delayed.
643         [[bbfc window] displayIfNeeded];
644       }
645     }
646   }
649 // Test whether |-dragShouldLockBarVisibility| returns NO iff the bar is
650 // detached.
651 TEST_F(BookmarkBarControllerTest, TestDragShouldLockBarVisibility) {
652   [bar_ updateState:BookmarkBar::HIDDEN
653          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
654   EXPECT_TRUE([bar_ dragShouldLockBarVisibility]);
656   [bar_ updateState:BookmarkBar::SHOW
657          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
658   EXPECT_TRUE([bar_ dragShouldLockBarVisibility]);
660   [bar_ updateState:BookmarkBar::DETACHED
661          changeType:BookmarkBar::DONT_ANIMATE_STATE_CHANGE];
662   EXPECT_FALSE([bar_ dragShouldLockBarVisibility]);
665 TEST_F(BookmarkBarControllerTest, TagMap) {
666   int64 ids[] = { 1, 3, 4, 40, 400, 4000, 800000000, 2, 123456789 };
667   std::vector<int32> tags;
669   // Generate some tags
670   for (unsigned int i = 0; i < arraysize(ids); i++) {
671     tags.push_back([bar_ menuTagFromNodeId:ids[i]]);
672   }
674   // Confirm reverse mapping.
675   for (unsigned int i = 0; i < arraysize(ids); i++) {
676     EXPECT_EQ(ids[i], [bar_ nodeIdFromMenuTag:tags[i]]);
677   }
679   // Confirm uniqueness.
680   std::sort(tags.begin(), tags.end());
681   for (unsigned int i=0; i<(tags.size()-1); i++) {
682     EXPECT_NE(tags[i], tags[i+1]);
683   }
686 TEST_F(BookmarkBarControllerTest, MenuForFolderNode) {
687   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
689   // First make sure something (e.g. "(empty)" string) is always present.
690   NSMenu* menu = [bar_ menuForFolderNode:model->bookmark_bar_node()];
691   EXPECT_GT([menu numberOfItems], 0);
693   // Test two bookmarks.
694   GURL gurl("http://www.foo.com");
695   bookmark_utils::AddIfNotBookmarked(model, gurl, ASCIIToUTF16("small"));
696   bookmark_utils::AddIfNotBookmarked(
697       model, GURL("http://www.cnn.com"), ASCIIToUTF16("bigger title"));
698   menu = [bar_ menuForFolderNode:model->bookmark_bar_node()];
699   EXPECT_EQ([menu numberOfItems], 2);
700   NSMenuItem *item = [menu itemWithTitle:@"bigger title"];
701   EXPECT_TRUE(item);
702   item = [menu itemWithTitle:@"small"];
703   EXPECT_TRUE(item);
704   if (item) {
705     int64 tag = [bar_ nodeIdFromMenuTag:[item tag]];
706     const BookmarkNode* node = GetBookmarkNodeByID(model, tag);
707     EXPECT_TRUE(node);
708     EXPECT_EQ(gurl, node->url());
709   }
711   // Test with an actual folder as well
712   const BookmarkNode* parent = model->bookmark_bar_node();
713   const BookmarkNode* folder = model->AddFolder(parent,
714                                                 parent->child_count(),
715                                                 ASCIIToUTF16("folder"));
716   model->AddURL(folder, folder->child_count(),
717                 ASCIIToUTF16("f1"), GURL("http://framma-lamma.com"));
718   model->AddURL(folder, folder->child_count(),
719                 ASCIIToUTF16("f2"), GURL("http://framma-lamma-ding-dong.com"));
720   menu = [bar_ menuForFolderNode:model->bookmark_bar_node()];
721   EXPECT_EQ([menu numberOfItems], 3);
723   item = [menu itemWithTitle:@"folder"];
724   EXPECT_TRUE(item);
725   EXPECT_TRUE([item hasSubmenu]);
726   NSMenu *submenu = [item submenu];
727   EXPECT_TRUE(submenu);
728   EXPECT_EQ(2, [submenu numberOfItems]);
729   EXPECT_TRUE([submenu itemWithTitle:@"f1"]);
730   EXPECT_TRUE([submenu itemWithTitle:@"f2"]);
733 // Confirm openBookmark: forwards the request to the controller's delegate
734 TEST_F(BookmarkBarControllerTest, OpenBookmark) {
735   GURL gurl("http://walla.walla.ding.dong.com");
736   scoped_ptr<BookmarkNode> node(new BookmarkNode(gurl));
738   base::scoped_nsobject<BookmarkButtonCell> cell(
739       [[BookmarkButtonCell alloc] init]);
740   [cell setBookmarkNode:node.get()];
741   base::scoped_nsobject<BookmarkButton> button([[BookmarkButton alloc] init]);
742   [button setCell:cell.get()];
743   [cell setRepresentedObject:[NSValue valueWithPointer:node.get()]];
745   [bar_ openBookmark:button];
746   EXPECT_EQ(noOpenBar()->urls_[0], node->url());
747   EXPECT_EQ(noOpenBar()->dispositions_[0], CURRENT_TAB);
750 TEST_F(BookmarkBarControllerTest, TestAddRemoveAndClear) {
751   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
752   NSView* buttonView = [bar_ buttonView];
753   EXPECT_EQ(0U, [[bar_ buttons] count]);
754   unsigned int initial_subview_count = [[buttonView subviews] count];
756   // Make sure a redundant call doesn't choke
757   [bar_ clearBookmarkBar];
758   EXPECT_EQ(0U, [[bar_ buttons] count]);
759   EXPECT_EQ(initial_subview_count, [[buttonView subviews] count]);
761   GURL gurl1("http://superfriends.hall-of-justice.edu");
762   // Short titles increase the chances of this test succeeding if the view is
763   // narrow.
764   // TODO(viettrungluu): make the test independent of window/view size, font
765   // metrics, button size and spacing, and everything else.
766   base::string16 title1(ASCIIToUTF16("x"));
767   bookmark_utils::AddIfNotBookmarked(model, gurl1, title1);
768   EXPECT_EQ(1U, [[bar_ buttons] count]);
769   EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]);
771   GURL gurl2("http://legion-of-doom.gov");
772   base::string16 title2(ASCIIToUTF16("y"));
773   bookmark_utils::AddIfNotBookmarked(model, gurl2, title2);
774   EXPECT_EQ(2U, [[bar_ buttons] count]);
775   EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]);
777   for (int i = 0; i < 3; i++) {
778     bookmark_utils::RemoveAllBookmarks(model, gurl2);
779     EXPECT_EQ(1U, [[bar_ buttons] count]);
780     EXPECT_EQ(1+initial_subview_count, [[buttonView subviews] count]);
782     // and bring it back
783     bookmark_utils::AddIfNotBookmarked(model, gurl2, title2);
784     EXPECT_EQ(2U, [[bar_ buttons] count]);
785     EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]);
786   }
788   [bar_ clearBookmarkBar];
789   EXPECT_EQ(0U, [[bar_ buttons] count]);
790   EXPECT_EQ(initial_subview_count, [[buttonView subviews] count]);
792   // Explicit test of loaded: since this is a convenient spot
793   [bar_ loaded:model];
794   EXPECT_EQ(2U, [[bar_ buttons] count]);
795   EXPECT_EQ(2+initial_subview_count, [[buttonView subviews] count]);
798 // Make sure we don't create too many buttons; we only really need
799 // ones that will be visible.
800 TEST_F(BookmarkBarControllerTest, TestButtonLimits) {
801   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
802   EXPECT_EQ(0U, [[bar_ buttons] count]);
803   // Add one; make sure we see it.
804   const BookmarkNode* parent = model->bookmark_bar_node();
805   model->AddURL(parent, parent->child_count(),
806                 ASCIIToUTF16("title"), GURL("http://www.google.com"));
807   EXPECT_EQ(1U, [[bar_ buttons] count]);
809   // Add 30 which we expect to be 'too many'.  Make sure we don't see
810   // 30 buttons.
811   model->Remove(parent, 0);
812   EXPECT_EQ(0U, [[bar_ buttons] count]);
813   for (int i=0; i<30; i++) {
814     model->AddURL(parent, parent->child_count(),
815                   ASCIIToUTF16("title"), GURL("http://www.google.com"));
816   }
817   int count = [[bar_ buttons] count];
818   EXPECT_LT(count, 30L);
820   // Add 10 more (to the front of the list so the on-screen buttons
821   // would change) and make sure the count stays the same.
822   for (int i=0; i<10; i++) {
823     model->AddURL(parent, 0,  /* index is 0, so front, not end */
824                   ASCIIToUTF16("title"), GURL("http://www.google.com"));
825   }
827   // Finally, grow the view and make sure the button count goes up.
828   NSRect frame = [[bar_ view] frame];
829   frame.size.width += 600;
830   [[bar_ view] setFrame:frame];
831   int finalcount = [[bar_ buttons] count];
832   EXPECT_GT(finalcount, count);
835 // Make sure that each button we add marches to the right and does not
836 // overlap with the previous one.
837 TEST_F(BookmarkBarControllerTest, TestButtonMarch) {
838   base::scoped_nsobject<NSMutableArray> cells([[NSMutableArray alloc] init]);
840   CGFloat widths[] = { 10, 10, 100, 10, 500, 500, 80000, 60000, 1, 345 };
841   for (unsigned int i = 0; i < arraysize(widths); i++) {
842     NSCell* cell = [[CellWithDesiredSize alloc]
843                      initTextCell:@"foo"
844                       desiredSize:NSMakeSize(widths[i], 30)];
845     [cells addObject:cell];
846     [cell release];
847   }
849   int x_offset = 0;
850   CGFloat x_end = x_offset;  // end of the previous button
851   for (unsigned int i = 0; i < arraysize(widths); i++) {
852     NSRect r = [bar_ frameForBookmarkButtonFromCell:[cells objectAtIndex:i]
853                                             xOffset:&x_offset];
854     EXPECT_GE(r.origin.x, x_end);
855     x_end = NSMaxX(r);
856   }
859 TEST_F(BookmarkBarControllerTest, CheckForGrowth) {
860   WithNoAnimation at_all; // Turn off Cocoa auto animation in this scope.
861   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
862   GURL gurl1("http://www.google.com");
863   base::string16 title1(ASCIIToUTF16("x"));
864   bookmark_utils::AddIfNotBookmarked(model, gurl1, title1);
866   GURL gurl2("http://www.google.com/blah");
867   base::string16 title2(ASCIIToUTF16("y"));
868   bookmark_utils::AddIfNotBookmarked(model, gurl2, title2);
870   EXPECT_EQ(2U, [[bar_ buttons] count]);
871   CGFloat width_1 = [[[bar_ buttons] objectAtIndex:0] frame].size.width;
872   CGFloat x_2 = [[[bar_ buttons] objectAtIndex:1] frame].origin.x;
874   NSButton* first = [[bar_ buttons] objectAtIndex:0];
875   [[first cell] setTitle:@"This is a really big title; watch out mom!"];
876   [bar_ checkForBookmarkButtonGrowth:first];
878   // Make sure the 1st button is now wider, the 2nd one is moved over,
879   // and they don't overlap.
880   NSRect frame_1 = [[[bar_ buttons] objectAtIndex:0] frame];
881   NSRect frame_2 = [[[bar_ buttons] objectAtIndex:1] frame];
882   EXPECT_GT(frame_1.size.width, width_1);
883   EXPECT_GT(frame_2.origin.x, x_2);
884   EXPECT_GE(frame_2.origin.x, frame_1.origin.x + frame_1.size.width);
887 TEST_F(BookmarkBarControllerTest, DeleteBookmark) {
888   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
890   const char* urls[] = { "https://secret.url.com",
891                          "http://super.duper.web.site.for.doodz.gov",
892                          "http://www.foo-bar-baz.com/" };
893   const BookmarkNode* parent = model->bookmark_bar_node();
894   for (unsigned int i = 0; i < arraysize(urls); i++) {
895     model->AddURL(parent, parent->child_count(),
896                   ASCIIToUTF16("title"), GURL(urls[i]));
897   }
898   EXPECT_EQ(3, parent->child_count());
899   const BookmarkNode* middle_node = parent->GetChild(1);
900   model->Remove(middle_node->parent(),
901                 middle_node->parent()->GetIndexOf(middle_node));
903   EXPECT_EQ(2, parent->child_count());
904   EXPECT_EQ(parent->GetChild(0)->url(), GURL(urls[0]));
905   // node 2 moved into spot 1
906   EXPECT_EQ(parent->GetChild(1)->url(), GURL(urls[2]));
909 // TODO(jrg): write a test to confirm that nodeFaviconLoaded calls
910 // checkForBookmarkButtonGrowth:.
912 TEST_F(BookmarkBarControllerTest, Cell) {
913   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
914   [bar_ loaded:model];
916   const BookmarkNode* parent = model->bookmark_bar_node();
917   model->AddURL(parent, parent->child_count(),
918                 ASCIIToUTF16("supertitle"),
919                 GURL("http://superfriends.hall-of-justice.edu"));
920   const BookmarkNode* node = parent->GetChild(0);
922   NSCell* cell = [bar_ cellForBookmarkNode:node];
923   EXPECT_TRUE(cell);
924   EXPECT_NSEQ(@"supertitle", [cell title]);
925   EXPECT_EQ(node, [[cell representedObject] pointerValue]);
926   EXPECT_TRUE([cell menu]);
928   // Empty cells still have a menu.
929   cell = [bar_ cellForBookmarkNode:nil];
930   EXPECT_TRUE([cell menu]);
931   // Even empty cells have a title (of "(empty)")
932   EXPECT_TRUE([cell title]);
934   // cell is autoreleased; no need to release here
937 // Test drawing, mostly to ensure nothing leaks or crashes.
938 TEST_F(BookmarkBarControllerTest, Display) {
939   [[bar_ view] display];
942 // Test that middle clicking on a bookmark button results in an open action.
943 TEST_F(BookmarkBarControllerTest, MiddleClick) {
944   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
945   GURL gurl1("http://www.google.com/");
946   base::string16 title1(ASCIIToUTF16("x"));
947   bookmark_utils::AddIfNotBookmarked(model, gurl1, title1);
949   EXPECT_EQ(1U, [[bar_ buttons] count]);
950   NSButton* first = [[bar_ buttons] objectAtIndex:0];
951   EXPECT_TRUE(first);
953   [first otherMouseUp:
954       cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0)];
955   EXPECT_EQ(noOpenBar()->urls_.size(), 1U);
958 TEST_F(BookmarkBarControllerTest, DisplaysHelpMessageOnEmpty) {
959   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
960   [bar_ loaded:model];
961   EXPECT_FALSE([[[bar_ buttonView] noItemContainer] isHidden]);
964 TEST_F(BookmarkBarControllerTest, HidesHelpMessageWithBookmark) {
965   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
967   const BookmarkNode* parent = model->bookmark_bar_node();
968   model->AddURL(parent, parent->child_count(),
969                 ASCIIToUTF16("title"), GURL("http://one.com"));
971   [bar_ loaded:model];
972   EXPECT_TRUE([[[bar_ buttonView] noItemContainer] isHidden]);
975 TEST_F(BookmarkBarControllerTest, BookmarkButtonSizing) {
976   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
978   const BookmarkNode* parent = model->bookmark_bar_node();
979   model->AddURL(parent, parent->child_count(),
980                 ASCIIToUTF16("title"), GURL("http://one.com"));
982   [bar_ loaded:model];
984   // Make sure the internal bookmark button also is the correct height.
985   NSArray* buttons = [bar_ buttons];
986   EXPECT_GT([buttons count], 0u);
987   for (NSButton* button in buttons) {
988     EXPECT_FLOAT_EQ(
989         (chrome::kBookmarkBarHeight + bookmarks::kVisualHeightOffset) -
990             2 * bookmarks::kBookmarkVerticalPadding,
991         [button frame].size.height);
992   }
995 TEST_F(BookmarkBarControllerTest, DropBookmarks) {
996   const char* urls[] = {
997     "http://qwantz.com",
998     "http://xkcd.com",
999     "javascript:alert('lolwut')",
1000     "file://localhost/tmp/local-file.txt"  // As if dragged from the desktop.
1001   };
1002   const char* titles[] = {
1003     "Philosophoraptor",
1004     "Can't draw",
1005     "Inspiration",
1006     "Frum stuf"
1007   };
1008   EXPECT_EQ(arraysize(urls), arraysize(titles));
1010   NSMutableArray* nsurls = [NSMutableArray array];
1011   NSMutableArray* nstitles = [NSMutableArray array];
1012   for (size_t i = 0; i < arraysize(urls); ++i) {
1013     [nsurls addObject:base::SysUTF8ToNSString(urls[i])];
1014     [nstitles addObject:base::SysUTF8ToNSString(titles[i])];
1015   }
1017   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1018   const BookmarkNode* parent = model->bookmark_bar_node();
1019   [bar_ addURLs:nsurls withTitles:nstitles at:NSZeroPoint];
1020   EXPECT_EQ(4, parent->child_count());
1021   for (int i = 0; i < parent->child_count(); ++i) {
1022     GURL gurl = parent->GetChild(i)->url();
1023     if (gurl.scheme() == "http" ||
1024         gurl.scheme() == "javascript") {
1025       EXPECT_EQ(parent->GetChild(i)->url(), GURL(urls[i]));
1026     } else {
1027       // Be flexible if the scheme needed to be added.
1028       std::string gurl_string = gurl.spec();
1029       std::string my_string = parent->GetChild(i)->url().spec();
1030       EXPECT_NE(gurl_string.find(my_string), std::string::npos);
1031     }
1032     EXPECT_EQ(parent->GetChild(i)->GetTitle(), ASCIIToUTF16(titles[i]));
1033   }
1036 TEST_F(BookmarkBarControllerTest, TestDragButton) {
1037   WithNoAnimation at_all;
1038   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1040   GURL gurls[] = { GURL("http://www.google.com/a"),
1041                    GURL("http://www.google.com/b"),
1042                    GURL("http://www.google.com/c") };
1043   base::string16 titles[] = { ASCIIToUTF16("a"),
1044                               ASCIIToUTF16("b"),
1045                               ASCIIToUTF16("c") };
1046   for (unsigned i = 0; i < arraysize(titles); i++)
1047     bookmark_utils::AddIfNotBookmarked(model, gurls[i], titles[i]);
1049   EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
1050   EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:0] title]);
1052   [bar_ dragButton:[[bar_ buttons] objectAtIndex:2]
1053                 to:NSZeroPoint
1054               copy:NO];
1055   EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:0] title]);
1056   // Make sure a 'copy' did not happen.
1057   EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
1059   [bar_ dragButton:[[bar_ buttons] objectAtIndex:1]
1060                 to:NSMakePoint(1000, 0)
1061               copy:NO];
1062   EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:0] title]);
1063   EXPECT_NSEQ(@"b", [[[bar_ buttons] objectAtIndex:1] title]);
1064   EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:2] title]);
1065   EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
1067   // A drop of the 1st between the next 2.
1068   CGFloat x = NSMinX([[[bar_ buttons] objectAtIndex:2] frame]);
1069   x += [[bar_ view] frame].origin.x;
1070   [bar_ dragButton:[[bar_ buttons] objectAtIndex:0]
1071                 to:NSMakePoint(x, 0)
1072               copy:NO];
1073   EXPECT_NSEQ(@"b", [[[bar_ buttons] objectAtIndex:0] title]);
1074   EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:1] title]);
1075   EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:2] title]);
1076   EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
1078   // A drop on a non-folder button.  (Shouldn't try and go in it.)
1079   x = NSMidX([[[bar_ buttons] objectAtIndex:0] frame]);
1080   x += [[bar_ view] frame].origin.x;
1081   [bar_ dragButton:[[bar_ buttons] objectAtIndex:2]
1082                 to:NSMakePoint(x, 0)
1083               copy:NO];
1084   EXPECT_EQ(arraysize(titles), [[bar_ buttons] count]);
1086   // A drop on a folder button.
1087   const BookmarkNode* folder = model->AddFolder(
1088       model->bookmark_bar_node(), 0, ASCIIToUTF16("awesome folder"));
1089   DCHECK(folder);
1090   model->AddURL(folder, 0, ASCIIToUTF16("already"),
1091                 GURL("http://www.google.com"));
1092   EXPECT_EQ(arraysize(titles) + 1, [[bar_ buttons] count]);
1093   EXPECT_EQ(1, folder->child_count());
1094   x = NSMidX([[[bar_ buttons] objectAtIndex:0] frame]);
1095   x += [[bar_ view] frame].origin.x;
1096   base::string16 title =
1097       [[[bar_ buttons] objectAtIndex:2] bookmarkNode]->GetTitle();
1098   [bar_ dragButton:[[bar_ buttons] objectAtIndex:2]
1099                 to:NSMakePoint(x, 0)
1100               copy:NO];
1101   // Gone from the bar
1102   EXPECT_EQ(arraysize(titles), [[bar_ buttons] count]);
1103   // In the folder
1104   EXPECT_EQ(2, folder->child_count());
1105   // At the end
1106   EXPECT_EQ(title, folder->GetChild(1)->GetTitle());
1109 TEST_F(BookmarkBarControllerTest, TestCopyButton) {
1110   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1112   GURL gurls[] = { GURL("http://www.google.com/a"),
1113                    GURL("http://www.google.com/b"),
1114                    GURL("http://www.google.com/c") };
1115   base::string16 titles[] = { ASCIIToUTF16("a"),
1116                               ASCIIToUTF16("b"),
1117                               ASCIIToUTF16("c") };
1118   for (unsigned i = 0; i < arraysize(titles); i++)
1119     bookmark_utils::AddIfNotBookmarked(model, gurls[i], titles[i]);
1121   EXPECT_EQ([[bar_ buttons] count], arraysize(titles));
1122   EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:0] title]);
1124   // Drag 'a' between 'b' and 'c'.
1125   CGFloat x = NSMinX([[[bar_ buttons] objectAtIndex:2] frame]);
1126   x += [[bar_ view] frame].origin.x;
1127   [bar_ dragButton:[[bar_ buttons] objectAtIndex:0]
1128                 to:NSMakePoint(x, 0)
1129               copy:YES];
1130   EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:0] title]);
1131   EXPECT_NSEQ(@"b", [[[bar_ buttons] objectAtIndex:1] title]);
1132   EXPECT_NSEQ(@"a", [[[bar_ buttons] objectAtIndex:2] title]);
1133   EXPECT_NSEQ(@"c", [[[bar_ buttons] objectAtIndex:3] title]);
1134   EXPECT_EQ([[bar_ buttons] count], 4U);
1137 // Fake a theme with colored text.  Apply it and make sure bookmark
1138 // buttons have the same colored text.  Repeat more than once.
1139 TEST_F(BookmarkBarControllerTest, TestThemedButton) {
1140   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1141   bookmark_utils::AddIfNotBookmarked(
1142       model, GURL("http://www.foo.com"), ASCIIToUTF16("small"));
1143   BookmarkButton* button = [[bar_ buttons] objectAtIndex:0];
1144   EXPECT_TRUE(button);
1146   NSArray* colors = [NSArray arrayWithObjects:[NSColor redColor],
1147                                               [NSColor blueColor],
1148                                               nil];
1149   for (NSColor* color in colors) {
1150     FakeTheme theme(color);
1151     [bar_ updateTheme:&theme];
1152     NSAttributedString* astr = [button attributedTitle];
1153     EXPECT_TRUE(astr);
1154     EXPECT_NSEQ(@"small", [astr string]);
1155     // Pick a char in the middle to test (index 3)
1156     NSDictionary* attributes = [astr attributesAtIndex:3 effectiveRange:NULL];
1157     NSColor* newColor =
1158         [attributes objectForKey:NSForegroundColorAttributeName];
1159     EXPECT_NSEQ(newColor, color);
1160   }
1163 // Test that delegates and targets of buttons are cleared on dealloc.
1164 TEST_F(BookmarkBarControllerTest, TestClearOnDealloc) {
1165   // Make some bookmark buttons.
1166   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1167   GURL gurls[] = { GURL("http://www.foo.com/"),
1168                    GURL("http://www.bar.com/"),
1169                    GURL("http://www.baz.com/") };
1170   base::string16 titles[] = { ASCIIToUTF16("a"),
1171                               ASCIIToUTF16("b"),
1172                               ASCIIToUTF16("c") };
1173   for (size_t i = 0; i < arraysize(titles); i++)
1174     bookmark_utils::AddIfNotBookmarked(model, gurls[i], titles[i]);
1176   // Get and retain the buttons so we can examine them after dealloc.
1177   base::scoped_nsobject<NSArray> buttons([[bar_ buttons] retain]);
1178   EXPECT_EQ([buttons count], arraysize(titles));
1180   // Make sure that everything is set.
1181   for (BookmarkButton* button in buttons.get()) {
1182     ASSERT_TRUE([button isKindOfClass:[BookmarkButton class]]);
1183     EXPECT_TRUE([button delegate]);
1184     EXPECT_TRUE([button target]);
1185     EXPECT_TRUE([button action]);
1186   }
1188   // This will dealloc....
1189   bar_.reset();
1191   // Make sure that everything is cleared.
1192   for (BookmarkButton* button in buttons.get()) {
1193     EXPECT_FALSE([button delegate]);
1194     EXPECT_FALSE([button target]);
1195     EXPECT_FALSE([button action]);
1196   }
1199 TEST_F(BookmarkBarControllerTest, TestFolders) {
1200   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1202   // Create some folder buttons.
1203   const BookmarkNode* parent = model->bookmark_bar_node();
1204   const BookmarkNode* folder = model->AddFolder(parent,
1205                                                 parent->child_count(),
1206                                                 ASCIIToUTF16("folder"));
1207   model->AddURL(folder, folder->child_count(),
1208                 ASCIIToUTF16("f1"), GURL("http://framma-lamma.com"));
1209   folder = model->AddFolder(parent, parent->child_count(),
1210                             ASCIIToUTF16("empty"));
1212   EXPECT_EQ([[bar_ buttons] count], 2U);
1214   // First confirm mouseEntered does nothing if "menus" aren't active.
1215   NSEvent* event =
1216       cocoa_test_event_utils::MouseEventWithType(NSOtherMouseUp, 0);
1217   [bar_ mouseEnteredButton:[[bar_ buttons] objectAtIndex:0] event:event];
1218   EXPECT_FALSE([bar_ folderController]);
1220   // Make one active.  Entering it is now a no-op.
1221   [bar_ openBookmarkFolderFromButton:[[bar_ buttons] objectAtIndex:0]];
1222   BookmarkBarFolderController* bbfc = [bar_ folderController];
1223   EXPECT_TRUE(bbfc);
1224   [bar_ mouseEnteredButton:[[bar_ buttons] objectAtIndex:0] event:event];
1225   EXPECT_EQ(bbfc, [bar_ folderController]);
1227   // Enter a different one; a new folderController is active.
1228   [bar_ mouseEnteredButton:[[bar_ buttons] objectAtIndex:1] event:event];
1229   EXPECT_NE(bbfc, [bar_ folderController]);
1231   // Confirm exited is a no-op.
1232   [bar_ mouseExitedButton:[[bar_ buttons] objectAtIndex:1] event:event];
1233   EXPECT_NE(bbfc, [bar_ folderController]);
1235   // Clean up.
1236   [bar_ closeBookmarkFolder:nil];
1239 // Verify that the folder menu presentation properly tracks mouse movements
1240 // over the bar. Until there is a click no folder menus should show. After a
1241 // click on a folder folder menus should show until another click on a folder
1242 // button, and a click outside the bar and its folder menus.
1243 TEST_F(BookmarkBarControllerTest, TestFolderButtons) {
1244   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1245   const BookmarkNode* root = model->bookmark_bar_node();
1246   const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b 4f:[ 4f1b 4f2b ] ");
1247   test::AddNodesFromModelString(model, root, model_string);
1249   // Validate initial model and that we do not have a folder controller.
1250   std::string actualModelString = test::ModelStringFromNode(root);
1251   EXPECT_EQ(model_string, actualModelString);
1252   EXPECT_FALSE([bar_ folderController]);
1254   // Add a real bookmark so we can click on it.
1255   const BookmarkNode* folder = root->GetChild(3);
1256   model->AddURL(folder, folder->child_count(), ASCIIToUTF16("CLICK ME"),
1257                 GURL("http://www.google.com/"));
1259   // Click on a folder button.
1260   BookmarkButton* button = [bar_ buttonWithTitleEqualTo:@"4f"];
1261   EXPECT_TRUE(button);
1262   [bar_ openBookmarkFolderFromButton:button];
1263   BookmarkBarFolderController* bbfc = [bar_ folderController];
1264   EXPECT_TRUE(bbfc);
1266   // Make sure a 2nd click on the same button closes things.
1267   [bar_ openBookmarkFolderFromButton:button];
1268   EXPECT_FALSE([bar_ folderController]);
1270   // Next open is a different button.
1271   button = [bar_ buttonWithTitleEqualTo:@"2f"];
1272   EXPECT_TRUE(button);
1273   [bar_ openBookmarkFolderFromButton:button];
1274   EXPECT_TRUE([bar_ folderController]);
1276   // Mouse over a non-folder button and confirm controller has gone away.
1277   button = [bar_ buttonWithTitleEqualTo:@"1b"];
1278   EXPECT_TRUE(button);
1279   NSEvent* event = cocoa_test_event_utils::MouseEventAtPoint([button center],
1280                                                              NSMouseMoved, 0);
1281   [bar_ mouseEnteredButton:button event:event];
1282   EXPECT_FALSE([bar_ folderController]);
1284   // Mouse over the original folder and confirm a new controller.
1285   button = [bar_ buttonWithTitleEqualTo:@"2f"];
1286   EXPECT_TRUE(button);
1287   [bar_ mouseEnteredButton:button event:event];
1288   BookmarkBarFolderController* oldBBFC = [bar_ folderController];
1289   EXPECT_TRUE(oldBBFC);
1291   // 'Jump' over to a different folder and confirm a new controller.
1292   button = [bar_ buttonWithTitleEqualTo:@"4f"];
1293   EXPECT_TRUE(button);
1294   [bar_ mouseEnteredButton:button event:event];
1295   BookmarkBarFolderController* newBBFC = [bar_ folderController];
1296   EXPECT_TRUE(newBBFC);
1297   EXPECT_NE(oldBBFC, newBBFC);
1300 // Make sure the "off the side" folder looks like a bookmark folder
1301 // but only contains "off the side" items.
1302 TEST_F(BookmarkBarControllerTest, OffTheSideFolder) {
1304   // It starts hidden.
1305   EXPECT_TRUE([bar_ offTheSideButtonIsHidden]);
1307   // Create some buttons.
1308   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1309   const BookmarkNode* parent = model->bookmark_bar_node();
1310   for (int x = 0; x < 30; x++) {
1311     model->AddURL(parent, parent->child_count(),
1312                   ASCIIToUTF16("medium-size-title"),
1313                   GURL("http://framma-lamma.com"));
1314   }
1315   // Add a couple more so we can delete one and make sure its button goes away.
1316   model->AddURL(parent, parent->child_count(),
1317                 ASCIIToUTF16("DELETE_ME"), GURL("http://ashton-tate.com"));
1318   model->AddURL(parent, parent->child_count(),
1319                 ASCIIToUTF16("medium-size-title"),
1320                 GURL("http://framma-lamma.com"));
1322   // Should no longer be hidden.
1323   EXPECT_FALSE([bar_ offTheSideButtonIsHidden]);
1325   // Open it; make sure we have a folder controller.
1326   EXPECT_FALSE([bar_ folderController]);
1327   [bar_ openOffTheSideFolderFromButton:[bar_ offTheSideButton]];
1328   BookmarkBarFolderController* bbfc = [bar_ folderController];
1329   EXPECT_TRUE(bbfc);
1331   // Confirm the contents are only buttons which fell off the side by
1332   // making sure that none of the nodes in the off-the-side folder are
1333   // found in bar buttons.  Be careful since not all the bar buttons
1334   // may be currently displayed.
1335   NSArray* folderButtons = [bbfc buttons];
1336   NSArray* barButtons = [bar_ buttons];
1337   for (BookmarkButton* folderButton in folderButtons) {
1338     for (BookmarkButton* barButton in barButtons) {
1339       if ([barButton superview]) {
1340         EXPECT_NE([folderButton bookmarkNode], [barButton bookmarkNode]);
1341       }
1342     }
1343   }
1345   // Delete a bookmark in the off-the-side and verify it's gone.
1346   BookmarkButton* button = [bbfc buttonWithTitleEqualTo:@"DELETE_ME"];
1347   EXPECT_TRUE(button);
1348   model->Remove(parent, parent->child_count() - 2);
1349   button = [bbfc buttonWithTitleEqualTo:@"DELETE_ME"];
1350   EXPECT_FALSE(button);
1353 TEST_F(BookmarkBarControllerTest, EventToExitCheck) {
1354   NSEvent* event = cocoa_test_event_utils::MouseEventWithType(NSMouseMoved, 0);
1355   EXPECT_FALSE([bar_ isEventAnExitEvent:event]);
1357   BookmarkBarFolderWindow* folderWindow = [[[BookmarkBarFolderWindow alloc]
1358                                              init] autorelease];
1359   [[[bar_ view] window] addChildWindow:folderWindow
1360                                ordered:NSWindowAbove];
1361   event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(NSMakePoint(1,1),
1362                                                                folderWindow);
1363   EXPECT_FALSE([bar_ isEventAnExitEvent:event]);
1365   event = cocoa_test_event_utils::LeftMouseDownAtPointInWindow(
1366       NSMakePoint(100,100), test_window());
1367   EXPECT_TRUE([bar_ isEventAnExitEvent:event]);
1369   // Many components are arbitrary (e.g. location, keycode).
1370   event = [NSEvent keyEventWithType:NSKeyDown
1371                            location:NSMakePoint(1,1)
1372                       modifierFlags:0
1373                           timestamp:0
1374                        windowNumber:0
1375                             context:nil
1376                          characters:@"x"
1377         charactersIgnoringModifiers:@"x"
1378                           isARepeat:NO
1379                             keyCode:87];
1380   EXPECT_FALSE([bar_ isEventAnExitEvent:event]);
1382   [[[bar_ view] window] removeChildWindow:folderWindow];
1385 TEST_F(BookmarkBarControllerTest, DropDestination) {
1386   // Make some buttons.
1387   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1388   const BookmarkNode* parent = model->bookmark_bar_node();
1389   model->AddFolder(parent, parent->child_count(), ASCIIToUTF16("folder 1"));
1390   model->AddFolder(parent, parent->child_count(), ASCIIToUTF16("folder 2"));
1391   EXPECT_EQ([[bar_ buttons] count], 2U);
1393   // Confirm "off to left" and "off to right" match nothing.
1394   NSPoint p = NSMakePoint(-1, 2);
1395   EXPECT_FALSE([bar_ buttonForDroppingOnAtPoint:p]);
1396   EXPECT_TRUE([bar_ shouldShowIndicatorShownForPoint:p]);
1397   p = NSMakePoint(50000, 10);
1398   EXPECT_FALSE([bar_ buttonForDroppingOnAtPoint:p]);
1399   EXPECT_TRUE([bar_ shouldShowIndicatorShownForPoint:p]);
1401   // Confirm "right in the center" (give or take a pixel) is a match,
1402   // and confirm "just barely in the button" is not.  Anything more
1403   // specific seems likely to be tweaked.
1404   CGFloat viewFrameXOffset = [[bar_ view] frame].origin.x;
1405   for (BookmarkButton* button in [bar_ buttons]) {
1406     CGFloat x = NSMidX([button frame]) + viewFrameXOffset;
1407     // Somewhere near the center: a match
1408     EXPECT_EQ(button,
1409               [bar_ buttonForDroppingOnAtPoint:NSMakePoint(x-1, 10)]);
1410     EXPECT_EQ(button,
1411               [bar_ buttonForDroppingOnAtPoint:NSMakePoint(x+1, 10)]);
1412     EXPECT_FALSE([bar_ shouldShowIndicatorShownForPoint:NSMakePoint(x, 10)]);;
1414     // On the very edges: NOT a match
1415     x = NSMinX([button frame]) + viewFrameXOffset;
1416     EXPECT_NE(button,
1417               [bar_ buttonForDroppingOnAtPoint:NSMakePoint(x, 9)]);
1418     x = NSMaxX([button frame]) + viewFrameXOffset;
1419     EXPECT_NE(button,
1420               [bar_ buttonForDroppingOnAtPoint:NSMakePoint(x, 11)]);
1421   }
1424 TEST_F(BookmarkBarControllerTest, CloseFolderOnAnimate) {
1425   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1426   [bar_ setStateAnimationsEnabled:YES];
1427   const BookmarkNode* parent = model->bookmark_bar_node();
1428   const BookmarkNode* folder = model->AddFolder(parent,
1429                                                 parent->child_count(),
1430                                                 ASCIIToUTF16("folder"));
1431   model->AddFolder(parent, parent->child_count(),
1432                   ASCIIToUTF16("sibbling folder"));
1433   model->AddURL(folder, folder->child_count(), ASCIIToUTF16("title a"),
1434                 GURL("http://www.google.com/a"));
1435   model->AddURL(folder, folder->child_count(),
1436       ASCIIToUTF16("title super duper long long whoa momma title you betcha"),
1437       GURL("http://www.google.com/b"));
1438   BookmarkButton* button = [[bar_ buttons] objectAtIndex:0];
1439   EXPECT_FALSE([bar_ folderController]);
1440   [bar_ openBookmarkFolderFromButton:button];
1441   BookmarkBarFolderController* bbfc = [bar_ folderController];
1442   // The following tells us that the folder menu is showing. We want to make
1443   // sure the folder menu goes away if the bookmark bar is hidden.
1444   EXPECT_TRUE(bbfc);
1445   EXPECT_TRUE([bar_ isVisible]);
1447   // Hide the bookmark bar.
1448   [bar_ updateState:BookmarkBar::DETACHED
1449          changeType:BookmarkBar::ANIMATE_STATE_CHANGE];
1450   EXPECT_TRUE([bar_ isAnimationRunning]);
1452   // Now that we've closed the bookmark bar (with animation) the folder menu
1453   // should have been closed thus releasing the folderController.
1454   EXPECT_FALSE([bar_ folderController]);
1457 TEST_F(BookmarkBarControllerTest, MoveRemoveAddButtons) {
1458   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1459   const BookmarkNode* root = model->bookmark_bar_node();
1460   const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
1461   test::AddNodesFromModelString(model, root, model_string);
1463   // Validate initial model.
1464   std::string actualModelString = test::ModelStringFromNode(root);
1465   EXPECT_EQ(model_string, actualModelString);
1467   // Remember how many buttons are showing.
1468   int oldDisplayedButtons = [bar_ displayedButtonCount];
1469   NSArray* buttons = [bar_ buttons];
1471   // Move a button around a bit.
1472   [bar_ moveButtonFromIndex:0 toIndex:2];
1473   EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:0] title]);
1474   EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:1] title]);
1475   EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:2] title]);
1476   EXPECT_EQ(oldDisplayedButtons, [bar_ displayedButtonCount]);
1477   [bar_ moveButtonFromIndex:2 toIndex:0];
1478   EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:0] title]);
1479   EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:1] title]);
1480   EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:2] title]);
1481   EXPECT_EQ(oldDisplayedButtons, [bar_ displayedButtonCount]);
1483   // Add a couple of buttons.
1484   const BookmarkNode* parent = root->GetChild(1); // Purloin an existing node.
1485   const BookmarkNode* node = parent->GetChild(0);
1486   [bar_ addButtonForNode:node atIndex:0];
1487   EXPECT_NSEQ(@"2f1b", [[buttons objectAtIndex:0] title]);
1488   EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:1] title]);
1489   EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:2] title]);
1490   EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:3] title]);
1491   EXPECT_EQ(oldDisplayedButtons + 1, [bar_ displayedButtonCount]);
1492   node = parent->GetChild(1);
1493   [bar_ addButtonForNode:node atIndex:-1];
1494   EXPECT_NSEQ(@"2f1b", [[buttons objectAtIndex:0] title]);
1495   EXPECT_NSEQ(@"1b", [[buttons objectAtIndex:1] title]);
1496   EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:2] title]);
1497   EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:3] title]);
1498   EXPECT_NSEQ(@"2f2b", [[buttons objectAtIndex:4] title]);
1499   EXPECT_EQ(oldDisplayedButtons + 2, [bar_ displayedButtonCount]);
1501   // Remove a couple of buttons.
1502   [bar_ removeButton:4 animate:NO];
1503   [bar_ removeButton:1 animate:NO];
1504   EXPECT_NSEQ(@"2f1b", [[buttons objectAtIndex:0] title]);
1505   EXPECT_NSEQ(@"2f", [[buttons objectAtIndex:1] title]);
1506   EXPECT_NSEQ(@"3b", [[buttons objectAtIndex:2] title]);
1507   EXPECT_EQ(oldDisplayedButtons, [bar_ displayedButtonCount]);
1510 TEST_F(BookmarkBarControllerTest, ShrinkOrHideView) {
1511   NSRect viewFrame = NSMakeRect(0.0, 0.0, 500.0, 50.0);
1512   NSView* view = [[[NSView alloc] initWithFrame:viewFrame] autorelease];
1513   EXPECT_FALSE([view isHidden]);
1514   [bar_ shrinkOrHideView:view forMaxX:500.0];
1515   EXPECT_EQ(500.0, NSWidth([view frame]));
1516   EXPECT_FALSE([view isHidden]);
1517   [bar_ shrinkOrHideView:view forMaxX:450.0];
1518   EXPECT_EQ(450.0, NSWidth([view frame]));
1519   EXPECT_FALSE([view isHidden]);
1520   [bar_ shrinkOrHideView:view forMaxX:40.0];
1521   EXPECT_EQ(40.0, NSWidth([view frame]));
1522   EXPECT_FALSE([view isHidden]);
1523   [bar_ shrinkOrHideView:view forMaxX:31.0];
1524   EXPECT_EQ(31.0, NSWidth([view frame]));
1525   EXPECT_FALSE([view isHidden]);
1526   [bar_ shrinkOrHideView:view forMaxX:29.0];
1527   EXPECT_TRUE([view isHidden]);
1530 TEST_F(BookmarkBarControllerTest, LastBookmarkResizeBehavior) {
1531   // Hide the apps shortcut.
1532   profile()->GetPrefs()->SetBoolean(prefs::kShowAppsShortcutInBookmarkBar,
1533                                     false);
1534   ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
1536   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1537   const BookmarkNode* root = model->bookmark_bar_node();
1538   const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
1539   test::AddNodesFromModelString(model, root, model_string);
1540   [bar_ frameDidChange];
1542   CGFloat viewWidths[] = { 123.0, 124.0, 151.0, 152.0, 153.0, 154.0, 155.0,
1543                            200.0, 155.0, 154.0, 153.0, 152.0, 151.0, 124.0,
1544                            123.0 };
1545   BOOL offTheSideButtonIsHiddenResults[] = { NO, NO, NO, NO, YES, YES, YES, YES,
1546                                              YES, YES, YES, NO, NO, NO, NO};
1547   int displayedButtonCountResults[] = { 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 2,
1548                                         2, 1 };
1550   for (unsigned int i = 0; i < sizeof(viewWidths) / sizeof(viewWidths[0]);
1551        ++i) {
1552     NSRect frame = [[bar_ view] frame];
1553     frame.size.width = viewWidths[i] + bookmarks::kBookmarkRightMargin;
1554     [[bar_ view] setFrame:frame];
1555     EXPECT_EQ(offTheSideButtonIsHiddenResults[i],
1556               [bar_ offTheSideButtonIsHidden]);
1557     EXPECT_EQ(displayedButtonCountResults[i], [bar_ displayedButtonCount]);
1558   }
1561 TEST_F(BookmarkBarControllerTest, BookmarksWithAppsPageShortcut) {
1562   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1563   const BookmarkNode* root = model->bookmark_bar_node();
1564   const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
1565   test::AddNodesFromModelString(model, root, model_string);
1566   [bar_ frameDidChange];
1568   // Apps page shortcut button should be visible.
1569   ASSERT_FALSE([bar_ appsPageShortcutButtonIsHidden]);
1571   // Bookmarks should be to the right of the Apps page shortcut button.
1572   CGFloat apps_button_right = NSMaxX([[bar_ appsPageShortcutButton] frame]);
1573   CGFloat right = apps_button_right;
1574   NSArray* buttons = [bar_ buttons];
1575   for (size_t i = 0; i < [buttons count]; ++i) {
1576     EXPECT_LE(right, NSMinX([[buttons objectAtIndex:i] frame]));
1577     right = NSMaxX([[buttons objectAtIndex:i] frame]);
1578   }
1580   // Removing the Apps button should move every bookmark to the left.
1581   profile()->GetPrefs()->SetBoolean(prefs::kShowAppsShortcutInBookmarkBar,
1582                                     false);
1583   ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
1584   EXPECT_GT(apps_button_right, NSMinX([[buttons objectAtIndex:0] frame]));
1585   for (size_t i = 1; i < [buttons count]; ++i) {
1586     EXPECT_LE(NSMaxX([[buttons objectAtIndex:i - 1] frame]),
1587               NSMinX([[buttons objectAtIndex:i] frame]));
1588   }
1591 TEST_F(BookmarkBarControllerTest, BookmarksWithoutAppsPageShortcut) {
1592   // The no item containers should be to the right of the Apps button.
1593   ASSERT_FALSE([bar_ appsPageShortcutButtonIsHidden]);
1594   CGFloat apps_button_right = NSMaxX([[bar_ appsPageShortcutButton] frame]);
1595   EXPECT_LE(apps_button_right,
1596             NSMinX([[[bar_ buttonView] noItemTextfield] frame]));
1597   EXPECT_LE(NSMaxX([[[bar_ buttonView] noItemTextfield] frame]),
1598             NSMinX([[[bar_ buttonView] importBookmarksButton] frame]));
1600   // Removing the Apps button should move the no item containers to the left.
1601   profile()->GetPrefs()->SetBoolean(prefs::kShowAppsShortcutInBookmarkBar,
1602                                     false);
1603   ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
1604   EXPECT_GT(apps_button_right,
1605             NSMinX([[[bar_ buttonView] noItemTextfield] frame]));
1606   EXPECT_LE(NSMaxX([[[bar_ buttonView] noItemTextfield] frame]),
1607             NSMinX([[[bar_ buttonView] importBookmarksButton] frame]));
1610 class BookmarkBarControllerOpenAllTest : public BookmarkBarControllerTest {
1611 public:
1612   virtual void SetUp() {
1613     BookmarkBarControllerTest::SetUp();
1614     ASSERT_TRUE(profile());
1616     resizeDelegate_.reset([[ViewResizerPong alloc] init]);
1617     NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
1618     bar_.reset(
1619                [[BookmarkBarControllerOpenAllPong alloc]
1620                 initWithBrowser:browser()
1621                    initialWidth:NSWidth(parent_frame)
1622                        delegate:nil
1623                  resizeDelegate:resizeDelegate_.get()]);
1624     [bar_ view];
1625     // Awkwardness to look like we've been installed.
1626     [parent_view_ addSubview:[bar_ view]];
1627     NSRect frame = [[[bar_ view] superview] frame];
1628     frame.origin.y = 100;
1629     [[[bar_ view] superview] setFrame:frame];
1631     BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1632     parent_ = model->bookmark_bar_node();
1633     // { one, { two-one, two-two }, three }
1634     model->AddURL(parent_, parent_->child_count(), ASCIIToUTF16("title"),
1635                   GURL("http://one.com"));
1636     folder_ = model->AddFolder(parent_, parent_->child_count(),
1637                                ASCIIToUTF16("folder"));
1638     model->AddURL(folder_, folder_->child_count(),
1639                   ASCIIToUTF16("title"), GURL("http://two-one.com"));
1640     model->AddURL(folder_, folder_->child_count(),
1641                   ASCIIToUTF16("title"), GURL("http://two-two.com"));
1642     model->AddURL(parent_, parent_->child_count(),
1643                   ASCIIToUTF16("title"), GURL("https://three.com"));
1644   }
1645   const BookmarkNode* parent_;  // Weak
1646   const BookmarkNode* folder_;  // Weak
1649 // Command-click on a folder should open all the bookmarks in it.
1650 TEST_F(BookmarkBarControllerOpenAllTest, CommandClickOnFolder) {
1651   NSButton* first = [[bar_ buttons] objectAtIndex:0];
1652   EXPECT_TRUE(first);
1654   // Create the right kind of event; mock NSApp so [NSApp
1655   // currentEvent] finds it.
1656   NSEvent* commandClick =
1657       cocoa_test_event_utils::MouseEventAtPoint(NSZeroPoint,
1658                                                 NSLeftMouseDown,
1659                                                 NSCommandKeyMask);
1660   id fakeApp = [OCMockObject partialMockForObject:NSApp];
1661   [[[fakeApp stub] andReturn:commandClick] currentEvent];
1662   id oldApp = NSApp;
1663   NSApp = fakeApp;
1664   size_t originalDispositionCount = noOpenBar()->dispositions_.size();
1666   // Click!
1667   [first performClick:first];
1669   size_t dispositionCount = noOpenBar()->dispositions_.size();
1670   EXPECT_EQ(originalDispositionCount+1, dispositionCount);
1671   EXPECT_EQ(noOpenBar()->dispositions_[dispositionCount-1], NEW_BACKGROUND_TAB);
1673   // Replace NSApp
1674   NSApp = oldApp;
1677 class BookmarkBarControllerNotificationTest : public CocoaProfileTest {
1678  public:
1679   virtual void SetUp() {
1680     CocoaProfileTest::SetUp();
1681     ASSERT_TRUE(browser());
1683     resizeDelegate_.reset([[ViewResizerPong alloc] init]);
1684     NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
1685     parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]);
1686     [parent_view_ setHidden:YES];
1687     bar_.reset(
1688       [[BookmarkBarControllerNotificationPong alloc]
1689           initWithBrowser:browser()
1690              initialWidth:NSWidth(parent_frame)
1691                  delegate:nil
1692            resizeDelegate:resizeDelegate_.get()]);
1694     // Force loading of the nib.
1695     [bar_ view];
1696     // Awkwardness to look like we've been installed.
1697     [parent_view_ addSubview:[bar_ view]];
1698     NSRect frame = [[[bar_ view] superview] frame];
1699     frame.origin.y = 100;
1700     [[[bar_ view] superview] setFrame:frame];
1702     // Do not add the bar to a window, yet.
1703   }
1705   base::scoped_nsobject<NSView> parent_view_;
1706   base::scoped_nsobject<ViewResizerPong> resizeDelegate_;
1707   base::scoped_nsobject<BookmarkBarControllerNotificationPong> bar_;
1710 TEST_F(BookmarkBarControllerNotificationTest, DeregistersForNotifications) {
1711   NSWindow* window = [[CocoaTestHelperWindow alloc] init];
1712   [window setReleasedWhenClosed:YES];
1714   // First add the bookmark bar to the temp window, then to another window.
1715   [[window contentView] addSubview:parent_view_];
1716   [[test_window() contentView] addSubview:parent_view_];
1718   // Post a fake windowDidResignKey notification for the temp window and make
1719   // sure the bookmark bar controller wasn't listening.
1720   [[NSNotificationCenter defaultCenter]
1721       postNotificationName:NSWindowDidResignKeyNotification
1722                     object:window];
1723   EXPECT_FALSE([bar_ windowDidResignKeyReceived]);
1725   // Close the temp window and make sure no notification was received.
1726   [window close];
1727   EXPECT_FALSE([bar_ windowWillCloseReceived]);
1731 // TODO(jrg): draggingEntered: and draggingExited: trigger timers so
1732 // they are hard to test.  Factor out "fire timers" into routines
1733 // which can be overridden to fire immediately to make behavior
1734 // confirmable.
1736 // TODO(jrg): add unit test to make sure "Other Bookmarks" responds
1737 // properly to a hover open.
1739 // TODO(viettrungluu): figure out how to test animations.
1741 class BookmarkBarControllerDragDropTest : public BookmarkBarControllerTestBase {
1742  public:
1743   base::scoped_nsobject<BookmarkBarControllerDragData> bar_;
1745   virtual void SetUp() {
1746     BookmarkBarControllerTestBase::SetUp();
1747     ASSERT_TRUE(browser());
1749     bar_.reset(
1750                [[BookmarkBarControllerDragData alloc]
1751                 initWithBrowser:browser()
1752                    initialWidth:NSWidth([parent_view_ frame])
1753                        delegate:nil
1754                  resizeDelegate:resizeDelegate_.get()]);
1755     InstallAndToggleBar(bar_.get());
1756   }
1759 TEST_F(BookmarkBarControllerDragDropTest, DragMoveBarBookmarkToOffTheSide) {
1760   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1761   const BookmarkNode* root = model->bookmark_bar_node();
1762   const std::string model_string("1bWithLongName 2fWithLongName:[ "
1763       "2f1bWithLongName 2f2fWithLongName:[ 2f2f1bWithLongName "
1764       "2f2f2bWithLongName 2f2f3bWithLongName 2f4b ] 2f3bWithLongName ] "
1765       "3bWithLongName 4bWithLongName 5bWithLongName 6bWithLongName "
1766       "7bWithLongName 8bWithLongName 9bWithLongName 10bWithLongName "
1767       "11bWithLongName 12bWithLongName 13b ");
1768   test::AddNodesFromModelString(model, root, model_string);
1770   // Validate initial model.
1771   std::string actualModelString = test::ModelStringFromNode(root);
1772   EXPECT_EQ(model_string, actualModelString);
1774   // Insure that the off-the-side is not showing.
1775   ASSERT_FALSE([bar_ offTheSideButtonIsHidden]);
1777   // Remember how many buttons are showing and are available.
1778   int oldDisplayedButtons = [bar_ displayedButtonCount];
1779   int oldChildCount = root->child_count();
1781   // Pop up the off-the-side menu.
1782   BookmarkButton* otsButton = (BookmarkButton*)[bar_ offTheSideButton];
1783   ASSERT_TRUE(otsButton);
1784   [[otsButton target] performSelector:@selector(openOffTheSideFolderFromButton:)
1785                            withObject:otsButton];
1786   BookmarkBarFolderController* otsController = [bar_ folderController];
1787   EXPECT_TRUE(otsController);
1788   NSWindow* toWindow = [otsController window];
1789   EXPECT_TRUE(toWindow);
1790   BookmarkButton* draggedButton =
1791       [bar_ buttonWithTitleEqualTo:@"3bWithLongName"];
1792   ASSERT_TRUE(draggedButton);
1793   int oldOTSCount = (int)[[otsController buttons] count];
1794   EXPECT_EQ(oldOTSCount, oldChildCount - oldDisplayedButtons);
1795   BookmarkButton* targetButton = [[otsController buttons] objectAtIndex:0];
1796   ASSERT_TRUE(targetButton);
1797   [otsController dragButton:draggedButton
1798                          to:[targetButton center]
1799                        copy:YES];
1800   // There should still be the same number of buttons in the bar
1801   // and off-the-side should have one more.
1802   int newDisplayedButtons = [bar_ displayedButtonCount];
1803   int newChildCount = root->child_count();
1804   int newOTSCount = (int)[[otsController buttons] count];
1805   EXPECT_EQ(oldDisplayedButtons, newDisplayedButtons);
1806   EXPECT_EQ(oldChildCount + 1, newChildCount);
1807   EXPECT_EQ(oldOTSCount + 1, newOTSCount);
1808   EXPECT_EQ(newOTSCount, newChildCount - newDisplayedButtons);
1811 TEST_F(BookmarkBarControllerDragDropTest, DragOffTheSideToOther) {
1812   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1813   const BookmarkNode* root = model->bookmark_bar_node();
1814   const std::string model_string("1bWithLongName 2bWithLongName "
1815       "3bWithLongName 4bWithLongName 5bWithLongName 6bWithLongName "
1816       "7bWithLongName 8bWithLongName 9bWithLongName 10bWithLongName "
1817       "11bWithLongName 12bWithLongName 13bWithLongName 14bWithLongName "
1818       "15bWithLongName 16bWithLongName 17bWithLongName 18bWithLongName "
1819       "19bWithLongName 20bWithLongName ");
1820   test::AddNodesFromModelString(model, root, model_string);
1822   const BookmarkNode* other = model->other_node();
1823   const std::string other_string("1other 2other 3other ");
1824   test::AddNodesFromModelString(model, other, other_string);
1826   // Validate initial model.
1827   std::string actualModelString = test::ModelStringFromNode(root);
1828   EXPECT_EQ(model_string, actualModelString);
1829   std::string actualOtherString = test::ModelStringFromNode(other);
1830   EXPECT_EQ(other_string, actualOtherString);
1832   // Insure that the off-the-side is showing.
1833   ASSERT_FALSE([bar_ offTheSideButtonIsHidden]);
1835   // Remember how many buttons are showing and are available.
1836   int oldDisplayedButtons = [bar_ displayedButtonCount];
1837   int oldRootCount = root->child_count();
1838   int oldOtherCount = other->child_count();
1840   // Pop up the off-the-side menu.
1841   BookmarkButton* otsButton = (BookmarkButton*)[bar_ offTheSideButton];
1842   ASSERT_TRUE(otsButton);
1843   [[otsButton target] performSelector:@selector(openOffTheSideFolderFromButton:)
1844                            withObject:otsButton];
1845   BookmarkBarFolderController* otsController = [bar_ folderController];
1846   EXPECT_TRUE(otsController);
1847   int oldOTSCount = (int)[[otsController buttons] count];
1848   EXPECT_EQ(oldOTSCount, oldRootCount - oldDisplayedButtons);
1850   // Pick an off-the-side button and drag it to the other bookmarks.
1851   BookmarkButton* draggedButton =
1852       [otsController buttonWithTitleEqualTo:@"20bWithLongName"];
1853   ASSERT_TRUE(draggedButton);
1854   BookmarkButton* targetButton = [bar_ otherBookmarksButton];
1855   ASSERT_TRUE(targetButton);
1856   [bar_ dragButton:draggedButton to:[targetButton center] copy:NO];
1858   // There should one less button in the bar, one less in off-the-side,
1859   // and one more in other bookmarks.
1860   int newRootCount = root->child_count();
1861   int newOTSCount = (int)[[otsController buttons] count];
1862   int newOtherCount = other->child_count();
1863   EXPECT_EQ(oldRootCount - 1, newRootCount);
1864   EXPECT_EQ(oldOTSCount - 1, newOTSCount);
1865   EXPECT_EQ(oldOtherCount + 1, newOtherCount);
1868 TEST_F(BookmarkBarControllerDragDropTest, DragBookmarkData) {
1869   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1870   const BookmarkNode* root = model->bookmark_bar_node();
1871   const std::string model_string("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
1872                                   "2f3b ] 3b 4b ");
1873   test::AddNodesFromModelString(model, root, model_string);
1874   const BookmarkNode* other = model->other_node();
1875   const std::string other_string("O1b O2b O3f:[ O3f1b O3f2f ] "
1876                                  "O4f:[ O4f1b O4f2f ] 05b ");
1877   test::AddNodesFromModelString(model, other, other_string);
1879   // Validate initial model.
1880   std::string actual = test::ModelStringFromNode(root);
1881   EXPECT_EQ(model_string, actual);
1882   actual = test::ModelStringFromNode(other);
1883   EXPECT_EQ(other_string, actual);
1885   // Remember the little ones.
1886   int oldChildCount = root->child_count();
1888   BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"3b"];
1889   ASSERT_TRUE(targetButton);
1891   // Gen up some dragging data.
1892   const BookmarkNode* newNode = other->GetChild(2);
1893   [bar_ setDragDataNode:newNode];
1894   base::scoped_nsobject<FakeDragInfo> dragInfo([[FakeDragInfo alloc] init]);
1895   [dragInfo setDropLocation:[targetButton center]];
1896   [bar_ dragBookmarkData:(id<NSDraggingInfo>)dragInfo.get()];
1898   // There should one more button in the bar.
1899   int newChildCount = root->child_count();
1900   EXPECT_EQ(oldChildCount + 1, newChildCount);
1901   // Verify the model.
1902   const std::string expected("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
1903                              "2f3b ] O3f:[ O3f1b O3f2f ] 3b 4b ");
1904   actual = test::ModelStringFromNode(root);
1905   EXPECT_EQ(expected, actual);
1906   oldChildCount = newChildCount;
1908   // Now do it over a folder button.
1909   targetButton = [bar_ buttonWithTitleEqualTo:@"2f"];
1910   ASSERT_TRUE(targetButton);
1911   NSPoint targetPoint = [targetButton center];
1912   newNode = other->GetChild(2);  // Should be O4f.
1913   EXPECT_EQ(newNode->GetTitle(), ASCIIToUTF16("O4f"));
1914   [bar_ setDragDataNode:newNode];
1915   [dragInfo setDropLocation:targetPoint];
1916   [bar_ dragBookmarkData:(id<NSDraggingInfo>)dragInfo.get()];
1918   newChildCount = root->child_count();
1919   EXPECT_EQ(oldChildCount, newChildCount);
1920   // Verify the model.
1921   const std::string expected1("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
1922                               "2f3b O4f:[ O4f1b O4f2f ] ] O3f:[ O3f1b O3f2f ] "
1923                               "3b 4b ");
1924   actual = test::ModelStringFromNode(root);
1925   EXPECT_EQ(expected1, actual);
1928 TEST_F(BookmarkBarControllerDragDropTest, AddURLs) {
1929   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1930   const BookmarkNode* root = model->bookmark_bar_node();
1931   const std::string model_string("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
1932                                  "2f3b ] 3b 4b ");
1933   test::AddNodesFromModelString(model, root, model_string);
1935   // Validate initial model.
1936   std::string actual = test::ModelStringFromNode(root);
1937   EXPECT_EQ(model_string, actual);
1939   // Remember the children.
1940   int oldChildCount = root->child_count();
1942   BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"3b"];
1943   ASSERT_TRUE(targetButton);
1945   NSArray* urls = [NSArray arrayWithObjects: @"http://www.a.com/",
1946                    @"http://www.b.com/", nil];
1947   NSArray* titles = [NSArray arrayWithObjects: @"SiteA", @"SiteB", nil];
1948   [bar_ addURLs:urls withTitles:titles at:[targetButton center]];
1950   // There should two more nodes in the bar.
1951   int newChildCount = root->child_count();
1952   EXPECT_EQ(oldChildCount + 2, newChildCount);
1953   // Verify the model.
1954   const std::string expected("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
1955                              "2f3b ] SiteA SiteB 3b 4b ");
1956   actual = test::ModelStringFromNode(root);
1957   EXPECT_EQ(expected, actual);
1960 TEST_F(BookmarkBarControllerDragDropTest, ControllerForNode) {
1961   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1962   const BookmarkNode* root = model->bookmark_bar_node();
1963   const std::string model_string("1b 2f:[ 2f1b 2f2b ] 3b ");
1964   test::AddNodesFromModelString(model, root, model_string);
1966   // Validate initial model.
1967   std::string actualModelString = test::ModelStringFromNode(root);
1968   EXPECT_EQ(model_string, actualModelString);
1970   // Find the main bar controller.
1971   const void* expectedController = bar_;
1972   const void* actualController = [bar_ controllerForNode:root];
1973   EXPECT_EQ(expectedController, actualController);
1976 TEST_F(BookmarkBarControllerDragDropTest, DropPositionIndicator) {
1977   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
1978   const BookmarkNode* root = model->bookmark_bar_node();
1979   const std::string model_string("1b 2f:[ 2f1b 2f2b 2f3b ] 3b 4b ");
1980   test::AddNodesFromModelString(model, root, model_string);
1982   // Hide the apps shortcut.
1983   profile()->GetPrefs()->SetBoolean(prefs::kShowAppsShortcutInBookmarkBar,
1984                                     false);
1985   ASSERT_TRUE([bar_ appsPageShortcutButtonIsHidden]);
1987   // Validate initial model.
1988   std::string actualModel = test::ModelStringFromNode(root);
1989   EXPECT_EQ(model_string, actualModel);
1991   // Test a series of points starting at the right edge of the bar.
1992   BookmarkButton* targetButton = [bar_ buttonWithTitleEqualTo:@"1b"];
1993   ASSERT_TRUE(targetButton);
1994   NSPoint targetPoint = [targetButton left];
1995   CGFloat leftMarginIndicatorPosition = bookmarks::kBookmarkLeftMargin - 0.5 *
1996                                         bookmarks::kBookmarkHorizontalPadding;
1997   const CGFloat baseOffset = targetPoint.x;
1998   CGFloat expected = leftMarginIndicatorPosition;
1999   CGFloat actual = [bar_ indicatorPosForDragToPoint:targetPoint];
2000   EXPECT_CGFLOAT_EQ(expected, actual);
2001   targetButton = [bar_ buttonWithTitleEqualTo:@"2f"];
2002   actual = [bar_ indicatorPosForDragToPoint:[targetButton right]];
2003   targetButton = [bar_ buttonWithTitleEqualTo:@"3b"];
2004   expected = [targetButton left].x - baseOffset + leftMarginIndicatorPosition;
2005   EXPECT_CGFLOAT_EQ(expected, actual);
2006   targetButton = [bar_ buttonWithTitleEqualTo:@"4b"];
2007   targetPoint = [targetButton right];
2008   targetPoint.x += 100;  // Somewhere off to the right.
2009   CGFloat xDelta = 0.5 * bookmarks::kBookmarkHorizontalPadding;
2010   expected = NSMaxX([targetButton frame]) + xDelta;
2011   actual = [bar_ indicatorPosForDragToPoint:targetPoint];
2012   EXPECT_CGFLOAT_EQ(expected, actual);
2015 TEST_F(BookmarkBarControllerDragDropTest, PulseButton) {
2016   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
2017   const BookmarkNode* root = model->bookmark_bar_node();
2018   GURL gurl("http://www.google.com");
2019   const BookmarkNode* node = model->AddURL(root, root->child_count(),
2020                                            ASCIIToUTF16("title"), gurl);
2022   BookmarkButton* button = [[bar_ buttons] objectAtIndex:0];
2023   EXPECT_FALSE([button isContinuousPulsing]);
2025   NSValue *value = [NSValue valueWithPointer:node];
2026   NSDictionary *dict = [NSDictionary
2027                          dictionaryWithObjectsAndKeys:value,
2028                          bookmark_button::kBookmarkKey,
2029                          [NSNumber numberWithBool:YES],
2030                          bookmark_button::kBookmarkPulseFlagKey,
2031                          nil];
2032   [[NSNotificationCenter defaultCenter]
2033         postNotificationName:bookmark_button::kPulseBookmarkButtonNotification
2034                       object:nil
2035                     userInfo:dict];
2036   EXPECT_TRUE([button isContinuousPulsing]);
2038   dict = [NSDictionary dictionaryWithObjectsAndKeys:value,
2039                        bookmark_button::kBookmarkKey,
2040                        [NSNumber numberWithBool:NO],
2041                        bookmark_button::kBookmarkPulseFlagKey,
2042                        nil];
2043   [[NSNotificationCenter defaultCenter]
2044         postNotificationName:bookmark_button::kPulseBookmarkButtonNotification
2045                       object:nil
2046                     userInfo:dict];
2047   EXPECT_FALSE([button isContinuousPulsing]);
2050 TEST_F(BookmarkBarControllerDragDropTest, DragBookmarkDataToTrash) {
2051   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
2052   const BookmarkNode* root = model->bookmark_bar_node();
2053   const std::string model_string("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
2054                                   "2f3b ] 3b 4b ");
2055   test::AddNodesFromModelString(model, root, model_string);
2057   // Validate initial model.
2058   std::string actual = test::ModelStringFromNode(root);
2059   EXPECT_EQ(model_string, actual);
2061   int oldChildCount = root->child_count();
2063   // Drag a button to the trash.
2064   BookmarkButton* buttonToDelete = [bar_ buttonWithTitleEqualTo:@"3b"];
2065   ASSERT_TRUE(buttonToDelete);
2066   EXPECT_TRUE([bar_ canDragBookmarkButtonToTrash:buttonToDelete]);
2067   [bar_ didDragBookmarkToTrash:buttonToDelete];
2069   // There should be one less button in the bar.
2070   int newChildCount = root->child_count();
2071   EXPECT_EQ(oldChildCount - 1, newChildCount);
2072   // Verify the model.
2073   const std::string expected("1b 2f:[ 2f1b 2f2f:[ 2f2f1b 2f2f2b 2f2f3b ] "
2074                              "2f3b ] 4b ");
2075   actual = test::ModelStringFromNode(root);
2076   EXPECT_EQ(expected, actual);
2078   // Verify that the other bookmark folder can't be deleted.
2079   BookmarkButton *otherButton = [bar_ otherBookmarksButton];
2080   EXPECT_FALSE([bar_ canDragBookmarkButtonToTrash:otherButton]);
2083 }  // namespace