| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_menu_manager.h" | 11 #include "chrome/browser/extensions/extension_menu_manager.h" |
| 12 #include "chrome/browser/extensions/extension_message_service.h" | 12 #include "chrome/browser/extensions/extension_message_service.h" |
| 13 #include "chrome/browser/extensions/test_extension_prefs.h" |
| 13 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
| 16 #include "chrome/common/notification_service.h" | 17 #include "chrome/common/notification_service.h" |
| 17 #include "chrome/test/testing_profile.h" | 18 #include "chrome/test/testing_profile.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "webkit/glue/context_menu.h" | 21 #include "webkit/glue/context_menu.h" |
| 21 | 22 |
| 22 using testing::_; | 23 using testing::_; |
| 23 using testing::AtLeast; | 24 using testing::AtLeast; |
| 24 using testing::Return; | 25 using testing::Return; |
| 25 using testing::SaveArg; | 26 using testing::SaveArg; |
| 26 | 27 |
| 27 // Base class for tests. | 28 // Base class for tests. |
| 28 class ExtensionMenuManagerTest : public testing::Test { | 29 class ExtensionMenuManagerTest : public testing::Test { |
| 29 public: | 30 public: |
| 30 ExtensionMenuManagerTest() {} | 31 ExtensionMenuManagerTest() {} |
| 31 ~ExtensionMenuManagerTest() {} | 32 ~ExtensionMenuManagerTest() {} |
| 32 | 33 |
| 33 // Returns a test item with some default values you can override if you want | 34 // Returns a test item. |
| 34 // to by passing in |properties| (currently just extension_id). Caller owns | 35 static ExtensionMenuItem* CreateTestItem(Extension* extension) { |
| 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; | 36 ExtensionMenuItem::Type type = ExtensionMenuItem::NORMAL; |
| 42 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::ALL); | 37 ExtensionMenuItem::ContextList contexts(ExtensionMenuItem::ALL); |
| 43 ExtensionMenuItem::ContextList enabled_contexts = contexts; | 38 ExtensionMenuItem::ContextList enabled_contexts = contexts; |
| 44 std::string title = "test"; | 39 return new ExtensionMenuItem(extension->id(), "test", false, type, contexts, |
| 40 enabled_contexts); |
| 41 } |
| 45 | 42 |
| 46 return new ExtensionMenuItem(extension_id, title, false, type, contexts, | 43 // Creates and returns a test Extension. The caller does *not* own the return |
| 47 enabled_contexts); | 44 // value. |
| 45 Extension* AddExtension(std::string name) { |
| 46 return prefs_.AddExtension(name); |
| 48 } | 47 } |
| 49 | 48 |
| 50 protected: | 49 protected: |
| 51 ExtensionMenuManager manager_; | 50 ExtensionMenuManager manager_; |
| 51 TestExtensionPrefs prefs_; |
| 52 | 52 |
| 53 private: | 53 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManagerTest); | 54 DISALLOW_COPY_AND_ASSIGN(ExtensionMenuManagerTest); |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 // Tests adding, getting, and removing items. | 57 // Tests adding, getting, and removing items. |
| 58 TEST_F(ExtensionMenuManagerTest, AddGetRemoveItems) { | 58 TEST_F(ExtensionMenuManagerTest, AddGetRemoveItems) { |
| 59 Extension* extension = AddExtension("test"); |
| 60 |
| 59 // Add a new item, make sure you can get it back. | 61 // Add a new item, make sure you can get it back. |
| 60 ExtensionMenuItem* item1 = CreateTestItem(NULL); | 62 ExtensionMenuItem* item1 = CreateTestItem(extension); |
| 61 ASSERT_TRUE(item1 != NULL); | 63 ASSERT_TRUE(item1 != NULL); |
| 62 int id1 = manager_.AddContextItem(item1); // Ownership transferred. | 64 int id1 = manager_.AddContextItem(extension, item1); |
| 63 ASSERT_GT(id1, 0); | 65 ASSERT_GT(id1, 0); |
| 64 ASSERT_EQ(item1, manager_.GetItemById(id1)); | 66 ASSERT_EQ(item1, manager_.GetItemById(id1)); |
| 65 const ExtensionMenuItem::List* items = | 67 const ExtensionMenuItem::List* items = |
| 66 manager_.MenuItems(item1->extension_id()); | 68 manager_.MenuItems(item1->extension_id()); |
| 67 ASSERT_EQ(1u, items->size()); | 69 ASSERT_EQ(1u, items->size()); |
| 68 ASSERT_EQ(item1, items->at(0)); | 70 ASSERT_EQ(item1, items->at(0)); |
| 69 | 71 |
| 70 // Add a second item, make sure it comes back too. | 72 // Add a second item, make sure it comes back too. |
| 71 ExtensionMenuItem* item2 = CreateTestItem(NULL); | 73 ExtensionMenuItem* item2 = CreateTestItem(extension); |
| 72 int id2 = manager_.AddContextItem(item2); // Ownership transferred. | 74 int id2 = manager_.AddContextItem(extension, item2); |
| 73 ASSERT_GT(id2, 0); | 75 ASSERT_GT(id2, 0); |
| 74 ASSERT_NE(id1, id2); | 76 ASSERT_NE(id1, id2); |
| 75 ASSERT_EQ(item2, manager_.GetItemById(id2)); | 77 ASSERT_EQ(item2, manager_.GetItemById(id2)); |
| 76 items = manager_.MenuItems(item2->extension_id()); | 78 items = manager_.MenuItems(item2->extension_id()); |
| 77 ASSERT_EQ(2u, items->size()); | 79 ASSERT_EQ(2u, items->size()); |
| 78 ASSERT_EQ(item1, items->at(0)); | 80 ASSERT_EQ(item1, items->at(0)); |
| 79 ASSERT_EQ(item2, items->at(1)); | 81 ASSERT_EQ(item2, items->at(1)); |
| 80 | 82 |
| 81 // Try adding item 3, then removing it. | 83 // Try adding item 3, then removing it. |
| 82 ExtensionMenuItem* item3 = CreateTestItem(NULL); | 84 ExtensionMenuItem* item3 = CreateTestItem(extension); |
| 83 std::string extension_id = item3->extension_id(); | 85 std::string extension_id = item3->extension_id(); |
| 84 int id3 = manager_.AddContextItem(item3); // Ownership transferred. | 86 int id3 = manager_.AddContextItem(extension, item3); |
| 85 ASSERT_GT(id3, 0); | 87 ASSERT_GT(id3, 0); |
| 86 ASSERT_EQ(item3, manager_.GetItemById(id3)); | 88 ASSERT_EQ(item3, manager_.GetItemById(id3)); |
| 87 ASSERT_EQ(3u, manager_.MenuItems(extension_id)->size()); | 89 ASSERT_EQ(3u, manager_.MenuItems(extension_id)->size()); |
| 88 ASSERT_TRUE(manager_.RemoveContextMenuItem(id3)); | 90 ASSERT_TRUE(manager_.RemoveContextMenuItem(id3)); |
| 89 ASSERT_EQ(NULL, manager_.GetItemById(id3)); | 91 ASSERT_EQ(NULL, manager_.GetItemById(id3)); |
| 90 ASSERT_EQ(2u, manager_.MenuItems(extension_id)->size()); | 92 ASSERT_EQ(2u, manager_.MenuItems(extension_id)->size()); |
| 91 | 93 |
| 92 // Make sure removing a non-existent item returns false. | 94 // Make sure removing a non-existent item returns false. |
| 93 ASSERT_FALSE(manager_.RemoveContextMenuItem(5)); | 95 ASSERT_FALSE(manager_.RemoveContextMenuItem(5)); |
| 94 } | 96 } |
| 95 | 97 |
| 96 // Test adding/removing child items. | 98 // Test adding/removing child items. |
| 97 TEST_F(ExtensionMenuManagerTest, ChildFunctions) { | 99 TEST_F(ExtensionMenuManagerTest, ChildFunctions) { |
| 98 DictionaryValue properties; | 100 Extension* extension1 = AddExtension("1111"); |
| 99 properties.SetString(L"extension_id", "1111"); | 101 Extension* extension2 = AddExtension("2222"); |
| 100 ExtensionMenuItem* item1 = CreateTestItem(&properties); | 102 Extension* extension3 = AddExtension("3333"); |
| 101 | 103 |
| 102 properties.SetString(L"extension_id", "2222"); | 104 ExtensionMenuItem* item1 = CreateTestItem(extension1); |
| 103 ExtensionMenuItem* item2 = CreateTestItem(&properties); | 105 ExtensionMenuItem* item2 = CreateTestItem(extension2); |
| 104 ExtensionMenuItem* item2_child = CreateTestItem(&properties); | 106 ExtensionMenuItem* item2_child = CreateTestItem(extension2); |
| 105 ExtensionMenuItem* item2_grandchild = CreateTestItem(&properties); | 107 ExtensionMenuItem* item2_grandchild = CreateTestItem(extension2); |
| 106 | 108 |
| 107 // This third item we expect to fail inserting, so we use a scoped_ptr to make | 109 // This third item we expect to fail inserting, so we use a scoped_ptr to make |
| 108 // sure it gets deleted. | 110 // sure it gets deleted. |
| 109 properties.SetString(L"extension_id", "3333"); | 111 scoped_ptr<ExtensionMenuItem> item3(CreateTestItem(extension3)); |
| 110 scoped_ptr<ExtensionMenuItem> item3(CreateTestItem(&properties)); | |
| 111 | 112 |
| 112 // Add in the first two items. | 113 // Add in the first two items. |
| 113 int id1 = manager_.AddContextItem(item1); // Ownership transferred. | 114 int id1 = manager_.AddContextItem(extension1, item1); |
| 114 int id2 = manager_.AddContextItem(item2); // Ownership transferred. | 115 int id2 = manager_.AddContextItem(extension2, item2); |
| 115 | 116 |
| 116 ASSERT_NE(id1, id2); | 117 ASSERT_NE(id1, id2); |
| 117 | 118 |
| 118 // Try adding item3 as a child of item2 - this should fail because item3 has | 119 // Try adding item3 as a child of item2 - this should fail because item3 has |
| 119 // a different extension id. | 120 // a different extension id. |
| 120 ASSERT_EQ(0, manager_.AddChildItem(id2, item3.get())); | 121 ASSERT_EQ(0, manager_.AddChildItem(id2, item3.get())); |
| 121 | 122 |
| 122 // Add item2_child as a child of item2. | 123 // Add item2_child as a child of item2. |
| 123 int id2_child = manager_.AddChildItem(id2, item2_child); | 124 int id2_child = manager_.AddChildItem(id2, item2_child); |
| 124 ASSERT_GT(id2_child, 0); | 125 ASSERT_GT(id2_child, 0); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 143 | 144 |
| 144 // Remove child2_item. | 145 // Remove child2_item. |
| 145 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_child)); | 146 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_child)); |
| 146 ASSERT_EQ(1u, manager_.MenuItems(item2->extension_id())->size()); | 147 ASSERT_EQ(1u, manager_.MenuItems(item2->extension_id())->size()); |
| 147 ASSERT_EQ(item2, manager_.MenuItems(item2->extension_id())->at(0)); | 148 ASSERT_EQ(item2, manager_.MenuItems(item2->extension_id())->at(0)); |
| 148 ASSERT_EQ(0, item2->child_count()); | 149 ASSERT_EQ(0, item2->child_count()); |
| 149 } | 150 } |
| 150 | 151 |
| 151 // Tests changing parents. | 152 // Tests changing parents. |
| 152 TEST_F(ExtensionMenuManagerTest, ChangeParent) { | 153 TEST_F(ExtensionMenuManagerTest, ChangeParent) { |
| 154 Extension* extension1 = AddExtension("1111"); |
| 155 |
| 153 // First create two items and add them both to the manager. | 156 // First create two items and add them both to the manager. |
| 154 ExtensionMenuItem* item1 = CreateTestItem(NULL); | 157 ExtensionMenuItem* item1 = CreateTestItem(extension1); |
| 155 ExtensionMenuItem* item2 = CreateTestItem(NULL); | 158 ExtensionMenuItem* item2 = CreateTestItem(extension1); |
| 156 | 159 |
| 157 int id1 = manager_.AddContextItem(item1); | 160 int id1 = manager_.AddContextItem(extension1, item1); |
| 158 ASSERT_GT(id1, 0); | 161 ASSERT_GT(id1, 0); |
| 159 int id2 = manager_.AddContextItem(item2); | 162 int id2 = manager_.AddContextItem(extension1, item2); |
| 160 ASSERT_GT(id2, 0); | 163 ASSERT_GT(id2, 0); |
| 161 | 164 |
| 162 const ExtensionMenuItem::List* items = | 165 const ExtensionMenuItem::List* items = |
| 163 manager_.MenuItems(item1->extension_id()); | 166 manager_.MenuItems(item1->extension_id()); |
| 164 ASSERT_EQ(2u, items->size()); | 167 ASSERT_EQ(2u, items->size()); |
| 165 ASSERT_EQ(item1, items->at(0)); | 168 ASSERT_EQ(item1, items->at(0)); |
| 166 ASSERT_EQ(item2, items->at(1)); | 169 ASSERT_EQ(item2, items->at(1)); |
| 167 | 170 |
| 168 // Now create a third item, initially add it as a child of item1, then move | 171 // Now create a third item, initially add it as a child of item1, then move |
| 169 // it to be a child of item2. | 172 // it to be a child of item2. |
| 170 ExtensionMenuItem* item3 = CreateTestItem(NULL); | 173 ExtensionMenuItem* item3 = CreateTestItem(extension1); |
| 171 | 174 |
| 172 int id3 = manager_.AddChildItem(id1, item3); | 175 int id3 = manager_.AddChildItem(id1, item3); |
| 173 ASSERT_GT(id3, 0); | 176 ASSERT_GT(id3, 0); |
| 174 ASSERT_EQ(1, item1->child_count()); | 177 ASSERT_EQ(1, item1->child_count()); |
| 175 ASSERT_EQ(item3, item1->children()[0]); | 178 ASSERT_EQ(item3, item1->children()[0]); |
| 176 | 179 |
| 177 ASSERT_TRUE(manager_.ChangeParent(id3, id2)); | 180 ASSERT_TRUE(manager_.ChangeParent(id3, id2)); |
| 178 ASSERT_EQ(0, item1->child_count()); | 181 ASSERT_EQ(0, item1->child_count()); |
| 179 ASSERT_EQ(1, item2->child_count()); | 182 ASSERT_EQ(1, item2->child_count()); |
| 180 ASSERT_EQ(item3, item2->children()[0]); | 183 ASSERT_EQ(item3, item2->children()[0]); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 211 // Move item2 to be a top-level item. | 214 // Move item2 to be a top-level item. |
| 212 ASSERT_TRUE(manager_.ChangeParent(id2, 0)); | 215 ASSERT_TRUE(manager_.ChangeParent(id2, 0)); |
| 213 items = manager_.MenuItems(item1->extension_id()); | 216 items = manager_.MenuItems(item1->extension_id()); |
| 214 ASSERT_EQ(2u, items->size()); | 217 ASSERT_EQ(2u, items->size()); |
| 215 ASSERT_EQ(item1, items->at(0)); | 218 ASSERT_EQ(item1, items->at(0)); |
| 216 ASSERT_EQ(item2, items->at(1)); | 219 ASSERT_EQ(item2, items->at(1)); |
| 217 ASSERT_EQ(1, item1->child_count()); | 220 ASSERT_EQ(1, item1->child_count()); |
| 218 ASSERT_EQ(item3, item1->children()[0]); | 221 ASSERT_EQ(item3, item1->children()[0]); |
| 219 | 222 |
| 220 // Make sure you can't move a node to be a child of another extension's item. | 223 // Make sure you can't move a node to be a child of another extension's item. |
| 221 DictionaryValue properties; | 224 Extension* extension2 = AddExtension("2222"); |
| 222 properties.SetString(L"extension_id", "4444"); | 225 ExtensionMenuItem* item4 = CreateTestItem(extension2); |
| 223 ExtensionMenuItem* item4 = CreateTestItem(&properties); | 226 int id4 = manager_.AddContextItem(extension2, item4); |
| 224 int id4 = manager_.AddContextItem(item4); | |
| 225 ASSERT_GT(id4, 0); | 227 ASSERT_GT(id4, 0); |
| 226 ASSERT_FALSE(manager_.ChangeParent(id4, id1)); | 228 ASSERT_FALSE(manager_.ChangeParent(id4, id1)); |
| 227 ASSERT_FALSE(manager_.ChangeParent(id1, id4)); | 229 ASSERT_FALSE(manager_.ChangeParent(id1, id4)); |
| 228 | 230 |
| 229 // Make sure you can't make an item be it's own parent. | 231 // Make sure you can't make an item be it's own parent. |
| 230 ASSERT_FALSE(manager_.ChangeParent(id1, id1)); | 232 ASSERT_FALSE(manager_.ChangeParent(id1, id1)); |
| 231 } | 233 } |
| 232 | 234 |
| 233 // Tests that we properly remove an extension's menu item when that extension is | 235 // Tests that we properly remove an extension's menu item when that extension is |
| 234 // unloaded. | 236 // unloaded. |
| 235 TEST_F(ExtensionMenuManagerTest, ExtensionUnloadRemovesMenuItems) { | 237 TEST_F(ExtensionMenuManagerTest, ExtensionUnloadRemovesMenuItems) { |
| 236 ScopedTempDir temp_dir; | |
| 237 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
| 238 | |
| 239 NotificationService* notifier = NotificationService::current(); | 238 NotificationService* notifier = NotificationService::current(); |
| 240 ASSERT_TRUE(notifier != NULL); | 239 ASSERT_TRUE(notifier != NULL); |
| 241 | 240 |
| 242 // Create a test extension. | 241 // Create a test extension. |
| 243 DictionaryValue extension_properties; | 242 Extension* extension1 = AddExtension("1111"); |
| 244 extension_properties.SetString(extension_manifest_keys::kVersion, "1"); | |
| 245 extension_properties.SetString(extension_manifest_keys::kName, "Test"); | |
| 246 Extension extension(temp_dir.path().AppendASCII("extension")); | |
| 247 std::string errors; | |
| 248 ASSERT_TRUE(extension.InitFromValue(extension_properties, | |
| 249 false, // No public key required. | |
| 250 &errors)) << errors; | |
| 251 | 243 |
| 252 // Create an ExtensionMenuItem and put it into the manager. | 244 // Create an ExtensionMenuItem and put it into the manager. |
| 253 DictionaryValue item_properties; | 245 ExtensionMenuItem* item1 = CreateTestItem(extension1); |
| 254 item_properties.SetString(L"extension_id", extension.id()); | 246 ASSERT_EQ(extension1->id(), item1->extension_id()); |
| 255 ExtensionMenuItem* item1 = CreateTestItem(&item_properties); | 247 int id1 = manager_.AddContextItem(extension1, item1); |
| 256 ASSERT_EQ(extension.id(), item1->extension_id()); | |
| 257 int id1 = manager_.AddContextItem(item1); // Ownership transferred. | |
| 258 ASSERT_GT(id1, 0); | 248 ASSERT_GT(id1, 0); |
| 259 ASSERT_EQ(1u, manager_.MenuItems(extension.id())->size()); | 249 ASSERT_EQ(1u, manager_.MenuItems(extension1->id())->size()); |
| 260 | 250 |
| 261 // Create a menu item with a different extension id and add it to the manager. | 251 // Create a menu item with a different extension id and add it to the manager. |
| 262 std::string alternate_extension_id = "0000"; | 252 Extension* extension2 = AddExtension("2222"); |
| 263 item_properties.SetString(L"extension_id", alternate_extension_id); | 253 ExtensionMenuItem* item2 = CreateTestItem(extension2); |
| 264 ExtensionMenuItem* item2 = CreateTestItem(&item_properties); | |
| 265 ASSERT_NE(item1->extension_id(), item2->extension_id()); | 254 ASSERT_NE(item1->extension_id(), item2->extension_id()); |
| 266 int id2 = manager_.AddContextItem(item2); // Ownership transferred. | 255 int id2 = manager_.AddContextItem(extension2, item2); |
| 267 ASSERT_GT(id2, 0); | 256 ASSERT_GT(id2, 0); |
| 268 | 257 |
| 269 // Notify that the extension was unloaded, and make sure the right item is | 258 // Notify that the extension was unloaded, and make sure the right item is |
| 270 // gone. | 259 // gone. |
| 271 notifier->Notify(NotificationType::EXTENSION_UNLOADED, | 260 notifier->Notify(NotificationType::EXTENSION_UNLOADED, |
| 272 Source<Profile>(NULL), | 261 Source<Profile>(NULL), |
| 273 Details<Extension>(&extension)); | 262 Details<Extension>(extension1)); |
| 274 ASSERT_EQ(NULL, manager_.MenuItems(extension.id())); | 263 ASSERT_EQ(NULL, manager_.MenuItems(extension1->id())); |
| 275 ASSERT_EQ(1u, manager_.MenuItems(alternate_extension_id)->size()); | 264 ASSERT_EQ(1u, manager_.MenuItems(extension2->id())->size()); |
| 276 ASSERT_TRUE(manager_.GetItemById(id1) == NULL); | 265 ASSERT_TRUE(manager_.GetItemById(id1) == NULL); |
| 277 ASSERT_TRUE(manager_.GetItemById(id2) != NULL); | 266 ASSERT_TRUE(manager_.GetItemById(id2) != NULL); |
| 278 } | 267 } |
| 279 | 268 |
| 280 // A mock message service for tests of ExtensionMenuManager::ExecuteCommand. | 269 // A mock message service for tests of ExtensionMenuManager::ExecuteCommand. |
| 281 class MockExtensionMessageService : public ExtensionMessageService { | 270 class MockExtensionMessageService : public ExtensionMessageService { |
| 282 public: | 271 public: |
| 283 explicit MockExtensionMessageService(Profile* profile) : | 272 explicit MockExtensionMessageService(Profile* profile) : |
| 284 ExtensionMessageService(profile) {} | 273 ExtensionMessageService(profile) {} |
| 285 | 274 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 301 | 290 |
| 302 private: | 291 private: |
| 303 DISALLOW_COPY_AND_ASSIGN(MockTestingProfile); | 292 DISALLOW_COPY_AND_ASSIGN(MockTestingProfile); |
| 304 }; | 293 }; |
| 305 | 294 |
| 306 // Tests the RemoveAll functionality. | 295 // Tests the RemoveAll functionality. |
| 307 TEST_F(ExtensionMenuManagerTest, RemoveAll) { | 296 TEST_F(ExtensionMenuManagerTest, RemoveAll) { |
| 308 // Try removing all items for an extension id that doesn't have any items. | 297 // Try removing all items for an extension id that doesn't have any items. |
| 309 manager_.RemoveAllContextItems("CCCC"); | 298 manager_.RemoveAllContextItems("CCCC"); |
| 310 | 299 |
| 311 // Add 2 top-level and one child item for extension id AAAA. | 300 // Add 2 top-level and one child item for extension 1. |
| 312 DictionaryValue properties; | 301 Extension* extension1 = AddExtension("1111"); |
| 313 properties.SetString(L"extension_id", "AAAA"); | 302 ExtensionMenuItem* item1 = CreateTestItem(extension1); |
| 314 ExtensionMenuItem* item1 = CreateTestItem(&properties); | 303 ExtensionMenuItem* item2 = CreateTestItem(extension1); |
| 315 ExtensionMenuItem* item2 = CreateTestItem(&properties); | 304 ExtensionMenuItem* item3 = CreateTestItem(extension1); |
| 316 ExtensionMenuItem* item3 = CreateTestItem(&properties); | 305 int id1 = manager_.AddContextItem(extension1, item1); |
| 317 int id1 = manager_.AddContextItem(item1); | 306 int id2 = manager_.AddContextItem(extension1, item2); |
| 318 int id2 = manager_.AddContextItem(item2); | |
| 319 EXPECT_GT(id1, 0); | 307 EXPECT_GT(id1, 0); |
| 320 EXPECT_GT(id2, 0); | 308 EXPECT_GT(id2, 0); |
| 321 int id3 = manager_.AddChildItem(id1, item3); | 309 int id3 = manager_.AddChildItem(id1, item3); |
| 322 EXPECT_GT(id3, 0); | 310 EXPECT_GT(id3, 0); |
| 323 | 311 |
| 324 // Add one top-level item for extension id BBBB. | 312 // Add one top-level item for extension 2. |
| 325 properties.SetString(L"extension_id", "BBBB"); | 313 Extension* extension2 = AddExtension("2222"); |
| 326 ExtensionMenuItem* item4 = CreateTestItem(&properties); | 314 ExtensionMenuItem* item4 = CreateTestItem(extension2); |
| 327 manager_.AddContextItem(item4); | 315 manager_.AddContextItem(extension2, item4); |
| 328 | 316 |
| 329 EXPECT_EQ(2u, manager_.MenuItems("AAAA")->size()); | 317 EXPECT_EQ(2u, manager_.MenuItems(extension1->id())->size()); |
| 330 EXPECT_EQ(1u, manager_.MenuItems("BBBB")->size()); | 318 EXPECT_EQ(1u, manager_.MenuItems(extension2->id())->size()); |
| 331 | 319 |
| 332 // Remove the BBBB item. | 320 // Remove extension2's item. |
| 333 manager_.RemoveAllContextItems("BBBB"); | 321 manager_.RemoveAllContextItems(extension2->id()); |
| 334 EXPECT_EQ(2u, manager_.MenuItems("AAAA")->size()); | 322 EXPECT_EQ(2u, manager_.MenuItems(extension1->id())->size()); |
| 335 EXPECT_EQ(NULL, manager_.MenuItems("BBBB")); | 323 EXPECT_EQ(NULL, manager_.MenuItems(extension2->id())); |
| 336 | 324 |
| 337 // Remove the AAAA items. | 325 // Remove extension1's items. |
| 338 manager_.RemoveAllContextItems("AAAA"); | 326 manager_.RemoveAllContextItems(extension1->id()); |
| 339 EXPECT_EQ(NULL, manager_.MenuItems("AAAA")); | 327 EXPECT_EQ(NULL, manager_.MenuItems(extension1->id())); |
| 340 } | 328 } |
| 341 | 329 |
| 342 TEST_F(ExtensionMenuManagerTest, ExecuteCommand) { | 330 TEST_F(ExtensionMenuManagerTest, ExecuteCommand) { |
| 343 MessageLoopForUI message_loop; | 331 MessageLoopForUI message_loop; |
| 344 ChromeThread ui_thread(ChromeThread::UI, &message_loop); | 332 ChromeThread ui_thread(ChromeThread::UI, &message_loop); |
| 345 | 333 |
| 346 MockTestingProfile profile; | 334 MockTestingProfile profile; |
| 347 | 335 |
| 348 scoped_refptr<MockExtensionMessageService> mock_message_service = | 336 scoped_refptr<MockExtensionMessageService> mock_message_service = |
| 349 new MockExtensionMessageService(&profile); | 337 new MockExtensionMessageService(&profile); |
| 350 | 338 |
| 351 ContextMenuParams params; | 339 ContextMenuParams params; |
| 352 params.media_type = WebKit::WebContextMenuData::MediaTypeImage; | 340 params.media_type = WebKit::WebContextMenuData::MediaTypeImage; |
| 353 params.src_url = GURL("http://foo.bar/image.png"); | 341 params.src_url = GURL("http://foo.bar/image.png"); |
| 354 params.page_url = GURL("http://foo.bar"); | 342 params.page_url = GURL("http://foo.bar"); |
| 355 params.selection_text = L"Hello World"; | 343 params.selection_text = L"Hello World"; |
| 356 params.is_editable = false; | 344 params.is_editable = false; |
| 357 | 345 |
| 358 ExtensionMenuItem* item = CreateTestItem(NULL); | 346 Extension* extension = AddExtension("test"); |
| 359 int id = manager_.AddContextItem(item); // Ownership transferred. | 347 ExtensionMenuItem* item = CreateTestItem(extension); |
| 348 int id = manager_.AddContextItem(extension, item); |
| 360 ASSERT_GT(id, 0); | 349 ASSERT_GT(id, 0); |
| 361 | 350 |
| 362 EXPECT_CALL(profile, GetExtensionMessageService()) | 351 EXPECT_CALL(profile, GetExtensionMessageService()) |
| 363 .Times(1) | 352 .Times(1) |
| 364 .WillOnce(Return(mock_message_service.get())); | 353 .WillOnce(Return(mock_message_service.get())); |
| 365 | 354 |
| 366 EXPECT_CALL(profile, IsOffTheRecord()) | 355 EXPECT_CALL(profile, IsOffTheRecord()) |
| 367 .Times(AtLeast(1)) | 356 .Times(AtLeast(1)) |
| 368 .WillRepeatedly(Return(false)); | 357 .WillRepeatedly(Return(false)); |
| 369 | 358 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 ASSERT_EQ(params.page_url.spec(), tmp); | 395 ASSERT_EQ(params.page_url.spec(), tmp); |
| 407 | 396 |
| 408 std::wstring wide_tmp; | 397 std::wstring wide_tmp; |
| 409 ASSERT_TRUE(info->GetString(L"selectionText", &wide_tmp)); | 398 ASSERT_TRUE(info->GetString(L"selectionText", &wide_tmp)); |
| 410 ASSERT_EQ(params.selection_text, wide_tmp); | 399 ASSERT_EQ(params.selection_text, wide_tmp); |
| 411 | 400 |
| 412 bool bool_tmp = true; | 401 bool bool_tmp = true; |
| 413 ASSERT_TRUE(info->GetBoolean(L"editable", &bool_tmp)); | 402 ASSERT_TRUE(info->GetBoolean(L"editable", &bool_tmp)); |
| 414 ASSERT_EQ(params.is_editable, bool_tmp); | 403 ASSERT_EQ(params.is_editable, bool_tmp); |
| 415 } | 404 } |
| OLD | NEW |