Home | History | Annotate | Download | only in bookmarks
      1 // Copyright (c) 2010 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/ui/cocoa/bookmarks/bookmark_bar_bridge.h"
      6 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
      7 #include "chrome/browser/ui/cocoa/browser_test_helper.h"
      8 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #import "testing/gtest_mac.h"
     11 #include "testing/platform_test.h"
     12 
     13 // TODO(jrg): use OCMock.
     14 
     15 namespace {
     16 
     17 // Information needed to open a URL, as passed to the
     18 // BookmarkBarController's delegate.
     19 typedef std::pair<GURL,WindowOpenDisposition> OpenInfo;
     20 
     21 }  // The namespace must end here -- I need to use OpenInfo in
     22    // FakeBookmarkBarController but can't place
     23    // FakeBookmarkBarController itself in the namespace ("error:
     24    // Objective-C declarations may only appear in global scope")
     25 
     26 // Oddly, we are our own delegate.
     27 @interface FakeBookmarkBarController : BookmarkBarController {
     28  @public
     29   scoped_nsobject<NSMutableArray> callbacks_;
     30   std::vector<OpenInfo> opens_;
     31 }
     32 @end
     33 
     34 @implementation FakeBookmarkBarController
     35 
     36 - (id)initWithBrowser:(Browser*)browser {
     37   if ((self = [super initWithBrowser:browser
     38                         initialWidth:100  // arbitrary
     39                             delegate:nil
     40                       resizeDelegate:nil])) {
     41     callbacks_.reset([[NSMutableArray alloc] init]);
     42   }
     43   return self;
     44 }
     45 
     46 - (void)loaded:(BookmarkModel*)model {
     47   [callbacks_ addObject:[NSNumber numberWithInt:0]];
     48 }
     49 
     50 - (void)beingDeleted:(BookmarkModel*)model {
     51   [callbacks_ addObject:[NSNumber numberWithInt:1]];
     52 }
     53 
     54 - (void)nodeMoved:(BookmarkModel*)model
     55         oldParent:(const BookmarkNode*)oldParent oldIndex:(int)oldIndex
     56         newParent:(const BookmarkNode*)newParent newIndex:(int)newIndex {
     57   [callbacks_ addObject:[NSNumber numberWithInt:2]];
     58 }
     59 
     60 - (void)nodeAdded:(BookmarkModel*)model
     61            parent:(const BookmarkNode*)oldParent index:(int)index {
     62   [callbacks_ addObject:[NSNumber numberWithInt:3]];
     63 }
     64 
     65 - (void)nodeChanged:(BookmarkModel*)model
     66                node:(const BookmarkNode*)node {
     67   [callbacks_ addObject:[NSNumber numberWithInt:4]];
     68 }
     69 
     70 - (void)nodeFaviconLoaded:(BookmarkModel*)model
     71                      node:(const BookmarkNode*)node {
     72   [callbacks_ addObject:[NSNumber numberWithInt:5]];
     73 }
     74 
     75 - (void)nodeChildrenReordered:(BookmarkModel*)model
     76                          node:(const BookmarkNode*)node {
     77   [callbacks_ addObject:[NSNumber numberWithInt:6]];
     78 }
     79 
     80 - (void)nodeRemoved:(BookmarkModel*)model
     81              parent:(const BookmarkNode*)oldParent index:(int)index {
     82   [callbacks_ addObject:[NSNumber numberWithInt:7]];
     83 }
     84 
     85 // Save the request.
     86 - (void)openBookmarkURL:(const GURL&)url
     87             disposition:(WindowOpenDisposition)disposition {
     88   opens_.push_back(OpenInfo(url, disposition));
     89 }
     90 
     91 @end
     92 
     93 
     94 class BookmarkBarBridgeTest : public CocoaTest {
     95  public:
     96   BrowserTestHelper browser_test_helper_;
     97 };
     98 
     99 // Call all the callbacks; make sure they are all redirected to the objc object.
    100 TEST_F(BookmarkBarBridgeTest, TestRedirect) {
    101   Browser* browser = browser_test_helper_.browser();
    102   Profile* profile = browser_test_helper_.profile();
    103   BookmarkModel* model = profile->GetBookmarkModel();
    104 
    105   scoped_nsobject<NSView> parentView([[NSView alloc]
    106                                        initWithFrame:NSMakeRect(0,0,100,100)]);
    107   scoped_nsobject<NSView> webView([[NSView alloc]
    108                                        initWithFrame:NSMakeRect(0,0,100,100)]);
    109   scoped_nsobject<NSView> infoBarsView(
    110       [[NSView alloc] initWithFrame:NSMakeRect(0,0,100,100)]);
    111 
    112   scoped_nsobject<FakeBookmarkBarController>
    113       controller([[FakeBookmarkBarController alloc] initWithBrowser:browser]);
    114   EXPECT_TRUE(controller.get());
    115   scoped_ptr<BookmarkBarBridge> bridge(new BookmarkBarBridge(controller.get(),
    116                                                              model));
    117   EXPECT_TRUE(bridge.get());
    118 
    119   bridge->Loaded(NULL);
    120   bridge->BookmarkModelBeingDeleted(NULL);
    121   bridge->BookmarkNodeMoved(NULL, NULL, 0, NULL, 0);
    122   bridge->BookmarkNodeAdded(NULL, NULL, 0);
    123   bridge->BookmarkNodeChanged(NULL, NULL);
    124   bridge->BookmarkNodeFaviconLoaded(NULL, NULL);
    125   bridge->BookmarkNodeChildrenReordered(NULL, NULL);
    126   bridge->BookmarkNodeRemoved(NULL, NULL, 0, NULL);
    127 
    128   // 8 calls above plus an initial Loaded() in init routine makes 9
    129   EXPECT_TRUE([controller.get()->callbacks_ count] == 9);
    130 
    131   for (int x = 1; x < 9; x++) {
    132     NSNumber* num = [NSNumber numberWithInt:x-1];
    133     EXPECT_NSEQ(num, [controller.get()->callbacks_ objectAtIndex:x]);
    134   }
    135 }
    136