| 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 "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 class Delegate : public SimpleMenuModel::Delegate { | 32 class Delegate : public SimpleMenuModel::Delegate { |
| 33 public: | 33 public: |
| 34 Delegate() | 34 Delegate() |
| 35 : execute_count_(0), | 35 : execute_count_(0), |
| 36 enable_count_(0), | 36 enable_count_(0), |
| 37 menu_to_close_(nil), | 37 menu_to_close_(nil), |
| 38 did_show_(false), | 38 did_show_(false), |
| 39 did_close_(false) { | 39 did_close_(false) { |
| 40 } | 40 } |
| 41 | 41 |
| 42 virtual bool IsCommandIdChecked(int command_id) const override { | 42 bool IsCommandIdChecked(int command_id) const override { return false; } |
| 43 return false; | 43 bool IsCommandIdEnabled(int command_id) const override { |
| 44 } | |
| 45 virtual bool IsCommandIdEnabled(int command_id) const override { | |
| 46 ++enable_count_; | 44 ++enable_count_; |
| 47 return true; | 45 return true; |
| 48 } | 46 } |
| 49 virtual bool GetAcceleratorForCommandId( | 47 bool GetAcceleratorForCommandId(int command_id, |
| 50 int command_id, | 48 Accelerator* accelerator) override { |
| 51 Accelerator* accelerator) override { return false; } | 49 return false; |
| 52 virtual void ExecuteCommand(int command_id, int event_flags) override { | 50 } |
| 51 void ExecuteCommand(int command_id, int event_flags) override { |
| 53 ++execute_count_; | 52 ++execute_count_; |
| 54 } | 53 } |
| 55 | 54 |
| 56 virtual void MenuWillShow(SimpleMenuModel* /*source*/) override { | 55 void MenuWillShow(SimpleMenuModel* /*source*/) override { |
| 57 EXPECT_FALSE(did_show_); | 56 EXPECT_FALSE(did_show_); |
| 58 EXPECT_FALSE(did_close_); | 57 EXPECT_FALSE(did_close_); |
| 59 did_show_ = true; | 58 did_show_ = true; |
| 60 NSArray* modes = [NSArray arrayWithObjects:NSEventTrackingRunLoopMode, | 59 NSArray* modes = [NSArray arrayWithObjects:NSEventTrackingRunLoopMode, |
| 61 NSDefaultRunLoopMode, | 60 NSDefaultRunLoopMode, |
| 62 nil]; | 61 nil]; |
| 63 [menu_to_close_ performSelector:@selector(cancelTracking) | 62 [menu_to_close_ performSelector:@selector(cancelTracking) |
| 64 withObject:nil | 63 withObject:nil |
| 65 afterDelay:0.1 | 64 afterDelay:0.1 |
| 66 inModes:modes]; | 65 inModes:modes]; |
| 67 } | 66 } |
| 68 | 67 |
| 69 virtual void MenuClosed(SimpleMenuModel* /*source*/) override { | 68 void MenuClosed(SimpleMenuModel* /*source*/) override { |
| 70 EXPECT_TRUE(did_show_); | 69 EXPECT_TRUE(did_show_); |
| 71 EXPECT_FALSE(did_close_); | 70 EXPECT_FALSE(did_close_); |
| 72 did_close_ = true; | 71 did_close_ = true; |
| 73 } | 72 } |
| 74 | 73 |
| 75 int execute_count_; | 74 int execute_count_; |
| 76 mutable int enable_count_; | 75 mutable int enable_count_; |
| 77 // The menu on which to call |-cancelTracking| after a short delay in | 76 // The menu on which to call |-cancelTracking| after a short delay in |
| 78 // MenuWillShow. | 77 // MenuWillShow. |
| 79 NSMenu* menu_to_close_; | 78 NSMenu* menu_to_close_; |
| 80 bool did_show_; | 79 bool did_show_; |
| 81 bool did_close_; | 80 bool did_close_; |
| 82 }; | 81 }; |
| 83 | 82 |
| 84 // Just like Delegate, except the items are treated as "dynamic" so updates to | 83 // Just like Delegate, except the items are treated as "dynamic" so updates to |
| 85 // the label/icon in the model are reflected in the menu. | 84 // the label/icon in the model are reflected in the menu. |
| 86 class DynamicDelegate : public Delegate { | 85 class DynamicDelegate : public Delegate { |
| 87 public: | 86 public: |
| 88 DynamicDelegate() {} | 87 DynamicDelegate() {} |
| 89 virtual bool IsItemForCommandIdDynamic(int command_id) const override { | 88 bool IsItemForCommandIdDynamic(int command_id) const override { return true; } |
| 90 return true; | 89 base::string16 GetLabelForCommandId(int command_id) const override { |
| 91 } | |
| 92 virtual base::string16 GetLabelForCommandId(int command_id) const override { | |
| 93 return label_; | 90 return label_; |
| 94 } | 91 } |
| 95 virtual bool GetIconForCommandId( | 92 bool GetIconForCommandId(int command_id, gfx::Image* icon) const override { |
| 96 int command_id, | |
| 97 gfx::Image* icon) const override { | |
| 98 if (icon_.IsEmpty()) { | 93 if (icon_.IsEmpty()) { |
| 99 return false; | 94 return false; |
| 100 } else { | 95 } else { |
| 101 *icon = icon_; | 96 *icon = icon_; |
| 102 return true; | 97 return true; |
| 103 } | 98 } |
| 104 } | 99 } |
| 105 void SetDynamicLabel(base::string16 label) { label_ = label; } | 100 void SetDynamicLabel(base::string16 label) { label_ = label; } |
| 106 void SetDynamicIcon(const gfx::Image& icon) { icon_ = icon; } | 101 void SetDynamicIcon(const gfx::Image& icon) { icon_ = icon; } |
| 107 | 102 |
| 108 private: | 103 private: |
| 109 base::string16 label_; | 104 base::string16 label_; |
| 110 gfx::Image icon_; | 105 gfx::Image icon_; |
| 111 }; | 106 }; |
| 112 | 107 |
| 113 // Menu model that returns a gfx::FontList object for one of the items in the | 108 // Menu model that returns a gfx::FontList object for one of the items in the |
| 114 // menu. | 109 // menu. |
| 115 class FontListMenuModel : public SimpleMenuModel { | 110 class FontListMenuModel : public SimpleMenuModel { |
| 116 public: | 111 public: |
| 117 FontListMenuModel(SimpleMenuModel::Delegate* delegate, | 112 FontListMenuModel(SimpleMenuModel::Delegate* delegate, |
| 118 const gfx::FontList* font_list, int index) | 113 const gfx::FontList* font_list, int index) |
| 119 : SimpleMenuModel(delegate), | 114 : SimpleMenuModel(delegate), |
| 120 font_list_(font_list), | 115 font_list_(font_list), |
| 121 index_(index) { | 116 index_(index) { |
| 122 } | 117 } |
| 123 virtual ~FontListMenuModel() {} | 118 ~FontListMenuModel() override {} |
| 124 virtual const gfx::FontList* GetLabelFontListAt(int index) const override { | 119 const gfx::FontList* GetLabelFontListAt(int index) const override { |
| 125 return (index == index_) ? font_list_ : NULL; | 120 return (index == index_) ? font_list_ : NULL; |
| 126 } | 121 } |
| 127 | 122 |
| 128 private: | 123 private: |
| 129 const gfx::FontList* font_list_; | 124 const gfx::FontList* font_list_; |
| 130 const int index_; | 125 const int index_; |
| 131 }; | 126 }; |
| 132 | 127 |
| 133 TEST_F(MenuControllerTest, EmptyMenu) { | 128 TEST_F(MenuControllerTest, EmptyMenu) { |
| 134 Delegate delegate; | 129 Delegate delegate; |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 // Pump the task that notifies the delegate. | 389 // Pump the task that notifies the delegate. |
| 395 message_loop.RunUntilIdle(); | 390 message_loop.RunUntilIdle(); |
| 396 | 391 |
| 397 // Expect that the delegate got notified properly. | 392 // Expect that the delegate got notified properly. |
| 398 EXPECT_TRUE(delegate.did_close_); | 393 EXPECT_TRUE(delegate.did_close_); |
| 399 } | 394 } |
| 400 | 395 |
| 401 } // namespace | 396 } // namespace |
| 402 | 397 |
| 403 } // namespace ui | 398 } // namespace ui |
| OLD | NEW |