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