Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_menu_cocoa_controller_unittest.mm
blobb702282bf148683b0cfd417e738c9e6e63d55b39
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_factory.h"
8 #include "chrome/browser/ui/browser.h"
9 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_cocoa_controller.h"
10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/bookmarks/browser/bookmark_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using bookmarks::BookmarkModel;
17 using bookmarks::BookmarkNode;
19 @interface FakeBookmarkMenuController : BookmarkMenuCocoaController {
20  @public
21   const BookmarkNode* nodes_[2];
22   BOOL opened_[2];
23   BOOL opened_new_foreground_tab;
24   BOOL opened_new_window;
25   BOOL opened_off_the_record;
27 - (id)initWithProfile:(Profile*)profile;
28 @end
30 @implementation FakeBookmarkMenuController
32 - (id)initWithProfile:(Profile*)profile {
33   if ((self = [super init])) {
34     base::string16 empty;
35     BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile);
36     const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
37     nodes_[0] = model->AddURL(bookmark_bar, 0, empty, GURL("http://0.com"));
38     nodes_[1] = model->AddURL(bookmark_bar, 1, empty, GURL("http://1.com"));
39   }
40   return self;
43 - (const BookmarkNode*)nodeForIdentifier:(int)identifier {
44   if ((identifier < 0) || (identifier >= 2))
45     return NULL;
46   return nodes_[identifier];
49 - (void)openURLForNode:(const BookmarkNode*)node {
50   std::string url = node->url().possibly_invalid_spec();
51   if (url.find("http://0.com") != std::string::npos)
52     opened_[0] = YES;
53   if (url.find("http://1.com") != std::string::npos)
54     opened_[1] = YES;
57 - (void)openAll:(NSInteger)tag
58     withDisposition:(WindowOpenDisposition)disposition {
59   if (disposition == NEW_FOREGROUND_TAB) {
60     opened_new_foreground_tab = YES;
61   } else if (disposition == NEW_WINDOW) {
62     opened_new_window = YES;
63   } else if (disposition == OFF_THE_RECORD) {
64     opened_off_the_record = YES;
65   }
68 @end  // FakeBookmarkMenuController
70 class BookmarkMenuCocoaControllerTest : public CocoaProfileTest {
71  public:
72   void SetUp() override {
73     CocoaProfileTest::SetUp();
74     ASSERT_TRUE(profile());
76     controller_.reset(
77         [[FakeBookmarkMenuController alloc] initWithProfile:profile()]);
78   }
80   FakeBookmarkMenuController* controller() { return controller_.get(); }
82  private:
83   base::scoped_nsobject<FakeBookmarkMenuController> controller_;
86 TEST_F(BookmarkMenuCocoaControllerTest, TestOpenItem) {
87   FakeBookmarkMenuController* c = controller();
88   NSMenuItem *item = [[[NSMenuItem alloc] init] autorelease];
89   for (int i = 0; i < 2; i++) {
90     [item setTag:i];
91     ASSERT_EQ(c->opened_[i], NO);
92     [c openBookmarkMenuItem:item];
93     ASSERT_NE(c->opened_[i], NO);
94   }
96   // Test three versions of 'Open All Bookmarks' item. tag id means nothing.
97   // I just reset the tag id to zero.
98   [item setTag:0];
99   EXPECT_EQ(c->opened_new_foreground_tab, NO);
100   [c openAllBookmarks:item];
101   EXPECT_NE(c->opened_new_foreground_tab, NO);
103   EXPECT_EQ(c->opened_new_window, NO);
104   [c openAllBookmarksNewWindow:item];
105   EXPECT_NE(c->opened_new_window, NO);
107   EXPECT_EQ(c->opened_off_the_record, NO);
108   [c openAllBookmarksIncognitoWindow:item];
109   EXPECT_NE(c->opened_off_the_record, NO);