- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_bar_bridge_unittest.mm
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.
4
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"
13 #include "url/gurl.h"
14
15 // TODO(jrg): use OCMock.
16
17 namespace {
18
19 // Information needed to open a URL, as passed to the
20 // BookmarkBarController's delegate.
21 typedef std::pair<GURL,WindowOpenDisposition> OpenInfo;
22
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")
27
28 // Oddly, we are our own delegate.
29 @interface FakeBookmarkBarController : BookmarkBarController {
30  @public
31   base::scoped_nsobject<NSMutableArray> callbacks_;
32   std::vector<OpenInfo> opens_;
33 }
34 @end
35
36 @implementation FakeBookmarkBarController
37
38 - (id)initWithBrowser:(Browser*)browser {
39   if ((self = [super initWithBrowser:browser
40                         initialWidth:100  // arbitrary
41                             delegate:nil
42                       resizeDelegate:nil])) {
43     callbacks_.reset([[NSMutableArray alloc] init]);
44   }
45   return self;
46 }
47
48 - (void)loaded:(BookmarkModel*)model {
49   [callbacks_ addObject:[NSNumber numberWithInt:0]];
50 }
51
52 - (void)beingDeleted:(BookmarkModel*)model {
53   [callbacks_ addObject:[NSNumber numberWithInt:1]];
54 }
55
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]];
60 }
61
62 - (void)nodeAdded:(BookmarkModel*)model
63            parent:(const BookmarkNode*)oldParent index:(int)index {
64   [callbacks_ addObject:[NSNumber numberWithInt:3]];
65 }
66
67 - (void)nodeChanged:(BookmarkModel*)model
68                node:(const BookmarkNode*)node {
69   [callbacks_ addObject:[NSNumber numberWithInt:4]];
70 }
71
72 - (void)nodeFaviconLoaded:(BookmarkModel*)model
73                      node:(const BookmarkNode*)node {
74   [callbacks_ addObject:[NSNumber numberWithInt:5]];
75 }
76
77 - (void)nodeChildrenReordered:(BookmarkModel*)model
78                          node:(const BookmarkNode*)node {
79   [callbacks_ addObject:[NSNumber numberWithInt:6]];
80 }
81
82 - (void)nodeRemoved:(BookmarkModel*)model
83              parent:(const BookmarkNode*)oldParent index:(int)index {
84   [callbacks_ addObject:[NSNumber numberWithInt:7]];
85 }
86
87 // Save the request.
88 - (void)openBookmarkURL:(const GURL&)url
89             disposition:(WindowOpenDisposition)disposition {
90   opens_.push_back(OpenInfo(url, disposition));
91 }
92
93 @end
94
95
96 class BookmarkBarBridgeTest : public CocoaProfileTest {
97 };
98
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());
102
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)]);
109
110   base::scoped_nsobject<FakeBookmarkBarController> controller(
111       [[FakeBookmarkBarController alloc] initWithBrowser:browser()]);
112   EXPECT_TRUE(controller.get());
113   scoped_ptr<BookmarkBarBridge> bridge(new BookmarkBarBridge(profile(),
114                                                              controller.get(),
115                                                              model));
116   EXPECT_TRUE(bridge.get());
117
118   bridge->Loaded(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);
126
127   // 8 calls above plus an initial Loaded() in init routine makes 9
128   EXPECT_TRUE([controller.get()->callbacks_ count] == 9);
129
130   for (int x = 1; x < 9; x++) {
131     NSNumber* num = [NSNumber numberWithInt:x-1];
132     EXPECT_NSEQ(num, [controller.get()->callbacks_ objectAtIndex:x]);
133   }
134 }