Chromium Code Reviews| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/scoped_temp_dir.h" | 11 #include "base/scoped_temp_dir.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/extensions/event_names.h" | 14 #include "chrome/browser/extensions/event_names.h" |
| 15 #include "chrome/browser/extensions/event_router.h" | 15 #include "chrome/browser/extensions/event_router.h" |
| 16 #include "chrome/browser/extensions/extension_system_factory.h" | |
| 16 #include "chrome/browser/extensions/menu_manager.h" | 17 #include "chrome/browser/extensions/menu_manager.h" |
| 17 #include "chrome/browser/extensions/test_extension_prefs.h" | 18 #include "chrome/browser/extensions/test_extension_prefs.h" |
| 19 #include "chrome/browser/extensions/test_extension_system.h" | |
| 18 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/common/extensions/extension.h" | 22 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/extensions/extension_constants.h" | 23 #include "chrome/common/extensions/extension_constants.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 23 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/common/context_menu_params.h" | 26 #include "content/public/common/context_menu_params.h" |
| 25 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 const GURL& event_url, | 456 const GURL& event_url, |
| 455 EventRouter::UserGestureState state) { | 457 EventRouter::UserGestureState state) { |
| 456 DispatchEventToExtensionMock(extension_id, event_name, event_args.release(), | 458 DispatchEventToExtensionMock(extension_id, event_name, event_args.release(), |
| 457 source_profile, event_url, state); | 459 source_profile, event_url, state); |
| 458 } | 460 } |
| 459 | 461 |
| 460 private: | 462 private: |
| 461 DISALLOW_COPY_AND_ASSIGN(MockEventRouter); | 463 DISALLOW_COPY_AND_ASSIGN(MockEventRouter); |
| 462 }; | 464 }; |
| 463 | 465 |
| 466 // A mock ExtensionSystem to serve our MockEventRouter. | |
| 467 class MockExtensionSystem : public TestExtensionSystem { | |
| 468 public: | |
| 469 explicit MockExtensionSystem(Profile* profile) | |
| 470 : TestExtensionSystem(profile) {} | |
| 471 | |
| 472 virtual EventRouter* event_router() { | |
| 473 if (!mock_event_router_.get()) | |
| 474 mock_event_router_.reset(new MockEventRouter(profile_)); | |
| 475 return mock_event_router_.get(); | |
| 476 } | |
| 477 | |
| 478 private: | |
| 479 scoped_ptr<MockEventRouter> mock_event_router_; | |
| 480 | |
| 481 DISALLOW_COPY_AND_ASSIGN(MockExtensionSystem); | |
| 482 }; | |
| 483 | |
| 484 ProfileKeyedService* BuildMockExtensionSystem(Profile* profile) { | |
| 485 return new MockExtensionSystem(profile); | |
| 486 } | |
| 487 | |
| 464 // A mock profile for tests of MenuManager::ExecuteCommand. | 488 // A mock profile for tests of MenuManager::ExecuteCommand. |
| 465 class MockTestingProfile : public TestingProfile { | 489 class MockTestingProfile : public TestingProfile { |
|
Yoyo Zhou
2012/10/25 22:24:47
Since this is no longer interesting, you can repla
Miranda Callahan
2012/10/26 19:23:01
Done.
| |
| 466 public: | 490 public: |
| 467 MockTestingProfile() {} | 491 MockTestingProfile() {} |
| 468 MOCK_METHOD0(GetExtensionEventRouter, EventRouter*()); | |
| 469 | 492 |
| 470 private: | 493 private: |
| 471 DISALLOW_COPY_AND_ASSIGN(MockTestingProfile); | 494 DISALLOW_COPY_AND_ASSIGN(MockTestingProfile); |
| 472 }; | 495 }; |
| 473 | 496 |
| 474 // Tests the RemoveAll functionality. | 497 // Tests the RemoveAll functionality. |
| 475 TEST_F(MenuManagerTest, RemoveAll) { | 498 TEST_F(MenuManagerTest, RemoveAll) { |
| 476 // Try removing all items for an extension id that doesn't have any items. | 499 // Try removing all items for an extension id that doesn't have any items. |
| 477 manager_.RemoveAllContextItems("CCCC"); | 500 manager_.RemoveAllContextItems("CCCC"); |
| 478 | 501 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 519 manager_.RemoveContextMenuItem(item3->id()); | 542 manager_.RemoveContextMenuItem(item3->id()); |
| 520 manager_.RemoveContextMenuItem(item1->id()); | 543 manager_.RemoveContextMenuItem(item1->id()); |
| 521 manager_.RemoveContextMenuItem(item2->id()); | 544 manager_.RemoveContextMenuItem(item2->id()); |
| 522 | 545 |
| 523 ASSERT_TRUE(manager_.context_items_.empty()); | 546 ASSERT_TRUE(manager_.context_items_.empty()); |
| 524 } | 547 } |
| 525 | 548 |
| 526 TEST_F(MenuManagerTest, ExecuteCommand) { | 549 TEST_F(MenuManagerTest, ExecuteCommand) { |
| 527 MockTestingProfile profile; | 550 MockTestingProfile profile; |
| 528 | 551 |
| 529 scoped_ptr<MockEventRouter> mock_event_router(new MockEventRouter(&profile)); | 552 MockExtensionSystem* mock_extension_system = |
| 553 static_cast<MockExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> | |
| 554 SetTestingFactoryAndUse(&profile, &BuildMockExtensionSystem)); | |
| 555 MockEventRouter* mock_event_router = | |
| 556 static_cast<MockEventRouter*>(mock_extension_system->event_router()); | |
| 530 | 557 |
| 531 content::ContextMenuParams params; | 558 content::ContextMenuParams params; |
| 532 params.media_type = WebKit::WebContextMenuData::MediaTypeImage; | 559 params.media_type = WebKit::WebContextMenuData::MediaTypeImage; |
| 533 params.src_url = GURL("http://foo.bar/image.png"); | 560 params.src_url = GURL("http://foo.bar/image.png"); |
| 534 params.page_url = GURL("http://foo.bar"); | 561 params.page_url = GURL("http://foo.bar"); |
| 535 params.selection_text = ASCIIToUTF16("Hello World"); | 562 params.selection_text = ASCIIToUTF16("Hello World"); |
| 536 params.is_editable = false; | 563 params.is_editable = false; |
| 537 | 564 |
| 538 Extension* extension = AddExtension("test"); | 565 Extension* extension = AddExtension("test"); |
| 539 MenuItem* item = CreateTestItem(extension); | 566 MenuItem* item = CreateTestItem(extension); |
| 540 MenuItem::Id id = item->id(); | 567 MenuItem::Id id = item->id(); |
| 541 ASSERT_TRUE(manager_.AddContextItem(extension, item)); | 568 ASSERT_TRUE(manager_.AddContextItem(extension, item)); |
| 542 | 569 |
| 543 EXPECT_CALL(profile, GetExtensionEventRouter()) | |
| 544 .Times(1) | |
| 545 .WillOnce(Return(mock_event_router.get())); | |
| 546 | |
| 547 // Use the magic of googlemock to save a parameter to our mock's | 570 // Use the magic of googlemock to save a parameter to our mock's |
| 548 // DispatchEventToExtension method into event_args. | 571 // DispatchEventToExtension method into event_)args. |
|
Yoyo Zhou
2012/10/25 22:24:47
spurious
Miranda Callahan
2012/10/26 19:23:01
Done.
| |
| 549 base::ListValue* list = NULL; | 572 base::ListValue* list = NULL; |
| 550 { | 573 { |
| 551 InSequence s; | 574 InSequence s; |
| 552 EXPECT_CALL(*mock_event_router.get(), | 575 EXPECT_CALL(*mock_event_router, |
| 553 DispatchEventToExtensionMock( | 576 DispatchEventToExtensionMock( |
| 554 item->extension_id(), | 577 item->extension_id(), |
| 555 extensions::event_names::kOnContextMenus, | 578 extensions::event_names::kOnContextMenus, |
| 556 _, | 579 _, |
| 557 &profile, | 580 &profile, |
| 558 GURL(), | 581 GURL(), |
| 559 EventRouter::USER_GESTURE_ENABLED)) | 582 EventRouter::USER_GESTURE_ENABLED)) |
| 560 .Times(1) | 583 .Times(1) |
| 561 .WillOnce(SaveArg<2>(&list)); | 584 .WillOnce(SaveArg<2>(&list)); |
| 562 EXPECT_CALL(*mock_event_router.get(), | 585 EXPECT_CALL(*mock_event_router, |
| 563 DispatchEventToExtensionMock( | 586 DispatchEventToExtensionMock( |
| 564 item->extension_id(), | 587 item->extension_id(), |
| 565 extensions::event_names::kOnContextMenuClicked, | 588 extensions::event_names::kOnContextMenuClicked, |
| 566 _, | 589 _, |
| 567 &profile, | 590 &profile, |
| 568 GURL(), | 591 GURL(), |
| 569 EventRouter::USER_GESTURE_ENABLED)) | 592 EventRouter::USER_GESTURE_ENABLED)) |
| 570 .Times(1) | 593 .Times(1) |
| 571 .WillOnce(DeleteArg<2>()); | 594 .WillOnce(DeleteArg<2>()); |
| 572 } | 595 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 685 ASSERT_TRUE(child1->checked()); | 708 ASSERT_TRUE(child1->checked()); |
| 686 | 709 |
| 687 // Removing |parent| should cause only |child1| to be selected. | 710 // Removing |parent| should cause only |child1| to be selected. |
| 688 manager_.RemoveContextMenuItem(parent->id()); | 711 manager_.RemoveContextMenuItem(parent->id()); |
| 689 parent = NULL; | 712 parent = NULL; |
| 690 ASSERT_FALSE(new_item->checked()); | 713 ASSERT_FALSE(new_item->checked()); |
| 691 ASSERT_TRUE(child1->checked()); | 714 ASSERT_TRUE(child1->checked()); |
| 692 } | 715 } |
| 693 | 716 |
| 694 } // namespace extensions | 717 } // namespace extensions |
| OLD | NEW |