Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/menus/simple_menu_model.h" | 7 #include "app/menus/simple_menu_model.h" |
| 8 #include "app/resource_bundle.h" | |
| 8 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 11 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
| 11 #include "chrome/browser/ui/cocoa/menu_controller.h" | 12 #include "chrome/browser/ui/cocoa/menu_controller.h" |
| 13 #include "grit/app_resources.h" | |
| 12 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | |
| 13 | 16 |
| 14 class MenuControllerTest : public CocoaTest { | 17 class MenuControllerTest : public CocoaTest { |
| 15 }; | 18 }; |
| 16 | 19 |
| 17 // A menu delegate that counts the number of times certain things are called | 20 // A menu delegate that counts the number of times certain things are called |
| 18 // to make sure things are hooked up properly. | 21 // to make sure things are hooked up properly. |
| 19 class Delegate : public menus::SimpleMenuModel::Delegate { | 22 class Delegate : public menus::SimpleMenuModel::Delegate { |
| 20 public: | 23 public: |
| 21 Delegate() : execute_count_(0), enable_count_(0) { } | 24 Delegate() : execute_count_(0), enable_count_(0) { } |
| 22 | 25 |
| 23 virtual bool IsCommandIdChecked(int command_id) const { return false; } | 26 virtual bool IsCommandIdChecked(int command_id) const { return false; } |
| 24 virtual bool IsCommandIdEnabled(int command_id) const { | 27 virtual bool IsCommandIdEnabled(int command_id) const { |
| 25 ++enable_count_; | 28 ++enable_count_; |
| 26 return true; | 29 return true; |
| 27 } | 30 } |
| 28 virtual bool GetAcceleratorForCommandId( | 31 virtual bool GetAcceleratorForCommandId( |
| 29 int command_id, | 32 int command_id, |
| 30 menus::Accelerator* accelerator) { return false; } | 33 menus::Accelerator* accelerator) { return false; } |
| 31 virtual void ExecuteCommand(int command_id) { ++execute_count_; } | 34 virtual void ExecuteCommand(int command_id) { ++execute_count_; } |
| 32 | 35 |
| 33 int execute_count_; | 36 int execute_count_; |
| 34 mutable int enable_count_; | 37 mutable int enable_count_; |
| 35 }; | 38 }; |
| 36 | 39 |
| 40 // Just like Delegate, except the items are treated as "dynamic" so updates to | |
| 41 // the label/icon in the model are reflected in the menu. | |
| 42 class DynamicDelegate : public Delegate { | |
| 43 public: | |
| 44 DynamicDelegate() : icon_(NULL) {} | |
| 45 virtual bool IsItemForCommandIdDynamic(int command_id) const { return true; } | |
| 46 virtual string16 GetLabelForCommandId(int command_id) const { return label_; } | |
| 47 virtual bool GetIconForCommandId(int command_id, SkBitmap* icon) const { | |
| 48 if (icon_) { | |
| 49 *icon = *icon_; | |
| 50 return true; | |
| 51 } else { | |
| 52 return false; | |
| 53 } | |
| 54 } | |
| 55 void SetDynamicLabel(string16 label) { label_ = label; } | |
| 56 void SetDynamicIcon(SkBitmap* icon) { icon_ = icon; } | |
| 57 private: | |
| 58 string16 label_; | |
| 59 SkBitmap* icon_; | |
| 60 }; | |
| 61 | |
| 37 TEST_F(MenuControllerTest, EmptyMenu) { | 62 TEST_F(MenuControllerTest, EmptyMenu) { |
| 38 Delegate delegate; | 63 Delegate delegate; |
| 39 menus::SimpleMenuModel model(&delegate); | 64 menus::SimpleMenuModel model(&delegate); |
| 40 scoped_nsobject<MenuController> menu( | 65 scoped_nsobject<MenuController> menu( |
| 41 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); | 66 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); |
| 42 EXPECT_EQ([[menu menu] numberOfItems], 0); | 67 EXPECT_EQ([[menu menu] numberOfItems], 0); |
| 43 } | 68 } |
| 44 | 69 |
| 45 TEST_F(MenuControllerTest, BasicCreation) { | 70 TEST_F(MenuControllerTest, BasicCreation) { |
| 46 Delegate delegate; | 71 Delegate delegate; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 | 213 |
| 189 [menu setModel:&model]; | 214 [menu setModel:&model]; |
| 190 [menu setUseWithPopUpButtonCell:NO]; | 215 [menu setUseWithPopUpButtonCell:NO]; |
| 191 EXPECT_TRUE([menu menu]); | 216 EXPECT_TRUE([menu menu]); |
| 192 EXPECT_EQ(3, [[menu menu] numberOfItems]); | 217 EXPECT_EQ(3, [[menu menu] numberOfItems]); |
| 193 | 218 |
| 194 // Check immutability. | 219 // Check immutability. |
| 195 model.AddItem(4, ASCIIToUTF16("four")); | 220 model.AddItem(4, ASCIIToUTF16("four")); |
| 196 EXPECT_EQ(3, [[menu menu] numberOfItems]); | 221 EXPECT_EQ(3, [[menu menu] numberOfItems]); |
| 197 } | 222 } |
| 223 | |
| 224 TEST_F(MenuControllerTest, Dynamic) { | |
| 225 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
| |
| 226 string16 initial = ASCIIToUTF16("initial"); | |
| 227 delegate.SetDynamicLabel(initial); | |
| 228 menus::SimpleMenuModel model(&delegate); | |
| 229 model.AddItem(1, ASCIIToUTF16("foo")); | |
| 230 scoped_nsobject<MenuController> menu( | |
| 231 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); | |
| 232 EXPECT_EQ([[menu menu] numberOfItems], 1); | |
| 233 Validate(menu.get(), [menu menu]); | |
| 234 NSMenuItem* item = [[menu menu] itemAtIndex:0]; | |
| 235 EXPECT_EQ(initial, base::SysNSStringToUTF16([item title])); | |
| 236 EXPECT_EQ(nil, [item image]); | |
| 237 string16 second = ASCIIToUTF16("second"); | |
| 238 delegate.SetDynamicLabel(second); | |
| 239 SkBitmap* bitmap = | |
| 240 ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_THROBBER); | |
| 241 delegate.SetDynamicIcon(bitmap); | |
| 242 Validate(menu.get(), [menu menu]); | |
| 243 EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); | |
| 244 EXPECT_TRUE([item image] != nil); | |
| 245 } | |
| OLD | NEW |