[Cleanup] Used scoped pointers in KeyedServiceFactory's SetTestingFactory functions.
[chromium-blink-merge.git] / chrome / browser / extensions / menu_manager_unittest.cc
blobd058f004b6979b7668856935d1325ed57e715475
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.
5 #include <vector>
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/json/json_reader.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/scoped_vector.h"
11 #include "base/message_loop/message_loop.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/event_router_factory.h"
29 #include "extensions/browser/extension_registry.h"
30 #include "extensions/browser/extension_system.h"
31 #include "extensions/common/extension.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 using content::BrowserThread;
36 using testing::_;
37 using testing::AtLeast;
38 using testing::DeleteArg;
39 using testing::InSequence;
40 using testing::Return;
41 using testing::SaveArg;
43 namespace extensions {
45 namespace context_menus = api::context_menus;
47 // Base class for tests.
48 class MenuManagerTest : public testing::Test {
49 public:
50 MenuManagerTest()
51 : ui_thread_(BrowserThread::UI, &message_loop_),
52 file_thread_(BrowserThread::FILE, &message_loop_),
53 profile_(new TestingProfile()),
54 manager_(profile_.get(),
55 ExtensionSystem::Get(profile_.get())->state_store()),
56 prefs_(message_loop_.message_loop_proxy().get()),
57 next_id_(1) {}
59 void TearDown() override {
60 prefs_.pref_service()->CommitPendingWrite();
61 message_loop_.RunUntilIdle();
64 // Returns a test item.
65 MenuItem* CreateTestItem(Extension* extension, bool incognito = false) {
66 MenuItem::Type type = MenuItem::NORMAL;
67 MenuItem::ContextList contexts(MenuItem::ALL);
68 const MenuItem::ExtensionKey key(extension->id());
69 MenuItem::Id id(incognito, key);
70 id.uid = next_id_++;
71 return new MenuItem(id, "test", false, true, type, contexts);
74 // Returns a test item with the given string ID.
75 MenuItem* CreateTestItemWithID(Extension* extension,
76 const std::string& string_id) {
77 MenuItem::Type type = MenuItem::NORMAL;
78 MenuItem::ContextList contexts(MenuItem::ALL);
79 const MenuItem::ExtensionKey key(extension->id());
80 MenuItem::Id id(false, key);
81 id.string_uid = string_id;
82 return new MenuItem(id, "test", false, true, type, contexts);
85 // Creates and returns a test Extension. The caller does *not* own the return
86 // value.
87 Extension* AddExtension(const std::string& name) {
88 scoped_refptr<Extension> extension = prefs_.AddExtension(name);
89 extensions_.push_back(extension);
90 return extension.get();
93 protected:
94 base::MessageLoopForUI message_loop_;
95 content::TestBrowserThread ui_thread_;
96 content::TestBrowserThread file_thread_;
97 scoped_ptr<TestingProfile> profile_;
99 MenuManager manager_;
100 ExtensionList extensions_;
101 TestExtensionPrefs prefs_;
102 int next_id_;
104 private:
105 DISALLOW_COPY_AND_ASSIGN(MenuManagerTest);
108 // Tests adding, getting, and removing items.
109 TEST_F(MenuManagerTest, AddGetRemoveItems) {
110 Extension* extension = AddExtension("test");
112 // Add a new item, make sure you can get it back.
113 MenuItem* item1 = CreateTestItem(extension);
114 ASSERT_TRUE(item1 != NULL);
115 ASSERT_TRUE(manager_.AddContextItem(extension, item1));
116 ASSERT_EQ(item1, manager_.GetItemById(item1->id()));
117 const MenuItem::List* items = manager_.MenuItems(item1->id().extension_key);
118 ASSERT_EQ(1u, items->size());
119 ASSERT_EQ(item1, items->at(0));
121 // Add a second item, make sure it comes back too.
122 MenuItem* item2 = CreateTestItemWithID(extension, "id2");
123 ASSERT_TRUE(manager_.AddContextItem(extension, item2));
124 ASSERT_EQ(item2, manager_.GetItemById(item2->id()));
125 items = manager_.MenuItems(item2->id().extension_key);
126 ASSERT_EQ(2u, items->size());
127 ASSERT_EQ(item1, items->at(0));
128 ASSERT_EQ(item2, items->at(1));
130 // Try adding item 3, then removing it.
131 MenuItem* item3 = CreateTestItem(extension);
132 MenuItem::Id id3 = item3->id();
133 const MenuItem::ExtensionKey extension_key3(item3->id().extension_key);
134 ASSERT_TRUE(manager_.AddContextItem(extension, item3));
135 ASSERT_EQ(item3, manager_.GetItemById(id3));
136 ASSERT_EQ(3u, manager_.MenuItems(extension_key3)->size());
137 ASSERT_TRUE(manager_.RemoveContextMenuItem(id3));
138 ASSERT_EQ(NULL, manager_.GetItemById(id3));
139 ASSERT_EQ(2u, manager_.MenuItems(extension_key3)->size());
141 // Make sure removing a non-existent item returns false.
142 const MenuItem::ExtensionKey key(extension->id());
143 MenuItem::Id id(false, key);
144 id.uid = id3.uid + 50;
145 ASSERT_FALSE(manager_.RemoveContextMenuItem(id));
147 // Make sure adding an item with the same string ID returns false.
148 scoped_ptr<MenuItem> item2too(CreateTestItemWithID(extension, "id2"));
149 ASSERT_FALSE(manager_.AddContextItem(extension, item2too.get()));
151 // But the same string ID should not collide with another extension.
152 Extension* extension2 = AddExtension("test2");
153 MenuItem* item2other = CreateTestItemWithID(extension2, "id2");
154 ASSERT_TRUE(manager_.AddContextItem(extension2, item2other));
157 // Test adding/removing child items.
158 TEST_F(MenuManagerTest, ChildFunctions) {
159 Extension* extension1 = AddExtension("1111");
160 Extension* extension2 = AddExtension("2222");
161 Extension* extension3 = AddExtension("3333");
163 MenuItem* item1 = CreateTestItem(extension1);
164 MenuItem* item2 = CreateTestItem(extension2);
165 MenuItem* item2_child = CreateTestItemWithID(extension2, "2child");
166 MenuItem* item2_grandchild = CreateTestItem(extension2);
168 // This third item we expect to fail inserting, so we use a scoped_ptr to make
169 // sure it gets deleted.
170 scoped_ptr<MenuItem> item3(CreateTestItem(extension3));
172 // Add in the first two items.
173 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
174 ASSERT_TRUE(manager_.AddContextItem(extension2, item2));
176 MenuItem::Id id1 = item1->id();
177 MenuItem::Id id2 = item2->id();
179 // Try adding item3 as a child of item2 - this should fail because item3 has
180 // a different extension id.
181 ASSERT_FALSE(manager_.AddChildItem(id2, item3.get()));
183 // Add item2_child as a child of item2.
184 MenuItem::Id id2_child = item2_child->id();
185 ASSERT_TRUE(manager_.AddChildItem(id2, item2_child));
186 ASSERT_EQ(1, item2->child_count());
187 ASSERT_EQ(0, item1->child_count());
188 ASSERT_EQ(item2_child, manager_.GetItemById(id2_child));
190 ASSERT_EQ(1u, manager_.MenuItems(item1->id().extension_key)->size());
191 ASSERT_EQ(item1, manager_.MenuItems(item1->id().extension_key)->at(0));
193 // Add item2_grandchild as a child of item2_child, then remove it.
194 MenuItem::Id id2_grandchild = item2_grandchild->id();
195 ASSERT_TRUE(manager_.AddChildItem(id2_child, item2_grandchild));
196 ASSERT_EQ(1, item2->child_count());
197 ASSERT_EQ(1, item2_child->child_count());
198 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_grandchild));
200 // We should only get 1 thing back when asking for item2's extension id, since
201 // it has a child item.
202 ASSERT_EQ(1u, manager_.MenuItems(item2->id().extension_key)->size());
203 ASSERT_EQ(item2, manager_.MenuItems(item2->id().extension_key)->at(0));
205 // Remove child2_item.
206 ASSERT_TRUE(manager_.RemoveContextMenuItem(id2_child));
207 ASSERT_EQ(1u, manager_.MenuItems(item2->id().extension_key)->size());
208 ASSERT_EQ(item2, manager_.MenuItems(item2->id().extension_key)->at(0));
209 ASSERT_EQ(0, item2->child_count());
212 TEST_F(MenuManagerTest, PopulateFromValue) {
213 Extension* extension = AddExtension("test");
215 bool incognito = true;
216 int type = MenuItem::CHECKBOX;
217 std::string title("TITLE");
218 bool checked = true;
219 bool enabled = true;
220 MenuItem::ContextList contexts;
221 contexts.Add(MenuItem::PAGE);
222 contexts.Add(MenuItem::SELECTION);
223 int contexts_value = 0;
224 ASSERT_TRUE(contexts.ToValue()->GetAsInteger(&contexts_value));
226 base::ListValue* document_url_patterns(new base::ListValue());
227 document_url_patterns->Append(
228 new base::StringValue("http://www.google.com/*"));
229 document_url_patterns->Append(
230 new base::StringValue("http://www.reddit.com/*"));
232 base::ListValue* target_url_patterns(new base::ListValue());
233 target_url_patterns->Append(
234 new base::StringValue("http://www.yahoo.com/*"));
235 target_url_patterns->Append(
236 new base::StringValue("http://www.facebook.com/*"));
238 base::DictionaryValue value;
239 value.SetBoolean("incognito", incognito);
240 value.SetString("string_uid", std::string());
241 value.SetInteger("type", type);
242 value.SetString("title", title);
243 value.SetBoolean("checked", checked);
244 value.SetBoolean("enabled", enabled);
245 value.SetInteger("contexts", contexts_value);
246 value.Set("document_url_patterns", document_url_patterns);
247 value.Set("target_url_patterns", target_url_patterns);
249 std::string error;
250 scoped_ptr<MenuItem> item(MenuItem::Populate(extension->id(), value, &error));
251 ASSERT_TRUE(item.get());
253 EXPECT_EQ(extension->id(), item->extension_id());
254 EXPECT_EQ(incognito, item->incognito());
255 EXPECT_EQ(title, item->title());
256 EXPECT_EQ(checked, item->checked());
257 EXPECT_EQ(item->checked(), item->checked());
258 EXPECT_EQ(enabled, item->enabled());
259 EXPECT_EQ(contexts, item->contexts());
261 URLPatternSet document_url_pattern_set;
262 document_url_pattern_set.Populate(*document_url_patterns,
263 URLPattern::SCHEME_ALL,
264 true,
265 &error);
266 EXPECT_EQ(document_url_pattern_set, item->document_url_patterns());
268 URLPatternSet target_url_pattern_set;
269 target_url_pattern_set.Populate(*target_url_patterns,
270 URLPattern::SCHEME_ALL,
271 true,
272 &error);
273 EXPECT_EQ(target_url_pattern_set, item->target_url_patterns());
276 // Tests that deleting a parent properly removes descendants.
277 TEST_F(MenuManagerTest, DeleteParent) {
278 Extension* extension = AddExtension("1111");
280 // Set up 5 items to add.
281 MenuItem* item1 = CreateTestItem(extension);
282 MenuItem* item2 = CreateTestItem(extension);
283 MenuItem* item3 = CreateTestItemWithID(extension, "id3");
284 MenuItem* item4 = CreateTestItemWithID(extension, "id4");
285 MenuItem* item5 = CreateTestItem(extension);
286 MenuItem* item6 = CreateTestItem(extension);
287 MenuItem::Id item1_id = item1->id();
288 MenuItem::Id item2_id = item2->id();
289 MenuItem::Id item3_id = item3->id();
290 MenuItem::Id item4_id = item4->id();
291 MenuItem::Id item5_id = item5->id();
292 MenuItem::Id item6_id = item6->id();
293 const MenuItem::ExtensionKey key(extension->id());
295 // Add the items in the hierarchy
296 // item1 -> item2 -> item3 -> item4 -> item5 -> item6.
297 ASSERT_TRUE(manager_.AddContextItem(extension, item1));
298 ASSERT_TRUE(manager_.AddChildItem(item1_id, item2));
299 ASSERT_TRUE(manager_.AddChildItem(item2_id, item3));
300 ASSERT_TRUE(manager_.AddChildItem(item3_id, item4));
301 ASSERT_TRUE(manager_.AddChildItem(item4_id, item5));
302 ASSERT_TRUE(manager_.AddChildItem(item5_id, item6));
303 ASSERT_EQ(item1, manager_.GetItemById(item1_id));
304 ASSERT_EQ(item2, manager_.GetItemById(item2_id));
305 ASSERT_EQ(item3, manager_.GetItemById(item3_id));
306 ASSERT_EQ(item4, manager_.GetItemById(item4_id));
307 ASSERT_EQ(item5, manager_.GetItemById(item5_id));
308 ASSERT_EQ(item6, manager_.GetItemById(item6_id));
309 ASSERT_EQ(1u, manager_.MenuItems(key)->size());
310 ASSERT_EQ(6u, manager_.items_by_id_.size());
312 // Remove item6 (a leaf node).
313 ASSERT_TRUE(manager_.RemoveContextMenuItem(item6_id));
314 ASSERT_EQ(item1, manager_.GetItemById(item1_id));
315 ASSERT_EQ(item2, manager_.GetItemById(item2_id));
316 ASSERT_EQ(item3, manager_.GetItemById(item3_id));
317 ASSERT_EQ(item4, manager_.GetItemById(item4_id));
318 ASSERT_EQ(item5, manager_.GetItemById(item5_id));
319 ASSERT_EQ(NULL, manager_.GetItemById(item6_id));
320 ASSERT_EQ(1u, manager_.MenuItems(key)->size());
321 ASSERT_EQ(5u, manager_.items_by_id_.size());
323 // Remove item4 and make sure item5 is gone as well.
324 ASSERT_TRUE(manager_.RemoveContextMenuItem(item4_id));
325 ASSERT_EQ(item1, manager_.GetItemById(item1_id));
326 ASSERT_EQ(item2, manager_.GetItemById(item2_id));
327 ASSERT_EQ(item3, manager_.GetItemById(item3_id));
328 ASSERT_EQ(NULL, manager_.GetItemById(item4_id));
329 ASSERT_EQ(NULL, manager_.GetItemById(item5_id));
330 ASSERT_EQ(1u, manager_.MenuItems(key)->size());
331 ASSERT_EQ(3u, manager_.items_by_id_.size());
333 // Now remove item1 and make sure item2 and item3 are gone as well.
334 ASSERT_TRUE(manager_.RemoveContextMenuItem(item1_id));
335 ASSERT_EQ(NULL, manager_.MenuItems(key));
336 ASSERT_EQ(0u, manager_.items_by_id_.size());
337 ASSERT_EQ(NULL, manager_.GetItemById(item1_id));
338 ASSERT_EQ(NULL, manager_.GetItemById(item2_id));
339 ASSERT_EQ(NULL, manager_.GetItemById(item3_id));
342 // Tests changing parents.
343 TEST_F(MenuManagerTest, ChangeParent) {
344 Extension* extension1 = AddExtension("1111");
346 // First create two items and add them both to the manager.
347 MenuItem* item1 = CreateTestItem(extension1);
348 MenuItem* item2 = CreateTestItem(extension1);
350 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
351 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
353 const MenuItem::List* items = manager_.MenuItems(item1->id().extension_key);
354 ASSERT_EQ(2u, items->size());
355 ASSERT_EQ(item1, items->at(0));
356 ASSERT_EQ(item2, items->at(1));
358 // Now create a third item, initially add it as a child of item1, then move
359 // it to be a child of item2.
360 MenuItem* item3 = CreateTestItem(extension1);
362 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
363 ASSERT_EQ(1, item1->child_count());
364 ASSERT_EQ(item3, item1->children()[0]);
366 ASSERT_TRUE(manager_.ChangeParent(item3->id(), &item2->id()));
367 ASSERT_EQ(0, item1->child_count());
368 ASSERT_EQ(1, item2->child_count());
369 ASSERT_EQ(item3, item2->children()[0]);
371 // Move item2 to be a child of item1.
372 ASSERT_TRUE(manager_.ChangeParent(item2->id(), &item1->id()));
373 ASSERT_EQ(1, item1->child_count());
374 ASSERT_EQ(item2, item1->children()[0]);
375 ASSERT_EQ(1, item2->child_count());
376 ASSERT_EQ(item3, item2->children()[0]);
378 // Since item2 was a top-level item but is no longer, we should only have 1
379 // top-level item.
380 items = manager_.MenuItems(item1->id().extension_key);
381 ASSERT_EQ(1u, items->size());
382 ASSERT_EQ(item1, items->at(0));
384 // Move item3 back to being a child of item1, so it's now a sibling of item2.
385 ASSERT_TRUE(manager_.ChangeParent(item3->id(), &item1->id()));
386 ASSERT_EQ(2, item1->child_count());
387 ASSERT_EQ(item2, item1->children()[0]);
388 ASSERT_EQ(item3, item1->children()[1]);
390 // Try switching item3 to be the parent of item1 - this should fail.
391 ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item3->id()));
392 ASSERT_EQ(0, item3->child_count());
393 ASSERT_EQ(2, item1->child_count());
394 ASSERT_EQ(item2, item1->children()[0]);
395 ASSERT_EQ(item3, item1->children()[1]);
396 items = manager_.MenuItems(item1->id().extension_key);
397 ASSERT_EQ(1u, items->size());
398 ASSERT_EQ(item1, items->at(0));
400 // Move item2 to be a top-level item.
401 ASSERT_TRUE(manager_.ChangeParent(item2->id(), NULL));
402 items = manager_.MenuItems(item1->id().extension_key);
403 ASSERT_EQ(2u, items->size());
404 ASSERT_EQ(item1, items->at(0));
405 ASSERT_EQ(item2, items->at(1));
406 ASSERT_EQ(1, item1->child_count());
407 ASSERT_EQ(item3, item1->children()[0]);
409 // Make sure you can't move a node to be a child of another extension's item.
410 Extension* extension2 = AddExtension("2222");
411 MenuItem* item4 = CreateTestItem(extension2);
412 ASSERT_TRUE(manager_.AddContextItem(extension2, item4));
413 ASSERT_FALSE(manager_.ChangeParent(item4->id(), &item1->id()));
414 ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item4->id()));
416 // Make sure you can't make an item be it's own parent.
417 ASSERT_FALSE(manager_.ChangeParent(item1->id(), &item1->id()));
420 // Tests that we properly remove an extension's menu item when that extension is
421 // unloaded.
422 TEST_F(MenuManagerTest, ExtensionUnloadRemovesMenuItems) {
423 content::NotificationService* notifier =
424 content::NotificationService::current();
425 ASSERT_TRUE(notifier != NULL);
427 // Create a test extension.
428 Extension* extension1 = AddExtension("1111");
430 // Create an MenuItem and put it into the manager.
431 MenuItem* item1 = CreateTestItem(extension1);
432 MenuItem::Id id1 = item1->id();
433 ASSERT_EQ(extension1->id(), item1->extension_id());
434 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
435 ASSERT_EQ(
436 1u, manager_.MenuItems(MenuItem::ExtensionKey(extension1->id()))->size());
438 // Create a menu item with a different extension id and add it to the manager.
439 Extension* extension2 = AddExtension("2222");
440 MenuItem* item2 = CreateTestItem(extension2);
441 ASSERT_NE(item1->extension_id(), item2->extension_id());
442 ASSERT_TRUE(manager_.AddContextItem(extension2, item2));
444 // Notify that the extension was unloaded, and make sure the right item is
445 // gone.
446 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_.get());
447 registry->TriggerOnUnloaded(extension1,
448 UnloadedExtensionInfo::REASON_DISABLE);
450 ASSERT_EQ(NULL, manager_.MenuItems(MenuItem::ExtensionKey(extension1->id())));
451 ASSERT_EQ(
452 1u, manager_.MenuItems(MenuItem::ExtensionKey(extension2->id()))->size());
453 ASSERT_TRUE(manager_.GetItemById(id1) == NULL);
454 ASSERT_TRUE(manager_.GetItemById(item2->id()) != NULL);
457 namespace {
459 // A mock message service for tests of MenuManager::ExecuteCommand.
460 class MockEventRouter : public EventRouter {
461 public:
462 explicit MockEventRouter(Profile* profile) : EventRouter(profile, NULL) {}
464 MOCK_METHOD6(DispatchEventToExtensionMock,
465 void(const std::string& extension_id,
466 const std::string& event_name,
467 base::ListValue* event_args,
468 content::BrowserContext* source_context,
469 const GURL& event_url,
470 EventRouter::UserGestureState state));
472 virtual void DispatchEventToExtension(const std::string& extension_id,
473 scoped_ptr<Event> event) {
474 DispatchEventToExtensionMock(extension_id,
475 event->event_name,
476 event->event_args.release(),
477 event->restrict_to_browser_context,
478 event->event_url,
479 event->user_gesture);
482 private:
483 DISALLOW_COPY_AND_ASSIGN(MockEventRouter);
486 // MockEventRouter factory function
487 scoped_ptr<KeyedService> MockEventRouterFactoryFunction(
488 content::BrowserContext* context) {
489 return make_scoped_ptr(new MockEventRouter(static_cast<Profile*>(context)));
492 } // namespace
494 // Tests the RemoveAll functionality.
495 TEST_F(MenuManagerTest, RemoveAll) {
496 // Try removing all items for an extension id that doesn't have any items.
497 manager_.RemoveAllContextItems(MenuItem::ExtensionKey("CCCC"));
499 // Add 2 top-level and one child item for extension 1.
500 Extension* extension1 = AddExtension("1111");
501 MenuItem* item1 = CreateTestItem(extension1);
502 MenuItem* item2 = CreateTestItem(extension1);
503 MenuItem* item3 = CreateTestItem(extension1);
504 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
505 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
506 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
508 // Add one top-level item for extension 2.
509 Extension* extension2 = AddExtension("2222");
510 MenuItem* item4 = CreateTestItem(extension2);
511 ASSERT_TRUE(manager_.AddContextItem(extension2, item4));
513 const MenuItem::ExtensionKey key1(extension1->id());
514 const MenuItem::ExtensionKey key2(extension2->id());
515 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
516 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
518 // Remove extension2's item.
519 manager_.RemoveAllContextItems(key2);
520 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
521 EXPECT_EQ(NULL, manager_.MenuItems(key2));
523 // Remove extension1's items.
524 manager_.RemoveAllContextItems(key1);
525 EXPECT_EQ(NULL, manager_.MenuItems(key1));
528 // Tests that removing all items one-by-one doesn't leave an entry around.
529 TEST_F(MenuManagerTest, RemoveOneByOne) {
530 // Add 2 test items.
531 Extension* extension1 = AddExtension("1111");
532 MenuItem* item1 = CreateTestItem(extension1);
533 MenuItem* item2 = CreateTestItem(extension1);
534 MenuItem* item3 = CreateTestItemWithID(extension1, "id3");
535 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
536 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
537 ASSERT_TRUE(manager_.AddContextItem(extension1, item3));
539 ASSERT_FALSE(manager_.context_items_.empty());
541 manager_.RemoveContextMenuItem(item3->id());
542 manager_.RemoveContextMenuItem(item1->id());
543 manager_.RemoveContextMenuItem(item2->id());
545 ASSERT_TRUE(manager_.context_items_.empty());
548 TEST_F(MenuManagerTest, ExecuteCommand) {
549 TestingProfile profile;
550 MockEventRouter* mock_event_router = static_cast<MockEventRouter*>(
551 EventRouterFactory::GetInstance()->SetTestingFactoryAndUse(
552 &profile, &MockEventRouterFactoryFunction));
554 content::ContextMenuParams params;
555 params.media_type = blink::WebContextMenuData::MediaTypeImage;
556 params.src_url = GURL("http://foo.bar/image.png");
557 params.page_url = GURL("http://foo.bar");
558 params.selection_text = base::ASCIIToUTF16("Hello World");
559 params.is_editable = false;
561 Extension* extension = AddExtension("test");
562 MenuItem* parent = CreateTestItem(extension);
563 MenuItem* item = CreateTestItem(extension);
564 MenuItem::Id parent_id = parent->id();
565 MenuItem::Id id = item->id();
566 ASSERT_TRUE(manager_.AddContextItem(extension, parent));
567 ASSERT_TRUE(manager_.AddChildItem(parent->id(), item));
569 // Use the magic of googlemock to save a parameter to our mock's
570 // DispatchEventToExtension method into event_args.
571 base::ListValue* list = NULL;
573 InSequence s;
574 EXPECT_CALL(*mock_event_router,
575 DispatchEventToExtensionMock(item->extension_id(),
576 MenuManager::kOnContextMenus,
578 &profile,
579 GURL(),
580 EventRouter::USER_GESTURE_ENABLED))
581 .Times(1)
582 .WillOnce(SaveArg<2>(&list));
583 EXPECT_CALL(*mock_event_router,
584 DispatchEventToExtensionMock(
585 item->extension_id(),
586 context_menus::OnClicked::kEventName,
588 &profile,
589 GURL(),
590 EventRouter::USER_GESTURE_ENABLED))
591 .Times(1)
592 .WillOnce(DeleteArg<2>());
594 manager_.ExecuteCommand(&profile, NULL /* web_contents */, params, id);
596 ASSERT_EQ(2u, list->GetSize());
598 base::DictionaryValue* info;
599 ASSERT_TRUE(list->GetDictionary(0, &info));
601 int tmp_id = 0;
602 ASSERT_TRUE(info->GetInteger("menuItemId", &tmp_id));
603 ASSERT_EQ(id.uid, tmp_id);
604 ASSERT_TRUE(info->GetInteger("parentMenuItemId", &tmp_id));
605 ASSERT_EQ(parent_id.uid, tmp_id);
607 std::string tmp;
608 ASSERT_TRUE(info->GetString("mediaType", &tmp));
609 ASSERT_EQ("image", tmp);
610 ASSERT_TRUE(info->GetString("srcUrl", &tmp));
611 ASSERT_EQ(params.src_url.spec(), tmp);
612 ASSERT_TRUE(info->GetString("pageUrl", &tmp));
613 ASSERT_EQ(params.page_url.spec(), tmp);
615 base::string16 tmp16;
616 ASSERT_TRUE(info->GetString("selectionText", &tmp16));
617 ASSERT_EQ(params.selection_text, tmp16);
619 bool bool_tmp = true;
620 ASSERT_TRUE(info->GetBoolean("editable", &bool_tmp));
621 ASSERT_EQ(params.is_editable, bool_tmp);
623 delete list;
626 // Test that there is always only one radio item selected.
627 TEST_F(MenuManagerTest, SanitizeRadioButtons) {
628 Extension* extension = AddExtension("test");
630 // A single unchecked item should get checked
631 MenuItem* item1 = CreateTestItem(extension);
633 item1->set_type(MenuItem::RADIO);
634 item1->SetChecked(false);
635 ASSERT_FALSE(item1->checked());
636 manager_.AddContextItem(extension, item1);
637 ASSERT_TRUE(item1->checked());
639 // In a run of two unchecked items, the first should get selected.
640 item1->SetChecked(false);
641 MenuItem* item2 = CreateTestItem(extension);
642 item2->set_type(MenuItem::RADIO);
643 item2->SetChecked(false);
644 ASSERT_FALSE(item1->checked());
645 ASSERT_FALSE(item2->checked());
646 manager_.AddContextItem(extension, item2);
647 ASSERT_TRUE(item1->checked());
648 ASSERT_FALSE(item2->checked());
650 // If multiple items are checked, only the last item should get checked.
651 item1->SetChecked(true);
652 item2->SetChecked(true);
653 ASSERT_TRUE(item1->checked());
654 ASSERT_TRUE(item2->checked());
655 manager_.ItemUpdated(item1->id());
656 ASSERT_FALSE(item1->checked());
657 ASSERT_TRUE(item2->checked());
659 // If the checked item is removed, the new first item should get checked.
660 item1->SetChecked(false);
661 item2->SetChecked(true);
662 ASSERT_FALSE(item1->checked());
663 ASSERT_TRUE(item2->checked());
664 manager_.RemoveContextMenuItem(item2->id());
665 item2 = NULL;
666 ASSERT_TRUE(item1->checked());
668 // If a checked item is added to a run that already has a checked item,
669 // then the new item should get checked.
670 item1->SetChecked(true);
671 MenuItem* new_item = CreateTestItem(extension);
672 new_item->set_type(MenuItem::RADIO);
673 new_item->SetChecked(true);
674 ASSERT_TRUE(item1->checked());
675 ASSERT_TRUE(new_item->checked());
676 manager_.AddContextItem(extension, new_item);
677 ASSERT_FALSE(item1->checked());
678 ASSERT_TRUE(new_item->checked());
679 // Make sure that children are checked as well.
680 MenuItem* parent = CreateTestItem(extension);
681 manager_.AddContextItem(extension, parent);
682 MenuItem* child1 = CreateTestItem(extension);
683 child1->set_type(MenuItem::RADIO);
684 child1->SetChecked(false);
685 MenuItem* child2 = CreateTestItem(extension);
686 child2->set_type(MenuItem::RADIO);
687 child2->SetChecked(true);
688 ASSERT_FALSE(child1->checked());
689 ASSERT_TRUE(child2->checked());
691 manager_.AddChildItem(parent->id(), child1);
692 ASSERT_TRUE(child1->checked());
694 manager_.AddChildItem(parent->id(), child2);
695 ASSERT_FALSE(child1->checked());
696 ASSERT_TRUE(child2->checked());
698 // Removing the checked item from the children should cause the
699 // remaining child to be checked.
700 manager_.RemoveContextMenuItem(child2->id());
701 child2 = NULL;
702 ASSERT_TRUE(child1->checked());
704 // This should NOT cause |new_item| to be deseleted because
705 // |parent| will be seperating the two runs of radio items.
706 manager_.ChangeParent(child1->id(), NULL);
707 ASSERT_TRUE(new_item->checked());
708 ASSERT_TRUE(child1->checked());
710 // Removing |parent| should cause only |child1| to be selected.
711 manager_.RemoveContextMenuItem(parent->id());
712 parent = NULL;
713 ASSERT_FALSE(new_item->checked());
714 ASSERT_TRUE(child1->checked());
717 // Tests the RemoveAllIncognitoContextItems functionality.
718 TEST_F(MenuManagerTest, RemoveAllIncognito) {
719 Extension* extension1 = AddExtension("1111");
720 // Add 2 top-level and one child item for extension 1
721 // with incognito 'true'.
722 MenuItem* item1 = CreateTestItem(extension1, true);
723 MenuItem* item2 = CreateTestItem(extension1, true);
724 MenuItem* item3 = CreateTestItem(extension1, true);
725 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
726 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
727 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
729 // Add 2 top-level and one child item for extension 1
730 // with incognito 'false'.
731 MenuItem* item4 = CreateTestItem(extension1);
732 MenuItem* item5 = CreateTestItem(extension1);
733 MenuItem* item6 = CreateTestItem(extension1);
734 ASSERT_TRUE(manager_.AddContextItem(extension1, item4));
735 ASSERT_TRUE(manager_.AddContextItem(extension1, item5));
736 ASSERT_TRUE(manager_.AddChildItem(item4->id(), item6));
738 // Add one top-level item for extension 2.
739 Extension* extension2 = AddExtension("2222");
740 MenuItem* item7 = CreateTestItem(extension2);
741 ASSERT_TRUE(manager_.AddContextItem(extension2, item7));
743 const MenuItem::ExtensionKey key1(extension1->id());
744 const MenuItem::ExtensionKey key2(extension2->id());
745 EXPECT_EQ(4u, manager_.MenuItems(key1)->size());
746 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
748 // Remove all context menu items with incognito true.
749 manager_.RemoveAllIncognitoContextItems();
750 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
751 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
754 } // namespace extensions