[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / extensions / menu_manager_unittest.cc
blob2afb80bdd480e4c0b08cd9ff38d632596fad5123
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/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_system_factory.h"
16 #include "chrome/browser/extensions/menu_manager.h"
17 #include "chrome/browser/extensions/test_extension_prefs.h"
18 #include "chrome/browser/extensions/test_extension_system.h"
19 #include "chrome/browser/prefs/pref_service_syncable.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/extensions/api/context_menus.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/common/context_menu_params.h"
25 #include "content/public/test/test_browser_thread.h"
26 #include "extensions/browser/event_router.h"
27 #include "extensions/browser/event_router_factory.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;
35 using testing::_;
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 {
48 public:
49 MenuManagerTest()
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_.task_runner().get()),
56 next_id_(1) {}
58 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);
69 id.uid = next_id_++;
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
85 // value.
86 Extension* AddExtension(const std::string& name) {
87 scoped_refptr<Extension> extension = prefs_.AddExtension(name);
88 extensions_.push_back(extension);
89 return extension.get();
92 protected:
93 base::MessageLoopForUI message_loop_;
94 content::TestBrowserThread ui_thread_;
95 content::TestBrowserThread file_thread_;
96 scoped_ptr<TestingProfile> profile_;
98 MenuManager manager_;
99 ExtensionList extensions_;
100 TestExtensionPrefs prefs_;
101 int next_id_;
103 private:
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");
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));
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);
248 std::string error;
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,
263 true,
264 &error);
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,
270 true,
271 &error);
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
378 // top-level item.
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
420 // unloaded.
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));
434 ASSERT_EQ(
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
444 // gone.
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())));
450 ASSERT_EQ(
451 1u, manager_.MenuItems(MenuItem::ExtensionKey(extension2->id()))->size());
452 ASSERT_TRUE(manager_.GetItemById(id1) == NULL);
453 ASSERT_TRUE(manager_.GetItemById(item2->id()) != NULL);
456 namespace {
458 // A mock message service for tests of MenuManager::ExecuteCommand.
459 class MockEventRouter : public EventRouter {
460 public:
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,
474 event->event_name,
475 event->event_args.release(),
476 event->restrict_to_browser_context,
477 event->event_url,
478 event->user_gesture);
481 private:
482 DISALLOW_COPY_AND_ASSIGN(MockEventRouter);
485 // MockEventRouter factory function
486 scoped_ptr<KeyedService> MockEventRouterFactoryFunction(
487 content::BrowserContext* context) {
488 return make_scoped_ptr(new MockEventRouter(static_cast<Profile*>(context)));
491 } // namespace
493 // Tests the RemoveAll functionality.
494 TEST_F(MenuManagerTest, RemoveAll) {
495 // Try removing all items for an extension id that doesn't have any items.
496 manager_.RemoveAllContextItems(MenuItem::ExtensionKey("CCCC"));
498 // Add 2 top-level and one child item for extension 1.
499 Extension* extension1 = AddExtension("1111");
500 MenuItem* item1 = CreateTestItem(extension1);
501 MenuItem* item2 = CreateTestItem(extension1);
502 MenuItem* item3 = CreateTestItem(extension1);
503 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
504 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
505 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
507 // Add one top-level item for extension 2.
508 Extension* extension2 = AddExtension("2222");
509 MenuItem* item4 = CreateTestItem(extension2);
510 ASSERT_TRUE(manager_.AddContextItem(extension2, item4));
512 const MenuItem::ExtensionKey key1(extension1->id());
513 const MenuItem::ExtensionKey key2(extension2->id());
514 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
515 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
517 // Remove extension2's item.
518 manager_.RemoveAllContextItems(key2);
519 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
520 EXPECT_EQ(NULL, manager_.MenuItems(key2));
522 // Remove extension1's items.
523 manager_.RemoveAllContextItems(key1);
524 EXPECT_EQ(NULL, manager_.MenuItems(key1));
527 // Tests that removing all items one-by-one doesn't leave an entry around.
528 TEST_F(MenuManagerTest, RemoveOneByOne) {
529 // Add 2 test items.
530 Extension* extension1 = AddExtension("1111");
531 MenuItem* item1 = CreateTestItem(extension1);
532 MenuItem* item2 = CreateTestItem(extension1);
533 MenuItem* item3 = CreateTestItemWithID(extension1, "id3");
534 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
535 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
536 ASSERT_TRUE(manager_.AddContextItem(extension1, item3));
538 ASSERT_FALSE(manager_.context_items_.empty());
540 manager_.RemoveContextMenuItem(item3->id());
541 manager_.RemoveContextMenuItem(item1->id());
542 manager_.RemoveContextMenuItem(item2->id());
544 ASSERT_TRUE(manager_.context_items_.empty());
547 TEST_F(MenuManagerTest, ExecuteCommand) {
548 TestingProfile profile;
549 MockEventRouter* mock_event_router = static_cast<MockEventRouter*>(
550 EventRouterFactory::GetInstance()->SetTestingFactoryAndUse(
551 &profile, &MockEventRouterFactoryFunction));
553 content::ContextMenuParams params;
554 params.media_type = blink::WebContextMenuData::MediaTypeImage;
555 params.src_url = GURL("http://foo.bar/image.png");
556 params.page_url = GURL("http://foo.bar");
557 params.selection_text = base::ASCIIToUTF16("Hello World");
558 params.is_editable = false;
560 Extension* extension = AddExtension("test");
561 MenuItem* parent = CreateTestItem(extension);
562 MenuItem* item = CreateTestItem(extension);
563 MenuItem::Id parent_id = parent->id();
564 MenuItem::Id id = item->id();
565 ASSERT_TRUE(manager_.AddContextItem(extension, parent));
566 ASSERT_TRUE(manager_.AddChildItem(parent->id(), item));
568 // Use the magic of googlemock to save a parameter to our mock's
569 // DispatchEventToExtension method into event_args.
570 base::ListValue* list = NULL;
572 InSequence s;
573 EXPECT_CALL(*mock_event_router,
574 DispatchEventToExtensionMock(item->extension_id(),
575 MenuManager::kOnContextMenus,
577 &profile,
578 GURL(),
579 EventRouter::USER_GESTURE_ENABLED))
580 .Times(1)
581 .WillOnce(SaveArg<2>(&list));
582 EXPECT_CALL(*mock_event_router,
583 DispatchEventToExtensionMock(
584 item->extension_id(),
585 context_menus::OnClicked::kEventName,
587 &profile,
588 GURL(),
589 EventRouter::USER_GESTURE_ENABLED))
590 .Times(1)
591 .WillOnce(DeleteArg<2>());
593 manager_.ExecuteCommand(&profile, NULL /* web_contents */, params, id);
595 ASSERT_EQ(2u, list->GetSize());
597 base::DictionaryValue* info;
598 ASSERT_TRUE(list->GetDictionary(0, &info));
600 int tmp_id = 0;
601 ASSERT_TRUE(info->GetInteger("menuItemId", &tmp_id));
602 ASSERT_EQ(id.uid, tmp_id);
603 ASSERT_TRUE(info->GetInteger("parentMenuItemId", &tmp_id));
604 ASSERT_EQ(parent_id.uid, tmp_id);
606 std::string tmp;
607 ASSERT_TRUE(info->GetString("mediaType", &tmp));
608 ASSERT_EQ("image", tmp);
609 ASSERT_TRUE(info->GetString("srcUrl", &tmp));
610 ASSERT_EQ(params.src_url.spec(), tmp);
611 ASSERT_TRUE(info->GetString("pageUrl", &tmp));
612 ASSERT_EQ(params.page_url.spec(), tmp);
614 base::string16 tmp16;
615 ASSERT_TRUE(info->GetString("selectionText", &tmp16));
616 ASSERT_EQ(params.selection_text, tmp16);
618 bool bool_tmp = true;
619 ASSERT_TRUE(info->GetBoolean("editable", &bool_tmp));
620 ASSERT_EQ(params.is_editable, bool_tmp);
622 delete list;
625 // Test that there is always only one radio item selected.
626 TEST_F(MenuManagerTest, SanitizeRadioButtons) {
627 Extension* extension = AddExtension("test");
629 // A single unchecked item should get checked
630 MenuItem* item1 = CreateTestItem(extension);
632 item1->set_type(MenuItem::RADIO);
633 item1->SetChecked(false);
634 ASSERT_FALSE(item1->checked());
635 manager_.AddContextItem(extension, item1);
636 ASSERT_TRUE(item1->checked());
638 // In a run of two unchecked items, the first should get selected.
639 item1->SetChecked(false);
640 MenuItem* item2 = CreateTestItem(extension);
641 item2->set_type(MenuItem::RADIO);
642 item2->SetChecked(false);
643 ASSERT_FALSE(item1->checked());
644 ASSERT_FALSE(item2->checked());
645 manager_.AddContextItem(extension, item2);
646 ASSERT_TRUE(item1->checked());
647 ASSERT_FALSE(item2->checked());
649 // If multiple items are checked, only the last item should get checked.
650 item1->SetChecked(true);
651 item2->SetChecked(true);
652 ASSERT_TRUE(item1->checked());
653 ASSERT_TRUE(item2->checked());
654 manager_.ItemUpdated(item1->id());
655 ASSERT_FALSE(item1->checked());
656 ASSERT_TRUE(item2->checked());
658 // If the checked item is removed, the new first item should get checked.
659 item1->SetChecked(false);
660 item2->SetChecked(true);
661 ASSERT_FALSE(item1->checked());
662 ASSERT_TRUE(item2->checked());
663 manager_.RemoveContextMenuItem(item2->id());
664 item2 = NULL;
665 ASSERT_TRUE(item1->checked());
667 // If a checked item is added to a run that already has a checked item,
668 // then the new item should get checked.
669 item1->SetChecked(true);
670 MenuItem* new_item = CreateTestItem(extension);
671 new_item->set_type(MenuItem::RADIO);
672 new_item->SetChecked(true);
673 ASSERT_TRUE(item1->checked());
674 ASSERT_TRUE(new_item->checked());
675 manager_.AddContextItem(extension, new_item);
676 ASSERT_FALSE(item1->checked());
677 ASSERT_TRUE(new_item->checked());
678 // Make sure that children are checked as well.
679 MenuItem* parent = CreateTestItem(extension);
680 manager_.AddContextItem(extension, parent);
681 MenuItem* child1 = CreateTestItem(extension);
682 child1->set_type(MenuItem::RADIO);
683 child1->SetChecked(false);
684 MenuItem* child2 = CreateTestItem(extension);
685 child2->set_type(MenuItem::RADIO);
686 child2->SetChecked(true);
687 ASSERT_FALSE(child1->checked());
688 ASSERT_TRUE(child2->checked());
690 manager_.AddChildItem(parent->id(), child1);
691 ASSERT_TRUE(child1->checked());
693 manager_.AddChildItem(parent->id(), child2);
694 ASSERT_FALSE(child1->checked());
695 ASSERT_TRUE(child2->checked());
697 // Removing the checked item from the children should cause the
698 // remaining child to be checked.
699 manager_.RemoveContextMenuItem(child2->id());
700 child2 = NULL;
701 ASSERT_TRUE(child1->checked());
703 // This should NOT cause |new_item| to be deseleted because
704 // |parent| will be seperating the two runs of radio items.
705 manager_.ChangeParent(child1->id(), NULL);
706 ASSERT_TRUE(new_item->checked());
707 ASSERT_TRUE(child1->checked());
709 // Removing |parent| should cause only |child1| to be selected.
710 manager_.RemoveContextMenuItem(parent->id());
711 parent = NULL;
712 ASSERT_FALSE(new_item->checked());
713 ASSERT_TRUE(child1->checked());
716 // Tests the RemoveAllIncognitoContextItems functionality.
717 TEST_F(MenuManagerTest, RemoveAllIncognito) {
718 Extension* extension1 = AddExtension("1111");
719 // Add 2 top-level and one child item for extension 1
720 // with incognito 'true'.
721 MenuItem* item1 = CreateTestItem(extension1, true);
722 MenuItem* item2 = CreateTestItem(extension1, true);
723 MenuItem* item3 = CreateTestItem(extension1, true);
724 ASSERT_TRUE(manager_.AddContextItem(extension1, item1));
725 ASSERT_TRUE(manager_.AddContextItem(extension1, item2));
726 ASSERT_TRUE(manager_.AddChildItem(item1->id(), item3));
728 // Add 2 top-level and one child item for extension 1
729 // with incognito 'false'.
730 MenuItem* item4 = CreateTestItem(extension1);
731 MenuItem* item5 = CreateTestItem(extension1);
732 MenuItem* item6 = CreateTestItem(extension1);
733 ASSERT_TRUE(manager_.AddContextItem(extension1, item4));
734 ASSERT_TRUE(manager_.AddContextItem(extension1, item5));
735 ASSERT_TRUE(manager_.AddChildItem(item4->id(), item6));
737 // Add one top-level item for extension 2.
738 Extension* extension2 = AddExtension("2222");
739 MenuItem* item7 = CreateTestItem(extension2);
740 ASSERT_TRUE(manager_.AddContextItem(extension2, item7));
742 const MenuItem::ExtensionKey key1(extension1->id());
743 const MenuItem::ExtensionKey key2(extension2->id());
744 EXPECT_EQ(4u, manager_.MenuItems(key1)->size());
745 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
747 // Remove all context menu items with incognito true.
748 manager_.RemoveAllIncognitoContextItems();
749 EXPECT_EQ(2u, manager_.MenuItems(key1)->size());
750 EXPECT_EQ(1u, manager_.MenuItems(key2)->size());
753 } // namespace extensions