Home | History | Annotate | Download | only in bookmarks
      1 // Copyright (c) 2011 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 "base/memory/scoped_nsobject.h"
      6 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h"
      7 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_view.h"
      8 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
      9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h"
     10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     11 #import "chrome/browser/ui/cocoa/url_drop_target.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 #include "testing/platform_test.h"
     14 #import "third_party/mozilla/NSPasteboard+Utils.h"
     15 
     16 namespace {
     17   const CGFloat kFakeIndicatorPos = 7.0;
     18 };
     19 
     20 // Fake DraggingInfo, fake BookmarkBarController, fake NSPasteboard...
     21 @interface FakeBookmarkDraggingInfo : NSObject {
     22  @public
     23   BOOL dragButtonToPong_;
     24   BOOL dragURLsPong_;
     25   BOOL dragBookmarkDataPong_;
     26   BOOL dropIndicatorShown_;
     27   BOOL draggingEnteredCalled_;
     28   // Only mock one type of drag data at a time.
     29   NSString* dragDataType_;
     30 }
     31 @property (nonatomic) BOOL dropIndicatorShown;
     32 @property (nonatomic) BOOL draggingEnteredCalled;
     33 @property (nonatomic, copy) NSString* dragDataType;
     34 @end
     35 
     36 @implementation FakeBookmarkDraggingInfo
     37 
     38 @synthesize dropIndicatorShown = dropIndicatorShown_;
     39 @synthesize draggingEnteredCalled = draggingEnteredCalled_;
     40 @synthesize dragDataType = dragDataType_;
     41 
     42 - (id)init {
     43   if ((self = [super init])) {
     44     dropIndicatorShown_ = YES;
     45   }
     46   return self;
     47 }
     48 
     49 - (void)dealloc {
     50   [dragDataType_ release];
     51   [super dealloc];
     52 }
     53 
     54 - (void)reset {
     55   [dragDataType_ release];
     56   dragDataType_ = nil;
     57   dragButtonToPong_ = NO;
     58   dragURLsPong_ = NO;
     59   dragBookmarkDataPong_ = NO;
     60   dropIndicatorShown_ = YES;
     61   draggingEnteredCalled_ = NO;
     62 }
     63 
     64 // NSDragInfo mocking functions.
     65 
     66 - (id)draggingPasteboard {
     67   return self;
     68 }
     69 
     70 // So we can look local.
     71 - (id)draggingSource {
     72   return self;
     73 }
     74 
     75 - (NSDragOperation)draggingSourceOperationMask {
     76   return NSDragOperationCopy | NSDragOperationMove;
     77 }
     78 
     79 - (NSPoint)draggingLocation {
     80   return NSMakePoint(10, 10);
     81 }
     82 
     83 // NSPasteboard mocking functions.
     84 
     85 - (BOOL)containsURLData {
     86   NSArray* urlTypes = [URLDropTargetHandler handledDragTypes];
     87   if (dragDataType_)
     88     return [urlTypes containsObject:dragDataType_];
     89   return NO;
     90 }
     91 
     92 - (NSData*)dataForType:(NSString*)type {
     93   if (dragDataType_ && [dragDataType_ isEqualToString:type])
     94     return [NSData data];  // Return something, anything.
     95   return nil;
     96 }
     97 
     98 // Fake a controller for callback ponging
     99 
    100 - (BOOL)dragButton:(BookmarkButton*)button to:(NSPoint)point copy:(BOOL)copy {
    101   dragButtonToPong_ = YES;
    102   return YES;
    103 }
    104 
    105 - (BOOL)addURLs:(NSArray*)urls withTitles:(NSArray*)titles at:(NSPoint)point {
    106   dragURLsPong_ = YES;
    107   return YES;
    108 }
    109 
    110 - (void)getURLs:(NSArray**)outUrls
    111     andTitles:(NSArray**)outTitles
    112     convertingFilenames:(BOOL)convertFilenames {
    113 }
    114 
    115 - (BOOL)dragBookmarkData:(id<NSDraggingInfo>)info {
    116   dragBookmarkDataPong_ = YES;
    117   return NO;
    118 }
    119 
    120 - (BOOL)canEditBookmarks {
    121   return YES;
    122 }
    123 
    124 // Confirm the pongs.
    125 
    126 - (BOOL)dragButtonToPong {
    127   return dragButtonToPong_;
    128 }
    129 
    130 - (BOOL)dragURLsPong {
    131   return dragURLsPong_;
    132 }
    133 
    134 - (BOOL)dragBookmarkDataPong {
    135   return dragBookmarkDataPong_;
    136 }
    137 
    138 - (CGFloat)indicatorPosForDragToPoint:(NSPoint)point {
    139   return kFakeIndicatorPos;
    140 }
    141 
    142 - (BOOL)shouldShowIndicatorShownForPoint:(NSPoint)point {
    143   return dropIndicatorShown_;
    144 }
    145 
    146 - (BOOL)draggingAllowed:(id<NSDraggingInfo>)info {
    147   return YES;
    148 }
    149 
    150 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info {
    151   draggingEnteredCalled_ = YES;
    152   return NSDragOperationNone;
    153 }
    154 
    155 - (void)setDropInsertionPos:(CGFloat)where {
    156 }
    157 
    158 - (void)clearDropInsertionPos {
    159 }
    160 
    161 @end
    162 
    163 namespace {
    164 
    165 class BookmarkBarViewTest : public CocoaTest {
    166  public:
    167   virtual void SetUp() {
    168     CocoaTest::SetUp();
    169     view_.reset([[BookmarkBarView alloc] init]);
    170   }
    171 
    172   scoped_nsobject<BookmarkBarView> view_;
    173 };
    174 
    175 TEST_F(BookmarkBarViewTest, CanDragWindow) {
    176   EXPECT_FALSE([view_ mouseDownCanMoveWindow]);
    177 }
    178 
    179 TEST_F(BookmarkBarViewTest, BookmarkButtonDragAndDrop) {
    180   scoped_nsobject<FakeBookmarkDraggingInfo>
    181       info([[FakeBookmarkDraggingInfo alloc] init]);
    182   [view_ setController:info.get()];
    183   [info reset];
    184 
    185   [info setDragDataType:kBookmarkButtonDragType];
    186   EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
    187   EXPECT_TRUE([view_ performDragOperation:(id)info.get()]);
    188   EXPECT_TRUE([info dragButtonToPong]);
    189   EXPECT_FALSE([info dragURLsPong]);
    190   EXPECT_TRUE([info dragBookmarkDataPong]);
    191 }
    192 
    193 TEST_F(BookmarkBarViewTest, URLDragAndDrop) {
    194   scoped_nsobject<FakeBookmarkDraggingInfo>
    195       info([[FakeBookmarkDraggingInfo alloc] init]);
    196   [view_ setController:info.get()];
    197   [info reset];
    198 
    199   NSArray* dragTypes = [URLDropTargetHandler handledDragTypes];
    200   for (NSString* type in dragTypes) {
    201     [info setDragDataType:type];
    202     EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
    203     EXPECT_TRUE([view_ performDragOperation:(id)info.get()]);
    204     EXPECT_FALSE([info dragButtonToPong]);
    205     EXPECT_TRUE([info dragURLsPong]);
    206     EXPECT_TRUE([info dragBookmarkDataPong]);
    207     [info reset];
    208   }
    209 }
    210 
    211 TEST_F(BookmarkBarViewTest, BookmarkButtonDropIndicator) {
    212   scoped_nsobject<FakeBookmarkDraggingInfo>
    213       info([[FakeBookmarkDraggingInfo alloc] init]);
    214   [view_ setController:info.get()];
    215 
    216   [info reset];
    217   [info setDragDataType:kBookmarkButtonDragType];
    218   EXPECT_FALSE([info draggingEnteredCalled]);
    219   EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
    220   EXPECT_TRUE([info draggingEnteredCalled]);  // Ensure controller pinged.
    221   EXPECT_TRUE([view_ dropIndicatorShown]);
    222   EXPECT_EQ([view_ dropIndicatorPosition], kFakeIndicatorPos);
    223 
    224   [info setDropIndicatorShown:NO];
    225   EXPECT_EQ([view_ draggingEntered:(id)info.get()], NSDragOperationMove);
    226   EXPECT_FALSE([view_ dropIndicatorShown]);
    227 }
    228 
    229 }  // namespace
    230