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.
5 #import "base/mac/scoped_nsobject.h"
6 #include "base/strings/string16.h"
7 #include "chrome/browser/bookmarks/bookmark_model.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/bookmarks/bookmark_utils.h"
10 #include "chrome/browser/ui/browser.h"
11 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_cocoa_controller.h"
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 @interface FakeBookmarkMenuController : BookmarkMenuCocoaController {
18 const BookmarkNode* nodes_[2];
20 BOOL opened_new_foreground_tab;
21 BOOL opened_new_window;
22 BOOL opened_off_the_record;
24 - (id)initWithProfile:(Profile*)profile;
27 @implementation FakeBookmarkMenuController
29 - (id)initWithProfile:(Profile*)profile {
30 if ((self = [super init])) {
32 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile);
33 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
34 nodes_[0] = model->AddURL(bookmark_bar, 0, empty, GURL("http://0.com"));
35 nodes_[1] = model->AddURL(bookmark_bar, 1, empty, GURL("http://1.com"));
40 - (const BookmarkNode*)nodeForIdentifier:(int)identifier {
41 if ((identifier < 0) || (identifier >= 2))
43 return nodes_[identifier];
46 - (void)openURLForNode:(const BookmarkNode*)node {
47 std::string url = node->url().possibly_invalid_spec();
48 if (url.find("http://0.com") != std::string::npos)
50 if (url.find("http://1.com") != std::string::npos)
54 - (void)openAll:(NSInteger)tag
55 withDisposition:(WindowOpenDisposition)disposition {
56 if (disposition == NEW_FOREGROUND_TAB) {
57 opened_new_foreground_tab = YES;
58 } else if (disposition == NEW_WINDOW) {
59 opened_new_window = YES;
60 } else if (disposition == OFF_THE_RECORD) {
61 opened_off_the_record = YES;
65 @end // FakeBookmarkMenuController
67 class BookmarkMenuCocoaControllerTest : public CocoaProfileTest {
69 virtual void SetUp() {
70 CocoaProfileTest::SetUp();
71 ASSERT_TRUE(profile());
74 [[FakeBookmarkMenuController alloc] initWithProfile:profile()]);
77 FakeBookmarkMenuController* controller() { return controller_.get(); }
80 base::scoped_nsobject<FakeBookmarkMenuController> controller_;
83 TEST_F(BookmarkMenuCocoaControllerTest, TestOpenItem) {
84 FakeBookmarkMenuController* c = controller();
85 NSMenuItem *item = [[[NSMenuItem alloc] init] autorelease];
86 for (int i = 0; i < 2; i++) {
88 ASSERT_EQ(c->opened_[i], NO);
89 [c openBookmarkMenuItem:item];
90 ASSERT_NE(c->opened_[i], NO);
93 // Test three versions of 'Open All Bookmarks' item. tag id means nothing.
94 // I just reset the tag id to zero.
96 EXPECT_EQ(c->opened_new_foreground_tab, NO);
97 [c openAllBookmarks:item];
98 EXPECT_NE(c->opened_new_foreground_tab, NO);
100 EXPECT_EQ(c->opened_new_window, NO);
101 [c openAllBookmarksNewWindow:item];
102 EXPECT_NE(c->opened_new_window, NO);
104 EXPECT_EQ(c->opened_off_the_record, NO);
105 [c openAllBookmarksIncognitoWindow:item];
106 EXPECT_NE(c->opened_off_the_record, NO);