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 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
6 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_bridge.h"
7 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
8 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #import "testing/gtest_mac.h"
12 #include "testing/platform_test.h"
15 using bookmarks::BookmarkModel;
16 using bookmarks::BookmarkNode;
18 // TODO(jrg): use OCMock.
22 // Information needed to open a URL, as passed to the
23 // BookmarkBarController's delegate.
24 typedef std::pair<GURL,WindowOpenDisposition> OpenInfo;
26 } // The namespace must end here -- I need to use OpenInfo in
27 // FakeBookmarkBarController but can't place
28 // FakeBookmarkBarController itself in the namespace ("error:
29 // Objective-C declarations may only appear in global scope")
31 // Oddly, we are our own delegate.
32 @interface FakeBookmarkBarController : BookmarkBarController {
34 base::scoped_nsobject<NSMutableArray> callbacks_;
35 std::vector<OpenInfo> opens_;
39 @implementation FakeBookmarkBarController
41 - (id)initWithBrowser:(Browser*)browser {
42 if ((self = [super initWithBrowser:browser
43 initialWidth:100 // arbitrary
45 resizeDelegate:nil])) {
46 callbacks_.reset([[NSMutableArray alloc] init]);
51 - (void)loaded:(BookmarkModel*)model {
52 [callbacks_ addObject:[NSNumber numberWithInt:0]];
55 - (void)beingDeleted:(BookmarkModel*)model {
56 [callbacks_ addObject:[NSNumber numberWithInt:1]];
59 - (void)nodeMoved:(BookmarkModel*)model
60 oldParent:(const BookmarkNode*)oldParent oldIndex:(int)oldIndex
61 newParent:(const BookmarkNode*)newParent newIndex:(int)newIndex {
62 [callbacks_ addObject:[NSNumber numberWithInt:2]];
65 - (void)nodeAdded:(BookmarkModel*)model
66 parent:(const BookmarkNode*)oldParent index:(int)index {
67 [callbacks_ addObject:[NSNumber numberWithInt:3]];
70 - (void)nodeChanged:(BookmarkModel*)model
71 node:(const BookmarkNode*)node {
72 [callbacks_ addObject:[NSNumber numberWithInt:4]];
75 - (void)nodeFaviconLoaded:(BookmarkModel*)model
76 node:(const BookmarkNode*)node {
77 [callbacks_ addObject:[NSNumber numberWithInt:5]];
80 - (void)nodeChildrenReordered:(BookmarkModel*)model
81 node:(const BookmarkNode*)node {
82 [callbacks_ addObject:[NSNumber numberWithInt:6]];
85 - (void)nodeRemoved:(BookmarkModel*)model
86 parent:(const BookmarkNode*)oldParent index:(int)index {
87 [callbacks_ addObject:[NSNumber numberWithInt:7]];
91 - (void)openBookmarkURL:(const GURL&)url
92 disposition:(WindowOpenDisposition)disposition {
93 opens_.push_back(OpenInfo(url, disposition));
99 class BookmarkBarBridgeTest : public CocoaProfileTest {
102 // Call all the callbacks; make sure they are all redirected to the objc object.
103 TEST_F(BookmarkBarBridgeTest, TestRedirect) {
104 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
106 base::scoped_nsobject<NSView> parentView(
107 [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]);
108 base::scoped_nsobject<NSView> webView(
109 [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]);
110 base::scoped_nsobject<NSView> infoBarsView(
111 [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]);
113 base::scoped_nsobject<FakeBookmarkBarController> controller(
114 [[FakeBookmarkBarController alloc] initWithBrowser:browser()]);
115 EXPECT_TRUE(controller.get());
116 scoped_ptr<BookmarkBarBridge> bridge(new BookmarkBarBridge(profile(),
119 EXPECT_TRUE(bridge.get());
121 bridge->BookmarkModelLoaded(NULL, false);
122 bridge->BookmarkModelBeingDeleted(NULL);
123 bridge->BookmarkNodeMoved(NULL, NULL, 0, NULL, 0);
124 bridge->BookmarkNodeAdded(NULL, NULL, 0);
125 bridge->BookmarkNodeChanged(NULL, NULL);
126 bridge->BookmarkNodeFaviconChanged(NULL, NULL);
127 bridge->BookmarkNodeChildrenReordered(NULL, NULL);
128 bridge->BookmarkNodeRemoved(NULL, NULL, 0, NULL, std::set<GURL>());
130 // 8 calls above plus an initial Loaded() in init routine makes 9
131 EXPECT_TRUE([controller.get()->callbacks_ count] == 9);
133 for (int x = 1; x < 9; x++) {
134 NSNumber* num = [NSNumber numberWithInt:x-1];
135 EXPECT_NSEQ(num, [controller.get()->callbacks_ objectAtIndex:x]);