[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 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 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 7 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 8 | #include "base/json/json_reader.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_vector.h" |
[email protected] | b19fe57 | 2013-07-18 04:54:26 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 11 | #include "base/path_service.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 12 | #include "base/prefs/pref_service.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 49a01e64 | 2013-07-12 00:29:45 | [diff] [blame] | 15 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extension_system_factory.h" |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 17 | #include "chrome/browser/extensions/menu_manager.h" |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 18 | #include "chrome/browser/extensions/test_extension_prefs.h" |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 19 | #include "chrome/browser/extensions/test_extension_system.h" |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 20 | #include "chrome/browser/prefs/pref_service_syncable.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 21 | #include "chrome/common/chrome_paths.h" |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 22 | #include "chrome/common/extensions/api/context_menus.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 23 | #include "chrome/test/base/testing_profile.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 24 | #include "content/public/browser/notification_service.h" |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 25 | #include "content/public/common/context_menu_params.h" |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 26 | #include "content/public/test/test_browser_thread.h" |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 27 | #include "extensions/browser/event_router.h" |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 28 | #include "extensions/browser/extension_registry.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 29 | #include "extensions/browser/extension_system.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 30 | #include "extensions/common/extension.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 31 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f20d733 | 2011-03-08 21:11:53 | [diff] [blame] | 32 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 33 | |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 34 | using content::BrowserThread; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 35 | using testing::_; |
| 36 | using testing::AtLeast; |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 37 | using testing::DeleteArg; |
[email protected] | bc4ae15 | 2012-06-01 22:23:34 | [diff] [blame] | 38 | using testing::InSequence; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 39 | using testing::Return; |
| 40 | using testing::SaveArg; |
| 41 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 42 | namespace extensions { |
| 43 | |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 44 | namespace context_menus = api::context_menus; |
| 45 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 46 | // Base class for tests. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 47 | class MenuManagerTest : public testing::Test { |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 48 | public: |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 49 | MenuManagerTest() |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 50 | : ui_thread_(BrowserThread::UI, &message_loop_), |
| 51 | file_thread_(BrowserThread::FILE, &message_loop_), |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 52 | profile_(new TestingProfile()), |
| 53 | manager_(profile_.get(), |
| 54 | ExtensionSystem::Get(profile_.get())->state_store()), |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 55 | prefs_(message_loop_.message_loop_proxy().get()), |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 56 | next_id_(1) {} |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 57 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame^] | 58 | void TearDown() override { |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 59 | prefs_.pref_service()->CommitPendingWrite(); |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 60 | message_loop_.RunUntilIdle(); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 63 | // Returns a test item. |
[email protected] | 6350346 | 2012-10-30 22:14:31 | [diff] [blame] | 64 | MenuItem* CreateTestItem(Extension* extension, bool incognito = false) { |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 65 | MenuItem::Type type = MenuItem::NORMAL; |
| 66 | MenuItem::ContextList contexts(MenuItem::ALL); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 67 | const MenuItem::ExtensionKey key(extension->id()); |
| 68 | MenuItem::Id id(incognito, key); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 69 | id.uid = next_id_++; |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 70 | return new MenuItem(id, "test", false, true, type, contexts); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Returns a test item with the given string ID. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 74 | MenuItem* CreateTestItemWithID(Extension* extension, |
| 75 | const std::string& string_id) { |
| 76 | MenuItem::Type type = MenuItem::NORMAL; |
| 77 | MenuItem::ContextList contexts(MenuItem::ALL); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 78 | const MenuItem::ExtensionKey key(extension->id()); |
| 79 | MenuItem::Id id(false, key); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 80 | id.string_uid = string_id; |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 81 | return new MenuItem(id, "test", false, true, type, contexts); |
[email protected] | 06a0c94e | 2010-06-24 23:20:15 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 84 | // Creates and returns a test Extension. The caller does *not* own the return |
| 85 | // value. |
[email protected] | 0bb29bd | 2014-04-30 21:39:18 | [diff] [blame] | 86 | Extension* AddExtension(const std::string& name) { |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 87 | scoped_refptr<Extension> extension = prefs_.AddExtension(name); |
[email protected] | 819faeb0 | 2010-06-25 21:16:22 | [diff] [blame] | 88 | extensions_.push_back(extension); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 89 | return extension.get(); |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 92 | protected: |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 93 | base::MessageLoopForUI message_loop_; |
| 94 | content::TestBrowserThread ui_thread_; |
| 95 | content::TestBrowserThread file_thread_; |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 96 | scoped_ptr<TestingProfile> profile_; |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 97 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 98 | MenuManager manager_; |
| 99 | ExtensionList extensions_; |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 100 | TestExtensionPrefs prefs_; |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 101 | int next_id_; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 102 | |
| 103 | private: |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(MenuManagerTest); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // Tests adding, getting, and removing items. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 108 | TEST_F(MenuManagerTest, AddGetRemoveItems) { |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 109 | Extension* extension = AddExtension("test"); |
| 110 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 111 | // Add a new item, make sure you can get it back. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 112 | MenuItem* item1 = CreateTestItem(extension); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 113 | ASSERT_TRUE(item1 != NULL); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 114 | ASSERT_TRUE(manager_.AddContextItem(extension, item1)); |
| 115 | ASSERT_EQ(item1, manager_.GetItemById(item1->id())); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 116 | const MenuItem::List* items = manager_.MenuItems(item1->id().extension_key); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 117 | ASSERT_EQ(1u, items->size()); |
| 118 | ASSERT_EQ(item1, items->at(0)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 119 | |
| 120 | // Add a second item, make sure it comes back too. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 121 | MenuItem* item2 = CreateTestItemWithID(extension, "id2"); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 122 | ASSERT_TRUE(manager_.AddContextItem(extension, item2)); |
| 123 | ASSERT_EQ(item2, manager_.GetItemById(item2->id())); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 124 | items = manager_.MenuItems(item2->id().extension_key); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 125 | ASSERT_EQ(2u, items->size()); |
| 126 | ASSERT_EQ(item1, items->at(0)); |
| 127 | ASSERT_EQ(item2, items->at(1)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 128 | |
| 129 | // Try adding item 3, then removing it. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 130 | MenuItem* item3 = CreateTestItem(extension); |
| 131 | MenuItem::Id id3 = item3->id(); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 132 | const MenuItem::ExtensionKey extension_key3(item3->id().extension_key); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 133 | ASSERT_TRUE(manager_.AddContextItem(extension, item3)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 134 | ASSERT_EQ(item3, manager_.GetItemById(id3)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 135 | ASSERT_EQ(3u, manager_.MenuItems(extension_key3)->size()); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 136 | ASSERT_TRUE(manager_.RemoveContextMenuItem(id3)); |
| 137 | ASSERT_EQ(NULL, manager_.GetItemById(id3)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 138 | ASSERT_EQ(2u, manager_.MenuItems(extension_key3)->size()); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 139 | |
| 140 | // Make sure removing a non-existent item returns false. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 141 | const MenuItem::ExtensionKey key(extension->id()); |
| 142 | MenuItem::Id id(false, key); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 143 | id.uid = id3.uid + 50; |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 144 | ASSERT_FALSE(manager_.RemoveContextMenuItem(id)); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 145 | |
| 146 | // Make sure adding an item with the same string ID returns false. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 147 | scoped_ptr<MenuItem> item2too(CreateTestItemWithID(extension, "id2")); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 148 | ASSERT_FALSE(manager_.AddContextItem(extension, item2too.get())); |
| 149 | |
| 150 | // But the same string ID should not collide with another extension. |
| 151 | Extension* extension2 = AddExtension("test2"); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 152 | MenuItem* item2other = CreateTestItemWithID(extension2, "id2"); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 153 | ASSERT_TRUE(manager_.AddContextItem(extension2, item2other)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Test adding/removing child items. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 157 | TEST_F(MenuManagerTest, ChildFunctions) { |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 158 | Extension* extension1 = AddExtension("1111"); |
| 159 | Extension* extension2 = AddExtension("2222"); |
| 160 | Extension* extension3 = AddExtension("3333"); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 161 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 162 | MenuItem* item1 = CreateTestItem(extension1); |
| 163 | MenuItem* item2 = CreateTestItem(extension2); |
| 164 | MenuItem* item2_child = CreateTestItemWithID(extension2, "2child"); |
| 165 | MenuItem* item2_grandchild = CreateTestItem(extension2); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 166 | |
| 167 | // This third item we expect to fail inserting, so we use a scoped_ptr to make |
| 168 | // sure it gets deleted. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 169 | scoped_ptr<MenuItem> item3(CreateTestItem(extension3)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 170 | |
| 171 | // Add in the first two items. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 172 | ASSERT_TRUE(manager_.AddContextItem(extension1, item1)); |
| 173 | ASSERT_TRUE(manager_.AddContextItem(extension2, item2)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 174 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 175 | MenuItem::Id id1 = item1->id(); |
| 176 | MenuItem::Id id2 = item2->id(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 177 | |
| 178 | // Try adding item3 as a child of item2 - this should fail because item3 has |
| 179 | // a different extension id. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 180 | ASSERT_FALSE(manager_.AddChildItem(id2, item3.get())); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 181 | |
| 182 | // Add item2_child as a child of item2. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 183 | MenuItem::Id id2_child = item2_child->id(); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 184 | ASSERT_TRUE(manager_.AddChildItem(id2, item2_child)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 185 | ASSERT_EQ(1, item2->child_count()); |
| 186 | ASSERT_EQ(0, item1->child_count()); |
| 187 | ASSERT_EQ(item2_child, manager_.GetItemById(id2_child)); |
| 188 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 189 | ASSERT_EQ(1u, manager_.MenuItems(item1->id().extension_key)->size()); |
| 190 | ASSERT_EQ(item1, manager_.MenuItems(item1->id().extension_key)->at(0)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 191 | |
| 192 | // Add item2_grandchild as a child of item2_child, then remove it. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 193 | MenuItem::Id id2_grandchild = item2_grandchild->id(); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 194 | ASSERT_TRUE(manager_.AddChildItem(id2_child, item2_grandchild)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 195 | ASSERT_EQ(1, item2->child_count()); |
| 196 | ASSERT_EQ(1, item2_child->child_count()); |
| 197 | ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_grandchild)); |
| 198 | |
| 199 | // We should only get 1 thing back when asking for item2's extension id, since |
| 200 | // it has a child item. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 201 | ASSERT_EQ(1u, manager_.MenuItems(item2->id().extension_key)->size()); |
| 202 | ASSERT_EQ(item2, manager_.MenuItems(item2->id().extension_key)->at(0)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 203 | |
| 204 | // Remove child2_item. |
| 205 | ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_child)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 206 | ASSERT_EQ(1u, manager_.MenuItems(item2->id().extension_key)->size()); |
| 207 | ASSERT_EQ(item2, manager_.MenuItems(item2->id().extension_key)->at(0)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 208 | ASSERT_EQ(0, item2->child_count()); |
| 209 | } |
| 210 | |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 211 | TEST_F(MenuManagerTest, PopulateFromValue) { |
| 212 | Extension* extension = AddExtension("test"); |
| 213 | |
| 214 | bool incognito = true; |
| 215 | int type = MenuItem::CHECKBOX; |
| 216 | std::string title("TITLE"); |
| 217 | bool checked = true; |
| 218 | bool enabled = true; |
| 219 | MenuItem::ContextList contexts; |
| 220 | contexts.Add(MenuItem::PAGE); |
| 221 | contexts.Add(MenuItem::SELECTION); |
| 222 | int contexts_value = 0; |
| 223 | ASSERT_TRUE(contexts.ToValue()->GetAsInteger(&contexts_value)); |
| 224 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 225 | base::ListValue* document_url_patterns(new base::ListValue()); |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 226 | document_url_patterns->Append( |
[email protected] | aa15e2e | 2013-08-14 02:13:58 | [diff] [blame] | 227 | new base::StringValue("http://www.google.com/*")); |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 228 | document_url_patterns->Append( |
[email protected] | aa15e2e | 2013-08-14 02:13:58 | [diff] [blame] | 229 | new base::StringValue("http://www.reddit.com/*")); |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 230 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 231 | base::ListValue* target_url_patterns(new base::ListValue()); |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 232 | target_url_patterns->Append( |
[email protected] | aa15e2e | 2013-08-14 02:13:58 | [diff] [blame] | 233 | new base::StringValue("http://www.yahoo.com/*")); |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 234 | target_url_patterns->Append( |
[email protected] | aa15e2e | 2013-08-14 02:13:58 | [diff] [blame] | 235 | new base::StringValue("http://www.facebook.com/*")); |
[email protected] | 8af81c0 | 2012-08-14 23:06:15 | [diff] [blame] | 236 | |
| 237 | base::DictionaryValue value; |
| 238 | value.SetBoolean("incognito", incognito); |
| 239 | value.SetString("string_uid", std::string()); |
| 240 | value.SetInteger("type", type); |
| 241 | value.SetString("title", title); |
| 242 | value.SetBoolean("checked", checked); |
| 243 | value.SetBoolean("enabled", enabled); |
| 244 | value.SetInteger("contexts", contexts_value); |
| 245 | value.Set("document_url_patterns", document_url_patterns); |
| 246 | value.Set("target_url_patterns", target_url_patterns); |
| 247 | |
| 248 | std::string error; |
| 249 | scoped_ptr<MenuItem> item(MenuItem::Populate(extension->id(), value, &error)); |
| 250 | ASSERT_TRUE(item.get()); |
| 251 | |
| 252 | EXPECT_EQ(extension->id(), item->extension_id()); |
| 253 | EXPECT_EQ(incognito, item->incognito()); |
| 254 | EXPECT_EQ(title, item->title()); |
| 255 | EXPECT_EQ(checked, item->checked()); |
| 256 | EXPECT_EQ(item->checked(), item->checked()); |
| 257 | EXPECT_EQ(enabled, item->enabled()); |
| 258 | EXPECT_EQ(contexts, item->contexts()); |
| 259 | |
| 260 | URLPatternSet document_url_pattern_set; |
| 261 | document_url_pattern_set.Populate(*document_url_patterns, |
| 262 | URLPattern::SCHEME_ALL, |
| 263 | true, |
| 264 | &error); |
| 265 | EXPECT_EQ(document_url_pattern_set, item->document_url_patterns()); |
| 266 | |
| 267 | URLPatternSet target_url_pattern_set; |
| 268 | target_url_pattern_set.Populate(*target_url_patterns, |
| 269 | URLPattern::SCHEME_ALL, |
| 270 | true, |
| 271 | &error); |
| 272 | EXPECT_EQ(target_url_pattern_set, item->target_url_patterns()); |
| 273 | } |
| 274 | |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 275 | // Tests that deleting a parent properly removes descendants. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 276 | TEST_F(MenuManagerTest, DeleteParent) { |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 277 | Extension* extension = AddExtension("1111"); |
| 278 | |
| 279 | // Set up 5 items to add. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 280 | MenuItem* item1 = CreateTestItem(extension); |
| 281 | MenuItem* item2 = CreateTestItem(extension); |
| 282 | MenuItem* item3 = CreateTestItemWithID(extension, "id3"); |
| 283 | MenuItem* item4 = CreateTestItemWithID(extension, "id4"); |
| 284 | MenuItem* item5 = CreateTestItem(extension); |
| 285 | MenuItem* item6 = CreateTestItem(extension); |
| 286 | MenuItem::Id item1_id = item1->id(); |
| 287 | MenuItem::Id item2_id = item2->id(); |
| 288 | MenuItem::Id item3_id = item3->id(); |
| 289 | MenuItem::Id item4_id = item4->id(); |
| 290 | MenuItem::Id item5_id = item5->id(); |
| 291 | MenuItem::Id item6_id = item6->id(); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 292 | const MenuItem::ExtensionKey key(extension->id()); |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 293 | |
| 294 | // Add the items in the hierarchy |
| 295 | // item1 -> item2 -> item3 -> item4 -> item5 -> item6. |
| 296 | ASSERT_TRUE(manager_.AddContextItem(extension, item1)); |
| 297 | ASSERT_TRUE(manager_.AddChildItem(item1_id, item2)); |
| 298 | ASSERT_TRUE(manager_.AddChildItem(item2_id, item3)); |
| 299 | ASSERT_TRUE(manager_.AddChildItem(item3_id, item4)); |
| 300 | ASSERT_TRUE(manager_.AddChildItem(item4_id, item5)); |
| 301 | ASSERT_TRUE(manager_.AddChildItem(item5_id, item6)); |
| 302 | ASSERT_EQ(item1, manager_.GetItemById(item1_id)); |
| 303 | ASSERT_EQ(item2, manager_.GetItemById(item2_id)); |
| 304 | ASSERT_EQ(item3, manager_.GetItemById(item3_id)); |
| 305 | ASSERT_EQ(item4, manager_.GetItemById(item4_id)); |
| 306 | ASSERT_EQ(item5, manager_.GetItemById(item5_id)); |
| 307 | ASSERT_EQ(item6, manager_.GetItemById(item6_id)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 308 | ASSERT_EQ(1u, manager_.MenuItems(key)->size()); |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 309 | ASSERT_EQ(6u, manager_.items_by_id_.size()); |
| 310 | |
| 311 | // Remove item6 (a leaf node). |
| 312 | ASSERT_TRUE(manager_.RemoveContextMenuItem(item6_id)); |
| 313 | ASSERT_EQ(item1, manager_.GetItemById(item1_id)); |
| 314 | ASSERT_EQ(item2, manager_.GetItemById(item2_id)); |
| 315 | ASSERT_EQ(item3, manager_.GetItemById(item3_id)); |
| 316 | ASSERT_EQ(item4, manager_.GetItemById(item4_id)); |
| 317 | ASSERT_EQ(item5, manager_.GetItemById(item5_id)); |
| 318 | ASSERT_EQ(NULL, manager_.GetItemById(item6_id)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 319 | ASSERT_EQ(1u, manager_.MenuItems(key)->size()); |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 320 | ASSERT_EQ(5u, manager_.items_by_id_.size()); |
| 321 | |
| 322 | // Remove item4 and make sure item5 is gone as well. |
| 323 | ASSERT_TRUE(manager_.RemoveContextMenuItem(item4_id)); |
| 324 | ASSERT_EQ(item1, manager_.GetItemById(item1_id)); |
| 325 | ASSERT_EQ(item2, manager_.GetItemById(item2_id)); |
| 326 | ASSERT_EQ(item3, manager_.GetItemById(item3_id)); |
| 327 | ASSERT_EQ(NULL, manager_.GetItemById(item4_id)); |
| 328 | ASSERT_EQ(NULL, manager_.GetItemById(item5_id)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 329 | ASSERT_EQ(1u, manager_.MenuItems(key)->size()); |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 330 | ASSERT_EQ(3u, manager_.items_by_id_.size()); |
| 331 | |
| 332 | // Now remove item1 and make sure item2 and item3 are gone as well. |
| 333 | ASSERT_TRUE(manager_.RemoveContextMenuItem(item1_id)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 334 | ASSERT_EQ(NULL, manager_.MenuItems(key)); |
[email protected] | 2b07c93f | 2010-08-02 23:13:04 | [diff] [blame] | 335 | ASSERT_EQ(0u, manager_.items_by_id_.size()); |
| 336 | ASSERT_EQ(NULL, manager_.GetItemById(item1_id)); |
| 337 | ASSERT_EQ(NULL, manager_.GetItemById(item2_id)); |
| 338 | ASSERT_EQ(NULL, manager_.GetItemById(item3_id)); |
| 339 | } |
| 340 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 341 | // Tests changing parents. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 342 | TEST_F(MenuManagerTest, ChangeParent) { |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 343 | Extension* extension1 = AddExtension("1111"); |
[email protected] | 06a0c94e | 2010-06-24 23:20:15 | [diff] [blame] | 344 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 345 | // First create two items and add them both to the manager. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 346 | MenuItem* item1 = CreateTestItem(extension1); |
| 347 | MenuItem* item2 = CreateTestItem(extension1); |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 348 | |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 349 | ASSERT_TRUE(manager_.AddContextItem(extension1, item1)); |
| 350 | ASSERT_TRUE(manager_.AddContextItem(extension1, item2)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 351 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 352 | const MenuItem::List* items = manager_.MenuItems(item1->id().extension_key); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 353 | ASSERT_EQ(2u, items->size()); |
| 354 | ASSERT_EQ(item1, items->at(0)); |
| 355 | ASSERT_EQ(item2, items->at(1)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 356 | |
| 357 | // Now create a third item, initially add it as a child of item1, then move |
| 358 | // it to be a child of item2. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 359 | MenuItem* item3 = CreateTestItem(extension1); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 360 | |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 361 | ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 362 | ASSERT_EQ(1, item1->child_count()); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 363 | ASSERT_EQ(item3, item1->children()[0]); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 364 | |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 365 | ASSERT_TRUE(manager_.ChangeParent(item3->id(), &item2->id())); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 366 | ASSERT_EQ(0, item1->child_count()); |
| 367 | ASSERT_EQ(1, item2->child_count()); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 368 | ASSERT_EQ(item3, item2->children()[0]); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 369 | |
| 370 | // Move item2 to be a child of item1. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 371 | ASSERT_TRUE(manager_.ChangeParent(item2->id(), &item1->id())); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 372 | ASSERT_EQ(1, item1->child_count()); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 373 | ASSERT_EQ(item2, item1->children()[0]); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 374 | ASSERT_EQ(1, item2->child_count()); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 375 | ASSERT_EQ(item3, item2->children()[0]); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 376 | |
| 377 | // Since item2 was a top-level item but is no longer, we should only have 1 |
| 378 | // top-level item. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 379 | items = manager_.MenuItems(item1->id().extension_key); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 380 | ASSERT_EQ(1u, items->size()); |
| 381 | ASSERT_EQ(item1, items->at(0)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 382 | |
| 383 | // Move item3 back to being a child of item1, so it's now a sibling of item2. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 384 | ASSERT_TRUE(manager_.ChangeParent(item3->id(), &item1->id())); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 385 | ASSERT_EQ(2, item1->child_count()); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 386 | ASSERT_EQ(item2, item1->children()[0]); |
| 387 | ASSERT_EQ(item3, item1->children()[1]); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 388 | |
| 389 | // Try switching item3 to be the parent of item1 - this should fail. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 390 | ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item3->id())); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 391 | ASSERT_EQ(0, item3->child_count()); |
| 392 | ASSERT_EQ(2, item1->child_count()); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 393 | ASSERT_EQ(item2, item1->children()[0]); |
| 394 | ASSERT_EQ(item3, item1->children()[1]); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 395 | items = manager_.MenuItems(item1->id().extension_key); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 396 | ASSERT_EQ(1u, items->size()); |
| 397 | ASSERT_EQ(item1, items->at(0)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 398 | |
| 399 | // Move item2 to be a top-level item. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 400 | ASSERT_TRUE(manager_.ChangeParent(item2->id(), NULL)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 401 | items = manager_.MenuItems(item1->id().extension_key); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 402 | ASSERT_EQ(2u, items->size()); |
| 403 | ASSERT_EQ(item1, items->at(0)); |
| 404 | ASSERT_EQ(item2, items->at(1)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 405 | ASSERT_EQ(1, item1->child_count()); |
[email protected] | 63a414b5 | 2010-06-03 23:20:49 | [diff] [blame] | 406 | ASSERT_EQ(item3, item1->children()[0]); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 407 | |
| 408 | // Make sure you can't move a node to be a child of another extension's item. |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 409 | Extension* extension2 = AddExtension("2222"); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 410 | MenuItem* item4 = CreateTestItem(extension2); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 411 | ASSERT_TRUE(manager_.AddContextItem(extension2, item4)); |
| 412 | ASSERT_FALSE(manager_.ChangeParent(item4->id(), &item1->id())); |
| 413 | ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item4->id())); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 414 | |
| 415 | // Make sure you can't make an item be it's own parent. |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 416 | ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item1->id())); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 417 | } |
| 418 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 419 | // Tests that we properly remove an extension's menu item when that extension is |
| 420 | // unloaded. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 421 | TEST_F(MenuManagerTest, ExtensionUnloadRemovesMenuItems) { |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 422 | content::NotificationService* notifier = |
| 423 | content::NotificationService::current(); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 424 | ASSERT_TRUE(notifier != NULL); |
| 425 | |
| 426 | // Create a test extension. |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 427 | Extension* extension1 = AddExtension("1111"); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 428 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 429 | // Create an MenuItem and put it into the manager. |
| 430 | MenuItem* item1 = CreateTestItem(extension1); |
| 431 | MenuItem::Id id1 = item1->id(); |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 432 | ASSERT_EQ(extension1->id(), item1->extension_id()); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 433 | ASSERT_TRUE(manager_.AddContextItem(extension1, item1)); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 434 | ASSERT_EQ( |
| 435 | 1u, manager_.MenuItems(MenuItem::ExtensionKey(extension1->id()))->size()); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 436 | |
| 437 | // Create a menu item with a different extension id and add it to the manager. |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 438 | Extension* extension2 = AddExtension("2222"); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 439 | MenuItem* item2 = CreateTestItem(extension2); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 440 | ASSERT_NE(item1->extension_id(), item2->extension_id()); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 441 | ASSERT_TRUE(manager_.AddContextItem(extension2, item2)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 442 | |
| 443 | // Notify that the extension was unloaded, and make sure the right item is |
| 444 | // gone. |
[email protected] | 00ee2f5 | 2014-04-25 09:25:52 | [diff] [blame] | 445 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get()); |
| 446 | registry->TriggerOnUnloaded(extension1, |
| 447 | UnloadedExtensionInfo::REASON_DISABLE); |
| 448 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 449 | ASSERT_EQ(NULL, manager_.MenuItems(MenuItem::ExtensionKey(extension1->id()))); |
| 450 | ASSERT_EQ( |
| 451 | 1u, manager_.MenuItems(MenuItem::ExtensionKey(extension2->id()))->size()); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 452 | ASSERT_TRUE(manager_.GetItemById(id1) == NULL); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 453 | ASSERT_TRUE(manager_.GetItemById(item2->id()) != NULL); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 454 | } |
| 455 | |
[email protected] | 4db043b | 2014-08-13 09:46:18 | [diff] [blame] | 456 | namespace { |
| 457 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 458 | // A mock message service for tests of MenuManager::ExecuteCommand. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 459 | class MockEventRouter : public EventRouter { |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 460 | public: |
[email protected] | 79cb81bb | 2012-09-20 02:23:31 | [diff] [blame] | 461 | explicit MockEventRouter(Profile* profile) : EventRouter(profile, NULL) {} |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 462 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 463 | MOCK_METHOD6(DispatchEventToExtensionMock, |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 464 | void(const std::string& extension_id, |
| 465 | const std::string& event_name, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 466 | base::ListValue* event_args, |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 467 | content::BrowserContext* source_context, |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 468 | const GURL& event_url, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 469 | EventRouter::UserGestureState state)); |
[email protected] | f72d0c6 | 2011-08-31 16:27:44 | [diff] [blame] | 470 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 471 | virtual void DispatchEventToExtension(const std::string& extension_id, |
[email protected] | 01f7a804 | 2012-12-07 07:48:02 | [diff] [blame] | 472 | scoped_ptr<Event> event) { |
| 473 | DispatchEventToExtensionMock(extension_id, |
| 474 | event->event_name, |
| 475 | event->event_args.release(), |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 476 | event->restrict_to_browser_context, |
[email protected] | 01f7a804 | 2012-12-07 07:48:02 | [diff] [blame] | 477 | event->event_url, |
| 478 | event->user_gesture); |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 479 | } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 480 | |
| 481 | private: |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 482 | DISALLOW_COPY_AND_ASSIGN(MockEventRouter); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 483 | }; |
| 484 | |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 485 | // A mock ExtensionSystem to serve our MockEventRouter. |
| 486 | class MockExtensionSystem : public TestExtensionSystem { |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 487 | public: |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 488 | explicit MockExtensionSystem(Profile* profile) |
| 489 | : TestExtensionSystem(profile) {} |
| 490 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 491 | EventRouter* event_router() override { |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 492 | if (!mock_event_router_) |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 493 | mock_event_router_.reset(new MockEventRouter(profile_)); |
| 494 | return mock_event_router_.get(); |
| 495 | } |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 496 | |
| 497 | private: |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 498 | scoped_ptr<MockEventRouter> mock_event_router_; |
| 499 | |
| 500 | DISALLOW_COPY_AND_ASSIGN(MockExtensionSystem); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 501 | }; |
| 502 | |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 503 | KeyedService* BuildMockExtensionSystem(content::BrowserContext* profile) { |
[email protected] | c7fa436 | 2013-04-26 18:09:02 | [diff] [blame] | 504 | return new MockExtensionSystem(static_cast<Profile*>(profile)); |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 505 | } |
| 506 | |
[email protected] | 4db043b | 2014-08-13 09:46:18 | [diff] [blame] | 507 | } // namespace |
| 508 | |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 509 | // Tests the RemoveAll functionality. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 510 | TEST_F(MenuManagerTest, RemoveAll) { |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 511 | // Try removing all items for an extension id that doesn't have any items. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 512 | manager_.RemoveAllContextItems(MenuItem::ExtensionKey("CCCC")); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 513 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 514 | // Add 2 top-level and one child item for extension 1. |
| 515 | Extension* extension1 = AddExtension("1111"); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 516 | MenuItem* item1 = CreateTestItem(extension1); |
| 517 | MenuItem* item2 = CreateTestItem(extension1); |
| 518 | MenuItem* item3 = CreateTestItem(extension1); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 519 | ASSERT_TRUE(manager_.AddContextItem(extension1, item1)); |
| 520 | ASSERT_TRUE(manager_.AddContextItem(extension1, item2)); |
| 521 | ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 522 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 523 | // Add one top-level item for extension 2. |
| 524 | Extension* extension2 = AddExtension("2222"); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 525 | MenuItem* item4 = CreateTestItem(extension2); |
[email protected] | f4f0459 | 2010-07-14 20:40:13 | [diff] [blame] | 526 | ASSERT_TRUE(manager_.AddContextItem(extension2, item4)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 527 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 528 | const MenuItem::ExtensionKey key1(extension1->id()); |
| 529 | const MenuItem::ExtensionKey key2(extension2->id()); |
| 530 | EXPECT_EQ(2u, manager_.MenuItems(key1)->size()); |
| 531 | EXPECT_EQ(1u, manager_.MenuItems(key2)->size()); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 532 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 533 | // Remove extension2's item. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 534 | manager_.RemoveAllContextItems(key2); |
| 535 | EXPECT_EQ(2u, manager_.MenuItems(key1)->size()); |
| 536 | EXPECT_EQ(NULL, manager_.MenuItems(key2)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 537 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 538 | // Remove extension1's items. |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 539 | manager_.RemoveAllContextItems(key1); |
| 540 | EXPECT_EQ(NULL, manager_.MenuItems(key1)); |
[email protected] | 66dbfb2c | 2010-05-12 20:20:15 | [diff] [blame] | 541 | } |
| 542 | |
[email protected] | f50da859 | 2010-10-28 23:39:32 | [diff] [blame] | 543 | // Tests that removing all items one-by-one doesn't leave an entry around. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 544 | TEST_F(MenuManagerTest, RemoveOneByOne) { |
[email protected] | f50da859 | 2010-10-28 23:39:32 | [diff] [blame] | 545 | // Add 2 test items. |
| 546 | Extension* extension1 = AddExtension("1111"); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 547 | MenuItem* item1 = CreateTestItem(extension1); |
| 548 | MenuItem* item2 = CreateTestItem(extension1); |
| 549 | MenuItem* item3 = CreateTestItemWithID(extension1, "id3"); |
[email protected] | f50da859 | 2010-10-28 23:39:32 | [diff] [blame] | 550 | ASSERT_TRUE(manager_.AddContextItem(extension1, item1)); |
| 551 | ASSERT_TRUE(manager_.AddContextItem(extension1, item2)); |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 552 | ASSERT_TRUE(manager_.AddContextItem(extension1, item3)); |
[email protected] | f50da859 | 2010-10-28 23:39:32 | [diff] [blame] | 553 | |
| 554 | ASSERT_FALSE(manager_.context_items_.empty()); |
| 555 | |
[email protected] | 619c5dba | 2012-05-16 00:44:48 | [diff] [blame] | 556 | manager_.RemoveContextMenuItem(item3->id()); |
[email protected] | f50da859 | 2010-10-28 23:39:32 | [diff] [blame] | 557 | manager_.RemoveContextMenuItem(item1->id()); |
| 558 | manager_.RemoveContextMenuItem(item2->id()); |
| 559 | |
| 560 | ASSERT_TRUE(manager_.context_items_.empty()); |
| 561 | } |
| 562 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 563 | TEST_F(MenuManagerTest, ExecuteCommand) { |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 564 | TestingProfile profile; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 565 | |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 566 | MockExtensionSystem* mock_extension_system = |
| 567 | static_cast<MockExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> |
| 568 | SetTestingFactoryAndUse(&profile, &BuildMockExtensionSystem)); |
| 569 | MockEventRouter* mock_event_router = |
| 570 | static_cast<MockEventRouter*>(mock_extension_system->event_router()); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 571 | |
[email protected] | 35be7ec | 2012-02-12 20:42:51 | [diff] [blame] | 572 | content::ContextMenuParams params; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 573 | params.media_type = blink::WebContextMenuData::MediaTypeImage; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 574 | params.src_url = GURL("http://foo.bar/image.png"); |
| 575 | params.page_url = GURL("http://foo.bar"); |
[email protected] | 0433872 | 2013-12-24 23:18:05 | [diff] [blame] | 576 | params.selection_text = base::ASCIIToUTF16("Hello World"); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 577 | params.is_editable = false; |
| 578 | |
[email protected] | 052c9270 | 2010-06-25 07:25:52 | [diff] [blame] | 579 | Extension* extension = AddExtension("test"); |
[email protected] | 504bf9c | 2012-12-13 01:51:20 | [diff] [blame] | 580 | MenuItem* parent = CreateTestItem(extension); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 581 | MenuItem* item = CreateTestItem(extension); |
[email protected] | 504bf9c | 2012-12-13 01:51:20 | [diff] [blame] | 582 | MenuItem::Id parent_id = parent->id(); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 583 | MenuItem::Id id = item->id(); |
[email protected] | 504bf9c | 2012-12-13 01:51:20 | [diff] [blame] | 584 | ASSERT_TRUE(manager_.AddContextItem(extension, parent)); |
| 585 | ASSERT_TRUE(manager_.AddChildItem(parent->id(), item)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 586 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 587 | // Use the magic of googlemock to save a parameter to our mock's |
[email protected] | 0a184b5 | 2011-06-23 00:41:13 | [diff] [blame] | 588 | // DispatchEventToExtension method into event_args. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 589 | base::ListValue* list = NULL; |
[email protected] | bc4ae15 | 2012-06-01 22:23:34 | [diff] [blame] | 590 | { |
| 591 | InSequence s; |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 592 | EXPECT_CALL(*mock_event_router, |
[email protected] | db6bf7f | 2014-03-27 15:49:26 | [diff] [blame] | 593 | DispatchEventToExtensionMock(item->extension_id(), |
| 594 | MenuManager::kOnContextMenus, |
| 595 | _, |
| 596 | &profile, |
| 597 | GURL(), |
| 598 | EventRouter::USER_GESTURE_ENABLED)) |
| 599 | .Times(1) |
| 600 | .WillOnce(SaveArg<2>(&list)); |
[email protected] | 9e5be1f9 | 2012-10-29 19:01:45 | [diff] [blame] | 601 | EXPECT_CALL(*mock_event_router, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 602 | DispatchEventToExtensionMock( |
[email protected] | bc4ae15 | 2012-06-01 22:23:34 | [diff] [blame] | 603 | item->extension_id(), |
[email protected] | e054ea1 | 2013-08-20 00:41:57 | [diff] [blame] | 604 | context_menus::OnClicked::kEventName, |
[email protected] | bc4ae15 | 2012-06-01 22:23:34 | [diff] [blame] | 605 | _, |
| 606 | &profile, |
| 607 | GURL(), |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 608 | EventRouter::USER_GESTURE_ENABLED)) |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 609 | .Times(1) |
| 610 | .WillOnce(DeleteArg<2>()); |
[email protected] | bc4ae15 | 2012-06-01 22:23:34 | [diff] [blame] | 611 | } |
[email protected] | 9531f74 | 2012-12-11 22:02:40 | [diff] [blame] | 612 | manager_.ExecuteCommand(&profile, NULL /* web_contents */, params, id); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 613 | |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 614 | ASSERT_EQ(2u, list->GetSize()); |
| 615 | |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 616 | base::DictionaryValue* info; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 617 | ASSERT_TRUE(list->GetDictionary(0, &info)); |
| 618 | |
| 619 | int tmp_id = 0; |
[email protected] | 88dc069 | 2010-08-14 16:29:42 | [diff] [blame] | 620 | ASSERT_TRUE(info->GetInteger("menuItemId", &tmp_id)); |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 621 | ASSERT_EQ(id.uid, tmp_id); |
[email protected] | 504bf9c | 2012-12-13 01:51:20 | [diff] [blame] | 622 | ASSERT_TRUE(info->GetInteger("parentMenuItemId", &tmp_id)); |
| 623 | ASSERT_EQ(parent_id.uid, tmp_id); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 624 | |
| 625 | std::string tmp; |
[email protected] | 88dc069 | 2010-08-14 16:29:42 | [diff] [blame] | 626 | ASSERT_TRUE(info->GetString("mediaType", &tmp)); |
[email protected] | 1741b97 | 2010-08-04 23:47:41 | [diff] [blame] | 627 | ASSERT_EQ("image", tmp); |
[email protected] | 88dc069 | 2010-08-14 16:29:42 | [diff] [blame] | 628 | ASSERT_TRUE(info->GetString("srcUrl", &tmp)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 629 | ASSERT_EQ(params.src_url.spec(), tmp); |
[email protected] | 88dc069 | 2010-08-14 16:29:42 | [diff] [blame] | 630 | ASSERT_TRUE(info->GetString("pageUrl", &tmp)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 631 | ASSERT_EQ(params.page_url.spec(), tmp); |
| 632 | |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 633 | base::string16 tmp16; |
[email protected] | 944b421 | 2010-12-14 00:00:33 | [diff] [blame] | 634 | ASSERT_TRUE(info->GetString("selectionText", &tmp16)); |
| 635 | ASSERT_EQ(params.selection_text, tmp16); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 636 | |
| 637 | bool bool_tmp = true; |
[email protected] | 88dc069 | 2010-08-14 16:29:42 | [diff] [blame] | 638 | ASSERT_TRUE(info->GetBoolean("editable", &bool_tmp)); |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 639 | ASSERT_EQ(params.is_editable, bool_tmp); |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 640 | |
| 641 | delete list; |
[email protected] | 2e3b520 | 2010-03-23 06:52:41 | [diff] [blame] | 642 | } |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 643 | |
| 644 | // Test that there is always only one radio item selected. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 645 | TEST_F(MenuManagerTest, SanitizeRadioButtons) { |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 646 | Extension* extension = AddExtension("test"); |
| 647 | |
| 648 | // A single unchecked item should get checked |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 649 | MenuItem* item1 = CreateTestItem(extension); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 650 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 651 | item1->set_type(MenuItem::RADIO); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 652 | item1->SetChecked(false); |
| 653 | ASSERT_FALSE(item1->checked()); |
| 654 | manager_.AddContextItem(extension, item1); |
| 655 | ASSERT_TRUE(item1->checked()); |
| 656 | |
| 657 | // In a run of two unchecked items, the first should get selected. |
| 658 | item1->SetChecked(false); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 659 | MenuItem* item2 = CreateTestItem(extension); |
| 660 | item2->set_type(MenuItem::RADIO); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 661 | item2->SetChecked(false); |
| 662 | ASSERT_FALSE(item1->checked()); |
| 663 | ASSERT_FALSE(item2->checked()); |
| 664 | manager_.AddContextItem(extension, item2); |
| 665 | ASSERT_TRUE(item1->checked()); |
| 666 | ASSERT_FALSE(item2->checked()); |
| 667 | |
| 668 | // If multiple items are checked, only the last item should get checked. |
| 669 | item1->SetChecked(true); |
| 670 | item2->SetChecked(true); |
| 671 | ASSERT_TRUE(item1->checked()); |
| 672 | ASSERT_TRUE(item2->checked()); |
| 673 | manager_.ItemUpdated(item1->id()); |
| 674 | ASSERT_FALSE(item1->checked()); |
| 675 | ASSERT_TRUE(item2->checked()); |
| 676 | |
| 677 | // If the checked item is removed, the new first item should get checked. |
| 678 | item1->SetChecked(false); |
| 679 | item2->SetChecked(true); |
| 680 | ASSERT_FALSE(item1->checked()); |
| 681 | ASSERT_TRUE(item2->checked()); |
| 682 | manager_.RemoveContextMenuItem(item2->id()); |
| 683 | item2 = NULL; |
| 684 | ASSERT_TRUE(item1->checked()); |
| 685 | |
| 686 | // If a checked item is added to a run that already has a checked item, |
| 687 | // then the new item should get checked. |
| 688 | item1->SetChecked(true); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 689 | MenuItem* new_item = CreateTestItem(extension); |
| 690 | new_item->set_type(MenuItem::RADIO); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 691 | new_item->SetChecked(true); |
| 692 | ASSERT_TRUE(item1->checked()); |
| 693 | ASSERT_TRUE(new_item->checked()); |
| 694 | manager_.AddContextItem(extension, new_item); |
| 695 | ASSERT_FALSE(item1->checked()); |
| 696 | ASSERT_TRUE(new_item->checked()); |
| 697 | // Make sure that children are checked as well. |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 698 | MenuItem* parent = CreateTestItem(extension); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 699 | manager_.AddContextItem(extension, parent); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 700 | MenuItem* child1 = CreateTestItem(extension); |
| 701 | child1->set_type(MenuItem::RADIO); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 702 | child1->SetChecked(false); |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 703 | MenuItem* child2 = CreateTestItem(extension); |
| 704 | child2->set_type(MenuItem::RADIO); |
[email protected] | 7dddebc3 | 2012-01-11 22:01:03 | [diff] [blame] | 705 | child2->SetChecked(true); |
| 706 | ASSERT_FALSE(child1->checked()); |
| 707 | ASSERT_TRUE(child2->checked()); |
| 708 | |
| 709 | manager_.AddChildItem(parent->id(), child1); |
| 710 | ASSERT_TRUE(child1->checked()); |
| 711 | |
| 712 | manager_.AddChildItem(parent->id(), child2); |
| 713 | ASSERT_FALSE(child1->checked()); |
| 714 | ASSERT_TRUE(child2->checked()); |
| 715 | |
| 716 | // Removing the checked item from the children should cause the |
| 717 | // remaining child to be checked. |
| 718 | manager_.RemoveContextMenuItem(child2->id()); |
| 719 | child2 = NULL; |
| 720 | ASSERT_TRUE(child1->checked()); |
| 721 | |
| 722 | // This should NOT cause |new_item| to be deseleted because |
| 723 | // |parent| will be seperating the two runs of radio items. |
| 724 | manager_.ChangeParent(child1->id(), NULL); |
| 725 | ASSERT_TRUE(new_item->checked()); |
| 726 | ASSERT_TRUE(child1->checked()); |
| 727 | |
| 728 | // Removing |parent| should cause only |child1| to be selected. |
| 729 | manager_.RemoveContextMenuItem(parent->id()); |
| 730 | parent = NULL; |
| 731 | ASSERT_FALSE(new_item->checked()); |
| 732 | ASSERT_TRUE(child1->checked()); |
| 733 | } |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 734 | |
[email protected] | 6350346 | 2012-10-30 22:14:31 | [diff] [blame] | 735 | // Tests the RemoveAllIncognitoContextItems functionality. |
| 736 | TEST_F(MenuManagerTest, RemoveAllIncognito) { |
| 737 | Extension* extension1 = AddExtension("1111"); |
| 738 | // Add 2 top-level and one child item for extension 1 |
| 739 | // with incognito 'true'. |
| 740 | MenuItem* item1 = CreateTestItem(extension1, true); |
| 741 | MenuItem* item2 = CreateTestItem(extension1, true); |
| 742 | MenuItem* item3 = CreateTestItem(extension1, true); |
| 743 | ASSERT_TRUE(manager_.AddContextItem(extension1, item1)); |
| 744 | ASSERT_TRUE(manager_.AddContextItem(extension1, item2)); |
| 745 | ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3)); |
| 746 | |
| 747 | // Add 2 top-level and one child item for extension 1 |
| 748 | // with incognito 'false'. |
| 749 | MenuItem* item4 = CreateTestItem(extension1); |
| 750 | MenuItem* item5 = CreateTestItem(extension1); |
| 751 | MenuItem* item6 = CreateTestItem(extension1); |
| 752 | ASSERT_TRUE(manager_.AddContextItem(extension1, item4)); |
| 753 | ASSERT_TRUE(manager_.AddContextItem(extension1, item5)); |
| 754 | ASSERT_TRUE(manager_.AddChildItem(item4->id(), item6)); |
| 755 | |
| 756 | // Add one top-level item for extension 2. |
| 757 | Extension* extension2 = AddExtension("2222"); |
| 758 | MenuItem* item7 = CreateTestItem(extension2); |
| 759 | ASSERT_TRUE(manager_.AddContextItem(extension2, item7)); |
| 760 | |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 761 | const MenuItem::ExtensionKey key1(extension1->id()); |
| 762 | const MenuItem::ExtensionKey key2(extension2->id()); |
| 763 | EXPECT_EQ(4u, manager_.MenuItems(key1)->size()); |
| 764 | EXPECT_EQ(1u, manager_.MenuItems(key2)->size()); |
[email protected] | 6350346 | 2012-10-30 22:14:31 | [diff] [blame] | 765 | |
| 766 | // Remove all context menu items with incognito true. |
| 767 | manager_.RemoveAllIncognitoContextItems(); |
[email protected] | 6f9d2c6 | 2014-03-10 12:12:05 | [diff] [blame] | 768 | EXPECT_EQ(2u, manager_.MenuItems(key1)->size()); |
| 769 | EXPECT_EQ(1u, manager_.MenuItems(key2)->size()); |
[email protected] | 6350346 | 2012-10-30 22:14:31 | [diff] [blame] | 770 | } |
| 771 | |
[email protected] | 5aeeae1 | 2012-07-05 19:13:11 | [diff] [blame] | 772 | } // namespace extensions |