- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_button_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 "base/mac/scoped_nsobject.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/bookmarks/bookmark_model.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h"
11 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
15 #import "ui/base/test/cocoa_test_event_utils.h"
16
17 // Fake BookmarkButton delegate to get a pong on mouse entered/exited
18 @interface FakeButtonDelegate : NSObject<BookmarkButtonDelegate> {
19  @public
20   int entered_;
21   int exited_;
22   BOOL canDragToTrash_;
23   int didDragToTrashCount_;
24 }
25 @end
26
27 @implementation FakeButtonDelegate
28
29 - (void)fillPasteboard:(NSPasteboard*)pboard
30        forDragOfButton:(BookmarkButton*)button {
31 }
32
33 - (void)mouseEnteredButton:(id)buton event:(NSEvent*)event {
34   entered_++;
35 }
36
37 - (void)mouseExitedButton:(id)buton event:(NSEvent*)event {
38   exited_++;
39 }
40
41 - (BOOL)dragShouldLockBarVisibility {
42   return NO;
43 }
44
45 - (NSWindow*)browserWindow {
46   return nil;
47 }
48
49 - (BOOL)canDragBookmarkButtonToTrash:(BookmarkButton*)button {
50   return canDragToTrash_;
51 }
52
53 - (void)didDragBookmarkToTrash:(BookmarkButton*)button {
54   didDragToTrashCount_++;
55 }
56
57 - (void)bookmarkDragDidEnd:(BookmarkButton*)button
58                  operation:(NSDragOperation)operation {
59 }
60 @end
61
62 namespace {
63
64 class BookmarkButtonTest : public CocoaProfileTest {
65 };
66
67 // Make sure nothing leaks
68 TEST_F(BookmarkButtonTest, Create) {
69   base::scoped_nsobject<BookmarkButton> button;
70   button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
71 }
72
73 // Test folder and empty node queries.
74 TEST_F(BookmarkButtonTest, FolderAndEmptyOrNot) {
75   base::scoped_nsobject<BookmarkButton> button;
76   base::scoped_nsobject<BookmarkButtonCell> cell;
77
78   button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
79   cell.reset([[BookmarkButtonCell alloc] initTextCell:@"hi mom"]);
80   [button setCell:cell];
81
82   EXPECT_TRUE([button isEmpty]);
83   EXPECT_FALSE([button isFolder]);
84   EXPECT_FALSE([button bookmarkNode]);
85
86   NSEvent* downEvent =
87       cocoa_test_event_utils::LeftMouseDownAtPoint(NSMakePoint(10,10));
88   // Since this returns (does not actually begin a modal drag), success!
89   [button beginDrag:downEvent];
90
91   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
92   const BookmarkNode* node = model->bookmark_bar_node();
93   [cell setBookmarkNode:node];
94   EXPECT_FALSE([button isEmpty]);
95   EXPECT_TRUE([button isFolder]);
96   EXPECT_EQ([button bookmarkNode], node);
97
98   node = model->AddURL(node, 0, ASCIIToUTF16("hi mom"),
99                        GURL("http://www.google.com"));
100   [cell setBookmarkNode:node];
101   EXPECT_FALSE([button isEmpty]);
102   EXPECT_FALSE([button isFolder]);
103   EXPECT_EQ([button bookmarkNode], node);
104 }
105
106 TEST_F(BookmarkButtonTest, MouseEnterExitRedirect) {
107   NSEvent* moveEvent =
108       cocoa_test_event_utils::MouseEventAtPoint(NSMakePoint(10,10),
109                                                 NSMouseMoved,
110                                                 0);
111   base::scoped_nsobject<BookmarkButton> button;
112   base::scoped_nsobject<BookmarkButtonCell> cell;
113   base::scoped_nsobject<FakeButtonDelegate> delegate(
114       [[FakeButtonDelegate alloc] init]);
115   button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
116   cell.reset([[BookmarkButtonCell alloc] initTextCell:@"hi mom"]);
117   [button setCell:cell];
118   [button setDelegate:delegate];
119
120   EXPECT_EQ(0, delegate.get()->entered_);
121   EXPECT_EQ(0, delegate.get()->exited_);
122
123   [button mouseEntered:moveEvent];
124   EXPECT_EQ(1, delegate.get()->entered_);
125   EXPECT_EQ(0, delegate.get()->exited_);
126
127   [button mouseExited:moveEvent];
128   [button mouseExited:moveEvent];
129   EXPECT_EQ(1, delegate.get()->entered_);
130   EXPECT_EQ(2, delegate.get()->exited_);
131 }
132
133 TEST_F(BookmarkButtonTest, DragToTrash) {
134   base::scoped_nsobject<BookmarkButton> button;
135   base::scoped_nsobject<BookmarkButtonCell> cell;
136   base::scoped_nsobject<FakeButtonDelegate> delegate(
137       [[FakeButtonDelegate alloc] init]);
138   button.reset([[BookmarkButton alloc] initWithFrame:NSMakeRect(0,0,500,500)]);
139   cell.reset([[BookmarkButtonCell alloc] initTextCell:@"hi mom"]);
140   [button setCell:cell];
141   [button setDelegate:delegate];
142
143   // Add a deletable bookmark to the button.
144   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile());
145   const BookmarkNode* barNode = model->bookmark_bar_node();
146   const BookmarkNode* node = model->AddURL(barNode, 0, ASCIIToUTF16("hi mom"),
147                                            GURL("http://www.google.com"));
148   [cell setBookmarkNode:node];
149
150   // Verify that if canDragBookmarkButtonToTrash is NO then the button can't
151   // be dragged to the trash.
152   delegate.get()->canDragToTrash_ = NO;
153   NSDragOperation operation = [button draggingSourceOperationMaskForLocal:NO];
154   EXPECT_EQ(0u, operation & NSDragOperationDelete);
155   operation = [button draggingSourceOperationMaskForLocal:YES];
156   EXPECT_EQ(0u, operation & NSDragOperationDelete);
157
158   // Verify that if canDragBookmarkButtonToTrash is YES then the button can
159   // be dragged to the trash.
160   delegate.get()->canDragToTrash_ = YES;
161   operation = [button draggingSourceOperationMaskForLocal:NO];
162   EXPECT_EQ(NSDragOperationDelete, operation & NSDragOperationDelete);
163   operation = [button draggingSourceOperationMaskForLocal:YES];
164   EXPECT_EQ(NSDragOperationDelete, operation & NSDragOperationDelete);
165
166   // Verify that canDragBookmarkButtonToTrash is called when expected.
167   delegate.get()->canDragToTrash_ = YES;
168   EXPECT_EQ(0, delegate.get()->didDragToTrashCount_);
169   [button draggedImage:nil endedAt:NSZeroPoint operation:NSDragOperationCopy];
170   EXPECT_EQ(0, delegate.get()->didDragToTrashCount_);
171   [button draggedImage:nil endedAt:NSZeroPoint operation:NSDragOperationMove];
172   EXPECT_EQ(0, delegate.get()->didDragToTrashCount_);
173   [button draggedImage:nil endedAt:NSZeroPoint operation:NSDragOperationDelete];
174   EXPECT_EQ(1, delegate.get()->didDragToTrashCount_);
175 }
176
177 }