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 // TODO(jrg): use OCMock.
19 // Information needed to open a URL, as passed to the
20 // BookmarkBarController's delegate.
21 typedef std::pair<GURL,WindowOpenDisposition> OpenInfo;
23 } // The namespace must end here -- I need to use OpenInfo in
24 // FakeBookmarkBarController but can't place
25 // FakeBookmarkBarController itself in the namespace ("error:
26 // Objective-C declarations may only appear in global scope")
28 // Oddly, we are our own delegate.
29 @interface FakeBookmarkBarController : BookmarkBarController {
31 base::scoped_nsobject<NSMutableArray> callbacks_;
32 std::vector<OpenInfo> opens_;
36 @implementation FakeBookmarkBarController
38 - (id)initWithBrowser:(Browser*)browser {
39 if ((self = [super initWithBrowser:browser
40 initialWidth:100 // arbitrary
42 resizeDelegate:nil])) {
43 callbacks_.reset([[NSMutableArray alloc] init]);
48 - (void)loaded:(BookmarkModel*)model {
49 [callbacks_ addObject:[NSNumber numberWithInt:0]];
52 - (void)beingDeleted:(BookmarkModel*)model {
53 [callbacks_ addObject:[NSNumber numberWithInt:1]];
56 - (void)nodeMoved:(BookmarkModel*)model
57 oldParent:(const BookmarkNode*)oldParent oldIndex:(int)oldIndex
58 newParent:(const BookmarkNode*)newParent newIndex:(int)newIndex {
59 [callbacks_ addObject:[NSNumber numberWithInt:2]];
62 - (void)nodeAdded:(BookmarkModel*)model
63 parent:(const BookmarkNode*)oldParent index:(int)index {
64 [callbacks_ addObject:[NSNumber numberWithInt:3]];
67 - (void)nodeChanged:(BookmarkModel*)model
68 node:(const BookmarkNode*)node {
69 [callbacks_ addObject:[NSNumber numberWithInt:4]];
72 - (void)nodeFaviconLoaded:(BookmarkModel*)model
73 node:(const BookmarkNode*)node {
74 [callbacks_ addObject:[NSNumber numberWithInt:5]];
77 - (void)nodeChildrenReordered:(BookmarkModel*)model
78 node:(const BookmarkNode*)node {
79 [callbacks_ addObject:[NSNumber numberWithInt:6]];
82 - (void)nodeRemoved:(BookmarkModel*)model
83 parent:(const BookmarkNode*)oldParent index:(int)index {
84 [callbacks_ addObject:[NSNumber numberWithInt:7]];
88 - (void)openBookmarkURL:(const GURL&)url
89 disposition:(WindowOpenDisposition)disposition {
90 opens_.push_back(OpenInfo(url, disposition));
96 class BookmarkBarBridgeTest : public CocoaProfileTest {
99 // Call all the callbacks; make sure they are all redirected to the objc object.
100 TEST_F(BookmarkBarBridgeTest, TestRedirect) {
101 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
103 base::scoped_nsobject<NSView> parentView(
104 [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]);
105 base::scoped_nsobject<NSView> webView(
106 [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]);
107 base::scoped_nsobject<NSView> infoBarsView(
108 [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]);
110 base::scoped_nsobject<FakeBookmarkBarController> controller(
111 [[FakeBookmarkBarController alloc] initWithBrowser:browser()]);
112 EXPECT_TRUE(controller.get());
113 scoped_ptr<BookmarkBarBridge> bridge(new BookmarkBarBridge(profile(),
116 EXPECT_TRUE(bridge.get());
118 bridge->BookmarkModelLoaded(NULL, false);
119 bridge->BookmarkModelBeingDeleted(NULL);
120 bridge->BookmarkNodeMoved(NULL, NULL, 0, NULL, 0);
121 bridge->BookmarkNodeAdded(NULL, NULL, 0);
122 bridge->BookmarkNodeChanged(NULL, NULL);
123 bridge->BookmarkNodeFaviconChanged(NULL, NULL);
124 bridge->BookmarkNodeChildrenReordered(NULL, NULL);
125 bridge->BookmarkNodeRemoved(NULL, NULL, 0, NULL);
127 // 8 calls above plus an initial Loaded() in init routine makes 9
128 EXPECT_TRUE([controller.get()->callbacks_ count] == 9);
130 for (int x = 1; x < 9; x++) {
131 NSNumber* num = [NSNumber numberWithInt:x-1];
132 EXPECT_NSEQ(num, [controller.get()->callbacks_ objectAtIndex:x]);