| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "grit/ui_resources.h" | 10 #include "grit/ui_resources.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 // Just like Delegate, except the items are treated as "dynamic" so updates to | 82 // Just like Delegate, except the items are treated as "dynamic" so updates to |
| 83 // the label/icon in the model are reflected in the menu. | 83 // the label/icon in the model are reflected in the menu. |
| 84 class DynamicDelegate : public Delegate { | 84 class DynamicDelegate : public Delegate { |
| 85 public: | 85 public: |
| 86 DynamicDelegate() {} | 86 DynamicDelegate() {} |
| 87 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE { | 87 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE { |
| 88 return true; | 88 return true; |
| 89 } | 89 } |
| 90 virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE { | 90 virtual base::string16 GetLabelForCommandId(int command_id) const OVERRIDE { |
| 91 return label_; | 91 return label_; |
| 92 } | 92 } |
| 93 virtual bool GetIconForCommandId( | 93 virtual bool GetIconForCommandId( |
| 94 int command_id, | 94 int command_id, |
| 95 gfx::Image* icon) const OVERRIDE { | 95 gfx::Image* icon) const OVERRIDE { |
| 96 if (icon_.IsEmpty()) { | 96 if (icon_.IsEmpty()) { |
| 97 return false; | 97 return false; |
| 98 } else { | 98 } else { |
| 99 *icon = icon_; | 99 *icon = icon_; |
| 100 return true; | 100 return true; |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 void SetDynamicLabel(string16 label) { label_ = label; } | 103 void SetDynamicLabel(base::string16 label) { label_ = label; } |
| 104 void SetDynamicIcon(const gfx::Image& icon) { icon_ = icon; } | 104 void SetDynamicIcon(const gfx::Image& icon) { icon_ = icon; } |
| 105 | 105 |
| 106 private: | 106 private: |
| 107 string16 label_; | 107 base::string16 label_; |
| 108 gfx::Image icon_; | 108 gfx::Image icon_; |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 // Menu model that returns a gfx::Font object for one of the items in the menu. | 111 // Menu model that returns a gfx::Font object for one of the items in the menu. |
| 112 class FontMenuModel : public SimpleMenuModel { | 112 class FontMenuModel : public SimpleMenuModel { |
| 113 public: | 113 public: |
| 114 FontMenuModel(SimpleMenuModel::Delegate* delegate, | 114 FontMenuModel(SimpleMenuModel::Delegate* delegate, |
| 115 const gfx::Font* font, int index) | 115 const gfx::Font* font, int index) |
| 116 : SimpleMenuModel(delegate), | 116 : SimpleMenuModel(delegate), |
| 117 font_(font), | 117 font_(font), |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 model.AddItem(1, ASCIIToUTF16("one")); | 215 model.AddItem(1, ASCIIToUTF16("one")); |
| 216 model.AddItem(2, ASCIIToUTF16("two")); | 216 model.AddItem(2, ASCIIToUTF16("two")); |
| 217 model.AddItem(3, ASCIIToUTF16("three")); | 217 model.AddItem(3, ASCIIToUTF16("three")); |
| 218 | 218 |
| 219 // Menu should have an extra item inserted at position 0 that has an empty | 219 // Menu should have an extra item inserted at position 0 that has an empty |
| 220 // title. | 220 // title. |
| 221 base::scoped_nsobject<MenuController> menu( | 221 base::scoped_nsobject<MenuController> menu( |
| 222 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:YES]); | 222 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:YES]); |
| 223 EXPECT_EQ([[menu menu] numberOfItems], 4); | 223 EXPECT_EQ([[menu menu] numberOfItems], 4); |
| 224 EXPECT_EQ(base::SysNSStringToUTF16([[[menu menu] itemAtIndex:0] title]), | 224 EXPECT_EQ(base::SysNSStringToUTF16([[[menu menu] itemAtIndex:0] title]), |
| 225 string16()); | 225 base::string16()); |
| 226 | 226 |
| 227 // Make sure the tags are still correct (the index no longer matches the tag). | 227 // Make sure the tags are still correct (the index no longer matches the tag). |
| 228 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2]; | 228 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2]; |
| 229 EXPECT_EQ([itemTwo tag], 1); | 229 EXPECT_EQ([itemTwo tag], 1); |
| 230 } | 230 } |
| 231 | 231 |
| 232 TEST_F(MenuControllerTest, Execute) { | 232 TEST_F(MenuControllerTest, Execute) { |
| 233 Delegate delegate; | 233 Delegate delegate; |
| 234 SimpleMenuModel model(&delegate); | 234 SimpleMenuModel model(&delegate); |
| 235 model.AddItem(1, ASCIIToUTF16("one")); | 235 model.AddItem(1, ASCIIToUTF16("one")); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 model.AddItem(4, ASCIIToUTF16("four")); | 306 model.AddItem(4, ASCIIToUTF16("four")); |
| 307 EXPECT_EQ(3, [[menu menu] numberOfItems]); | 307 EXPECT_EQ(3, [[menu menu] numberOfItems]); |
| 308 } | 308 } |
| 309 | 309 |
| 310 // Test that menus with dynamic labels actually get updated. | 310 // Test that menus with dynamic labels actually get updated. |
| 311 TEST_F(MenuControllerTest, Dynamic) { | 311 TEST_F(MenuControllerTest, Dynamic) { |
| 312 DynamicDelegate delegate; | 312 DynamicDelegate delegate; |
| 313 | 313 |
| 314 // Create a menu containing a single item whose label is "initial" and who has | 314 // Create a menu containing a single item whose label is "initial" and who has |
| 315 // no icon. | 315 // no icon. |
| 316 string16 initial = ASCIIToUTF16("initial"); | 316 base::string16 initial = ASCIIToUTF16("initial"); |
| 317 delegate.SetDynamicLabel(initial); | 317 delegate.SetDynamicLabel(initial); |
| 318 SimpleMenuModel model(&delegate); | 318 SimpleMenuModel model(&delegate); |
| 319 model.AddItem(1, ASCIIToUTF16("foo")); | 319 model.AddItem(1, ASCIIToUTF16("foo")); |
| 320 base::scoped_nsobject<MenuController> menu( | 320 base::scoped_nsobject<MenuController> menu( |
| 321 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); | 321 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); |
| 322 EXPECT_EQ([[menu menu] numberOfItems], 1); | 322 EXPECT_EQ([[menu menu] numberOfItems], 1); |
| 323 // Validate() simulates opening the menu - the item label/icon should be | 323 // Validate() simulates opening the menu - the item label/icon should be |
| 324 // initialized after this so we can validate the menu contents. | 324 // initialized after this so we can validate the menu contents. |
| 325 Validate(menu.get(), [menu menu]); | 325 Validate(menu.get(), [menu menu]); |
| 326 NSMenuItem* item = [[menu menu] itemAtIndex:0]; | 326 NSMenuItem* item = [[menu menu] itemAtIndex:0]; |
| 327 // Item should have the "initial" label and no icon. | 327 // Item should have the "initial" label and no icon. |
| 328 EXPECT_EQ(initial, base::SysNSStringToUTF16([item title])); | 328 EXPECT_EQ(initial, base::SysNSStringToUTF16([item title])); |
| 329 EXPECT_EQ(nil, [item image]); | 329 EXPECT_EQ(nil, [item image]); |
| 330 | 330 |
| 331 // Now update the item to have a label of "second" and an icon. | 331 // Now update the item to have a label of "second" and an icon. |
| 332 string16 second = ASCIIToUTF16("second"); | 332 base::string16 second = ASCIIToUTF16("second"); |
| 333 delegate.SetDynamicLabel(second); | 333 delegate.SetDynamicLabel(second); |
| 334 const gfx::Image& icon = | 334 const gfx::Image& icon = |
| 335 ResourceBundle::GetSharedInstance().GetNativeImageNamed(IDR_THROBBER); | 335 ResourceBundle::GetSharedInstance().GetNativeImageNamed(IDR_THROBBER); |
| 336 delegate.SetDynamicIcon(icon); | 336 delegate.SetDynamicIcon(icon); |
| 337 // Simulate opening the menu and validate that the item label + icon changes. | 337 // Simulate opening the menu and validate that the item label + icon changes. |
| 338 Validate(menu.get(), [menu menu]); | 338 Validate(menu.get(), [menu menu]); |
| 339 EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); | 339 EXPECT_EQ(second, base::SysNSStringToUTF16([item title])); |
| 340 EXPECT_TRUE([item image] != nil); | 340 EXPECT_TRUE([item image] != nil); |
| 341 | 341 |
| 342 // Now get rid of the icon and make sure it goes away. | 342 // Now get rid of the icon and make sure it goes away. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 // Pump the task that notifies the delegate. | 390 // Pump the task that notifies the delegate. |
| 391 message_loop.RunUntilIdle(); | 391 message_loop.RunUntilIdle(); |
| 392 | 392 |
| 393 // Expect that the delegate got notified properly. | 393 // Expect that the delegate got notified properly. |
| 394 EXPECT_TRUE(delegate.did_close_); | 394 EXPECT_TRUE(delegate.did_close_); |
| 395 } | 395 } |
| 396 | 396 |
| 397 } // namespace | 397 } // namespace |
| 398 | 398 |
| 399 } // namespace ui | 399 } // namespace ui |
| OLD | NEW |