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 |
464 // A mock profile for tests of MenuManager::ExecuteCommand. | 466 // A mock ExtensionSystem to serve our MockEventRouter. |
465 class MockTestingProfile : public TestingProfile { | 467 class MockExtensionSystem : public TestExtensionSystem { |
466 public: | 468 public: |
467 MockTestingProfile() {} | 469 explicit MockExtensionSystem(Profile* profile) |
468 MOCK_METHOD0(GetExtensionEventRouter, EventRouter*()); | 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 } |
469 | 477 |
470 private: | 478 private: |
471 DISALLOW_COPY_AND_ASSIGN(MockTestingProfile); | 479 scoped_ptr<MockEventRouter> mock_event_router_; |
| 480 |
| 481 DISALLOW_COPY_AND_ASSIGN(MockExtensionSystem); |
472 }; | 482 }; |
473 | 483 |
| 484 ProfileKeyedService* BuildMockExtensionSystem(Profile* profile) { |
| 485 return new MockExtensionSystem(profile); |
| 486 } |
| 487 |
474 // Tests the RemoveAll functionality. | 488 // Tests the RemoveAll functionality. |
475 TEST_F(MenuManagerTest, RemoveAll) { | 489 TEST_F(MenuManagerTest, RemoveAll) { |
476 // Try removing all items for an extension id that doesn't have any items. | 490 // Try removing all items for an extension id that doesn't have any items. |
477 manager_.RemoveAllContextItems("CCCC"); | 491 manager_.RemoveAllContextItems("CCCC"); |
478 | 492 |
479 // Add 2 top-level and one child item for extension 1. | 493 // Add 2 top-level and one child item for extension 1. |
480 Extension* extension1 = AddExtension("1111"); | 494 Extension* extension1 = AddExtension("1111"); |
481 MenuItem* item1 = CreateTestItem(extension1); | 495 MenuItem* item1 = CreateTestItem(extension1); |
482 MenuItem* item2 = CreateTestItem(extension1); | 496 MenuItem* item2 = CreateTestItem(extension1); |
483 MenuItem* item3 = CreateTestItem(extension1); | 497 MenuItem* item3 = CreateTestItem(extension1); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 ASSERT_FALSE(manager_.context_items_.empty()); | 531 ASSERT_FALSE(manager_.context_items_.empty()); |
518 | 532 |
519 manager_.RemoveContextMenuItem(item3->id()); | 533 manager_.RemoveContextMenuItem(item3->id()); |
520 manager_.RemoveContextMenuItem(item1->id()); | 534 manager_.RemoveContextMenuItem(item1->id()); |
521 manager_.RemoveContextMenuItem(item2->id()); | 535 manager_.RemoveContextMenuItem(item2->id()); |
522 | 536 |
523 ASSERT_TRUE(manager_.context_items_.empty()); | 537 ASSERT_TRUE(manager_.context_items_.empty()); |
524 } | 538 } |
525 | 539 |
526 TEST_F(MenuManagerTest, ExecuteCommand) { | 540 TEST_F(MenuManagerTest, ExecuteCommand) { |
527 MockTestingProfile profile; | 541 TestingProfile profile; |
528 | 542 |
529 scoped_ptr<MockEventRouter> mock_event_router(new MockEventRouter(&profile)); | 543 MockExtensionSystem* mock_extension_system = |
| 544 static_cast<MockExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> |
| 545 SetTestingFactoryAndUse(&profile, &BuildMockExtensionSystem)); |
| 546 MockEventRouter* mock_event_router = |
| 547 static_cast<MockEventRouter*>(mock_extension_system->event_router()); |
530 | 548 |
531 content::ContextMenuParams params; | 549 content::ContextMenuParams params; |
532 params.media_type = WebKit::WebContextMenuData::MediaTypeImage; | 550 params.media_type = WebKit::WebContextMenuData::MediaTypeImage; |
533 params.src_url = GURL("http://foo.bar/image.png"); | 551 params.src_url = GURL("http://foo.bar/image.png"); |
534 params.page_url = GURL("http://foo.bar"); | 552 params.page_url = GURL("http://foo.bar"); |
535 params.selection_text = ASCIIToUTF16("Hello World"); | 553 params.selection_text = ASCIIToUTF16("Hello World"); |
536 params.is_editable = false; | 554 params.is_editable = false; |
537 | 555 |
538 Extension* extension = AddExtension("test"); | 556 Extension* extension = AddExtension("test"); |
539 MenuItem* item = CreateTestItem(extension); | 557 MenuItem* item = CreateTestItem(extension); |
540 MenuItem::Id id = item->id(); | 558 MenuItem::Id id = item->id(); |
541 ASSERT_TRUE(manager_.AddContextItem(extension, item)); | 559 ASSERT_TRUE(manager_.AddContextItem(extension, item)); |
542 | 560 |
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 | 561 // Use the magic of googlemock to save a parameter to our mock's |
548 // DispatchEventToExtension method into event_args. | 562 // DispatchEventToExtension method into event_args. |
549 base::ListValue* list = NULL; | 563 base::ListValue* list = NULL; |
550 { | 564 { |
551 InSequence s; | 565 InSequence s; |
552 EXPECT_CALL(*mock_event_router.get(), | 566 EXPECT_CALL(*mock_event_router, |
553 DispatchEventToExtensionMock( | 567 DispatchEventToExtensionMock( |
554 item->extension_id(), | 568 item->extension_id(), |
555 extensions::event_names::kOnContextMenus, | 569 extensions::event_names::kOnContextMenus, |
556 _, | 570 _, |
557 &profile, | 571 &profile, |
558 GURL(), | 572 GURL(), |
559 EventRouter::USER_GESTURE_ENABLED)) | 573 EventRouter::USER_GESTURE_ENABLED)) |
560 .Times(1) | 574 .Times(1) |
561 .WillOnce(SaveArg<2>(&list)); | 575 .WillOnce(SaveArg<2>(&list)); |
562 EXPECT_CALL(*mock_event_router.get(), | 576 EXPECT_CALL(*mock_event_router, |
563 DispatchEventToExtensionMock( | 577 DispatchEventToExtensionMock( |
564 item->extension_id(), | 578 item->extension_id(), |
565 extensions::event_names::kOnContextMenuClicked, | 579 extensions::event_names::kOnContextMenuClicked, |
566 _, | 580 _, |
567 &profile, | 581 &profile, |
568 GURL(), | 582 GURL(), |
569 EventRouter::USER_GESTURE_ENABLED)) | 583 EventRouter::USER_GESTURE_ENABLED)) |
570 .Times(1) | 584 .Times(1) |
571 .WillOnce(DeleteArg<2>()); | 585 .WillOnce(DeleteArg<2>()); |
572 } | 586 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 ASSERT_TRUE(child1->checked()); | 699 ASSERT_TRUE(child1->checked()); |
686 | 700 |
687 // Removing |parent| should cause only |child1| to be selected. | 701 // Removing |parent| should cause only |child1| to be selected. |
688 manager_.RemoveContextMenuItem(parent->id()); | 702 manager_.RemoveContextMenuItem(parent->id()); |
689 parent = NULL; | 703 parent = NULL; |
690 ASSERT_FALSE(new_item->checked()); | 704 ASSERT_FALSE(new_item->checked()); |
691 ASSERT_TRUE(child1->checked()); | 705 ASSERT_TRUE(child1->checked()); |
692 } | 706 } |
693 | 707 |
694 } // namespace extensions | 708 } // namespace extensions |
OLD | NEW |