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.
5 #include "base/mac/scoped_nsobject.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
11 #include "chrome/browser/ui/cocoa/history_menu_bridge.h"
12 #include "chrome/browser/ui/cocoa/history_menu_cocoa_controller.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 @interface FakeHistoryMenuController : HistoryMenuCocoaController {
22 @implementation FakeHistoryMenuController
25 if ((self = [super init])) {
32 - (void)openURLForItem:(const HistoryMenuBridge::HistoryItem*)item {
33 opened_[item->session_id] = YES;
36 @end // FakeHistoryMenuController
38 class HistoryMenuCocoaControllerTest : public CocoaProfileTest {
40 virtual void SetUp() OVERRIDE {
41 CocoaProfileTest::SetUp();
42 ASSERT_TRUE(profile());
44 bridge_.reset(new HistoryMenuBridge(profile()));
45 bridge_->controller_.reset(
46 [[FakeHistoryMenuController alloc] initWithBridge:bridge_.get()]);
47 [controller() initTest];
50 void CreateItems(NSMenu* menu) {
51 HistoryMenuBridge::HistoryItem* item = new HistoryMenuBridge::HistoryItem();
52 item->url = GURL("http://google.com");
54 bridge_->AddItemToMenu(item, menu, HistoryMenuBridge::kVisited, 0);
56 item = new HistoryMenuBridge::HistoryItem();
57 item->url = GURL("http://apple.com");
59 bridge_->AddItemToMenu(item, menu, HistoryMenuBridge::kVisited, 1);
62 std::map<NSMenuItem*, HistoryMenuBridge::HistoryItem*>& menu_item_map() {
63 return bridge_->menu_item_map_;
66 FakeHistoryMenuController* controller() {
67 return static_cast<FakeHistoryMenuController*>(bridge_->controller_.get());
71 scoped_ptr<HistoryMenuBridge> bridge_;
74 TEST_F(HistoryMenuCocoaControllerTest, OpenURLForItem) {
75 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"History"]);
76 CreateItems(menu.get());
78 std::map<NSMenuItem*, HistoryMenuBridge::HistoryItem*>& items =
80 std::map<NSMenuItem*, HistoryMenuBridge::HistoryItem*>::iterator it =
83 for ( ; it != items.end(); ++it) {
84 HistoryMenuBridge::HistoryItem* item = it->second;
85 EXPECT_FALSE(controller()->opened_[item->session_id]);
86 [controller() openHistoryMenuItem:it->first];
87 EXPECT_TRUE(controller()->opened_[item->session_id]);