Chromium Code Reviews| Index: chrome/browser/ui/cocoa/menu_controller_unittest.mm |
| diff --git a/chrome/browser/ui/cocoa/menu_controller_unittest.mm b/chrome/browser/ui/cocoa/menu_controller_unittest.mm |
| index 9171adfde22d7f3a7fd358b66e1c4a2fe6e8fe08..5c6e3af52824b584000ecc68f4107db03fed7f28 100644 |
| --- a/chrome/browser/ui/cocoa/menu_controller_unittest.mm |
| +++ b/chrome/browser/ui/cocoa/menu_controller_unittest.mm |
| @@ -5,11 +5,14 @@ |
| #import <Cocoa/Cocoa.h> |
| #include "app/menus/simple_menu_model.h" |
| +#include "app/resource_bundle.h" |
| #include "base/sys_string_conversions.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| #include "chrome/browser/ui/cocoa/menu_controller.h" |
| +#include "grit/app_resources.h" |
| #include "grit/generated_resources.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| class MenuControllerTest : public CocoaTest { |
| }; |
| @@ -34,6 +37,28 @@ class Delegate : public menus::SimpleMenuModel::Delegate { |
| mutable int enable_count_; |
| }; |
| +// Just like Delegate, except the items are treated as "dynamic" so updates to |
| +// the label/icon in the model are reflected in the menu. |
| +class DynamicDelegate : public Delegate { |
| + public: |
| + DynamicDelegate() : icon_(NULL) {} |
| + virtual bool IsItemForCommandIdDynamic(int command_id) const { return true; } |
| + virtual string16 GetLabelForCommandId(int command_id) const { return label_; } |
| + virtual bool GetIconForCommandId(int command_id, SkBitmap* icon) const { |
| + if (icon_) { |
| + *icon = *icon_; |
| + return true; |
| + } else { |
| + return false; |
| + } |
| + } |
| + void SetDynamicLabel(string16 label) { label_ = label; } |
| + void SetDynamicIcon(SkBitmap* icon) { icon_ = icon; } |
| + private: |
| + string16 label_; |
| + SkBitmap* icon_; |
| +}; |
| + |
| TEST_F(MenuControllerTest, EmptyMenu) { |
| Delegate delegate; |
| menus::SimpleMenuModel model(&delegate); |
| @@ -195,3 +220,26 @@ TEST_F(MenuControllerTest, DefaultInitializer) { |
| model.AddItem(4, ASCIIToUTF16("four")); |
| EXPECT_EQ(3, [[menu menu] numberOfItems]); |
| } |
| + |
| +TEST_F(MenuControllerTest, Dynamic) { |
| + DynamicDelegate delegate; |
|
Evan Stade
2010/12/14 01:20:13
can you add a couple short comments walking th rea
Andrew T Wilson (Slow)
2010/12/14 18:23:27
Done. Also added one more test to ensure we can cl
|
| + string16 initial = ASCIIToUTF16("initial"); |
| + delegate.SetDynamicLabel(initial); |
| + menus::SimpleMenuModel model(&delegate); |
| + model.AddItem(1, ASCIIToUTF16("foo")); |
| + scoped_nsobject<MenuController> menu( |
| + [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); |
| + EXPECT_EQ([[menu menu] numberOfItems], 1); |
| + Validate(menu.get(), [menu menu]); |
| + NSMenuItem* item = [[menu menu] itemAtIndex:0]; |
| + EXPECT_EQ(initial, base::SysNSStringToUTF16([item title])); |
| + EXPECT_EQ(nil, [item image]); |
| + string16 second = ASCIIToUTF16("second"); |
| + delegate.SetDynamicLabel(second); |
| + SkBitmap* bitmap = |
| + ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_THROBBER); |
| + delegate.SetDynamicIcon(bitmap); |
| + Validate(menu.get(), [menu menu]); |
| + EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); |
| + EXPECT_TRUE([item image] != nil); |
| +} |