OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
12 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h" | 12 #include "chrome/browser/bookmarks/managed_bookmark_service_factory.h" |
13 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "chrome/browser/ui/bookmarks/bookmark_bubble_observer.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
16 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bubble_controller.h" | 17 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bubble_controller.h" |
17 #include "chrome/browser/ui/cocoa/browser_window_controller.h" | 18 #include "chrome/browser/ui/cocoa/browser_window_controller.h" |
18 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 19 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
19 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | 20 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
20 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
21 #include "components/bookmarks/browser/bookmark_model.h" | 22 #include "components/bookmarks/browser/bookmark_model.h" |
22 #include "components/bookmarks/managed/managed_bookmark_service.h" | 23 #include "components/bookmarks/managed/managed_bookmark_service.h" |
23 #include "components/signin/core/browser/signin_manager.h" | 24 #include "components/signin/core/browser/signin_manager.h" |
24 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 #import "testing/gtest_mac.h" | 27 #import "testing/gtest_mac.h" |
27 #include "testing/platform_test.h" | 28 #include "testing/platform_test.h" |
28 | 29 |
29 using base::ASCIIToUTF16; | 30 using base::ASCIIToUTF16; |
| 31 using bookmarks::BookmarkBubbleObserver; |
30 using bookmarks::BookmarkModel; | 32 using bookmarks::BookmarkModel; |
31 using bookmarks::BookmarkNode; | 33 using bookmarks::BookmarkNode; |
32 using content::WebContents; | 34 using content::WebContents; |
33 | 35 |
34 // Watch for bookmark pulse notifications so we can confirm they were sent. | |
35 @interface BookmarkPulseObserver : NSObject { | |
36 int notifications_; | |
37 } | |
38 @property (assign, nonatomic) int notifications; | |
39 @end | |
40 | |
41 | |
42 @implementation BookmarkPulseObserver | |
43 | |
44 @synthesize notifications = notifications_; | |
45 | |
46 - (id)init { | |
47 if ((self = [super init])) { | |
48 [[NSNotificationCenter defaultCenter] | |
49 addObserver:self | |
50 selector:@selector(pulseBookmarkNotification:) | |
51 name:bookmark_button::kPulseBookmarkButtonNotification | |
52 object:nil]; | |
53 } | |
54 return self; | |
55 } | |
56 | |
57 - (void)pulseBookmarkNotification:(NSNotificationCenter *)notification { | |
58 notifications_++; | |
59 } | |
60 | |
61 - (void)dealloc { | |
62 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
63 [super dealloc]; | |
64 } | |
65 | |
66 @end | |
67 | |
68 | |
69 namespace { | 36 namespace { |
70 | 37 |
71 // URL of the test bookmark. | 38 // URL of the test bookmark. |
72 const char kTestBookmarkURL[] = "http://www.google.com"; | 39 const char kTestBookmarkURL[] = "http://www.google.com"; |
73 | 40 |
| 41 class TestBookmarkBubbleObserver : public BookmarkBubbleObserver { |
| 42 public: |
| 43 TestBookmarkBubbleObserver() {} |
| 44 ~TestBookmarkBubbleObserver() override{}; |
| 45 |
| 46 // bookmarks::BookmarkBubbleObserver. |
| 47 void OnBookmarkBubbleShown(const BookmarkNode* node) override { |
| 48 ++shown_count_; |
| 49 } |
| 50 void OnBookmarkBubbleHidden() override { ++hidden_count_; } |
| 51 |
| 52 int shown_count() { return shown_count_; } |
| 53 int hidden_count() { return hidden_count_; } |
| 54 |
| 55 private: |
| 56 int shown_count_ = 0; |
| 57 int hidden_count_ = 0; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(TestBookmarkBubbleObserver); |
| 60 }; |
| 61 |
74 class BookmarkBubbleControllerTest : public CocoaProfileTest { | 62 class BookmarkBubbleControllerTest : public CocoaProfileTest { |
75 public: | 63 public: |
76 static int edits_; | 64 static int edits_; |
77 BookmarkBubbleController* controller_; | 65 BookmarkBubbleController* controller_; |
| 66 TestBookmarkBubbleObserver test_observer_; |
78 | 67 |
79 BookmarkBubbleControllerTest() : controller_(nil) { | 68 BookmarkBubbleControllerTest() : controller_(nil) { |
80 edits_ = 0; | 69 edits_ = 0; |
81 } | 70 } |
82 | 71 |
83 void TearDown() override { | 72 void TearDown() override { |
84 [controller_ close]; | 73 [controller_ close]; |
85 CocoaProfileTest::TearDown(); | 74 CocoaProfileTest::TearDown(); |
86 } | 75 } |
87 | 76 |
88 // Returns a controller but ownership not transferred. | 77 // Returns a controller but ownership not transferred. |
89 // Only one of these will be valid at a time. | 78 // Only one of these will be valid at a time. |
90 BookmarkBubbleController* ControllerForNode(const BookmarkNode* node) { | 79 BookmarkBubbleController* ControllerForNode(const BookmarkNode* node) { |
91 if (controller_ && !IsWindowClosing()) { | 80 if (controller_ && !IsWindowClosing()) { |
92 [controller_ close]; | 81 [controller_ close]; |
93 controller_ = nil; | 82 controller_ = nil; |
94 } | 83 } |
95 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 84 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
96 bookmarks::ManagedBookmarkService* managed = | 85 bookmarks::ManagedBookmarkService* managed = |
97 ManagedBookmarkServiceFactory::GetForProfile(profile()); | 86 ManagedBookmarkServiceFactory::GetForProfile(profile()); |
98 controller_ = [[BookmarkBubbleController alloc] | 87 controller_ = [[BookmarkBubbleController alloc] |
99 initWithParentWindow:browser()->window()->GetNativeWindow() | 88 initWithParentWindow:browser()->window()->GetNativeWindow() |
| 89 bubbleObserver:&test_observer_ |
100 managed:managed | 90 managed:managed |
101 model:model | 91 model:model |
102 node:node | 92 node:node |
103 alreadyBookmarked:YES]; | 93 alreadyBookmarked:YES]; |
104 EXPECT_TRUE([controller_ window]); | 94 EXPECT_TRUE([controller_ window]); |
105 // The window must be gone or we'll fail a unit test with windows left open. | 95 // The window must be gone or we'll fail a unit test with windows left open. |
106 [static_cast<InfoBubbleWindow*>([controller_ window]) | 96 [static_cast<InfoBubbleWindow*>([controller_ window]) |
107 setAllowedAnimations:info_bubble::kAnimateNone]; | 97 setAllowedAnimations:info_bubble::kAnimateNone]; |
108 [controller_ showWindow:nil]; | 98 [controller_ showWindow:nil]; |
109 return controller_; | 99 return controller_; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 if ([[item title] length] == 0 && | 225 if ([[item title] length] == 0 && |
236 static_cast<const BookmarkNode*>([[item representedObject] | 226 static_cast<const BookmarkNode*>([[item representedObject] |
237 pointerValue]) == node2) { | 227 pointerValue]) == node2) { |
238 blankFolderFound = YES; | 228 blankFolderFound = YES; |
239 break; | 229 break; |
240 } | 230 } |
241 } | 231 } |
242 EXPECT_TRUE(blankFolderFound); | 232 EXPECT_TRUE(blankFolderFound); |
243 } | 233 } |
244 | 234 |
245 | |
246 // Click on edit; bubble gets closed. | 235 // Click on edit; bubble gets closed. |
247 TEST_F(BookmarkBubbleControllerTest, TestEdit) { | 236 TEST_F(BookmarkBubbleControllerTest, TestEdit) { |
248 const BookmarkNode* node = CreateTestBookmark(); | 237 const BookmarkNode* node = CreateTestBookmark(); |
249 BookmarkBubbleController* controller = ControllerForNode(node); | 238 BookmarkBubbleController* controller = ControllerForNode(node); |
250 EXPECT_TRUE(controller); | 239 EXPECT_TRUE(controller); |
251 | 240 |
252 EXPECT_EQ(edits_, 0); | 241 EXPECT_EQ(edits_, 0); |
253 EXPECT_FALSE(IsWindowClosing()); | 242 EXPECT_FALSE(IsWindowClosing()); |
254 [controller edit:controller]; | 243 [controller edit:controller]; |
255 EXPECT_EQ(edits_, 1); | 244 EXPECT_EQ(edits_, 1); |
256 EXPECT_TRUE(IsWindowClosing()); | 245 EXPECT_TRUE(IsWindowClosing()); |
257 } | 246 } |
258 | 247 |
259 // CallClose; bubble gets closed. | 248 // CallClose; bubble gets closed. |
260 // Also confirm pulse notifications get sent. | 249 // Also confirm bubble notifications get sent. |
261 TEST_F(BookmarkBubbleControllerTest, TestClose) { | 250 TEST_F(BookmarkBubbleControllerTest, TestClose) { |
262 const BookmarkNode* node = CreateTestBookmark(); | 251 const BookmarkNode* node = CreateTestBookmark(); |
263 EXPECT_EQ(edits_, 0); | 252 EXPECT_EQ(edits_, 0); |
264 | 253 |
265 base::scoped_nsobject<BookmarkPulseObserver> observer( | |
266 [[BookmarkPulseObserver alloc] init]); | |
267 EXPECT_EQ([observer notifications], 0); | |
268 BookmarkBubbleController* controller = ControllerForNode(node); | 254 BookmarkBubbleController* controller = ControllerForNode(node); |
| 255 EXPECT_TRUE(controller.bookmarkBubbleObserver); |
| 256 EXPECT_EQ(1, test_observer_.shown_count()); |
| 257 EXPECT_EQ(0, test_observer_.hidden_count()); |
269 EXPECT_TRUE(controller); | 258 EXPECT_TRUE(controller); |
270 EXPECT_FALSE(IsWindowClosing()); | 259 EXPECT_FALSE(IsWindowClosing()); |
271 EXPECT_EQ([observer notifications], 1); | |
272 [controller ok:controller]; | 260 [controller ok:controller]; |
273 EXPECT_EQ(edits_, 0); | 261 EXPECT_EQ(edits_, 0); |
274 EXPECT_TRUE(IsWindowClosing()); | 262 EXPECT_TRUE(IsWindowClosing()); |
275 EXPECT_EQ([observer notifications], 2); | 263 EXPECT_FALSE(controller.bookmarkBubbleObserver); |
| 264 EXPECT_EQ(1, test_observer_.hidden_count()); |
276 } | 265 } |
277 | 266 |
278 // User changes title and parent folder in the UI | 267 // User changes title and parent folder in the UI |
279 TEST_F(BookmarkBubbleControllerTest, TestUserEdit) { | 268 TEST_F(BookmarkBubbleControllerTest, TestUserEdit) { |
280 BookmarkModel* model = GetBookmarkModel(); | 269 BookmarkModel* model = GetBookmarkModel(); |
281 EXPECT_TRUE(model); | 270 EXPECT_TRUE(model); |
282 const BookmarkNode* bookmarkBarNode = model->bookmark_bar_node(); | 271 const BookmarkNode* bookmarkBarNode = model->bookmark_bar_node(); |
283 EXPECT_TRUE(bookmarkBarNode); | 272 EXPECT_TRUE(bookmarkBarNode); |
284 const BookmarkNode* node = model->AddURL(bookmarkBarNode, | 273 const BookmarkNode* node = model->AddURL(bookmarkBarNode, |
285 0, | 274 0, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 // Create a controller that simulates the bookmark just now being created by | 383 // Create a controller that simulates the bookmark just now being created by |
395 // the user clicking the star, then sending the "cancel" command to represent | 384 // the user clicking the star, then sending the "cancel" command to represent |
396 // them pressing escape. The bookmark should not be there. | 385 // them pressing escape. The bookmark should not be there. |
397 TEST_F(BookmarkBubbleControllerTest, EscapeRemovesNewBookmark) { | 386 TEST_F(BookmarkBubbleControllerTest, EscapeRemovesNewBookmark) { |
398 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); | 387 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile()); |
399 bookmarks::ManagedBookmarkService* managed = | 388 bookmarks::ManagedBookmarkService* managed = |
400 ManagedBookmarkServiceFactory::GetForProfile(profile()); | 389 ManagedBookmarkServiceFactory::GetForProfile(profile()); |
401 const BookmarkNode* node = CreateTestBookmark(); | 390 const BookmarkNode* node = CreateTestBookmark(); |
402 BookmarkBubbleController* controller = [[BookmarkBubbleController alloc] | 391 BookmarkBubbleController* controller = [[BookmarkBubbleController alloc] |
403 initWithParentWindow:browser()->window()->GetNativeWindow() | 392 initWithParentWindow:browser()->window()->GetNativeWindow() |
| 393 bubbleObserver:&test_observer_ |
404 managed:managed | 394 managed:managed |
405 model:model | 395 model:model |
406 node:node | 396 node:node |
407 alreadyBookmarked:NO]; // The last param is the key difference. | 397 alreadyBookmarked:NO]; // The last param is the key difference. |
408 EXPECT_TRUE([controller window]); | 398 EXPECT_TRUE([controller window]); |
409 // Calls release on controller. | 399 // Calls release on controller. |
410 [controller cancel:nil]; | 400 [controller cancel:nil]; |
411 EXPECT_FALSE(model->IsBookmarked(GURL(kTestBookmarkURL))); | 401 EXPECT_FALSE(model->IsBookmarked(GURL(kTestBookmarkURL))); |
412 } | 402 } |
413 | 403 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 // Normally this would be sent up the responder tree correctly, but since | 473 // Normally this would be sent up the responder tree correctly, but since |
484 // tests run in the background, key window and main window are never set on | 474 // tests run in the background, key window and main window are never set on |
485 // NSApplication. Adding it to NSApplication directly removes the need for | 475 // NSApplication. Adding it to NSApplication directly removes the need for |
486 // worrying about what the current window with focus is. | 476 // worrying about what the current window with focus is. |
487 - (void)editBookmarkNode:(id)sender { | 477 - (void)editBookmarkNode:(id)sender { |
488 EXPECT_TRUE([sender respondsToSelector:@selector(node)]); | 478 EXPECT_TRUE([sender respondsToSelector:@selector(node)]); |
489 BookmarkBubbleControllerTest::edits_++; | 479 BookmarkBubbleControllerTest::edits_++; |
490 } | 480 } |
491 | 481 |
492 @end | 482 @end |
OLD | NEW |