| 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" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 23 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/common/context_menu_params.h" | 24 #include "content/public/common/context_menu_params.h" |
| 25 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 using content::BrowserThread; | 29 using content::BrowserThread; |
| 30 using testing::_; | 30 using testing::_; |
| 31 using testing::AtLeast; | 31 using testing::AtLeast; |
| 32 using testing::DeleteArg; |
| 32 using testing::InSequence; | 33 using testing::InSequence; |
| 33 using testing::Return; | 34 using testing::Return; |
| 34 using testing::SaveArg; | 35 using testing::SaveArg; |
| 35 | 36 |
| 36 namespace extensions { | 37 namespace extensions { |
| 37 | 38 |
| 38 // Base class for tests. | 39 // Base class for tests. |
| 39 class MenuManagerTest : public testing::Test { | 40 class MenuManagerTest : public testing::Test { |
| 40 public: | 41 public: |
| 41 MenuManagerTest() : ui_thread_(BrowserThread::UI, &message_loop_), | 42 MenuManagerTest() : ui_thread_(BrowserThread::UI, &message_loop_), |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 ASSERT_TRUE(manager_.GetItemById(id1) == NULL); | 369 ASSERT_TRUE(manager_.GetItemById(id1) == NULL); |
| 369 ASSERT_TRUE(manager_.GetItemById(item2->id()) != NULL); | 370 ASSERT_TRUE(manager_.GetItemById(item2->id()) != NULL); |
| 370 } | 371 } |
| 371 | 372 |
| 372 // A mock message service for tests of MenuManager::ExecuteCommand. | 373 // A mock message service for tests of MenuManager::ExecuteCommand. |
| 373 class MockEventRouter : public EventRouter { | 374 class MockEventRouter : public EventRouter { |
| 374 public: | 375 public: |
| 375 explicit MockEventRouter(Profile* profile) : | 376 explicit MockEventRouter(Profile* profile) : |
| 376 EventRouter(profile) {} | 377 EventRouter(profile) {} |
| 377 | 378 |
| 378 MOCK_METHOD6(DispatchEventToExtension, | 379 MOCK_METHOD6(DispatchEventToExtensionMock, |
| 379 void(const std::string& extension_id, | 380 void(const std::string& extension_id, |
| 380 const std::string& event_name, | 381 const std::string& event_name, |
| 381 const std::string& event_args, | 382 base::ListValue* event_args, |
| 382 Profile* source_profile, | 383 Profile* source_profile, |
| 383 const GURL& event_url, | 384 const GURL& event_url, |
| 384 EventRouter::UserGestureState state)); | 385 EventRouter::UserGestureState state)); |
| 385 | 386 |
| 387 virtual void DispatchEventToExtension(const std::string& extension_id, |
| 388 const std::string& event_name, |
| 389 scoped_ptr<base::ListValue> event_args, |
| 390 Profile* source_profile, |
| 391 const GURL& event_url, |
| 392 EventRouter::UserGestureState state) { |
| 393 DispatchEventToExtensionMock(extension_id, event_name, event_args.release(), |
| 394 source_profile, event_url, state); |
| 395 } |
| 386 | 396 |
| 387 private: | 397 private: |
| 388 DISALLOW_COPY_AND_ASSIGN(MockEventRouter); | 398 DISALLOW_COPY_AND_ASSIGN(MockEventRouter); |
| 389 }; | 399 }; |
| 390 | 400 |
| 391 // A mock profile for tests of MenuManager::ExecuteCommand. | 401 // A mock profile for tests of MenuManager::ExecuteCommand. |
| 392 class MockTestingProfile : public TestingProfile { | 402 class MockTestingProfile : public TestingProfile { |
| 393 public: | 403 public: |
| 394 MockTestingProfile() {} | 404 MockTestingProfile() {} |
| 395 MOCK_METHOD0(GetExtensionEventRouter, EventRouter*()); | 405 MOCK_METHOD0(GetExtensionEventRouter, EventRouter*()); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 MenuItem* item = CreateTestItem(extension); | 477 MenuItem* item = CreateTestItem(extension); |
| 468 MenuItem::Id id = item->id(); | 478 MenuItem::Id id = item->id(); |
| 469 ASSERT_TRUE(manager_.AddContextItem(extension, item)); | 479 ASSERT_TRUE(manager_.AddContextItem(extension, item)); |
| 470 | 480 |
| 471 EXPECT_CALL(profile, GetExtensionEventRouter()) | 481 EXPECT_CALL(profile, GetExtensionEventRouter()) |
| 472 .Times(1) | 482 .Times(1) |
| 473 .WillOnce(Return(mock_event_router.get())); | 483 .WillOnce(Return(mock_event_router.get())); |
| 474 | 484 |
| 475 // Use the magic of googlemock to save a parameter to our mock's | 485 // Use the magic of googlemock to save a parameter to our mock's |
| 476 // DispatchEventToExtension method into event_args. | 486 // DispatchEventToExtension method into event_args. |
| 477 std::string event_args; | 487 base::ListValue* list = NULL; |
| 478 { | 488 { |
| 479 InSequence s; | 489 InSequence s; |
| 480 EXPECT_CALL(*mock_event_router.get(), | 490 EXPECT_CALL(*mock_event_router.get(), |
| 481 DispatchEventToExtension( | 491 DispatchEventToExtensionMock( |
| 482 item->extension_id(), | 492 item->extension_id(), |
| 483 extensions::event_names::kOnContextMenus, | 493 extensions::event_names::kOnContextMenus, |
| 484 _, | 494 _, |
| 485 &profile, | 495 &profile, |
| 486 GURL(), | 496 GURL(), |
| 487 EventRouter::USER_GESTURE_ENABLED)) | 497 EventRouter::USER_GESTURE_ENABLED)) |
| 488 .Times(1) | 498 .Times(1) |
| 489 .WillOnce(SaveArg<2>(&event_args)); | 499 .WillOnce(SaveArg<2>(&list)); |
| 490 EXPECT_CALL(*mock_event_router.get(), | 500 EXPECT_CALL(*mock_event_router.get(), |
| 491 DispatchEventToExtension( | 501 DispatchEventToExtensionMock( |
| 492 item->extension_id(), | 502 item->extension_id(), |
| 493 extensions::event_names::kOnContextMenuClicked, | 503 extensions::event_names::kOnContextMenuClicked, |
| 494 _, | 504 _, |
| 495 &profile, | 505 &profile, |
| 496 GURL(), | 506 GURL(), |
| 497 EventRouter::USER_GESTURE_ENABLED)) | 507 EventRouter::USER_GESTURE_ENABLED)) |
| 498 .Times(1); | 508 .Times(1) |
| 509 .WillOnce(DeleteArg<2>()); |
| 499 } | 510 } |
| 500 manager_.ExecuteCommand(&profile, NULL /* tab_contents */, params, id); | 511 manager_.ExecuteCommand(&profile, NULL /* tab_contents */, params, id); |
| 501 | 512 |
| 502 // Parse the json event_args, which should turn into a 2-element list where | |
| 503 // the first element is a dictionary we want to inspect for the correct | |
| 504 // values. | |
| 505 scoped_ptr<Value> result( | |
| 506 base::JSONReader::Read(event_args, base::JSON_ALLOW_TRAILING_COMMAS)); | |
| 507 Value* value = result.get(); | |
| 508 ASSERT_TRUE(result.get() != NULL); | |
| 509 ASSERT_EQ(Value::TYPE_LIST, value->GetType()); | |
| 510 ListValue* list = static_cast<ListValue*>(value); | |
| 511 ASSERT_EQ(2u, list->GetSize()); | 513 ASSERT_EQ(2u, list->GetSize()); |
| 512 | 514 |
| 513 DictionaryValue* info; | 515 DictionaryValue* info; |
| 514 ASSERT_TRUE(list->GetDictionary(0, &info)); | 516 ASSERT_TRUE(list->GetDictionary(0, &info)); |
| 515 | 517 |
| 516 int tmp_id = 0; | 518 int tmp_id = 0; |
| 517 ASSERT_TRUE(info->GetInteger("menuItemId", &tmp_id)); | 519 ASSERT_TRUE(info->GetInteger("menuItemId", &tmp_id)); |
| 518 ASSERT_EQ(id.uid, tmp_id); | 520 ASSERT_EQ(id.uid, tmp_id); |
| 519 | 521 |
| 520 std::string tmp; | 522 std::string tmp; |
| 521 ASSERT_TRUE(info->GetString("mediaType", &tmp)); | 523 ASSERT_TRUE(info->GetString("mediaType", &tmp)); |
| 522 ASSERT_EQ("image", tmp); | 524 ASSERT_EQ("image", tmp); |
| 523 ASSERT_TRUE(info->GetString("srcUrl", &tmp)); | 525 ASSERT_TRUE(info->GetString("srcUrl", &tmp)); |
| 524 ASSERT_EQ(params.src_url.spec(), tmp); | 526 ASSERT_EQ(params.src_url.spec(), tmp); |
| 525 ASSERT_TRUE(info->GetString("pageUrl", &tmp)); | 527 ASSERT_TRUE(info->GetString("pageUrl", &tmp)); |
| 526 ASSERT_EQ(params.page_url.spec(), tmp); | 528 ASSERT_EQ(params.page_url.spec(), tmp); |
| 527 | 529 |
| 528 string16 tmp16; | 530 string16 tmp16; |
| 529 ASSERT_TRUE(info->GetString("selectionText", &tmp16)); | 531 ASSERT_TRUE(info->GetString("selectionText", &tmp16)); |
| 530 ASSERT_EQ(params.selection_text, tmp16); | 532 ASSERT_EQ(params.selection_text, tmp16); |
| 531 | 533 |
| 532 bool bool_tmp = true; | 534 bool bool_tmp = true; |
| 533 ASSERT_TRUE(info->GetBoolean("editable", &bool_tmp)); | 535 ASSERT_TRUE(info->GetBoolean("editable", &bool_tmp)); |
| 534 ASSERT_EQ(params.is_editable, bool_tmp); | 536 ASSERT_EQ(params.is_editable, bool_tmp); |
| 537 |
| 538 delete list; |
| 535 } | 539 } |
| 536 | 540 |
| 537 // Test that there is always only one radio item selected. | 541 // Test that there is always only one radio item selected. |
| 538 TEST_F(MenuManagerTest, SanitizeRadioButtons) { | 542 TEST_F(MenuManagerTest, SanitizeRadioButtons) { |
| 539 Extension* extension = AddExtension("test"); | 543 Extension* extension = AddExtension("test"); |
| 540 | 544 |
| 541 // A single unchecked item should get checked | 545 // A single unchecked item should get checked |
| 542 MenuItem* item1 = CreateTestItem(extension); | 546 MenuItem* item1 = CreateTestItem(extension); |
| 543 | 547 |
| 544 item1->set_type(MenuItem::RADIO); | 548 item1->set_type(MenuItem::RADIO); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 ASSERT_TRUE(child1->checked()); | 623 ASSERT_TRUE(child1->checked()); |
| 620 | 624 |
| 621 // Removing |parent| should cause only |child1| to be selected. | 625 // Removing |parent| should cause only |child1| to be selected. |
| 622 manager_.RemoveContextMenuItem(parent->id()); | 626 manager_.RemoveContextMenuItem(parent->id()); |
| 623 parent = NULL; | 627 parent = NULL; |
| 624 ASSERT_FALSE(new_item->checked()); | 628 ASSERT_FALSE(new_item->checked()); |
| 625 ASSERT_TRUE(child1->checked()); | 629 ASSERT_TRUE(child1->checked()); |
| 626 } | 630 } |
| 627 | 631 |
| 628 } // namespace extensions | 632 } // namespace extensions |
| OLD | NEW |