| 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/mac/mac_util.h" |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
| 12 #import "ui/base/cocoa/menu_controller.h" | 13 #import "ui/base/cocoa/menu_controller.h" |
| 13 #include "ui/base/models/simple_menu_model.h" | 14 #include "ui/base/models/simple_menu_model.h" |
| 14 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/events/test/cocoa_test_event_utils.h" | 16 #include "ui/events/test/cocoa_test_event_utils.h" |
| 16 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
| 17 #import "ui/gfx/test/ui_cocoa_test_helper.h" | 18 #import "ui/gfx/test/ui_cocoa_test_helper.h" |
| 18 #include "ui/resources/grit/ui_resources.h" | 19 #include "ui/resources/grit/ui_resources.h" |
| 19 #include "ui/strings/grit/ui_strings.h" | 20 #include "ui/strings/grit/ui_strings.h" |
| 20 | 21 |
| 21 using base::ASCIIToUTF16; | 22 using base::ASCIIToUTF16; |
| 22 | 23 |
| 24 @interface TestResponsiveMenuController : MenuController |
| 25 @property(assign, nonatomic) BOOL processEarly; |
| 26 @property(assign, nonatomic) BOOL sawItemEarly; |
| 27 @end |
| 28 |
| 29 @implementation TestResponsiveMenuController { |
| 30 BOOL sawItemEarly_; |
| 31 BOOL processEarly_; |
| 32 } |
| 33 |
| 34 @synthesize sawItemEarly = sawItemEarly_; |
| 35 @synthesize processEarly = processEarly_; |
| 36 |
| 37 - (BOOL)processItemSelectedEarly:(id)sender { |
| 38 sawItemEarly_ = YES; |
| 39 if (!processEarly_) |
| 40 return NO; |
| 41 |
| 42 [self itemSelected:sender]; |
| 43 return YES; |
| 44 } |
| 45 |
| 46 @end |
| 47 |
| 48 @interface NSMenuItem (Private) |
| 49 // Exposed to simulate in testing. |
| 50 - (void)_sendItemSelectedNote; |
| 51 @end |
| 52 |
| 23 namespace ui { | 53 namespace ui { |
| 24 | 54 |
| 25 namespace { | 55 namespace { |
| 26 | 56 |
| 27 const int kTestLabelResourceId = IDS_APP_SCROLLBAR_CXMENU_SCROLLHERE; | 57 const int kTestLabelResourceId = IDS_APP_SCROLLBAR_CXMENU_SCROLLHERE; |
| 28 | 58 |
| 29 class MenuControllerTest : public CocoaTest { | 59 class MenuControllerTest : public CocoaTest { |
| 30 }; | 60 }; |
| 31 | 61 |
| 32 // A menu delegate that counts the number of times certain things are called | 62 // A menu delegate that counts the number of times certain things are called |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 // |did_close_| will remain false until we pump the task manually. | 416 // |did_close_| will remain false until we pump the task manually. |
| 387 EXPECT_FALSE(delegate.did_close_); | 417 EXPECT_FALSE(delegate.did_close_); |
| 388 | 418 |
| 389 // Pump the task that notifies the delegate. | 419 // Pump the task that notifies the delegate. |
| 390 base::RunLoop().RunUntilIdle(); | 420 base::RunLoop().RunUntilIdle(); |
| 391 | 421 |
| 392 // Expect that the delegate got notified properly. | 422 // Expect that the delegate got notified properly. |
| 393 EXPECT_TRUE(delegate.did_close_); | 423 EXPECT_TRUE(delegate.did_close_); |
| 394 } | 424 } |
| 395 | 425 |
| 426 // Verify that the private API used by MenuController's ResponsiveNSMenuItem |
| 427 // exists in the runtime. It's not a disaster if it disappears, (or AppKit |
| 428 // stops invoking it) but consumers will stop receiving opportunities to |
| 429 // -processItemSelectedEarly:. |
| 430 TEST_F(MenuControllerTest, SendItemSelectedNoteExists) { |
| 431 // -_sendItemSelectedNote doesn't exist on 10.9 or 10.10. NSPopUpButton menus |
| 432 // on 10.9 don't animate out, and always suffer from the brief "flash" of the |
| 433 // old selection when the menu disappears. |
| 434 // TODO(tapted): Find a hook on 10.10 if we deem it necessary. |
| 435 if (base::mac::IsAtMostOS10_10()) |
| 436 return; |
| 437 |
| 438 EXPECT_TRUE( |
| 439 [NSMenuItem instancesRespondToSelector:@selector(_sendItemSelectedNote)]); |
| 440 } |
| 441 |
| 442 // Emulate the flow for -[MenuController processItemSelectedEarly:]. |
| 443 TEST_F(MenuControllerTest, EmulateItemSelectedEarly) { |
| 444 if (![NSMenuItem instancesRespondToSelector:@selector(_sendItemSelectedNote)]) |
| 445 return; |
| 446 |
| 447 base::MessageLoopForUI message_loop; |
| 448 |
| 449 Delegate delegate; |
| 450 SimpleMenuModel model(&delegate); |
| 451 model.AddItem(1, ASCIIToUTF16("foo")); |
| 452 |
| 453 base::scoped_nsobject<TestResponsiveMenuController> controller( |
| 454 [[TestResponsiveMenuController alloc] initWithModel:&model |
| 455 useWithPopUpButtonCell:NO]); |
| 456 [controller setProcessEarly:YES]; |
| 457 |
| 458 NSMenuItem* item = [[controller menu] itemAtIndex:0]; |
| 459 EXPECT_TRUE(item); |
| 460 |
| 461 [controller menuWillOpen:[controller menu]]; |
| 462 |
| 463 // Pretend the first item got clicked. AppKit sends _sendItemSelectedNote to |
| 464 // the menu item, then performs its action. |
| 465 EXPECT_FALSE([controller sawItemEarly]); |
| 466 EXPECT_EQ(0, delegate.execute_count_); |
| 467 [item _sendItemSelectedNote]; |
| 468 |
| 469 EXPECT_TRUE([controller sawItemEarly]); |
| 470 |
| 471 // Item gets executed early. |
| 472 EXPECT_EQ(1, delegate.execute_count_); |
| 473 |
| 474 // Simulate dismissal. This happens before the action. |
| 475 [controller menuDidClose:[controller menu]]; |
| 476 |
| 477 // Perform the action normally. Shouldn't get executed again. |
| 478 [[item target] performSelector:[item action] withObject:item]; |
| 479 EXPECT_EQ(1, delegate.execute_count_); |
| 480 |
| 481 // Repeat, without processing early. |
| 482 [controller setProcessEarly:NO]; |
| 483 [controller setSawItemEarly:NO]; |
| 484 delegate.execute_count_ = 0; |
| 485 delegate.did_show_ = delegate.did_close_ = false; |
| 486 |
| 487 [controller menuWillOpen:[controller menu]]; |
| 488 [item _sendItemSelectedNote]; |
| 489 |
| 490 // Saw it, but didn't execute. |
| 491 EXPECT_TRUE([controller sawItemEarly]); |
| 492 EXPECT_EQ(0, delegate.execute_count_); |
| 493 |
| 494 [controller menuDidClose:[controller menu]]; |
| 495 |
| 496 // Perform the action normally. Now executes. |
| 497 [[item target] performSelector:[item action] withObject:item]; |
| 498 EXPECT_EQ(1, delegate.execute_count_); |
| 499 } |
| 500 |
| 396 } // namespace | 501 } // namespace |
| 397 | 502 |
| 398 } // namespace ui | 503 } // namespace ui |
| OLD | NEW |