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 #include "base/strings/string_util.h"
6 #include "base/strings/sys_string_conversions.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/status_icons/status_icon_menu_model.h"
9 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
10 #include "chrome/browser/ui/cocoa/status_icons/status_icon_mac.h"
11 #include "grit/chrome_unscaled_resources.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/base/resource/resource_bundle.h"
18 class StatusIconMacTest : public CocoaTest {
21 TEST_F(StatusIconMacTest, Create) {
22 // Create an icon, set the tool tip, then shut it down (checks for leaks).
23 scoped_ptr<StatusIcon> icon(new StatusIconMac());
24 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
25 gfx::ImageSkia* image = rb.GetImageSkiaNamed(IDR_STATUS_TRAY_ICON);
26 icon->SetImage(*image);
27 icon->SetToolTip(base::ASCIIToUTF16("tool tip"));
30 TEST_F(StatusIconMacTest, CreateMenu) {
31 // Create a menu and verify by getting the title of the first menu item.
32 const char* menu_title = "Menu Title";
33 scoped_ptr<StatusIconMenuModel> model(new StatusIconMenuModel(NULL));
34 model->AddItem(0, base::ASCIIToUTF16(menu_title));
36 scoped_ptr<StatusIconMac> icon(new StatusIconMac());
37 icon->UpdatePlatformContextMenu(model.get());
38 EXPECT_EQ(1, [[icon->item() menu] numberOfItems]);
40 NSMenuItem* menuItem = [[icon->item() menu] itemAtIndex:0];
41 EXPECT_STREQ(menu_title, [[menuItem title] UTF8String]);
44 TEST_F(StatusIconMacTest, MenuToolTip) {
45 // Create a menu and set a tool tip. Verify the tool tip is inserted as the
47 const char* menu_title = "Menu Title";
48 const char* tool_tip = "Tool tip";
49 scoped_ptr<StatusIconMenuModel> model(new StatusIconMenuModel(NULL));
50 model->AddItem(0, base::ASCIIToUTF16(menu_title));
52 scoped_ptr<StatusIconMac> icon(new StatusIconMac());
53 icon->UpdatePlatformContextMenu(model.get());
54 icon->SetToolTip(base::ASCIIToUTF16(tool_tip));
55 EXPECT_EQ(2, [[icon->item() menu] numberOfItems]);
57 NSMenuItem* toolTipItem = [[icon->item() menu] itemAtIndex:0];
58 EXPECT_STREQ(tool_tip, [[toolTipItem title] UTF8String]);
59 NSMenuItem* menuItem = [[icon->item() menu] itemAtIndex:1];
60 EXPECT_STREQ(menu_title, [[menuItem title] UTF8String]);