BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / history_menu_cocoa_controller_unittest.mm
blob1c673fcfd3e376bc223062240c3efe58f9021873
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 {
17  @public
18   BOOL opened_[2];
20 @end
22 @implementation FakeHistoryMenuController
24 - (id)initTest {
25   if ((self = [super init])) {
26     opened_[0] = NO;
27     opened_[1] = NO;
28   }
29   return self;
32 - (void)openURLForItem:(const HistoryMenuBridge::HistoryItem*)item {
33   opened_[item->session_id] = YES;
36 @end  // FakeHistoryMenuController
38 class HistoryMenuCocoaControllerTest : public CocoaProfileTest {
39  public:
40   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];
48   }
50   void CreateItems(NSMenu* menu) {
51     HistoryMenuBridge::HistoryItem* item = new HistoryMenuBridge::HistoryItem();
52     item->url = GURL("http://google.com");
53     item->session_id = 0;
54     bridge_->AddItemToMenu(item, menu, HistoryMenuBridge::kVisited, 0);
56     item = new HistoryMenuBridge::HistoryItem();
57     item->url = GURL("http://apple.com");
58     item->session_id = 1;
59     bridge_->AddItemToMenu(item, menu, HistoryMenuBridge::kVisited, 1);
60   }
62   std::map<NSMenuItem*, HistoryMenuBridge::HistoryItem*>& menu_item_map() {
63     return bridge_->menu_item_map_;
64   }
66   FakeHistoryMenuController* controller() {
67     return static_cast<FakeHistoryMenuController*>(bridge_->controller_.get());
68   }
70  private:
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 =
79       menu_item_map();
80   std::map<NSMenuItem*, HistoryMenuBridge::HistoryItem*>::iterator it =
81       items.begin();
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]);
88   }