| 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 #include "base/callback.h" | 5 #include "base/callback.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/test/base/interactive_test_utils.h" | 7 #include "chrome/test/base/interactive_test_utils.h" |
| 8 #include "chrome/test/base/ui_test_utils.h" | 8 #include "chrome/test/base/ui_test_utils.h" |
| 9 #include "chrome/test/base/view_event_test_base.h" | 9 #include "chrome/test/base/view_event_test_base.h" |
| 10 #include "ui/base/models/menu_model.h" | 10 #include "ui/base/models/menu_model.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // Exceptions: | 29 // Exceptions: |
| 30 // virtual int GetItemCount() const = 0; | 30 // virtual int GetItemCount() const = 0; |
| 31 // virtual ItemType GetTypeAt(int index) const = 0; | 31 // virtual ItemType GetTypeAt(int index) const = 0; |
| 32 // virtual int GetCommandIdAt(int index) const = 0; | 32 // virtual int GetCommandIdAt(int index) const = 0; |
| 33 // virtual base::string16 GetLabelAt(int index) const = 0; | 33 // virtual base::string16 GetLabelAt(int index) const = 0; |
| 34 class CommonMenuModel : public ui::MenuModel { | 34 class CommonMenuModel : public ui::MenuModel { |
| 35 public: | 35 public: |
| 36 CommonMenuModel() { | 36 CommonMenuModel() { |
| 37 } | 37 } |
| 38 | 38 |
| 39 virtual ~CommonMenuModel() { | 39 ~CommonMenuModel() override {} |
| 40 } | |
| 41 | 40 |
| 42 protected: | 41 protected: |
| 43 // ui::MenuModel implementation. | 42 // ui::MenuModel implementation. |
| 44 virtual bool HasIcons() const override { | 43 bool HasIcons() const override { return false; } |
| 44 |
| 45 bool IsItemDynamicAt(int index) const override { return false; } |
| 46 |
| 47 bool GetAcceleratorAt(int index, |
| 48 ui::Accelerator* accelerator) const override { |
| 45 return false; | 49 return false; |
| 46 } | 50 } |
| 47 | 51 |
| 48 virtual bool IsItemDynamicAt(int index) const override { | 52 ui::MenuSeparatorType GetSeparatorTypeAt(int index) const override { |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 virtual bool GetAcceleratorAt(int index, | |
| 53 ui::Accelerator* accelerator) const override { | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 virtual ui::MenuSeparatorType GetSeparatorTypeAt(int index) const override { | |
| 58 return ui::NORMAL_SEPARATOR; | 53 return ui::NORMAL_SEPARATOR; |
| 59 } | 54 } |
| 60 | 55 |
| 61 virtual bool IsItemCheckedAt(int index) const override { | 56 bool IsItemCheckedAt(int index) const override { return false; } |
| 62 return false; | |
| 63 } | |
| 64 | 57 |
| 65 virtual int GetGroupIdAt(int index) const override { | 58 int GetGroupIdAt(int index) const override { return 0; } |
| 66 return 0; | |
| 67 } | |
| 68 | 59 |
| 69 virtual bool GetIconAt(int index, gfx::Image* icon) override { | 60 bool GetIconAt(int index, gfx::Image* icon) override { return false; } |
| 70 return false; | |
| 71 } | |
| 72 | 61 |
| 73 virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt( | 62 ui::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const override { |
| 74 int index) const override { | |
| 75 return NULL; | 63 return NULL; |
| 76 } | 64 } |
| 77 | 65 |
| 78 virtual bool IsEnabledAt(int index) const override { | 66 bool IsEnabledAt(int index) const override { return true; } |
| 79 return true; | |
| 80 } | |
| 81 | 67 |
| 82 virtual ui::MenuModel* GetSubmenuModelAt(int index) const override { | 68 ui::MenuModel* GetSubmenuModelAt(int index) const override { return NULL; } |
| 83 return NULL; | |
| 84 } | |
| 85 | 69 |
| 86 virtual void HighlightChangedTo(int index) override { | 70 void HighlightChangedTo(int index) override {} |
| 87 } | |
| 88 | 71 |
| 89 virtual void ActivatedAt(int index) override { | 72 void ActivatedAt(int index) override {} |
| 90 } | |
| 91 | 73 |
| 92 virtual void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) override { | 74 void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) override {} |
| 93 } | |
| 94 | 75 |
| 95 virtual ui::MenuModelDelegate* GetMenuModelDelegate() const override { | 76 ui::MenuModelDelegate* GetMenuModelDelegate() const override { return NULL; } |
| 96 return NULL; | |
| 97 } | |
| 98 | 77 |
| 99 private: | 78 private: |
| 100 DISALLOW_COPY_AND_ASSIGN(CommonMenuModel); | 79 DISALLOW_COPY_AND_ASSIGN(CommonMenuModel); |
| 101 }; | 80 }; |
| 102 | 81 |
| 103 class SubMenuModel : public CommonMenuModel { | 82 class SubMenuModel : public CommonMenuModel { |
| 104 public: | 83 public: |
| 105 SubMenuModel() | 84 SubMenuModel() |
| 106 : showing_(false) { | 85 : showing_(false) { |
| 107 } | 86 } |
| 108 | 87 |
| 109 virtual ~SubMenuModel() { | 88 ~SubMenuModel() override {} |
| 110 } | |
| 111 | 89 |
| 112 bool showing() const { | 90 bool showing() const { |
| 113 return showing_; | 91 return showing_; |
| 114 } | 92 } |
| 115 | 93 |
| 116 private: | 94 private: |
| 117 // ui::MenuModel implementation. | 95 // ui::MenuModel implementation. |
| 118 virtual int GetItemCount() const override { | 96 int GetItemCount() const override { return 1; } |
| 119 return 1; | |
| 120 } | |
| 121 | 97 |
| 122 virtual ItemType GetTypeAt(int index) const override { | 98 ItemType GetTypeAt(int index) const override { return TYPE_COMMAND; } |
| 123 return TYPE_COMMAND; | |
| 124 } | |
| 125 | 99 |
| 126 virtual int GetCommandIdAt(int index) const override { | 100 int GetCommandIdAt(int index) const override { |
| 127 return index + kSubMenuBaseId; | 101 return index + kSubMenuBaseId; |
| 128 } | 102 } |
| 129 | 103 |
| 130 virtual base::string16 GetLabelAt(int index) const override { | 104 base::string16 GetLabelAt(int index) const override { |
| 131 return base::ASCIIToUTF16("Item"); | 105 return base::ASCIIToUTF16("Item"); |
| 132 } | 106 } |
| 133 | 107 |
| 134 virtual void MenuWillShow() override { | 108 void MenuWillShow() override { showing_ = true; } |
| 135 showing_ = true; | |
| 136 } | |
| 137 | 109 |
| 138 // Called when the menu has been closed. | 110 // Called when the menu has been closed. |
| 139 virtual void MenuClosed() override { | 111 void MenuClosed() override { showing_ = false; } |
| 140 showing_ = false; | |
| 141 } | |
| 142 | 112 |
| 143 bool showing_; | 113 bool showing_; |
| 144 | 114 |
| 145 DISALLOW_COPY_AND_ASSIGN(SubMenuModel); | 115 DISALLOW_COPY_AND_ASSIGN(SubMenuModel); |
| 146 }; | 116 }; |
| 147 | 117 |
| 148 class TopMenuModel : public CommonMenuModel { | 118 class TopMenuModel : public CommonMenuModel { |
| 149 public: | 119 public: |
| 150 TopMenuModel() { | 120 TopMenuModel() { |
| 151 } | 121 } |
| 152 | 122 |
| 153 virtual ~TopMenuModel() { | 123 ~TopMenuModel() override {} |
| 154 } | |
| 155 | 124 |
| 156 bool IsSubmenuShowing() { | 125 bool IsSubmenuShowing() { |
| 157 return sub_menu_model_.showing(); | 126 return sub_menu_model_.showing(); |
| 158 } | 127 } |
| 159 | 128 |
| 160 private: | 129 private: |
| 161 // ui::MenuModel implementation. | 130 // ui::MenuModel implementation. |
| 162 virtual int GetItemCount() const override { | 131 int GetItemCount() const override { return 1; } |
| 163 return 1; | |
| 164 } | |
| 165 | 132 |
| 166 virtual ItemType GetTypeAt(int index) const override { | 133 ItemType GetTypeAt(int index) const override { return TYPE_SUBMENU; } |
| 167 return TYPE_SUBMENU; | |
| 168 } | |
| 169 | 134 |
| 170 virtual int GetCommandIdAt(int index) const override { | 135 int GetCommandIdAt(int index) const override { |
| 171 return index + kTopMenuBaseId; | 136 return index + kTopMenuBaseId; |
| 172 } | 137 } |
| 173 | 138 |
| 174 virtual base::string16 GetLabelAt(int index) const override { | 139 base::string16 GetLabelAt(int index) const override { |
| 175 return base::ASCIIToUTF16("submenu"); | 140 return base::ASCIIToUTF16("submenu"); |
| 176 } | 141 } |
| 177 | 142 |
| 178 virtual MenuModel* GetSubmenuModelAt(int index) const override { | 143 MenuModel* GetSubmenuModelAt(int index) const override { |
| 179 return &sub_menu_model_; | 144 return &sub_menu_model_; |
| 180 } | 145 } |
| 181 | 146 |
| 182 mutable SubMenuModel sub_menu_model_; | 147 mutable SubMenuModel sub_menu_model_; |
| 183 | 148 |
| 184 DISALLOW_COPY_AND_ASSIGN(TopMenuModel); | 149 DISALLOW_COPY_AND_ASSIGN(TopMenuModel); |
| 185 }; | 150 }; |
| 186 | 151 |
| 187 } // namespace | 152 } // namespace |
| 188 | 153 |
| 189 class MenuModelAdapterTest : public ViewEventTestBase, | 154 class MenuModelAdapterTest : public ViewEventTestBase, |
| 190 public views::MenuButtonListener { | 155 public views::MenuButtonListener { |
| 191 public: | 156 public: |
| 192 MenuModelAdapterTest() | 157 MenuModelAdapterTest() |
| 193 : ViewEventTestBase(), | 158 : ViewEventTestBase(), |
| 194 button_(NULL), | 159 button_(NULL), |
| 195 menu_model_adapter_(&top_menu_model_), | 160 menu_model_adapter_(&top_menu_model_), |
| 196 menu_(NULL) { | 161 menu_(NULL) { |
| 197 } | 162 } |
| 198 | 163 |
| 199 virtual ~MenuModelAdapterTest() { | 164 ~MenuModelAdapterTest() override {} |
| 200 } | |
| 201 | 165 |
| 202 // ViewEventTestBase implementation. | 166 // ViewEventTestBase implementation. |
| 203 | 167 |
| 204 virtual void SetUp() override { | 168 void SetUp() override { |
| 205 button_ = new views::MenuButton( | 169 button_ = new views::MenuButton( |
| 206 NULL, base::ASCIIToUTF16("Menu Adapter Test"), this, true); | 170 NULL, base::ASCIIToUTF16("Menu Adapter Test"), this, true); |
| 207 | 171 |
| 208 menu_ = menu_model_adapter_.CreateMenu(); | 172 menu_ = menu_model_adapter_.CreateMenu(); |
| 209 menu_runner_.reset( | 173 menu_runner_.reset( |
| 210 new views::MenuRunner(menu_, views::MenuRunner::HAS_MNEMONICS)); | 174 new views::MenuRunner(menu_, views::MenuRunner::HAS_MNEMONICS)); |
| 211 | 175 |
| 212 ViewEventTestBase::SetUp(); | 176 ViewEventTestBase::SetUp(); |
| 213 } | 177 } |
| 214 | 178 |
| 215 virtual void TearDown() override { | 179 void TearDown() override { |
| 216 menu_runner_.reset(NULL); | 180 menu_runner_.reset(NULL); |
| 217 menu_ = NULL; | 181 menu_ = NULL; |
| 218 ViewEventTestBase::TearDown(); | 182 ViewEventTestBase::TearDown(); |
| 219 } | 183 } |
| 220 | 184 |
| 221 virtual views::View* CreateContentsView() override { | 185 views::View* CreateContentsView() override { return button_; } |
| 222 return button_; | |
| 223 } | |
| 224 | 186 |
| 225 virtual gfx::Size GetPreferredSize() const override { | 187 gfx::Size GetPreferredSize() const override { |
| 226 return button_->GetPreferredSize(); | 188 return button_->GetPreferredSize(); |
| 227 } | 189 } |
| 228 | 190 |
| 229 // views::MenuButtonListener implementation. | 191 // views::MenuButtonListener implementation. |
| 230 virtual void OnMenuButtonClicked(views::View* source, | 192 void OnMenuButtonClicked(views::View* source, |
| 231 const gfx::Point& point) override { | 193 const gfx::Point& point) override { |
| 232 gfx::Point screen_location; | 194 gfx::Point screen_location; |
| 233 views::View::ConvertPointToScreen(source, &screen_location); | 195 views::View::ConvertPointToScreen(source, &screen_location); |
| 234 gfx::Rect bounds(screen_location, source->size()); | 196 gfx::Rect bounds(screen_location, source->size()); |
| 235 ignore_result(menu_runner_->RunMenuAt(source->GetWidget(), | 197 ignore_result(menu_runner_->RunMenuAt(source->GetWidget(), |
| 236 button_, | 198 button_, |
| 237 bounds, | 199 bounds, |
| 238 views::MENU_ANCHOR_TOPLEFT, | 200 views::MENU_ANCHOR_TOPLEFT, |
| 239 ui::MENU_SOURCE_NONE)); | 201 ui::MENU_SOURCE_NONE)); |
| 240 } | 202 } |
| 241 | 203 |
| 242 // ViewEventTestBase implementation | 204 // ViewEventTestBase implementation |
| 243 virtual void DoTestOnMessageLoop() override { | 205 void DoTestOnMessageLoop() override { |
| 244 Click(button_, CreateEventTask(this, &MenuModelAdapterTest::Step1)); | 206 Click(button_, CreateEventTask(this, &MenuModelAdapterTest::Step1)); |
| 245 } | 207 } |
| 246 | 208 |
| 247 // Open the submenu. | 209 // Open the submenu. |
| 248 void Step1() { | 210 void Step1() { |
| 249 views::SubmenuView* topmenu = menu_->GetSubmenu(); | 211 views::SubmenuView* topmenu = menu_->GetSubmenu(); |
| 250 ASSERT_TRUE(topmenu); | 212 ASSERT_TRUE(topmenu); |
| 251 ASSERT_TRUE(topmenu->IsShowing()); | 213 ASSERT_TRUE(topmenu->IsShowing()); |
| 252 ASSERT_FALSE(top_menu_model_.IsSubmenuShowing()); | 214 ASSERT_FALSE(top_menu_model_.IsSubmenuShowing()); |
| 253 | 215 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } | 265 } |
| 304 | 266 |
| 305 views::MenuButton* button_; | 267 views::MenuButton* button_; |
| 306 TopMenuModel top_menu_model_; | 268 TopMenuModel top_menu_model_; |
| 307 views::MenuModelAdapter menu_model_adapter_; | 269 views::MenuModelAdapter menu_model_adapter_; |
| 308 views::MenuItemView* menu_; | 270 views::MenuItemView* menu_; |
| 309 scoped_ptr<views::MenuRunner> menu_runner_; | 271 scoped_ptr<views::MenuRunner> menu_runner_; |
| 310 }; | 272 }; |
| 311 | 273 |
| 312 VIEW_TEST(MenuModelAdapterTest, RebuildMenu) | 274 VIEW_TEST(MenuModelAdapterTest, RebuildMenu) |
| OLD | NEW |