| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 11 #include "chrome/browser/ui/cocoa/menu_controller.h" | 11 #include "chrome/browser/ui/cocoa/menu_controller.h" |
| 12 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
| 13 #include "grit/ui_resources.h" | 13 #include "grit/ui_resources.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/base/models/simple_menu_model.h" | 15 #include "ui/base/models/simple_menu_model.h" |
| 16 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/image/image_skia.h" | 17 #include "ui/gfx/image/image.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 class MenuControllerTest : public CocoaTest { | 21 class MenuControllerTest : public CocoaTest { |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 // A menu delegate that counts the number of times certain things are called | 24 // A menu delegate that counts the number of times certain things are called |
| 25 // to make sure things are hooked up properly. | 25 // to make sure things are hooked up properly. |
| 26 class Delegate : public ui::SimpleMenuModel::Delegate { | 26 class Delegate : public ui::SimpleMenuModel::Delegate { |
| 27 public: | 27 public: |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // MenuWillShow. | 68 // MenuWillShow. |
| 69 NSMenu* menu_to_close_; | 69 NSMenu* menu_to_close_; |
| 70 bool did_show_; | 70 bool did_show_; |
| 71 bool did_close_; | 71 bool did_close_; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 // Just like Delegate, except the items are treated as "dynamic" so updates to | 74 // Just like Delegate, except the items are treated as "dynamic" so updates to |
| 75 // the label/icon in the model are reflected in the menu. | 75 // the label/icon in the model are reflected in the menu. |
| 76 class DynamicDelegate : public Delegate { | 76 class DynamicDelegate : public Delegate { |
| 77 public: | 77 public: |
| 78 DynamicDelegate() : icon_(NULL) {} | 78 DynamicDelegate() {} |
| 79 virtual bool IsItemForCommandIdDynamic(int command_id) const { return true; } | 79 virtual bool IsItemForCommandIdDynamic(int command_id) const { return true; } |
| 80 virtual string16 GetLabelForCommandId(int command_id) const { return label_; } | 80 virtual string16 GetLabelForCommandId(int command_id) const { return label_; } |
| 81 virtual bool GetIconForCommandId(int command_id, gfx::ImageSkia* icon) const { | 81 virtual bool GetIconForCommandId(int command_id, gfx::Image* icon) const { |
| 82 if (icon_) { | 82 if (icon_.IsEmpty()) { |
| 83 *icon = *icon_; | 83 return false; |
| 84 } else { |
| 85 *icon = icon_; |
| 84 return true; | 86 return true; |
| 85 } else { | |
| 86 return false; | |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 void SetDynamicLabel(string16 label) { label_ = label; } | 89 void SetDynamicLabel(string16 label) { label_ = label; } |
| 90 void SetDynamicIcon(SkBitmap* icon) { icon_ = icon; } | 90 void SetDynamicIcon(const gfx::Image& icon) { icon_ = icon; } |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 string16 label_; | 93 string16 label_; |
| 94 SkBitmap* icon_; | 94 gfx::Image icon_; |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 TEST_F(MenuControllerTest, EmptyMenu) { | 97 TEST_F(MenuControllerTest, EmptyMenu) { |
| 98 Delegate delegate; | 98 Delegate delegate; |
| 99 ui::SimpleMenuModel model(&delegate); | 99 ui::SimpleMenuModel model(&delegate); |
| 100 scoped_nsobject<MenuController> menu( | 100 scoped_nsobject<MenuController> menu( |
| 101 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); | 101 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); |
| 102 EXPECT_EQ([[menu menu] numberOfItems], 0); | 102 EXPECT_EQ([[menu menu] numberOfItems], 0); |
| 103 } | 103 } |
| 104 | 104 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 // initialized after this so we can validate the menu contents. | 273 // initialized after this so we can validate the menu contents. |
| 274 Validate(menu.get(), [menu menu]); | 274 Validate(menu.get(), [menu menu]); |
| 275 NSMenuItem* item = [[menu menu] itemAtIndex:0]; | 275 NSMenuItem* item = [[menu menu] itemAtIndex:0]; |
| 276 // Item should have the "initial" label and no icon. | 276 // Item should have the "initial" label and no icon. |
| 277 EXPECT_EQ(initial, base::SysNSStringToUTF16([item title])); | 277 EXPECT_EQ(initial, base::SysNSStringToUTF16([item title])); |
| 278 EXPECT_EQ(nil, [item image]); | 278 EXPECT_EQ(nil, [item image]); |
| 279 | 279 |
| 280 // Now update the item to have a label of "second" and an icon. | 280 // Now update the item to have a label of "second" and an icon. |
| 281 string16 second = ASCIIToUTF16("second"); | 281 string16 second = ASCIIToUTF16("second"); |
| 282 delegate.SetDynamicLabel(second); | 282 delegate.SetDynamicLabel(second); |
| 283 SkBitmap* bitmap = | 283 const gfx::Image& icon = |
| 284 ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_THROBBER); | 284 ResourceBundle::GetSharedInstance().GetNativeImageNamed(IDR_THROBBER); |
| 285 delegate.SetDynamicIcon(bitmap); | 285 delegate.SetDynamicIcon(icon); |
| 286 // Simulate opening the menu and validate that the item label + icon changes. | 286 // Simulate opening the menu and validate that the item label + icon changes. |
| 287 Validate(menu.get(), [menu menu]); | 287 Validate(menu.get(), [menu menu]); |
| 288 EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); | 288 EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); |
| 289 EXPECT_TRUE([item image] != nil); | 289 EXPECT_TRUE([item image] != nil); |
| 290 | 290 |
| 291 // Now get rid of the icon and make sure it goes away. | 291 // Now get rid of the icon and make sure it goes away. |
| 292 delegate.SetDynamicIcon(NULL); | 292 delegate.SetDynamicIcon(gfx::Image()); |
| 293 Validate(menu.get(), [menu menu]); | 293 Validate(menu.get(), [menu menu]); |
| 294 EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); | 294 EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); |
| 295 EXPECT_EQ(nil, [item image]); | 295 EXPECT_EQ(nil, [item image]); |
| 296 } | 296 } |
| 297 | 297 |
| 298 TEST_F(MenuControllerTest, OpenClose) { | 298 TEST_F(MenuControllerTest, OpenClose) { |
| 299 // ui::SimpleMenuModel posts a task that calls Delegate::MenuClosed. Create | 299 // ui::SimpleMenuModel posts a task that calls Delegate::MenuClosed. Create |
| 300 // a MessageLoop for that purpose. | 300 // a MessageLoop for that purpose. |
| 301 MessageLoop message_loop(MessageLoop::TYPE_UI); | 301 MessageLoop message_loop(MessageLoop::TYPE_UI); |
| 302 | 302 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 327 EXPECT_FALSE(delegate.did_close_); | 327 EXPECT_FALSE(delegate.did_close_); |
| 328 | 328 |
| 329 // Pump the task that notifies the delegate. | 329 // Pump the task that notifies the delegate. |
| 330 message_loop.RunAllPending(); | 330 message_loop.RunAllPending(); |
| 331 | 331 |
| 332 // Expect that the delegate got notified properly. | 332 // Expect that the delegate got notified properly. |
| 333 EXPECT_TRUE(delegate.did_close_); | 333 EXPECT_TRUE(delegate.did_close_); |
| 334 } | 334 } |
| 335 | 335 |
| 336 } // namespace | 336 } // namespace |
| OLD | NEW |