OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/automation/ui_controls.h" |
| 6 #include "chrome/test/interactive_ui/view_event_test_base.h" |
| 7 #include "ui/base/models/menu_model.h" |
| 8 #include "views/controls/button/menu_button.h" |
| 9 #include "views/controls/menu/menu_controller.h" |
| 10 #include "views/controls/menu/menu_item_view.h" |
| 11 #include "views/controls/menu/menu_model_adapter.h" |
| 12 #include "views/controls/menu/submenu_view.h" |
| 13 #include "views/controls/menu/view_menu_delegate.h" |
| 14 #include "views/test/test_views_delegate.h" |
| 15 #include "views/views_delegate.h" |
| 16 #include "views/widget/root_view.h" |
| 17 #include "views/widget/widget.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 const int kTopMenuBaseId = 100; |
| 22 const int kSubMenuBaseId = 200; |
| 23 |
| 24 // ViewsDelegate::GetDispositionForEvent() is used by views::MenuModelAdapter. |
| 25 class TestViewsDelegate : public views::ViewsDelegate { |
| 26 public: |
| 27 TestViewsDelegate() { |
| 28 } |
| 29 |
| 30 ~TestViewsDelegate() { |
| 31 } |
| 32 |
| 33 // views::ViewsDelegate implementation |
| 34 virtual ui::Clipboard* GetClipboard() const OVERRIDE { |
| 35 return NULL; |
| 36 } |
| 37 |
| 38 virtual views::View* GetDefaultParentView() OVERRIDE |
| 39 { |
| 40 return NULL; |
| 41 } |
| 42 |
| 43 virtual void SaveWindowPlacement(const views::Widget* widget, |
| 44 const std::wstring& window_name, |
| 45 const gfx::Rect& bounds, |
| 46 bool maximized) OVERRIDE { |
| 47 } |
| 48 |
| 49 virtual bool GetSavedWindowBounds(const std::wstring& window_name, |
| 50 gfx::Rect* bounds) const OVERRIDE { |
| 51 return false; |
| 52 } |
| 53 |
| 54 virtual bool GetSavedMaximizedState(const std::wstring& window_name, |
| 55 bool* maximized) const OVERRIDE { |
| 56 return false; |
| 57 } |
| 58 |
| 59 virtual void NotifyAccessibilityEvent( |
| 60 views::View* view, ui::AccessibilityTypes::Event event_type) OVERRIDE { |
| 61 } |
| 62 |
| 63 virtual void NotifyMenuItemFocused( |
| 64 const std::wstring& menu_name, |
| 65 const std::wstring& menu_item_name, |
| 66 int item_index, |
| 67 int item_count, |
| 68 bool has_submenu) OVERRIDE { |
| 69 } |
| 70 |
| 71 #if defined(OS_WIN) |
| 72 virtual HICON GetDefaultWindowIcon() const OVERRIDE |
| 73 { |
| 74 return NULL; |
| 75 } |
| 76 #endif |
| 77 |
| 78 virtual void AddRef() OVERRIDE { |
| 79 } |
| 80 |
| 81 virtual void ReleaseRef() OVERRIDE { |
| 82 } |
| 83 |
| 84 // Converts views::Event::flags to a WindowOpenDisposition. |
| 85 virtual int GetDispositionForEvent(int event_flags) OVERRIDE { |
| 86 return 0; |
| 87 } |
| 88 |
| 89 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(TestViewsDelegate); |
| 91 }; |
| 92 |
| 93 // Implement most of the ui::MenuModel pure virtuals for subclasses |
| 94 // |
| 95 // Exceptions: |
| 96 // virtual int GetItemCount() const = 0; |
| 97 // virtual ItemType GetTypeAt(int index) const = 0; |
| 98 // virtual int GetCommandIdAt(int index) const = 0; |
| 99 // virtual string16 GetLabelAt(int index) const = 0; |
| 100 class CommonMenuModel : public ui::MenuModel { |
| 101 public: |
| 102 CommonMenuModel() { |
| 103 } |
| 104 |
| 105 virtual ~CommonMenuModel() { |
| 106 } |
| 107 |
| 108 protected: |
| 109 // ui::MenuModel implementation. |
| 110 virtual bool HasIcons() const OVERRIDE { |
| 111 return false; |
| 112 } |
| 113 |
| 114 virtual bool IsItemDynamicAt(int index) const OVERRIDE { |
| 115 return false; |
| 116 } |
| 117 |
| 118 virtual bool GetAcceleratorAt(int index, |
| 119 ui::Accelerator* accelerator) const OVERRIDE { |
| 120 return false; |
| 121 } |
| 122 |
| 123 virtual bool IsItemCheckedAt(int index) const OVERRIDE { |
| 124 return false; |
| 125 } |
| 126 |
| 127 virtual int GetGroupIdAt(int index) const OVERRIDE { |
| 128 return 0; |
| 129 } |
| 130 |
| 131 virtual bool GetIconAt(int index, SkBitmap* icon) OVERRIDE { |
| 132 return false; |
| 133 } |
| 134 |
| 135 virtual ui::ButtonMenuItemModel* GetButtonMenuItemAt( |
| 136 int index) const OVERRIDE { |
| 137 return NULL; |
| 138 } |
| 139 |
| 140 virtual bool IsEnabledAt(int index) const OVERRIDE { |
| 141 return true; |
| 142 } |
| 143 |
| 144 virtual ui::MenuModel* GetSubmenuModelAt(int index) const OVERRIDE { |
| 145 return NULL; |
| 146 } |
| 147 |
| 148 virtual void HighlightChangedTo(int index) OVERRIDE { |
| 149 } |
| 150 |
| 151 virtual void ActivatedAt(int index) OVERRIDE { |
| 152 } |
| 153 |
| 154 virtual void SetMenuModelDelegate(ui::MenuModelDelegate* delegate) OVERRIDE { |
| 155 } |
| 156 |
| 157 private: |
| 158 DISALLOW_COPY_AND_ASSIGN(CommonMenuModel); |
| 159 }; |
| 160 |
| 161 class SubMenuModel : public CommonMenuModel { |
| 162 public: |
| 163 SubMenuModel() |
| 164 : showing_(false) { |
| 165 } |
| 166 |
| 167 ~SubMenuModel() { |
| 168 } |
| 169 |
| 170 bool showing() const { |
| 171 return showing_; |
| 172 } |
| 173 |
| 174 private: |
| 175 // ui::MenuModel implementation. |
| 176 virtual int GetItemCount() const OVERRIDE { |
| 177 return 1; |
| 178 } |
| 179 |
| 180 virtual ItemType GetTypeAt(int index) const OVERRIDE { |
| 181 return TYPE_COMMAND; |
| 182 } |
| 183 |
| 184 virtual int GetCommandIdAt(int index) const OVERRIDE { |
| 185 return index + kSubMenuBaseId; |
| 186 } |
| 187 |
| 188 virtual string16 GetLabelAt(int index) const OVERRIDE { |
| 189 return ASCIIToUTF16("Item"); |
| 190 } |
| 191 |
| 192 virtual void MenuWillShow() { |
| 193 showing_ = true; |
| 194 } |
| 195 |
| 196 // Called when the menu has been closed. |
| 197 virtual void MenuClosed() { |
| 198 showing_ = false; |
| 199 } |
| 200 |
| 201 bool showing_; |
| 202 |
| 203 DISALLOW_COPY_AND_ASSIGN(SubMenuModel); |
| 204 }; |
| 205 |
| 206 class TopMenuModel : public CommonMenuModel { |
| 207 public: |
| 208 TopMenuModel() { |
| 209 } |
| 210 |
| 211 ~TopMenuModel() { |
| 212 } |
| 213 |
| 214 bool IsSubmenuShowing() { |
| 215 return sub_menu_model_.showing(); |
| 216 } |
| 217 |
| 218 private: |
| 219 // ui::MenuModel implementation. |
| 220 virtual int GetItemCount() const OVERRIDE { |
| 221 return 1; |
| 222 } |
| 223 |
| 224 virtual ItemType GetTypeAt(int index) const OVERRIDE { |
| 225 return TYPE_SUBMENU; |
| 226 } |
| 227 |
| 228 virtual int GetCommandIdAt(int index) const OVERRIDE { |
| 229 return index + kTopMenuBaseId; |
| 230 } |
| 231 |
| 232 virtual string16 GetLabelAt(int index) const OVERRIDE { |
| 233 return ASCIIToUTF16("submenu"); |
| 234 } |
| 235 |
| 236 virtual MenuModel* GetSubmenuModelAt(int index) const OVERRIDE { |
| 237 return &sub_menu_model_; |
| 238 } |
| 239 |
| 240 mutable SubMenuModel sub_menu_model_; |
| 241 |
| 242 DISALLOW_COPY_AND_ASSIGN(TopMenuModel); |
| 243 }; |
| 244 |
| 245 } // namespace |
| 246 |
| 247 class MenuModelAdapterTest : public ViewEventTestBase, |
| 248 public views::ViewMenuDelegate { |
| 249 public: |
| 250 MenuModelAdapterTest() : |
| 251 ViewEventTestBase(), |
| 252 button_(NULL), |
| 253 menu_model_adapter_(&top_menu_model_) { |
| 254 old_views_delegate_ = views::ViewsDelegate::views_delegate; |
| 255 views::ViewsDelegate::views_delegate = &views_delegate_; |
| 256 } |
| 257 |
| 258 virtual ~MenuModelAdapterTest() { |
| 259 views::ViewsDelegate::views_delegate = old_views_delegate_; |
| 260 } |
| 261 |
| 262 // ViewEventTestBase implementation. |
| 263 |
| 264 virtual void SetUp() OVERRIDE { |
| 265 button_ = new views::MenuButton(NULL, L"Menu Adapter Test", this, true); |
| 266 |
| 267 menu_.reset(new views::MenuItemView(&menu_model_adapter_)); |
| 268 menu_model_adapter_.BuildMenu(menu_.get()); |
| 269 |
| 270 ViewEventTestBase::SetUp(); |
| 271 } |
| 272 |
| 273 virtual void TearDown() OVERRIDE { |
| 274 menu_.reset(NULL); |
| 275 ViewEventTestBase::TearDown(); |
| 276 } |
| 277 |
| 278 virtual views::View* CreateContentsView() OVERRIDE { |
| 279 return button_; |
| 280 } |
| 281 |
| 282 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 283 return button_->GetPreferredSize(); |
| 284 } |
| 285 |
| 286 // views::ViewMenuDelegate implementation. |
| 287 virtual void RunMenu(views::View* source, const gfx::Point& pt) OVERRIDE { |
| 288 gfx::Point screen_location; |
| 289 views::View::ConvertPointToScreen(source, &screen_location); |
| 290 gfx::Rect bounds(screen_location, source->size()); |
| 291 menu_->RunMenuAt( |
| 292 source->GetWidget(), |
| 293 button_, |
| 294 bounds, |
| 295 views::MenuItemView::TOPLEFT, |
| 296 true); |
| 297 } |
| 298 |
| 299 // ViewEventTestBase implementation |
| 300 virtual void DoTestOnMessageLoop() OVERRIDE { |
| 301 Click(button_, CreateEventTask(this, &MenuModelAdapterTest::Step1)); |
| 302 } |
| 303 |
| 304 // Open the submenu. |
| 305 void Step1() { |
| 306 views::SubmenuView* topmenu = menu_->GetSubmenu(); |
| 307 ASSERT_TRUE(topmenu); |
| 308 ASSERT_TRUE(topmenu->IsShowing()); |
| 309 ASSERT_FALSE(top_menu_model_.IsSubmenuShowing()); |
| 310 |
| 311 // Click the first item to open the submenu. |
| 312 views::MenuItemView* item = topmenu->GetMenuItemAt(0); |
| 313 ASSERT_TRUE(item); |
| 314 Click(item, CreateEventTask(this, &MenuModelAdapterTest::Step2)); |
| 315 } |
| 316 |
| 317 // Rebuild the menu which should close the submenu. |
| 318 void Step2() { |
| 319 views::SubmenuView* topmenu = menu_->GetSubmenu(); |
| 320 ASSERT_TRUE(topmenu); |
| 321 ASSERT_TRUE(topmenu->IsShowing()); |
| 322 ASSERT_TRUE(top_menu_model_.IsSubmenuShowing()); |
| 323 |
| 324 menu_model_adapter_.BuildMenu(menu_.get()); |
| 325 |
| 326 MessageLoopForUI::current()->PostTask( |
| 327 FROM_HERE, |
| 328 CreateEventTask(this, &MenuModelAdapterTest::Step3)); |
| 329 } |
| 330 |
| 331 // Verify that the submenu MenuModel received the close callback |
| 332 // and close the menu. |
| 333 void Step3() { |
| 334 views::SubmenuView* topmenu = menu_->GetSubmenu(); |
| 335 ASSERT_TRUE(topmenu); |
| 336 ASSERT_TRUE(topmenu->IsShowing()); |
| 337 ASSERT_FALSE(top_menu_model_.IsSubmenuShowing()); |
| 338 |
| 339 // Click the button to exit the menu. |
| 340 Click(button_, CreateEventTask(this, &MenuModelAdapterTest::Step4)); |
| 341 } |
| 342 |
| 343 // All done. |
| 344 void Step4() { |
| 345 views::SubmenuView* topmenu = menu_->GetSubmenu(); |
| 346 ASSERT_TRUE(topmenu); |
| 347 ASSERT_FALSE(topmenu->IsShowing()); |
| 348 ASSERT_FALSE(top_menu_model_.IsSubmenuShowing()); |
| 349 |
| 350 Done(); |
| 351 } |
| 352 |
| 353 private: |
| 354 // Generate a mouse click on the specified view and post a new task. |
| 355 virtual void Click(views::View* view, Task* next) { |
| 356 ui_controls::MoveMouseToCenterAndPress( |
| 357 view, |
| 358 ui_controls::LEFT, |
| 359 ui_controls::DOWN | ui_controls::UP, |
| 360 next); |
| 361 } |
| 362 |
| 363 views::ViewsDelegate* old_views_delegate_; |
| 364 TestViewsDelegate views_delegate_; |
| 365 |
| 366 views::MenuButton* button_; |
| 367 TopMenuModel top_menu_model_; |
| 368 views::MenuModelAdapter menu_model_adapter_; |
| 369 scoped_ptr<views::MenuItemView> menu_; |
| 370 }; |
| 371 |
| 372 VIEW_TEST(MenuModelAdapterTest, RebuildMenu) |
OLD | NEW |