| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 <vector> |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_menu_manager.h" |
| 12 #include "chrome/browser/extensions/extension_message_service.h" |
| 13 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 14 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" |
| 17 #include "chrome/common/notification_service.h" |
| 18 #include "chrome/test/testing_profile.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "webkit/glue/context_menu.h" |
| 22 |
| 23 using testing::_; |
| 24 using testing::AtLeast; |
| 25 using testing::Return; |
| 26 using testing::SaveArg; |
| 27 |
| 28 // Base class for tests. |
| 29 class ExtensionMenuManagerTest : public testing::Test { |
| 30 public: |
| 31 ExtensionMenuManagerTest() {} |
| 32 ~ExtensionMenuManagerTest() {} |
| 33 |
| 34 // Returns a test item with some default values you can override if you want |
| 35 // to by passing in |properties| (currently just extension_id). Caller owns |
| 36 // the return value and is responsible for freeing it. |
| 37 static ExtensionMenuItem* CreateTestItem(DictionaryValue* properties) { |
| 38 std::string extension_id = "0123456789"; // A default dummy value. |
| 39 if (properties && properties->HasKey(L"extension_id")) |
| 40 EXPECT_TRUE(properties->GetString(L"extension_id", &extension_id)); |
| 41 |
| 42 ExtensionMenuItem::Type type = ExtensionMenuItem::NORMAL; |
| 43 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::ALL); |
| 44 ExtensionMenuItem::ContextList enabled_contexts = contexts; |
| 45 std::string title = "test"; |
| 46 |
| 47 return new ExtensionMenuItem(extension_id, title, false, type, contexts, |
| 48 enabled_contexts); |
| 49 } |
| 50 |
| 51 protected: |
| 52 ExtensionMenuManager manager_; |
| 53 |
| 54 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManagerTest); |
| 56 }; |
| 57 |
| 58 // Tests adding, getting, and removing items. |
| 59 TEST_F(ExtensionMenuManagerTest, AddGetRemoveItems) { |
| 60 // Add a new item, make sure you can get it back. |
| 61 ExtensionMenuItem* item1 = CreateTestItem(NULL); |
| 62 ASSERT_TRUE(item1 != NULL); |
| 63 int id1 = manager_.AddContextItem(item1); // Ownership transferred. |
| 64 ASSERT_GT(id1, 0); |
| 65 ASSERT_EQ(item1, manager_.GetItemById(id1)); |
| 66 std::vector<const ExtensionMenuItem*> items = |
| 67 manager_.MenuItems(item1->extension_id()); |
| 68 ASSERT_EQ(1u, items.size()); |
| 69 ASSERT_EQ(item1, items[0]); |
| 70 |
| 71 // Add a second item, make sure it comes back too. |
| 72 ExtensionMenuItem* item2 = CreateTestItem(NULL); |
| 73 int id2 = manager_.AddContextItem(item2); // Ownership transferred. |
| 74 ASSERT_GT(id2, 0); |
| 75 ASSERT_NE(id1, id2); |
| 76 ASSERT_EQ(item2, manager_.GetItemById(id2)); |
| 77 items = manager_.MenuItems(item2->extension_id()); |
| 78 ASSERT_EQ(2u, items.size()); |
| 79 ASSERT_EQ(item1, items[0]); |
| 80 ASSERT_EQ(item2, items[1]); |
| 81 |
| 82 // Try adding item 3, then removing it. |
| 83 ExtensionMenuItem* item3 = CreateTestItem(NULL); |
| 84 std::string extension_id = item3->extension_id(); |
| 85 int id3 = manager_.AddContextItem(item3); // Ownership transferred. |
| 86 ASSERT_GT(id3, 0); |
| 87 ASSERT_EQ(item3, manager_.GetItemById(id3)); |
| 88 ASSERT_EQ(3u, manager_.MenuItems(extension_id).size()); |
| 89 ASSERT_TRUE(manager_.RemoveContextMenuItem(id3)); |
| 90 ASSERT_EQ(NULL, manager_.GetItemById(id3)); |
| 91 ASSERT_EQ(2u, manager_.MenuItems(extension_id).size()); |
| 92 |
| 93 // Make sure removing a non-existent item returns false. |
| 94 ASSERT_FALSE(manager_.RemoveContextMenuItem(5)); |
| 95 } |
| 96 |
| 97 // Test adding/removing child items. |
| 98 TEST_F(ExtensionMenuManagerTest, ChildFunctions) { |
| 99 DictionaryValue properties; |
| 100 properties.SetString(L"extension_id", "1111"); |
| 101 ExtensionMenuItem* item1 = CreateTestItem(&properties); |
| 102 |
| 103 properties.SetString(L"extension_id", "2222"); |
| 104 ExtensionMenuItem* item2 = CreateTestItem(&properties); |
| 105 ExtensionMenuItem* item2_child = CreateTestItem(&properties); |
| 106 ExtensionMenuItem* item2_grandchild = CreateTestItem(&properties); |
| 107 |
| 108 // This third item we expect to fail inserting, so we use a scoped_ptr to make |
| 109 // sure it gets deleted. |
| 110 properties.SetString(L"extension_id", "3333"); |
| 111 scoped_ptr<ExtensionMenuItem> item3(CreateTestItem(&properties)); |
| 112 |
| 113 // Add in the first two items. |
| 114 int id1 = manager_.AddContextItem(item1); // Ownership transferred. |
| 115 int id2 = manager_.AddContextItem(item2); // Ownership transferred. |
| 116 |
| 117 ASSERT_NE(id1, id2); |
| 118 |
| 119 // Try adding item3 as a child of item2 - this should fail because item3 has |
| 120 // a different extension id. |
| 121 ASSERT_EQ(0, manager_.AddChildItem(id2, item3.get())); |
| 122 |
| 123 // Add item2_child as a child of item2. |
| 124 int id2_child = manager_.AddChildItem(id2, item2_child); |
| 125 ASSERT_GT(id2_child, 0); |
| 126 ASSERT_EQ(1, item2->child_count()); |
| 127 ASSERT_EQ(0, item1->child_count()); |
| 128 ASSERT_EQ(item2_child, manager_.GetItemById(id2_child)); |
| 129 |
| 130 ASSERT_EQ(1u, manager_.MenuItems(item1->extension_id()).size()); |
| 131 ASSERT_EQ(item1, manager_.MenuItems(item1->extension_id()).at(0)); |
| 132 |
| 133 // Add item2_grandchild as a child of item2_child, then remove it. |
| 134 int id2_grandchild = manager_.AddChildItem(id2_child, item2_grandchild); |
| 135 ASSERT_GT(id2_grandchild, 0); |
| 136 ASSERT_EQ(1, item2->child_count()); |
| 137 ASSERT_EQ(1, item2_child->child_count()); |
| 138 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_grandchild)); |
| 139 |
| 140 // We should only get 1 thing back when asking for item2's extension id, since |
| 141 // it has a child item. |
| 142 ASSERT_EQ(1u, manager_.MenuItems(item2->extension_id()).size()); |
| 143 ASSERT_EQ(item2, manager_.MenuItems(item2->extension_id()).at(0)); |
| 144 |
| 145 // Remove child2_item. |
| 146 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_child)); |
| 147 ASSERT_EQ(1u, manager_.MenuItems(item2->extension_id()).size()); |
| 148 ASSERT_EQ(item2, manager_.MenuItems(item2->extension_id()).at(0)); |
| 149 ASSERT_EQ(0, item2->child_count()); |
| 150 } |
| 151 |
| 152 // Tests that we properly remove an extension's menu item when that extension is |
| 153 // unloaded. |
| 154 TEST_F(ExtensionMenuManagerTest, ExtensionUnloadRemovesMenuItems) { |
| 155 ScopedTempDir temp_dir; |
| 156 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 157 |
| 158 NotificationService* notifier = NotificationService::current(); |
| 159 ASSERT_TRUE(notifier != NULL); |
| 160 |
| 161 // Create a test extension. |
| 162 DictionaryValue extension_properties; |
| 163 extension_properties.SetString(extension_manifest_keys::kVersion, "1"); |
| 164 extension_properties.SetString(extension_manifest_keys::kName, "Test"); |
| 165 Extension extension(temp_dir.path().AppendASCII("extension")); |
| 166 std::string errors; |
| 167 ASSERT_TRUE(extension.InitFromValue(extension_properties, |
| 168 false, // No public key required. |
| 169 &errors)) << errors; |
| 170 |
| 171 // Create an ExtensionMenuItem and put it into the manager. |
| 172 DictionaryValue item_properties; |
| 173 item_properties.SetString(L"extension_id", extension.id()); |
| 174 ExtensionMenuItem* item1 = CreateTestItem(&item_properties); |
| 175 ASSERT_EQ(extension.id(), item1->extension_id()); |
| 176 int id1 = manager_.AddContextItem(item1); // Ownership transferred. |
| 177 ASSERT_GT(id1, 0); |
| 178 ASSERT_EQ(1u, manager_.MenuItems(extension.id()).size()); |
| 179 |
| 180 // Create a menu item with a different extension id and add it to the manager. |
| 181 std::string alternate_extension_id = "0000"; |
| 182 item_properties.SetString(L"extension_id", alternate_extension_id); |
| 183 ExtensionMenuItem* item2 = CreateTestItem(&item_properties); |
| 184 ASSERT_NE(item1->extension_id(), item2->extension_id()); |
| 185 int id2 = manager_.AddContextItem(item2); // Ownership transferred. |
| 186 ASSERT_GT(id2, 0); |
| 187 |
| 188 // Notify that the extension was unloaded, and make sure the right item is |
| 189 // gone. |
| 190 notifier->Notify(NotificationType::EXTENSION_UNLOADED, |
| 191 Source<Profile>(NULL), |
| 192 Details<Extension>(&extension)); |
| 193 ASSERT_EQ(0u, manager_.MenuItems(extension.id()).size()); |
| 194 ASSERT_EQ(1u, manager_.MenuItems(alternate_extension_id).size()); |
| 195 ASSERT_TRUE(manager_.GetItemById(id1) == NULL); |
| 196 ASSERT_TRUE(manager_.GetItemById(id2) != NULL); |
| 197 } |
| 198 |
| 199 // A mock message service for tests of ExtensionMenuManager::ExecuteCommand. |
| 200 class MockExtensionMessageService : public ExtensionMessageService { |
| 201 public: |
| 202 explicit MockExtensionMessageService(Profile* profile) : |
| 203 ExtensionMessageService(profile) {} |
| 204 |
| 205 MOCK_METHOD3(DispatchEventToRenderers, void(const std::string& event_name, |
| 206 const std::string& event_args, |
| 207 bool has_incognito_data)); |
| 208 |
| 209 private: |
| 210 DISALLOW_COPY_AND_ASSIGN(MockExtensionMessageService); |
| 211 }; |
| 212 |
| 213 // A mock profile for tests of ExtensionMenuManager::ExecuteCommand. |
| 214 class MockTestingProfile : public TestingProfile { |
| 215 public: |
| 216 MockTestingProfile() {} |
| 217 MOCK_METHOD0(GetExtensionMessageService, ExtensionMessageService*()); |
| 218 MOCK_METHOD0(IsOffTheRecord, bool()); |
| 219 |
| 220 private: |
| 221 DISALLOW_COPY_AND_ASSIGN(MockTestingProfile); |
| 222 }; |
| 223 |
| 224 TEST_F(ExtensionMenuManagerTest, ExecuteCommand) { |
| 225 MessageLoopForUI message_loop; |
| 226 ChromeThread ui_thread(ChromeThread::UI, &message_loop); |
| 227 |
| 228 MockTestingProfile profile; |
| 229 |
| 230 scoped_refptr<MockExtensionMessageService> mock_message_service = |
| 231 new MockExtensionMessageService(&profile); |
| 232 |
| 233 TestTabContents tab_contents(&profile, NULL); // No SiteInstance. |
| 234 ContextMenuParams params; |
| 235 params.media_type = WebKit::WebContextMenuData::MediaTypeImage; |
| 236 params.src_url = GURL("http://foo.bar/image.png"); |
| 237 params.page_url = GURL("http://foo.bar"); |
| 238 params.selection_text = L"Hello World"; |
| 239 params.is_editable = false; |
| 240 |
| 241 ExtensionMenuItem* item = CreateTestItem(NULL); |
| 242 int id = manager_.AddContextItem(item); // Ownership transferred. |
| 243 ASSERT_GT(id, 0); |
| 244 |
| 245 EXPECT_CALL(profile, GetExtensionMessageService()) |
| 246 .Times(1) |
| 247 .WillOnce(Return(mock_message_service.get())); |
| 248 |
| 249 EXPECT_CALL(profile, IsOffTheRecord()) |
| 250 .Times(AtLeast(1)) |
| 251 .WillRepeatedly(Return(false)); |
| 252 |
| 253 // Use the magic of googlemock to save a parameter to our mock's |
| 254 // DispatchEventToRenderers method into event_args. |
| 255 std::string event_args; |
| 256 std::string expected_event_name = "contextMenu/" + item->extension_id(); |
| 257 EXPECT_CALL(*mock_message_service.get(), |
| 258 DispatchEventToRenderers(expected_event_name, _, |
| 259 profile.IsOffTheRecord())) |
| 260 .Times(1) |
| 261 .WillOnce(SaveArg<1>(&event_args)); |
| 262 |
| 263 manager_.ExecuteCommand(&profile, &tab_contents, params, id); |
| 264 |
| 265 // Parse the json event_args, which should turn into a 2-element list where |
| 266 // the first element is a dictionary we want to inspect for the correct |
| 267 // values. |
| 268 scoped_ptr<Value> result(base::JSONReader::Read(event_args, true)); |
| 269 Value* value = result.get(); |
| 270 ASSERT_TRUE(result.get() != NULL); |
| 271 ASSERT_EQ(Value::TYPE_LIST, value->GetType()); |
| 272 ListValue* list = static_cast<ListValue*>(value); |
| 273 ASSERT_EQ(2u, list->GetSize()); |
| 274 |
| 275 DictionaryValue* info; |
| 276 ASSERT_TRUE(list->GetDictionary(0, &info)); |
| 277 |
| 278 int tmp_id = 0; |
| 279 ASSERT_TRUE(info->GetInteger(L"menuItemId", &tmp_id)); |
| 280 ASSERT_EQ(id, tmp_id); |
| 281 |
| 282 std::string tmp; |
| 283 ASSERT_TRUE(info->GetString(L"mediaType", &tmp)); |
| 284 ASSERT_EQ("IMAGE", tmp); |
| 285 ASSERT_TRUE(info->GetString(L"srcUrl", &tmp)); |
| 286 ASSERT_EQ(params.src_url.spec(), tmp); |
| 287 ASSERT_TRUE(info->GetString(L"mainFrameUrl", &tmp)); |
| 288 ASSERT_EQ(params.page_url.spec(), tmp); |
| 289 |
| 290 std::wstring wide_tmp; |
| 291 ASSERT_TRUE(info->GetString(L"selectionText", &wide_tmp)); |
| 292 ASSERT_EQ(params.selection_text, wide_tmp); |
| 293 |
| 294 bool bool_tmp = true; |
| 295 ASSERT_TRUE(info->GetBoolean(L"editable", &bool_tmp)); |
| 296 ASSERT_EQ(params.is_editable, bool_tmp); |
| 297 } |
| OLD | NEW |