1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/json/json_reader.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/extension_system_factory.h"
17 #include "chrome/browser/extensions/menu_manager.h"
18 #include "chrome/browser/extensions/test_extension_prefs.h"
19 #include "chrome/browser/extensions/test_extension_system.h"
20 #include "chrome/browser/prefs/pref_service_syncable.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/extensions/api/context_menus.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/common/context_menu_params.h"
26 #include "content/public/test/test_browser_thread.h"
27 #include "extensions/browser/event_router.h"
28 #include "extensions/browser/extension_registry.h"
29 #include "extensions/browser/extension_system.h"
30 #include "extensions/common/extension.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 using content::BrowserThread
;
36 using testing::AtLeast
;
37 using testing::DeleteArg
;
38 using testing::InSequence
;
39 using testing::Return
;
40 using testing::SaveArg
;
42 namespace extensions
{
44 namespace context_menus
= api::context_menus
;
46 // Base class for tests.
47 class MenuManagerTest
: public testing::Test
{
50 : ui_thread_(BrowserThread::UI
, &message_loop_
),
51 file_thread_(BrowserThread::FILE, &message_loop_
),
52 profile_(new TestingProfile()),
53 manager_(profile_
.get(),
54 ExtensionSystem::Get(profile_
.get())->state_store()),
55 prefs_(message_loop_
.message_loop_proxy().get()),
58 virtual void TearDown() override
{
59 prefs_
.pref_service()->CommitPendingWrite();
60 message_loop_
.RunUntilIdle();
63 // Returns a test item.
64 MenuItem
* CreateTestItem(Extension
* extension
, bool incognito
= false) {
65 MenuItem::Type type
= MenuItem::NORMAL
;
66 MenuItem::ContextList
contexts(MenuItem::ALL
);
67 const MenuItem::ExtensionKey
key(extension
->id());
68 MenuItem::Id
id(incognito
, key
);
70 return new MenuItem(id
, "test", false, true, type
, contexts
);
73 // Returns a test item with the given string ID.
74 MenuItem
* CreateTestItemWithID(Extension
* extension
,
75 const std::string
& string_id
) {
76 MenuItem::Type type
= MenuItem::NORMAL
;
77 MenuItem::ContextList
contexts(MenuItem::ALL
);
78 const MenuItem::ExtensionKey
key(extension
->id());
79 MenuItem::Id
id(false, key
);
80 id
.string_uid
= string_id
;
81 return new MenuItem(id
, "test", false, true, type
, contexts
);
84 // Creates and returns a test Extension. The caller does *not* own the return
86 Extension
* AddExtension(const std::string
& name
) {
87 scoped_refptr
<Extension
> extension
= prefs_
.AddExtension(name
);
88 extensions_
.push_back(extension
);
89 return extension
.get();
93 base::MessageLoopForUI message_loop_
;
94 content::TestBrowserThread ui_thread_
;
95 content::TestBrowserThread file_thread_
;
96 scoped_ptr
<TestingProfile
> profile_
;
99 ExtensionList extensions_
;
100 TestExtensionPrefs prefs_
;
104 DISALLOW_COPY_AND_ASSIGN(MenuManagerTest
);
107 // Tests adding, getting, and removing items.
108 TEST_F(MenuManagerTest
, AddGetRemoveItems
) {
109 Extension
* extension
= AddExtension("test");
111 // Add a new item, make sure you can get it back.
112 MenuItem
* item1
= CreateTestItem(extension
);
113 ASSERT_TRUE(item1
!= NULL
);
114 ASSERT_TRUE(manager_
.AddContextItem(extension
, item1
));
115 ASSERT_EQ(item1
, manager_
.GetItemById(item1
->id()));
116 const MenuItem::List
* items
= manager_
.MenuItems(item1
->id().extension_key
);
117 ASSERT_EQ(1u, items
->size());
118 ASSERT_EQ(item1
, items
->at(0));
120 // Add a second item, make sure it comes back too.
121 MenuItem
* item2
= CreateTestItemWithID(extension
, "id2");
122 ASSERT_TRUE(manager_
.AddContextItem(extension
, item2
));
123 ASSERT_EQ(item2
, manager_
.GetItemById(item2
->id()));
124 items
= manager_
.MenuItems(item2
->id().extension_key
);
125 ASSERT_EQ(2u, items
->size());
126 ASSERT_EQ(item1
, items
->at(0));
127 ASSERT_EQ(item2
, items
->at(1));
129 // Try adding item 3, then removing it.
130 MenuItem
* item3
= CreateTestItem(extension
);
131 MenuItem::Id id3
= item3
->id();
132 const MenuItem::ExtensionKey
extension_key3(item3
->id().extension_key
);
133 ASSERT_TRUE(manager_
.AddContextItem(extension
, item3
));
134 ASSERT_EQ(item3
, manager_
.GetItemById(id3
));
135 ASSERT_EQ(3u, manager_
.MenuItems(extension_key3
)->size());
136 ASSERT_TRUE(manager_
.RemoveContextMenuItem(id3
));
137 ASSERT_EQ(NULL
, manager_
.GetItemById(id3
));
138 ASSERT_EQ(2u, manager_
.MenuItems(extension_key3
)->size());
140 // Make sure removing a non-existent item returns false.
141 const MenuItem::ExtensionKey
key(extension
->id());
142 MenuItem::Id
id(false, key
);
143 id
.uid
= id3
.uid
+ 50;
144 ASSERT_FALSE(manager_
.RemoveContextMenuItem(id
));
146 // Make sure adding an item with the same string ID returns false.
147 scoped_ptr
<MenuItem
> item2too(CreateTestItemWithID(extension
, "id2"));
148 ASSERT_FALSE(manager_
.AddContextItem(extension
, item2too
.get()));
150 // But the same string ID should not collide with another extension.
151 Extension
* extension2
= AddExtension("test2");
152 MenuItem
* item2other
= CreateTestItemWithID(extension2
, "id2");
153 ASSERT_TRUE(manager_
.AddContextItem(extension2
, item2other
));
156 // Test adding/removing child items.
157 TEST_F(MenuManagerTest
, ChildFunctions
) {
158 Extension
* extension1
= AddExtension("1111");
159 Extension
* extension2
= AddExtension("2222");
160 Extension
* extension3
= AddExtension("3333");
162 MenuItem
* item1
= CreateTestItem(extension1
);
163 MenuItem
* item2
= CreateTestItem(extension2
);
164 MenuItem
* item2_child
= CreateTestItemWithID(extension2
, "2child");
165 MenuItem
* item2_grandchild
= CreateTestItem(extension2
);
167 // This third item we expect to fail inserting, so we use a scoped_ptr to make
168 // sure it gets deleted.
169 scoped_ptr
<MenuItem
> item3(CreateTestItem(extension3
));
171 // Add in the first two items.
172 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item1
));
173 ASSERT_TRUE(manager_
.AddContextItem(extension2
, item2
));
175 MenuItem::Id id1
= item1
->id();
176 MenuItem::Id id2
= item2
->id();
178 // Try adding item3 as a child of item2 - this should fail because item3 has
179 // a different extension id.
180 ASSERT_FALSE(manager_
.AddChildItem(id2
, item3
.get()));
182 // Add item2_child as a child of item2.
183 MenuItem::Id id2_child
= item2_child
->id();
184 ASSERT_TRUE(manager_
.AddChildItem(id2
, item2_child
));
185 ASSERT_EQ(1, item2
->child_count());
186 ASSERT_EQ(0, item1
->child_count());
187 ASSERT_EQ(item2_child
, manager_
.GetItemById(id2_child
));
189 ASSERT_EQ(1u, manager_
.MenuItems(item1
->id().extension_key
)->size());
190 ASSERT_EQ(item1
, manager_
.MenuItems(item1
->id().extension_key
)->at(0));
192 // Add item2_grandchild as a child of item2_child, then remove it.
193 MenuItem::Id id2_grandchild
= item2_grandchild
->id();
194 ASSERT_TRUE(manager_
.AddChildItem(id2_child
, item2_grandchild
));
195 ASSERT_EQ(1, item2
->child_count());
196 ASSERT_EQ(1, item2_child
->child_count());
197 ASSERT_TRUE(manager_
.RemoveContextMenuItem(id2_grandchild
));
199 // We should only get 1 thing back when asking for item2's extension id, since
200 // it has a child item.
201 ASSERT_EQ(1u, manager_
.MenuItems(item2
->id().extension_key
)->size());
202 ASSERT_EQ(item2
, manager_
.MenuItems(item2
->id().extension_key
)->at(0));
204 // Remove child2_item.
205 ASSERT_TRUE(manager_
.RemoveContextMenuItem(id2_child
));
206 ASSERT_EQ(1u, manager_
.MenuItems(item2
->id().extension_key
)->size());
207 ASSERT_EQ(item2
, manager_
.MenuItems(item2
->id().extension_key
)->at(0));
208 ASSERT_EQ(0, item2
->child_count());
211 TEST_F(MenuManagerTest
, PopulateFromValue
) {
212 Extension
* extension
= AddExtension("test");
214 bool incognito
= true;
215 int type
= MenuItem::CHECKBOX
;
216 std::string
title("TITLE");
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
));
225 base::ListValue
* document_url_patterns(new base::ListValue());
226 document_url_patterns
->Append(
227 new base::StringValue("http://www.google.com/*"));
228 document_url_patterns
->Append(
229 new base::StringValue("http://www.reddit.com/*"));
231 base::ListValue
* target_url_patterns(new base::ListValue());
232 target_url_patterns
->Append(
233 new base::StringValue("http://www.yahoo.com/*"));
234 target_url_patterns
->Append(
235 new base::StringValue("http://www.facebook.com/*"));
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
);
249 scoped_ptr
<MenuItem
> item(MenuItem::Populate(extension
->id(), value
, &error
));
250 ASSERT_TRUE(item
.get());
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());
260 URLPatternSet document_url_pattern_set
;
261 document_url_pattern_set
.Populate(*document_url_patterns
,
262 URLPattern::SCHEME_ALL
,
265 EXPECT_EQ(document_url_pattern_set
, item
->document_url_patterns());
267 URLPatternSet target_url_pattern_set
;
268 target_url_pattern_set
.Populate(*target_url_patterns
,
269 URLPattern::SCHEME_ALL
,
272 EXPECT_EQ(target_url_pattern_set
, item
->target_url_patterns());
275 // Tests that deleting a parent properly removes descendants.
276 TEST_F(MenuManagerTest
, DeleteParent
) {
277 Extension
* extension
= AddExtension("1111");
279 // Set up 5 items to add.
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();
292 const MenuItem::ExtensionKey
key(extension
->id());
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
));
308 ASSERT_EQ(1u, manager_
.MenuItems(key
)->size());
309 ASSERT_EQ(6u, manager_
.items_by_id_
.size());
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
));
319 ASSERT_EQ(1u, manager_
.MenuItems(key
)->size());
320 ASSERT_EQ(5u, manager_
.items_by_id_
.size());
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
));
329 ASSERT_EQ(1u, manager_
.MenuItems(key
)->size());
330 ASSERT_EQ(3u, manager_
.items_by_id_
.size());
332 // Now remove item1 and make sure item2 and item3 are gone as well.
333 ASSERT_TRUE(manager_
.RemoveContextMenuItem(item1_id
));
334 ASSERT_EQ(NULL
, manager_
.MenuItems(key
));
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
));
341 // Tests changing parents.
342 TEST_F(MenuManagerTest
, ChangeParent
) {
343 Extension
* extension1
= AddExtension("1111");
345 // First create two items and add them both to the manager.
346 MenuItem
* item1
= CreateTestItem(extension1
);
347 MenuItem
* item2
= CreateTestItem(extension1
);
349 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item1
));
350 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item2
));
352 const MenuItem::List
* items
= manager_
.MenuItems(item1
->id().extension_key
);
353 ASSERT_EQ(2u, items
->size());
354 ASSERT_EQ(item1
, items
->at(0));
355 ASSERT_EQ(item2
, items
->at(1));
357 // Now create a third item, initially add it as a child of item1, then move
358 // it to be a child of item2.
359 MenuItem
* item3
= CreateTestItem(extension1
);
361 ASSERT_TRUE(manager_
.AddChildItem(item1
->id(), item3
));
362 ASSERT_EQ(1, item1
->child_count());
363 ASSERT_EQ(item3
, item1
->children()[0]);
365 ASSERT_TRUE(manager_
.ChangeParent(item3
->id(), &item2
->id()));
366 ASSERT_EQ(0, item1
->child_count());
367 ASSERT_EQ(1, item2
->child_count());
368 ASSERT_EQ(item3
, item2
->children()[0]);
370 // Move item2 to be a child of item1.
371 ASSERT_TRUE(manager_
.ChangeParent(item2
->id(), &item1
->id()));
372 ASSERT_EQ(1, item1
->child_count());
373 ASSERT_EQ(item2
, item1
->children()[0]);
374 ASSERT_EQ(1, item2
->child_count());
375 ASSERT_EQ(item3
, item2
->children()[0]);
377 // Since item2 was a top-level item but is no longer, we should only have 1
379 items
= manager_
.MenuItems(item1
->id().extension_key
);
380 ASSERT_EQ(1u, items
->size());
381 ASSERT_EQ(item1
, items
->at(0));
383 // Move item3 back to being a child of item1, so it's now a sibling of item2.
384 ASSERT_TRUE(manager_
.ChangeParent(item3
->id(), &item1
->id()));
385 ASSERT_EQ(2, item1
->child_count());
386 ASSERT_EQ(item2
, item1
->children()[0]);
387 ASSERT_EQ(item3
, item1
->children()[1]);
389 // Try switching item3 to be the parent of item1 - this should fail.
390 ASSERT_FALSE(manager_
.ChangeParent(item1
->id(), &item3
->id()));
391 ASSERT_EQ(0, item3
->child_count());
392 ASSERT_EQ(2, item1
->child_count());
393 ASSERT_EQ(item2
, item1
->children()[0]);
394 ASSERT_EQ(item3
, item1
->children()[1]);
395 items
= manager_
.MenuItems(item1
->id().extension_key
);
396 ASSERT_EQ(1u, items
->size());
397 ASSERT_EQ(item1
, items
->at(0));
399 // Move item2 to be a top-level item.
400 ASSERT_TRUE(manager_
.ChangeParent(item2
->id(), NULL
));
401 items
= manager_
.MenuItems(item1
->id().extension_key
);
402 ASSERT_EQ(2u, items
->size());
403 ASSERT_EQ(item1
, items
->at(0));
404 ASSERT_EQ(item2
, items
->at(1));
405 ASSERT_EQ(1, item1
->child_count());
406 ASSERT_EQ(item3
, item1
->children()[0]);
408 // Make sure you can't move a node to be a child of another extension's item.
409 Extension
* extension2
= AddExtension("2222");
410 MenuItem
* item4
= CreateTestItem(extension2
);
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()));
415 // Make sure you can't make an item be it's own parent.
416 ASSERT_FALSE(manager_
.ChangeParent(item1
->id(), &item1
->id()));
419 // Tests that we properly remove an extension's menu item when that extension is
421 TEST_F(MenuManagerTest
, ExtensionUnloadRemovesMenuItems
) {
422 content::NotificationService
* notifier
=
423 content::NotificationService::current();
424 ASSERT_TRUE(notifier
!= NULL
);
426 // Create a test extension.
427 Extension
* extension1
= AddExtension("1111");
429 // Create an MenuItem and put it into the manager.
430 MenuItem
* item1
= CreateTestItem(extension1
);
431 MenuItem::Id id1
= item1
->id();
432 ASSERT_EQ(extension1
->id(), item1
->extension_id());
433 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item1
));
435 1u, manager_
.MenuItems(MenuItem::ExtensionKey(extension1
->id()))->size());
437 // Create a menu item with a different extension id and add it to the manager.
438 Extension
* extension2
= AddExtension("2222");
439 MenuItem
* item2
= CreateTestItem(extension2
);
440 ASSERT_NE(item1
->extension_id(), item2
->extension_id());
441 ASSERT_TRUE(manager_
.AddContextItem(extension2
, item2
));
443 // Notify that the extension was unloaded, and make sure the right item is
445 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile_
.get());
446 registry
->TriggerOnUnloaded(extension1
,
447 UnloadedExtensionInfo::REASON_DISABLE
);
449 ASSERT_EQ(NULL
, manager_
.MenuItems(MenuItem::ExtensionKey(extension1
->id())));
451 1u, manager_
.MenuItems(MenuItem::ExtensionKey(extension2
->id()))->size());
452 ASSERT_TRUE(manager_
.GetItemById(id1
) == NULL
);
453 ASSERT_TRUE(manager_
.GetItemById(item2
->id()) != NULL
);
458 // A mock message service for tests of MenuManager::ExecuteCommand.
459 class MockEventRouter
: public EventRouter
{
461 explicit MockEventRouter(Profile
* profile
) : EventRouter(profile
, NULL
) {}
463 MOCK_METHOD6(DispatchEventToExtensionMock
,
464 void(const std::string
& extension_id
,
465 const std::string
& event_name
,
466 base::ListValue
* event_args
,
467 content::BrowserContext
* source_context
,
468 const GURL
& event_url
,
469 EventRouter::UserGestureState state
));
471 virtual void DispatchEventToExtension(const std::string
& extension_id
,
472 scoped_ptr
<Event
> event
) {
473 DispatchEventToExtensionMock(extension_id
,
475 event
->event_args
.release(),
476 event
->restrict_to_browser_context
,
478 event
->user_gesture
);
482 DISALLOW_COPY_AND_ASSIGN(MockEventRouter
);
485 // A mock ExtensionSystem to serve our MockEventRouter.
486 class MockExtensionSystem
: public TestExtensionSystem
{
488 explicit MockExtensionSystem(Profile
* profile
)
489 : TestExtensionSystem(profile
) {}
491 EventRouter
* event_router() override
{
492 if (!mock_event_router_
)
493 mock_event_router_
.reset(new MockEventRouter(profile_
));
494 return mock_event_router_
.get();
498 scoped_ptr
<MockEventRouter
> mock_event_router_
;
500 DISALLOW_COPY_AND_ASSIGN(MockExtensionSystem
);
503 KeyedService
* BuildMockExtensionSystem(content::BrowserContext
* profile
) {
504 return new MockExtensionSystem(static_cast<Profile
*>(profile
));
509 // Tests the RemoveAll functionality.
510 TEST_F(MenuManagerTest
, RemoveAll
) {
511 // Try removing all items for an extension id that doesn't have any items.
512 manager_
.RemoveAllContextItems(MenuItem::ExtensionKey("CCCC"));
514 // Add 2 top-level and one child item for extension 1.
515 Extension
* extension1
= AddExtension("1111");
516 MenuItem
* item1
= CreateTestItem(extension1
);
517 MenuItem
* item2
= CreateTestItem(extension1
);
518 MenuItem
* item3
= CreateTestItem(extension1
);
519 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item1
));
520 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item2
));
521 ASSERT_TRUE(manager_
.AddChildItem(item1
->id(), item3
));
523 // Add one top-level item for extension 2.
524 Extension
* extension2
= AddExtension("2222");
525 MenuItem
* item4
= CreateTestItem(extension2
);
526 ASSERT_TRUE(manager_
.AddContextItem(extension2
, item4
));
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());
533 // Remove extension2's item.
534 manager_
.RemoveAllContextItems(key2
);
535 EXPECT_EQ(2u, manager_
.MenuItems(key1
)->size());
536 EXPECT_EQ(NULL
, manager_
.MenuItems(key2
));
538 // Remove extension1's items.
539 manager_
.RemoveAllContextItems(key1
);
540 EXPECT_EQ(NULL
, manager_
.MenuItems(key1
));
543 // Tests that removing all items one-by-one doesn't leave an entry around.
544 TEST_F(MenuManagerTest
, RemoveOneByOne
) {
546 Extension
* extension1
= AddExtension("1111");
547 MenuItem
* item1
= CreateTestItem(extension1
);
548 MenuItem
* item2
= CreateTestItem(extension1
);
549 MenuItem
* item3
= CreateTestItemWithID(extension1
, "id3");
550 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item1
));
551 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item2
));
552 ASSERT_TRUE(manager_
.AddContextItem(extension1
, item3
));
554 ASSERT_FALSE(manager_
.context_items_
.empty());
556 manager_
.RemoveContextMenuItem(item3
->id());
557 manager_
.RemoveContextMenuItem(item1
->id());
558 manager_
.RemoveContextMenuItem(item2
->id());
560 ASSERT_TRUE(manager_
.context_items_
.empty());
563 TEST_F(MenuManagerTest
, ExecuteCommand
) {
564 TestingProfile profile
;
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());
572 content::ContextMenuParams params
;
573 params
.media_type
= blink::WebContextMenuData::MediaTypeImage
;
574 params
.src_url
= GURL("http://foo.bar/image.png");
575 params
.page_url
= GURL("http://foo.bar");
576 params
.selection_text
= base::ASCIIToUTF16("Hello World");
577 params
.is_editable
= false;
579 Extension
* extension
= AddExtension("test");
580 MenuItem
* parent
= CreateTestItem(extension
);
581 MenuItem
* item
= CreateTestItem(extension
);
582 MenuItem::Id parent_id
= parent
->id();
583 MenuItem::Id id
= item
->id();
584 ASSERT_TRUE(manager_
.AddContextItem(extension
, parent
));
585 ASSERT_TRUE(manager_
.AddChildItem(parent
->id(), item
));
587 // Use the magic of googlemock to save a parameter to our mock's
588 // DispatchEventToExtension method into event_args.
589 base::ListValue
* list
= NULL
;
592 EXPECT_CALL(*mock_event_router
,
593 DispatchEventToExtensionMock(item
->extension_id(),
594 MenuManager::kOnContextMenus
,
598 EventRouter::USER_GESTURE_ENABLED
))
600 .WillOnce(SaveArg
<2>(&list
));
601 EXPECT_CALL(*mock_event_router
,
602 DispatchEventToExtensionMock(
603 item
->extension_id(),
604 context_menus::OnClicked::kEventName
,
608 EventRouter::USER_GESTURE_ENABLED
))
610 .WillOnce(DeleteArg
<2>());
612 manager_
.ExecuteCommand(&profile
, NULL
/* web_contents */, params
, id
);
614 ASSERT_EQ(2u, list
->GetSize());
616 base::DictionaryValue
* info
;
617 ASSERT_TRUE(list
->GetDictionary(0, &info
));
620 ASSERT_TRUE(info
->GetInteger("menuItemId", &tmp_id
));
621 ASSERT_EQ(id
.uid
, tmp_id
);
622 ASSERT_TRUE(info
->GetInteger("parentMenuItemId", &tmp_id
));
623 ASSERT_EQ(parent_id
.uid
, tmp_id
);
626 ASSERT_TRUE(info
->GetString("mediaType", &tmp
));
627 ASSERT_EQ("image", tmp
);
628 ASSERT_TRUE(info
->GetString("srcUrl", &tmp
));
629 ASSERT_EQ(params
.src_url
.spec(), tmp
);
630 ASSERT_TRUE(info
->GetString("pageUrl", &tmp
));
631 ASSERT_EQ(params
.page_url
.spec(), tmp
);
633 base::string16 tmp16
;
634 ASSERT_TRUE(info
->GetString("selectionText", &tmp16
));
635 ASSERT_EQ(params
.selection_text
, tmp16
);
637 bool bool_tmp
= true;
638 ASSERT_TRUE(info
->GetBoolean("editable", &bool_tmp
));
639 ASSERT_EQ(params
.is_editable
, bool_tmp
);
644 // Test that there is always only one radio item selected.
645 TEST_F(MenuManagerTest
, SanitizeRadioButtons
) {
646 Extension
* extension
= AddExtension("test");
648 // A single unchecked item should get checked
649 MenuItem
* item1
= CreateTestItem(extension
);
651 item1
->set_type(MenuItem::RADIO
);
652 item1
->SetChecked(false);
653 ASSERT_FALSE(item1
->checked());
654 manager_
.AddContextItem(extension
, item1
);
655 ASSERT_TRUE(item1
->checked());
657 // In a run of two unchecked items, the first should get selected.
658 item1
->SetChecked(false);
659 MenuItem
* item2
= CreateTestItem(extension
);
660 item2
->set_type(MenuItem::RADIO
);
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());
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());
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());
684 ASSERT_TRUE(item1
->checked());
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);
689 MenuItem
* new_item
= CreateTestItem(extension
);
690 new_item
->set_type(MenuItem::RADIO
);
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.
698 MenuItem
* parent
= CreateTestItem(extension
);
699 manager_
.AddContextItem(extension
, parent
);
700 MenuItem
* child1
= CreateTestItem(extension
);
701 child1
->set_type(MenuItem::RADIO
);
702 child1
->SetChecked(false);
703 MenuItem
* child2
= CreateTestItem(extension
);
704 child2
->set_type(MenuItem::RADIO
);
705 child2
->SetChecked(true);
706 ASSERT_FALSE(child1
->checked());
707 ASSERT_TRUE(child2
->checked());
709 manager_
.AddChildItem(parent
->id(), child1
);
710 ASSERT_TRUE(child1
->checked());
712 manager_
.AddChildItem(parent
->id(), child2
);
713 ASSERT_FALSE(child1
->checked());
714 ASSERT_TRUE(child2
->checked());
716 // Removing the checked item from the children should cause the
717 // remaining child to be checked.
718 manager_
.RemoveContextMenuItem(child2
->id());
720 ASSERT_TRUE(child1
->checked());
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());
728 // Removing |parent| should cause only |child1| to be selected.
729 manager_
.RemoveContextMenuItem(parent
->id());
731 ASSERT_FALSE(new_item
->checked());
732 ASSERT_TRUE(child1
->checked());
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
));
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
));
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
));
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());
766 // Remove all context menu items with incognito true.
767 manager_
.RemoveAllIncognitoContextItems();
768 EXPECT_EQ(2u, manager_
.MenuItems(key1
)->size());
769 EXPECT_EQ(1u, manager_
.MenuItems(key2
)->size());
772 } // namespace extensions